如何使用Clang从一串C ++生成AST?

如何使用Clang从一串C ++生成AST?

问题描述:

我正在尝试使用Clang来操纵C ++源代码,但是在发现API时遇到了麻烦。

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

我想获取一串C ++源代码并从中生成一个AST;

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?

您可以尝试下一个代码:

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";
}