On 8/17/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
snip
> I have multiple C/C++ files which I need to go manually and find out
> specific function call. I want to check 2nd parameter of this
> function
> (Function_To_Look_For) and get value of it and store it in local file
> or output it. For example:
snip

Yes, Perl can do this.  No, it won't be easy to make a %100 solution
(and that is true in any language).  You can get an %80 solution
fairly easily (works for eighty percent of the possible valid inputs).
 The code at the end of this email is probably a %7 solution.   It
works for your test case, but will break on common inputs like

Function_To_Look_For("foo,bar,baz", sizeof(STRUCT_3_T), third);

To get a %99 solution you would need to write a C++ language parser,
but even it might have a problem with this valid code:

#define FTLF Function_To_Look_for

FTLF("foo,bar,baz", sizeof(STRUCT_3_T), third);

as acting on information to transform the tree is outside of the
parsers job.  So you would need a parser and some software to
interpret, at least partially, the parsers output.  And in the end,
even that isn't enough if you allow function pointers (you will need
to either prove that the function you are looking for is never
assigned to a pointer or write a full fledged C++ interpreter).

Now, don't let that daunt you too much, an 80% solution is often good
enough.  Especially if the input has all been written in the same
style.  This is especially true if you are looking for a specific
functions signature (as opposed to writing a generic find a function
and return its nth argument program).
#!/usr/bin/perl

use strict;
use warnings;

my $file = join '', <DATA>;

print "$_\n" for $file =~ /
        Function_To_Look_For #look for the constant "Function_To_Look_For"
        \s*                  #followed by any number of whitespace chars
        \(                   #followed by an open paren
        \s*                  #followed by  any number of whitespace chars
        .*?                  #match the shortest possible string up to
        ,                    #the next comma character
        \s*                  #followed by more optional whitespace
        (.*?)                #this is the parameter we want, so capture it
        \s*                  #but not the optional whitespace after it
        ,                    #no point in looking past this comma
                             #we have what we came for
/gx;

__DATA__
void
MY_CLASS:: Func1()
{


   STRUCT_1_T      my_struct1;
   Function_To_Look_For (MY_1ST_PARAMETER,
                             sizeof( STRUCT_1_T ),
                             MY_3RD_PARAMETER);



}


void
MY_CLASS:: Func2()
{

   STRUCT_2_T  my_struct2;
  Function_To_Look_For (MY_1ST_PARAMETER,
                             sizeof(STRUCT_2_T ),
                             MY_3RD_PARAMETER);



}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to