ASTContext: how to get an object or pointer of ASTContext

Hi all:
now I am trying to run my own ASTConsumer against AST, but not quite sure how to do it, especially how to get an object or pointer of ASTContext. BTW, ASTContext is required by ParseAST.
here is my code snippet:

You can just create an ASTContext on the stack, passing in the various other pieces (lang options, source manager, target info, etc.) when you construct it.

Have you considered using CompilerInvocation/CompilerInstance rather than building all of the pieces together?

  • Doug

Hi all:
now I am trying to run my own ASTConsumer against AST, but not quite sure how to do it, especially how to get an object or pointer of ASTContext. BTW, ASTContext is required by ParseAST.
here is my code snippet:

You can just create an ASTContext on the stack, passing in the various other pieces (lang options, source manager, target info, etc.) when you construct it.

Thank you. I finally compiled it through.my command:
g++ llvm-config --cxxflags --ldflags -fno-rtti -c Myfile.cpp -o Myfile.o
but when I generated the executable, unexpected errors occured. my command:
g++ llvm-config --cxxflasg --ldflags --libs core -o Myfile Myfile.o

errors like this:
undefined reference to ‘llvm::errs()’
undefined reference to ‘clang::Diagnostic::Diagnostic(clang::DiagnosticClient*)’
undefined reference to ‘clang::FileManager::FileManager()’

missing something?

Have you considered using CompilerInvocation/CompilerInstance rather than building all of the pieces together?

Yes, I knew. I will try it later. -:slight_smile:

You need to link against the Clang libraries. -lClangBasic -lClang -lClangAST and so on.

  • Doug

the same errors are still there. But when I use -lCIndex instead of -lclangBasic(etc.), it links successfully. Another error appears when it comes to running the executable(by ./Myfile).
error like this:
error while loading shared libraries: libCIndex.so: cannot open shared object file: No such file or directory