c++structextracttypedefunions

How to extract ALL typedefs and structs and unions from c++ source


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.


Solution

  • 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.