fix(Clang CodeGen): remove warnings

./fmt/fmt/format.h(308,10): warning : unknown pragma ignored [-Wunknown-pragmas]
         ^
1 warning generated.
format.cc In file included from fmt\fmt\format.cc:28:
fmt\fmt/format.h(308,10): warning : unknown pragma ignored [-Wunknown-pragmas]
         ^
fmt\fmt\format.cc(165,17): warning : 'strerror' is deprecated: This
function or variable may be unsafe. Consider using strerror_s instead.
To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for
details. [-Wdeprecated-declarations]
      buffer_ = strerror(error_code_);
                ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\string.h(178,24) :  note: 'strerror' has been explicitly marked deprecated here
_ACRTIMP char* __cdecl strerror(
                       ^
fmt\fmt\format.cc(78,37): warning : unused function 'strerror_s' [-Wunused-function]
static inline fmt::internal::Null<> strerror_s(char *, std::size_t, ...) {
                                    ^
3 warnings generated.

refactor: use attribute to remove -Wunused-function warnings instead of dummy function call
This commit is contained in:
yumetodo 2017-10-14 13:54:23 +09:00 committed by Victor Zverovich
parent 9328a074b1
commit 708d9509ff
2 changed files with 34 additions and 4 deletions

View File

@ -72,9 +72,11 @@
// Dummy implementations of strerror_r and strerror_s called if corresponding // Dummy implementations of strerror_r and strerror_s called if corresponding
// system functions are not available. // system functions are not available.
FMT_MAYBE_UNUSED
static inline fmt::internal::Null<> strerror_r(int, char *, ...) { static inline fmt::internal::Null<> strerror_r(int, char *, ...) {
return fmt::internal::Null<>(); return fmt::internal::Null<>();
} }
FMT_MAYBE_UNUSED
static inline fmt::internal::Null<> strerror_s(char *, std::size_t, ...) { static inline fmt::internal::Null<> strerror_s(char *, std::size_t, ...) {
return fmt::internal::Null<>(); return fmt::internal::Null<>();
} }
@ -159,6 +161,11 @@ int safe_strerror(
ERANGE : result; ERANGE : result;
} }
#ifdef __c2__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
// Fallback to strerror if strerror_r and strerror_s are not available. // Fallback to strerror if strerror_r and strerror_s are not available.
int fallback(internal::Null<>) { int fallback(internal::Null<>) {
errno = 0; errno = 0;
@ -166,13 +173,15 @@ int safe_strerror(
return errno; return errno;
} }
#ifdef __c2__
# pragma clang diagnostic pop
#endif
public: public:
StrError(int err_code, char *&buf, std::size_t buf_size) StrError(int err_code, char *&buf, std::size_t buf_size)
: error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {} : error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {}
int run() { int run() {
// Suppress a warning about unused strerror_r.
strerror_r(0, FMT_NULL, "");
return handle(strerror_r(error_code_, buffer_, buffer_size_)); return handle(strerror_r(error_code_, buffer_, buffer_size_));
} }
}; };

View File

@ -153,6 +153,23 @@ typedef __int64 intmax_t;
# define FMT_HAS_CPP_ATTRIBUTE(x) 0 # define FMT_HAS_CPP_ATTRIBUTE(x) 0
#endif #endif
#if FMT_HAS_CPP_ATTRIBUTE(maybe_unused)
# define FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED
// VC++ 1910 support /std: option and that will set _MSVC_LANG macro
// Clang with Microsoft CodeGen doesn't define _MSVC_LANG macro
#elif defined(_MSVC_LANG) && _MSVC_LANG > 201402
# define FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED
#endif
#ifdef FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED
# define FMT_MAYBE_UNUSED [[maybe_unused]]
// g++/clang++ also support [[gnu::unused]]. However, we don't use it.
#elif defined(__GNUC__)
# define FMT_MAYBE_UNUSED __attribute__((unused))
#else
# define FMT_MAYBE_UNUSED
#endif
// Use the compiler's attribute noreturn // Use the compiler's attribute noreturn
#if defined(__MINGW32__) || defined(__MINGW64__) #if defined(__MINGW32__) || defined(__MINGW64__)
# define FMT_NORETURN __attribute__((noreturn)) # define FMT_NORETURN __attribute__((noreturn))
@ -335,7 +352,10 @@ typedef __int64 intmax_t;
namespace fmt { namespace fmt {
namespace internal { namespace internal {
// avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning
# ifndef __clang__
# pragma intrinsic(_BitScanReverse) # pragma intrinsic(_BitScanReverse)
# endif
inline uint32_t clz(uint32_t x) { inline uint32_t clz(uint32_t x) {
unsigned long r = 0; unsigned long r = 0;
_BitScanReverse(&r, x); _BitScanReverse(&r, x);
@ -349,7 +369,8 @@ inline uint32_t clz(uint32_t x) {
} }
# define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n) # define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n)
# ifdef _WIN64 // avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning
# if defined(_WIN64) && !defined(__clang__)
# pragma intrinsic(_BitScanReverse64) # pragma intrinsic(_BitScanReverse64)
# endif # endif