[cfe-dev] ASTs from multiple sources

Socrates Katsoulacos socrates.katsoulacos11 at imperial.ac.uk
Wed Aug 8 11:26:53 PDT 2012


Dear Sir/Madam,

I am using clang to get the AST of a source file and then traverse it.
However, if you compile more than one files with clang invokes the compiler
multiple times and so the ASTs of the files cannot be shared. I have found
a solution to the web using indexes and *.pch files. The code is the
following:

    //visit AST
  CXChildVisitResult TranslationUnitVisitor(CXCursor cursor, CXCursor
parent, CXClientData data)
  {
return CXChildVisit_Recurse; // visit complete AST recursivly
  }

    // excludeDeclsFromPCH = 1, displayDiagnostics=1
    CXIndex cx_idx = clang_createIndex(1, 1);

  // test.pch was produced with the following command:
  // "clang -x c test.h -emit-ast -o test.pch"
  CXTranslationUnit TU = clang_createTranslationUnit(cx_idx, "test.pch");

  // This will load all the symbols from 'test.pch'
  clang_visitChildren(clang_getTranslationUnitCursor(TU),
TranslationUnitVisitor, 0);
  clang_disposeTranslationUnit(TU);

  // This will load all the symbols from 'test.c', excluding symbols
  // from 'test.pch'.
  char *args[] = { "-Xclang", "-include-pch=test.pch" };
  TU = clang_createTranslationUnitFromSourceFile(cx_idx, "test.c", 2,
args, 0, 0);

  //clang_visitChildren(clang_getTranslationUnitCursor(TU),
TranslationUnitVisitor,0);
  clang_disposeTranslationUnit(TU);

My problems is that it does not compile and I cannot understand why, since
this is a standard code taken from
http://wiki.ifs.hsr.ch/SemProgAnTr/files/Clang_Architecture_-*
ChristopherGuntli.pdf*<http://wiki.ifs.hsr.ch/SemProgAnTr/files/Clang_Architecture_-_ChristopherGuntli.pdf>
* page 10. A similar code is also
here:http://clang.llvm.org/doxygen/group<http://clang.llvm.org/doxygen/group__CINDEX.html#ga51eb9b38c18743bf2d824c6230e61f93>
*_CINDEX.html#ga51eb9b38c18743bf2d824c6230e61f93<http://clang.llvm.org/doxygen/group__CINDEX.html#ga51eb9b38c18743bf2d824c6230e61f93>
.

The error I am getting is the following:

Checker.cpp:211:85: error: argument of type ‘CXChildVisitResult
({anonymous}::TypeCheckingConsumer::)(CXCursor, CXCursor, CXClientData)
{aka CXChildVisitResult ({anonymous}::TypeCheckingConsumer::)(CXCursor,
CXCursor, void*)}’ does not match ‘CXCursorVisitor {aka CXChildVisitResult (
*)(CXCursor, CXCursor, void*)}’

I would be greatful for your help with this.

Socrates
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120808/caa246cc/attachment.html>


More information about the cfe-dev mailing list