如何从 c++ 中提取所有类型定义、结构和联合来源

发布于 2024-08-31 03:34:36 字数 988 浏览 4 评论 0原文

我继承了一个包含数百个文件的 Visual Studio 项目。

我想从每个 .h/.cpp 文件中提取所有 typedef、结构和联合,并将结果放入一个文件中)。

每个 typedef/struct/union 应该位于结果文件中的一行上。这将使排序变得更加容易。

typedef int myType;
struct myFirstStruct { char a; int b;...};
联合 Part_Number_Serial_Number_Part_2_Response_Message_Type {struct{Message_Response_Head_Type Head; Part_Num_Serial_Num_Part_2_Report_Array Part_2_Report; Message_Tail_Type Tail;} 数据; BYTE byData[140];}myUnion;
结构 { bool c; int d;...}mySecondStruct;

我的问题是,我不知道使用正则表达式要查找什么(typedef/结构/联合的语法)。 我不敢相信以前没有人这样做过(我用谷歌搜索并没有找到任何相关内容)。

有谁知道这些的正则表达式吗? (请注意,有些使用 // other /* */ 注释掉)

或完成此操作的工具。

编辑:
我正在考虑自动生成源代码和/或对话框来修改使用底层 typedef/struct/union 的消息。我打算使用输出生成一个可用于此原因的 XML 文件。

这些的源代码是 C/C++ 的,几乎在我所有的项目中都使用过。这些项目通常不是用 C/C++ 编写的。通过使用 XML 版本,我只需要在一处更新/添加 typedef/struct/union,并且所有项目都能够自动生成源和/或对话框。

I have inherited a Visual Studio project that contains hundreds of files.

I would like to extract all the typedefs, structs and unions from each .h/.cpp file and put the results in a file).

Each typedef/struct/union should be on one line in the results file. This would make sorting much easier.

typedef int myType;
struct myFirstStruct { char a; int b;...};
union Part_Number_Serial_Number_Part_2_Response_Message_Type {struct{Message_Response_Head_Type Head; Part_Num_Serial_Num_Part_2_Report_Array Part_2_Report; Message_Tail_Type Tail;} Data; BYTE byData[140];}myUnion;
struct { bool c; int d;...}mySecondStruct;

My problem is, I do not know what to look for (grammar of typedef/structs/unions) using a regular expression.
I cannot believe that nobody has done this before (I googled and have not found anything on this).

Does anyone know the regular expressions for these? (Note some are commented out using // others /* */)

Or a tool to accomplish this.

Edit:
I am toying with the idea of autogenerating source code and/or dialogs for modifying messages that use the underlying typedef/struct/union. I was going to use the output to generate an XML file that could be used for this reason.

The source for these are in C/C++ and used in almost all my projects. These projects are usually NOT in C/C++. By using the XML version I would only need to update/add the typedef/struct/union only in one place and all the projects would be able to autogen the source and/or dialogs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞花火° 2024-09-07 03:34:36

除了某种文档工作之外,我无法想象这样做的目的。如果这就是你正在寻找的,我会建议 doxygen。

为了回答你的问题,我严重怀疑任何数量的正则表达式都足够。您需要做的实际上是解析代码。我听说过一个用于构建编译器和 C++ 工具的库,可以提供解析功能,但很抱歉我忘记了名字。我知道它就在那里,所以我会开始寻找它。

I can't imagine a purpose for this except for some sort of documentation effort. If that is what you're looking for I would suggest doxygen.

To answer your question, I seriously doubt any amount of regular expressions will be sufficient. What you need to do is actually parse the code. I have heard of a library out there for building compilers and C++ tools that would provide the parsing aspect but I'm sorry to say I have forgotten the name. I know it's out there though so I'd start searching for that.

悲歌长辞 2024-09-07 03:34:36

您将无法使用正则表达式来完成此任务。实际做到这一点的唯一方法是获取 C++ 语法的词法分析器和解析器,并自己编写代码,以便在遇到您感兴趣的结构之一时将感兴趣的位转储到文件或数据库中。不幸的是,C++解析相当困难。

You will NOT be able to accomplish this with a regular expression. The only way to actually do this will be to get hold of a lexer and parser for the C++ grammar and write the code yourself to dump the interesting bits to a file or database upon encountering one of the structures you're interested in. And unfortunately, C++ parsing is rather hard.

毅然前行 2024-09-07 03:34:36

解析 C++ 是......困难的。除了试图解析它而自杀之外,还有一些选择。

其中每个都将解析 C++ 代码并获取您之后的信息。如果您想将其转储到您请求的格式的文件中,那么解析它们的数据文件比解析原始 C++ 要好得多。

我建议您跳过所有这些,只使用 doxygen。它不会是您喜欢的格式,但您最好习惯 doxygen 的布局。

parsing c++ is ... difficult. Instead of killing yourself trying to parse it there are a few options.

Each of these will parse c++ code and grab the info your after. If you want to dump it to a file in the format you requested you'd be a lot better off parsing their data files than parsing raw c++.

I recommend you skip all of this and just use doxygen. It won't be in your preferred format but you'll be better off getting used to doxygen's layout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文