Bug #56506 __FUNCTION__ is not a macro in Linux gcc/g++
Submitted: 2 Sep 2010 16:50 Modified: 22 Sep 2010 12:32
Reporter: peter kwong Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / C++ Severity:S3 (Non-critical)
Version:1.0.5 OS:Linux
Assigned to: Ulf Wendel CPU Architecture:Any

[2 Sep 2010 16:50] peter kwong
Description:
Under Centos 5.2 with gcc/g++ 4.1.2-4

In files:

examples/examples.h
test/test_common.cpp

// Portable __FUNCTION__
#ifndef __FUNCTION__
 #ifdef __func__
   #define __FUNCTION__ __func__
 #else
   #define __FUNCTION__ "(function n/a)"
 #endif
#endif

// Portable __FUNCTION__
#ifndef __FUNCTION__
 #ifdef __func__
   #define CPPCONN_FUNC __func__
 #else
   #define CPPCONN_FUNC "(function n/a)"
 #endif
#else
#define CPPCONN_FUNC __FUNCTION__
#endif

the "#ifndef __FUNCTION__" does not work for Linux gcc/g++ 4.1.2-42
because __FUNCTION__ is **NOT** a macro, but __FUNCTION__ does exist
and does work.

So the "#ifndef" will always be true (because __FUNCTION__ is not
defined as a macro), and so CPPCONN_FUNC gets set
incorrectly to "(function n/a)".

How to repeat:
Just compile the test_common.cpp, for example, and run test_common.
Check the log output
where __FUNCTION__ is used and it will say "(function n/a)".

Suggested fix:
Perhaps a better way is make this check a platform check rather than
assume __FUNCTION__ is a macro.

__FUNCTION__ does exist, it is just not a macro in that particular
compiler.
[10 Sep 2010 4:06] Alexey Kishkin
Verified on centos 5.3, gcc version 4.1.2 20080704 (Red Hat 4.1.2-44)

$ ./static_test
1..2
#
# Client version: 5.1.36
# 0 - total_errors 0, last_error_total = 0
# Host=tcp://127.0.0.1
# User=root
#---------------  1 -----------------
# 
# ERR: Caught sql::SQLException at (function n/a)::2139  [Can't connect to MySQL server on '127.0.0.1' (111)] (2003/HY000)
not ok

$ cat /etc/redhat-release 
CentOS release 5.3 (Final)

and on ubuntu 10.4 with gcc version 4.4.3 
 
$ ./static_test
1..2
#
# Client version: 5.1.41
# 0 - total_errors 0, last_error_total = 0
# Host=tcp://127.0.0.1
# User=root
#---------------  1 -----------------
# 
# ERR: Caught sql::SQLException at (function n/a)::2139  [Access denied for user 'root'@'localhost' (using password: YES)] (1045/28000)
not ok

in both cases no function names in the output.
[22 Sep 2010 10:07] Ulf Wendel
__FUNCTION__ does not follow any standards, AFAIK. I am not sure if we shall "fix" this. Its about example code. Whatever we do, there will always be a compiler or OS that does it differently. Maybe we take the GCC part from libmysql and that's it?

For comparison:

MySQL 5.1.50 - sql/sql_profile.h 

#ifndef __func__
#ifdef __FUNCTION__
#define __func__ __FUNCTION__
#else
#define __func__ "unknown function"
#endif
#endif

Libmysql - libmysql/include/my_global.h:

#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
[22 Sep 2010 12:32] Ulf Wendel
This has been fixed in the development tree (revision 899) and will be available in the next GA version which is 1.1.1.