From d254d8c19714af69a9897bdd67c4e9aa3267c3a3 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 18 Apr 2017 16:43:41 -0700 Subject: Use the C++ [[nodiscard]] attribute Change-Id: I7814054a102a407d876ffffd14b6a285c70b21de Reviewed-by: Olivier Goffart (Woboq GmbH) Reviewed-by: Lars Knoll --- src/corelib/global/qcompilerdetection.h | 67 ++++++++++++++++++--------------- src/corelib/tools/qbytearray.h | 2 +- src/corelib/tools/qstring.h | 2 +- 3 files changed, 38 insertions(+), 33 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/global/qcompilerdetection.h b/src/corelib/global/qcompilerdetection.h index c221115e8f..9ffd164b61 100644 --- a/src/corelib/global/qcompilerdetection.h +++ b/src/corelib/global/qcompilerdetection.h @@ -1060,6 +1060,37 @@ # endif #endif +/* + * SG10's SD-6 feature detection and some useful extensions from Clang and GCC + * https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations + * http://clang.llvm.org/docs/LanguageExtensions.html#feature-checking-macros + */ +#ifdef __has_builtin +# define QT_HAS_BUILTIN(x) __has_builtin(x) +#else +# define QT_HAS_BUILTIN(x) 0 +#endif +#ifdef __has_attribute +# define QT_HAS_ATTRIBUTE(x) __has_attribute(x) +#else +# define QT_HAS_ATTRIBUTE(x) 0 +#endif +#ifdef __has_cpp_attribute +# define QT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) +#else +# define QT_HAS_CPP_ATTRIBUTE(x) 0 +#endif +#ifdef __has_include +# define QT_HAS_INCLUDE(x) __has_include(x) +#else +# define QT_HAS_INCLUDE(x) 0 +#endif +#ifdef __has_include_next +# define QT_HAS_INCLUDE_NEXT(x) __has_include_next(x) +#else +# define QT_HAS_INCLUDE_NEXT(x) 0 +#endif + /* * C++11 keywords and expressions */ @@ -1141,6 +1172,11 @@ # define Q_DECL_ALIGN(n) alignas(n) #endif +#if QT_HAS_CPP_ATTRIBUTE(nodiscard) // P0188R1 +# undef Q_REQUIRED_RESULT +# define Q_REQUIRED_RESULT [[nodiscard]] +#endif + /* * Fallback macros to certain compiler features */ @@ -1215,37 +1251,6 @@ #ifndef QT_MAKE_CHECKED_ARRAY_ITERATOR # define QT_MAKE_CHECKED_ARRAY_ITERATOR(x, N) (x) #endif - -/* - * SG10's SD-6 feature detection and some useful extensions from Clang and GCC - * https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations - * http://clang.llvm.org/docs/LanguageExtensions.html#feature-checking-macros - */ -#ifdef __has_builtin -# define QT_HAS_BUILTIN(x) __has_builtin(x) -#else -# define QT_HAS_BUILTIN(x) 0 -#endif -#ifdef __has_attribute -# define QT_HAS_ATTRIBUTE(x) __has_attribute(x) -#else -# define QT_HAS_ATTRIBUTE(x) 0 -#endif -#ifdef __has_cpp_attribute -# define QT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) -#else -# define QT_HAS_CPP_ATTRIBUTE(x) 0 -#endif -#ifdef __has_include -# define QT_HAS_INCLUDE(x) __has_include(x) -#else -# define QT_HAS_INCLUDE(x) 0 -#endif -#ifdef __has_include_next -# define QT_HAS_INCLUDE_NEXT(x) __has_include_next(x) -#else -# define QT_HAS_INCLUDE_NEXT(x) 0 -#endif #ifdef __has_feature # define QT_HAS_FEATURE(x) __has_feature(x) #else diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 03bb9b5747..432ef922f6 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -243,7 +243,7 @@ public: void chop(int n); #if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC) -# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) +# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_CPP_ATTRIBUTE(nodiscard) // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941 # pragma push_macro("Q_REQUIRED_RESULT") # undef Q_REQUIRED_RESULT diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 109b68c544..88ba4a3fb5 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -383,7 +383,7 @@ public: Q_REQUIRED_RESULT QString rightJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const; #if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC) -# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) +# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_CPP_ATTRIBUTE(nodiscard) // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941 # pragma push_macro("Q_REQUIRED_RESULT") # undef Q_REQUIRED_RESULT -- cgit v1.2.3