Skip to content

grep like command line utility that lists entire function definition of found regex

Notifications You must be signed in to change notification settings

oci-labs/FuncFinder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

FuncFinder

The FuncFinder command line application takes a regular expression and a list of C++ source files and lists the entire function definition of any function that contains the regular expression. It works similarly to grep with -A NUM, --after-context=NUM and -B NUM, --before-context=NUM options. Except, instead of a specified number of trailing and leading lines it prints the entire function definition regardless of the number of lines. In addition to the function definition, the output includes the line with the regex as well as the range of lines of the function in the file.

> FuncFinder
Usage: FuncFinder regex source_file ...

It can be tedious to specify each source file individually, but under Unix-like environments like Linux or Mac OS X or even Windows with Cygwin find and xargs can do that for you.

> find . -name ".cpp" | xargs FuncFinder regex_search_string

Here is an example of running it on the TAO services source code looking for uses of std::ifstream.

> cd ACE_wrappers-2.0/TAO/orbsvcs
> find . -name ".cpp" | xargs FuncFinder std::ifstream
== ./performance-tests/RTEvent/Colocated_Roundtrip/compare_histo.cpp(30) range [24,41]==

void load_file (Vector &vector, const char *filename) { std::ifstream is (filename); if (!is) throw "Cannot open file";

while (is && !is.eof ()) { long i; double v; is >> i >> v; Vector::value_type e (i, v); vector.insert (e); } }

Note the use of std::ifstream in the first line of the load_file function. Since FuncFinder found the search string in the compare_histo.cpp file, it reported the number of the line that matched std::ifstream and printed the entire function definition. Here our search regular expression is just the simple string, "std::ifstream", but it could have been any regular expression.

FuncFinder is not written with performance particularly in mind, although performance is not completely ignored, it runs sufficiently quick for most use cases. You can speed up the previous example by incorporating the amazing performance of grep. The following produces the same output as above, but runs much faster.

> cd ACE_wrappers-2.0/TAO/orbsvcs
> find . -name "*.cpp" | xargs grep -l std::ifstream | xargs FuncFinder std::ifstream

About

grep like command line utility that lists entire function definition of found regex

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages