From b61d55ee3ce49abe378838d7519a78106b8bd39f Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 3 Jan 2019 15:48:34 +1100 Subject: [PATCH] build: satiate clang-tidy somewhat --- adapter.hpp | 5 +++-- alloc/linear.cpp | 4 ++-- alloc/linear.hpp | 4 ++-- ascii.hpp | 3 ++- coord/base.hpp | 23 +++++++++++++++++++---- extent.cpp | 6 +++--- geom/ray.cpp | 6 +++--- hash/murmur/murmur2.cpp | 6 ++++-- hash/xxhash.cpp | 2 +- point.cpp | 36 +++++++++++++----------------------- rational.cpp | 5 ++++- tap.hpp | 38 +++++++++++++++++++++++++++++++++----- test/array/darray.cpp | 12 ++++++------ test/ascii.cpp | 36 +++++++++++++++++------------------- test/backtrace.cpp | 5 ++++- test/cmdopt.cpp | 30 ++++++++++++++---------------- test/hash/crc.cpp | 28 +++++++++++++--------------- test/pool.cpp | 14 ++++++-------- test/signal.cpp | 19 +++++++++---------- vector.cpp | 21 ++++----------------- 20 files changed, 162 insertions(+), 141 deletions(-) diff --git a/adapter.hpp b/adapter.hpp index 4b4d3dea..7bffa874 100644 --- a/adapter.hpp +++ b/adapter.hpp @@ -90,8 +90,9 @@ namespace cruft::adapter { public: using inner_type = typename std::iterator_traits::value_type; - using value_type = decltype (std::get (std::declval)); + using value_type = decltype (std::get (std::declval ())); using reference = value_type&; + using const_reference = value_type const&; explicit scalar (IteratorT _inner): @@ -99,7 +100,7 @@ namespace cruft::adapter { { ; } - const reference operator* (void) const& + const_reference operator* (void) const& { return std::get (*m_inner); } reference operator* (void)& diff --git a/alloc/linear.cpp b/alloc/linear.cpp index 62821c9f..15c7ba62 100644 --- a/alloc/linear.cpp +++ b/alloc/linear.cpp @@ -25,7 +25,7 @@ linear::linear (cruft::view _data): /////////////////////////////////////////////////////////////////////////////// -linear::linear (linear &&rhs) +linear::linear (linear &&rhs) noexcept : m_begin (std::exchange (rhs.m_begin, nullptr)) , m_end (std::exchange (rhs.m_end, nullptr)) , m_cursor (std::exchange (rhs.m_cursor, nullptr)) @@ -34,7 +34,7 @@ linear::linear (linear &&rhs) //----------------------------------------------------------------------------- linear& -linear::operator= (linear &&rhs) +linear::operator= (linear &&rhs) noexcept { m_begin = std::exchange (rhs.m_begin, nullptr); m_end = std::exchange (rhs.m_end, nullptr); diff --git a/alloc/linear.hpp b/alloc/linear.hpp index 8d9fa7d4..cb7826af 100644 --- a/alloc/linear.hpp +++ b/alloc/linear.hpp @@ -25,8 +25,8 @@ namespace cruft::alloc { linear (const linear&) = delete; linear& operator= (const linear&) = delete; - linear (linear&&); - linear& operator= (linear&&); + linear (linear&&) noexcept; + linear& operator= (linear&&) noexcept; linear (cruft::view _data); diff --git a/ascii.hpp b/ascii.hpp index d3623a96..612dcf50 100644 --- a/ascii.hpp +++ b/ascii.hpp @@ -3,12 +3,13 @@ * 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-2018 Danny Robson + * Copyright 2016-2019 Danny Robson */ #pragma once #include "annotation.hpp" +#include "debug.hpp" #include #include diff --git a/coord/base.hpp b/coord/base.hpp index 9e15a349..bddd2bca 100644 --- a/coord/base.hpp +++ b/coord/base.hpp @@ -3,11 +3,10 @@ * 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 2012-2017 Danny Robson + * Copyright 2012-2019 Danny Robson */ -#ifndef CRUFT_UTIL_COORD_BASE_HPP -#define CRUFT_UTIL_COORD_BASE_HPP +#pragma once #include "fwd.hpp" @@ -254,4 +253,20 @@ namespace cruft::coord { }; } -#endif + +/// Invoke a macro across all typical coordinate parameter combinations. +/// +/// ie, each of the types i16, i32, i64, f32, and f64; and arities of 1, 2, 3, +/// and 4. +/// +/// This is useful for exhaustive delayed instantiation in .cpp files. + +#define MAP_CRUFT_COORD_PARAMS(F) \ +F(1,i16) F(2,i16) F(3,i16) F(4,i16) \ +F(1,i32) F(2,i32) F(3,i32) F(4,i32) \ +F(1,i64) F(2,i64) F(3,i64) F(4,i64) \ +F(1,u16) F(2,u16) F(3,u16) F(4,u16) \ +F(1,u32) F(2,u32) F(3,u32) F(4,u32) \ +F(1,u64) F(2,u64) F(3,u64) F(4,u64) \ +F(1,f32) F(2,f32) F(3,f32) F(4,f32) \ +F(1,f64) F(2,f64) F(3,f64) F(4,f64) diff --git a/extent.cpp b/extent.cpp index ade25411..402191c3 100644 --- a/extent.cpp +++ b/extent.cpp @@ -93,9 +93,9 @@ namespace cruft::debug { //----------------------------------------------------------------------------- #define INSTANTIATE_S_T(S,T) \ -template struct ::cruft::extent; \ -template bool ::cruft::debug::is_valid (const ::cruft::extent&); \ -template struct ::cruft::debug::validator<::cruft::extent>; +template struct ::cruft::extent<(S),T>; \ +template bool ::cruft::debug::is_valid (const ::cruft::extent<(S),T>&); \ +template struct ::cruft::debug::validator<::cruft::extent<(S),T>>; #define INSTANTIATE(T) \ INSTANTIATE_S_T(1,T) \ diff --git a/geom/ray.cpp b/geom/ray.cpp index cacd4ad6..51a6486e 100644 --- a/geom/ray.cpp +++ b/geom/ray.cpp @@ -60,9 +60,9 @@ struct cruft::debug::validator> { /////////////////////////////////////////////////////////////////////////////// #define INSTANTIATE_S_T(S,T) \ -template std::ostream& cruft::geom::operator<< (std::ostream&, ray); \ -template struct cruft::debug::validator>; \ -template struct cruft::geom::ray; +template std::ostream& cruft::geom::operator<< (std::ostream&, ray<(S),T>); \ +template struct cruft::debug::validator>; \ +template struct cruft::geom::ray<(S),T>; INSTANTIATE_S_T(2,float) diff --git a/hash/murmur/murmur2.cpp b/hash/murmur/murmur2.cpp index 34f38cf6..90e5c42f 100644 --- a/hash/murmur/murmur2.cpp +++ b/hash/murmur/murmur2.cpp @@ -86,11 +86,13 @@ murmur2::operator() (cruft::view data) const noexcept { static_assert (std::is_same_v || std::is_same_v); - if constexpr (std::is_same_v) + if constexpr (std::is_same_v) { return hash_32 (data.data (), data.size (), m_seed); + } - if constexpr (std::is_same_v) + if constexpr (std::is_same_v) { return hash_64 (data.data (), data.size (), m_seed); + } unreachable (); } diff --git a/hash/xxhash.cpp b/hash/xxhash.cpp index ddb5962e..1e7498c2 100644 --- a/hash/xxhash.cpp +++ b/hash/xxhash.cpp @@ -31,8 +31,8 @@ read_le (const void *ptr) template struct constants { static const T prime[5]; - static const T round_rotate; static const T final_rotate[3]; + static const T round_rotate; }; diff --git a/point.cpp b/point.cpp index b720f4b4..1f18ff78 100644 --- a/point.cpp +++ b/point.cpp @@ -9,6 +9,7 @@ #include "point.hpp" #include "debug.hpp" +#include "std.hpp" #include @@ -58,27 +59,16 @@ cruft::furthest (cruft::view*> src) } +/////////////////////////////////////////////////////////////////////////////// +#define INSTANTIATE_S_T(S,T) \ +template struct cruft::point<(S),T>; \ +template bool cruft::debug::is_valid (const point<(S),T>&); \ +template std::pair< \ + cruft::point<(S),T>, \ + cruft::point<(S),T> \ +> cruft::furthest (cruft::view*>); \ +template struct cruft::debug::validator>; + + //----------------------------------------------------------------------------- -#define INSTANTIATE_S_T(S,T) \ -template struct cruft::point; \ -template bool cruft::debug::is_valid (const point&); \ -template std::pair,cruft::point> cruft::furthest (cruft::view*>); \ -template struct cruft::debug::validator>; - -#define INSTANTIATE(T) \ -INSTANTIATE_S_T(1,T) \ -INSTANTIATE_S_T(2,T) \ -INSTANTIATE_S_T(3,T) \ -INSTANTIATE_S_T(4,T) - - -INSTANTIATE(int16_t) -INSTANTIATE(int32_t) -INSTANTIATE(int64_t) - -INSTANTIATE(uint16_t) -INSTANTIATE(uint32_t) -INSTANTIATE(uint64_t) - -INSTANTIATE(float) -INSTANTIATE(double) +MAP_CRUFT_COORD_PARAMS(INSTANTIATE_S_T) diff --git a/rational.cpp b/rational.cpp index ab254eb6..621716f3 100644 --- a/rational.cpp +++ b/rational.cpp @@ -102,9 +102,12 @@ template rational rational::reduced (void) const { + if (n == 0) { + return { 0, 1 }; + } + CHECK_NEZ (d); auto x = std::gcd (abs (n), abs (d)); - return { n / x, d / x }; } diff --git a/tap.hpp b/tap.hpp index 3a8b6c11..7913aab9 100644 --- a/tap.hpp +++ b/tap.hpp @@ -3,13 +3,13 @@ * 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 2015-2017 Danny Robson + * Copyright 2015-2019 Danny Robson */ -#ifndef CRUFT_UTIL_TAP_HPP -#define CRUFT_UTIL_TAP_HPP +#pragma once #include "debug.hpp" +#include "except.hpp" #include "format.hpp" #include "maths.hpp" @@ -45,6 +45,7 @@ namespace cruft::TAP { logger& operator= (logger const &) = delete; logger& operator= (logger &&) = delete; + //--------------------------------------------------------------------- template bool @@ -223,6 +224,35 @@ namespace cruft::TAP { int status [[nodiscard]] (void) const; + /// Invoke a FunctionT with a TAP::logger as the first argument, and + /// the remainder forwarded from the provided parameter pack. + /// + /// If an exception escapes from the FunctionT the logger will be + /// marked as failed, using a message that attempts to use the raised + /// exception value (else a static generic message if the type isn't + /// easily discoverable). + /// + /// Returns the status of the logger provided to the FunctionT. + template + static auto + run [[nodiscard]] (FunctionT &&function, Args&&...args) noexcept + { + logger tap; + try { + function (tap, args...); + return tap.status (); + } catch (std::exception const &err) { + tap.fail ("no exceptions: %s", err.what ()); + } catch (cruft::error const &err) { + tap.fail ("no exceptions: %s", err); + } catch (...) { + tap.fail ("no exceptions"); + } + + return EXIT_FAILURE; + } + + private: #if !defined(NDEBUG) mutable int m_reported = -1; @@ -233,5 +263,3 @@ namespace cruft::TAP { size_t m_size; }; } - -#endif diff --git a/test/array/darray.cpp b/test/array/darray.cpp index 27ab4ec2..3ab6e953 100644 --- a/test/array/darray.cpp +++ b/test/array/darray.cpp @@ -20,16 +20,16 @@ main (int, char**) tap.expect_eq (val.capacity (), decltype(val)::elements, "static vs dynamic query for capacity"); { - auto const sum = std::accumulate (val.cbegin (), val.cend (), 0u); - tap.expect_eq (sum, 28u, "accumulate over initializer_list range"); + auto const sum = std::accumulate (val.cbegin (), val.cend (), 0); + tap.expect_eq (sum, 28, "accumulate over initializer_list range"); } { val.erase (val.begin () + 2); tap.expect_eq (val.size (), 7u, "erasing removes one element"); - auto const sum = std::accumulate (val.cbegin (), val.cend (), 0u); - tap.expect_eq (sum, 26u, "erase one element"); + auto const sum = std::accumulate (val.cbegin (), val.cend (), 0); + tap.expect_eq (sum, 26, "erase one element"); } { @@ -39,8 +39,8 @@ main (int, char**) // Perform an accumulate to check the rest of the values are there too. val.insert (val.begin () + 4, 13); tap.expect_eq (val[4], 13, "inserted values matches indexed value"); - auto const sum = std::accumulate (val.cbegin (), val.cend (), 0u); - tap.expect_eq (sum, 39u, "accumulate over new array suggests all still present"); + auto const sum = std::accumulate (val.cbegin (), val.cend (), 0); + tap.expect_eq (sum, 39, "accumulate over new array suggests all still present"); } } diff --git a/test/ascii.cpp b/test/ascii.cpp index 3d1e2bbb..909c3d3f 100644 --- a/test/ascii.cpp +++ b/test/ascii.cpp @@ -13,8 +13,6 @@ int main (int, char**) { - cruft::TAP::logger tap; - static constexpr bool T = true; static constexpr bool F = false; @@ -49,26 +47,26 @@ main (int, char**) { '\n', F, F, F, F, T, 0, 0, "newline" }, }; + return cruft::TAP::logger::run ([] (auto &tap) { #define TRY_BOOL(X) [&] () -> bool { try { return X; } catch (...) { return false; }; } () - for (auto &t: TESTS) { - bool is_digit = TRY_BOOL(cruft::ascii::is_digit (t.character)) == t.is_digit; - bool is_hex = TRY_BOOL(cruft::ascii::is_hex (t.character)) == t.is_hex; - bool is_upper = TRY_BOOL(cruft::ascii::is_upper (t.character)) == t.is_upper; - bool is_lower = TRY_BOOL(cruft::ascii::is_lower (t.character)) == t.is_lower; - bool is_space = TRY_BOOL(cruft::ascii::is_space (t.character)) == t.is_space; + for (auto &t: TESTS) { + bool is_digit = TRY_BOOL(cruft::ascii::is_digit (t.character)) == t.is_digit; + bool is_hex = TRY_BOOL(cruft::ascii::is_hex (t.character)) == t.is_hex; + bool is_upper = TRY_BOOL(cruft::ascii::is_upper (t.character)) == t.is_upper; + bool is_lower = TRY_BOOL(cruft::ascii::is_lower (t.character)) == t.is_lower; + bool is_space = TRY_BOOL(cruft::ascii::is_space (t.character)) == t.is_space; - bool integer = !t.is_digit || cruft::ascii::to_integer (t.character) == t.integer; - bool hexadecimal = !t.is_hex || cruft::ascii::from_hex (t.character) == t.hexadecimal; + bool integer = !t.is_digit || cruft::ascii::to_integer (t.character) == t.integer; + bool hexadecimal = !t.is_hex || cruft::ascii::from_hex (t.character) == t.hexadecimal; - tap.expect ( - is_digit && is_hex && is_upper && is_lower && is_space && integer && hexadecimal, - "%s", t.msg - ); - } + tap.expect ( + is_digit && is_hex && is_upper && is_lower && is_space && integer && hexadecimal, + "%s", t.msg + ); + } - tap.expect_eq ("6a"_hex2u8, std::vector({0x6a}), "hex2u8, 1 byte"); - tap.expect_eq ("a6"_hex2array, std::array ({0xa6}), "hex2array, 1 byte"); - - return tap.status (); + tap.expect_eq ("6a"_hex2u8, std::vector({0x6a}), "hex2u8, 1 byte"); + tap.expect_eq ("a6"_hex2array, std::array ({0xa6}), "hex2array, 1 byte"); + }); } diff --git a/test/backtrace.cpp b/test/backtrace.cpp index d747a732..da050732 100644 --- a/test/backtrace.cpp +++ b/test/backtrace.cpp @@ -10,7 +10,10 @@ int main (int, char **) { cruft::TAP::logger tap; - debug::backtrace (); + // Instantiate a backtrace just so we know that linking has worked. + debug::backtrace foo; + (void)foo; + tap.noop (); return tap.status (); diff --git a/test/cmdopt.cpp b/test/cmdopt.cpp index 5901ef6a..0366f093 100644 --- a/test/cmdopt.cpp +++ b/test/cmdopt.cpp @@ -249,20 +249,18 @@ test_positional (cruft::TAP::logger &tap) /////////////////////////////////////////////////////////////////////////////// int main (int, char **) { - cruft::TAP::logger tap; - - test_null (tap); - test_present (tap); - test_bool (tap); - test_numeric< int16_t> (tap); - test_numeric< int32_t> (tap); - test_numeric< int64_t> (tap); - test_numeric (tap); - test_numeric (tap); - test_numeric (tap); - test_bytes (tap); - test_required (tap); - test_positional (tap); - - return tap.status (); + return cruft::TAP::logger::run ([] (auto &tap) { + test_null (tap); + test_present (tap); + test_bool (tap); + test_numeric< int16_t> (tap); + test_numeric< int32_t> (tap); + test_numeric< int64_t> (tap); + test_numeric (tap); + test_numeric (tap); + test_numeric (tap); + test_bytes (tap); + test_required (tap); + test_positional (tap); + }); } diff --git a/test/hash/crc.cpp b/test/hash/crc.cpp index 31de3e67..d1ab4c7f 100644 --- a/test/hash/crc.cpp +++ b/test/hash/crc.cpp @@ -30,20 +30,18 @@ struct { int main (int, char**) { - cruft::TAP::logger tap; + return cruft::TAP::logger::run ([] (auto &tap) { + for (const auto &t: TESTS) { + #define TEST(KLASS) do { \ + auto computed = cruft::hash::KLASS{}(cruft::view {t.dat}.template cast ()); \ + tap.expect_eq (t.result.KLASS, computed, "%s: %s", #KLASS, t.msg); \ + } while (0) - for (const auto &t: TESTS) { - #define TEST(KLASS) do { \ - auto computed = cruft::hash::KLASS{}(cruft::view {t.dat}.template cast ()); \ - tap.expect_eq (t.result.KLASS, computed, "%s: %s", #KLASS, t.msg); \ - } while (0) - - TEST(crc32); - TEST(crc32b); - TEST(crc32c); - TEST(crc32d); - TEST(crc64); - } - - return tap.status (); + TEST(crc32); + TEST(crc32b); + TEST(crc32c); + TEST(crc32d); + TEST(crc64); + } + }); } \ No newline at end of file diff --git a/test/pool.cpp b/test/pool.cpp index 8ebb2b3c..e2979f45 100644 --- a/test/pool.cpp +++ b/test/pool.cpp @@ -124,12 +124,10 @@ done: int main (int, char **) { - cruft::TAP::logger tap; - - check_single (tap); - check_unique_ptr (tap); - check_keep_value (tap); - check_keep_variadic_value (tap); - - return tap.status (); + return cruft::TAP::logger::run ([] (auto &tap) { + check_single (tap); + check_unique_ptr (tap); + check_keep_value (tap); + check_keep_variadic_value (tap); + }); } diff --git a/test/signal.cpp b/test/signal.cpp index 167b1d8b..cae05a45 100644 --- a/test/signal.cpp +++ b/test/signal.cpp @@ -1,5 +1,6 @@ #include "signal.hpp" +#include "except.hpp" #include "tap.hpp" @@ -124,14 +125,12 @@ test_disconnect (cruft::TAP::logger &tap) int main (int, char **) { - cruft::TAP::logger tap; - - test_null (tap); - test_single (tap); - test_double (tap); - test_value_signal (tap); - test_combiner (tap); - test_disconnect (tap); - - return tap.status (); + return cruft::TAP::logger::run ([] (auto &tap) { + test_null (tap); + test_single (tap); + test_double (tap); + test_value_signal (tap); + test_combiner (tap); + test_disconnect (tap); + }); } diff --git a/vector.cpp b/vector.cpp index f45af9ea..f01f0639 100644 --- a/vector.cpp +++ b/vector.cpp @@ -9,6 +9,7 @@ #include "vector.hpp" #include "debug.hpp" +#include "std.hpp" #include @@ -91,24 +92,10 @@ struct cruft::debug::validator> { /////////////////////////////////////////////////////////////////////////////// #define INSTANTIATE_S_T(S,T) \ -template struct cruft::vector; \ -template struct cruft::debug::validator>; - - -#define INSTANTIATE(T) \ -INSTANTIATE_S_T(1,T) \ -INSTANTIATE_S_T(2,T) \ -INSTANTIATE_S_T(3,T) \ -INSTANTIATE_S_T(4,T) - - -INSTANTIATE(uint32_t) -INSTANTIATE(int32_t) -INSTANTIATE(uint64_t) -INSTANTIATE(int64_t) -INSTANTIATE(float) -INSTANTIATE(double) +template struct cruft::vector<(S),T>; \ +template struct cruft::debug::validator>; +MAP_CRUFT_COORD_PARAMS(INSTANTIATE_S_T) //----------------------------------------------------------------------------- template vector<2,float> cruft::polar_to_cartesian (cruft::vector<2,float>);