From c1021867fb4bb8ec827ecf50d50cbd6a945d866b Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 7 Nov 2019 12:38:37 -0800 Subject: [PATCH 1/9] [clang-tidy] Turn deleted function to public Found with modernize-use-equals-delete Signed-off-by: Rosen Penev --- include/fmt/core.h | 15 ++++++--------- include/fmt/posix.h | 15 ++++++--------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index c481fb434077..1417e941b1f4 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -552,9 +552,6 @@ using has_formatter = /** A contiguous memory buffer with an optional growing ability. */ template class buffer { private: - buffer(const buffer&) = delete; - void operator=(const buffer&) = delete; - T* ptr_; std::size_t size_; std::size_t capacity_; @@ -581,6 +578,8 @@ template class buffer { using value_type = T; using const_reference = const T&; + buffer(const buffer&) = delete; + void operator=(const buffer&) = delete; virtual ~buffer() {} T* begin() FMT_NOEXCEPT { return ptr_; } @@ -1035,9 +1034,6 @@ namespace internal { // A map from argument names to their values for named arguments. template class arg_map { private: - arg_map(const arg_map&) = delete; - void operator=(const arg_map&) = delete; - using char_type = typename Context::char_type; struct entry { @@ -1055,6 +1051,8 @@ template class arg_map { } public: + arg_map(const arg_map&) = delete; + void operator=(const arg_map&) = delete; arg_map() : map_(nullptr), size_(0) {} void init(const basic_format_args& args); ~arg_map() { delete[] map_; } @@ -1121,14 +1119,13 @@ template class basic_format_context { internal::arg_map map_; internal::locale_ref loc_; - basic_format_context(const basic_format_context&) = delete; - void operator=(const basic_format_context&) = delete; - public: using iterator = OutputIt; using format_arg = basic_format_arg; template using formatter_type = formatter; + basic_format_context(const basic_format_context&) = delete; + void operator=(const basic_format_context&) = delete; /** Constructs a ``basic_format_context`` object. References to the arguments are stored in the object so make sure they have appropriate lifetimes. diff --git a/include/fmt/posix.h b/include/fmt/posix.h index 6b2d7f8e4c4c..2512b7d183b3 100644 --- a/include/fmt/posix.h +++ b/include/fmt/posix.h @@ -132,16 +132,15 @@ class buffered_file { explicit buffered_file(FILE* f) : file_(f) {} public: + buffered_file(const buffered_file&) = delete; + void operator=(const buffered_file&) = delete; + // Constructs a buffered_file object which doesn't represent any file. buffered_file() FMT_NOEXCEPT : file_(nullptr) {} // Destroys the object closing the file it represents if any. FMT_API ~buffered_file() FMT_NOEXCEPT; - private: - buffered_file(const buffered_file&) = delete; - void operator=(const buffered_file&) = delete; - public: buffered_file(buffered_file&& other) FMT_NOEXCEPT : file_(other.file_) { other.file_ = nullptr; @@ -204,11 +203,10 @@ class file { // Opens a file and constructs a file object representing this file. FMT_API file(cstring_view path, int oflag); - private: + public: file(const file&) = delete; void operator=(const file&) = delete; - public: file(file&& other) FMT_NOEXCEPT : fd_(other.fd_) { other.fd_ = -1; } file& operator=(file&& other) { @@ -283,11 +281,10 @@ class Locale { locale_t locale_; - Locale(const Locale&) = delete; - void operator=(const Locale&) = delete; - public: using type = locale_t; + Locale(const Locale&) = delete; + void operator=(const Locale&) = delete; Locale() : locale_(newlocale(LC_NUMERIC_MASK, "C", nullptr)) { if (!locale_) FMT_THROW(system_error(errno, "cannot create locale")); From 6a09864aefe587654ff5c59248eb1ca3524004d6 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 7 Nov 2019 12:40:46 -0800 Subject: [PATCH 2/9] [clang-tidy] Changes suffixes to uppercase Found with hicpp-uppercase-literal-suffix Signed-off-by: Rosen Penev --- include/fmt/core.h | 2 +- include/fmt/format-inl.h | 16 ++++++++-------- include/fmt/format.h | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 1417e941b1f4..34aac1c9aac6 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -919,7 +919,7 @@ using mapped_type_constant = enum { packed_arg_bits = 5 }; // Maximum number of arguments with packed types. enum { max_packed_args = 63 / packed_arg_bits }; -enum : unsigned long long { is_unpacked_bit = 1ull << 63 }; +enum : unsigned long long { is_unpacked_bit = 1ULL << 63 }; template class arg_map; } // namespace internal diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 766dc0072a91..396617da7c21 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -286,8 +286,8 @@ const char basic_data::hex_digits[] = "0123456789abcdef"; template const uint64_t basic_data::powers_of_10_64[] = { - 1, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ull), - 10000000000000000000ull}; + 1, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ULL), + 10000000000000000000ULL}; template const uint32_t basic_data::zero_or_powers_of_10_32[] = {0, @@ -295,8 +295,8 @@ const uint32_t basic_data::zero_or_powers_of_10_32[] = {0, template const uint64_t basic_data::zero_or_powers_of_10_64[] = { - 0, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ull), - 10000000000000000000ull}; + 0, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ULL), + 10000000000000000000ULL}; // Normalized 64-bit significands of pow(10, k), for k = -348, -340, ..., 340. // These are generated by support/compute-powers.py. @@ -373,7 +373,7 @@ class fp { static FMT_CONSTEXPR_DECL const int double_significand_size = std::numeric_limits::digits - 1; static FMT_CONSTEXPR_DECL const uint64_t implicit_bit = - 1ull << double_significand_size; + 1ULL << double_significand_size; public: significand_type f; @@ -412,7 +412,7 @@ class fp { const int exponent_size = bits::value - double_significand_size - 1; // -1 for sign const uint64_t significand_mask = implicit_bit - 1; - const uint64_t exponent_mask = (~0ull >> 1) & ~significand_mask; + const uint64_t exponent_mask = (~0ULL >> 1) & ~significand_mask; const int exponent_bias = (1 << exponent_size) - limits::max_exponent - 1; auto u = bit_cast(d); f = u & significand_mask; @@ -796,7 +796,7 @@ enum result { template FMT_ALWAYS_INLINE digits::result grisu_gen_digits(fp value, uint64_t error, int& exp, Handler& handler) { - const fp one(1ull << -value.e, value.e); + const fp one(1ULL << -value.e, value.e); // The integral part of scaled value (p1 in Grisu) = value / one. It cannot be // zero because it contains a product of two 64-bit numbers with MSB set (due // to normalization) - 1, shifted right by at most 60 bits. @@ -1022,7 +1022,7 @@ void fallback_format(Double d, buffer& buf, int& exp10) { denominator <<= shift - value.e; lower.assign(1); if (shift != 1) { - upper_store.assign(1ull << 1); + upper_store.assign(1ULL << 1); upper = &upper_store; } } diff --git a/include/fmt/format.h b/include/fmt/format.h index a3d1ee31d78d..5f6519e4a1b2 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -740,7 +740,7 @@ inline int count_digits(uint128_t n) { if (n < 100) return count + 1; if (n < 1000) return count + 2; if (n < 10000) return count + 3; - n /= 10000u; + n /= 10000U; count += 4; } } From 4210c143fafb08bc8c131480acc13f44e75b1fb5 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 7 Nov 2019 12:42:35 -0800 Subject: [PATCH 3/9] [clang-tidy] Use braced init list Found with modernize-return-braced-init-list Signed-off-by: Rosen Penev --- include/fmt/format-inl.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 396617da7c21..44e070cbb833 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -56,10 +56,10 @@ // Dummy implementations of strerror_r and strerror_s called if corresponding // system functions are not available. inline fmt::internal::null<> strerror_r(int, char*, ...) { - return fmt::internal::null<>(); + return {}; } inline fmt::internal::null<> strerror_s(char*, std::size_t, ...) { - return fmt::internal::null<>(); + return {}; } FMT_BEGIN_NAMESPACE @@ -463,7 +463,7 @@ inline bool operator==(fp x, fp y) { return x.f == y.f && x.e == y.e; } // Returns an fp number representing x - y. Result may not be normalized. inline fp operator-(fp x, fp y) { FMT_ASSERT(x.f >= y.f && x.e == y.e, "invalid operands"); - return fp(x.f - y.f, x.e); + return {x.f - y.f, x.e}; } // Computes an fp number r with r.f = x.f * y.f / pow(2, 64) rounded to nearest @@ -475,7 +475,7 @@ FMT_FUNC fp operator*(fp x, fp y) { auto product = static_cast<__uint128_t>(x.f) * y.f; auto f = static_cast(product >> 64); if ((static_cast(product) & (1ULL << 63)) != 0) ++f; - return fp(f, exp); + return {f, exp}; #else // Multiply 32-bit parts of significands. uint64_t mask = (1ULL << 32) - 1; @@ -505,7 +505,7 @@ FMT_FUNC fp get_cached_power(int min_exponent, int& pow10_exponent) { const int dec_exp_step = 8; index = (index - first_dec_exp - 1) / dec_exp_step + 1; pow10_exponent = first_dec_exp + index * dec_exp_step; - return fp(data::pow10_significands[index], data::pow10_exponents[index]); + return {data::pow10_significands[index], data::pow10_exponents[index]}; } // A simple accumulator to hold the sums of terms in bigint::square if uint128_t From 018fa12dca02b308324b89d6b5074e369caf98fc Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 7 Nov 2019 12:44:10 -0800 Subject: [PATCH 4/9] [clang-tidy] Add parentheses to macro arguments Found with bugprone-macro-parentheses Signed-off-by: Rosen Penev --- include/fmt/core.h | 2 +- include/fmt/format-inl.h | 6 +++--- include/fmt/posix.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 34aac1c9aac6..231c0e989b46 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -188,7 +188,7 @@ #endif #ifndef FMT_ASSERT -# define FMT_ASSERT(condition, message) assert((condition) && message) +# define FMT_ASSERT(condition, message) assert((condition) && (message)) #endif // libc++ supports string_view in pre-c++17. diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 44e070cbb833..0904fa400a7d 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -280,9 +280,9 @@ template const char basic_data::hex_digits[] = "0123456789abcdef"; #define FMT_POWERS_OF_10(factor) \ - factor * 10, factor * 100, factor * 1000, factor * 10000, factor * 100000, \ - factor * 1000000, factor * 10000000, factor * 100000000, \ - factor * 1000000000 + factor * 10, (factor) * 100, (factor) * 1000, (factor) * 10000, (factor) * 100000, \ + (factor) * 1000000, (factor) * 10000000, (factor) * 100000000, \ + (factor) * 1000000000 template const uint64_t basic_data::powers_of_10_64[] = { diff --git a/include/fmt/posix.h b/include/fmt/posix.h index 2512b7d183b3..5c306d302aae 100644 --- a/include/fmt/posix.h +++ b/include/fmt/posix.h @@ -54,8 +54,8 @@ #ifndef _WIN32 # define FMT_RETRY_VAL(result, expression, error_result) \ do { \ - result = (expression); \ - } while (result == error_result && errno == EINTR) + (result) = (expression); \ + } while ((result) == (error_result) && errno == EINTR) #else # define FMT_RETRY_VAL(result, expression, error_result) result = (expression) #endif From c8db53b3b0ff8b1c2aec1d6a89c6c4971ec2c912 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 7 Nov 2019 12:56:09 -0800 Subject: [PATCH 5/9] [clang-tidy] Add noexcept where move is used Found with performance-noexcept-move-constructor Signed-off-by: Rosen Penev --- include/fmt/format.h | 4 ++-- include/fmt/posix.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 5f6519e4a1b2..329e343f6bf7 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -607,14 +607,14 @@ class basic_memory_buffer : private Allocator, public internal::buffer { of the other object to it. \endrst */ - basic_memory_buffer(basic_memory_buffer&& other) { move(other); } + basic_memory_buffer(basic_memory_buffer&& other) FMT_NOEXCEPT { move(other); } /** \rst Moves the content of the other ``basic_memory_buffer`` object to this one. \endrst */ - basic_memory_buffer& operator=(basic_memory_buffer&& other) { + basic_memory_buffer& operator=(basic_memory_buffer&& other) FMT_NOEXCEPT { assert(this != &other); deallocate(); move(other); diff --git a/include/fmt/posix.h b/include/fmt/posix.h index 5c306d302aae..3ed3b0551b4b 100644 --- a/include/fmt/posix.h +++ b/include/fmt/posix.h @@ -146,7 +146,7 @@ class buffered_file { other.file_ = nullptr; } - buffered_file& operator=(buffered_file&& other) { + buffered_file& operator=(buffered_file&& other) FMT_NOEXCEPT { close(); file_ = other.file_; other.file_ = nullptr; @@ -209,7 +209,7 @@ class file { file(file&& other) FMT_NOEXCEPT : fd_(other.fd_) { other.fd_ = -1; } - file& operator=(file&& other) { + file& operator=(file&& other) FMT_NOEXCEPT { close(); fd_ = other.fd_; other.fd_ = -1; From ff78ed8f411a189a98ee129a0819958c5185e432 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 7 Nov 2019 12:57:30 -0800 Subject: [PATCH 6/9] [clang-tidy] Use auto Found with hicpp-use-auto Signed-off-by: Rosen Penev --- include/fmt/format-inl.h | 2 +- include/fmt/format.h | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 0904fa400a7d..22b7c82db544 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -800,7 +800,7 @@ FMT_ALWAYS_INLINE digits::result grisu_gen_digits(fp value, uint64_t error, // The integral part of scaled value (p1 in Grisu) = value / one. It cannot be // zero because it contains a product of two 64-bit numbers with MSB set (due // to normalization) - 1, shifted right by at most 60 bits. - uint32_t integral = static_cast(value.f >> -one.e); + auto integral = static_cast(value.f >> -one.e); FMT_ASSERT(integral != 0, ""); FMT_ASSERT(integral == value.f >> -one.e, ""); // The fractional part of scaled value (p2 in Grisu) c = value % one. diff --git a/include/fmt/format.h b/include/fmt/format.h index 329e343f6bf7..0c14ebda013b 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -862,7 +862,7 @@ inline Char* format_decimal(Char* buffer, UInt value, int num_digits, // Integer division is slow so do it for a group of two digits instead // of for every digit. The idea comes from the talk by Alexandrescu // "Three Optimization Tips for C++". See speed-test for a comparison. - unsigned index = static_cast((value % 100) * 2); + auto index = static_cast((value % 100) * 2); value /= 100; *--buffer = static_cast(data::digits[index + 1]); add_thousands_sep(buffer); @@ -873,7 +873,7 @@ inline Char* format_decimal(Char* buffer, UInt value, int num_digits, *--buffer = static_cast('0' + value); return end; } - unsigned index = static_cast(value * 2); + auto index = static_cast(value * 2); *--buffer = static_cast(data::digits[index + 1]); add_thousands_sep(buffer); *--buffer = static_cast(data::digits[index]); @@ -1568,7 +1568,7 @@ template class basic_writer { void on_num() { std::string groups = internal::grouping(writer.locale_); if (groups.empty()) return on_dec(); - char_type sep = internal::thousands_sep(writer.locale_); + auto sep = internal::thousands_sep(writer.locale_); if (!sep) return on_dec(); int num_digits = internal::count_digits(abs_value); int size = num_digits; @@ -2965,7 +2965,7 @@ class format_int { // Integer division is slow so do it for a group of two digits instead // of for every digit. The idea comes from the talk by Alexandrescu // "Three Optimization Tips for C++". See speed-test for a comparison. - unsigned index = static_cast((value % 100) * 2); + auto index = static_cast((value % 100) * 2); value /= 100; *--ptr = internal::data::digits[index + 1]; *--ptr = internal::data::digits[index]; @@ -2974,14 +2974,14 @@ class format_int { *--ptr = static_cast('0' + value); return ptr; } - unsigned index = static_cast(value * 2); + auto index = static_cast(value * 2); *--ptr = internal::data::digits[index + 1]; *--ptr = internal::data::digits[index]; return ptr; } void format_signed(long long value) { - unsigned long long abs_value = static_cast(value); + auto abs_value = static_cast(value); bool negative = value < 0; if (negative) abs_value = 0 - abs_value; str_ = format_decimal(abs_value); From 20a905c88e6c396b411fcf20a329b8173d79d39b Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 7 Nov 2019 12:58:32 -0800 Subject: [PATCH 7/9] [clang-tidy] Replace {} with = default Found with hicpp-use-equals-default Signed-off-by: Rosen Penev --- include/fmt/core.h | 6 +++--- include/fmt/format-inl.h | 4 ++-- include/fmt/format.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 231c0e989b46..02a389fa0763 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -437,8 +437,8 @@ template struct char_t_impl::value>> { }; struct error_handler { - FMT_CONSTEXPR error_handler() {} - FMT_CONSTEXPR error_handler(const error_handler&) {} + FMT_CONSTEXPR error_handler() = default; + FMT_CONSTEXPR error_handler(const error_handler&) = default; // This function is intentionally not constexpr to give a compile-time error. FMT_NORETURN FMT_API void on_error(const char* message); @@ -580,7 +580,7 @@ template class buffer { buffer(const buffer&) = delete; void operator=(const buffer&) = delete; - virtual ~buffer() {} + virtual ~buffer() = default; T* begin() FMT_NOEXCEPT { return ptr_; } T* end() FMT_NOEXCEPT { return ptr_ + size_; } diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 22b7c82db544..a231ab1dbee0 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -232,8 +232,8 @@ FMT_FUNC Char internal::decimal_point_impl(locale_ref) { } #endif -FMT_API FMT_FUNC format_error::~format_error() FMT_NOEXCEPT {} -FMT_API FMT_FUNC system_error::~system_error() FMT_NOEXCEPT {} +FMT_API FMT_FUNC format_error::~format_error() FMT_NOEXCEPT = default; +FMT_API FMT_FUNC system_error::~system_error() FMT_NOEXCEPT = default; FMT_FUNC void system_error::init(int err_code, string_view format_str, format_args args) { diff --git a/include/fmt/format.h b/include/fmt/format.h index 0c14ebda013b..332b827189f3 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3030,7 +3030,7 @@ template struct formatter::value != internal::custom_type>> { - FMT_CONSTEXPR formatter() {} + FMT_CONSTEXPR formatter() = default; // Parses format specifiers stopping either at the end of the range or at the // terminating '}'. From 983135b96dbf4712d3fa7105b8d79053a1fe2854 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 7 Nov 2019 13:02:05 -0800 Subject: [PATCH 8/9] [clang-tidy] Add missing override Found with hicpp-use-override Signed-off-by: Rosen Penev --- include/fmt/format.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 332b827189f3..9c104df1d0ca 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -578,7 +578,7 @@ class basic_memory_buffer : private Allocator, public internal::buffer { : Allocator(alloc) { this->set(store_, SIZE); } - ~basic_memory_buffer() { deallocate(); } + ~basic_memory_buffer() FMT_OVERRIDE { deallocate(); } private: // Move data from other to this buffer. @@ -658,7 +658,7 @@ class FMT_API format_error : public std::runtime_error { format_error& operator=(const format_error&) = default; format_error(format_error&&) = default; format_error& operator=(format_error&&) = default; - ~format_error() FMT_NOEXCEPT; + ~format_error() FMT_NOEXCEPT FMT_OVERRIDE; }; namespace internal { @@ -2752,7 +2752,7 @@ class FMT_API system_error : public std::runtime_error { system_error& operator=(const system_error&) = default; system_error(system_error&&) = default; system_error& operator=(system_error&&) = default; - ~system_error() FMT_NOEXCEPT; + ~system_error() FMT_NOEXCEPT FMT_OVERRIDE; int error_code() const { return error_code_; } }; From 57a5ec048399623f8fb30b87244df7e64fc54a86 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 7 Nov 2019 13:10:32 -0800 Subject: [PATCH 9/9] [clang-tidy] Replace deprecated C headers Found with modernize-deprecated-headers Signed-off-by: Rosen Penev --- include/fmt/format-inl.h | 2 +- include/fmt/posix.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index a231ab1dbee0..d9e37d20e3d7 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -10,7 +10,7 @@ #include "format.h" -#include +#include #include #include diff --git a/include/fmt/posix.h b/include/fmt/posix.h index 3ed3b0551b4b..0651f047d385 100644 --- a/include/fmt/posix.h +++ b/include/fmt/posix.h @@ -13,11 +13,11 @@ # undef __STRICT_ANSI__ #endif -#include +#include #include // for O_RDONLY -#include // for locale_t -#include -#include // for strtod_l +#include // for locale_t +#include +#include // for strtod_l #include