Adding printf's to c functions

Discussion in 'Perl Misc' started by Caj Zell, Dec 19, 2006.

  1. Caj Zell

    Caj Zell Guest

    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?
    Caj Zell, Dec 19, 2006
    #1
    1. Advertising

  2. Caj Zell

    Ric Guest

    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.


    >
    Ric, Dec 19, 2006
    #2
    1. Advertising

  3. Caj Zell

    Dr.Ruud Guest

    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."
    Dr.Ruud, Dec 19, 2006
    #3
  4. Caj Zell

    Dr.Ruud Guest

    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."
    Dr.Ruud, Dec 19, 2006
    #4
  5. Caj Zell

    Ben Morrow Guest

    Quoth "Dr.Ruud" <>:
    > 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 []
    Ben Morrow, Dec 19, 2006
    #5
  6. Caj Zell

    Dr.Ruud Guest

    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."
    Dr.Ruud, Dec 20, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,581
    Victor Bazarov
    Jun 21, 2004
  2. ben
    Replies:
    4
    Views:
    603
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,229
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    428
  5. guru
    Replies:
    8
    Views:
    268
Loading...

Share This Page