llvm - 为什么 libclang 不返回有意义的完成结果?

标签 llvm clang llvm-clang libclang

我试图了解如何使用 libclang 完成代码。我看过“超越编译器的思考”,并且查看了 c-index-test,我发现了一个简单的示例程序 here

我编译了该程序,并在我制作的示例文件上运行它,该文件类似于视频中的文件:

struct List {
    int Data;
    struct List *Next;
};

int sumListNode(struct List *Node) {
    int result = 0;
    for (; Node; Node = Node->Next)
        result = result + Node->
}

void test() {
    sumLi
}

如果我将程序指向 Node-> 之后的第一个不完整的空格,它会输出一些 C 关键字,但不会像视频中所说的那样输出 Next 或 Data。

如果我将其指向 sumLi 之后的空格,它会打印出相同的 C 关键字。如果我将它指向 sumLi 中具有“s”的列,我可以让它打印出 sumListNode,但即使如此,它也会将其分配为与其他关键字相同的优先级值,因此它实际上只是打印出我所输入的所有内容可以放在那里,而不是阅读光标下的内容并尝试做出明智的猜测。我只是捕获救命稻草,希望将光标放在片段的开头而不是结尾会有所帮助。

我已经从 doxygen 和 c-index-test 中学到了很多关于 libclang 可以提供的数据类型以及如何使用它进行操作的知识,但我只是还没学会如何制作它给我相关数据,以便我有工作可做。

最佳答案

首先,您应该尝试打印任何 CXDiagnostic由翻译单元输出,因为任何错误都可能导致 clang 在您的代码中丢失(在您提到的非常简单的情况下这不太可能)。

其次,请注意 libclang 定义行号和列号的方式可能与您习惯的方式不同(即,如果您从文本编辑器获取行/列信息,则可能需要将列号加 1与 clang 的定义保持同步)。

第三,您可以使用 clang 编译器本身来测试编译选项和行/列信息的有效性。这样您就可以消除基于 libclang 的代码带来的不确定性。您可以例如使用以下命令行:

clang++ -cc1 -fsyntax-only -code-completion-at FILENAME:LINE:COL CLANG_ARGS
<小时/>

另请注意 clang_codeCompleteAt仅在 token 开头调用,并生成所有可能 token 的列表,客户端负责使用已在文本编辑器中输入的潜在部分 token 来过滤结果。

来自文档(重点是我的):

Perform code completion at a given location in a translation unit.

This function performs code completion at a particular file, line, and column within source code, providing results that suggest potential code snippets based on the context of the completion. The basic model for code completion is that Clang will parse a complete source file, performing syntax checking up to the location where code-completion has been requested. At that point, a special code-completion token is passed to the parser, which recognizes this token and determines, based on the current location in the C/Objective-C/C++ grammar and the state of semantic analysis, what completions to provide. These completions are returned via a new CXCodeCompleteResults structure.

Code completion itself is meant to be triggered by the client when the user types punctuation characters or whitespace, at which point the code-completion location will coincide with the cursor. For example, if p is a pointer, code-completion might be triggered after the "-" and then after the ">" in p->. When the code-completion location is afer the ">", the completion results will provide, e.g., the members of the struct that "p" points to. The client is responsible for placing the cursor at the beginning of the token currently being typed, then filtering the results based on the contents of the token. For example, when code-completing for the expression p->get, the client should provide the location just after the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the client can filter the results based on the current token text ("get"), only showing those results that start with "get". The intent of this interface is to separate the relatively high-latency acquisition of code-completion results from the filtering of results on a per-character basis, which must have a lower latency.

以修改后的第二个示例为例:

int main (int argc, char **argv) {
  int i = sumLi
  //      ^
}

应在标记位置(即标记的开头)调用代码完成。然后,Clang 可以给出一长串结果,例如:

  • argc
  • sumListNode(<# struct List *Node #>)

然后您可以根据部分输入的 sumLi 过滤此列表。 token 并保留唯一相关的完成:sumListNode .

如果您了解 elisp,clang 的源代码包含 Emacs 的自动完成库,这是这种两级实现的一个很好的示例:

trunk/utils/clang-completion-mode.el

关于llvm - 为什么 libclang 不返回有意义的完成结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875800/

相关文章:

c++ - 错误:必须使用常量表达式初始化constexpr变量 'struct2Var'

c++ - 在 Linux 上为 clang 构建 libc++ 的认可方法是什么?

c++ - ADL 在 constexpr 函数中不起作用(仅限 clang)

c++builder - 使用 clang 分析器分析 Embarcadero RAD Studio 项目

llvm - 在 LLVM 3.2 中使用 GHC

llvm - 如何在 LLVM 中找到值的 "the"定义点?

linux - 使用-O0 编译linux kernel (4.4) bpf samples 导致错误

c++ - 如何使用 AST 进行自定义前端操作和 clang 静态分析

bash - 如何修复 LD_PRELOAD 中的 "ERROR: ld.so: object ' libfakeroot-sysv.so' 无法预加载(无法打开共享对象文件): ignored"?

llvm - 无法构建 LLVM 示例 |未知组件名称 : jit