How to parse Simulink Coder system hierarchy from generated source code

In Simulink Coder generated source code, there is a comment in the main .h file that contains the system hierarchy. This comment is required in order to know what other comments in the file mean, such as Referenced by: '<S291>/index4'.

The system hierarchy comment looks like this:

with one line per subsystem.

In our previous post How to parse C++ comments from source code using clang we showed how to parse C++ comments using clang. We can use the same approach to parse the system hierarchy comment.

Parsing the system hierarchy comment

The following code uses the clang comment parser (see link above) and a regex based approach to extract the system hierarchy from the comment.

Compile using

Run the program with the source file and output JSON file as arguments:

After that, the output.json file will contain the system hierarchy in JSON format:


Check out similar posts by category: Matlab/Simulink