-
Notifications
You must be signed in to change notification settings - Fork 14k
[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
[Support] Avoid warning about possibly uninitialized variable in format_provider #95704
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be 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 If you have received no comments on your PR for a week, you can request a review 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. |
@llvm/pr-subscribers-llvm-support Author: Sven Verdoolaege (skimo-openhub) ChangesThe original implementation of HelperFunctions::consumeHexStyle always Change HelperFunctions::consumeHexStyle to return an optional Full diff: https://github.com/llvm/llvm-project/pull/95704.diff 1 Files Affected:
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)) { |
There was a problem hiding this comment.
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
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.
15de982
to
0c77bd6
Compare
On Tue, Jun 18, 2024 at 08:54:38AM -0700, David Blaikie wrote:
@dwblaikie approved this pull request.
Thanks.
I updated the branch as per
https://www.llvm.org/docs/GitHub.html#landing-your-change
but I can't do the actual merge because I don't have permission.
skimo
|
@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. |
On Fri, Jun 21, 2024 at 12:46:52PM -0700, Chandler Carruth wrote:
> @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.
Thanks for the quick response.
(I asked you because, as you are probably aware,
you are listed as the code owner of Support.)
skimo
|
@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 Please check whether problems have been caused by your change specifically, as 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. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…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>
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.