How to extract ALL typedefs and structs and union from C ++ source

I inherited a Visual Studio project containing hundreds of files.

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

Each type of typdef / struct / union must be on the same line in the result file. This will make sorting easier.

typdef 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 that I don't know what to look for (typedef / structs / union grammar) using regex. I can’t believe that no one has done this before (I searched Google and found nothing on this).

Does anyone know regular expressions for them? (Note that some of them are commented using // others / * * /)

Or a tool to achieve this.

Edit:
I play with the idea of ​​automatically generating source code and / or dialogs to modify messages that use the base typedef / struct / union. I was going to use the output to create an XML file that could be used for this reason.

The source for them is in C / C ++ and is used in almost all of my projects. These projects are usually NOT C / C ++. Using the XML version, I would only need to update / add typedef / struct / union in only one place, and all projects will be able to auto-generate the source and / or dialogs.

+4
source share
3 answers

I cannot imagine the purpose for this, except for some kind of documentation effort. If this is what you are looking for, I would suggest doxygen.

To answer your question, I seriously doubt that enough regular expressions will be enough. What you need to do is actually parse the code. I heard about a library for creating compilers and tools in C ++ that would provide parsing, but unfortunately I forgot the name. I know it there, although I would start looking for it.

+3
source

You can NOT do this with regex. The only way to do this is to get a lexer and a parser for C ++ grammar and write the code yourself to upload interesting bits to a file or database when faced with one of the structures that interest you. And unfortunately, C ++ parsing is quite complicated.

+2
source

C ++ parsing ... complicated. Instead of killing yourself, trying to make it out, there are several options.

Each of them will analyze C ++ code and capture information after you. If you want to dump it into a file in the format you requested, it will be much better for you to parse their data files than to analyze raw C ++.

I recommend that you skip all this and just use doxygen . It will not be in your preferred format, but you will be better off getting used to the doxygen layout.

+1
source

Source: https://habr.com/ru/post/1308256/


All Articles