Skip to content

[Support] Fix bugs in formatv automatic index assignment #108384

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
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions llvm/lib/Support/FormatVariadic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,25 @@ formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs,
#if ENABLE_VALIDATION
const StringRef SavedFmtStr = Fmt;
unsigned NumExpectedArgs = 0;
bool HasExplicitIndex = false;
#endif

while (!Fmt.empty()) {
std::optional<ReplacementItem> I;
std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
if (!I)
continue;
if (I->Index == ~0U)
I->Index = NextAutomaticIndex++;

Replacements.emplace_back(*I);
if (I->Type == ReplacementType::Format) {
if (I->Index == ~0U)
I->Index = NextAutomaticIndex++;
#if ENABLE_VALIDATION
if (I->Type == ReplacementType::Format)
else
HasExplicitIndex = true;
NumExpectedArgs = std::max(NumExpectedArgs, I->Index + 1);
#endif
}

Replacements.emplace_back(*I);
}

#if ENABLE_VALIDATION
Expand Down Expand Up @@ -208,9 +212,8 @@ formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs,
return getErrorReplacements("Replacement indices have holes");
}

// If we had automatic numbering of replacement indices, verify that all
// indices used automatic numbering.
if (NextAutomaticIndex != 0 && NextAutomaticIndex != Count) {
// Fail validation if we see both automatic index and explicit index.
if (NextAutomaticIndex != 0 && HasExplicitIndex) {
errs() << formatv(
"Cannot mix automatic and explicit indices for format string '{}'\n",
SavedFmtStr);
Expand Down
12 changes: 12 additions & 0 deletions llvm/unittests/Support/FormatVariadicTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ TEST(FormatVariadicTest, AutomaticIndices) {
EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
EXPECT_EQ(0u, Replacements[0].Index);

Replacements = parseFormatString("{}bar{}");
ASSERT_EQ(3u, Replacements.size());
EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
EXPECT_EQ(0u, Replacements[0].Index);
EXPECT_EQ(ReplacementType::Literal, Replacements[1].Type);
EXPECT_EQ("bar", Replacements[1].Spec);
EXPECT_EQ(ReplacementType::Format, Replacements[2].Type);
EXPECT_EQ(1u, Replacements[2].Index);

Replacements = parseFormatString("{}{}");
ASSERT_EQ(2u, Replacements.size());
EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
Expand Down Expand Up @@ -760,6 +769,8 @@ TEST(FormatVariadicTest, Validate) {
"Replacement field indices cannot have holes");
EXPECT_DEATH(formatv("{}{1}", 0, 1).str(),
"Cannot mix automatic and explicit indices");
EXPECT_DEATH(formatv("{}{0}{}", 0, 1).str(),
"Cannot mix automatic and explicit indices");
#else // GTEST_HAS_DEATH_TEST
GTEST_SKIP() << "No support for EXPECT_DEATH";
#endif // GTEST_HAS_DEATH_TEST
Expand All @@ -768,6 +779,7 @@ TEST(FormatVariadicTest, Validate) {
EXPECT_EQ(formatv("{0}", 1, 2).str(), "1");
EXPECT_EQ(formatv("{0} {2}", 1, 2, 3).str(), "1 3");
EXPECT_EQ(formatv("{}{1}", 0, 1).str(), "01");
EXPECT_EQ(formatv("{}{0}{}", 0, 1).str(), "001");
#endif // NDEBUG
}

Expand Down
Loading