Clang代码自动补全 - 实现设计

4

是否有一些隐藏的文档介绍了clang的代码自动补全功能是如何实现的?目前我所了解的是,一个特殊的词元(tok::code_completion)会被注入到词法分析器中,并在语法分析器中进行处理。在观察到这样的词元后,语法分析器可以填入可能的自动补全字符串。

我的疑问是:
如果所调用的功能决定我们可以插入当前环境中可用的变量,那么这种情况是如何处理的呢?

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

解析器没有看到bar,但调用它是有效的。
1个回答

4
据我看来,这是在解析结构体内的方法定义时普遍存在的问题,而不仅仅是代码补全的问题。无论如何,解析器中有特殊处理,专门针对这种情况,你可以在ParseCXXInlineMethods.cpp文件中找到它。

根据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(...

接下来,解析方法定义的代码:

/// 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(...

因此,函数体的词法分析生成的标记仅在解析完类的其余部分后进行解析。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接