bug-gnulib
[Top][All Lists]
Advanced

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

Fix compilation errors in C++ mode on Solaris 11 OpenIndiana


From: Bruno Haible
Subject: Fix compilation errors in C++ mode on Solaris 11 OpenIndiana
Date: Tue, 27 Dec 2022 11:33:08 +0100

The newest Solaris OpenIndiana release (OpenIndiana 2022.10) comes with
GCC 11. Therefore the _GL_ATTRIBUTE_DEALLOC and _GL_ATTRIBUTE_DEALLOC_FREE
macros start to be effective on this platform. A gnulib testdir shows a
couple of compilation errors in C++ files, see attached file 'log2b'.

I could reduce the input file to this small one:
=============================== i.cc ===============================
extern "C" {
namespace std {
extern void free(void *);
}
}
using std::free;
extern "C" {
extern wchar_t *wcsdup(const wchar_t *);
}
extern "C" void free (void *);
extern "C" wchar_t * wcsdup (const wchar_t *s) 
__attribute__ ((__malloc__)) __attribute__ ((__malloc__ (free, 1)));
====================================================================
$ g++ -S i.cc
i.cc:12:67: error: ‘malloc’ attribute argument 1 is ambiguous
   12 | __attribute__ ((__malloc__)) __attribute__ ((__malloc__ (free, 1)))
      |                                                                   ^
i.cc:12:67: note: use a cast to the expected type to disambiguate

It's a g++ bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108231

There are three possible workarounds:
(1) Write
       __attribute__ ((__malloc__ ((void (*) (void *)) free, 1)))
    instead of
       __attribute__ ((__malloc__ (free, 1)))
(2) Use the command-line option -fno-builtin.
(3) Swap the order of declarations of 'free'.

I'm going with (1), since (2) is not good for production code and (3) is
hairy (depends on which include file gets included first, and what the
system include files contain).


2022-12-27  Bruno Haible  <bruno@clisp.org>

        Fix compilation errors in C++ mode on Solaris 11 OpenIndiana.
        * m4/gnulib-common.m4 (gl_COMMON_BODY): In _GL_ATTRIBUTE_DEALLOC_FREE,
        with GNU C++, cast the 'free' function.
        * lib/string.in.h (_GL_ATTRIBUTE_DEALLOC_FREE): With GNU C++, cast the
        'free' function.
        * lib/wchar.in.h (_GL_ATTRIBUTE_DEALLOC_FREE): Likewise.

diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4
index 728a006ce8..40c19cab69 100644
--- a/m4/gnulib-common.m4
+++ b/m4/gnulib-common.m4
@@ -1,4 +1,4 @@
-# gnulib-common.m4 serial 74
+# gnulib-common.m4 serial 75
 dnl Copyright (C) 2007-2022 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -187,7 +187,14 @@ AC_DEFUN([gl_COMMON_BODY], [
    to use this earlier definition, since <stdlib.h> may not have been included
    yet.  */
 #ifndef _GL_ATTRIBUTE_DEALLOC_FREE
-# define _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC (free, 1)
+# if defined __cplusplus && defined __GNUC__ && !defined __clang__
+/* Work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108231> */
+#  define _GL_ATTRIBUTE_DEALLOC_FREE \
+     _GL_ATTRIBUTE_DEALLOC ((void (*) (void *)) free, 1)
+# else
+#  define _GL_ATTRIBUTE_DEALLOC_FREE \
+     _GL_ATTRIBUTE_DEALLOC (free, 1)
+# endif
 #endif
 
 /* _GL_ATTRIBUTE_DEPRECATED: Declares that an entity is deprecated.
diff --git a/lib/string.in.h b/lib/string.in.h
index 21356914e2..64a0ab19f7 100644
--- a/lib/string.in.h
+++ b/lib/string.in.h
@@ -82,7 +82,14 @@
    can be freed via 'free'; it can be used only after declaring 'free'.  */
 /* Applies to: functions.  Cannot be used on inline functions.  */
 #ifndef _GL_ATTRIBUTE_DEALLOC_FREE
-# define _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC (free, 1)
+# if defined __cplusplus && defined __GNUC__ && !defined __clang__
+/* Work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108231> */
+#  define _GL_ATTRIBUTE_DEALLOC_FREE \
+     _GL_ATTRIBUTE_DEALLOC ((void (*) (void *)) free, 1)
+# else
+#  define _GL_ATTRIBUTE_DEALLOC_FREE \
+     _GL_ATTRIBUTE_DEALLOC (free, 1)
+# endif
 #endif
 
 /* _GL_ATTRIBUTE_MALLOC declares that the function returns a pointer to freshly
diff --git a/lib/wchar.in.h b/lib/wchar.in.h
index 3558adfb9d..70b8d7d259 100644
--- a/lib/wchar.in.h
+++ b/lib/wchar.in.h
@@ -99,7 +99,14 @@
    can be freed via 'free'; it can be used only after declaring 'free'.  */
 /* Applies to: functions.  Cannot be used on inline functions.  */
 #ifndef _GL_ATTRIBUTE_DEALLOC_FREE
-# define _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC (free, 1)
+# if defined __cplusplus && defined __GNUC__ && !defined __clang__
+/* Work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108231> */
+#  define _GL_ATTRIBUTE_DEALLOC_FREE \
+     _GL_ATTRIBUTE_DEALLOC ((void (*) (void *)) free, 1)
+# else
+#  define _GL_ATTRIBUTE_DEALLOC_FREE \
+     _GL_ATTRIBUTE_DEALLOC (free, 1)
+# endif
 #endif
 
 /* _GL_ATTRIBUTE_MALLOC declares that the function returns a pointer to freshly

Attachment: log2b
Description: Text document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]