How do I generate an AST from a string of C++ usin

2020-08-20 06:39发布

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?

1条回答
我只想做你的唯一
2楼-- · 2020-08-20 07:40

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";
}
查看更多
登录 后发表回答