[cfe-dev] libclang parsing generates bad output

Zschemisch, Enrico enrico.zschemisch at verigy.com
Mon Apr 11 23:59:52 PDT 2011


Hi,

I am parsing some very simple C-header file with libclang. My program parses the translation unit and visits all nodes in the AST:


int main(int argc, char *argv[]) {
    CXIndex index = clang_createIndex(0, 0);

    const char *filename = "Node.h";

    CXTranslationUnit TU = clang_parseTranslationUnit(index, filename, NULL, 0, NULL, 0, CXTranslationUnit_None);

    CXCursor rootCursor = clang_getTranslationUnitCursor(TU);

    clang_visitChildren(rootCursor, printVisitor, NULL);

    clang_disposeTranslationUnit(TU);
    clang_disposeIndex(index);
    return 0;
}

And the visitor:
CXChildVisitResult printVisitor(CXCursor cursor, CXCursor parent, CXClientData client_data) {

    CXSourceRange range = clang_getCursorExtent(cursor);
    CXSourceLocation startLocation = clang_getRangeStart(range);
    CXSourceLocation endLocation = clang_getRangeEnd(range);

    CXFile file;
    unsigned int line, column, offset;
    clang_getInstantiationLocation(startLocation, &file, &line, &column, &offset);
    printf("Start: Line: %u Column: %u Offset: %u\n", line, column, offset);
    clang_getInstantiationLocation(endLocation, &file, &line, &column, &offset);
    printf("End: Line: %u Column: %u Offset: %u\n", line, column, offset);

    return CXChildVisit_Recurse;
}
When I run this code with an empty header file I get lots of weird output like this:
Parsing: empty.h
Kind: A typedef.
Start: Line: 0 Column: 0 Offset: 0
End: Line: 0 Column: 0 Offset: 0
Filename: (null)
or
Kind: A field (in C) or non-static data member (in C++) in a struct, union or C++ class.
Start: Line: 99 Column: 81 Offset: 3243
End: Line: 99 Column: 98 Offset: 3260
Filename: (null)

The very last output is a "First Expression" - so I assume I have to ignore everything until the first expression is found and start working from there?

Any help appreciated.

Thanks,
Enrico

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110412/14f06b1a/attachment.html>


More information about the cfe-dev mailing list