bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* Apple LLVM 10 and `__fallthrough__`
@ 2023-02-22 18:53 Werner LEMBERG
  2023-02-26 16:04 ` Bruno Haible
  0 siblings, 1 reply; 10+ messages in thread
From: Werner LEMBERG @ 2023-02-22 18:53 UTC (permalink / raw)
  To: bug-gnulib


This snippet

```
#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.
```

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.


    Werner


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Apple LLVM 10 and `__fallthrough__`
  2023-02-22 18:53 Werner LEMBERG
@ 2023-02-26 16:04 ` Bruno Haible
  2023-02-26 17:26   ` Werner LEMBERG
  0 siblings, 1 reply; 10+ messages in thread
From: Bruno Haible @ 2023-02-26 16:04 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Werner LEMBERG

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)





^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: Apple LLVM 10 and `__fallthrough__`
  2023-02-26 16:04 ` Bruno Haible
@ 2023-02-26 17:26   ` Werner LEMBERG
  0 siblings, 0 replies; 10+ messages in thread
From: Werner LEMBERG @ 2023-02-26 17:26 UTC (permalink / raw)
  To: bruno; +Cc: bug-gnulib


> 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: [...]

Thanks!  Maybe other Apple users chime in so that the test can be
refined if necessary.


    Werner


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Apple LLVM 10 and `__fallthrough__`
@ 2023-02-26 22:41 Alexei Podtelezhnikov
  2023-02-27  0:08 ` Bruno Haible
  2023-02-27  0:20 ` Jeffrey Walton
  0 siblings, 2 replies; 10+ messages in thread
From: Alexei Podtelezhnikov @ 2023-02-26 22:41 UTC (permalink / raw)
  To: bug-gnulib

-# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
+# elif ((__GNUC__ >= 7) \
+        || (defined __apple_build_version__ \
+            ? __apple_build_version__ >= 14000000 \
+            : __clang_major__ >= 10))

Wiki suggests __apple_build_version__ >= 12000000
https://en.wikipedia.org/wiki/Xcode#Xcode_11.0_-_14.x_(since_SwiftUI_framework)_2

Would  it perhaps be better if clang used [[fallthrough]]  instead of
__attribute__((fallthrough))? They have supported [[fallthrough]]
since at least 3.5.0 circa 2014. In this particular case they are
ahead of GCC and towards the standard acceptance.
https://releases.llvm.org/3.5.0/tools/clang/docs/AttributeReference.html

There was also a suggestion on this list to use __has_attribute.
https://lists.gnu.org/archive/html/bug-gnulib/2021-01/msg00267.html


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Apple LLVM 10 and `__fallthrough__`
  2023-02-26 22:41 Apple LLVM 10 and `__fallthrough__` Alexei Podtelezhnikov
@ 2023-02-27  0:08 ` Bruno Haible
  2023-02-27  3:07   ` Alexei Podtelezhnikov
  2023-02-27  0:20 ` Jeffrey Walton
  1 sibling, 1 reply; 10+ messages in thread
From: Bruno Haible @ 2023-02-27  0:08 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Alexei Podtelezhnikov

Alexei Podtelezhnikov wrote:
> -# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
> +# elif ((__GNUC__ >= 7) \
> +        || (defined __apple_build_version__ \
> +            ? __apple_build_version__ >= 14000000 \
> +            : __clang_major__ >= 10))
> 
> Wiki suggests __apple_build_version__ >= 12000000
> https://en.wikipedia.org/wiki/Xcode#Xcode_11.0_-_14.x_(since_SwiftUI_framework)_2

Interesting. Can you test it (by compiling Werner's test program from
<https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00159.html>
on an Xcode version between 12.0 and 12.4)?

> Would  it perhaps be better if clang used [[fallthrough]]  instead of
> __attribute__((fallthrough))? They have supported [[fallthrough]]
> since at least 3.5.0 circa 2014.

They have supported it only in C++ mode. Not in C mode. E.g. with clang 4.0.0:

$ clang -S foo.c
foo.c:9:9: error: expected expression
        [[fallthrough]];
        ^
foo.c:12:9: error: expected expression
        [[fallthrough]];
        ^
2 errors generated.

> There was also a suggestion on this list to use __has_attribute.
> https://lists.gnu.org/archive/html/bug-gnulib/2021-01/msg00267.html

We do so in gnulib-common.m4, and the resulting code is more convoluted
than the code we have now.

Also, we're not using '#include "attribute.h"' in dfa.c because gawk uses
dfa.c and does not like to import so many Gnulib definitions.

Bruno





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Apple LLVM 10 and `__fallthrough__`
  2023-02-26 22:41 Apple LLVM 10 and `__fallthrough__` Alexei Podtelezhnikov
  2023-02-27  0:08 ` Bruno Haible
@ 2023-02-27  0:20 ` Jeffrey Walton
  1 sibling, 0 replies; 10+ messages in thread
From: Jeffrey Walton @ 2023-02-27  0:20 UTC (permalink / raw)
  To: Alexei Podtelezhnikov; +Cc: bug-gnulib

On Sun, Feb 26, 2023 at 6:36 PM Alexei Podtelezhnikov
<apodtele@gmail.com> wrote:
> [...]
> Would  it perhaps be better if clang used [[fallthrough]]  instead of
> __attribute__((fallthrough))? They have supported [[fallthrough]]
> since at least 3.5.0 circa 2014. In this particular case they are
> ahead of GCC and towards the standard acceptance.
> https://releases.llvm.org/3.5.0/tools/clang/docs/AttributeReference.html

I think the best course of action is to use a C comment. GCC and Clang
recognize them, and it does not require compiler extensions or new C
language features:

    switch (n)
    {
        case 1:
            n--;
            /* fallthrough */
        case 0:
            /* do something */
            break;
        default:
            /* do something else */
    }

Jeff


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Apple LLVM 10 and `__fallthrough__`
  2023-02-27  0:08 ` Bruno Haible
@ 2023-02-27  3:07   ` Alexei Podtelezhnikov
  2023-02-27  3:14     ` Jeffrey Walton
                       ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Alexei Podtelezhnikov @ 2023-02-27  3:07 UTC (permalink / raw)
  To: Bruno Haible; +Cc: bug-gnulib

On Sun, Feb 26, 2023 at 7:08 PM Bruno Haible <bruno@clisp.org> wrote:
>
> Alexei Podtelezhnikov wrote:
> > -# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
> > +# elif ((__GNUC__ >= 7) \
> > +        || (defined __apple_build_version__ \
> > +            ? __apple_build_version__ >= 14000000 \
> > +            : __clang_major__ >= 10))
> >
> > Wiki suggests __apple_build_version__ >= 12000000
> > https://en.wikipedia.org/wiki/Xcode#Xcode_11.0_-_14.x_(since_SwiftUI_framework)_2
>
> Interesting. Can you test it (by compiling Werner's test program from
> <https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00159.html>
> on an Xcode version between 12.0 and 12.4)?

I wish I could directly test it. However, I could confirm that Swift
release 5.3 (Xcode 12) was the first release with this line
https://github.com/apple/llvm-project/blob/swift-5.3-RELEASE/clang/test/Sema/fallthrough-attr.c#L18


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Apple LLVM 10 and `__fallthrough__`
  2023-02-27  3:07   ` Alexei Podtelezhnikov
@ 2023-02-27  3:14     ` Jeffrey Walton
  2023-02-27 12:32     ` Alexei Podtelezhnikov
  2023-02-28  5:03     ` Bruno Haible
  2 siblings, 0 replies; 10+ messages in thread
From: Jeffrey Walton @ 2023-02-27  3:14 UTC (permalink / raw)
  To: Alexei Podtelezhnikov; +Cc: Bruno Haible, bug-gnulib

On Sun, Feb 26, 2023 at 10:08 PM Alexei Podtelezhnikov
<apodtele@gmail.com> wrote:
>
> On Sun, Feb 26, 2023 at 7:08 PM Bruno Haible <bruno@clisp.org> wrote:
> >
> > Alexei Podtelezhnikov wrote:
> > > -# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
> > > +# elif ((__GNUC__ >= 7) \
> > > +        || (defined __apple_build_version__ \
> > > +            ? __apple_build_version__ >= 14000000 \
> > > +            : __clang_major__ >= 10))
> > >
> > > Wiki suggests __apple_build_version__ >= 12000000
> > > https://en.wikipedia.org/wiki/Xcode#Xcode_11.0_-_14.x_(since_SwiftUI_framework)_2
> >
> > Interesting. Can you test it (by compiling Werner's test program from
> > <https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00159.html>
> > on an Xcode version between 12.0 and 12.4)?
>
> I wish I could directly test it. However, I could confirm that Swift
> release 5.3 (Xcode 12) was the first release with this line
> https://github.com/apple/llvm-project/blob/swift-5.3-RELEASE/clang/test/Sema/fallthrough-attr.c#L18

Why jump through the compiler version hoops for a non-portable solution?

The simplest solution is to use a /* fallthrough */ comment:
https://stackoverflow.com/a/45137452 . It does not depend on compiler
extensions or language versions.

Jeff


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Apple LLVM 10 and `__fallthrough__`
  2023-02-27  3:07   ` Alexei Podtelezhnikov
  2023-02-27  3:14     ` Jeffrey Walton
@ 2023-02-27 12:32     ` Alexei Podtelezhnikov
  2023-02-28  5:03     ` Bruno Haible
  2 siblings, 0 replies; 10+ messages in thread
From: Alexei Podtelezhnikov @ 2023-02-27 12:32 UTC (permalink / raw)
  To: Bruno Haible; +Cc: bug-gnulib

> > > -# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
> > > +# elif ((__GNUC__ >= 7) \
> > > +        || (defined __apple_build_version__ \
> > > +            ? __apple_build_version__ >= 14000000 \
> > > +            : __clang_major__ >= 10))
> > >
> > > Wiki suggests __apple_build_version__ >= 12000000
> > > https://en.wikipedia.org/wiki/Xcode#Xcode_11.0_-_14.x_(since_SwiftUI_framework)_2

Alternative Xcode versioning source found:
https://github.com/gsl-lite/gsl-lite/blob/master/include/gsl/gsl-lite.hpp#L534-L557


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Apple LLVM 10 and `__fallthrough__`
  2023-02-27  3:07   ` Alexei Podtelezhnikov
  2023-02-27  3:14     ` Jeffrey Walton
  2023-02-27 12:32     ` Alexei Podtelezhnikov
@ 2023-02-28  5:03     ` Bruno Haible
  2 siblings, 0 replies; 10+ messages in thread
From: Bruno Haible @ 2023-02-28  5:03 UTC (permalink / raw)
  To: Alexei Podtelezhnikov; +Cc: bug-gnulib

Alexei Podtelezhnikov wrote:
> > > Wiki suggests __apple_build_version__ >= 12000000
> > > https://en.wikipedia.org/wiki/Xcode#Xcode_11.0_-_14.x_(since_SwiftUI_framework)_2
> ...
> I wish I could directly test it. However, I could confirm that Swift
> release 5.3 (Xcode 12) was the first release with this line
> https://github.com/apple/llvm-project/blob/swift-5.3-RELEASE/clang/test/Sema/fallthrough-attr.c#L18

Thanks for the investigation. Although we can't test it, these are two
independent hints that __apple_build_version__ >= 12000000 is OK. Applied:


2023-02-28  Bruno Haible  <bruno@clisp.org>

	dfa: Tweak the last patch.
	Suggested by Alexei Podtelezhnikov <apodtele@gmail.com>.
	* lib/dfa.c (FALLTHROUGH): Assume that Apple clang, in C mode, supports
	__attribute__ ((__fallthrough__)) starting with version 1200.
	References:
	https://en.wikipedia.org/wiki/Xcode#Xcode_11.0_-_14.x_(since_SwiftUI_framework)_2
	https://github.com/apple/llvm-project/blob/swift-5.3-RELEASE/clang/test/Sema/fallthrough-attr.c

diff --git a/lib/dfa.c b/lib/dfa.c
index 994900fea2..20502a802f 100644
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -69,7 +69,7 @@ c_isdigit (char c)
 #  define FALLTHROUGH [[__fallthrough__]]
 # elif ((__GNUC__ >= 7) \
         || (defined __apple_build_version__ \
-            ? __apple_build_version__ >= 14000000 \
+            ? __apple_build_version__ >= 12000000 \
             : __clang_major__ >= 10))
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else





^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-02-28  5:03 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-26 22:41 Apple LLVM 10 and `__fallthrough__` Alexei Podtelezhnikov
2023-02-27  0:08 ` Bruno Haible
2023-02-27  3:07   ` Alexei Podtelezhnikov
2023-02-27  3:14     ` Jeffrey Walton
2023-02-27 12:32     ` Alexei Podtelezhnikov
2023-02-28  5:03     ` Bruno Haible
2023-02-27  0:20 ` Jeffrey Walton
  -- strict thread matches above, loose matches on Subject: below --
2023-02-22 18:53 Werner LEMBERG
2023-02-26 16:04 ` Bruno Haible
2023-02-26 17:26   ` Werner LEMBERG

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).