Playing around with libclang -> reverse engineering UML class diagrams

libclang is a stable, high level C interface to Clang - a compiler for C/C++ family programming languages based on LLVM. I recently played around with the python bindings to libclang and created a small tool called CodeDependencyVisualizer. This tool is intended for reverse engineering UML class diagrams out of existing C++ code.

It can generate class diagrams with inheritances and associations.

If class X is derived from class Y, a closed arrow is drawn from X to Y (inheritance). If class X contains a class member variable of type Y, an open arrow is drawn from X to Y (association). This means it captures the logical static/compile time dependencies between classes (it does not consider function bodies, just the declarations).

For illustration of the output of the tool, take a look at the figure below - this is the result of the CodeDependencyVisualizer run against the “dummyCppProject” (this test code contains “random” language constructs, including classes, templates, namespaces, etc.):


As a bigger example, take a look at the huge graph that the tool generated for the Open Motion Planning Library (OMPL): dot, svg - such a large graph may make just limited sense, but at least it shows that CodeDependencyVisualizer is capable of reverse engineer an UML class diagrams of a large code base.

This small project was more about playing around and learn aboutĀ libclang than to create a serious software, so there is plenty room for improvements. At least it’s a showcase of how to traverse and access some properties of the clang AST with _libclang. S_omebody might findĀ  useful.

For the code of CodeDependencyVisualizer and Information on how to use it visit my github page.

comments powered by Disqus