mario::konrad
programming / C++ / sailing / nerd stuff
c99parser
© 2005 / Mario Konrad

Description

This tool provides some static code analysis. It is able to parse some preprocessed C source code and generate different graphs from the caputred data.

The parser is written in C++, using flex++ and bison++ as scanner- and parser-generators.

The lastest version does only support standard C99 source code as well as C source for Tasking 6.0r4 compilers (almost completely). GCC is not supported right now, due to proprietary language extensions (gcc has many intrinsic functions).

The tool is tested under Linux (RH9) and Cygwin (1.5.7).

Unpack the source:

$ tar -xzf parser-x.x.x-src.tgz

and read the files README and INSTALL for a description of the build and installation process.

Usage

After you unpacked, build and installed the tool successfully, type

$ c99 --help
$ analysis --help

for a complete list of parameters. For a more concrete example, please read the tutorial.

Tutorial

This tutorial shows how to use the tool and what kind of outputs you can get. As mentioned above, there is only a very limited support for compilers, therefore this tutorial only uses Standard C99.

To go through the tutorial you will need the built and installed tool and a working GraphViz installation (at least dot).

The Source

We have 2 modules:

which have some internal data and functions as well as some exported function. module_a even has global data.

Preprocess the Code

Although GCC is not supported, we use it to preprocess the files. This works because the source code for this tutorial does not contain any proprietary extensions.

$ for i in *.c ; do gcc -E $i -o ${i%.c}.E ;done

This creates two new files:

Parse the Source

Now let's parse the preprocessed code:

$ c99 -p std -r analysis -o source.dat *.E

Short explanation of the parameters:

Option Descritption
-p std The parser shall use the Standard C99 parser.
-r analysis Use the report generator analysis
-o source.dat The output of the report generator shall be stored in this file. We'll need this file to generate the graphs.
*.E Those are the input files: all preprocessed source files.

Generate Output

Since we have all necessary data, we're able to generate some graphs. There are more possible output types, but now we will do the most common.

Call Graph of a Function

$ analysis -f source.dat -c "module_b.c:func_foobar" -o test1.dot

Inverse Call Graph of a Function

$ analysis -f source.dat -i "module_a.c:func_test_1" -o test2.dot

Dependencies of a Module

$ analysis -f source.dat -m "module_b.c" -o test3.dot

Details of a Module

$ analysis -f source.dat -b "module_a.c" -o test4.dot

Access to a global Variable

$ analysis -f source.dat -g "module_a.c:var_B" -o test5.dot

Generate the Graphs

$ for i in *.dot ;do dot -Tgif $i -o ${i%.dot}.gif ;done

Clean Up

$ rm *.bak *.E *.dot *.dat

Results

Finally, after the hard work ;-) we may enjoy the results.

Download

All source code provided by this page is distributed under the terms of the GPL. Read also the LICENCE file. Use it on your own risk.

Versions: