1

I am looking for a way to extract all functions found in a C++ header file including comments for that respective function which sit on top of the function definition.

The idea is to produce a list of all functions in the file in order to get it into a CSV or Excel file. I would like to have on each line:

Header file name;Function name only;Complete function signature;Comments in code for that function

Example (from file someHeader.h):

// This is the multi-line
// comment for the DoSomething function.
void            DoSomething (int CleverParameter);

turned into

someHeader.h;DoSomething;void DoSomething (int CleverParameter);This is the multi-line comment for the DoSomething function.

I don't need clever parsing and type/dependency resolution or such things. I have VisualStudio 2010 and 2012 at my hands. Also not afraid of Linux or Windows command-line tools. If whitespace gets cleaned up in the process, that would be great.

4
  • 2
    If you don't need an accurate answer, then a regex will do about well as anything (e.g., badly). Otherwise you are going to discover that to extract such function definitions you need a full preprocessor to resolve macros and conditionals (maybe you can run the header through the preprocessor first to resolve this). Even with that, C++'s syntax is arcane enough (and MS's variant is worse because of its 30 year history) so whatever non-clever parsing solution you choose will get false negatives and false positives. Commented Nov 21, 2013 at 16:10
  • You say you're not afraid of Linux. I don't know how you'll be throwing your code between Windows and Linux but if you can get it between the two fairly easy and don't really care take a look at 'lex'. I'm positive you can find predefined scanners for C/C++. Commented Nov 21, 2013 at 16:37
  • 1
    Look at ctags - it will do most of the parsing job for you (though without comments). Commented Nov 21, 2013 at 16:40
  • ctags looks nice and gets me there halfway. I don't get comments in the tag file (which is okay). But I also don't get tags for function definitions which use types defined in other files. Commented Nov 22, 2013 at 9:46

1 Answer 1

3

Writing a decent C++ parser won't be an easy task.

A possible solution would be to use a documentation generator such as doxygen's, it doesn't include a complete C++ parser but it should be sufficient for your purpose:

  • Use doxygen to generates an XML output containing all function definitions and comments
  • Use an XSLT stylesheet or an XML parser to generates your CSV file

It's not the perfect solution, but it would definitely be more robust than a regex approach and won't require a lot of code to get it working.

Sign up to request clarification or add additional context in comments.

2 Comments

"Won't be easy" is the understatement of the year. We've been working continuously on ours for about 5 years. Doxygen is workable, but not accurate, simply because it isn't a real C++ parser. It is probably better than a regex solution.
The doxygen approach is pretty close. Since I don't have the time right now to learn enough about XML postprocessing, I'll just bluntly copy things from the HTML output. Thanks very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.