[llvm-bugs] [Bug 43743] clang-6: __has_cpp_attribute(nodiscard) == true with -std=c++14, but warns about usage with -pedantic

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Sep 25 12:03:55 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=43743

Aaron Ballman <aaron at aaronballman.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |WONTFIX
                 CC|                            |aaron at aaronballman.com

--- Comment #3 from Aaron Ballman <aaron at aaronballman.com> ---
(In reply to Hans Dembinski from comment #2)
> In Boost, we have a macro that expands to [[nodiscard]] when the attribute
> is supported, but the macro should not raise any warnings, -pedantic or not.
> So far we are checking
> 
> #if __has_cpp_attribute(nodiscard) ...
> 
> But maybe that is simply not how feature detection is supposed to work.
> After reading https://en.cppreference.com/w/cpp/feature_test I learned that
> __has_cpp_attribute(nodiscard) does not return simply true or false, but the
> date when the feature was added to the C++ standard.
> 
> So to get the desired behavior in Boost, we should probably do
> 
> #if __cplusplus > __has_cpp_attribute(nodiscard) ...
> 
> The branch is then taken only if the C++ version is high enough to
> officially include [[nodiscard]].
> 
> Would that make sense?

Feature testing macros only tell you if the compiler knows about the feature,
not whether the feature can be used without diagnostics. If you want to test
whether the compiler knows about the nodiscard attribute at all, you would use
`#if __has_cpp_attribute(nodiscard)`. If you want to test whether the compiler
knows about the nodiscard attribute where nodiscard can accept an optional
string-literal message, you would use `#if __has_cpp_attribute(nodiscard) >=
201907L` -- the date value returned tells you what additional functionality is
supported.

Using `#if __cplusplus > __has_cpp_attribute(nodiscard)` would work most of the
time but could fail in two circumstances I can think of: 0) when the attribute
date returned is the same as the date used by __cplusplus and 1) when the
attribute is known to Clang but doesn't conform to the standard. In that case,
we will often use a value that's less than what's specified in
http://eel.is/c++draft/cpp#tab:cpp.cond.ha (for instance, we're doing that with
the [[likely]] and [[unlikely]] attributes while fleshing out the
implementation). Neither of these circumstances apply for nodiscard in Clang,
but could apply to other attributes.

I am resolving this as WONTFIX because I think the current behavior is correct.
If there's strong sentiment that we should reconsider that, feel free to reopen
the bug with more details.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200925/705ad956/attachment.html>


More information about the llvm-bugs mailing list