From 8d787f217625a97292ab40183372d916c723da2d Mon Sep 17 00:00:00 2001 From: Frank Osterfeld Date: Fri, 22 Nov 2024 20:13:05 +0100 Subject: [PATCH] Fixes for clang Signed-off-by: Frank Osterfeld --- core/include/gnuradio-4.0/YamlPmt.hpp | 60 +++++++++++++++------------ core/test/qa_YamlPmt.cpp | 9 ++-- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/core/include/gnuradio-4.0/YamlPmt.hpp b/core/include/gnuradio-4.0/YamlPmt.hpp index 472245251..d5317fa66 100644 --- a/core/include/gnuradio-4.0/YamlPmt.hpp +++ b/core/include/gnuradio-4.0/YamlPmt.hpp @@ -462,39 +462,45 @@ std::expected parseAs(std::string_view sv) { return false; } return std::unexpected(ValueParseError{0UZ, "Invalid value for type"}); - } else if constexpr (std::is_arithmetic_v) { - if constexpr (std::is_floating_point_v) { - if (sv == ".inf" || sv == ".Inf" || sv == ".INF") { - return std::numeric_limits::infinity(); - } else if (sv == "-.inf" || sv == "-.Inf" || sv == "-.INF") { - return -std::numeric_limits::infinity(); - } else if (sv == ".nan" || sv == ".NaN" || sv == ".NAN") { - return std::numeric_limits::quiet_NaN(); + } else if constexpr (std::is_floating_point_v) { + if (sv == ".inf" || sv == ".Inf" || sv == ".INF") { + return std::numeric_limits::infinity(); + } else if (sv == "-.inf" || sv == "-.Inf" || sv == "-.INF") { + return -std::numeric_limits::infinity(); + } else if (sv == ".nan" || sv == ".NaN" || sv == ".NAN") { + return std::numeric_limits::quiet_NaN(); + } + try { + if constexpr (std::is_same_v) { + 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) { + 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) { - if (sv.contains(".")) { - // from_chars() accepts "123.456", but we reject it + auto parseWithBase = [](std::string_view s, int base) -> std::expected { + 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 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{}) { diff --git a/core/test/qa_YamlPmt.cpp b/core/test/qa_YamlPmt.cpp index 521f6e5fa..d67e3ebdd 100644 --- a/core/test/qa_YamlPmt.cpp +++ b/core/test/qa_YamlPmt.cpp @@ -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; @@ -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: @@ -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 */ }