[llvm] [Support] Avoid warning about possibly uninitialized variable in format_provider (PR #95704)
Sven Verdoolaege via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 18 02:49:36 PDT 2024
https://github.com/skimo-openhub updated https://github.com/llvm/llvm-project/pull/95704
>From 97173efa457fd570ea1a9e3e2f335297298ee0b5 Mon Sep 17 00:00:00 2001
From: Sven Verdoolaege <sven.verdoolaege at gmail.com>
Date: Sun, 16 Jun 2024 14:34:12 +0200
Subject: [PATCH 1/2] [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.
---
llvm/include/llvm/Support/FormatProviders.h | 30 ++++++++++-----------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/llvm/include/llvm/Support/FormatProviders.h b/llvm/include/llvm/Support/FormatProviders.h
index bf489e2bfa077..9bb1dde784721 100644
--- a/llvm/include/llvm/Support/FormatProviders.h
+++ b/llvm/include/llvm/Support/FormatProviders.h
@@ -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,
@@ -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 (const auto &HS = consumeHexStyle(Style)) {
+ Digits = consumeNumHexDigits(Style, *HS, 0);
+ write_hex(Stream, V, *HS, Digits);
return;
}
@@ -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 (const auto &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);
}
>From 15de9829920db6f4be3975484c2566ca685312fa Mon Sep 17 00:00:00 2001
From: Sven Verdoolaege <sven.verdoolaege at gmail.com>
Date: Tue, 18 Jun 2024 11:49:12 +0200
Subject: [PATCH 2/2] fixup! [Support] Avoid warning about possibly
uninitialized variable in format_provider
---
llvm/include/llvm/Support/FormatProviders.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Support/FormatProviders.h b/llvm/include/llvm/Support/FormatProviders.h
index 9bb1dde784721..b7d2e2e45f71f 100644
--- a/llvm/include/llvm/Support/FormatProviders.h
+++ b/llvm/include/llvm/Support/FormatProviders.h
@@ -133,7 +133,7 @@ struct format_provider<
public:
static void format(const T &V, llvm::raw_ostream &Stream, StringRef Style) {
size_t Digits = 0;
- if (const auto &HS = consumeHexStyle(Style)) {
+ if (std::optional<HexPrintStyle> HS = consumeHexStyle(Style)) {
Digits = consumeNumHexDigits(Style, *HS, 0);
write_hex(Stream, V, *HS, Digits);
return;
@@ -181,7 +181,7 @@ struct format_provider<
public:
static void format(const T &V, llvm::raw_ostream &Stream, StringRef Style) {
HexPrintStyle HS = HexPrintStyle::PrefixUpper;
- if (const auto &consumed = consumeHexStyle(Style))
+ 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);
More information about the llvm-commits
mailing list