Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Adding printf's to c functions

Reply
Thread Tools

Adding printf's to c functions

 
 
Caj Zell
Guest
Posts: n/a
 
      12-19-2006
Hello,

I have a large collection of .c/.cpp and .h files in a project and
want to add "enter" and "leave" printf:s in every function. Like

int foo(int t)
{
int a;
printf("Entering foo...\n");
return t;
printf("Leaving foo...\n");
}

I don't have the possibility of using gcov or anything similar and
just want to have these printf's added.

I have tried to google around for it and stumbled upon RecDescent, but
I haven't found a way to do this.

Can this be done in Perl? Maybe it is already done?

 
Reply With Quote
 
 
 
 
Ric
Guest
Posts: n/a
 
      12-19-2006
Caj Zell schrieb:
> Hello,
>
> I have a large collection of .c/.cpp and .h files in a project and
> want to add "enter" and "leave" printf:s in every function. Like
>
> int foo(int t)
> {
> int a;
> printf("Entering foo...\n");
> return t;
> printf("Leaving foo...\n");


The printf after "return t" won't be reached

So you would have to write:
printf("Leaving foo...\n");
return t;

or place a printf after the foo() call

> }
>
> I don't have the possibility of using gcov or anything similar and
> just want to have these printf's added.
>
> I have tried to google around for it and stumbled upon RecDescent, but
> I haven't found a way to do this.
>
> Can this be done in Perl? Maybe it is already done?


Sure this can be done, I'm not sure if there is already a perl module,
that can be used to add some output functions for code coverage tests.
But this should not be too difficult to implement.


>

 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      12-19-2006
Caj Zell schreef:

> I have a large collection of .c/.cpp and .h files in a project and
> want to add "enter" and "leave" printf:s in every function. Like
>
> int foo(int t)
> {
> int a;
> printf("Entering foo...\n");
> return t;
> printf("Leaving foo...\n");
> }
>
> I don't have the possibility of using gcov or anything similar and
> just want to have these printf's added.
>
> I have tried to google around for it and stumbled upon RecDescent, but
> I haven't found a way to do this.
>
> Can this be done in Perl? Maybe it is already done?



Replace any start-{ of any function by
{ BEGIN_FUNCTION("function-name");
and any end-} of any function by
END_FUNCTION("function-name") };
and all functional "return"-s by "RETURN(<function-name>)".

Then make a special include-file with some handy defines like

#define BEGIN_FUNCTION(n) printf( "Entering %s...\n", (n) )
#define END_FUNCTION(n) printf( "Leaving %s...\n" , (n) )
#define RETURN(n) END_FUNCTION(n), return

(untested)

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-19-2006
Dr.Ruud schreef:

> and all functional "return"-s by "RETURN(<function-name>)".


Rewrite:

Replace any start-{ of any function by

{
BEGIN_FUNCTION

and any end-} of any function by

END_FUNCTION
}

and all functional return-s by

RETURN


Then make a special include-file with some handy defines like

#define BEGIN_FUNCTION static const char *My_name = "foo";\
printf( "Entering %s...\n", My_name )
#define END_FUNCTION printf( "Leaving %s...\n" , My_name )
#define RETURN (END_FUNCTION), return

(still untested)

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      12-19-2006

Quoth "Dr.Ruud" <(E-Mail Removed)>:
> Dr.Ruud schreef:
>
> > and all functional "return"-s by "RETURN(<function-name>)".

>
> Rewrite:
>
> Replace any start-{ of any function by
>
> {
> BEGIN_FUNCTION

<snip>
>
> #define BEGIN_FUNCTION static const char *My_name = "foo";\
> printf( "Entering %s...\n", My_name )


Nope. This fails in the case the OP posted, of

int
foo ()
{
int a = 2;
return a;
}

(or something like that). With your substitutions, it becomes

int
foo ()
{
static const char *My_name = "foo";
printf( "Entering %s...\n", My_name )
int a = 2;
return a;
}

which mixes up declarations and statements, which isn't allowed in C89.
Something like

#define BEGIN FUNCTION { printf ("Entering foo...\n"); {
#define END_FUNCTION } printf ("Leaving foo...\n"); }

(with an extra scope) might work, though; or, since the OP seems to call
his C++ files .cpp, he's probably on Windows, and (IIRC) MSVC is not
very strict about C89.

Ben

--
Many users now operate their own computers day in and day out on various
applications without ever writing a program. Indeed, many of these users
cannot write new programs for their machines...
-- F.P. Brooks, 'No Silver Bullet', 1987 [(E-Mail Removed)]
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-20-2006
Ben Morrow schreef:
> Dr.Ruud:


>> #define BEGIN_FUNCTION static const char *My_name = "foo";\
>> printf( "Entering %s...\n", My_name )

>
> Nope. This [...]
> mixes up declarations and statements, which isn't allowed in
> C89.


I was actually aware of that when posting, but from context left it to
the user to either use a certain compiler (and options), or insert the
macro at the end of the variable declarations.


> Something like
>
> #define BEGIN FUNCTION { printf ("Entering foo...\n"); {
> #define END_FUNCTION } printf ("Leaving foo...\n"); }
>
> (with an extra scope) might work, though; or, since the OP seems to
> call his C++ files .cpp, he's probably on Windows, and (IIRC) MSVC is
> not very strict about C89.


I thought about that too for a while, but that would get in trouble with
return(s).

A C-preprocessor (in Perl of course), working like XML::SAX, would be
nice. See also Syntax::Highlight::Engine::Kate::C.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
Passing member functions to C functions? Steven T. Hatton C++ 7 10-07-2004 02:12 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Help! Passing Templates functions to template functions ILLOGIC C++ 1 06-01-2004 10:51 PM
Exportable class functions as stand alone functions to .DLL or .SO Timothy Wong C++ 3 05-20-2004 01:44 PM



Advertisments