Alex Rivera | Logout

Listing C/C++ functions (Code analysis in Unix)

Asked 2010-05-06T18:51:00.190
13

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?

Edit
Report

1 Answer

2

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).

answered 2010-05-06T19:06:37.577

Your Answer