如何使用Clang从一串C ++生成AST? [英] How do I generate an AST from a string of C++ using Clang?

查看:862
本文介绍了如何使用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";
}

这篇关于如何使用Clang从一串C ++生成AST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆