[cfe-dev] ASTs from multiple sources

Eli Friedman eli.friedman at gmail.com
Wed Aug 8 11:41:31 PDT 2012


On Wed, Aug 8, 2012 at 11:26 AM, Socrates Katsoulacos
<socrates.katsoulacos11 at imperial.ac.uk> wrote:
> 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
> page 10. A similar code is also
> here: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)}’

That error message is terrible, but you're trying to pass a member
function as a function pointer, which isn't allowed in C++.

-Eli




More information about the cfe-dev mailing list