[libcxx-commits] [libcxx] [libc++] Enable availability based on the compiler instead of __has_extension (PR #84065)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Mar 5 12:33:21 PST 2024


ldionne wrote:

I could agree with that. Here's the analysis I did elsewhere, pasted:

The problem seems to be that `_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS` is disabled via

```
#if defined(_LIBCPP_BUILDING_LIBRARY) || defined(_LIBCXXABI_BUILDING_LIBRARY) ||                                       \
    !__has_feature(attribute_availability_with_strict) || !__has_feature(attribute_availability_in_templates) ||       \
    !__has_extension(pragma_clang_attribute_external_declaration)
#  if !defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
#    define _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS
#  endif
#endif
```

And I am able to reproduce the cause for that here locally with:

```
cat <<EOF | xcrun clang++ -xc++ - -pedantic -pedantic-errors -fsyntax-only 
#if !__has_extension(pragma_clang_attribute_external_declaration)
#error extension missing
#endif
EOF
```

I just found this in `clang/docs/LanguageExtensions.rst:143`:

> If the `-pedantic-errors option is given, __has_extension` is equivalent to `__has_feature`.

And in `clang/lib/Lex/PPMacroExpansion.cpp:1161`:

```
static bool HasExtension(const Preprocessor &PP, StringRef Extension) {
  if (HasFeature(PP, Extension))
    return true;

  // If the use of an extension results in an error diagnostic, extensions are
  // effectively unavailable, so just return false here.
  if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
      diag::Severity::Error)
    return false;

  const LangOptions &LangOpts = PP.getLangOpts();

  ...
```

So basically, it looks like `__has_extension(...)` always returns `0` when `-pedantic-errors` is used. That means that as it stands, libc++ is not compatible at all with `-pedantic-errors`, since it uses a good number of compiler extensions (and really there’s no way it could go without using compiler extensions).

https://github.com/llvm/llvm-project/pull/84065


More information about the libcxx-commits mailing list