Reputation: 4830
I have a C++ header file containing class definition, and I want to retrieve the types and names of its member variables.
Editors like Eclipse and Visual Studio do this to visualize the code, so I am interested if they also provide API in some (maybe native) scripting language or maybe in Java, which will allow to get member variable types and names as strings and, say, write them to a file. If not, then maybe there are utilities to dump the class description to some XML-like file?
Upvotes: 1
Views: 999
Reputation: 4950
IDEs basically use a C++ compiler to extract this information because parsing C++ is hard. Even worse, the C++ compiler has to be fault-tolerant if it should work while you're writing the code.
Visual Studio creates a SQL Server database in your project folder. That is the database used for code completion aka IntelliSense. Look for the file "projectname.sdf". You can write a Visual Studio add in that opens this database and accesses its members. I have done so.
But: There is absolutely no guarantee that the information in this database is complete and correct. On small projects it will probably work ok, on large projects with several 100k LOC the database will almost certainly not be 100% complete. If you want to generate code automatically based on this database, be very, very careful.
The Visual Studio compiler generates a debug symbols database that you can query after you have compiled your project. There is an example project on MSDN to do that.
Clang provides an API to do that. See libclang et al.
From that page:
Clang provides infrastructure to write tools that need syntactic and semantic information about a program. This document will give a short introduction of the different ways to write clang tools, and their pros and cons.
This is certainly a better option than 1) because you actually access the intermediate compiler syntax tree. Several people (e.g. Apple, Google) have written refactoring tools and syntax checkers based on Clang. More information here.
One big caveat: Clang is currently not able to parse many Windows headers because it does not support all Microsoft compiler options.
Upvotes: 2