Skip to content

Commit

Permalink
Don't use [[unlikely]] just before throw.
Browse files Browse the repository at this point in the history
Compilers will probably figure out that the `throw` is unlikely to happen.
Exceptions are, sort of by definition.
  • Loading branch information
jtv committed May 27, 2021
1 parent 3e50a0a commit a3e66cd
Show file tree
Hide file tree
Showing 27 changed files with 210 additions and 311 deletions.
3 changes: 2 additions & 1 deletion config-tests/concepts.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <vector>


template<typename T> concept Foo = std::ranges::input_range<T>;
template<typename T>
concept Foo = std::ranges::input_range<T>;


template<Foo F> auto foo(F const &r)
Expand Down
13 changes: 5 additions & 8 deletions include/pqxx/composite.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ inline void parse_composite(
auto const data{std::data(text)};
auto const size{std::size(text)};
if (size == 0)
PQXX_UNLIKELY
throw conversion_error{"Cannot parse composite value from empty string."};
throw conversion_error{"Cannot parse composite value from empty string."};

std::size_t here{0}, next{scan(data, size, here)};
if (next != 1 or data[here] != '(')
PQXX_UNLIKELY
throw conversion_error{
internal::concat("Invalid composite value string: ", text)};
throw conversion_error{
internal::concat("Invalid composite value string: ", text)};

here = next;

Expand Down Expand Up @@ -114,9 +112,8 @@ template<typename... T>
inline char *composite_into_buf(char *begin, char *end, T const &...fields)
{
if (std::size_t(end - begin) < composite_size_buffer(fields...))
PQXX_UNLIKELY
throw conversion_error{
"Buffer space may not be enough to represent composite value."};
throw conversion_error{
"Buffer space may not be enough to represent composite value."};

constexpr auto num_fields{sizeof...(fields)};
if constexpr (num_fields == 0)
Expand Down
24 changes: 10 additions & 14 deletions include/pqxx/connection.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,12 @@ concept ZKey_ZValues = std::ranges::input_range<T> and requires(T t)
{std::cbegin(t)};
{
std::get<0>(*std::cbegin(t))
}
->ZString;
} -> ZString;
{
std::get<1>(*std::cbegin(t))
}
->ZString;
}
and std::tuple_size_v<typename std::ranges::iterator_t<T>::value_type> == 2;
} -> ZString;
} and std::tuple_size_v<typename std::ranges::iterator_t<T>::value_type>
== 2;
#endif // PQXX_HAVE_CONCEPTS
} // namespace pqxx::internal

Expand Down Expand Up @@ -605,10 +603,9 @@ public:
auto const size{std::size(text)}, space{std::size(buffer)};
auto const needed{2 * size + 1};
if (space < needed)
PQXX_UNLIKELY
throw range_error{internal::concat(
"Not enough room to escape string of ", size, " byte(s): need ", needed,
" bytes of buffer space, but buffer size is ", space, ".")};
throw range_error{internal::concat(
"Not enough room to escape string of ", size, " byte(s): need ",
needed, " bytes of buffer space, but buffer size is ", space, ".")};
return std::string_view{
std::data(buffer), esc_to_buf(text, std::data(buffer))};
}
Expand Down Expand Up @@ -646,10 +643,9 @@ public:
auto const size{std::size(data)}, space{std::size(buffer)};
auto const needed{internal::size_esc_bin(std::size(data))};
if (space < needed)
PQXX_UNLIKELY
throw range_error{internal::concat(
"Not enough room to escape binary string of ", size, " byte(s): need ",
needed, " bytes of buffer space, but buffer size is ", space, ".")};
throw range_error{internal::concat(
"Not enough room to escape binary string of ", size, " byte(s): need ",
needed, " bytes of buffer space, but buffer size is ", space, ".")};

std::basic_string_view<std::byte> view{std::data(data), std::size(data)};
// Actually, in the modern format, we know beforehand exactly how many
Expand Down
4 changes: 2 additions & 2 deletions include/pqxx/field.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ template<>
inline std::nullptr_t from_string<std::nullptr_t>(field const &value)
{
if (not value.is_null())
PQXX_UNLIKELY
throw conversion_error{"Extracting non-null field into nullptr_t variable."};
throw conversion_error{
"Extracting non-null field into nullptr_t variable."};
return nullptr;
}

Expand Down
5 changes: 2 additions & 3 deletions include/pqxx/params.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ public:
void next()
{
if (m_current >= max_params)
PQXX_UNLIKELY
throw range_error{pqxx::internal::concat(
"Too many parameters in one statement: limit is ", max_params, ".")};
throw range_error{pqxx::internal::concat(
"Too many parameters in one statement: limit is ", max_params, ".")};
++m_current;
if (m_current % 10 == 0)
{
Expand Down
6 changes: 3 additions & 3 deletions include/pqxx/row.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ protected:
void check_size(size_type expected) const
{
if (size() != expected)
PQXX_UNLIKELY
throw usage_error{internal::concat(
"Tried to extract ", expected, " field(s) from a row of ", size(), ".")};
throw usage_error{internal::concat(
"Tried to extract ", expected, " field(s) from a row of ", size(),
".")};
}

template<typename... T> friend class pqxx::internal::result_iter;
Expand Down
4 changes: 2 additions & 2 deletions include/pqxx/strconv.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ template<typename TYPE> inline constexpr format param_format(TYPE const &)
* we can reference them by a pointer.
*/
template<class TYPE>
concept binary = std::ranges::contiguous_range<TYPE>
and std::is_same_v<strip_t<value_type<TYPE>>, std::byte>;
concept binary = std::ranges::contiguous_range<TYPE> and
std::is_same_v<strip_t<value_type<TYPE>>, std::byte>;
#endif
//@}
} // namespace pqxx
Expand Down
10 changes: 4 additions & 6 deletions include/pqxx/stream_from.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,9 @@ template<typename Tuple> inline stream_from &stream_from::operator>>(Tuple &t)
return *this;

if (std::size(m_fields) != tup_size)
PQXX_UNLIKELY
throw usage_error{internal::concat(
"Tried to extract ", tup_size, " field(s) from a stream of ",
std::size(m_fields), ".")};
throw usage_error{internal::concat(
"Tried to extract ", tup_size, " field(s) from a stream of ",
std::size(m_fields), ".")};

extract_fields(t, std::make_index_sequence<tup_size>{});
return *this;
Expand All @@ -332,8 +331,7 @@ inline void stream_from::extract_value(Tuple &t) const
if constexpr (nullity::always_null)
{
if (std::data(m_fields[index]) != nullptr)
PQXX_UNLIKELY
throw conversion_error{"Streaming non-null value into null field."};
throw conversion_error{"Streaming non-null value into null field."};
}
else if (std::data(m_fields[index]) == nullptr)
{
Expand Down
5 changes: 2 additions & 3 deletions include/pqxx/transaction_base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,8 @@ public:
{
row const r{exec1(query, desc)};
if (std::size(r) != 1)
PQXX_UNLIKELY
throw usage_error{internal::concat(
"Queried single value from result with ", std::size(r), " columns.")};
throw usage_error{internal::concat(
"Queried single value from result with ", std::size(r), " columns.")};
return r[0].as<TYPE>();
}

Expand Down
5 changes: 2 additions & 3 deletions include/pqxx/transactor.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ inline auto perform(TRANSACTION_CALLBACK &&callback, int attempts = 3)
-> std::invoke_result_t<TRANSACTION_CALLBACK>
{
if (attempts <= 0)
PQXX_UNLIKELY
throw std::invalid_argument{
"Zero or negative number of attempts passed to pqxx::perform()."};
throw std::invalid_argument{
"Zero or negative number of attempts passed to pqxx::perform()."};

for (; attempts > 0; --attempts)
{
Expand Down
7 changes: 4 additions & 3 deletions include/pqxx/types.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ using value_type = decltype(*std::begin(std::declval<CONTAINER>()));
#if defined(PQXX_HAVE_CONCEPTS)
/// Concept: Any type that we can read as a string of @c char.
template<typename STRING>
concept char_string = std::ranges::contiguous_range<STRING>
and std::same_as<strip_t<value_type<STRING>>, char>;
concept char_string = std::ranges::contiguous_range<STRING> and
std::same_as < strip_t<value_type<STRING>>,
char > ;

/// Concept: Anything we can iterate to get things we can read as strings.
template<typename RANGE>
Expand All @@ -118,7 +119,7 @@ concept char_strings =
/// Concept: Anything we might want to treat as binary data.
template<typename DATA>
concept potential_binary = std::ranges::contiguous_range<DATA> and
(sizeof(value_type<DATA>) == 1);
(sizeof(value_type<DATA>) == 1);
#endif // PQXX_HAVE_CONCEPTS


Expand Down
17 changes: 7 additions & 10 deletions include/pqxx/util.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,16 @@ inline TO check_cast(FROM value, std::string_view description)
if constexpr (std::is_signed_v<TO>)
{
if (value < to_limits::lowest())
PQXX_UNLIKELY
throw range_error{internal::cat2("Cast underflow: "sv, description)};
throw range_error{internal::cat2("Cast underflow: "sv, description)};
}
else
{
// FROM is signed, but TO is not. Treat this as a special case, because
// there may not be a good broader type in which the compiler can even
// perform our check.
if (value < 0)
PQXX_UNLIKELY
throw range_error{internal::cat2(
"Casting negative value to unsigned type: "sv, description)};
throw range_error{internal::cat2(
"Casting negative value to unsigned type: "sv, description)};
}
}
else
Expand All @@ -127,15 +125,13 @@ inline TO check_cast(FROM value, std::string_view description)
if constexpr (from_max > to_max)
{
if (static_cast<unsigned_from>(value) > to_max)
PQXX_UNLIKELY
throw range_error{internal::cat2("Cast overflow: "sv, description)};
throw range_error{internal::cat2("Cast overflow: "sv, description)};
}
}
else if constexpr ((from_limits::max)() > (to_limits::max)())
{
if (value > (to_limits::max)())
PQXX_UNLIKELY
throw range_error{internal::cat2("Cast overflow: ", description)};
throw range_error{internal::cat2("Cast overflow: ", description)};
}

return static_cast<TO>(value);
Expand Down Expand Up @@ -243,7 +239,8 @@ std::basic_string_view<std::byte> binary_cast(TYPE const &data)


#if defined(PQXX_HAVE_CONCEPTS)
template<typename CHAR> concept char_sized = (sizeof(CHAR) == 1);
template<typename CHAR>
concept char_sized = (sizeof(CHAR) == 1);
# define PQXX_CHAR_SIZED_ARG char_sized
#else
# define PQXX_CHAR_SIZED_ARG typename
Expand Down
6 changes: 3 additions & 3 deletions include/pqxx/zview.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ namespace pqxx::internal
* support each of these individually.
*/
template<typename T>
concept ZString = std::is_convertible_v<strip_t<T>, char const *> or
std::is_convertible_v<strip_t<T>, zview> or
std::is_convertible_v<T, std::string const &>;
concept ZString = std::is_convertible_v < strip_t<T>,
char const * > or std::is_convertible_v<strip_t<T>, zview> or
std::is_convertible_v<T, std::string const &>;
} // namespace pqxx::internal
#endif // PQXX_HAVE_CONCEPTS

Expand Down
3 changes: 1 addition & 2 deletions src/array.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ std::string::size_type array_parser::scan_single_quoted_string() const
break;
}
}
PQXX_UNLIKELY
throw argument_error{internal::concat("Null byte in SQL string: ", m_input)};
}

Expand Down Expand Up @@ -177,7 +176,7 @@ std::pair<array_parser::juncture, std::string> array_parser::get_next()
else
switch (m_input[m_pos])
{
case '\0': PQXX_UNLIKELY throw failure{"Unexpected zero byte in array."};
case '\0': throw failure{"Unexpected zero byte in array."};
case '{':
found = juncture::row_start;
end = scan_glyph(m_pos);
Expand Down
Loading

0 comments on commit a3e66cd

Please sign in to comment.