C ++ libclang: Cursor von CXSourceLocation abrufen und falschen Cursor zurückgeben?

Ich schreibe gerade einen einfachen Klondetektor mit libclang in C ++.

Das Programm speichert Cursor mithilfe einer Struktur, die einen Zeiger auf die Übersetzungseinheit und die CXSourceLocation enthält, die durch den Aufruf von clang_getCursorLocation (cursor) erhalten wurden.

typedef struct {
    CXTranslationUnit* tu;
    CXSourceLocation srcLoc;
} t_cursorLocation;

Aus diesem Grund besucht die untergeordnete Besucherfunktion jeden Knoten und erstellt aus jedem Cursor eine Struktur. Mit einer Struktur vom Typ t_cursorLocation habe ich diese Funktion geschrieben, um den entsprechenden Cursor abzurufen:

CXCursor getCursor(t_cursorLocation *loc1) {
    return clang_getCursor(*loc1->tu, loc1->srcLoc);
}

Bei einigen Cursorn entspricht der abgerufene Cursor jedoch nicht dem Cursor, von dem er stammt, wenn ich die Struktur t_cursorLocation erstelle und ihn zum Abrufen des Cursors verwende, von dem er erstellt wurde. Ein Beispiel finden Sie in der Funktion für untergeordnete Besucher:

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;
}

Meine Hauptfunktion ist wie folgt:

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;
}

Der Dummy-Quellcode, mit dem ich dies ausgeführt habe, lautet wie folgt:

void goo() {
    // nothing here
}

void foo() {
    // do something
    int a;
    switch (a) {
        case 0:
            goo();
    };
}

Die Ausgabe ist jedoch konsistent, was darauf hindeutet, dass dies nur bei bestimmten Cursortypen der Fall ist.

Ist das ein Fehler oder fehlt mir etwas oder mache ich etwas falsch?

Danke im Voraus, Jacob

Antworten auf die Frage(1)

Ihre Antwort auf die Frage