Re: Apple LLVM 10 and `__fallthrough__`

2023-02-27 Thread Bruno Haible
Alexei Podtelezhnikov wrote:
> > > Wiki suggests __apple_build_version__ >= 1200
> > > 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__ >= 1200 is OK. Applied:


2023-02-28  Bruno Haible  

dfa: Tweak the last patch.
Suggested by Alexei Podtelezhnikov .
* 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__ >= 1400 \
+? __apple_build_version__ >= 1200 \
 : __clang_major__ >= 10))
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else






Re: Apple LLVM 10 and `__fallthrough__`

2023-02-27 Thread Alexei Podtelezhnikov
> > > -# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
> > > +# elif ((__GNUC__ >= 7) \
> > > +|| (defined __apple_build_version__ \
> > > +? __apple_build_version__ >= 1400 \
> > > +: __clang_major__ >= 10))
> > >
> > > Wiki suggests __apple_build_version__ >= 1200
> > > 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



Re: Apple LLVM 10 and `__fallthrough__`

2023-02-26 Thread Jeffrey Walton
On Sun, Feb 26, 2023 at 10:08 PM Alexei Podtelezhnikov
 wrote:
>
> On Sun, Feb 26, 2023 at 7:08 PM Bruno Haible  wrote:
> >
> > Alexei Podtelezhnikov wrote:
> > > -# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
> > > +# elif ((__GNUC__ >= 7) \
> > > +|| (defined __apple_build_version__ \
> > > +? __apple_build_version__ >= 1400 \
> > > +: __clang_major__ >= 10))
> > >
> > > Wiki suggests __apple_build_version__ >= 1200
> > > 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
> > 
> > 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



Re: Apple LLVM 10 and `__fallthrough__`

2023-02-26 Thread Alexei Podtelezhnikov
On Sun, Feb 26, 2023 at 7:08 PM Bruno Haible  wrote:
>
> Alexei Podtelezhnikov wrote:
> > -# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
> > +# elif ((__GNUC__ >= 7) \
> > +|| (defined __apple_build_version__ \
> > +? __apple_build_version__ >= 1400 \
> > +: __clang_major__ >= 10))
> >
> > Wiki suggests __apple_build_version__ >= 1200
> > 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
> 
> 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



Re: Apple LLVM 10 and `__fallthrough__`

2023-02-26 Thread Jeffrey Walton
On Sun, Feb 26, 2023 at 6:36 PM Alexei Podtelezhnikov
 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



Re: Apple LLVM 10 and `__fallthrough__`

2023-02-26 Thread Bruno Haible
Alexei Podtelezhnikov wrote:
> -# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
> +# elif ((__GNUC__ >= 7) \
> +|| (defined __apple_build_version__ \
> +? __apple_build_version__ >= 1400 \
> +: __clang_major__ >= 10))
> 
> Wiki suggests __apple_build_version__ >= 1200
> 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

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






Re: Apple LLVM 10 and `__fallthrough__`

2023-02-26 Thread Alexei Podtelezhnikov
-# elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
+# elif ((__GNUC__ >= 7) \
+|| (defined __apple_build_version__ \
+? __apple_build_version__ >= 1400 \
+: __clang_major__ >= 10))

Wiki suggests __apple_build_version__ >= 1200
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



Re: Apple LLVM 10 and `__fallthrough__`

2023-02-26 Thread Werner LEMBERG


> 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



Re: Apple LLVM 10 and `__fallthrough__`

2023-02-26 Thread Bruno Haible
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
> 
> 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  

dfa: Avoid warnings with some Apple clang versions.
Reported by Werner Lemberg  in
.
* 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__ >= 1400 \
+: __clang_major__ >= 10))
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)






Apple LLVM 10 and `__fallthrough__`

2023-02-22 Thread Werner LEMBERG


This snippet

```
#include

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