- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在使用 libclang 和 C++ 编写一个简单的克隆检测器。
程序使用结构存储游标,包含指向翻译单元的指针和通过调用 clang_getCursorLocation(cursor) 获得的 CXSourceLocation。
typedef struct {
CXTranslationUnit* tu;
CXSourceLocation srcLoc;
} t_cursorLocation;
为了这个错误,子访问者函数访问每个节点并从每个游标创建一个结构。使用 t_cursorLocation 类型的结构,我编写了这个函数来检索相应的光标:
CXCursor getCursor(t_cursorLocation *loc1) {
return clang_getCursor(*loc1->tu, loc1->srcLoc);
}
但是,对于某些游标,当我创建 t_cursorLocation 结构并使用它来检索从中创建的游标时,检索到的游标不等于它的来源游标。例如,查看子访问者函数:
CXChildVisitResult traverseAST(CXCursor cursor, CXCursor parent,
CXClientData client_data) {
CXTranslationUnit tu = clang_Cursor_getTranslationUnit(cursor);
CXTranslationUnit tu2 = *((CXTranslationUnit *) client_data);
t_cursorLocation *loc = new t_cursorLocation();
loc->tu = &tu;
loc->srcLoc = clang_getCursorLocation(cursor);
CXCursor c2 = getCursor(loc);
printf("CursorKind\t%s\n",
clang_getCString(clang_getCursorKindSpelling(cursor.kind)));
if (clang_equalCursors(cursor, c2)) {
printf("Noooo - the cursors do not match! Next test.....");
// use translation unit passed as client_data to see if
// there's a difference
loc->tu = &tu2;
c2 = getCursor(loc);
if (clang_equalCursors(cursor, c2)) {
printf("FAILED ALSO!\n");
} else {
printf("PASSED???\n");
}
} else {
printf("We have a match!\n");
}
return CXChildVisit_Recurse;
}
我的主要功能如下:
int main(int argc, char **argv) {
CXIndex index = clang_createIndex(0, 0);
// initialise the translation unit
CXTranslationUnit tu = clang_parseTranslationUnit(index, 0,
argv, argc, 0, 0, CXTranslationUnit_None);
// set the client data in traverseAST
CXClientData data = &tu;// NULL;
// get the root cursor for the translation unit
CXCursor rootCursor = clang_getTranslationUnitCursor(tu);
clang_visitChildren(rootCursor, traverseAST, data);
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index);
return 0;
}
我运行的虚拟源代码如下:
void goo() {
// nothing here
}
void foo() {
// do something
int a;
switch (a) {
case 0:
goo();
};
}
但是输出是一致的,这表明这只发生在某些游标类型上。
这是一个错误还是我遗漏了什么或做错了什么?
提前致谢,雅各布
最佳答案
要么我完全没有理解您的观点,要么您以错误的方式使用了 clang_equalCursors
:当两个游标相等时,clang_equalCursors
返回一个非零值。这意味着我认为您正在测试游标不等式而不是等式。
现在,让我尝试解释为什么某些游标的行为明显不同于其他游标。每个光标只有一个源位置。但是,同一源位置可能有多个游标。例如考虑以下行:
CXIndex index = clang_createIndex(0, 0);
// ^
上面标记的位置应该至少有两个光标:
索引
index = clang_createIndex(0,0)
当您将源位置转换回游标时,clang_getCursor
会为您提供最具体的一个(在本例中为变量声明)。我怀疑这就是您在这种情况下发生的情况:getCursor(loc)
仅返回您正在访问的光标,前提是它在其位置最具体。
尝试打印每个光标的物理源位置(例如使用 clang_getCursorExtent 和 clang_getExpansionLocation )以了解发生了什么。
关于C++ libclang : Retrieving cursor from CXSourceLocation returning wrong cursor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15346521/
如果我在一个大项目中有多个文件,所有文件共享大量包含的头文件,有什么办法可以共享解析头文件的工作吗?我曾希望创建一个索引,然后向其中添加多个 translationUnits 可能会导致一些工作被共享
如果我在一个大项目中有多个文件,所有这些文件共享大量包含的头文件,有没有办法分担解析头文件的工作?我曾希望创建一个 Index 然后向其添加多个 translationUnits 可能会导致一些工作被
我使用 libclang 来解析源文件并获取对某种类型的引用,如 CXType ,假设它是“const std::__1::basic_string ”(由 clang_getTypeSpelling
我正在使用最新的 LibClang 来解析一些 C 头文件。我处理的代码来自 CXUnsavedFile 的(它都是动态生成的,没有任何内容存在于磁盘上)。例如: FileA.h 包含: struct
有时,当我尝试在Xcode中构建项目时(大约十分之一),它崩溃并退出。然后,我得到一个错误框,其中显示:“使用libclang.dylib插件时Xcode意外退出”。 Xcode有时也会随机崩溃,没有
我使用的是 Linux Mint,并且使用 Clang Complete 中的 makefile 安装了 clang_complete ,但它不起作用。当我打开 cpp 文件时,出现错误消息: Loa
我正在尝试使用 libclang 构建一个小型解析程序。 要解析的源文件(Node.h): #pragma once struct Node { int value; struct N
Currentlty 我正在做一个项目,用 libclang 转储 C++ 代码的类信息。还有一些关于类型限定符的悲惨经历:const、volatile、&、&& 以及它们的组合。以下是转储函数删除参
有什么方法可以从 libclang 中获取信息,了解源文件中的 C++ 代码是否具有正确的语法?即使使用无效的 C++ 代码,Libclang 也会尝试创建翻译单元。 最佳答案 一般问题的一般答案是肯
如何使用 libclang 获取原始文字的值? 例如,如果我有一个游标类型为 CXCursor_IntegerLiteral 的 CXCursor,我该如何提取文字值。 更新: 我在使用 libcla
我有以下使用 clang-c API 的代码。 #include #include #include CXChildVisitResult printVisitor(CXCursor curso
我有一个 c++ 文件,它会更改,其余所有头文件保持不变。但是每当我重新解析一个翻译单元时,libclang 最终会消耗大量的 cpu 和 ram。虽然它使用预编译头和所有(我可以看到生成的前导文件)
class aclass{ public: int num; }; int main() { aclass *ok; ok->num = 4; return 0; }
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 4年前关闭。 Improve this qu
我正在使用 LibClang 列出所有函数调用及其相应的定义。 以下是执行此操作的python脚本: def traverse(node): if node.kind == CALL_EXPR:
我尝试使用 libClang 解析 C++ 方法,但在尝试获取函数的参数/参数时,有时会给出错误的类型。 例子: 我有两种不同的方法 std::string Method::exportMethod(
我尝试使用 libClang 解析 C++ 方法,但在尝试获取函数的参数/参数时,有时会给出错误的类型。 例子: 我有两种不同的方法 std::string Method::exportMethod(
我试图了解如何使用 libclang 完成代码。我看过“超越编译器的思考”,并且查看了 c-index-test,我发现了一个简单的示例程序 here 我编译了该程序,并在我制作的示例文件上运行它,该
我需要解析一个 C++ 代码文件并找到其中所有具有完全限定名称的函数调用。我正在使用 libclang 的 Python 绑定(bind),因为它似乎比编写我自己的 C++ 解析器更容易,即使文档很少
我正在学习使用 Python + libclang 解析 C++ 文件,并借助 Eli Bendersky 提供的这个信息量很大(但有点过时)的教程. 我的目标是解析 C++ 文件并识别这些文件中存在
我是一名优秀的程序员,十分优秀!