Skip to content

Commit 06315bf

Browse files
committed
Fancier error messages upon static analysis check failure
commit_hash:f939fba86939275047d2eca49b11bec3d0ea3ce7
1 parent e62cc48 commit 06315bf

File tree

2 files changed

+15
-43
lines changed

2 files changed

+15
-43
lines changed

library/cpp/yt/logging/unittests/static_analysis_ut.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ TEST(TStaticAnalysisTest, ValidFormats)
2323
}
2424

2525
// Uncomment this test to see that we don't have false negatives!
26-
// TEST(TStaticAnalysisTest, InvalidFormats)
27-
// {
28-
// YT_LOG_INFO("Hello", 1);
29-
// YT_LOG_INFO("Hello %");
30-
// YT_LOG_INFO("Hello %false");
31-
// YT_LOG_INFO("Hello ", "World");
32-
// YT_LOG_INFO("Hello ", "(World: %v)", 42);
33-
// YT_LOG_INFO("Hello %lbov", 42); // There is no 'b' flag.
34-
// }
26+
TEST(TStaticAnalysisTest, InvalidFormats)
27+
{
28+
// YT_LOG_INFO("Hello", 1);
29+
// YT_LOG_INFO("Hello %");
30+
// YT_LOG_INFO("Hello %false");
31+
// YT_LOG_INFO("Hello ", "World");
32+
// YT_LOG_INFO("Hello ", "(World: %v)", 42);
33+
// YT_LOG_INFO("Hello %lbov", 42); // There is no 'b' flag.
34+
}
3535

3636
////////////////////////////////////////////////////////////////////////////////
3737

library/cpp/yt/string/format_analyser.h

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,6 @@ struct TFormatAnalyser
2424
}
2525

2626
private:
27-
// Non-constexpr function call will terminate compilation.
28-
// Purposefully undefined and non-constexpr/consteval
29-
template <class T>
30-
static void CrashCompilerNotFormattable(std::string_view /*msg*/)
31-
{ /*Suppress "internal linkage but undefined" warning*/ }
32-
static void CrashCompilerNotEnoughArguments(std::string_view /*msg*/)
33-
{ }
34-
35-
static void CrashCompilerTooManyArguments(std::string_view /*msg*/)
36-
{ }
37-
38-
static void CrashCompilerWrongTermination(std::string_view /*msg*/)
39-
{ }
40-
41-
static void CrashCompilerMissingTermination(std::string_view /*msg*/)
42-
{ }
43-
44-
static void CrashCompilerWrongFlagSpecifier(std::string_view /*msg*/)
45-
{ }
46-
47-
4827
static consteval bool Contains(std::string_view sv, char symbol)
4928
{
5029
return sv.find(symbol) != std::string_view::npos;
@@ -59,10 +38,6 @@ struct TFormatAnalyser
5938
template <class TArg>
6039
static consteval auto GetSpecifiers()
6140
{
62-
if constexpr (!CFormattable<TArg>) {
63-
CrashCompilerNotFormattable<TArg>("Your specialization of TFormatArg is broken");
64-
}
65-
6641
return TSpecifiers{
6742
.Conversion = std::string_view{
6843
std::data(TFormatArg<TArg>::ConversionSpecifiers),
@@ -103,8 +78,7 @@ struct TFormatAnalyser
10378
if (symbol == IntroductorySymbol) {
10479
if (currentMarkerStart + 1 != index) {
10580
// '%a% detected'
106-
CrashCompilerWrongTermination("You may not terminate flag sequence other than %% with \'%\' symbol");
107-
return;
81+
throw "You may not terminate flag sequence other than %% with \'%\' symbol";
10882
}
10983
// '%%' detected --- skip
11084
currentMarkerStart = -1;
@@ -113,9 +87,8 @@ struct TFormatAnalyser
11387

11488
// We are inside of marker.
11589
if (markerCount == std::ssize(markers)) {
116-
// To many markers
117-
CrashCompilerNotEnoughArguments("Number of arguments supplied to format is smaller than the number of flag sequences");
118-
return;
90+
// Too many markers
91+
throw "Number of arguments supplied to format is smaller than the number of flag sequences";
11992
}
12093

12194
if (Contains(specifiers[markerCount].Conversion, symbol)) {
@@ -130,20 +103,19 @@ struct TFormatAnalyser
130103
}
131104

132105
if (!Contains(specifiers[markerCount].Flags, symbol)) {
133-
CrashCompilerWrongFlagSpecifier("Symbol is not a valid flag specifier; See FlagSpecifiers");
106+
throw "Symbol is not a valid flag specifier; See FlagSpecifiers";
134107
}
135108
}
136109

137110
if (currentMarkerStart != -1) {
138111
// Runaway marker.
139-
CrashCompilerMissingTermination("Unterminated flag sequence detected; Use \'%%\' to type plain %");
112+
throw "Unterminated flag sequence detected; Use \'%%\' to type plain %";
140113
return;
141114
}
142115

143116
if (markerCount < std::ssize(markers)) {
144117
// Missing markers.
145-
CrashCompilerTooManyArguments("Number of arguments supplied to format is greater than the number of flag sequences");
146-
return;
118+
throw "Number of arguments supplied to format is greater than the number of flag sequences";
147119
}
148120

149121
// TODO(arkady-e1ppa): Consider per-type verification

0 commit comments

Comments
 (0)