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

2020-03-01 08:46发布

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?

5条回答
家丑人穷心不美
2楼-- · 2020-03-01 09:30

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

查看更多
够拽才男人
3楼-- · 2020-03-01 09:32

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

查看更多
何必那么认真
4楼-- · 2020-03-01 09:36

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

查看更多
Animai°情兽
5楼-- · 2020-03-01 09:37

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

查看更多
贼婆χ
6楼-- · 2020-03-01 09:44

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

查看更多
登录 后发表回答