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

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 16 06:32:05 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Sven Verdoolaege (skimo-openhub)

<details>
<summary>Changes</summary>

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.


---
Full diff: https://github.com/llvm/llvm-project/pull/95704.diff


1 Files Affected:

- (modified) llvm/include/llvm/Support/FormatProviders.h (+15-15) 


``````````diff
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);
   }

``````````

</details>


https://github.com/llvm/llvm-project/pull/95704


More information about the llvm-commits mailing list