Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count # of functions/methods in a project 1

Status
Not open for further replies.

ArmenD

Programmer
Feb 3, 2005
101
US
hi,
does anyone know how to count # of functions in a c/c++ project repository using bash or csh?
is there a mixture of grep/wc/and redirecting operators.

say for instance there is a proj directory called Foo with subfolders A,B,C.
 
Do the c/c++ - files follow a coding style like
Code:
RETURNTYPE mName (<optional: paramlist>)
{
or:
Code:
RETURNTYPE mName (<optional: paramlist>) {
And do you have 'find'?
Code:
find PROJECTFOLDER -name "*.c" -exec grep {EXPRESSION} \;
with EXPRESSION something like:
Code:
"^CNAME CNAME (.*)[ \t]*$"
and CNAME:
Code:
[a-zA-Z][a-zA-Z0-9_]*

This will not solve #define evil macros and functions in header-files, and return-types of pointertype, but could be a starting point:
Code:
find $1 -name "*.cc" -exec grep "^[a-zA-Z][a-zA-Z0-9_]* [a-zA-Z][a-zA-Z0-9_]* (.*)[ \t]*$" {} \;


seeking a job as java-programmer in Berlin:
 
this is a good start, but I have define's in my code and I would need a broader range of detections.
 
Well if you're going to restrict yourself to such a limited set of tools, then expect poor results.

Why not actually use the tools available to give you a direct answer without all the messing about with increasingly bizarre grep expressions.

Example
Code:
gcc prog.c
nm -g a.out | awk 'NF==3 && $2=="T" && $3 !~ /^_/ { print $3 }'
Study the output of your 'nm' command carefully, it contains all the symbols used in your program. From that, it's usually a pretty easy deal to get a list of say all the global functions.

Appending [tt]| wc[/tt] is a formality.

Or do [tt]man ctags[/tt] to find out the information it can retrieve about source files. Parsing the output of ctags would be considerably easier than parsing the source code directly.

--
 
you can first "indent" your code, and then run the mixture of awk and grep commands
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top