我有一个指向函数调用的游标,原型在包含的头文件中声明.我想获得这样的声明的位置,但是在gdb输出中可以看到,在获取函数调用的类型之后,获取声明类型的游标失败了,如何正确?
(gdb) n
66 CXType mickey_type = clang_getCursorType(mickey_cursor);
(gdb) n
67 CXCursor mickey_decl_cursor = clang_getTypeDeclaration(mickey_type);
(gdb) n
68 CXSourceLocation mickey_decl_location = clang_getCursorLocation(mickey_decl_cursor);
(gdb) p mickey_type
$1 = {
kind = CXType_FunctionProto,
data = {0x102826cb0, 0x100a03d90}
}
(gdb) print mickey_decl_cursor
$2 = {
kind = CXCursor_NoDeclFound,
xdata = 0,
data = {0x0, 0x0, 0x0}
}
(gdb) q
Run Code Online (Sandbox Code Playgroud)
这是所有相关的源代码:
我写了一个测试程序(parse_ast.c)来解析ac源文件(tt.c)来看看libclang是如何工作的,输出是AST的层次结构:
这是测试文件:
/* tt.c */ // line 1
#include <unistd.h>
#include <stdio.h>
typedef ssize_t (*write_fn_t)(int, const void *, size_t);
void indirect_write(write_fn_t write_fn) { // line 7
(*write_fn)(1, "indirect call\n", 14);
}
void direct_write() { // line 11
write(1, "direct call\n", 12); // line 12 mising in the ast?
}
int main() { // line 15
direct_write();
indirect_write(write); // line 17 missing in the ast?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出显示如下:
...
...
inclusion directive at tt.c (2, 1) to (2, 20)
inclusion …Run Code Online (Sandbox Code Playgroud) 如果我在一个大项目中有多个文件,所有文件共享大量包含的头文件,有什么办法可以共享解析头文件的工作吗?我曾希望创建一个索引然后向其中添加多个 translationUnit 可以导致一些工作被共享 - 但是即使是(伪代码)
index = clang_createIndex();
clang_parseTranslationUnit(index, "myfile");
clang_parseTranslationUnit(index, "myfile");
Run Code Online (Sandbox Code Playgroud)
每次调用 parseTranslationUnit 似乎都花费了全部时间,性能并不比
index1 = clang_createIndex();
clang_parseTranslationUnit(index1, "myfile");
index2 = clang_createIndex();
clang_parseTranslationUnit(index2, "myfile");
Run Code Online (Sandbox Code Playgroud)
我知道有专门的函数可以重新解析完全相同的文件;然而,我真正想要的是解析“myfile1”和“myfile2”可以共享解析“myheader.h”的工作,并且重新解析特定的函数在那里无济于事。
作为一个子问题,重用索引和为每个翻译单元创建新索引之间有什么有意义的区别吗?
我有一个能够生成C语言代码的软件,希望在即时编译环境中使用。据我了解,LLVM / Clang是可行的方法,为了项目的可维护性,我想使用llvm和Clang的C API(libclang)。
我开始使用创建一个libclang上下文,clang_createIndex并使用一个翻译单元创建一个createTranslationUnitFromSourceFile(很高兴能够避免通过文件系统,而是将源代码作为字符串传递)。但是我几乎被困在那里。如何从libclang转换单元转到LLVM“执行引擎”,这似乎是JIT所需要的?还是使用C API甚至无法做到这一点?
我正在Eli Bendersky的这篇信息丰富(但有点过时)的教程的帮助下学习使用 Python + libclang 解析 C++ 文件。
我的目标是解析 C++ 文件并识别这些文件中存在的函数的函数边界。我期望建立一个这种形式的Python字典:
{<func_name>:(<func_start_loc>, <func_end_loc>), ...}
为此,我能够获取函数名称(用于cursor.spellingAST 节点CursorKind.FUNCTION_DECL)CursorKind.CXX_METHOD和起始位置(使用cursor.location)
我的问题是,如何获得函数位置的结束位置
运行时doxygen我收到以下错误:
doxygen: error while loading shared libraries: libclang.so.6: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我已经在 Ubuntu 17 上安装doxygen使用。在我有但没有。sudo apt install doxygen/usr/lib/x86_64-linux-gnulibclang-4.0.so.1libclang-5.0.so.1libclang.so.6
我尝试过重新安装 doxygen,但没有帮助。
我还尝试创建一个调用libclang.so.6现有文件的符号链接,libclang-5.0.so.1如下所示,但它会导致此错误:
doxygen: /usr/lib/x86_64-linux-gnu/libclang.so.6: version `LLVM_6.0' not found (required by doxygen)
Run Code Online (Sandbox Code Playgroud)
如何解决此错误并使 doxygen 正常工作?
我正在尝试探索libclang(在Windows上)以基于AST分析“ stuff *”。我发现了几个例子,但我什么都做不了。
当我尝试运行此
index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1], args=['-x', 'c++'])
print len(tu.cursor.get_children())
Run Code Online (Sandbox Code Playgroud)
我遇到访问冲突,
File "C:\python27\lib\site-packages\clang\cindex.py", line 1783, in get_children
children)
WindowsError: exception: access violation writing 0x000000001D1B81A8
cindex.py在这里稍有不同(https://github.com/llvm-mirror/clang/tree/master/bindings/python),但给出的结果相同,只是一行不同。
此外,似乎有些杰出的错误(https://bugs.llvm.org/show_bug.cgi?id=13907)听起来非常相似...尽管现在已经有好几年了。
这样,当前在Windows上有人使用吗? 看来这可能在某个时候起作用了...
似乎libclang是推荐的方法,而python易于使用,但似乎不起作用。
*注意:按东西,我想使用属性来生成序列化函数或绑定,以标记类/结构。
LLVM 7.0(和6.0)
Windows 10
python 2.7 x64
我正在尝试使用 libClang 解析函数属性,但到目前为止还没有成功。
例如,我可能在标题中定义了这样的属性。
#define __mobile__ __attribute__((mobile))
Run Code Online (Sandbox Code Playgroud)
在一个单独的 .cpp 文件中,我有我的函数定义:
<template T>
__mobile__ void run(T int){...}
Run Code Online (Sandbox Code Playgroud)
我花了一天时间玩弄 libClang 库并通过它与 C++ 进行交互。
到目前为止,我还无法判断该函数是否包含这样的属性。方法 clang_Cursor_hasAttrs 为游标运行返回 0,这是 CXCursor_FunctionTemplate 类型。
有关此问题的完整讨论,请参见https://github.com/tingraldi/SwiftScripting/issues/18。
上面的链接有一个很长的Python脚本,该脚本将Objective-C File.h头文件转换为File.swift。为此,它使用libclang解析文件并将AST转换为Swift输出。
这是我们正在处理的头文件的缩写内容:
@interface FinderContainer : FinderItem
- (SBElementArray<FinderItem *> *) items;
- (SBElementArray<FinderContainer *> *) containers;
@end
Run Code Online (Sandbox Code Playgroud)
在具有Xcode 9.4.1和的macOS 10.13.6上pip install clang==3.5,脚本可以完美运行并生成正确的输出:
@objc public protocol FinderContainer: FinderItem {
@objc optional func items() -> SBElementArray
... // Truncated
}
Run Code Online (Sandbox Code Playgroud)
但是,在安装了Xcode 10且相同的macOS Mojave上pip install clang==3.5,缺少函数的返回值:
@objc public protocol FinderContainer: FinderItem {
@objc optional func items()
... // Truncated
}
Run Code Online (Sandbox Code Playgroud)
注意:我已经测试了clang 3.5和最新版本:6.0.0.2。两者产生相同的结果。
python脚本的此部分使用函数/方法声明,并将其转换为Swift:
def emit_function(self, cursor):
# ----LOGGING ADDITIONS …Run Code Online (Sandbox Code Playgroud) I have been trying to build the Canvas Data Loader. I've gone as far as step 10 and it's been pretty easy for me to resolve dependencies on a cargo build fail, but I've been stuck trying to resolve the librocksdb-sys v5.14.2 dependency for the past 4 days:
[root@localhost home]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin …Run Code Online (Sandbox Code Playgroud) libclang ×10
clang ×5
llvm ×3
python ×3
c++ ×1
clang++ ×1
doxygen ×1
jit ×1
llvm-c++-api ×1
llvm-clang ×1
objective-c ×1
python-2.7 ×1
rhel ×1
rocksdb ×1
rust ×1
rust-cargo ×1
swift ×1
ubuntu ×1
windows ×1