The minimal config required to generate a sequence diagram is presented below:
Consider the following diagram:
clang-uml
generated sequence diagrams are not strictly speaking conforming to the UML specification. In order to make them more useful for documenting modern C++ code, the following assumptions were made:
clang-uml
can be used to generate sequence diagrams from plain old C code). Functions can also be aggregated into file participants, based on their place of declarationif
or while
) are rendered inside the PlantUML or MermaidJS alt
or loop
blocks but wrapped in [
, ]
bracketsSequence diagrams require specification of location constraints in order to determine, which call chains should be included in the diagram. Currently, there are 3 types of constraints:
from
- will include all message call chains, which start at the locations specified in this constraint (this was previously named start_from
)to
- will include all message call chains, which end at the specified locationsfrom_to
- will include all call chains, which start and end at the specified locationsCurrently, the constraints can be a method or a free function, both specified using either the full signature of the function or a regular expression, e.g.:
or
However note that the latter will match all functions starting with 'main', so make sure the regular expression only matches the intended functions.
In case of methods the signature must include fully qualified name, e.g.:
or regular expression matching against the fully qualified name, e.g.:
In case of the from_to
constraint, it is necessary to provide both from
and to
locations as follows:
To find the exact function signature, which can be used as a from
location, run clang-uml
as follows (assuming the function of interest is called main
):
or to get all possible to
locations, run:
Command line flags --print-from
and --print-to
will print on stdout all functions and methods available in the diagram model, and each line of this output can be directly used as a value of from
, from_to
or to
properties in the config file.
Since that list can be quite large, it's best to filter the output to limit the number of lines to a subset of possible candidates.
By default, clang-uml
will generate a new participant for each call to a free function (not method), which can lead to a very large number of participants in the diagram. If it's an issue, an option can be provided in the diagram definition:
which will aggregate free functions per source file where they were declared thus minimizing the diagram size. An example of such diagram is presented below:
Lambda expressions in sequence diagrams are... tricky. There is currently tentative support, which follows the following rules:
std::transform
call) the call will not be generatedAnother issue is the naming of lambda participants. Currently, each lambda is rendered in the diagram as a separate class whose name is composed of the lambda location in the code (the only unique way of identifying lambdas I was able to find). For example the following code:
results in the following diagram:
In case lambda expressions are redundant and we are only interested in the calls generate from the lambda expressions, it is possible to inline lambda expressions in the generated diagrams by specifying the following option:
For example compare the test cases t20012 and t20052.
The default participant order in the sequence diagram can be suboptimal in the sense that consecutive calls can go right, then left, then right again depending on the specific call chain in the code. It is however possible to override this order in the diagram definition using participants_order
property, for instance like this:
By default, return messages do not contain the return type information from the function or method. Instead, if the result is void there is no return arrow from the activity representing the function body.
It is however possible to enable rendering of return types, by adding the following configuration option:
This option only affects the plantuml
and mermaid
generators, in json
generator return_type
property is always present in the message nodes.
The diagram below presents what it looks like in a PlantUML generated diagram:
Sometimes, it is useful to include actual condition statements (for instance contents of the if()
condition in the alt
or loop
blocks in the sequence diagrams, to make them more readable.
This can be enabled using the following option:
An example of a diagram with this feature enabled is presented below:
If in a given sequence diagram functions or methods are called multiple times from different branches, each of these activities will be rendered fully which can mean that the diagram be very large.
In order to minimize the diagram size in such situations, an option can be set:
which will render any activity only once, and any further calls to that activity will only render a call to the activity and an indicator - a single *
character
For an example of this see the test case t20056.
In some cases, clang-uml
is not yet able to discover a call expression target in some line of code. This can include passing function or method address to some executor (e.g. thread), async calls etc.
However, a call expression can be injected manually through a comment directive
It should be placed in the comment just before such line of code, for example:
also see the t20038 test case.
Please note that the callee must have fully qualified name including complete namespace.
In order to enable this, the .clang-uml
must contain the following option:
otherwise Clang will skip these comments during AST traversal.
clang-uml
can add code comments placed directly before or next to a call expression as notes in the diagram (see for instance t20038).
This however is not enabled by default. In order to enable this feature it is necessary to first of all force Clang to parse all comments in the source code by adding the following compile flag at the top of .clang-uml
:
or adding it to the compile_commands.json
database somehow directly.
Another option needed to generate these comments in the diagram is to set
for each sequence diagram, which should include these comments.
In case only selected messages should have some specific comments, instead of enabling the generate_message_comments
option, it is possible to use \\uml{note TEXT}
directive in the comment above the expression, see t20001.