我可以在不使用编译器的情况下使用 clang 获得 C/C++ 代码的 XML AST 转储吗? [英] Can I get an XML AST dump of C/C++ code with clang without using the compiler?

查看:30
本文介绍了我可以在不使用编译器的情况下使用 clang 获得 C/C++ 代码的 XML AST 转储吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法使用 cmake 和 Visual Studio 10 成功地为 windows 编译了 clang.我想获得一个 XML 文件作为源代码的 AST 表示.有一个选项可以在 linux (ubuntu) 下使用带有 gcc 的 clang 提供结果,但在 windows 框上不起作用:

I managed to compile successfully clang for windows with cmake and visual studio 10. I would like to get an XML file as AST representation of the source code. There is one option that provides the result with clang with gcc under linux (ubuntu) but doesn't work on the windows box:

clang -cc1 -ast-print-xml source.c

然而,这是调用编译阶段(我想避免).挖掘源代码对我没有帮助,因为我对 clang 还很陌生.我可以使用以下方法生成 AST 的二进制版本:

However, this is invoking the compilation stage (which I would like to avoid). Digging in the source code didn't help me so far as I am quite new to clang. I could manage to generate the binary version of the AST by using:

clang -emit-ast source.c

不幸的是,这种格式不能直接用于解析.是否有一些现有的方法可以直接生成 XML 树而不是 clang 中的二进制树?

Unfortunately, this format is unusable directly for parsing. Is there some existing method to directly generate the XML tree instead of a binary one in clang?

目标是在 .NET 环境中的其他工具中使用 XML 表示,因此我需要对本机 clang 库进行一些包装以访问二进制 AST.如果有人已经为 .NET 编写了一些二进制 clang AST 解析器,也许还有第三种选择?

The goal is to use the XML representation in other tools in the .NET environment so I would need to make some wrapping around the native clang lib to access the binary AST. Maybe there is a third option if someone already wrote some binary clang AST parser for .NET?

是否有可能我遗漏了一些东西,比如 clang 前端生成的 AST 与编译阶段生成的 AST 不等价.

Is it possible that I am missing something like if the AST generated by the clang front end is not equivalent to the one generated in the compilation stage.

推荐答案

请注意,Douglas Gregor(CLang FrontEnd 的负责人)已从 2.9 版本中删除了 XML 打印机.

For your information, the XML printer has been removed from the 2.9 version by Douglas Gregor (responsible of CLang FrontEnd).

问题在于缺少 XML 打印机.许多 AST 节点从未在打印机中实现,以及一些节点的许多属性,导致源代码的表示不准确.

The issue was that the XML printer was lacking. A number of the AST nodes had never been implemented in the printer, as well as a number of the properties of some nodes, which led to an inaccurate representation of the source code.

Douglas 提出的另一点是,输出应该不适合调试 CLang 本身(这就是 -emit-ast 的内容),而是适合外部工具使用.这要求输出从一个版本到另一个版本都保持稳定.值得注意的是,它不应该是 CLang 内部的一对一映射,而是将源代码翻译成标准化语言.

Another point raised by Douglas was that the output should be suitable not for debugging CLang itself (which is what the -emit-ast is about) but for consumption by external tools. This requires the output to be stable from one version to another. Notably it should not be a 1-on-1 mapping of CLang internal, but rather translate the source code into standarized language.

除非在打印机上有大量工作(需要志愿者),否则它不会被集成回来......

Unless there is significant work on the printer (which requires volunteers) it will not be integrated back...

这篇关于我可以在不使用编译器的情况下使用 clang 获得 C/C++ 代码的 XML AST 转储吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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