bug-gnulib
[Top][All Lists]
Advanced

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

improve clang support (19)


From: Bruno Haible
Subject: improve clang support (19)
Date: Sun, 09 Aug 2020 20:10:23 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-186-generic; KDE/5.18.0; x86_64; ; )

Our use of __attribute__((__fallthrough__)) started here:
<https://lists.gnu.org/archive/html/bug-gnulib/2017-05/msg00177.html>

At the time, Jim wrote:
  When building diffutils with latest gnulib and gcc.git, the
  fallthrough-like comments did not suppress warnings

But in the released versions of GCC, a /* FALLTHROUGH */ comment
*does* suppress the -Wimplicit-fallthrough warning that GCC 7 introduced.

Test case:
=================== foo.c ==================
int foo (int x, int y)
{
  switch (x)
    {
    case 1:
      y += 4;
      /* FALLTHROUGH */
    case 2:
      y *= 3;
      break;
    }
  return x + y;
}
============================================
$ gcc -O2 -Wall -Wimplicit-fallthrough -S foo.c
<no warning>

Tested with GCC versions 7.5.0, 8.4.0, 9.3.0, 10.2.0.

So, when it comes to GCC, replacing the comments with the
  __attribute__((__fallthrough__));
statements was not needed.

But clang, now, in version 10, also activated -Wimplicit-fallthrough warnings.
(Earlier versions of clang already had the option, but it produces no warning,
at least in the test case above.) And in clang 10,
  - the /* FALLTHROUGH */ comment does *not* suppress the warning,
  - a statement   __attribute__((__fallthrough__));   does,
  - in C++ mode, a statement   [[fallthrough]];       does as well.

The __attribute__((__fallthrough__)); syntax should not be used with earlier
versions of clang, however, as it then produces a warning
   declaration does not declare anything

Documentation: <https://clang.llvm.org/docs/AttributeReference.html#fallthrough>


The definition of _GL_ATTRIBUTE_FALLTHROUGH in m4/gnulib-common.m4 and of
FALLTHROUGH in attribute.h works fine with clang 10. But in a couple of other
places, the definition of the macro needs to be adjusted, in order to avoid
warnings from clang 10. (Maybe some people will try to compile glibc with
clang at some point in the future, who knows...)


2020-08-09  Bruno Haible  <bruno@clisp.org>

        Silence warnings from clang 10 with -Wimplicit-fallthrough.
        * lib/dfa.c (FALLTHROUGH): Use __attribute__ __fallthrough__ also on
        clang >= 10.
        * lib/fnmatch.c (FALLTHROUGH): Likewise.
        * lib/fts.c (FALLTHROUGH): Likewise.
        * tests/macros.h (FALLTHROUGH): Likewise.
        * lib/regex_internal.h (FALLTHROUGH): Likewise.
        * config/srclist.txt: Mark it as needing sync with glibc.

diff --git a/lib/dfa.c b/lib/dfa.c
index e79d882..1f0587a 100644
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -64,10 +64,10 @@ isasciidigit (char c)
 #ifndef FALLTHROUGH
 # if 201710L < __STDC_VERSION__
 #  define FALLTHROUGH [[__fallthrough__]]
-# elif __GNUC__ < 7
-#  define FALLTHROUGH ((void) 0)
-# else
+# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
+# else
+#  define FALLTHROUGH ((void) 0)
 # endif
 #endif
 
diff --git a/lib/fnmatch.c b/lib/fnmatch.c
index 4d017cf..3014428 100644
--- a/lib/fnmatch.c
+++ b/lib/fnmatch.c
@@ -64,10 +64,10 @@ extern int fnmatch (const char *pattern, const char 
*string, int flags);
 #endif
 
 #ifdef _LIBC
-# if __GNUC__ < 7
-#  define FALLTHROUGH ((void) 0)
-# else
+# if (__GNUC__ >= 7) || (__clang_major__ >= 10)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
+# else
+#  define FALLTHROUGH ((void) 0)
 # endif
 #else
 # include "attribute.h"
diff --git a/lib/fts.c b/lib/fts.c
index a34092c..a7deaa0 100644
--- a/lib/fts.c
+++ b/lib/fts.c
@@ -201,10 +201,10 @@ enum Fts_stat
 #endif
 
 #ifndef FALLTHROUGH
-# if __GNUC__ < 7
-#  define FALLTHROUGH ((void) 0)
-# else
+# if (__GNUC__ >= 7) || (__clang_major__ >= 10)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
+# else
+#  define FALLTHROUGH ((void) 0)
 # endif
 #endif
 
diff --git a/lib/regex_internal.h b/lib/regex_internal.h
index 8c42586..df5303c 100644
--- a/lib/regex_internal.h
+++ b/lib/regex_internal.h
@@ -841,10 +841,10 @@ re_string_elem_size_at (const re_string_t *pstr, Idx idx)
 #endif /* RE_ENABLE_I18N */
 
 #ifndef FALLTHROUGH
-# if __GNUC__ < 7
-#  define FALLTHROUGH ((void) 0)
-# else
+# if (__GNUC__ >= 7) || (__clang_major__ >= 10)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
+# else
+#  define FALLTHROUGH ((void) 0)
 # endif
 #endif
 
diff --git a/tests/macros.h b/tests/macros.h
index c72cc92..bbc340d 100644
--- a/tests/macros.h
+++ b/tests/macros.h
@@ -22,10 +22,10 @@
 #include <stdlib.h>
 
 #ifndef FALLTHROUGH
-# if __GNUC__ < 7
-#  define FALLTHROUGH ((void) 0)
-# else
+# if (__GNUC__ >= 7) || (__clang_major__ >= 10)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
+# else
+#  define FALLTHROUGH ((void) 0)
 # endif
 #endif
 
diff --git a/config/srclist.txt b/config/srclist.txt
index 58b34d7..429cb94 100644
--- a/config/srclist.txt
+++ b/config/srclist.txt
@@ -58,7 +58,7 @@ $LIBCSRC posix/regcomp.c              lib
 $LIBCSRC posix/regex.c                 lib
 $LIBCSRC posix/regex.h                 lib
 $LIBCSRC posix/regex_internal.c                lib
-$LIBCSRC posix/regex_internal.h                lib
+#$LIBCSRC posix/regex_internal.h               lib
 $LIBCSRC posix/regexec.c               lib
 $LIBCSRC time/timegm.c                 lib
 $LIBCSRC time/mktime.c                 lib






reply via email to

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