[llvm] ca5ba2e - [Support] Avoid warning about possibly uninitialized variable in format_provider (#95704)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 22 12:20:10 PDT 2024


Author: Sven Verdoolaege
Date: 2024-06-22T12:20:06-07:00
New Revision: ca5ba2e46445caf2c8063a53eb6351fb596190e8

URL: https://github.com/llvm/llvm-project/commit/ca5ba2e46445caf2c8063a53eb6351fb596190e8
DIFF: https://github.com/llvm/llvm-project/commit/ca5ba2e46445caf2c8063a53eb6351fb596190e8.diff

LOG: [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 at gmail.com>

Added: 
    

Modified: 
    llvm/include/llvm/Support/FormatProviders.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/FormatProviders.h b/llvm/include/llvm/Support/FormatProviders.h
index bf489e2bfa077..b7d2e2e45f71f 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 (std::optional<HexPrintStyle> 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 (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