C ++ Introspection: list available classes and methods in C ++ codebase

I am working on some static analysis of C ++ code for my PHD dissertation. As part of the extension for a C ++ type system, I want to take a C ++ database and list its available functions, methods and classes along with their type signatures with minimal effort (this is just a prototype). What is the best way to do something like this quickly and easily? Do I have to hack Klang to spit out the information I need? Should I look at parsing header files with something like SWIG? Or is there an even simpler thing I could do?

+4
source share
5 answers

GCC -based GCCXML may be a ticket. As far as I understand, it collects and deletes all definitions, but not the contents of functions / methods.

Others are most likely mentioned by CLANG, which certainly parses the code and needs to have access to the definitions of characters in the compilation unit. (I have no experience here).

For completeness, you should know about our DMS Software Reengineering Toolkit with C ++ Front End . (The CLANG answers seem to say, "Pass AST.") The DMS solution provides an enumerable character table containing all type information. You can also walk around AST if you want.

Often, static analysis leads to a diagnosis and a desire to change the source code. A DMS may apply transformations of source software transformations to perform such changes by analysis.

+5
source

I heartily recommend LLVM for static analysis (see also Clang Static Analyzer )

+1
source

I think your best bet is to hack on clang and get an AST. There is a good tutorial for this . Its very easy to change its syntax, and it also has a static analyzer .

+1
source

In my work, I use the API from a software package called "Understand 4 C ++" scitools . I use this to write all my static analysis tools. I even wrote a .NET API to wrap my C API. Which I put on codeplex .

Once you do, resetting all class types is easy:

ClassType[] allclasses = Database.GetAllClassTypes() foreach (ClassType c in allclasses) { Console.WriteLine("Class Name: {0}", c.NameLong); } 

Now for a little background about the task, I had something that looked like yours. After a few years, we must maintain SDK compatibility with the previous SDK. In this case, it is useful to compare the SDK between releases to check for possible violations. However, with several hundred files and tens of thousands of lines of comments, which can be a big headache, using a tool for delimiting text, such as Beyond Compare or Araxis. So what I really need to see is the actual code changes, not reordering, not moving the code up and down in the file, not adding comments, etc.

So, the tool I wrote to download all the code.

In one text file, I delete all classes. For each class, I print its inheritance tree, its member functions, both virtual and non-virtual. For each virtual function, I print some virtual methods of the parent class that it overrides (if any). I also print out its member variables. The same thing happens with structures. In another file, I print all the macros. In another file, I print all typedefs.

Then, using this, I can share these files with the files from the previous version. Then it becomes apparent what has changed since release to release. For example, it is easy to see where the function parameter has been changed from TCHAR * to const TCHAR *, for example.

+1
source

You might consider developing a GCC Plugin for your purpose.

And GCC MELT is a high-level domain language (which I developed and implemented) to easily extend GCC.

A document at Peter Collingborn and Paul Kelly's GROW09 Workshop on Compile-Time Infrastructure for GCC using Haskell may be relevant to your work.

0
source

All Articles