User forums > Help

C++ comments inside generated C code?

<< < (2/4) > >>

ToApolytoXaos:

--- Quote from: Alpha on October 15, 2013, 04:56:54 pm ---Try: src/plugins/codecompletion/codecompletion.cpp line 1601 and src/plugins/contrib/SmartIndent/SmartIndentCpp.cpp line 566.

--- End quote ---
First of all, Alpha thank you so much for pointing out which files and lines should get implemented; much appreciated mate.

My concept from mixing C code with C++ is to use preprocessor conditionals, but I don't think it would work, as the existing code will compile with C++ and not with C.

For example, at codecompletion.cpp lines 1600-1602 we see the following:


--- Code: ---if (!pp.GetMatch(control->GetLine(ppLine), 2).IsEmpty())
    itemText.Append(wxT(" // ") + pp.GetMatch(control->GetLine(ppLine), 2));
break;

--- End code ---

Now, even if you try something like the following below, it won't work as it depends on C++ in order to compile wxWidgets:


--- Code: ---if (!pp.GetMatch(control->GetLine(ppLine), 2).IsEmpty())
    #if defined(__cplusplus)
    itemText.Append(wxT(" // ") + pp.GetMatch(control->GetLine(ppLine), 2));
    #else
    itemText.Append(wxT(" /* ") + pp.GetMatch(control->GetLine(ppLine), 2) + wxT(" */ "));
    #endif
break;

--- End code ---

What would be your way to ask the system to check whether it's C or C++?

Cheers.

BlueHazzard:
you can't use the preprocessor here, because c::b is always compiled with c++, so your c condition will never be true.
I think you have to check for the file extension, and if it is c use the c style comment and if it is cpp use the c++ comment.
[Edit:] or only check for .c extension and use c++ comment otherwise because c++ has many extensions... In the header files i have no idea...

oBFusCATed:
The check should ask the editor to see if it has C++ or C lexing, I think there is a global function that can tell what is the language somewhere in the global  headers.

ToApolytoXaos:
I think I got confused a bit right now.


* Does C::B use two separated parsing mechanisms or just one, that of codecompletion project?
* I have checked the wiki pages and the only parsing mechanism I could find is that of codecompletion. Any suggestions?
* Is Scintilla's lexer mechanism related to this request?
I wish I could find more information...

Alpha:
There is no good/global way defined (yet) to determine between C and C++.  (Code::Blocks treats them as the same thing most of the time.)  This is how it is currently done:

--- Code: ---HighlightLanguage EditorColourSet::Apply(cbEditor* editor, HighlightLanguage lang)
{
    if (!editor)
        return HL_NONE;

    if (lang == HL_AUTO)
        lang = GetLanguageForFilename(editor->GetFilename());

    const bool isC = (   Manager::Get()->GetConfigManager(wxT("editor"))->ReadBool(wxT("no_stl_in_c"), true)
                      && lang == GetHighlightLanguage(wxT("C/C++"))
                      && editor->GetFilename().Lower().EndsWith(wxT(".c")) );

    Apply(lang, editor->GetLeftSplitViewControl(),  isC);
    Apply(lang, editor->GetRightSplitViewControl(), isC);

    return lang;
}

--- End code ---

--- Code: ---void CodeCompletion::UpdateEditorSyntax(cbEditor* ed)
{
    if (!Manager::Get()->GetConfigManager(wxT("code_completion"))->ReadBool(wxT("/semantic_keywords"), false))
        return;
    if (!ed)
        ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
    if (!ed || ed->GetControl()->GetLexer() != wxSCI_LEX_CPP)
        return;

    TokenIdxSet result;
    int flags = tkAnyContainer | tkAnyFunction;
    if (ed->GetFilename().EndsWith(wxT(".c")))
        flags |= tkVariable;
    m_NativeParser.GetParser().FindTokensInFile(ed->GetFilename(), result, flags);
[...]
}

--- End code ---

--- Code: ---void EditorManager::CollectDefines(CodeBlocksEvent& event)
{
[...]
    defines.Add(wxT("__cplusplus"));
    for (FilesList::iterator it = lst->begin(); it != lst->end(); ++it)
    {
        if ((*it)->relativeFilename.EndsWith(wxT(".c")))
        {
            defines.RemoveAt(defines.GetCount() - 1); // do not define '__cplusplus' if even a single C file is found
            break;
        }
    }
[...]
}

--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version