find function name in a program file

Hello All,
Is there any way to find a function name in a program file using perl.

for example, there is a file called Test.C in that
.
.

void function1(..)
{
<some code>
}
int function2(..)
{
.
.
sam() /*RA abc100*/
...
..
..
xyz()/*RA abc201*/
..
.
}

from this I know only the string 'abc100', using this how can I find the function name and the other associate RA numbers within the funcion.

thanks in advance,
Parthiban.

Do you mean to search in a file for the given pattern?

grep 'abc100' TEST.C

Not sure I understood you correctly.

It will show only the particular line which contains abc100.
I want the function name which contains 'abc100' and other other numbers which having the same pattern 'RA '.

I tried the following

awk -F"RA " '{ print $2}' $FileName | awk '{print $1}'| sed 's/[-+;:!=?"|{}()<>~\%\$\@\#\^\&\*\[\\\/]/ /g' | sed '/^$/d' | uniq

it will list out all the strings with pattern "RA " in the file.

I just want to know the function name which contains the specific string, and the line numbers of starting of the function and ending of the function.

any idea?

try this

  • put this code in a file called script.pl or whatever you like:

$str=shift @ARGV;
while(<>){
$c++ if $sw && /\{/;
$c-- if $sw && /\}/;
if($sw && $c==0){
print "end line $.\n";
$sw=0;
}
if(!$sw && /(.+\(.*?\)).*\/.*RA $str/){
print "function $1\nstart line $.\n";
$sw=1;
}
}

  • your C program file assuming is named prog.c and has opening an closing curly braces, like this:

void function1(..)
{
<some code>
}
int function2(..)
{
.
.
sam() /RA abc100/
{
...
..
..
}
xyz()/RA abc201/
{
..
.
}

  • so if you do this you might get what you want:

perl script.pl abc100 prog.c

---------- Post updated at 08:12 PM ---------- Previous update was at 04:09 PM ----------

sorry I think i made a mistake, please use this perl script instead teh one i sent:

$str=shift @ARGV;
while(<>){
        if(/(.+\s+.+\(.*\))/){
                $func_name=$1;
                $func_start=$.;
                $sw=1;
                @ra=();
                next;
        }
        $c++ if $sw && /\{/;
        $c-- if $sw && /\}/; 
        if(/\/\*RA (.+)\*\//){
                push(@ra,$1);
        }
        if(/\/\*RA $str/){
                $print=1;
        }
        if($sw && $c==0){
                if($print){
                        print "here4\n";
                        print "function $func_name\n";
                        print "start line $func_start\n";
                        print "end line $.\n";
                        print "ra's:\n  ".join("\n  ",@ra)."\n";
                        $print=0;
                }
                $sw=0;
        }
}
1 Like

Hi, Thanks for your great work. but This script doesn't print anything. I used the the following file for testing

file name : prog.c

function1()
{
  abcd;
  xyz;
  sam()/*RA abc00123*/
}

function2()
{
  abcd;
  xyz;
  sam;/*RA xyz00891*/
  sdfsdg;
  dgsdg;
  ertwt; /*RA abc100*/
  sf;
}

>perl script.pl abc100 prog.c
>

try this version

$str=shift @ARGV;
while(<>){
        if(!$sw && /(.+\(.*\))/){
                $func_name=$1;
                $func_start=$.;
                $sw=1;
                @ra=();
                next;
        }
        $c++ if $sw && /\{/;
        $c-- if $sw && /\}/; 
        if(/\/\*RA (.+)\*\//){
                push(@ra,$1);
        }
        if(/\/\*RA $str/){
                $print=1;
        }
        if($sw && $c==0){
                if($print){
                        print "here4\n";
                        print "function $func_name\n";
                        print "start line $func_start\n";
                        print "end line $.\n";
                        print "ra's:\n  ".join("\n  ",@ra)."\n";
                        $print=0;
                }
                $sw=0;
        }
}

1 Like

Hi, Its working great... Thank you very much.. you made my work simple now.. As am new to perl can you explain me how it works.?

here's the code with some comments to try to explain...i hope it helps..

$str=shift @ARGV;
while(<>){
        # match function name and save name and line to display later in
        # case the RA is found
        if(!$sw && /(.+\(.*\))/){
                $func_name=$1;
                $func_start=$.;
                $sw=1;  
                @ra=();
                next;
        }       
        # those counters basically server to know if we are ($c>0) or not ($c==0) 
        # inside a function body i.e. between { and } 
        $c++ if $sw && /\{/;
        $c-- if $sw && /\}/; 

        # match RA string and save it in a list
        if(/\/\*RA (.+)\*\//){
                push(@ra,$1);
        }       
                
        # flag to know if the RA in questions was found, so we'll preint everything
       # we've saved   
        if(/\/\*RA $str/){ 
                $print=1;
        }       
                
        # if we are at the end of a function $sw==1 and $c==0 then 
        if($sw && $c==0){ 
            # we test if any of the RAs in the function was the one we were 
            # looking for, if so print all the elements
                if($print){
                        print "function $func_name\n";
                        print "start line $func_start\n";
                        print "end line $.\n";
                        print "ra's:\n  ".join("\n  ",@ra)."\n";
                        $print=0;
                }
                $sw=0;
        }       
}

1 Like

what is
if(!$sw && /(.+\(.*\))/) looking for. and we didn't declare $sw previously, how it works and what is the use for it.?

Thanks & Regards,
Parthiban.