Skip to content

Commit 0c77bd6

Browse files
author
Sven Verdoolaege
committed
[Support] Avoid warning about possibly uninitialized variable in format_provider
The original implementation of HelperFunctions::consumeHexStyle always sets Style when it returns true, but this is difficult for a compiler to understand since it requires seeing that Str starts with either an "x" or an "X" when starts_with_insensitive("x") return true. In particular, g++ 12 warns that HS may be used uninitialized in the format_provider::format caller. Change HelperFunctions::consumeHexStyle to return an optional HexPrintStyle and to make the fact that Str necessarily starts with an "X" when all other cases do not apply more explicit. This helps both the compiler and the human reader of the code.
1 parent fcee033 commit 0c77bd6

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

llvm/include/llvm/Support/FormatProviders.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,19 @@ class HelperFunctions {
7676
return Result;
7777
}
7878

79-
static bool consumeHexStyle(StringRef &Str, HexPrintStyle &Style) {
79+
static std::optional<HexPrintStyle> consumeHexStyle(StringRef &Str) {
8080
if (!Str.starts_with_insensitive("x"))
81-
return false;
81+
return std::nullopt;
8282

8383
if (Str.consume_front("x-"))
84-
Style = HexPrintStyle::Lower;
85-
else if (Str.consume_front("X-"))
86-
Style = HexPrintStyle::Upper;
87-
else if (Str.consume_front("x+") || Str.consume_front("x"))
88-
Style = HexPrintStyle::PrefixLower;
89-
else if (Str.consume_front("X+") || Str.consume_front("X"))
90-
Style = HexPrintStyle::PrefixUpper;
91-
return true;
84+
return HexPrintStyle::Lower;
85+
if (Str.consume_front("X-"))
86+
return HexPrintStyle::Upper;
87+
if (Str.consume_front("x+") || Str.consume_front("x"))
88+
return HexPrintStyle::PrefixLower;
89+
if (!Str.consume_front("X+"))
90+
Str.consume_front("X");
91+
return HexPrintStyle::PrefixUpper;
9292
}
9393

9494
static size_t consumeNumHexDigits(StringRef &Str, HexPrintStyle Style,
@@ -132,11 +132,10 @@ struct format_provider<
132132
private:
133133
public:
134134
static void format(const T &V, llvm::raw_ostream &Stream, StringRef Style) {
135-
HexPrintStyle HS;
136135
size_t Digits = 0;
137-
if (consumeHexStyle(Style, HS)) {
138-
Digits = consumeNumHexDigits(Style, HS, 0);
139-
write_hex(Stream, V, HS, Digits);
136+
if (std::optional<HexPrintStyle> HS = consumeHexStyle(Style)) {
137+
Digits = consumeNumHexDigits(Style, *HS, 0);
138+
write_hex(Stream, V, *HS, Digits);
140139
return;
141140
}
142141

@@ -182,7 +181,8 @@ struct format_provider<
182181
public:
183182
static void format(const T &V, llvm::raw_ostream &Stream, StringRef Style) {
184183
HexPrintStyle HS = HexPrintStyle::PrefixUpper;
185-
consumeHexStyle(Style, HS);
184+
if (std::optional<HexPrintStyle> consumed = consumeHexStyle(Style))
185+
HS = *consumed;
186186
size_t Digits = consumeNumHexDigits(Style, HS, sizeof(void *) * 2);
187187
write_hex(Stream, reinterpret_cast<std::uintptr_t>(V), HS, Digits);
188188
}

0 commit comments

Comments
 (0)