tags:

views:

973

answers:

2

I am trying to write a wrapper function to figure out who is calling a specific function. So in .h file I added the following: (and implementation in the .cc file)

extern int foo(/*some arguments*/);

extern void call_log(const char*file,const char*function,const int line,const char*args);

#define foo(...) (call_log(__FILE__, __FUNCTION__, __LINE__, "" #__VA_ARGS__), foo(__VA_ARGS__))

However, I get the following error:

 error: expected a type specifier
    (call_log(__FILE__, __FUNCTION__, __LINE__, "" #__VA_ARGS__),foo(__VA_ARGS__)

Assume that the foo function is called with some parameters and returns an int.

the compiler is gcc 3.4

Thanks

EDIT

removed "." and extra spaced, yet problem still there. Can anyone see what can cause it. Also notice that I am using variadic macros -- supported by my compiler (c99)

Edit 2 Just to get the claims about my illegal use of some c/c++ constructs. The following code below works, and I am trying to adapt it to my current (above function)

#include <stdio.h> 

int funcA( int a, int b ){ return a+b; }

// instrumentation

void call_log(const char*file,const char*function,const int line,const char*args){
  printf("file:%s line: %i function: %s args: %s\n",file,line,function,args);
}

#define funcA(...) \
  (call_log(__FILE__, __FUNCTION__, __LINE__, "" #__VA_ARGS__), funcA(__VA_ARGS__)) 

// testing

void funcB()
{
  funcA(7,8);
}


int main(void){
  int x = funcA(1,2)+

          funcA(3,4);

  printf( "x: %i (==10)\n", x );

  funcA(5,6);

  funcB();
}

Edit 3

As litb pointed out, the problem is, in fact, due to macro substitutions. I also noticed that foo is not only a function call but also used as ptr->foo[] in some cases. Any ides how to resolve this sort of issues, without breaking more code

+1  A: 

The best way to find out what's wrong would be to make the compiler show the preprocessed code. You can then easier spot the problem in the offending line.

xtofl
A: 

If I'm not mistaken I believe you have a number of syntax errors in the definition and declaration of call_log.

In the definition I am pretty sure you need a space between the const char* and the variable name. ie...

extern void call_log(const char* file,const char* function,const int line,const char* args);

In addition, in the declaration, I don't think you can append #__VA_ARGS__ to "" in the manner you are doing. The macro will resolved this as:

(call_log(file_name, function_name, line_#, "" arg1, arg2...), foo(args[])

which is not a valid function call. On a side-note you are also missing a semicolon after the macro definition.

Finally, I do not see a reference to foo(args) in your macro definition; It's possible I'm missing some context but you may wish to look at that as well.

Hope this helps...

CJ

1) the const char* and the variable name -- I don't need, try with an example to see taht it compiles. 2)I don't think you can append #__VA_ARGS__ -- why not? I just tried with a dummy example and it works (posted above) 3) everything is valid.
vehomzzz
I second xtofl's answer, add the command to generate the .i file to the compiler command-line and then search for call_log and pwlf_eval to see what is actually generated. BTW, I missed that you were generating pwlf_eval in my original answer. I think you need to declare call_log by itself before you can include it in another call. It's been a while since I worked with extern function definitions so I could be wrong about this.
You may be right but in my years I have seen plenty of code that will compile but not work :). As I mentioned I have not worked with externally defined functions for a while so my advice may be worth what it cost you...
The original code doesn't declare call_log as an argument to a function as your example does. As I mentioned before this might be the issue. pwlf_eval does not include a type definition for call_log. Correct me if I'm wrong but isn't the syntax for declaring an external function more like:extern (void)(call_log(...));?