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

Conversation

skimo-openhub
Copy link
Contributor

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.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jun 16, 2024

@llvm/pr-subscribers-llvm-support

Author: Sven Verdoolaege (skimo-openhub)

Changes

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

if (consumeHexStyle(Style, HS)) {
Digits = consumeNumHexDigits(Style, HS, 0);
write_hex(Stream, V, HS, Digits);
if (const auto &HS = consumeHexStyle(Style)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use non-ref (best to avoid reference lifetime extension) (and probably non-const, once it's local - there aren't many uses of local const in LLVM) here and below. Maybe name the type rather than using auto

@dwblaikie
Copy link
Collaborator

In general, I think we should disable any compiler warnings that produce would otherwise encourage us to contort our code in ways that don't make it more readable - and I worry this warning will do that at some point, but this particular instance I don't mind/seems nice enough.

…at_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.
@skimo-openhub skimo-openhub force-pushed the format_provider_uninitialized branch from 15de982 to 0c77bd6 Compare June 18, 2024 16:06
@skimo-openhub
Copy link
Contributor Author

skimo-openhub commented Jun 18, 2024 via email

@skimo-openhub
Copy link
Contributor Author

@chandlerc Could you approve the workflow for this PR?

@chandlerc
Copy link
Member

@chandlerc Could you approve the workflow for this PR?

I'm not that up-to-date with LLVM these days, probably better for @dwblaikie or someone else more active to look at this.

@skimo-openhub
Copy link
Contributor Author

skimo-openhub commented Jun 21, 2024 via email

@dwblaikie dwblaikie merged commit ca5ba2e into llvm:main Jun 22, 2024
6 checks passed
Copy link

@skimo-openhub Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@skimo-openhub skimo-openhub deleted the format_provider_uninitialized branch June 25, 2024 09:35
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
…at_provider (llvm#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants