libclang parsing generates bad output

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

libclang parsing generates bad output

Zschemisch, Enrico

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

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)
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.


cfe-dev mailing list
[hidden email]