[PATCH] glib/gmacros.h G_STMT_START/G_STMT_END modification



This patch will allow the proper usage of the "do while(0)" format macros for 
G_STMT_START / G_STMT_END on Solaris by pushing the detection of the broken 
support (which is believed to exist in SunOS4 compilers) to a configure-time 
#define. Without this patch, gcc in ANSI mode, g++, or any recent Forte/Sun 
Studio compiler on Solaris would incorrectly use the older SunOS4 macros. This 
has a side effect of causing gcc "abiguous else" warnings when compiling 
G_STMT_START/END based macros without explicit braces such as:

if (1) g_assert_not_reached();

If no one has objections, I ask that this be checked into glib CVS.

Thanks,

Andrew Paprocki
Bloomberg LP

--- configure.in        18 Nov 2005 14:01:59 -0000      1.474
+++ configure.in        20 Nov 2005 21:11:56 -0000
@@ -700,6 +700,21 @@
 }],[g_can_inline=yes],[g_can_inline=no])
 AC_MSG_RESULT($g_can_inline)

+dnl *** check for working do while(0) macros ***
+AC_CACHE_CHECK([for working do while(0) macros], g_support_dowhile_macros, [
+       AC_TRY_COMPILE([],[
+       #define STMT_START do
+       #define STMT_END while(0)
+       #define STMT_TEST STMT_START { i = 0; } STMT_END
+       int main(void) { int i = 1; STMT_TEST; return i; }],
+       [g_support_dowhile_macros=yes],
+       [g_support_dowhile_macros=no],
+       [g_support_dowhile_macros=yes])
+])
+if test x$g_support_dowhile_macros = xyes; then
+  AC_DEFINE(HAVE_DOWHILE_MACROS, 1, [define for working do while(0) macros])
+fi
+
 # check for flavours of varargs macros
 AC_MSG_CHECKING(for ISO C99 varargs macros in C)
 AC_TRY_COMPILE([],[
--- glib/gmacros.h      8 Mar 2005 05:41:42 -0000       1.26
+++ glib/gmacros.h      20 Nov 2005 21:11:56 -0000
@@ -194,23 +194,26 @@
  *  can be used as a single statement, as in
  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
  *
- *  For gcc we will wrap the statements within `({' and `})' braces.
- *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
- *  and otherwise within `do' and `while (0)'.
+ *  When GCC is compiling C code in non-ANSI mode, it will use the
+ *  compiler __extension__ to wrap the statements wihin `({' and '})' braces.
+ *  When compiling on platforms where configure has defined
+ *  HAVE_DOWHILE_MACROS, statements will be wrapped with `do' and `while (0)'.
+ *  For any other platforms (SunOS4 is known to have this issue), wrap the
+ *  statements with `if (1)' and `else (void) 0'.
  */
 #if !(defined (G_STMT_START) && defined (G_STMT_END))
-#  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined 
(__cplusplus)
-#    define G_STMT_START       (void) __extension__ (
-#    define G_STMT_END         )
-#  else
-#    if (defined (sun) || defined (__sun__))
-#      define G_STMT_START     if (1)
-#      define G_STMT_END       else (void)0
-#    else
-#      define G_STMT_START     do
-#      define G_STMT_END       while (0)
-#    endif
-#  endif
+# if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
+#  define G_STMT_START (void) __extension__ (
+#  define G_STMT_END )
+# else /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
+#  if defined (HAVE_DOWHILE_MACROS)
+#   define G_STMT_START do
+#   define G_STMT_END while (0)
+#  else /* !HAVE_DOWHILE_MACROS */
+#   define G_STMT_START if (1)
+#   define G_STMT_END else (void) 0
+#  endif /* !HAVE_DOWHILE_MACROS */
+# endif /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
 #endif

 /* Allow the app programmer to select whether or not return values





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]