This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

__FUNCTION__ et al not set up in C


Except for the very first function in a file, the function-name magic
strings are not set up properly by the current compiler.  If you use
__FUNCTION__ or __PRETTY_FUNCTION__, you get the empty string; if you
use __func__ you get an undeclared variable error.  For example, the
test program at the end of this message will abort when run, which it
should not do.  Or, if you get an ICE out of the stage2 compiler, it
won't print the name of the function that gave up.

Analysis: c_begin_compound_stmt is in charge of calling declare_function_name.
It only does this if c_function_name_declared_p is false, and then it sets
c_f_n_d_p to 1.  This is because c_begin_compound_stmt is called every time
we begin a compound statement, not just on function entry.  c_f_n_d_p
should be cleared again by pop_c_function_context, but that function is
never called - the only use of {push,pop}_c_function_context is to
initialize {save,restore}_lang_status, and those hooks are only used for
nested functions.

Adding 

  /* If we are popping out of a function, clear c_function_name_declared_p.  */
  if (functionbody)
    c_function_name_declared_p = 0;

at the end of poplevel() seems to fix the bug, but I do not know if
this is correct or complete.

Note that __FUNCTION__ works fine in C++.

zw

-- test case --
extern void abort(void);
extern int strcmp(const char *, const char *);

int foo(const char *a)
{
    return strcmp(a, "main");
}

int main(void)
{
    if(foo(__FUNCTION__))
	abort();
    return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]