Synergy of Graphviz and C / C ++ Preprocessor

This article is about how to use the popular Graphviz graphing tool in cooperation with the C / C ++ preprocessor (hereinafter simply referred to as the preprocessor).


The key point is that the dot graph language used by Graphviz is suitable for preprocessor processing in its syntax. So conceived by the developers. Thanks to their insight, we can use the following features when describing graphs (I quote from Wikipedia from memory):


  • replacing the corresponding digraphs and trigraphs with the equivalent characters "#" and "\";
  • removal of escaped line feed characters;
  • replacing line and block comments with empty lines (with the removal of surrounding spaces and tabs);
  • insertion (inclusion) of the contents of an arbitrary file ( #include );
  • macro substitution ( #define );
  • conditional compilation ( #if, #ifdef, #elif, #else, #endif );

Now we will demonstrate the opened opportunities in practice. As an example, take the graph from my article on MediaStrimer2 , it is in the figure below.



The graph is large enough and if you describe it β€œon the forehead”, then a lot of manual error-free work will be required. If you look closely, you can isolate repeating elements with a difference only in the content of some fields. This is how the nodes of the graph look like m 1, m2, m2_1, m2_2, m3, m4 . These are the first candidates for the use of small mechanization using macros. Create a header file for our main dot-file. Let's call it common.dot :


//  common.dot       .

#define Q(x) #x         //    ""   .

#define QUOTE(x) Q(x)   //   ""    .

//         mblk_t.
//       . 
#define msg_staff \
   <p> *prev \
  |<n> *next \
  |<c> *cont  \
  |<d> *data \
  | other\nstuff

//        mblk_t.
#define msg(name, ... ) \
name[xlabel=mblk_t label=QUOTE(<h> name | msg_staff) \
];

//        dblk_t.
#define dbuf(name ...) \
name[label=QUOTE(<h> name) xlabel="dblk_t"];

//       queue_t.
//         mblk_t, 
//      msg_staff.
#define queue(name, ...) \
name[ xlabel="queue_t" label=QUOTE(<h> name | \
msg_staff)];

Now it's time to write the main graph file. Let's call it my_graph.dot :


//  my_graph.dot

//      .
#include "common.dot"

digraph queue_demo
{
    rankdir=LR;
    node[shape=Mrecord];

    //     ,
    //  ,       .
    dbuf(d1);
    dbuf(d2);
    dbuf(d2_1);
    dbuf(d2_2);
    dbuf(d3);
    dbuf(d4);

    //  ,      ,
    //     .
    msg(m1);
    msg(m2);
    msg(m2_1);
    msg(m2_2);
    msg(m3);
    msg(m4);

    //     .
    //     q1.
    queue(q1);

    //       .
    m1:d->d1;
    m2:d->d2;
    m2_1:d->d2_1;
    m2_2:d->d2_2;
    m3:d->d3;
    m4:d->d4;

    //     .
    m1:n -> m2:h;
    m1:p -> q1:h;
    m2:n -> m3:h;
    m2:c->m2_1:h;
    m2_1:c->m2_2:h;
    m3:n -> m4:h;

    m2:p -> m1:h;
    m3:p -> m2:h;
    m4:p -> m3:h;

    //      .
    q1:n->m1:h;  
    q1:p->m4:h;
    m4:n -> q1:h[color=blue]; //    .
}

We process the file with a preprocessor:


cpp my_graph.dot -o my_graph_res.dot

my_graph_res.dot. :


# 1 "<built-in>"
# 1 "<command-line>"
# 1 "my_graph.dot"

# 1 "common.dot" 1
# 3 "my_graph.dot" 2

digraph queue_demo
{
    rankdir=LR;
    node[shape=Mrecord];

    d1[label="<h> d1" xlabel="dblk_t"];;
    d2[label="<h> d2" xlabel="dblk_t"];;
    d2_1[label="<h> d2_1" xlabel="dblk_t"];;
    d2_2[label="<h> d2_2" xlabel="dblk_t"];;
    d3[label="<h> d3" xlabel="dblk_t"];;
    d4[label="<h> d4" xlabel="dblk_t"];;

    m1[xlabel=mblk_t label="<h> m1 | <p> *prev |<n> *next |<c> *cont |<d> *data | other\nstuff" ];;
    m2[xlabel=mblk_t label="<h> m2 | <p> *prev |<n> *next |<c> *cont |<d> *data | other\nstuff" ];;
    m2_1[xlabel=mblk_t label="<h> m2_1 | <p> *prev |<n> *next |<c> *cont |<d> *data | other\nstuff" ];;
    m2_2[xlabel=mblk_t label="<h> m2_2 | <p> *prev |<n> *next |<c> *cont |<d> *data | other\nstuff" ];;
    m3[xlabel=mblk_t label="<h> m3 | <p> *prev |<n> *next |<c> *cont |<d> *data | other\nstuff" ];;
    m4[xlabel=mblk_t label="<h> m4 | <p> *prev |<n> *next |<c> *cont |<d> *data | other\nstuff" ];;

    q1[ xlabel="queue_t" label="<h> q1 | <p> *prev |<n> *next |<c> *cont |<d> *data | other\nstuff"];;

    m1:d->d1;
    m2:d->d2;
    m2_1:d->d2_1;
    m2_2:d->d2_2;
    m3:d->d3;
    m4:d->d4;

    m1:n -> m2:h;
    m1:p -> q1:h;
    m2:n -> m3:h;
    m2:c->m2_1:h;
    m2_1:c->m2_2:h;
    m3:n -> m4:h;

    m2:p -> m1:h;
    m3:p -> m2:h;
    m4:p -> m3:h;

    q1:n->m1:h;
    q1:p->m4:h;
    m4:n -> q1:h[color=blue];
}

, . . Graphviz ( dot) : https://sketchviz.com/new


:



, , .


.


All Articles