a __FUNCNAME__ like macro? 
Author Message
 a __FUNCNAME__ like macro?

The ANSI predefined macros like __LINE__ and __FILE__ are really useful for
asserts, logging, profiling, etc. I'm wondering if there is such a thing as
something like a __FUNCNAME__ macro (or a clean way to mimic one) that would
expand to the name of the current function. I end up cutting and pasting my
function name into a lot of logging/profiling macros cause it is much more
useful to see the name than the file/line number. For example:

void CMyObject::DoSomething( void )
{
    MY_PROFILE_MACRO( "CMyObject::DoSomething" )
    // ...blah
    // or...
    LOG( "CMyObject::DoSomething: invalid parameter" )

Quote:
}

I'd really just like to have these macros have some way of automatically
getting the current function name (human readable...not mangled) from the
compiler without me having to pass it as an argument... just like they can
get the file name and line number automatically.

I can come up with ugly ways to do this but not anything I would actually
use. Anybody got any ideas?

Thanks,

--Scott



Wed, 26 Jun 2002 03:00:00 GMT  
 a __FUNCNAME__ like macro?
Quote:

> The ANSI predefined macros like __LINE__ and __FILE__ are really useful for
> asserts, logging, profiling, etc. I'm wondering if there is such a thing as
> something like a __FUNCNAME__ macro

It would be useful, wouldn't it? People ask pretty frequently... No,
unfortunately, there isn't such a macro.

Quote:
> (or a clean way to mimic one) that would
> expand to the name of the current function.

That, I think, is relatively easy to do with perl. Go ahead and use this
__FUNCNAME__ identifier as if it were available. Then write a small perl
utility that would scan files, and copy them out w/o any change except
for replacing every occurrence of the token "__FUNCNAME__" with the
current function's name.

Of course, this utility would have to be able to determine function
scopes and memorize the name of the current one. That shouldn't be too
difficult to do, just look for token-space-token-opening
paren-whatever-closing paren-whatever-opening brace, while not inside
comments. When you hit this, store the second token. After which, all
__FUNCNAME__s have to be replaced by this stored token (the current
function's name) UNTIL: you hit a closing brace--end of scope, clear the
stored string, switch to searching for the first condition again. Keep
track of the number of braces, so you don't quit too soon on, say, a
switch or other control statement closing brace.

Then, configure your makefile to run your source file through this
utility, just like the compiler runs it through the preprocessor. Then
give the result (not the original source file) to the compiler and after
that, all is normal. It's a bit of extra work, but you can do it once
and then use forever.

Quote:
> I end up cutting and pasting my
> function name into a lot of logging/profiling macros cause it is much more
> useful to see the name than the file/line number.

Right. So you can automate that. Btw, AWK will do fine too, if you know
it better.


Wed, 26 Jun 2002 03:00:00 GMT  
 a __FUNCNAME__ like macro?

: >
: > The ANSI predefined macros like __LINE__ and __FILE__ are really useful for
: > asserts, logging, profiling, etc. I'm wondering if there is such a thing as
: > something like a __FUNCNAME__ macro
: It would be useful, wouldn't it? People ask pretty frequently... No,
: unfortunately, there isn't such a macro.
<snip>

You can't have a macro that holds the function name, since macros are
expanded by the preprocessor stage, which does not understand the C
language.  (Furthermore, it couldn't successfully be expanded if used
within another macro definition outside a function, and you wouldn't
want it to be)   However, the most recent draft I've seen of C9X (which
is probably called C2K now), the next C standard, says that __FUNC__
must expand to a string containing the function name; I suppose this
would be done (conceptually, at least) in the compiler stage.

Whether this will ever make it into C++, let alone VC++, I don't know.

--
Any opinions expressed are my own and not necessarily those of Laser-Scan.



Fri, 28 Jun 2002 03:00:00 GMT  
 a __FUNCNAME__ like macro?

Quote:



>: >
>: > The ANSI predefined macros like __LINE__ and __FILE__ are really useful for
>: > asserts, logging, profiling, etc. I'm wondering if there is such a thing as
>: > something like a __FUNCNAME__ macro
>: It would be useful, wouldn't it? People ask pretty frequently... No,
>: unfortunately, there isn't such a macro.
><snip>

>You can't have a macro that holds the function name, since macros are
>expanded by the preprocessor stage, which does not understand the C
>language.  (Furthermore, it couldn't successfully be expanded if used
>within another macro definition outside a function, and you wouldn't
>want it to be)   However, the most recent draft I've seen of C9X (which
>is probably called C2K now), the next C standard, says that __FUNC__
>must expand to a string containing the function name; I suppose this
>would be done (conceptually, at least) in the compiler stage.

>Whether this will ever make it into C++, let alone VC++, I don't know.

GCC does have this as an extension, (__FUNCTION__ and __PRETTY_FUNCTION__
which can be very useful...

        AndyM

--
Andy Moreton
Virata Ltd                            http://www.virata.com/
Mount Pleasant House, Huntingdon Road, Cambridge CB3 0BL, UK
Tel: +44 1223 566919  Fax: +44 1223 566915



Fri, 28 Jun 2002 03:00:00 GMT  
 a __FUNCNAME__ like macro?

Quote:

>However, the most recent draft I've seen of C9X (which
>is probably called C2K now), the next C standard, says that __FUNC__
>must expand to a string containing the function name; I suppose this
>would be done (conceptually, at least) in the compiler stage.

Interesting. I did not know that.

Quote:
>Whether this will ever make it into C++, let alone VC++, I don't know.

Probably; eventually.


Fri, 28 Jun 2002 03:00:00 GMT  
 a __FUNCNAME__ like macro?

Quote:

>GCC does have this as an extension, (__FUNCTION__ and __PRETTY_FUNCTION__
>which can be very useful...

Hmmmmm, it's been a while since I used GCC so I got to know what
__PRETTY_FUNCTION__ does? :)

Mike

--

"Genius gives birth, talent delivers."

           -- Jack Kerouac

(Remove NOSPAM, if present, to reply via email)



Fri, 28 Jun 2002 03:00:00 GMT  
 a __FUNCNAME__ like macro?

Quote:


>>GCC does have this as an extension, (__FUNCTION__ and __PRETTY_FUNCTION__
>>which can be very useful...
>Hmmmmm, it's been a while since I used GCC so I got to know what
>__PRETTY_FUNCTION__ does? :)

Quoting from the info page for gcc:

   GNU CC predefines two string variables to be the name of the current
function.  The variable `__FUNCTION__' is the name of the function as
it appears in the source.  The variable `__PRETTY_FUNCTION__' is the
name of the function pretty printed in a language specific fashion.

   These names are always the same in a C function, but in a C++
function they may be different.  For example, this program:

     extern "C" {
     extern int printf (char *, ...);
     }

     class a {
      public:
       sub (int i)
         {
           printf ("__FUNCTION__ = %s\n", __FUNCTION__);
           printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
         }
     };

     int
     main (void)
     {
       a ax;
       ax.sub (0);
       return 0;
     }

gives this output:

     __FUNCTION__ = sub
     __PRETTY_FUNCTION__ = int  a::sub (int)

   These names are not macros: they are predefined string variables.
For example, `#ifdef __FUNCTION__' does not have any special meaning
inside a function, since the preprocessor does not do anything special
with the identifier `__FUNCTION__'.

HTH,

        AndyM

--
Andy Moreton
Virata Ltd                            http://www.virata.com/
Mount Pleasant House, Huntingdon Road, Cambridge CB3 0BL, UK
Tel: +44 1223 566919  Fax: +44 1223 566915



Fri, 28 Jun 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. static char funcname[]

2. (*functab.funcname)() ??

3. Doubly liked lists...HELP PLEASE

4. liked lists in c

5. Doubly liked lists...HELP PLEASE

6. My short int likes 0xffff9001

7. C training - likes<->dislikes<->suggestions

8. Access liked dialog

9. Macro problem (new lines in macros?)

10. Building Macros out of Macros

11. #define a macro to be another macro?

12. Macro of a macro?

 

 
Powered by phpBB® Forum Software