Find declaration and definition of function using bash

in the very simplest case,

grep -ER '^.*(.*);' file  #find declaration.

as for definition, its a bit tricky if you want to get the whole defintion. (ie. from opening braces to closing). you might want to show what you want to get by using a sample C++ file


I believe that the most robust way is to use dedicated tools for this instead of using grep, ack or anything of that order. It is always possible definitions are spread over multiple lines in the most unimaginable way. The aforementioned tools will easily return the requested information for functions like:

int f(int a) { return 2; }
int g(a,b,c,d) int a,b,c,d; { return 2; }

but they will fail to give the complete value when your source is a bit more convoluted:

int f ( // declares a function
        // this function is going to be awesome      
        int a  // first variable
      ) { return 2; }

You problably should make use of ctags.


Try ack, which is way better than grep. Color highlighting, the works...

$ ack funcion_name

Prints out all uses of function_name searching recursively through source files, it also is smart enough to ignore .svn and other metadata

Tags:

Bash