[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Apple LLVM 10 and `__fallthrough__`
From: |
Bruno Haible |
Subject: |
Re: Apple LLVM 10 and `__fallthrough__` |
Date: |
Sun, 26 Feb 2023 17:04:00 +0100 |
Hi Werner,
> In `lib/dfa.c` I see
>
> ```
> ...
> # elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
> # define FALLTHROUGH __attribute__ ((__fallthrough__))
> ...
> ```
>
> I now wonder whether it would be better to have a special case for
> Apple LLVM to avoid this warning.
Indeed, it's not the first time that we see that the Apple
compiler with a certain LLVM version behaves differently (more
like an older compiler) than the original LLVM compiler with that
version. One can use __apple_build_version__ or __APPLE_CC__ to detect
this situation.
> ```
> #include<stdio.h>
>
> int main(int argc, char* argv[])
> {
> switch(argc)
> {
> case 3:
> puts(argv[2]);
> __attribute__((fallthrough));
> case 2:
> puts(argv[1]);
> __attribute__((__fallthrough__));
> case 1:
> puts(argv[0]);
> /* fall through */
> default:
> puts("done");
> }
> }
> ```
>
> if compiled with
>
> ```
> $ llvm-gcc --version
> Apple LLVM version 10.0.0 (clang-1000.10.44.4)
> Target: x86_64-apple-darwin17.7.0
> Thread model: posix
> InstalledDir: /Library/Developer/CommandLineTools/usr/bin
> ```
>
> yields
>
> ```
> nicola@Quark:freetype $ clang main.c
> main.c:9:9: warning: declaration does not declare anything
> [-Wmissing-declarations]
> __attribute__((fallthrough));
> ^
> main.c:12:9: warning: declaration does not declare anything
> [-Wmissing-declarations]
> __attribute__((__fallthrough__));
> ^
> 2 warnings generated.
> ```
On the other hand, the same file, compiled by
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
produces no warnings.
I haven't been able to find out precisely which versions produce the warning,
by looking at https://github.com/apple/llvm-project . So, I'm applying the
conservative patch:
2023-02-26 Bruno Haible <bruno@clisp.org>
dfa: Avoid warnings with some Apple clang versions.
Reported by Werner Lemberg <wl@gnu.org> in
<https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00159.html>.
* lib/dfa.c (FALLTHROUGH): When __apple_build_version__ is defined,
ignore __clang_major__.
diff --git a/lib/dfa.c b/lib/dfa.c
index 211e1ed18f..994900fea2 100644
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -67,7 +67,10 @@ c_isdigit (char c)
#ifndef FALLTHROUGH
# if 201710L < __STDC_VERSION__
# define FALLTHROUGH [[__fallthrough__]]
-# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
+# elif ((__GNUC__ >= 7) \
+ || (defined __apple_build_version__ \
+ ? __apple_build_version__ >= 14000000 \
+ : __clang_major__ >= 10))
# define FALLTHROUGH __attribute__ ((__fallthrough__))
# else
# define FALLTHROUGH ((void) 0)