From d3a8eb1ae2f2fd87c8f1be8c5110caaa725a9d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Wed, 15 Nov 2023 15:31:12 +0100 Subject: [PATCH] config.h: improved and cleaned up some preprocessor checks (#5663) --- gui/translationhandler.cpp | 2 +- lib/config.h | 53 ++++++++++++++++++++++++-------------- lib/sourcelocation.h | 8 +----- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/gui/translationhandler.cpp b/gui/translationhandler.cpp index 2e17148fd..0caa3dcb1 100644 --- a/gui/translationhandler.cpp +++ b/gui/translationhandler.cpp @@ -32,7 +32,7 @@ // Provide own translations for standard buttons. This (garbage) code is needed to enforce them to appear in .ts files even after "lupdate gui.pro" -static UNUSED void unused() +UNUSED static void unused() { Q_UNUSED(QT_TRANSLATE_NOOP("QPlatformTheme", "OK")) Q_UNUSED(QT_TRANSLATE_NOOP("QPlatformTheme", "Cancel")) diff --git a/lib/config.h b/lib/config.h index 6cb34b5e4..5379eada4 100644 --- a/lib/config.h +++ b/lib/config.h @@ -41,8 +41,26 @@ # include #endif +// compatibility macros +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +#ifndef __has_include +#define __has_include(x) 0 +#endif + +#ifndef __has_cpp_attribute +#define __has_cpp_attribute(x) 0 +#endif + +#ifndef __has_feature +#define __has_feature(x) 0 +#endif + // C++11 noexcept -#if (defined(__GNUC__) && (__GNUC__ >= 5)) \ +#if defined(__cpp_noexcept_function_type) || \ + (defined(__GNUC__) && (__GNUC__ >= 5)) \ || defined(__clang__) \ || defined(__CPPCHECK__) # define NOEXCEPT noexcept @@ -51,25 +69,21 @@ #endif // C++11 noreturn -#if defined __has_cpp_attribute -# if __has_cpp_attribute (noreturn) -# define NORETURN [[noreturn]] -# endif -#endif -#if !defined(NORETURN) -# if (defined(__GNUC__) && (__GNUC__ >= 5)) \ +#if __has_cpp_attribute (noreturn) \ + || (defined(__GNUC__) && (__GNUC__ >= 5)) \ || defined(__clang__) \ || defined(__CPPCHECK__) -# define NORETURN [[noreturn]] -# elif defined(__GNUC__) -# define NORETURN __attribute__((noreturn)) -# else -# define NORETURN -# endif +# define NORETURN [[noreturn]] +#elif defined(__GNUC__) +# define NORETURN __attribute__((noreturn)) +#else +# define NORETURN #endif // fallthrough -#if defined(__clang__) +#if __cplusplus >= 201703L && __has_cpp_attribute (fallthrough) +# define FALLTHROUGH [[fallthrough]] +#elif defined(__clang__) # define FALLTHROUGH [[clang::fallthrough]] #elif (defined(__GNUC__) && (__GNUC__ >= 7)) # define FALLTHROUGH __attribute__((fallthrough)) @@ -78,7 +92,9 @@ #endif // unused -#if defined(__GNUC__) \ +#if __cplusplus >= 201703L && __has_cpp_attribute (maybe_unused) +# define UNUSED [[maybe_unused]] +#elif defined(__GNUC__) \ || defined(__clang__) \ || defined(__CPPCHECK__) # define UNUSED __attribute__((unused)) @@ -87,7 +103,8 @@ #endif // warn_unused -#if (defined(__clang__) && (__clang_major__ >= 15)) +#if __has_cpp_attribute (gnu::warn_unused) || \ + (defined(__clang__) && (__clang_major__ >= 15)) # define WARN_UNUSED [[gnu::warn_unused]] #else # define WARN_UNUSED @@ -110,11 +127,9 @@ static const std::string emptyString; #define nonneg #endif -#if defined(__has_feature) #if __has_feature(address_sanitizer) #define ASAN 1 #endif -#endif #ifndef ASAN #ifdef __SANITIZE_ADDRESS__ diff --git a/lib/sourcelocation.h b/lib/sourcelocation.h index f94fa7907..759f30d15 100644 --- a/lib/sourcelocation.h +++ b/lib/sourcelocation.h @@ -19,13 +19,7 @@ #ifndef sourcelocationH #define sourcelocationH -#ifndef __has_builtin -#define __has_builtin(x) 0 // Compatibility with non-clang compilers. -#endif - -#ifndef __has_include -#define __has_include(x) 0 // Compatibility with non-clang compilers. -#endif +#include "config.h" #ifdef __CPPCHECK__ #define CPPCHECK_HAS_SOURCE_LOCATION 0