Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 681
jond3k Avatar asked May 06 '10 18:05

jond3k


People also ask

Which of the following is a type of C or C static code analysis tool?

Helix QAC is an excellent static analysis testing tool for C and C++ code from Perforce (formerly PRQA).

What is static code analysis C?

Static analysis identifies defects before you run a program (e.g., between coding and unit testing). Dynamic code analysis identifies defects after you run a program (e.g., during unit testing). However, some coding errors might not surface during unit testing.

Is Valgrind a static analysis tool?

Static analyses are distinct from dynamic analyses such as valgrind, which extract facts from a program as it runs, and model checking, which verifies the correctness of a separate external specification of a program. Dynamic and static analyses are often used in tandem across many applications.

Which one is the commercial code quality Tool for c c++ language?

CppDepend is a commercial static code analysis tool for C++.


2 Answers

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

like image 147
Gregor Brandt Avatar answered Oct 27 '22 03:10

Gregor Brandt


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

like image 32
James Morris Avatar answered Oct 27 '22 02:10

James Morris