如何使用 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?
答
可以试试下一个代码:
std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("auto x = 1 + 1;"));
TranslationUnitDecl *DC = AST->getASTContext().getTranslationUnitDecl();
if (DC) {
llvm::errs() << "---------dump begin----------
";
DC->dump();
llvm::errs() << "---------dump end----------
";
}