gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:18:33 30 4
gpt4 key购买 nike

我试图了解如何使用 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com