[EMAIL PROTECTED] wrote on 11/15/06 06:06:
Hi all,
i must use cfg library to build and manipulate a control flow graph. I have 
read more but i have not found an answer to my question: It is possible to 
build a cfg structure directly from a file .cfg ?? How i can building a cfg 
from a file??
Thanks to all,

Ask for a dump using the -blocks switch and post-process the dump file with the attached script.

$ gcc -fdump-tree-all-blocks file.c
$ dump2dot file.c.XXXt.yyy

It generates a graphviz file with the flow graph of the function. The script is fairly simplistic and will not handle more than one function too gracefully, but that should be easy to change.
#!/bin/sh
#
# (C) 2005 Free Software Foundation
# Contributed by Diego Novillo <[EMAIL PROTECTED]>.
#
# This script is Free Software, and it can be copied, distributed and
# modified as defined in the GNU General Public License.  A copy of
# its license can be downloaded from http://www.gnu.org/copyleft/gpl.html

if [ "$1" = "" ] ; then
    echo "usage: $0 file"
    echo
    echo "Generates a GraphViz .dot graph file from 'file'."
    echo "It assumes that 'file' has been generated with -fdump-tree-...-blocks"
    echo
    exit 1
fi

file=$1
out=$file.dot
echo "digraph cfg {"            > $out
echo "  node [shape=box]"       >>$out
echo '  size="11,8.5"'          >>$out
echo                            >>$out
(grep -E '# BLOCK|# PRED:|# SUCC:' $file |                              \
        sed -e 's:\[\([0-9\.%]*\)*\]::g;s:([a-z_,]*)::g' |              \
        awk '{  #print $0;                                              \
                if ($2 == "BLOCK")                                      \
                    {                                                   \
                        bb = $3;                                        \
                        print "\t", bb, "[label=\"", bb, "\", style=filled, 
color=gray]";               \
                    }                                                   \
                else if ($2 == "PRED:")                                 \
                    {                                                   \
                        for (i = 3; i <= NF; i++)                       \
                            print "\t", $i, "->", bb, ";";              \
                    }                                                   \
            }')                 >> $out
echo "}"                        >> $out

Reply via email to