问题标签 [libclang]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
628 浏览

xcode - 如何在 Xcode 中使用 libclang

我想使用 Xcode 附带的 libclang 库来做一些简单的源代码解析。

我就是这样做的。

  1. echo使用xcode-select --print-path找出 libclang.dylib/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib
  2. 将 libclang.dylib 文件复制到我的项目文件夹中。
  3. 将它添加到一个干净的 Xcode 项目(一个 Cocoa 应用程序)。
  4. 构建并运行应用程序

构建的进程运行正常,但应用程序在启动时崩溃并出现此错误

添加/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib到运行路径并不能解决问题。

我知道这个问题可能已经被问过很多次了,但是在谷歌上搜索了一整天之后,我还没有找到答案:(

提前致谢!

0 投票
1 回答
534 浏览

c++11 - 如何使用 libclang 检测枚举和作用域枚举之间的区别?

我一直在使用出色的 C 接口 libclang ( http://clang.llvm.org/doxygen/group__CINDEX.html ) 编写 C++ AST 解析器。不幸的是,C++ 11 范围内的枚举和老式枚举之间似乎没有歧义:两者都具有 CXCursor_EnumDecl 的游标类型和 CXType_Enum 的类型,即相同。

我曾尝试拜访孩子们,看看他们的父母类型是否不同 - 遗憾的是没有。我尝试询问基础类型,我得到了两者的整数。我检查了 Enum 之后声明的所有项目,看看是否可能出现老式 Enum 的 bind 或 typedef,同样没有明显区别。

我开始认为我一定是错过了什么。我是否必须使用代码完成 API 来确定它是哪种枚举?

0 投票
1 回答
1882 浏览

python - Python clang 不搜索系统包含路径

在 Python 中使用 libclang 时,它似乎不会自动搜索系统的包含路径。

有没有可靠的方法来获得这些路径?我不喜欢硬编码路径,因为我正在编写将在各种 UNIX 系统上运行的代码。

例如,给定 test.cpp

和 test.py

运行python test.py将打印:

当然,我可以通过做找到系统包含路径

并添加"-Isome/path"parse参数列表中,即

这实际上有效并且不会产生错误。

但是,这不是可移植的,如果我能以编程方式让 clang 自动使用它们,那就太好了。

0 投票
1 回答
201 浏览

c - 使用 libclang 在 for 循环中查找条件评估

我正在使用 clang(通过 Python 绑定通过 libclang)来组合一个代码审查机器人。我一直假设所有 FOR_STMT 游标都有 4 个孩子;INIT、EVAL、INC 和 BODY..

这意味着我可以使用 python 中的某些内容检查评估表达式的内容,例如:

这种方法似乎……一开始不太好,但我只是因为 libclang 而接受了它,尤其是 Python 绑定,相当稀疏。但是我最近注意到一个像这样的循环:

将只有 3 个孩子 - 现在评估将是第一个而不是第二个。我一直认为 libclang 只会为 FOR_STMT 的任何未使用部分返回 NULL_STMT ......显然,我错了。

解析 FOR_STMT 的正确方法是什么?我在 libclang 中找不到任何有用的东西。

更新:通过 libclang 源代码,看起来这 4 个组件是使用访问者对象从 clang::ForStmt 类中添加的。ForStmt 对象应该返回空语句对象,但是某处的某个层似乎正在将这些从访问节点向量中剥离出来......?

0 投票
2 回答
5275 浏览

c++ - Faster code-completion with clang

I am investigating potential code-completion speedups while using clang's code-completion mechanism. The flow described below is what I found in rtags, by Anders Bakken.

Translation units are parsed by a daemon monitoring files for changes. This is done by called clang_parseTranslationUnit and related functions(reparse*, dispose*). When the user requests a completion at a given line and column in a source file, the daemon passes the cached translation unit for the last saved version of the source file and the current source file to clang_codeCompleteAt. (Clang CodeComplete docs).

The flags passed to clang_parseTranslationUnit(from CompletionThread::process, line 271) are CXTranslationUnit_PrecompiledPreamble|CXTranslationUnit_CacheCompletionResults|CXTranslationUnit_SkipFunctionBodes. The flags passed to clang_codeCompleteAt(from CompletionThread::process, line 305) are CXCodeComplete_IncludeMacros|CXCodeComplete_IncludeCodePatterns.

The call to clang_codeCompleteAt is very slow - it takes around 3-5 seconds to obtain a completion even in the cases where the completion location is a legitimate member access code, a subset of the intended use case mentioned in the documentation of clang_codeCompleteAt. This seems way too slow by IDE code-completion standards. Is there a way of speeding this up?

0 投票
0 回答
905 浏览

c++ - 将 clang 编译器嵌入 OS X Cocoa App

作为我的生成 C 源代码的应用程序开发的一部分,我需要能够将编译器嵌入到我的应用程序中,以便它可以生成可执行的目标代码。由于这是一款适用于 Mac App Store 的沙盒应用程序,因此我无法NSTask直接调用clang,因为独立工具显然无法在沙盒下工作。遗憾。

我遇到libclang了编译器的库版本,它似乎可以解决问题。使用此处的信息,我能够获取一个预配置的libclang二进制文件以集成到我的应用程序中。麻烦的是,我似乎无法弄清楚libclangAPI 调用是什么进程并生成可执行文件。我找到了这个示例代码:

不幸的是,它不会编译。我收到这些错误:

我的错误

也许我没有包含正确的头文件,我不知道。但鉴于缺少 的文档libclang,我将非常感谢一些帮助。

0 投票
0 回答
154 浏览

python - pip package: proper way of compiling code that depends on libclang

I am building a python library, that I want to be installable via pip. The installation process requires a cpp file to be compiled, and that cpp file depends on libclang (in particular, it includes some of clang-c header files, and needs to be linked against libclang.so).

I am assuming that the end user has clang++ installed. However, I don't know where that installation is. For example, when I installed clang++ locally, even though it installed all the headers and the library I need, if I just compile a blank C++ file that has

It won't find it. I need to explicitly provide the path via command-line argument or CPLUS_INCLUDE_PATH.

Now I need some way for the script that pip invokes to find those headers. I obviously can ask the user to set CPLUS_INCLUDE_PATH and LD_LIBRARY_PATH to include paths to clang++ before running the installation process, but it seems ugly. I can add headers and the library into my package, but then I would rather it build against the version that the user has. Is there a way to find a clang++ installation if a user has one (or at least if he installed one via apt-get or another package manager?), or in general, what is the correct way of solving this issue when building a pip package?

0 投票
0 回答
180 浏览

c++ - Clang c-api 总内存使用量

如何获取给定翻译单元的 clang c-api 的总内存使用量?

有,clang_getCXTUResourceUsage但它返回的内存使用情况分为条目,其中每个条目描述特定类别。

UPDATE1:
文档CXTUResourceUsageEntry::amount说明了依赖的单位,CXTUResourceUsageEntry::kind但没有说明如何。所以不清楚如何总结。

0 投票
1 回答
413 浏览

c++ - libclang 获取成员声明

现在当我做 clang_getCursorDefinition(cur_cursor); 在 o 开头的光标右侧 ok->num = 4; 它为 int num 提供光标;类里面。我希望它给我 aclass *ok; 的光标。main里面的声明。调用 clang_getCursorSemanticParent(cur_cursor) 似乎是错误的,好像我将声明放在 main() 函数内部或外部,它仍然返回 main() 作为语义父级。

有没有一种方法可以有效地做到这一点,而无需迭代每个可能的游标?

编辑:Clang 将制表符计为 1 个字符,这是正确的。我正在测试的编辑器自动将选项卡计数为 4 号,所以预期的第 2 列,我的编辑器作为第 5 列发送。现在 getCursorDefinition 正在按预期工作,并将我带到范围定义。

0 投票
3 回答
1382 浏览

vim - vim youcompleteme 找不到 cstdint

我在尝试使用带有 Unix 风格 Makefiles 的 CMake 和带有 youcompleteme 插件的 vim 在我的 Mac(带有 Xcode 6 的 Yosemite)上设置 C++ 项目时遇到了问题(我是 Linux 的老手和 Mac 新手,所以我更喜欢这个设置Xcode)。代码构建并运行,但 youcompleteme 抛出了一些虚假错误,我认为归结为它无法找到 <cstdint> 标头。

我也刚刚在Linux上尝试过,并且遇到了同样的问题。

我已将 .ycm_extra_conf.py 配置为使用 cake 生成的 compile_commands.json。compile_commands.json 中的“命令”行使用这些标志:

那里似乎没有明确引用包含 stdint 作为直接父级的任何目录。

有没有一种方法可以让 youcompleteme 用 libclang 完成它的工作,以便它可以隐式找到目录,这在命令行上运行 c++ 时似乎有效?或者,让 cmake 添加适当的系统头文件路径而不进行硬连线的最佳方法是什么?我希望我的 CMakeLists.txt 是可移植的并且能够应对工具链升级。

我的 .ycm_extra_conf.py 几乎是所提供示例的副本,稍作修改以找到我放置的 compile_commands.json。