Paul Eggert wrote:
> Since gnulib-common.m4 already has a macro _GL_GNUC_PREREQ that uses "defined 
> __GNUC__" to protect against complaints when __GNUC__ is undefined, perhaps 
> gnulib-common.m4 should add a macro _GL_CLANG_PREREQ that does something 
> similar for Clang.  That should help readability a bit for Clang, just as 
> we've already done so for GCC.

This would help only if we wanted to go to full elimination of -Wundef
warnings, with both GCC and clang. But this would require more changes:
  - Protect all uses of __clang_major__ in public *.h files.
  - Use AC_DEFINE_UNQUOTED instead of AC_DEFINE, AC_CHECK_DECL, AC_CHECK_FUNC
    for all HAVE_* macros that are used in public *.h files.

I would dislike this. I'm thus only make the requested change: uses of
__clang_major__ that happen when using a modern GCC in C mode.

The reason why Andrew Hamilton did not see warnings from other *.h files
is because we typically test
  #if (some GCC version) || (_clang_major__ >= 4)
and the "(some GCC version)" part already returns true.


2026-10-21  Bruno Haible  <[email protected]>

        Avoid -Wundef warnings about __clang_major__ with recent GCC in C mode.
        Reported by Andrew Hamilton <[email protected]> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2026-10/msg00091.html>.
        * m4/gnulib-common.m4 (gl_COMMON_BODY, gl_CC_GNULIB_WARNINGS): Test
        __clang__ before __clang_major__.

diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4
index 719c6275e4..a1d0f0d26d 100644
--- a/m4/gnulib-common.m4
+++ b/m4/gnulib-common.m4
@@ -1,5 +1,5 @@
 # gnulib-common.m4
-# serial 124
+# serial 125
 dnl Copyright (C) 2007-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -911,7 +911,7 @@ AC_DEFUN([gl_COMMON_BODY]
    -Wthread-safety is enabled.  For usage, see
    <https://clang.llvm.org/docs/ThreadSafetyAnalysis.html>.  */
 #ifndef _GL_ATTRIBUTE_CAPABILITY_TYPE
-# if __clang_major__ >= 15
+# if defined __clang__ && __clang_major__ >= 15
 #  define _GL_ATTRIBUTE_CAPABILITY_TYPE(concept) \
      __attribute__ ((__capability__ (concept)))
 # else
@@ -919,7 +919,7 @@ AC_DEFUN([gl_COMMON_BODY]
 # endif
 #endif
 #ifndef _GL_ATTRIBUTE_ACQUIRE_CAPABILITY
-# if __clang_major__ >= 15
+# if defined __clang__ && __clang_major__ >= 15
 #  define _GL_ATTRIBUTE_ACQUIRE_CAPABILITY(resource) \
      __attribute__ ((__acquire_capability__ (resource)))
 # else
@@ -927,7 +927,7 @@ AC_DEFUN([gl_COMMON_BODY]
 # endif
 #endif
 #ifndef _GL_ATTRIBUTE_RELEASE_CAPABILITY
-# if __clang_major__ >= 15
+# if defined __clang__ && __clang_major__ >= 15
 #  define _GL_ATTRIBUTE_RELEASE_CAPABILITY(resource) \
      __attribute__ ((__release_capability__ (resource)))
 # else
@@ -1497,7 +1497,7 @@ AC_DEFUN([gl_CC_GNULIB_WARNINGS]
       #if (__GNUC__ + (__GNUC_MINOR__ >= 8) > 4 && !defined __clang__) || 
(__clang_major__ + (__clang_minor__ >= 9) > 3)
       -Wno-pedantic
       #endif
-      #if 3 < __clang_major__ + (9 <= __clang_minor__)
+      #if defined __clang__ && 3 < __clang_major__ + (9 <= __clang_minor__)
       -Wno-string-plus-int
       -Wno-tautological-constant-out-of-range-compare
       #endif




Reply via email to