From 50b2105df529c5e395536c1fa6266eca56b03818 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 19 Apr 2021 14:52:22 +1000 Subject: [PATCH] build: clang-tidy fixes --- alloc/std.hpp | 8 +-- array/darray.hpp | 6 +- bezier3.cpp | 2 +- buffer/paged.hpp | 4 +- buffer/simple.hpp | 4 +- cmdopt.hpp | 12 ++-- colour.hpp | 6 +- container.hpp | 2 +- coord/traits.hpp | 6 +- cpuid/x86.cpp | 2 +- expected.hpp | 12 +++- fixup/experimental/filesystem | 1 - fixup/experimental/filesystem.cpp | 113 ------------------------------ fixup/experimental/filesystem.hpp | 66 ----------------- geom/ops.hpp | 4 +- hash/buzhash.hpp | 2 +- library_win32.cpp | 4 +- library_win32.hpp | 4 +- parallel/stack.hpp | 4 +- parse/time.cpp | 4 +- pointer.hpp | 2 +- pool.hpp | 8 ++- posix/util.cpp | 2 +- rand/distribution/normal.hpp | 16 ++--- range.cpp | 2 + registrar.hpp | 6 +- scoped.hpp | 4 +- strongdef.hpp | 4 +- test/parallel/stack.cpp | 10 +-- zlib.cpp | 2 +- 30 files changed, 79 insertions(+), 243 deletions(-) delete mode 100644 fixup/experimental/filesystem delete mode 100644 fixup/experimental/filesystem.cpp delete mode 100644 fixup/experimental/filesystem.hpp diff --git a/alloc/std.hpp b/alloc/std.hpp index 6540cea2..30cfca97 100644 --- a/alloc/std.hpp +++ b/alloc/std.hpp @@ -14,16 +14,16 @@ namespace cruft::alloc { template struct std { using value_type = ValueT; - using size_type = std::size_t; - using difference_type = std::ptrdiff_t; + using size_type = ::std::size_t; + using difference_type = ::std::ptrdiff_t; - [[nodiscard]] ValueT* allocate (std::size_t n) + [[nodiscard]] ValueT* allocate (::std::size_t n) { return m_allocator.allocate (n * sizeof (ValueT), alignof (ValueT)); } void - deallocate (ValueT *ptr, std::size_t n) + deallocate (ValueT *ptr, ::std::size_t n) { m_allocator.deallocate (ptr, n * sizeof (ValueT), alignof (ValueT)); } diff --git a/array/darray.hpp b/array/darray.hpp index 2a658093..ecd4f91c 100644 --- a/array/darray.hpp +++ b/array/darray.hpp @@ -34,10 +34,10 @@ namespace cruft { darray (): m_size (0) { ; } darray (darray const&) = default; - darray (darray &&) = default; + darray (darray &&) noexcept (std::is_trivial_v) = default; - darray& operator= (darray const&) = default; - darray& operator= (darray &&) = default; + darray& operator= (darray const&) noexcept (std::is_nothrow_copy_assignable_v) = default; + darray& operator= (darray &&) noexcept (std::is_nothrow_move_assignable_v)= default; ~darray () { diff --git a/bezier3.cpp b/bezier3.cpp index b2aa7b73..4aca3f5b 100644 --- a/bezier3.cpp +++ b/bezier3.cpp @@ -92,7 +92,7 @@ namespace cruft { std::array lookup; for (int i = 0; i < SUBDIV; ++i) - lookup[i] = eval (i / (SUBDIV - 1.f)); + lookup[i] = eval (float (i) / (SUBDIV - 1.f)); size_t best = 0; for (size_t i = 1; i < lookup.size (); ++i) { diff --git a/buffer/paged.hpp b/buffer/paged.hpp index 8eacd2cc..e0e65576 100644 --- a/buffer/paged.hpp +++ b/buffer/paged.hpp @@ -27,9 +27,9 @@ namespace cruft::buffer { ~paged (); paged (const paged&) = delete; - paged (paged &&); + paged (paged &&) noexcept; paged& operator= (const paged&) = delete; - paged& operator= (paged &&); + paged& operator= (paged &&) noexcept; value_type* begin (void)&; value_type* end (void)&; diff --git a/buffer/simple.hpp b/buffer/simple.hpp index e6d8ccfd..53891788 100644 --- a/buffer/simple.hpp +++ b/buffer/simple.hpp @@ -31,9 +31,9 @@ namespace cruft::buffer { explicit simple (size_t _size); simple (const simple&) = delete; - simple (simple &&) = default; + simple (simple &&) noexcept = default; simple& operator= (const simple&) = delete; - simple& operator= (simple &&); + simple& operator= (simple &&) noexcept; value_type* begin (void)&; value_type* end (void)&; diff --git a/cmdopt.hpp b/cmdopt.hpp index 34886a74..00a2aea1 100644 --- a/cmdopt.hpp +++ b/cmdopt.hpp @@ -313,29 +313,29 @@ namespace cruft::cmdopt { public: template T& add (char shortname, - std::string longname, - std::string description, + std::string const &longname, + std::string const &description, Args&&... args) { auto handler = std::make_unique (std::forward (args)...); T& ref = *handler; m_short.insert({ shortname, ref }); - m_long.insert({ std::move (longname), ref }); + m_long.insert({ longname, ref }); - m_options.push_back ({ std::move (description), std::move (handler) }); + m_options.push_back ({ description, std::move (handler) }); return ref; } template T& - append (std::string description, Args&&...args) + append (std::string const &description, Args&&...args) { auto handler = std::make_unique (std::forward (args)...); auto &ref = *handler; m_positional.push_back (ref); - m_options.push_back ({ std::move (description), std::move (handler) }); + m_options.push_back ({ description, std::move (handler) }); return ref; } diff --git a/colour.hpp b/colour.hpp index da0b3bce..e1d2120e 100644 --- a/colour.hpp +++ b/colour.hpp @@ -89,12 +89,12 @@ namespace cruft { template struct redim_type< srgba - > { template using type = srgba<_S,T>; }; + > { template using type = srgba; }; template struct revalue_type> { - template - using type = srgba; + template + using type = srgba; }; diff --git a/container.hpp b/container.hpp index 0d203bdd..07b7c091 100644 --- a/container.hpp +++ b/container.hpp @@ -24,7 +24,7 @@ namespace cruft { auto const pos = std::find ( std::cbegin (container), std::cend (container), - std::move (value) + std::forward (value) ); return pos != std::cend (container); diff --git a/coord/traits.hpp b/coord/traits.hpp index a791bf14..fb7936e0 100644 --- a/coord/traits.hpp +++ b/coord/traits.hpp @@ -148,13 +148,13 @@ namespace cruft { //--------------------------------------------------------------------- template struct redim_type> - { template using type = point<_S,T>; }; + { template using type = point; }; template struct redim_type> - { template using type = vector<_S,T>; }; + { template using type = vector; }; template struct redim_type> - { template using type = extent<_S,T>; }; + { template using type = extent; }; //--------------------------------------------------------------------- diff --git a/cpuid/x86.cpp b/cpuid/x86.cpp index ac97a7d3..92663cd2 100644 --- a/cpuid/x86.cpp +++ b/cpuid/x86.cpp @@ -140,7 +140,7 @@ x86::x86 () if (auto const apic_id_size = from_bits (size_identifiers.c, 15, 12); apic_id_size) { cores.physical = 1 << (apic_id_size - 1); } else { - cores.physical = (size_identifiers.c & 0xff) + 1; + cores.physical = cruft::cast::lossless (size_identifiers.c & 0xff) + 1; } } else { cores.physical = 0; diff --git a/expected.hpp b/expected.hpp index e93d6735..113c4a45 100644 --- a/expected.hpp +++ b/expected.hpp @@ -26,8 +26,14 @@ namespace cruft { template class unexpected { public: - unexpected (ErrorT && _value): m_value (std::move (_value)) { ; } - unexpected (ErrorT const &_value): m_value (_value) { ; } + unexpected (ErrorT && _value) + noexcept (std::is_nothrow_constructible_v) + : m_value (std::move (_value)) + { ; } + + unexpected (ErrorT const &_value) + : m_value (_value) + { ; } ErrorT& value (void)& { return m_value; } ErrorT&& value (void)&& { return std::move (m_value); } @@ -58,7 +64,7 @@ namespace cruft { } } - expected& operator=(expected &&); + expected& operator=(expected &&) noexcept (std::is_trivially_move_assignable_v); expected (expected const&); expected& operator=(expected const&); diff --git a/fixup/experimental/filesystem b/fixup/experimental/filesystem deleted file mode 100644 index b342cdce..00000000 --- a/fixup/experimental/filesystem +++ /dev/null @@ -1 +0,0 @@ -#include "filesystem.hpp" diff --git a/fixup/experimental/filesystem.cpp b/fixup/experimental/filesystem.cpp deleted file mode 100644 index e7dbae93..00000000 --- a/fixup/experimental/filesystem.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright 2016 Danny Robson - */ - -#include "filesystem.hpp" - -#include "../../except.hpp" - -#include -#include -#include -#include - -namespace ns = std::filesystem; - - -/////////////////////////////////////////////////////////////////////////////// -ns::path::path () -{ ; } - - -//----------------------------------------------------------------------------- -ns::path::path (const path &p): - m_path (p.m_path) -{ ; } - - -/////////////////////////////////////////////////////////////////////////////// -std::string -ns::path::string (void) const -{ - return m_path; -} - - -/////////////////////////////////////////////////////////////////////////////// -const ns::path::string_type& -ns::path::native (void) const -{ - return m_path; -} - - -//----------------------------------------------------------------------------- -const ns::path::value_type* -ns::path::c_str (void) const -{ - return m_path.c_str (); -} - - -/////////////////////////////////////////////////////////////////////////////// -ns::path -ns::path::filename (void) const -{ - auto slash = m_path.find_last_of (preferred_separator); - if (slash == decltype(m_path)::npos) - return m_path; - return ns::path (m_path.cbegin () + slash, m_path.cend ()); -} - -//----------------------------------------------------------------------------- -ns::path -ns::path::stem (void) const -{ - auto name = filename (); - - auto first = name.m_path.cbegin (); - auto last = std::find_if (first, name.m_path.cend (), [] (auto c) { return c == '.'; }); - return path (first, last); -} - - -/////////////////////////////////////////////////////////////////////////////// -ns::path -ns::operator/ (const ns::path &a, const ns::path &b) -{ - return ns::path (a) /= b; -} - - -//----------------------------------------------------------------------------- -ns::path& -ns::path::operator/= (const path &rhs) -{ - m_path += preferred_separator + rhs.m_path; - return *this; -} - - -/////////////////////////////////////////////////////////////////////////////// -bool -ns::operator== (const path &a, const path &b) -{ - return a == b; -} - - -/////////////////////////////////////////////////////////////////////////////// -bool -ns::is_directory (const path &p) -{ - struct stat buf; - - if (stat (p.c_str (), &buf)) - ::cruft::errno_error::throw_code (); - - return S_ISDIR (buf.st_mode); -} diff --git a/fixup/experimental/filesystem.hpp b/fixup/experimental/filesystem.hpp deleted file mode 100644 index d3d4d84d..00000000 --- a/fixup/experimental/filesystem.hpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright 2016 Danny Robson - */ - -#ifndef CRUFT_UTIL_FIXUP_EXPERIMENTAL_FILESYSTEM_HPP -#define CRUFT_UTIL_FIXUP_EXPERIMENTAL_FILESYSTEM_HPP - -#include - -/////////////////////////////////////////////////////////////////////////////// -namespace std::filesystem { - class path { - public: - using value_type = char; - using string_type = std::basic_string; - - static constexpr value_type preferred_separator = '/'; - - path (); - explicit path (const path&); - - template - path (const Source &s): - m_path (s) - { ; } - - template - path (InputT first, InputT last): - m_path (first, last) - { ; } - - std::string string (void) const; - - const string_type& native (void) const; - const value_type* c_str (void) const; - - path filename (void) const; - path stem (void) const; - - path& operator/= (const path&); - - private: - string_type m_path; - }; - - path operator/ (const path&, const path&); - - bool operator== (const path&, const path&); - - //bool is_directory (file_status); - bool is_directory (const path&); - //bool is_directory (const path&, error_code&); - - template - std::basic_ostream& - operator<< (std::basic_ostream &os, const path &p) - { return os << p.native (); } -} - - -#endif - diff --git a/geom/ops.hpp b/geom/ops.hpp index aeadd0bd..ee21a308 100644 --- a/geom/ops.hpp +++ b/geom/ops.hpp @@ -57,8 +57,8 @@ namespace cruft::geom { template class A, template class B, typename = std::enable_if_t< - !std::is_same_v, A> && - !std::is_same_v, B> + !std::is_same_v, A> && + !std::is_same_v, B> > > T diff --git a/hash/buzhash.hpp b/hash/buzhash.hpp index 11b48ab6..8adb0157 100644 --- a/hash/buzhash.hpp +++ b/hash/buzhash.hpp @@ -65,7 +65,7 @@ namespace cruft::hash { buzhash (buzhash const&) = default; buzhash (buzhash &&) noexcept = default; buzhash& operator= (buzhash const&) = default; - buzhash& operator= (buzhash &&) = default; + buzhash& operator= (buzhash &&) noexcept = default; /// Rotate the hash over a pointer to the buffer we've been operating /// on. diff --git a/library_win32.cpp b/library_win32.cpp index bd55db8c..b2d37a74 100644 --- a/library_win32.cpp +++ b/library_win32.cpp @@ -22,7 +22,7 @@ library::library (const std::filesystem::path &path): //----------------------------------------------------------------------------- -library::library (library &&rhs): +library::library (library &&rhs) noexcept: m_handle (nullptr) { std::swap (m_handle, rhs.m_handle); @@ -31,7 +31,7 @@ library::library (library &&rhs): //----------------------------------------------------------------------------- library& -library::operator= (cruft::library &&rhs) +library::operator= (cruft::library &&rhs) noexcept { std::swap (m_handle, rhs.m_handle); return *this; diff --git a/library_win32.hpp b/library_win32.hpp index f5a07e5d..b0c8c025 100644 --- a/library_win32.hpp +++ b/library_win32.hpp @@ -22,8 +22,8 @@ namespace cruft { explicit library (const std::filesystem::path&); library (library const&) = delete; library& operator=(library const&) = delete; - library (library&&); - library& operator= (library&&); + library (library&&) noexcept; + library& operator= (library&&) noexcept; ~library (); diff --git a/parallel/stack.hpp b/parallel/stack.hpp index 4f3ebefe..591491f9 100644 --- a/parallel/stack.hpp +++ b/parallel/stack.hpp @@ -87,7 +87,7 @@ namespace cruft::parallel { /// the value `false` will be returned. /// /// NOTE: There are no exception guarantees at this time. - bool pop (ValueT *out) + bool pop [[nodiscard]] (ValueT *out) { std::lock_guard lk (m_lock); if (m_cursor == 0) @@ -109,7 +109,7 @@ namespace cruft::parallel { /// /// NOTE: There are no exception guarantees at this time. template - bool push (InputT &&arg) + bool push [[nodiscard]] (InputT &&arg) { std::lock_guard lk (m_lock); if (m_cursor >= m_store.size ()) diff --git a/parse/time.cpp b/parse/time.cpp index a0665a00..b50af854 100644 --- a/parse/time.cpp +++ b/parse/time.cpp @@ -51,11 +51,11 @@ enum plural_t { /// \param prefix The prefix we are checking for. /// \param plural Whether to (optionall) perform pluralisation on the prefix. /// \return True IFF the prefix was found and the view was updated. -template +template static bool try_consume_prefix ( cruft::view &str, - char const (&prefix)[_N], + char const (&prefix)[N_], plural_t plural = SINGULAR) { static_assert (N > 0); diff --git a/pointer.hpp b/pointer.hpp index cd19d320..ea247630 100644 --- a/pointer.hpp +++ b/pointer.hpp @@ -37,7 +37,7 @@ namespace cruft::ptr { thin (thin const&) = delete; thin& operator= (thin const&) = delete; - thin (thin &&rhs) + thin (thin &&rhs) noexcept : m_value (rhs.m_value) { rhs.m_value = nullptr; } diff --git a/pool.hpp b/pool.hpp index 1ffcf0d3..9d7dab53 100644 --- a/pool.hpp +++ b/pool.hpp @@ -63,7 +63,7 @@ namespace cruft { //--------------------------------------------------------------------- - pool& operator= (pool &&rhs) + pool& operator= (pool &&rhs) noexcept { std::swap (m_available, rhs.m_available); std::swap (m_store, rhs.m_store); @@ -268,7 +268,11 @@ namespace cruft { T* elements = reinterpret_cast (m_store); for (size_t i = m_capacity; i--; ) - m_available.push (elements + i); + // The available indices _should_ have enough capacity at all + // times as we've presumably allocated it at construction time, + // but it's worthwhile checking anyway. + if (!m_available.push (elements + i)) [[unlikely]] + throw std::bad_alloc (); } }; } diff --git a/posix/util.cpp b/posix/util.cpp index 5127e556..05b89602 100644 --- a/posix/util.cpp +++ b/posix/util.cpp @@ -27,7 +27,7 @@ cruft::posix::stat (char const *path) struct ::stat cruft::posix::stat (std::filesystem::path const &path) { - return stat (path.u8string ().c_str ()); + return stat (reinterpret_cast (path.u8string ().c_str ())); } diff --git a/rand/distribution/normal.hpp b/rand/distribution/normal.hpp index 341e894b..c37b2cac 100644 --- a/rand/distribution/normal.hpp +++ b/rand/distribution/normal.hpp @@ -3,13 +3,14 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * - * Copyright 2020, Danny Robson + * Copyright 2020-2021, Danny Robson */ #pragma once #include "uniform.hpp" +#include #include @@ -43,7 +44,7 @@ namespace cruft::rand::distribution { void reset (void) { - m_live = false; + m_prev.reset (); } @@ -61,9 +62,10 @@ namespace cruft::rand::distribution { result_type operator() (GeneratorT &&g, param_type const ¶ms) { - if (m_live) { - m_live = false; - return m_prev * params.stddev + params.mean; + if (m_prev) { + auto const res = m_prev.value () * params.stddev + params.mean; + m_prev.reset (); + return res; } auto [u, v, s] = find_uvs (g); @@ -71,7 +73,6 @@ namespace cruft::rand::distribution { result_type z1 = v * std::sqrt (-2 * std::log (s) / s); m_prev = z1; - m_live = true; return z0 * params.stddev + params.mean; } @@ -101,7 +102,6 @@ namespace cruft::rand::distribution { } param_type m_param; - bool m_live = false; - result_type m_prev; + std::optional m_prev; }; } diff --git a/range.cpp b/range.cpp index ad172abf..aca4dec8 100644 --- a/range.cpp +++ b/range.cpp @@ -13,6 +13,8 @@ #include "maths.hpp" #include "random.hpp" +#include + #include #include diff --git a/registrar.hpp b/registrar.hpp index 00995888..1b11b0db 100644 --- a/registrar.hpp +++ b/registrar.hpp @@ -42,7 +42,11 @@ namespace cruft { ~cookie () { - registry::remove (m_key); + try { + registry::remove (m_key); + } catch (std::exception const &err) { + LOG_ERROR ("Unable to remove registration for {}", m_key); + } } private: diff --git a/scoped.hpp b/scoped.hpp index c7090936..c41b45cf 100644 --- a/scoped.hpp +++ b/scoped.hpp @@ -82,8 +82,8 @@ namespace cruft::scoped { m_args (std::forward (_args)...) { ; } - function (function&&); - function (const function&); + function (function&&) noexcept; + function (function const&); ~function () { diff --git a/strongdef.hpp b/strongdef.hpp index 0e010a4e..9439317d 100644 --- a/strongdef.hpp +++ b/strongdef.hpp @@ -34,11 +34,11 @@ namespace cruft::strongdef { constexpr explicit index (cruft::types::identity_t const &_data): data (_data) { ; } constexpr index (index const&) = default; - constexpr index (index &&) = default; + constexpr index (index &&) noexcept (std::is_nothrow_constructible_v) = default; index& operator= (T const &) = delete; index& operator= (index const &) = default; - index& operator= (index &&) = default; + index& operator= (index &&) noexcept (std::is_nothrow_copy_assignable_v) = default; // conversion operators must not be explicit or it defeats the point // of this class (ease of use, transparency). diff --git a/test/parallel/stack.cpp b/test/parallel/stack.cpp index ec0d2e57..47e03306 100644 --- a/test/parallel/stack.cpp +++ b/test/parallel/stack.cpp @@ -27,14 +27,14 @@ int main () { static constexpr int COUNT = 4; cruft::parallel::stack values (COUNT); - for (int i = 0; i < COUNT; ++i) - values.push (i); bool success = true; - for (int i = COUNT - 1; i >= 0; --i) { - int res = -1; - values.pop (&res); + for (int i = 0; i < COUNT; ++i) + success = success && values.push (i); + for (int i = COUNT - 1; success && i >= 0; --i) { + int res = -1; + success = success && values.pop (&res); success = success && res == i; } diff --git a/zlib.cpp b/zlib.cpp index bd9a8dd5..c9804b1d 100644 --- a/zlib.cpp +++ b/zlib.cpp @@ -9,7 +9,7 @@ #include "zlib.hpp" -#include "debug.hpp" +#include "debug/assert.hpp" const char * cruft::zlib::version (void) {