views:

158

answers:

3

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 typdef/struct/union should be on one line in the results file. This would make sorting much 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, 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.

+2  A: 

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.

Noah Roberts
+1 Doxygen would solve the doc problem nicely.
Mark B
one useful purpose I've come across for this is expanding MSVC's usertype.dat file, to add new keywords to the highlighter(why this isn't in the IDE I dunno...). the 'library' you might be thinking of is yacc or bison, but Doxygen is far better.
Necrolis
No, it's not the parser generator. There's some thing out there that I saw news about that is actually a C++ compiler library. I just did a web search and can't find it but I'm pretty sure it's out there. The article talked about how it would help C++ tool creation.
Noah Roberts
Doxygen would require that I go through all the files and add some sort of text block. I might as well just copy and paste what I need then.
Michael Wells
Ignore my previous comment. I stand corrected. I assumed that Doxygen would require that I go through all my source and add tags. All I need to do now is figure out how to use the generated XML files for my purpose.
Michael Wells
+1  A: 

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.

Dathan
A: 

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.

caspin