Issue 244682
Summary Clarify intended meaning of `__has_feature` and `__has_extension`
Labels new issue
Assignees
Reporter zygoloid
    See also #153522, #153519, #84372.

We have a few different suggestions of what `__has_feature` and `__has_extension` are supposed to mean in different places.

Features.def says:
```
// FEATURE(...) should be used to advertise support for standard language
// features, whereas EXTENSION(...) should be used for clang extensions. Note
// that many of the identifiers in this file don't follow this rule for backward
// compatibility reasons.
```

The documentation initially agrees:

> These function-like macros take a single identifier argument that is the name of a feature. __has_feature evaluates to 1 if the feature is both supported by Clang and standardized in the current language standard or 0 if not (but see [below](https://clang.llvm.org/docs/LanguageExtensions.html#langext-has-feature-back-compat)), while __has_extension evaluates to 1 if the feature is supported by Clang in the current language (either as a language extension or a standard language feature) or 0 if not. They can be used like this:
> [...]
> For backward compatibility, __has_feature can also be used to test for support for non-standardized features, i.e., features not prefixed c_, cxx_ or objc_.

But also says this, which doesn't fit the above:

> Another use of __has_feature is to check for compiler features not related to the language standard, such as [AddressSanitizer](https://clang.llvm.org/docs/AddressSanitizer.html).

Finally this, which seems to present a third model for what the difference means:

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

Our features themselves include a great many things that have nothing to do with language standards, and almost all of them use `FEATURE`. The exceptions are almost all language standard features from later standards versions that are made available in earlier modes.

Drawing a distinction between standard language features and other things seems like a mistake to me. The overall mode that Clang is using to compile is a combination of language standard, explicitly enabled non-standard extensions, and a long tail of other odd things, and all of those are features.

### What should the rule be?

I think there are (at most) two questions that we should support source code asking via these feature checks:

1) Did the build configuration enable this feature? That is, were we given the *intent* from the command line that this feature be enabled? `FEATURE` and `__has_feature` should tell us this. This covers language standard features, sanitizers, and other `-f` flags that change which things we have been asked to accept.

2) Regardless of build configuration, can I actually use the feature? That is, if I really need it, will it *work in practice*? => `EXTENSION` and `__has_extension`. This covers everything that `__has_feature` covers plus also things that we have chosen to accept (with an `Extension` or `ExtWarn`) even though we weren't asked to. (And this ties into the connection to `-pedantic-errors`: under that mode, we do not "choose to" accept some of these extra things.)

I think that approach is closer to what we actually implement, and to how these features are used in practice, than our documentation is.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to