Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate an AST from a string of C++ using Clang?

I am trying to use Clang to manipulate C++ source-code, but I am having trouble discovering the API.

I would like to take a string of C++ source-code and generate an AST from it; something like:

auto myAst = clang::parse("auto x = 1 + 1;"); 

Is there a minimal working example of this?

like image 407
sdgfsdh Avatar asked May 17 '16 12:05

sdgfsdh


1 Answers

You can try the next code:

std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("auto x = 1 + 1;"));
TranslationUnitDecl *DC = AST->getASTContext().getTranslationUnitDecl();
if (DC) {
   llvm::errs() << "---------dump begin----------\n";
   DC->dump();
   llvm::errs() << "---------dump end----------\n";
}
like image 129
xawi2000 Avatar answered Nov 28 '22 09:11

xawi2000