[llvm] 54aadcd - [DebugInfo] Convert format() to formatv() in DWARFVerifier (#179194)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 2 07:24:11 PST 2026
Author: Konrad Kleine
Date: 2026-02-02T16:24:05+01:00
New Revision: 54aadcd5d150fdcf9c09f3ea3b0526309e522477
URL: https://github.com/llvm/llvm-project/commit/54aadcd5d150fdcf9c09f3ea3b0526309e522477
DIFF: https://github.com/llvm/llvm-project/commit/54aadcd5d150fdcf9c09f3ea3b0526309e522477.diff
LOG: [DebugInfo] Convert format() to formatv() in DWARFVerifier (#179194)
Replace all of calls of `format()` with `formatv()` in
`DWARFVerifier.cpp`. Also use `formatv()` when strings were concatenated
with `+` or streamed with `<<`.
See
[llvm/include/llvm/Support/FormatProviders.h](https://github.com/llvm/llvm-project/blob/2f3935bcee6eaf7df8c85a21b7c0fbef967316b5/llvm/include/llvm/Support/FormatProviders.h#L96-L117)
for the hexadecimal grammar. e.g. `x+` to print the `0x` prefix or `x-`
to not print it.
My motivation is to:
1. make it easier to read the format strings and
2. have less `formatv()` calls when it was used on arguments to the
format string before.
Relates to #35980
Added:
Modified:
llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
Removed:
################################################################################
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp b/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
index 3a92140ede9d0..05191f68e0ddd 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
@@ -182,8 +182,8 @@ bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
bool HeaderShown = false;
auto ShowHeaderOnce = [&]() {
if (!HeaderShown) {
- error() << format("Units[%d] - start offset: 0x%08" PRIx64 " \n",
- UnitIndex, OffsetStart);
+ error() << formatv("Units[{0}] - start offset: {1:x+8}\n", UnitIndex,
+ OffsetStart);
HeaderShown = true;
}
};
@@ -272,8 +272,8 @@ unsigned DWARFVerifier::verifyUnitContents(DWARFUnit &Unit,
if (Die.hasChildren()) {
if (Die.getFirstChild().isValid() &&
Die.getFirstChild().getTag() == DW_TAG_null) {
- warn() << dwarf::TagString(Die.getTag())
- << " has DW_CHILDREN_yes but DIE has no children: ";
+ warn() << formatv("{0} has DW_CHILDREN_yes but DIE has no children: ",
+ dwarf::TagString(Die.getTag()));
Die.dump(OS);
}
}
@@ -292,8 +292,8 @@ unsigned DWARFVerifier::verifyUnitContents(DWARFUnit &Unit,
if (!dwarf::isUnitType(Die.getTag())) {
ErrorCategory.Report("Compilation unit root DIE is not a unit DIE", [&]() {
- error() << "Compilation unit root DIE is not a unit DIE: "
- << dwarf::TagString(Die.getTag()) << ".\n";
+ error() << formatv("Compilation unit root DIE is not a unit DIE: {0}.\n",
+ dwarf::TagString(Die.getTag()));
});
NumUnitErrors++;
}
@@ -301,9 +301,9 @@ unsigned DWARFVerifier::verifyUnitContents(DWARFUnit &Unit,
uint8_t UnitType = Unit.getUnitType();
if (!DWARFUnit::isMatchingUnitTypeAndTag(UnitType, Die.getTag())) {
ErrorCategory.Report("Mismatched unit type", [&]() {
- error() << "Compilation unit type (" << dwarf::UnitTypeString(UnitType)
- << ") and root DIE (" << dwarf::TagString(Die.getTag())
- << ") do not match.\n";
+ error() << formatv(
+ "Compilation unit type ({0}) and root DIE ({1}) do not match.\n",
+ dwarf::UnitTypeString(UnitType), dwarf::TagString(Die.getTag()));
});
NumUnitErrors++;
}
@@ -389,8 +389,9 @@ unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
if (!Result.second) {
ErrorCategory.Report(
"Abbreviation declartion contains multiple attributes", [&]() {
- error() << "Abbreviation declaration contains multiple "
- << AttributeString(Attribute.Attr) << " attributes.\n";
+ error() << formatv("Abbreviation declaration contains multiple "
+ "{0} attributes.\n",
+ AttributeString(Attribute.Attr));
AbbrDecl.dump(OS);
});
++NumErrors;
@@ -418,10 +419,11 @@ unsigned DWARFVerifier::verifyUnits(const DWARFUnitVector &Units) {
ReferenceMap CrossUnitReferences;
unsigned Index = 1;
+
for (const auto &Unit : Units) {
- OS << "Verifying unit: " << Index << " / " << Units.getNumUnits();
+ OS << formatv("Verifying unit: {0} / {1}", Index, Units.getNumUnits());
if (const char* Name = Unit->getUnitDIE(true).getShortName())
- OS << ", \"" << Name << '\"';
+ OS << formatv(", \"{0}\"", Name);
OS << '\n';
OS.flush();
ReferenceMap UnitLocalReferences;
@@ -600,7 +602,7 @@ unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
if (!Range.valid()) {
++NumErrors;
ErrorCategory.Report("Invalid address range", [&]() {
- error() << "Invalid address range " << Range << "\n";
+ error() << formatv("Invalid address range {0}\n", Range);
DumpDieAfterError = true;
});
continue;
@@ -615,8 +617,9 @@ unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
if (auto PrevRange = RI.insert(Range)) {
++NumErrors;
ErrorCategory.Report("DIE has overlapping DW_AT_ranges", [&]() {
- error() << "DIE has overlapping ranges in DW_AT_ranges attribute: "
- << *PrevRange << " and " << Range << '\n';
+ error() << formatv("DIE has overlapping ranges in DW_AT_ranges "
+ "attribute: {0} and {1}\n",
+ *PrevRange, Range);
DumpDieAfterError = true;
});
}
@@ -693,7 +696,7 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
auto ReportError = [&](StringRef category, const Twine &TitleMsg) {
++NumErrors;
ErrorCategory.Report(category, [&]() {
- error() << TitleMsg << '\n';
+ error() << formatv("{0}\n", TitleMsg);
dump(Die) << '\n';
});
};
@@ -712,11 +715,12 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
if (U->isDWOUnit() && RangeSection.Data.empty())
break;
if (*SectionOffset >= RangeSection.Data.size())
- ReportError("DW_AT_ranges offset out of bounds",
- "DW_AT_ranges offset is beyond " +
- StringRef(DwarfVersion < 5 ? ".debug_ranges"
- : ".debug_rnglists") +
- " bounds: " + llvm::formatv("{0:x8}", *SectionOffset));
+ ReportError(
+ "DW_AT_ranges offset out of bounds",
+ llvm::formatv("DW_AT_ranges offset is beyond {0} bounds: {1:x8}",
+ StringRef(DwarfVersion < 5 ? ".debug_ranges"
+ : ".debug_rnglists"),
+ *SectionOffset));
break;
}
ReportError("Invalid DW_AT_ranges encoding",
@@ -726,9 +730,11 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
// Make sure the offset in the DW_AT_stmt_list attribute is valid.
if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
if (*SectionOffset >= U->getLineSection().Data.size())
- ReportError("DW_AT_stmt_list offset out of bounds",
- "DW_AT_stmt_list offset is beyond .debug_line bounds: " +
- llvm::formatv("{0:x8}", *SectionOffset));
+ ReportError(
+ "DW_AT_stmt_list offset out of bounds",
+ llvm::formatv(
+ "DW_AT_stmt_list offset is beyond .debug_line bounds: {0:x8}",
+ *SectionOffset));
break;
}
ReportError("Invalid DW_AT_stmt_list encoding",
@@ -782,11 +788,10 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
if (DieTag == DW_TAG_GNU_call_site && RefTag == DW_TAG_subprogram)
break;
ReportError("Incompatible DW_AT_abstract_origin tag reference",
- "DIE with tag " + TagString(DieTag) + " has " +
- AttributeString(Attr) +
- " that points to DIE with "
- "incompatible tag " +
- TagString(RefTag));
+ formatv("DIE with tag {0} has {1} that points to DIE with "
+ "incompatible tag {2}",
+ TagString(DieTag), AttributeString(Attr),
+ TagString(RefTag)));
}
break;
}
@@ -794,8 +799,8 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
DWARFDie TypeDie = Die.getAttributeValueAsReferencedDie(DW_AT_type);
if (TypeDie && !isType(TypeDie.getTag())) {
ReportError("Incompatible DW_AT_type attribute tag",
- "DIE has " + AttributeString(Attr) +
- " with incompatible tag " + TagString(TypeDie.getTag()));
+ formatv("DIE has {0} with incompatible tag {1}",
+ AttributeString(Attr), TagString(TypeDie.getTag())));
}
break;
}
@@ -811,32 +816,30 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
if (std::optional<uint64_t> LastFileIdx =
LT->getLastValidFileIndex()) {
ReportError("Invalid file index in DW_AT_decl_file",
- "DIE has " + AttributeString(Attr) +
- " with an invalid file index " +
- llvm::formatv("{0}", *FileIdx) +
- " (valid values are [" +
- (IsZeroIndexed ? "0-" : "1-") +
- llvm::formatv("{0}", *LastFileIdx) + "])");
+ llvm::formatv("DIE has {0} with an invalid file index "
+ "{1} (valid values are [{2}-{3}])",
+ AttributeString(Attr), *FileIdx,
+ (IsZeroIndexed ? "0" : "1"),
+ *LastFileIdx));
} else {
- ReportError("Invalid file index in DW_AT_decl_file",
- "DIE has " + AttributeString(Attr) +
- " with an invalid file index " +
- llvm::formatv("{0}", *FileIdx) +
- " (the file table in the prologue is empty)");
+ ReportError(
+ "Invalid file index in DW_AT_decl_file",
+ llvm::formatv("DIE has {0} with an invalid file index {1} (the "
+ "file table in the prologue is empty)",
+ AttributeString(Attr), *FileIdx));
}
}
} else {
ReportError(
"File index in DW_AT_decl_file reference CU with no line table",
- "DIE has " + AttributeString(Attr) +
- " that references a file with index " +
- llvm::formatv("{0}", *FileIdx) +
- " and the compile unit has no line table");
+ llvm::formatv("DIE has {0} that references a file with index {1} "
+ "and the compile unit has no line table",
+ AttributeString(Attr), *FileIdx));
}
} else {
ReportError("Invalid encoding in DW_AT_decl_file",
- "DIE has " + AttributeString(Attr) +
- " with invalid encoding");
+ llvm::formatv("DIE has {0} with invalid encoding",
+ AttributeString(Attr)));
}
break;
}
@@ -846,7 +849,7 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
ReportError(
Attr == DW_AT_call_line ? "Invalid file index in DW_AT_decl_line"
: "Invalid file index in DW_AT_call_line",
- "DIE has " + AttributeString(Attr) + " with invalid encoding");
+ formatv("DIE has {0} with invalid encoding", AttributeString(Attr)));
}
break;
}
@@ -907,11 +910,9 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
// Check if the offset is within the bounds of this specific line table
if (*SectionOffset < SequencesStart || *SectionOffset >= LineTableEnd) {
ReportError("DW_AT_LLVM_stmt_sequence offset out of line table bounds",
- "DW_AT_LLVM_stmt_sequence offset " +
- llvm::formatv("{0:x8}", *SectionOffset) +
- " is not within the line table bounds [" +
- llvm::formatv("{0:x8}", SequencesStart) + ", " +
- llvm::formatv("{0:x8}", LineTableEnd) + ")");
+ llvm::formatv("DW_AT_LLVM_stmt_sequence offset {0:x8} is not "
+ "within the line table bounds [{1:x8}, {2:x8})",
+ *SectionOffset, SequencesStart, LineTableEnd));
break;
}
@@ -924,9 +925,9 @@ unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
if (It == LineTable->Sequences.end())
ReportError(
"Invalid DW_AT_LLVM_stmt_sequence offset",
- "DW_AT_LLVM_stmt_sequence offset " +
- llvm::formatv("{0:x8}", *SectionOffset) +
- " does not point to a valid sequence offset in the line table");
+ llvm::formatv("DW_AT_LLVM_stmt_sequence offset {0:x8} does not point "
+ "to a valid sequence offset in the line table",
+ *SectionOffset));
break;
}
default:
@@ -957,10 +958,9 @@ unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
if (CUOffset >= CUSize) {
++NumErrors;
ErrorCategory.Report("Invalid CU offset", [&]() {
- error() << FormEncodingString(Form) << " CU offset "
- << format("0x%08" PRIx64, CUOffset)
- << " is invalid (must be less than CU size of "
- << format("0x%08" PRIx64, CUSize) << "):\n";
+ error() << formatv("{0} CU offset {1:x+8} is invalid (must be less "
+ "than CU size of {2:x+8}):\n",
+ FormEncodingString(Form), CUOffset, CUSize);
Die.dump(OS, 0, DumpOpts);
dump(Die) << '\n';
});
@@ -1005,7 +1005,7 @@ unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
++NumErrors;
std::string ErrMsg = toString(std::move(E));
ErrorCategory.Report("Invalid DW_FORM attribute", [&]() {
- error() << ErrMsg << ":\n";
+ error() << formatv("{0}:\n", ErrMsg);
dump(Die) << '\n';
});
}
@@ -1032,8 +1032,9 @@ unsigned DWARFVerifier::verifyDebugInfoReferences(
continue;
++NumErrors;
ErrorCategory.Report("Invalid DIE reference", [&]() {
- error() << "invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
- << ". Offset is in between DIEs:\n";
+ error() << formatv(
+ "invalid DIE reference {0:x+8}. Offset is in between DIEs:\n",
+ Pair.first);
for (auto Offset : Pair.second)
dump(GetDIEForOffset(Offset)) << '\n';
OS << "\n";
@@ -1058,8 +1059,9 @@ void DWARFVerifier::verifyDebugLineStmtOffsets() {
if (!LineTable) {
++NumDebugLineErrors;
ErrorCategory.Report("Unparsable .debug_line entry", [&]() {
- error() << ".debug_line[" << format("0x%08" PRIx64, LineTableOffset)
- << "] was not able to be parsed for CU:\n";
+ error() << formatv(
+ ".debug_line[{0:x+8}] was not able to be parsed for CU:\n",
+ LineTableOffset);
dump(Die) << '\n';
});
continue;
@@ -1076,10 +1078,9 @@ void DWARFVerifier::verifyDebugLineStmtOffsets() {
++NumDebugLineErrors;
const auto &OldDie = Iter->second;
ErrorCategory.Report("Identical DW_AT_stmt_list section offset", [&]() {
- error() << "two compile unit DIEs, "
- << format("0x%08" PRIx64, OldDie.getOffset()) << " and "
- << format("0x%08" PRIx64, Die.getOffset())
- << ", have the same DW_AT_stmt_list section offset:\n";
+ error() << formatv("two compile unit DIEs, {0:x+8} and {1:x+8}, have "
+ "the same DW_AT_stmt_list section offset:\n",
+ OldDie.getOffset(), Die.getOffset());
dump(OldDie);
dump(Die) << '\n';
});
@@ -1110,12 +1111,10 @@ void DWARFVerifier::verifyDebugLineRows() {
ErrorCategory.Report(
"Invalid index in .debug_line->prologue.file_names->dir_idx",
[&]() {
- error() << ".debug_line["
- << format("0x%08" PRIx64,
- *toSectionOffset(Die.find(DW_AT_stmt_list)))
- << "].prologue.file_names[" << FileIndex
- << "].dir_idx contains an invalid index: "
- << FileName.DirIdx << "\n";
+ error() << formatv(".debug_line[{0:x+8}].prologue.file_names[{1}]"
+ ".dir_idx contains an invalid index: {2}\n",
+ *toSectionOffset(Die.find(DW_AT_stmt_list)),
+ FileIndex, FileName.DirIdx);
});
}
@@ -1128,11 +1127,10 @@ void DWARFVerifier::verifyDebugLineRows() {
(void)HasFullPath;
auto [It, Inserted] = FullPathMap.try_emplace(FullPath, FileIndex);
if (!Inserted && It->second != FileIndex && DumpOpts.Verbose) {
- warn() << ".debug_line["
- << format("0x%08" PRIx64,
- *toSectionOffset(Die.find(DW_AT_stmt_list)))
- << "].prologue.file_names[" << FileIndex
- << "] is a duplicate of file_names[" << It->second << "]\n";
+ warn() << formatv(".debug_line[{0:x+8}].prologue.file_names[{1}] is a "
+ "duplicate of file_names[{2}]\n",
+ *toSectionOffset(Die.find(DW_AT_stmt_list)),
+ FileIndex, It->second);
}
FileIndex++;
@@ -1152,11 +1150,10 @@ void DWARFVerifier::verifyDebugLineRows() {
++NumDebugLineErrors;
ErrorCategory.Report(
"decreasing address between debug_line rows", [&]() {
- error() << ".debug_line["
- << format("0x%08" PRIx64,
- *toSectionOffset(Die.find(DW_AT_stmt_list)))
- << "] row[" << RowIndex
- << "] decreases in address from previous row:\n";
+ error() << formatv(".debug_line[{0:x+8}] row[{1}] decreases in "
+ "address from previous row:\n",
+ *toSectionOffset(Die.find(DW_AT_stmt_list)),
+ RowIndex);
DWARFDebugLine::Row::dumpTableHeader(OS, 0);
if (RowIndex > 0)
@@ -1169,13 +1166,12 @@ void DWARFVerifier::verifyDebugLineRows() {
if (!LineTable->hasFileAtIndex(Row.File)) {
++NumDebugLineErrors;
ErrorCategory.Report("Invalid file index in debug_line", [&]() {
- error() << ".debug_line["
- << format("0x%08" PRIx64,
- *toSectionOffset(Die.find(DW_AT_stmt_list)))
- << "][" << RowIndex << "] has invalid file index " << Row.File
- << " (valid values are [" << MinFileIndex << ','
- << LineTable->Prologue.FileNames.size()
- << (isDWARF5 ? ")" : "]") << "):\n";
+ error() << formatv(".debug_line[{0:x+8}][{1}] has invalid file index "
+ "{2} (valid values are [{3},{4}{5}):\n",
+ *toSectionOffset(Die.find(DW_AT_stmt_list)),
+ RowIndex, Row.File, MinFileIndex,
+ LineTable->Prologue.FileNames.size(),
+ (isDWARF5 ? ")" : "]"));
DWARFDebugLine::Row::dumpTableHeader(OS, 0);
Row.dump(OS);
OS << '\n';
@@ -1247,8 +1243,8 @@ void DWARFVerifier::verifyAppleAccelTable(const DWARFSection *AccelSection,
uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
ErrorCategory.Report("Invalid hash index", [&]() {
- error() << format("Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
- HashIdx);
+ error() << formatv("Bucket[{0}] has invalid hash index: {1}.\n",
+ BucketIdx, HashIdx);
});
}
}
@@ -1274,9 +1270,8 @@ void DWARFVerifier::verifyAppleAccelTable(const DWARFSection *AccelSection,
if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
sizeof(uint64_t))) {
ErrorCategory.Report("Invalid HashData offset", [&]() {
- error() << format("Hash[%d] has invalid HashData offset: "
- "0x%08" PRIx64 ".\n",
- HashIdx, HashDataOffset);
+ error() << formatv("Hash[{0}] has invalid HashData offset: {1:x+8}.\n",
+ HashIdx, HashDataOffset);
});
}
@@ -1301,21 +1296,21 @@ void DWARFVerifier::verifyAppleAccelTable(const DWARFSection *AccelSection,
Name = "<NULL>";
ErrorCategory.Report("Invalid DIE offset", [&]() {
- error() << format(
- "%s Bucket[%d] Hash[%d] = 0x%08x "
- "Str[%u] = 0x%08" PRIx64 " DIE[%d] = 0x%08" PRIx64 " "
- "is not a valid DIE offset for \"%s\".\n",
- SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
- HashDataIdx, Offset, Name);
+ error() << formatv("{0} Bucket[{1}] Hash[{2}] = {3:x+8} "
+ "Str[{4}] = {5:x+8} DIE[{6}] = {7:x+8} "
+ "is not a valid DIE offset for \"{8}\".\n",
+ SectionName, BucketIdx, HashIdx, Hash,
+ StringCount, StrpOffset, HashDataIdx, Offset,
+ Name);
});
continue;
}
if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
ErrorCategory.Report("Mismatched Tag in accellerator table", [&]() {
- error() << "Tag " << dwarf::TagString(Tag)
- << " in accelerator table does not match Tag "
- << dwarf::TagString(Die.getTag()) << " of DIE["
- << HashDataIdx << "].\n";
+ error() << formatv("Tag {0} in accelerator table does not match "
+ "Tag {1} of DIE[{2}].\n",
+ dwarf::TagString(Tag),
+ dwarf::TagString(Die.getTag()), HashDataIdx);
});
}
}
@@ -2317,7 +2312,7 @@ bool DWARFVerifier::verifyDebugStrOffsets(
if (Error E = C.takeError()) {
std::string Msg = toString(std::move(E));
ErrorCategory.Report("String offset error", [&]() {
- error() << SectionName << ": " << Msg << '\n';
+ error() << formatv("{0}: {1}\n", SectionName, Msg);
return false;
});
}
@@ -2364,7 +2359,7 @@ void DWARFVerifier::summarize() {
if (DumpOpts.ShowAggregateErrors && ErrorCategory.GetNumCategories()) {
error() << "Aggregated error counts:\n";
ErrorCategory.EnumerateResults([&](StringRef s, unsigned count) {
- error() << s << " occurred " << count << " time(s).\n";
+ error() << formatv("{0} occurred {1} time(s).\n", s, count);
});
}
if (!DumpOpts.JsonErrSummaryFile.empty()) {
@@ -2372,9 +2367,9 @@ void DWARFVerifier::summarize() {
raw_fd_ostream JsonStream(DumpOpts.JsonErrSummaryFile, EC,
sys::fs::OF_Text);
if (EC) {
- error() << "unable to open json summary file '"
- << DumpOpts.JsonErrSummaryFile
- << "' for writing: " << EC.message() << '\n';
+ error() << formatv(
+ "unable to open json summary file {0} for writing: {1}\n",
+ DumpOpts.JsonErrSummaryFile, EC.message());
return;
}
More information about the llvm-commits
mailing list