C ++ libclang: ¿Recuperar el cursor de CXSourceLocation devolviendo el cursor incorrecto?
Actualmente estoy escribiendo un simple detector de clones usando libclang con C ++.
El programa almacena los cursores utilizando una estructura, que contiene un puntero a la unidad de traducción y la ubicación de CXSource obtenida al llamar a clang_getCursorLocation (cursor).
typedef struct {
CXTranslationUnit* tu;
CXSourceLocation srcLoc;
} t_cursorLocation;
Por el bien de este error, la función de visitante secundario visita cada nodo y crea una estructura desde cada cursor. Con una estructura de tipo t_cursorLocation, escribí esta función para recuperar el cursor correspondiente:
CXCursor getCursor(t_cursorLocation *loc1) {
return clang_getCursor(*loc1->tu, loc1->srcLoc);
}
Sin embargo, con algunos cursores, cuando creo la estructura t_cursorLocation y la uso para recuperar el cursor desde el que se creó, el cursor recuperado no es igual al cursor del que se originó. Como ejemplo, vea la función visitante infantil:
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;
}
Mi función principal es la siguiente:
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;
}
El código fuente ficticio en el que ejecuté esto es el siguiente:
void goo() {
// nothing here
}
void foo() {
// do something
int a;
switch (a) {
case 0:
goo();
};
}
Sin embargo, la salida es consistente, lo que sugiere que esto solo ocurre con ciertos tipos de cursor.
¿Es esto un error o hay algo que me falta o que estoy haciendo mal?
Gracias de antemano, Jacob