From 529a340ed02a5dd9f750985ff6d54d3bffa021a7 Mon Sep 17 00:00:00 2001 From: arkady-e1ppa Date: Wed, 16 Oct 2024 10:57:50 +0300 Subject: [PATCH] Fancier error messages upon static analysis check failure commit_hash:f939fba86939275047d2eca49b11bec3d0ea3ce7 --- .../logging/unittests/static_analysis_ut.cpp | 18 ++++----- library/cpp/yt/string/format_analyser.h | 40 +++---------------- 2 files changed, 15 insertions(+), 43 deletions(-) diff --git a/library/cpp/yt/logging/unittests/static_analysis_ut.cpp b/library/cpp/yt/logging/unittests/static_analysis_ut.cpp index 7fb4c0150..b9a01b334 100644 --- a/library/cpp/yt/logging/unittests/static_analysis_ut.cpp +++ b/library/cpp/yt/logging/unittests/static_analysis_ut.cpp @@ -23,15 +23,15 @@ TEST(TStaticAnalysisTest, ValidFormats) } // Uncomment this test to see that we don't have false negatives! -// TEST(TStaticAnalysisTest, InvalidFormats) -// { -// YT_LOG_INFO("Hello", 1); -// YT_LOG_INFO("Hello %"); -// YT_LOG_INFO("Hello %false"); -// YT_LOG_INFO("Hello ", "World"); -// YT_LOG_INFO("Hello ", "(World: %v)", 42); -// YT_LOG_INFO("Hello %lbov", 42); // There is no 'b' flag. -// } +TEST(TStaticAnalysisTest, InvalidFormats) +{ + // YT_LOG_INFO("Hello", 1); + // YT_LOG_INFO("Hello %"); + // YT_LOG_INFO("Hello %false"); + // YT_LOG_INFO("Hello ", "World"); + // YT_LOG_INFO("Hello ", "(World: %v)", 42); + // YT_LOG_INFO("Hello %lbov", 42); // There is no 'b' flag. +} //////////////////////////////////////////////////////////////////////////////// diff --git a/library/cpp/yt/string/format_analyser.h b/library/cpp/yt/string/format_analyser.h index e8287ea8f..20eee6058 100644 --- a/library/cpp/yt/string/format_analyser.h +++ b/library/cpp/yt/string/format_analyser.h @@ -24,27 +24,6 @@ struct TFormatAnalyser } private: - // Non-constexpr function call will terminate compilation. - // Purposefully undefined and non-constexpr/consteval - template - static void CrashCompilerNotFormattable(std::string_view /*msg*/) - { /*Suppress "internal linkage but undefined" warning*/ } - static void CrashCompilerNotEnoughArguments(std::string_view /*msg*/) - { } - - static void CrashCompilerTooManyArguments(std::string_view /*msg*/) - { } - - static void CrashCompilerWrongTermination(std::string_view /*msg*/) - { } - - static void CrashCompilerMissingTermination(std::string_view /*msg*/) - { } - - static void CrashCompilerWrongFlagSpecifier(std::string_view /*msg*/) - { } - - static consteval bool Contains(std::string_view sv, char symbol) { return sv.find(symbol) != std::string_view::npos; @@ -59,10 +38,6 @@ struct TFormatAnalyser template static consteval auto GetSpecifiers() { - if constexpr (!CFormattable) { - CrashCompilerNotFormattable("Your specialization of TFormatArg is broken"); - } - return TSpecifiers{ .Conversion = std::string_view{ std::data(TFormatArg::ConversionSpecifiers), @@ -103,8 +78,7 @@ struct TFormatAnalyser if (symbol == IntroductorySymbol) { if (currentMarkerStart + 1 != index) { // '%a% detected' - CrashCompilerWrongTermination("You may not terminate flag sequence other than %% with \'%\' symbol"); - return; + throw "You may not terminate flag sequence other than %% with \'%\' symbol"; } // '%%' detected --- skip currentMarkerStart = -1; @@ -113,9 +87,8 @@ struct TFormatAnalyser // We are inside of marker. if (markerCount == std::ssize(markers)) { - // To many markers - CrashCompilerNotEnoughArguments("Number of arguments supplied to format is smaller than the number of flag sequences"); - return; + // Too many markers + throw "Number of arguments supplied to format is smaller than the number of flag sequences"; } if (Contains(specifiers[markerCount].Conversion, symbol)) { @@ -130,20 +103,19 @@ struct TFormatAnalyser } if (!Contains(specifiers[markerCount].Flags, symbol)) { - CrashCompilerWrongFlagSpecifier("Symbol is not a valid flag specifier; See FlagSpecifiers"); + throw "Symbol is not a valid flag specifier; See FlagSpecifiers"; } } if (currentMarkerStart != -1) { // Runaway marker. - CrashCompilerMissingTermination("Unterminated flag sequence detected; Use \'%%\' to type plain %"); + throw "Unterminated flag sequence detected; Use \'%%\' to type plain %"; return; } if (markerCount < std::ssize(markers)) { // Missing markers. - CrashCompilerTooManyArguments("Number of arguments supplied to format is greater than the number of flag sequences"); - return; + throw "Number of arguments supplied to format is greater than the number of flag sequences"; } // TODO(arkady-e1ppa): Consider per-type verification