views:

275

answers:

5

Whether we're maintaining unfamiliar code or checking out the implementation details of an Apache module it can help if we can quickly traverse the code and build up an overview of what we're looking at. Grep serves most of my daily needs but there are some cases where it just wont do.

Here's a common example of how it can help. To find the definition of a PHP function I'm interested in I can type this at the command line:

grep -r "function myfunc" .

This could be adapted very quickly to C or C++ if we know the return type, but things become more complicated if, say, I want to list every method that my class provides:

grep "function " ./src/mine.class.php

Since there's no single keyword that denotes a function or method in C++ and because it's generally more complex syntax, I think I'd need some kind of static code analysis tool, smart use of the C Preprocessor or blind faith the coder followed strict code guidelines (# of whitespace, position of curlies etc) to get these sorts of results.

What would you recommend?

p.s. be nice, this is my first post ;-) :p

+13  A: 

Run it through Doxygen. It will complain about lack of commenting , but it will still produce call graphs and list all the functions. Presented in HTML with links to follow code paths.

doxygen

gbrandt
+1 You should also enable the option that generates source code output in HTML ("SOURCE BROWSER"). The resulting code will contain hyperlinks that will make navigating the source code a breeze.
Emile Cormier
+1 I've just started using Doxygen to generate diagrams using GraphViz to help me remember the flow of data.
James Morris
+8  A: 

Exuberant Ctags http://ctags.sourceforge.net/

I've only used it from time to time some time ago, and from within a text editor, but check out the list of utilities/tools which can use it:

http://ctags.sourceforge.net/tools.html

James Morris
ctags is great for investigating new code, and widely supported by IDE's and editors. Go into the directory you're looking at, execute `ctags -R`, launch your favorite editor and jump around the code. In vim you use `ctrl-]` to jump to a definition and `ctrl-t` to jump back.
Winder
+4  A: 

Doxygen is able to generate some reasonable html documentation and parse out comments. It's not perfect, but it might help. You could incorporate Ctags into your editor to jump you to the functions that you're looking for.

Personally, I use grep ;)

Stephen
I also use grep on my own project when I need to find something... but a true IDE to edit the code AND have the declaration/definition just a clik away is just too powerful to pass up IMNSHO.
Matthieu M.
For projects I'm not familiar with, I totally agree an IDE is fantastic.
Stephen
+1  A: 

grep '^[a-zA-Z0-9][ *]+ {[a-zA-Z0-9_]+}\([a-zA-Z0-9\,\.\-\>]\*\)$'

Is roughly what you want. It may take some playing with, but match a valid C++ return type, give the option of it being a pointer, then a function name (which will be \1), open parentheses, parameters, close.

That general form (return, name, (param)) should work unless you may have line-breaks within a function declaration.

I'd use Doxygen or another tool to parse it, but if you need to do it quickly and once, regex might be easier (or might not, with regex you never know).

peachykeen
+3  A: 

cscope is very good for this sort of thing. Unlike ctags, cscope provides an interface suitible for searching (ctags requires an editor).

Just run cscope in the root directory of the code you want to inspect. It will: create a database if one isn't there, update the database if one is there, and open a curses gui where you can query all sorts of useful info

  • all references to a symbol
  • global definitions
  • functions called by a function
  • functions calling a function
  • text string
  • regular expression pattern
  • a file
  • files including a file

ctags only does the first one, 'all references to a symbol'.

caspin