[PATCH] D45179: [libc++] Add _LIBCPP_FORCE_NODISCARD define to force-enable nodiscard in pre-C++17

Roman Lebedev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 11 10:49:22 PDT 2018


lebedev.ri added a comment.

I'm waiting for @mclow.lists to have the final say re this differential.

In https://reviews.llvm.org/D45179#1057187, @Quuxplusone wrote:

> In https://reviews.llvm.org/D45179#1056706, @lebedev.ri wrote:
>
> > gcc does not silence the warning produced by
> >  these attributes via `(void)` cast, unless it's `[[nodiscard]]` attribute :/
> >  [...] So until they fix that (they will fix that right? :)),
> >  we can only support `_LIBCPP_FORCE_NODISCARD` for clang compilers.
>
>
> The GCC warn_unused_result issue is https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509 and also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 . I would certainly love for them to fix it, but the sentiment expressed in those bug reports is "no! never! we love being broken!". :/
>  I re-suggest my idea that perhaps libc++ should respect the user's choice to pass `-D_LIBCPP_NODISCARD=__attribute__((warn_unused_result))` on the GCC command line. This would make your patch only one line longer than it is right now...


So roughly:

  // NOTE: Do not use [[nodiscard]] in pre-C++17 mode
  //       to avoid -Wc++17-extensions warning.
  // And we can't use GCC's [[gnu::warn_unused_result]] and
  // __attribute__((warn_unused_result)),
  // because GCC does not silence them via (void) cast.
  #if !defined(_LIBCPP_NODISCARD)
  #  if __has_cpp_attribute(nodiscard) && _LIBCPP_STD_VER > 17
  #    define _LIBCPP_NODISCARD [[nodiscard]]
  #  elif __has_cpp_attribute(clang::warn_unused_result)
  #    define _LIBCPP_NODISCARD [[clang::warn_unused_result]]
  #  else
  #    define _LIBCPP_NODISCARD
  #  endif
  #endif

?


Repository:
  rCXX libc++

https://reviews.llvm.org/D45179





More information about the cfe-commits mailing list