Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__func__ not replaced in preprocessed output

Tags:

c++

c

macros

I was reading about __FUNCTION__ / __func__ in C/C++ (they are used to print the name of the function in which they are used). Every place I read they said that these are macros and are replaced at preprocessing time. So, I investigated this by seeing the preprocessed output using command gcc -E prog.c. But I saw that neither __func__ nor __FUNCTION__ were replaced by the preprocessor by the function name.

So, is it a macro? If not, what is it and how is it implemented?

EDIT

Even tried cpp prog.c. But still not replaced.

Also __FILE__, __LINE__, and __FUNCTION__ usage in C++ this post says that it never affects performance. Please clarify.

like image 712
tapananand Avatar asked Dec 11 '22 01:12

tapananand


2 Answers

They are implemented as (and are) "magic variables". The manual says:

GCC provides three magic variables that hold the name of the current function, as a string. The first of these is __func__, which is part of the C99 standard:

The identifier __func__ is implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

static const char __func__[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function. This name is the unadorned name of the function.

They typically cannot be implemented as preprocessor macros; the preprocessor doesn't parse function scopes. Of course a preprocessor could be made to understand enough of the syntax to know where functions begin and end, but typically they're not operating at that level.

like image 109
unwind Avatar answered Dec 23 '22 06:12

unwind


__func__ is essentially a pre-defined variable, not a macro. Where __FUNCTION__ will be expanded, __func__ will not, and will be used as if you defined the following in your function:

static const char __func__[] = "function-name";

or

static const char __func__[] = __FUNCTION__;

Please refer to:

The intent of __func__ is similar to __FUNCTION__, but the delivery is different.

http://www.open-std.org/jtc1/sc22/WG21/docs/papers/2004/n1642.html

like image 22
bizzehdee Avatar answered Dec 23 '22 08:12

bizzehdee