From 02bfd8a9a54e3ccc1e1e5a31c4782d2fb652d7c8 Mon Sep 17 00:00:00 2001 From: Dair Grant Date: Mon, 2 Mar 2020 14:51:51 +0000 Subject: [PATCH] Add FMT_HAS_CPP14_ATTRIBUTE / FMT_HAS_CPP17_ATTRIBUTE to test for language-specific attributes. FMT_DEPRECATED is now defined as FMT_HAS_CPP14_ATTRIBUTE(deprecated), as this attribute was introduced in C++14. FMT_FALLTHROUGH is now defined as FMT_HAS_CPP17_ATTRIBUTE(fallthrough), as this attribute was introduced in C++17. FMT_MAYBE_UNUSED is defined as FMT_HAS_CPP17_ATTRIBUTE(maybe_unused), as this attribute was introduced in C++17. FMT_MAYBE_UNUSED has been applied to fix a couple of -Wunused-member-function warnings from clang. --- include/fmt/core.h | 17 +++++++++++++++-- include/fmt/format-inl.h | 3 +++ include/fmt/format.h | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 8bcc0d06..25647037 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -36,6 +36,12 @@ # define FMT_HAS_CPP_ATTRIBUTE(x) 0 #endif +#define FMT_HAS_CPP14_ATTRIBUTE(attribute) \ + (__cplusplus >= 201402L && FMT_HAS_CPP_ATTRIBUTE(attribute)) + +#define FMT_HAS_CPP17_ATTRIBUTE(attribute) \ + (__cplusplus >= 201703L && FMT_HAS_CPP_ATTRIBUTE(attribute)) + #ifdef __clang__ # define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) #else @@ -132,9 +138,16 @@ # define FMT_NORETURN #endif +#ifndef FMT_MAYBE_UNUSED +# if FMT_HAS_CPP17_ATTRIBUTE(maybe_unused) +# define FMT_MAYBE_UNUSED [[maybe_unused]] +# else +# define FMT_MAYBE_UNUSED +# endif +#endif + #ifndef FMT_DEPRECATED -# if (FMT_HAS_CPP_ATTRIBUTE(deprecated) && __cplusplus >= 201402L) || \ - FMT_MSC_VER >= 1900 +# if FMT_HAS_CPP14_ATTRIBUTE(deprecated) || FMT_MSC_VER >= 1900 # define FMT_DEPRECATED [[deprecated]] # else # if defined(__GNUC__) || defined(__clang__) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index c42f7280..5d294bc7 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -86,6 +86,7 @@ FMT_FUNC int safe_strerror(int error_code, char*& buffer, } // Handle the result of GNU-specific version of strerror_r. + FMT_MAYBE_UNUSED int handle(char* message) { // If the buffer is full then the message is probably truncated. if (message == buffer_ && strlen(buffer_) == buffer_size_ - 1) @@ -95,11 +96,13 @@ FMT_FUNC int safe_strerror(int error_code, char*& buffer, } // Handle the case when strerror_r is not available. + FMT_MAYBE_UNUSED int handle(internal::null<>) { return fallback(strerror_s(buffer_, buffer_size_, error_code_)); } // Fallback to strerror_s when strerror_r is not available. + FMT_MAYBE_UNUSED int fallback(int result) { // If the buffer is full then the message is probably truncated. return result == 0 && strlen(buffer_) == buffer_size_ - 1 ? ERANGE diff --git a/include/fmt/format.h b/include/fmt/format.h index 537a80ce..07d4f5f8 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -71,7 +71,7 @@ # else # define FMT_FALLTHROUGH # endif -#elif (FMT_HAS_CPP_ATTRIBUTE(fallthrough) && (__cplusplus >= 201703)) || \ +#elif FMT_HAS_CPP17_ATTRIBUTE(fallthrough) || \ (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) # define FMT_FALLTHROUGH [[fallthrough]] #else @@ -3549,7 +3549,7 @@ FMT_END_NAMESPACE /* Use a macro-like name to avoid shadowing warnings. */ \ struct FMT_COMPILE_STRING : fmt::compile_string { \ using char_type = fmt::remove_cvref_t; \ - __VA_ARGS__ FMT_CONSTEXPR \ + FMT_MAYBE_UNUSED __VA_ARGS__ FMT_CONSTEXPR \ operator fmt::basic_string_view() const { \ /* FMT_STRING only accepts string literals. */ \ return fmt::internal::literal_to_view(s); \