如何解析 c++ 的集合头文件?
我正在一个项目中工作,我想在 C++ 中进行反射,所以经过研究,我发现最好的方法是解析头文件以获取 XML 格式的抽象语法树并在反射中使用它。我尝试了很多工具,但没有一个与 Visual C++ 2008 或 Visual C++ 2010 兼容,如 coco、cint、gccxml。请尽快重播
I am working in a project and I want to do reflection in C++ so after research I found that the best way is to parse header files to get abstract syntax tree in XML format and use it in reflection. I tried many tools but none of them compatible with visual c++ 2008 or visual c++ 2010 like coco, cint, gccxml. please replay soon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Visual Studio 已解析项目中的所有代码(IntelliSense 功能)。您可以使用 Visual C++ 代码模型进行访问。
Visual Studio already parses all code in your project (IntelliSense feature). You can use Visual C++ Code Model for access.
我们的 C++ 前端 能够解析许多 C++ 方言,包括 GNU 和 MS 。它为 AST 和符号表构建编译器数据结构,并提供 C++“反射”所需的信息。将解析树导出为 XML 文档相当简单。通过遍历符号结构,可以将符号表信息导出为 XML。
人们似乎总是想要 XML 格式的 AST 和符号表数据,我猜是假设他们可以将其读入 DOM 结构或使用 XSLT 对其进行操作。这个想法有两个严重的缺陷:1) XML 数据的绝对数量是巨大的,生成/重新读取它只会增加大量时间 2) 拥有这些可用的结构将使做某事变得“容易” ....
我们认为人们真正想做的是分析代码和/或转换代码(通常基于分析)。这要求该工具,无论是什么,都以一种“更容易”分析和转换的方式提供对程序结构的访问。例如,如果您决定修改 AST,您将如何重新生成源文本?
我们构建了 DMS 软件重组工具包来提供完全通用的支持解析、分析、转换、漂亮打印(“重新生成源”)。 DMS 具有适用于多种语言(C++、C、Java、COBOL、Python 等)的前端,并提供了一组可用于在代码上构建自定义分析器/转换的标准服务。冒着大胆的风险,我们花了很长时间考虑实现有用的机制来完成这组任务,就像 MS 花了很长时间来确定 Windows 中应该有什么一样。你可以尝试复制这个机制,但预计它会花费巨大的成本(我们已经在 DMS 上工作了 15 年),或者你可以闭上眼睛,假装你可以拼凑足够多的东西来完成你想象你需要做的事情(大多数情况下,您会发现这在实践中是不够的)。
由于对“程序操作服务”的普遍需求,我们的 C++ 前端托管在 DMS 之上。
具有 C++ 前端的 DMS 已用于构建各种标准软件工程工具(测试覆盖率、分析器)以及对代码进行大规模更改(网站上有一篇关于如何使用 DMS 大规模重新构建飞机任务的论文)软件)。
2014 年 7 月 8 日编辑:我们的前端现在可以处理完整的 C++11 和部分 C++14,包括函数/过程/方法的控制和数据流。
Our C++ front end is capable of parsing many dialects of C++, including GNU and MS. It builds compiler data structures for ASTs and symbol tables with the kind of information needed to "do reflection" for C++. It is rather trivial to export the parse tree as an XML document. The symbol table information could be exported as XML by walking the symbol structure.
People always seem to want the AST and symbol table data in XML format, I guess under the assumption that they can read it into a DOM structure or manipulate it with XSLT. There are two serious flaws to this idea: 1) the sheer volume of the XML data is enormous, and generating/rereading it simply adds a lot of time 2) that having these structures available will make "easy" to do ...something....
What we think people really want to do is to analyze the code, and/or transform the code (typically based on an analysis). That requires that the tool, whatever it is, provide access to the program structure in a way that makes is "easier" to analyze and, well, transform. For instance, if you decide to modify the AST how will you regenerate the source text?
We have built the DMS Software Reengineering Toolkit to provide exactly the kind of general purpose support to parse, analyze, transform, prettyprint ("regenerate source"). DMS has front ends for a wide variety of languages (C++, C, Java, COBOL, Python, ...) and provides the set of standard services useful to build custom analyzers/transformations on code. At the risk of being bold, we have spent a long time thinking about implementing useful mechanisms to cover this set of tasks, in the same way that MS has spent a long time determining what should be in Windows. You can try to replicate this mechanism but expect it to be a huge cost (we have been working on DMS for 15 years), or you can close your eyes and pretend you can hack together enough to do what you imagine you need to do (mostly what you'll discover is that it isn't enough in practice).
Because of this general need for "program manipulation services", our C++ front end is hosted on top of DMS.
DMS with the C++ front end have been used to build a variety of standard software engineering tools (test coverage, profilers) as well as carry out massive changes to code (there's a paper at the webiste on how DMS was used to massively rearchitect aircraft mission software).
EDIT 7/8/2014: Our Front end now handles full C++11, and parts of C++14, including control and dataflow for functions/procedures/methods.