Adding printf's to c functions

C

Caj Zell

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?
 
R

Ric

Caj said:
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.
 
D

Dr.Ruud

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)
 
D

Dr.Ruud

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)
 
B

Ben Morrow

Quoth "Dr.Ruud said:
Dr.Ruud schreef:


Rewrite:

Replace any start-{ of any function by

{
BEGIN_FUNCTION
#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
 
D

Dr.Ruud

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top