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

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

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

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