Question

Is there some hidden documentation how the code-completion part of clang is implemented? What i've found out so far is that a special token (tok::code_completion) gets injected into the lexer and is handled in the parser. After observing such a token the parser can fill in possible completion strings.

What I don't understand:
If the called functionality decides that we can insert a variable which is available in the current context. How is such a case handled?

struct FooBar {
    void foo() {
        ba<<code completion here>>
    }
    void bar() {
    }
};

The parser hasn't seen bar but it's valid to call it.

Was it helpful?

Solution

As I see it, this is a general problem when parsing method definitions inside a struct, and is not specific to code completion. In any case, there's special handling in the parser precisely for this case, which you can find in the ParseCXXInlineMethods.cpp file.

From the comment on Parser::ParseCXXInlineMethodDef():

/// ParseCXXInlineMethodDef - We parsed and verified that the specified
/// Declarator is a well formed C++ inline method definition. Now lex its body
/// and store its tokens for parsing after the C++ class is complete.
Parser::DeclPtrTy
Parser::ParseCXXInlineMethodDef(...

And later, code for parsing the method definitions:

/// ParseLexedMethodDefs - We finished parsing the member specification of a top
/// (non-nested) C++ class. Now go over the stack of lexed methods that were
/// collected during its parsing and parse them all.
void Parser::ParseLexedMethodDefs(...

Thus the lexer-generated tokens for the function bodies are only parsed after the rest of the class was parsed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top