Skip to content

Commit

Permalink
Fixes for clang
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Osterfeld <[email protected]>
  • Loading branch information
frankosterfeld committed Nov 23, 2024
1 parent db4f131 commit 8d787f2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
60 changes: 33 additions & 27 deletions core/include/gnuradio-4.0/YamlPmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,39 +462,45 @@ std::expected<T, ValueParseError> parseAs(std::string_view sv) {
return false;
}
return std::unexpected(ValueParseError{0UZ, "Invalid value for type"});
} else if constexpr (std::is_arithmetic_v<T>) {
if constexpr (std::is_floating_point_v<T>) {
if (sv == ".inf" || sv == ".Inf" || sv == ".INF") {
return std::numeric_limits<T>::infinity();
} else if (sv == "-.inf" || sv == "-.Inf" || sv == "-.INF") {
return -std::numeric_limits<T>::infinity();
} else if (sv == ".nan" || sv == ".NaN" || sv == ".NAN") {
return std::numeric_limits<T>::quiet_NaN();
} else if constexpr (std::is_floating_point_v<T>) {
if (sv == ".inf" || sv == ".Inf" || sv == ".INF") {
return std::numeric_limits<T>::infinity();
} else if (sv == "-.inf" || sv == "-.Inf" || sv == "-.INF") {
return -std::numeric_limits<T>::infinity();
} else if (sv == ".nan" || sv == ".NaN" || sv == ".NAN") {
return std::numeric_limits<T>::quiet_NaN();
}
try {
if constexpr (std::is_same_v<T, float>) {
return std::stof(sv.data());
} else {
return std::stod(sv.data());
}
} catch (...) {
return std::unexpected(ValueParseError{0UZ, "Invalid value for type"});
}
} else if constexpr (std::is_integral_v<T>) {
if (sv.contains(".")) {
// from_chars() accepts "123.456", but we reject it
return std::unexpected(ValueParseError{0UZ, "Invalid value for type"});
}

if constexpr (std::is_integral_v<T>) {
if (sv.contains(".")) {
// from_chars() accepts "123.456", but we reject it
auto parseWithBase = [](std::string_view s, int base) -> std::expected<T, ValueParseError> {
T value;
const auto [_, ec] = std::from_chars(s.begin(), s.end(), value, base);
if (ec != std::errc{}) {
return std::unexpected(ValueParseError{0UZ, "Invalid value for type"});
}

auto parseWithBase = [](std::string_view s, int base) -> std::expected<T, ValueParseError> {
T value;
const auto [_, ec] = std::from_chars(s.begin(), s.end(), value, base);
if (ec != std::errc{}) {
return std::unexpected(ValueParseError{0UZ, "Invalid value for type"});
}
return value;
};
if (sv.starts_with("0x")) {
return parseWithBase(sv.substr(2), 16);
} else if (sv.starts_with("0o")) {
return parseWithBase(sv.substr(2), 8);
} else if (sv.starts_with("0b")) {
return parseWithBase(sv.substr(2), 2);
}
return value;
};
if (sv.starts_with("0x")) {
return parseWithBase(sv.substr(2), 16);
} else if (sv.starts_with("0o")) {
return parseWithBase(sv.substr(2), 8);
} else if (sv.starts_with("0b")) {
return parseWithBase(sv.substr(2), 2);
}

T value;
const auto [_, ec] = std::from_chars(sv.begin(), sv.end(), value);
if (ec != std::errc{}) {
Expand Down
9 changes: 4 additions & 5 deletions core/test/qa_YamlPmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ void testYAML(std::string_view src, const pmtv::map_t expected) {
}

const boost::ut::suite YamlPmtTests = [] {
#pragma GCC diagnostic push
#ifndef __clang__
#pragma GCC diagnostic ignored "-Wuseless-cast" // we want explicit casts for testing
#endif
using namespace boost::ut;
using namespace pmtv;
using namespace std::string_literals;
Expand Down Expand Up @@ -581,9 +582,8 @@ complex: !!complex64 (foo, bar)
};

"Errors"_test = [] {
constexpr std::string_view src1 = R"(value: !!float64 string
)";
expect(eq(formatResult(yaml::deserialize(src1)), "Error in 1:18: Invalid value for type"sv));
expect(eq(formatResult(yaml::deserialize("value: !!float64 string")), "Error in 1:18: Invalid value for type"sv));
expect(eq(formatResult(yaml::deserialize("value: !!int8 128")), "Error in 1:15: Invalid value for type"sv));

constexpr std::string_view mapListMix = R"(
value:
Expand Down Expand Up @@ -677,7 +677,6 @@ value: 43
)";
expect(eq(formatResult(yaml::deserialize(multiple_documents)), "Error in 4:1: Parser limitation: Multiple documents not supported"sv));
};
#pragma GCC diagnostic pop // Reenable -Wuseless-cast
};

int main() { /* tests are statically executed */ }

0 comments on commit 8d787f2

Please sign in to comment.