Support FALLTHROUGH macro better in glibc+clang

2023-02-26 Thread Bruno Haible
Looking through the FALLTHROUGH definitions in gnulib, it appears that it
has the assumption built-in that glibc will only be built by GCC.

Nowadays people are alternatively using (non-Apple) clang to build glibc, see
https://www.collabora.com/news-and-blog/blog/2021/09/30/a-tale-of-two-toolchains-and-glibc/
https://www.collabora.com/news-and-blog/blog/2023/01/17/a-brave-new-world-building-glibc-with-llvm/

To help going in this direction, how about this patch? Paul, do you object?


2023-02-26  Bruno Haible  

Support FALLTHROUGH macro better in glibc+clang.
* lib/fnmatch.c (FALLTHROUGH): Use __attribute__ ((__fallthrough__))
also in clang >= 10.
* lib/fts.c (FALLTHROUGH): Likewise.
* lib/regex_internal.h (FALLTHROUGH): Likewise.

diff --git a/lib/fnmatch.c b/lib/fnmatch.c
index 7c9c4e0f24..306b967c62 100644
--- a/lib/fnmatch.c
+++ b/lib/fnmatch.c
@@ -64,7 +64,7 @@ extern int fnmatch (const char *pattern, const char *string, 
int flags);
 #endif
 
 #ifdef _LIBC
-# if __GNUC__ >= 7
+# if (__GNUC__ >= 7) || (__clang_major__ >= 10)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)
diff --git a/lib/fts.c b/lib/fts.c
index 78584b2902..388d251252 100644
--- a/lib/fts.c
+++ b/lib/fts.c
@@ -203,7 +203,7 @@ enum Fts_stat
 #endif
 
 #ifdef _LIBC
-# if __GNUC__ >= 7
+# if (__GNUC__ >= 7) || (__clang_major__ >= 10)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)
diff --git a/lib/regex_internal.h b/lib/regex_internal.h
index 149ec2e868..d4dae98534 100644
--- a/lib/regex_internal.h
+++ b/lib/regex_internal.h
@@ -822,7 +822,7 @@ re_string_elem_size_at (const re_string_t *pstr, Idx idx)
 }
 
 #ifdef _LIBC
-# if __GNUC__ >= 7
+# if (__GNUC__ >= 7) || (__clang_major__ >= 10)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)






Re: Support FALLTHROUGH macro better in glibc+clang

2023-03-25 Thread Bruno Haible
Hi Paul,

Any objections against this proposed patch from
<https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00178.html> ?

> 2023-02-26  Bruno Haible  
> 
>   Support FALLTHROUGH macro better in glibc+clang.
>   * lib/fnmatch.c (FALLTHROUGH): Use __attribute__ ((__fallthrough__))
>   also in clang >= 10.
>   * lib/fts.c (FALLTHROUGH): Likewise.
>   * lib/regex_internal.h (FALLTHROUGH): Likewise.






Re: Support FALLTHROUGH macro better in glibc+clang

2023-03-25 Thread Paul Eggert

On 2023-02-26 08:43, Bruno Haible wrote:

-# if __GNUC__ >= 7
+# if (__GNUC__ >= 7) || (__clang_major__ >= 10)


Sorry I didn't see this earlier.

Since this is protected by #ifdef _LIBC" it might be better to do this 
the glibc way. Something like the following, perhaps?


   # if __GNUC_PREREQ (7,0) || __glibc_has_attribute (__fallthrough__)



Re: Support FALLTHROUGH macro better in glibc+clang

2023-03-25 Thread Paul Eggert

On 2023-03-25 14:24, Paul Eggert wrote:

    # if __GNUC_PREREQ (7,0) || __glibc_has_attribute (__fallthrough__)


Come to think of it this could be simplified further, to just:

  # if __glibc_has_attribute (__fallthrough__)

since GCC started supporting __has_attribute in GCC 5.



Re: Support FALLTHROUGH macro better in glibc+clang

2023-03-27 Thread Bruno Haible
Paul Eggert wrote:
> On 2023-03-25 14:24, Paul Eggert wrote:
> > # if __GNUC_PREREQ (7,0) || __glibc_has_attribute (__fallthrough__)
> 
> Come to think of it this could be simplified further, to just:
> 
># if __glibc_has_attribute (__fallthrough__)
> 
> since GCC started supporting __has_attribute in GCC 5.

Good point. I'm thus committing this in your name:


2023-03-27  Paul Eggert  

    Support FALLTHROUGH macro better in glibc+clang.
* lib/fnmatch.c (FALLTHROUGH): Use __attribute__ ((__fallthrough__))
also in clang >= 10.
* lib/fts.c (FALLTHROUGH): Likewise.
* lib/regex_internal.h (FALLTHROUGH): Likewise.

diff --git a/lib/fnmatch.c b/lib/fnmatch.c
index 7c9c4e0f24..32cfb48d0f 100644
--- a/lib/fnmatch.c
+++ b/lib/fnmatch.c
@@ -64,7 +64,7 @@ extern int fnmatch (const char *pattern, const char *string, 
int flags);
 #endif
 
 #ifdef _LIBC
-# if __GNUC__ >= 7
+# if __glibc_has_attribute (__fallthrough__)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)
diff --git a/lib/fts.c b/lib/fts.c
index 794a4f75d7..3fffb45d70 100644
--- a/lib/fts.c
+++ b/lib/fts.c
@@ -203,7 +203,7 @@ enum Fts_stat
 #endif
 
 #ifdef _LIBC
-# if __GNUC__ >= 7
+# if __glibc_has_attribute (__fallthrough__)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)
diff --git a/lib/regex_internal.h b/lib/regex_internal.h
index 149ec2e868..ae9257eac0 100644
--- a/lib/regex_internal.h
+++ b/lib/regex_internal.h
@@ -822,7 +822,7 @@ re_string_elem_size_at (const re_string_t *pstr, Idx idx)
 }
 
 #ifdef _LIBC
-# if __GNUC__ >= 7
+# if __glibc_has_attribute (__fallthrough__)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)