diff options
author | Sven Verdoolaege <sven@cerebras.net> | 2024-06-22 12:20:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-22 12:20:06 -0700 |
commit | ca5ba2e46445caf2c8063a53eb6351fb596190e8 (patch) | |
tree | 1e2b0f1564e1007450c7eb75d366d7b580ad5037 | |
parent | 6621505a1eb5428b13d2d29f377050624ef5531c (diff) | |
download | llvm-ca5ba2e46445caf2c8063a53eb6351fb596190e8.zip llvm-ca5ba2e46445caf2c8063a53eb6351fb596190e8.tar.gz llvm-ca5ba2e46445caf2c8063a53eb6351fb596190e8.tar.bz2 |
[Support] Avoid warning about possibly uninitialized variable in format_provider (#95704)
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.
Co-authored-by: Sven Verdoolaege <sven.verdoolaege@gmail.com>
-rw-r--r-- | llvm/include/llvm/Support/FormatProviders.h | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/llvm/include/llvm/Support/FormatProviders.h b/llvm/include/llvm/Support/FormatProviders.h index bf489e2..b7d2e2e 100644 --- a/llvm/include/llvm/Support/FormatProviders.h +++ b/llvm/include/llvm/Support/FormatProviders.h @@ -76,19 +76,19 @@ protected: 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, @@ -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; } @@ -182,7 +181,8 @@ private: 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); } |