Skip to content

[Support] Avoid warning about possibly uninitialized variable in format_provider #95704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions llvm/include/llvm/Support/FormatProviders.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ class HelperFunctions {
return Result;
}

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

if (Str.consume_front("x-"))
Style = HexPrintStyle::Lower;
else if (Str.consume_front("X-"))
Style = HexPrintStyle::Upper;
else if (Str.consume_front("x+") || Str.consume_front("x"))
Style = HexPrintStyle::PrefixLower;
else if (Str.consume_front("X+") || Str.consume_front("X"))
Style = HexPrintStyle::PrefixUpper;
return true;
return HexPrintStyle::Lower;
if (Str.consume_front("X-"))
return HexPrintStyle::Upper;
if (Str.consume_front("x+") || Str.consume_front("x"))
return HexPrintStyle::PrefixLower;
if (!Str.consume_front("X+"))
Str.consume_front("X");
return HexPrintStyle::PrefixUpper;
}

static size_t consumeNumHexDigits(StringRef &Str, HexPrintStyle Style,
Expand Down Expand Up @@ -132,11 +132,10 @@ struct format_provider<
private:
public:
static void format(const T &V, llvm::raw_ostream &Stream, StringRef Style) {
HexPrintStyle HS;
size_t Digits = 0;
if (consumeHexStyle(Style, HS)) {
Digits = consumeNumHexDigits(Style, HS, 0);
write_hex(Stream, V, HS, Digits);
if (std::optional<HexPrintStyle> HS = consumeHexStyle(Style)) {
Digits = consumeNumHexDigits(Style, *HS, 0);
write_hex(Stream, V, *HS, Digits);
return;
}

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