26

a smoking pile of spaghetti just landed on my desk, and my task is to understand it (so I can refactor / reimplement it).

The code is C, and a mess of global variables, structure types and function calls.

I would like to plot graphs of the code with the information:
- Call graph
- Which struct types are used in which functions
- Which global variable is used in what function

Hopefully this would make it easier to identify connected components, and extract them to separate modules.

I have tried the following software for similar purposes:
- ncc
- ctags
- codeviz / gengraph
- doxygen
- egypt
- cflow
EDIT2:
- frama-c
- snavigator
- Understand

The shortcomings of these are either
a) requires me to be able to compile the code. My code does not compile, since portions of the source code is missing.
b) issues with preprocessor macros (like cflow, who wants to execute both branches of #if statements). Running it through cpp would mess up the line numbers.
c) I for some reason do not manage to get the software to do what I want to do (like doxygen; the documentation for call graph generation is not easy to find, and since it does not seem to plot variables/data types anyway, it is probably not worth spending more time learning about doxygen's config options). EDIT: I did follow a these Doxygen instrcutions, but it did only plot header file dependencies.

I am on Linux, so it is a huge plus if the software is for linux, and free software. Not sure my boss understands the need to buy a visualizer :-(

For example: a command line tool that lists in which functions a symbol (=function,variable,type) is referenced in would be of great help (like addr2line, but for types/variable names/functions and source code).

//T

Community
  • 1
  • 1
The Apa
  • 863
  • 1
  • 9
  • 6
  • 1
    *"Running it through cpp would mess up the line numbers."* — you might be interested in [Coan](http://coan2.sourceforge.net/index.php?page=about) (see [this answer](http://stackoverflow.com/questions/2786000/separate-specific-ifdef-branches/2786157#2786157)) – detly Jul 26 '11 at 08:22
  • 1
    coan can indeed be helpful simplifying the preprocessor mess. Thank you! – The Apa Jul 26 '11 at 09:28

4 Answers4

6

My vote goes to gnu global. It has all the features of ctags/cscope combined as well as the possibility to generate fully indexed html which allows you to browse the code in your favorite browser. Fire it up in apache and you have a web-service that anyone can access including full search capabilities.

It integrates nicely into emacs/vim/even the bash-shell, and you can use it directly from the shell-prompt.

To see it in action on the linux kernel, visit this

Combine that with a tool for cyclomatic complexity plugin for eclipse which calculates the complexity of your code. besides the cyclomatic complexity it can handle:

  • McCabe's Cyclomatic Complexity
  • Efferent Couplings
  • Lack of Cohesion in Methods
  • Lines Of Code in Method
  • Number Of Fields
  • Number Of Levels
  • Number Of Locals In Scope
  • Number Of Parameters
  • Number Of Statements
  • Weighted Methods Per Class

...and you should have everything you need.

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • GNU global seems to be a superset of ctags, and have much of cscope's functionality. However, I am still trying to figure out how to show the functions where, say, a certain struct or typedef is used? – The Apa Jul 26 '11 at 08:56
  • It would be great if [GNU Global was supported directly in Eclipse](https://bugs.eclipse.org/bugs/show_bug.cgi?id=129278). – Craig McQueen Jun 19 '12 at 05:57
3

If you like command line ;) maybe you could try cscope, it does static analysis of code and can tell you where are referenced some symbols/variables/functions... Not the Holy Graal, but it can be pretty usefull to browse unknown source code.

There are also some GUI that can handle csope results (Vi, Emacs, JEdit...).

On the other hand, Eclipse with the CDT plugin can also help you to navigate into the spaghetti code you have to maintain.

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • +1 for eclipse, although if the code doesn't compile, you might need to add some header files to be indexed by hand. – haggai_e Jul 26 '11 at 09:28
0

It's not free and afaik not linux but cppDepend might be worth evaluating - at least until someone comes up with a more suitable suggestion :)

http://www.cppdepend.com/ [Demo video here]

MattDavey
  • 8,897
  • 3
  • 31
  • 54
0

If you'd like to know in which functions a symbol is declared or referenced you can try LXR. It's not console based, but is quite usable.

Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79