2

I want to work on an app that will open up a Visual Studio Project and display all of the classes in the project. It will only read the header files to find classes.

It's taking me forever to parse each data member and each method for display properly.

So I was wondering if there is some sort of API or library that I can use to parse all of the details of a C++ Header file so I can display them.

EDIT: This is what my app currently looks like. I currently have issues getting User Defined types, which is why you see several unnamed Int32 types. App Preview

szMuzzyA
  • 136
  • 13
  • I've looked into CTags and LibCLang and they seem very complex. But I can't find any useful documentation on either, so I could be wrong. – szMuzzyA Feb 21 '16 at 21:06
  • What specifically is "taking forever"? – Ira Baxter Feb 21 '16 at 21:40
  • Checking each line of code for keywords and the names of the data member/functions. I have it somewhat working, but there's just so many little things that become issues like pointers and global functions/variables and class forwards. – szMuzzyA Feb 21 '16 at 22:10
  • Those "so many little" things take up 600 pages in the reference manual. Read my answer. – Ira Baxter Feb 21 '16 at 23:22

1 Answers1

0

Everybody hopes parsing source code is easy, even C++. It is not.

If you want to accurately parse C++ (header) files, you need a full C++ parser. In fact, parsing header files, especially those from the vendor (e.g., Microsoft's and even GNU's) is particularly nasty because they tend to contain undocumented constructs specific to the compiler.

You have only 4 good choices here:

  • The GNU compiler. It obviously can read GCC header files. I doubt it can read MS header files because of the vendor-specific extenstions. GCC really, really, wants to be a compiler and will resist your attempts to bend it to other tasks. Melt is a GCC extension that tries to make this easier; I've looked at it, and it doesn't seem that much better, but I'm biased.

  • Clang. It has a full C++ parser, specializing in GCC-style source files. I don't know what it can do about MS specific constructs let alone MS headers. Clang is at least organized to let you use it for custom tasks. (Apparantly VS2015 includes a copy of Clang to support Intellisense, but you can't get at the information it collects).

  • EDG. This is a commercial front end. It has a full parser, and is designed to let you build tools around it. I don't know what it does about MS or GNU headers. AFAIK, it doesn't provide anything other than the front end. (That's a lot).

  • (Our) commercial DMS Software Reengineering Toolkit with its C++ front end. (I obviously know a lot about this). It has a full C++14 parser, and handles both GCC and MS header files. Our front ends are the only ones that make any attempt to preserve preprocessor directives, if that matters to you. DMS is designed to let you build tools around it. DMS provides lots of support for pattern matching and code transformation above and beyond "just parsing". After parsing, information about each class is available in C++'s symbol table; it would be pretty easy to enumerate them and their members, and their relationships to other classes.

No matter what C++ parsing technology you use, "its very complex", don't expect that dealing with C++ is going to be easy. And, expect a high learning curve to understand any of the above frameworks. If you make the investment, and follow through with building a real tool, you'll learn a lot and be ready to build a next, more sophisticated tool with a lot less effort.

If you don't care about accuracy, you could scan files using Perl and regexes to hunt for class declarations. This will probably lead to a useless tool.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • Before making this post I downloaded this and could not find any way to use it in code. I couldn't find anything in the documentation to help me parse the classes – szMuzzyA Feb 21 '16 at 22:11
  • You aren't going to get far if you can't describe what you did clearly. Downloaded... *what*? Reviewed *what* documentation? – Ira Baxter Feb 21 '16 at 22:12
  • Shame on me for not reading your answer. I was thinking you posted a Semantic Designs link like you did in another Question similar to this. I can't figure out how to use Clang. The documentation wasn't very helpful. I'm looking into EDG and The GNU Compiler atm. – szMuzzyA Feb 21 '16 at 22:23
  • CLang seems to be the easiest of these to manage. So I suppose I will stick to that. Do you have anything you can tell me that might help me when parsing a C++ header so I can get the class name, data members, and methods so I can display them? I'm adding an image to the original question to show what exactly I'm doing. You'll notice the issues with grabbing the data members. – szMuzzyA Feb 21 '16 at 23:19
  • Don't know how to use Clang; I work hard enough on my own stuff. I will say that open source code tends not to have a lot of documentation. Clang may be different; I haven't looked very hard. We work hard to make sure our stuff is documented. – Ira Baxter Feb 21 '16 at 23:25