From a8b3e29cac31317aea4c5b1fe874d527ce08ae26 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 14 Aug 2019 23:16:38 -0700 Subject: [PATCH] ircd: Apply noexcept on various constexpr callstacks. --- include/ircd/json/tuple/get.h | 8 ++++++++ include/ircd/json/tuple/indexof.h | 8 ++++++++ include/ircd/json/tuple/key.h | 4 ++++ include/ircd/json/tuple/tuple.h | 14 ++++++++++---- include/ircd/json/util.h | 6 ++++-- include/ircd/string_view.h | 22 ++++++++++++---------- include/ircd/util/hash.h | 9 ++++++--- 7 files changed, 52 insertions(+), 19 deletions(-) diff --git a/include/ircd/json/tuple/get.h b/include/ircd/json/tuple/get.h index 0532e9e9c..a92b9b3cf 100644 --- a/include/ircd/json/tuple/get.h +++ b/include/ircd/json/tuple/get.h @@ -18,6 +18,7 @@ template enable_if_tuple()> &> get(const tuple &t) +noexcept { constexpr size_t idx { @@ -37,6 +38,7 @@ template()>> get(const tuple &t, const tuple_value_type()> &def) +noexcept { constexpr size_t idx { @@ -55,6 +57,7 @@ template enable_if_tuple()> &> get(tuple &t) +noexcept { constexpr size_t idx { @@ -74,6 +77,7 @@ template()> &> get(tuple &t, tuple_value_type()> &def) +noexcept { auto &ret { @@ -87,6 +91,7 @@ template enable_if_tuple()> &> get(const tuple &t) +noexcept { return get(t); } @@ -96,6 +101,7 @@ template()>> get(const tuple &t, const tuple_value_type()> &def) +noexcept { return get(t, def); } @@ -104,6 +110,7 @@ template enable_if_tuple()>> get(tuple &t) +noexcept { return get(t); } @@ -113,6 +120,7 @@ template()>> get(tuple &t, tuple_value_type()> &def) +noexcept { return get(t, def); } diff --git a/include/ircd/json/tuple/indexof.h b/include/ircd/json/tuple/indexof.h index 7c04f4db4..658d700a3 100644 --- a/include/ircd/json/tuple/indexof.h +++ b/include/ircd/json/tuple/indexof.h @@ -19,6 +19,7 @@ template constexpr typename std::enable_if(), size_t>::type indexof() +noexcept { return size(); } @@ -28,6 +29,7 @@ template constexpr typename std::enable_if(), size_t>::type indexof() +noexcept { constexpr auto equal { @@ -42,6 +44,7 @@ template constexpr typename std::enable_if(), size_t>::type indexof() +noexcept { return size(); } @@ -51,6 +54,7 @@ template constexpr typename std::enable_if(), size_t>::type indexof() +noexcept { return indexof(); } @@ -59,6 +63,7 @@ template constexpr typename std::enable_if(), size_t>::type indexof(const char *const &name) +noexcept { return size(); } @@ -67,6 +72,7 @@ template constexpr typename std::enable_if(), size_t>::type indexof(const char *const &name) +noexcept { constexpr auto equal { @@ -80,6 +86,7 @@ template constexpr typename std::enable_if(), size_t>::type indexof(const string_view &name) +noexcept { return size(); } @@ -88,6 +95,7 @@ template constexpr typename std::enable_if(), size_t>::type indexof(const string_view &name) +noexcept { const auto equal { diff --git a/include/ircd/json/tuple/key.h b/include/ircd/json/tuple/key.h index 6846b99b0..ca91b2035 100644 --- a/include/ircd/json/tuple/key.h +++ b/include/ircd/json/tuple/key.h @@ -18,6 +18,7 @@ template constexpr enable_if_tuple key() +noexcept { return tuple_element::key; } @@ -26,6 +27,7 @@ template constexpr typename std::enable_if::type key(const size_t &j) +noexcept { return nullptr; } @@ -34,6 +36,7 @@ template constexpr typename std::enable_if::type key(const size_t &j) +noexcept { return j == i? tuple_element::key: @@ -44,6 +47,7 @@ template enable_if_tuple key(const tuple &t) +noexcept { return std::get(t).key; } diff --git a/include/ircd/json/tuple/tuple.h b/include/ircd/json/tuple/tuple.h index c3cd36de8..994183012 100644 --- a/include/ircd/json/tuple/tuple.h +++ b/include/ircd/json/tuple/tuple.h @@ -57,13 +57,13 @@ struct tuple using tuple_type = std::tuple; using super_type = tuple; - static constexpr size_t size(); + static constexpr size_t size() noexcept; operator json::value() const; operator crh::sha256::buf() const; - template constexpr decltype(auto) get(name&&) const; - template constexpr decltype(auto) get(name&&); + template constexpr decltype(auto) get(name&&) const noexcept; + template constexpr decltype(auto) get(name&&) noexcept; template constexpr decltype(auto) at(name&&) const; template constexpr decltype(auto) at(name&&); @@ -79,6 +79,7 @@ struct tuple template constexpr bool is_tuple() +noexcept { return std::is_base_of::value; } @@ -123,6 +124,7 @@ stdcast(tuple &o) template constexpr enable_if_tuple size() +noexcept { return tuple_size::value; } @@ -140,6 +142,7 @@ template enable_if_tuple &> val(tuple &t) +noexcept { return static_cast &>(std::get(t)); } @@ -148,6 +151,7 @@ template enable_if_tuple &> val(const tuple &t) +noexcept { return static_cast &>(std::get(t)); } @@ -269,6 +273,7 @@ template template constexpr decltype(auto) tuple::get(name&& n) +noexcept { constexpr const size_t hash { @@ -282,7 +287,7 @@ template template constexpr decltype(auto) tuple::get(name&& n) -const +const noexcept { constexpr const size_t hash { @@ -295,6 +300,7 @@ const template constexpr size_t tuple::size() +noexcept { return std::tuple_size(); } diff --git a/include/ircd/json/util.h b/include/ircd/json/util.h index 94300c6f9..dbbadc8d2 100644 --- a/include/ircd/json/util.h +++ b/include/ircd/json/util.h @@ -21,8 +21,8 @@ namespace ircd::json template struct buffer; using name_hash_t = size_t; - constexpr name_hash_t name_hash(const string_view name); - constexpr name_hash_t operator ""_(const char *const name, const size_t len); + constexpr name_hash_t name_hash(const string_view name) noexcept; + constexpr name_hash_t operator ""_(const char *const name, const size_t len) noexcept; size_t serialized(const string_view &); string_view stringify(mutable_buffer &, const string_view &); @@ -79,12 +79,14 @@ ircd::json::print(const mutable_buffer &buf, constexpr ircd::json::name_hash_t ircd::json::operator ""_(const char *const text, const size_t len) +noexcept { return name_hash(string_view(text, len)); } constexpr ircd::json::name_hash_t ircd::json::name_hash(const string_view name) +noexcept { return ircd::hash(name); } diff --git a/include/ircd/string_view.h b/include/ircd/string_view.h index 2727f4e2e..e98639afe 100644 --- a/include/ircd/string_view.h +++ b/include/ircd/string_view.h @@ -15,9 +15,9 @@ namespace ircd { struct string_view; - constexpr size_t _constexpr_strlen(const char *); - constexpr const char *data(const string_view &); - constexpr size_t size(const string_view &); + constexpr size_t _constexpr_strlen(const char *) noexcept; + constexpr const char *data(const string_view &) noexcept; + constexpr size_t size(const string_view &) noexcept; bool empty(const string_view &); bool operator!(const string_view &); bool defined(const string_view &); @@ -144,7 +144,7 @@ struct ircd::string_view } // (non-standard) our iterator-based constructor - constexpr string_view(const char *const &begin, const char *const &end) + constexpr string_view(const char *const &begin, const char *const &end) noexcept :string_view{begin, size_t(end - begin)} { assert(begin <= end); @@ -158,7 +158,7 @@ struct ircd::string_view // (non-standard) our array based constructor template constexpr __attribute__((always_inline)) - string_view(const std::array &array) + string_view(const std::array &array) noexcept :string_view { array.data(), std::find(array.begin(), array.end(), '\0') @@ -167,22 +167,21 @@ struct ircd::string_view // (non-standard) our buffer based constructor template constexpr __attribute__((always_inline)) - string_view(const char (&buf)[SIZE]) + string_view(const char (&buf)[SIZE]) noexcept :string_view { buf, std::find(buf, buf + SIZE, '\0') }{} - constexpr string_view(const char *const &start, const size_t &size) + constexpr string_view(const char *const &start, const size_t &size) noexcept :std::string_view{start, size} {} - constexpr string_view(const char *const &start) + constexpr string_view(const char *const &start) noexcept :std::string_view{start, _constexpr_strlen(start)} {} - string_view(const std::string &string) - noexcept + string_view(const std::string &string) noexcept :string_view{string.data(), string.size()} {} @@ -257,12 +256,14 @@ ircd::defined(const string_view &str) constexpr size_t ircd::size(const string_view &str) +noexcept { return str.size(); } constexpr const char * ircd::data(const string_view &str) +noexcept { return str.data(); } @@ -270,6 +271,7 @@ ircd::data(const string_view &str) [[gnu::pure]] constexpr size_t ircd::_constexpr_strlen(const char *const s) +noexcept { const char *e(s); if(e) for(; *e; ++e); diff --git a/include/ircd/util/hash.h b/include/ircd/util/hash.h index fb79ddaf0..3b24e610b 100644 --- a/include/ircd/util/hash.h +++ b/include/ircd/util/hash.h @@ -17,12 +17,12 @@ inline namespace util // constexpr bernstein string hasher suite; these functions will hash the // string at compile time leaving an integer residue at runtime. Decent // primes are at least 7681 and 5381. - template constexpr size_t hash(const char16_t *const str, const size_t i = 0); - template constexpr size_t hash(const string_view str, const size_t i = 0); + template constexpr size_t hash(const char16_t *const str, const size_t i = 0) noexcept; + template constexpr size_t hash(const string_view str, const size_t i = 0) noexcept; // Note that at runtime this hash uses multiplication on every character // which can consume many cycles... - template size_t hash(const std::u16string &str, const size_t i = 0); + template size_t hash(const std::u16string &str, const size_t i = 0) noexcept; }} /// Runtime hashing of a std::u16string (for js). Non-cryptographic. @@ -31,6 +31,7 @@ template size_t ircd::util::hash(const std::u16string &str, const size_t i) +noexcept { return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i); } @@ -41,6 +42,7 @@ template constexpr size_t ircd::util::hash(const string_view str, const size_t i) +noexcept { return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i); } @@ -51,6 +53,7 @@ template constexpr size_t ircd::util::hash(const char16_t *const str, const size_t i) +noexcept { return !str[i]? PRIME : (hash(str, i+1) * 33ULL) ^ str[i]; }