c++ libclang:从CXSourceLocation检索游标,返回错误的游标
C++ libclang: Retrieving cursor from CXSourceLocation returning wrong cursor?
我目前正在用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("CursorKindt%sn",
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();
};
}
但是输出是一致的,这表明这种情况只发生在某些游标类型上。
这是一个bug还是有什么我错过或做错了?
提前感谢,雅各
要么我完全错过了你的观点,要么你以错误的方式使用clang_equalCursors
:当两个游标相等时,clang_equalCursors
返回非零值。这意味着我认为你在测试游标不等式而不是等式。
现在,让我试着解释为什么某些光标的行为明显不同于其他光标。每个游标只有一个源位置。但是,在同一源位置可能有多个游标。例如,考虑以下行:
CXIndex index = clang_createIndex(0, 0);
// ^
在上面标记的位置至少应该有两个游标:
- VarDecl:
index
- DeclRefExpr:
index = clang_createIndex(0,0)
当您将源位置转换回游标时,clang_getCursor
将给出最具体的位置(在本例中是变量声明)。我怀疑这就是在这种情况下发生在您身上的情况:getCursor(loc)
只在您正在访问的光标在其位置上是最特定的时才返回给您。
尝试打印每个光标的物理源位置(例如使用clang_getcursorexent和clang_getExpansionLocation),以了解发生了什么。
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何取消对nullptr的屏蔽,返回正确的对象
- 奇怪的结构&GCC&clang(void*返回类型)
- 架构决策:返回std::future还是提供回调
- 通过OCI调用Oracle存储过程,并在C++中使用out-ref游标返回结果
- c++ libclang:从CXSourceLocation检索游标,返回错误的游标
- SQLFetch()失败,返回SQLState 24000(无效游标)