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

痞子三分冷 提交于 2019-12-10 02:39:54

问题


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:


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


来源:https://stackoverflow.com/questions/37276015/how-do-i-generate-an-ast-from-a-string-of-c-using-clang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!