summaryrefslogtreecommitdiff
path: root/llvm/lib/Support/FormatVariadic.cpp
diff options
context:
space:
mode:
authorVitaly Buka <vitalybuka@google.com>2024-09-12 11:01:01 -0700
committerVitaly Buka <vitalybuka@google.com>2024-09-12 11:01:01 -0700
commitc74308f2b67c73c4989a1301d5f800eb61310fdd (patch)
tree9b3dfe001546b221ea3a95d07f7ce4eb9edc8f89 /llvm/lib/Support/FormatVariadic.cpp
parent6711e8f505bb7786ffedd564f88e46eb71690eb1 (diff)
parent885ac29910a23db923292fe3fc09d0ec105186dc (diff)
[𝘀𝗽𝗿] changes introduced through rebaseusers/vitalybuka/spr/main.test
Created using spr 1.3.4 [skip ci]
Diffstat (limited to 'llvm/lib/Support/FormatVariadic.cpp')
-rw-r--r--llvm/lib/Support/FormatVariadic.cpp41
1 files changed, 29 insertions, 12 deletions
diff --git a/llvm/lib/Support/FormatVariadic.cpp b/llvm/lib/Support/FormatVariadic.cpp
index 905646619028..3240b1c0f4e4 100644
--- a/llvm/lib/Support/FormatVariadic.cpp
+++ b/llvm/lib/Support/FormatVariadic.cpp
@@ -63,16 +63,18 @@ static std::optional<ReplacementItem> parseReplacementItem(StringRef Spec) {
unsigned Align = 0;
AlignStyle Where = AlignStyle::Right;
StringRef Options;
- unsigned Index = 0;
+ unsigned Index = ~0U;
RepString = RepString.trim();
- if (RepString.consumeInteger(0, Index)) {
- assert(false && "Invalid replacement sequence index!");
- return std::nullopt;
- }
+
+ // If index is not specified, keep it ~0U to indicate unresolved index.
+ RepString.consumeInteger(0, Index);
RepString = RepString.trim();
+
if (RepString.consume_front(",")) {
- if (!consumeFieldLayout(RepString, Where, Align, Pad))
+ if (!consumeFieldLayout(RepString, Where, Align, Pad)) {
assert(false && "Invalid replacement field layout specification!");
+ return std::nullopt;
+ }
}
RepString = RepString.trim();
if (RepString.consume_front(":")) {
@@ -80,8 +82,10 @@ static std::optional<ReplacementItem> parseReplacementItem(StringRef Spec) {
RepString = StringRef();
}
RepString = RepString.trim();
- assert(RepString.empty() &&
- "Unexpected characters found in replacement string!");
+ if (!RepString.empty()) {
+ assert(0 && "Unexpected characters found in replacement string!");
+ return std::nullopt;
+ }
return ReplacementItem(Spec, Index, Align, Where, Pad, Options);
}
@@ -139,6 +143,7 @@ SmallVector<ReplacementItem, 2>
formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs,
bool Validate) {
SmallVector<ReplacementItem, 2> Replacements;
+ unsigned NextAutomaticIndex = 0;
#if ENABLE_VALIDATION
const StringRef SavedFmtStr = Fmt;
@@ -150,6 +155,9 @@ formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs,
std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
if (!I)
continue;
+ if (I->Index == ~0U)
+ I->Index = NextAutomaticIndex++;
+
Replacements.emplace_back(*I);
#if ENABLE_VALIDATION
if (I->Type == ReplacementType::Format)
@@ -175,9 +183,8 @@ formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs,
};
if (NumExpectedArgs != NumArgs) {
- errs() << formatv(
- "Expected {0} Args, but got {1} for format string '{2}'\n",
- NumExpectedArgs, NumArgs, SavedFmtStr);
+ errs() << formatv("Expected {} Args, but got {} for format string '{}'\n",
+ NumExpectedArgs, NumArgs, SavedFmtStr);
assert(0 && "Invalid formatv() call");
return getErrorReplacements("Unexpected number of arguments");
}
@@ -195,11 +202,21 @@ formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs,
if (Count != NumExpectedArgs) {
errs() << formatv(
- "Replacement field indices cannot have holes for format string '{0}'\n",
+ "Replacement field indices cannot have holes for format string '{}'\n",
SavedFmtStr);
assert(0 && "Invalid format string");
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) {
+ errs() << formatv(
+ "Cannot mix automatic and explicit indices for format string '{}'\n",
+ SavedFmtStr);
+ assert(0 && "Invalid format string");
+ return getErrorReplacements("Cannot mix automatic and explicit indices");
+ }
#endif // ENABLE_VALIDATION
return Replacements;
}