Bug #65294 my_global.h breaks __func__ on Linux with C99
Submitted: 12 May 2012 15:22 Modified: 14 May 2012 10:57
Reporter: Andrew Clayton Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S3 (Non-critical)
Version:5.5.22, 5.5.25 OS:Linux (Fedora 16, 14)
Assigned to: CPU Architecture:Any

[12 May 2012 15:22] Andrew Clayton
Description:
On at least Fedora 16 with glibc 2.14.90 and gcc 4.6.3, when including my_global.h and compiling with -std=c99 you end up with a non working __func__, i.e the function name is displayed as <unknown>

If you compile without -std=c99, __func__ works as expected.

This would seem to be due to the following in my_global.h

/* Provide __func__ macro definition for platforms that miss it. */
#if __STDC_VERSION__ < 199901L
#  if __GNUC__ >= 2
#    define __func__ __FUNCTION__
#  else
#    define __func__ "<unknown>"
#  endif
#elif defined(_MSC_VER)
#  if _MSC_VER < 1300
#    define __func__ "<unknown>"
#  else
#    define __func__ __FUNCTION__
#  endif
#elif defined(__BORLANDC__)
#  define __func__ __FUNC__
#else
#  define __func__ "<unknown>"
#endif

AFAICT if you don't compile with -std=c99, the __func__ is aliased to __FUNCTION__, however when using -stc=c99 you fall through to the

#else
#  define __func__ "<unknown>"

part.

How to repeat:
Compile a program on current Linux with -std=c99 that includes my_global.h and try to use the __func__ feature.

Suggested fix:
It seems you want to skip this altogether if __STDC_VERSION__ >= 199901L
[12 May 2012 15:23] Andrew Clayton
Simple test case for __func__ + my_global.h

Attachment: funcname.c (text/x-csrc), 164 bytes.

[12 May 2012 15:30] Andrew Clayton
When compiling the test program with

gcc -Wall -std=c99 -O2 -o funcname funcname.c `mysql-config --cflags --libs`

it will show the function name as <unknown>.

When compiling with

gcc -Wall -O2 -o funcname funcname.c `mysql-config --cflags --libs`

it displays as: main

A simple workaround seems to be to do the following in your code

#ifdef __func__
#undef __func__
#endif
[14 May 2012 10:57] Valeriy Kravchuk
Thank you for the problem report. I also see difference/problems on FC14 using current mysql-5.5:

[openxs@chief 5.5]$ gcc -Wall -O2 -o funcname funcname.c `bin/mysql_config --cflags --libs`
funcname.c: In function ‘main’:
funcname.c:6:14: warning: unused parameter ‘argc’
funcname.c:6:27: warning: unused parameter ‘argv’
[openxs@chief 5.5]$ gcc -Wall -std=c99 -O2 -o funcname funcname.c `bin/mysql_config --cflags --libs`
In file included from funcname.c:4:0:
/home/openxs/dbs/5.5/include/my_global.h:971:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘nesting_map’
In file included from /home/openxs/dbs/5.5/include/my_global.h:1039:0,
                 from funcname.c:4:
/home/openxs/dbs/5.5/include/my_dbug.h:37:3: error: expected specifier-qualifier-list before ‘uint’
/home/openxs/dbs/5.5/include/my_dbug.h:54:64: error: expected declaration specifiers or ‘...’ before ‘uint’
/home/openxs/dbs/5.5/include/my_dbug.h:56:31: error: expected ‘)’ before ‘_line_’
/home/openxs/dbs/5.5/include/my_dbug.h:57:30: error: expected ‘)’ before ‘_line_’
/home/openxs/dbs/5.5/include/my_dbug.h:60:29: error: expected ‘)’ before ‘_line_’
funcname.c: In function ‘main’:
funcname.c:6:14: warning: unused parameter ‘argc’
funcname.c:6:27: warning: unused parameter ‘argv’
[openxs@chief 5.5]$ gcc --version
gcc (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[openxs@chief 5.5]$ getconf GNU_LIBC_VERSION
glibc 2.13