clang-uml
is an automatic C++ to UML class, sequence, package and include diagram generator, driven by
YAML configuration files. The main idea behind the
project is to easily maintain up-to-date diagrams within a code-base or document
legacy code. The configuration file or files for clang-uml
define the
types and contents of each generated diagram.
The diagrams can be generated in PlantUML and JSON formats.
clang-uml
currently supports C++ up to version 17 with partial support for C++ 20.
Full documentation can be found at clang-uml.github.io.
To see what clang-uml
can do, checkout the diagrams generated for unit
test cases here or examples in
clang-uml-examples repository.
Features
Main features supported so far include:
- Class diagram generation
- Class properties and methods including access – example
- Class inheritance – example
- Other class relationships including associations, aggregations, dependencies and friendship – example
- Template instantiation relationships – example
- Template specialization and instantiation based on deduced context – example
- Relationship inference from C++ containers and smart pointers – example
- Diagram content filtering based on namespaces, elements and relationships – example
- Optional package generation from namespaces – example
- Optional package generation from subdirectories – example
- Interactive links to online code to classes, methods and class fields in SVG diagrams – example
- Support for plain C99/C11 code (struct and units relationships) – example
- C++20 concept constraints – example
- Sequence diagram generation
- Generation of sequence diagram from specific method or function – example
- Generation of loop and conditional statements – example
- Generation of switch statements – example
- Generation of try/catch blocks – example
- Handling of template code including constexpr conditionals – example
- Handling of lambda expressions – example
- Interactive links to online code to classes and call expressions – example
- Package diagram generation
- Include graph diagram generation
- Show include graph for selected files – example
More comprehensive documentation can be at clang-uml.github.io.
Installation
Installation instructions for Linux
, macos
and Windows
can be found
here.
Usage
Generating compile commands database
clang-uml
requires an up-to-date
compile_commands.json
file, containing the list of commands used for compiling the source code.
Nowadays, this file can be generated rather easily using multiple methods:
- For CMake projects, simply invoke the
cmake
command
ascmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ...
- For Make projects checkout compiledb
or Bear - For Boost-based projects
try commands_to_compilation_database - For SCons, invoke
compilation_db
tool (requires SCons > 4.0.0) - For Microsoft Visual Studio projects try Clang Power Tools
Invocation
By default, config-uml
will assume that the configuration file .clang-uml
and compilation database compile_commands.json
files are in the
current directory, so if they are in the top level directory of a project,
simply run:
The output path for diagrams, as well as alternative location of
compilation database can be specified in .clang-uml
configuration file.
For other options checkout help:
Configuration file format and examples
Configuration files are written in YAML, and provide a list of diagrams
which should be generated by clang-uml
. Basic example is as follows:
compilation_database_dir: . output_directory: puml diagrams: myproject_class: type: class glob: - src/*.cc using_namespace: - myproject include: namespaces: - myproject exclude: namespaces: - myproject::detail plantuml: after: - 'note left of {{ alias("MyProjectMain") }}: Main class of myproject library.'
See here for detailed configuration file reference guide.
Examples
To see what clang-uml
can do, checkout the test cases documentation here.
In order to see diagrams for the clang-uml
itself, based on its own config run
the following:
and checkout the SVG diagrams in docs/diagrams
folder.
Class diagrams
Example
The following C++ code:
Open the raw image here,
and checkout the hover tooltips and hyperlinks to classes and methods.
Sequence diagrams
Example
The following C++ code:
Package diagrams
Example
The following C++ code:
namespace clanguml { namespace t30003 { namespace ns1 { namespace ns2_v1_0_0 { class A { }; } namespace [[deprecated]] ns2_v0_9_0 { class A { }; } namespace { class Anon final { }; } } namespace [[deprecated]] ns3 { namespace ns1::ns2 { class Anon : public t30003::ns1::ns2_v1_0_0::A { }; } class B : public ns1::ns2::Anon { }; } } }
results in the following diagram (via PlantUML):
Include diagrams
In case you’re looking for a simpler tool to visualize and analyze include graphs
checkout my other tool – clang-include-graph
Example
The following C++ code structure:
tests/t40001
├── include
│ ├── lib1
│ │ └── lib1.h
│ └── t40001_include1.h
└── src
└── t40001.cc
results in the following diagram (via PlantUML) based on i