views:

113

answers:

6

Hi,

can anyone tell when g++ replaces the __FUNCTION__ 'macro' with the string containing the function name? It seems it can replace it not until it has check the syntactical correctness of the source code, i.e. the following will not work

#include <whatsneeded>
#define DBG_WHEREAMI __FUNCTION__ __FILE__ __LINE__

int main(int argc, char* argv)
{
  printf(DBG_WHEREAMI "\n"); //*
}

since after preprocessing using

g++ -E test.cc

the source looks like

[...]

int main(int argc, char* argv)
{
  printf(__FUNCTION__ "test.cc" "6" "\n"); //*
}

and now the compiler rightly throws up because the *ed line is incorrect.

Is there any way to force that replacement with a string to an earlier step so that the line is correct?

Is __FUNCTION__ really replaced with a string after all? Or is it a variable in the compiled code?

T.H.X. bbb

+3  A: 

You are using __FUNCTION__ like a preprocessor macro, but it's a variable (please read http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html).

Try printf(__FUNCTION__) just for testing and it will print the function name.

albtraum
+2  A: 

__FUNCTION__ is not standard. Use __func__. As the documentation says, it's as if:

<ret-type> function_name( <args> )
{
    static const char __func__[] = "function-name";
    ...
Nikolai N Fetissov
+2  A: 

In C/C++, the preprocessor will turn "my " "name " "is " "Bob" into the string literal "my name is Bob", since __FILE__ and __LINE__ are preprocessor instructions, "We are on line " __LINE__ will pass "We are on line 27" to the compiler.

__FUNCTION__ is normally a synonym for __func__. __func__ can be though of as a pseudo-function that returns the name of the function in which it is called. This can only be done by the compiler and not by the preprocessor. Because __func__ is not evaluated by the preprocessor, you do not get automatic concatenation. So if you are using printf it must be done by printf ("the function name is %s", __func__);

doron
+4  A: 

Is there any way to force that replacement with a string to an earlier step so that the line is correct?

No. __FUNCTION__ (and it's standardized counterpart, __func__) are compiler constructs. __FILE__ and __LINE__ on the other hand, are preprocessor constructs. There is no way to make __FUNCTION__ a preprocessor construct because the preprocessor has no knowledge of the C++ language. When a source file is being preprocessed, the preprocessor has absolutely no idea about which function it is looking at because it doesn't even have a concept of functions.

On the other hand, the preprocessor does know which file it is working on, and it also knows which line of the file it is looking at, so it is able to handle __FILE__ and __LINE__.

This is why __func__ is defined as being equivalent to a static local variable (i.e. a compiler construct); only the compiler can provide this functionality.

Dan Moulding
+1  A: 

Is this what you want?

#include <stdio.h>

#define DBG_WHEREAMI(X) printf("%s %s(%d): %s\n",__func__,__FILE__,__LINE__,X)

int main(int argc, char* argv)
{
  DBG_WHEREAMI("Starting");
}

Note: Since you marked this as C++ you should probably be using the iostreams to make sure its type safe.

Martin York
Unfortunately, this doesn't allow for something like `DBG_WHEREAMI("Error code %d", errorCode)`. It can't be made to work, either (at least not portably) because C++ doesn't (yet) have variadic macros.
Dan Moulding
not quite...i was wondering if i could generate a string containing __func__ at compile time so that is is runtime constant.that seems to be impossible.
bbb
A: 
printf("%s" __FILE__ __LINE__ "\n", __FUNCTION__);

Yeah, I know that's not really the same.

BCS