Skip to content

Commit 9664ce6

Browse files
committed
[ELF] Simplify complex diagnostics
1 parent fd9f3be commit 9664ce6

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

lld/ELF/Relocations.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,44 +100,41 @@ static std::string getLocation(Ctx &ctx, InputSectionBase &s, const Symbol &sym,
100100
void elf::reportRangeError(Ctx &ctx, uint8_t *loc, const Relocation &rel,
101101
const Twine &v, int64_t min, uint64_t max) {
102102
ErrorPlace errPlace = getErrorPlace(ctx, loc);
103-
std::string hint;
103+
auto diag = Err(ctx);
104+
diag << errPlace.loc << "relocation " << rel.type
105+
<< " out of range: " << v.str() << " is not in [" << min << ", " << max
106+
<< ']';
107+
104108
if (rel.sym) {
105109
if (!rel.sym->isSection())
106-
hint = "; references '" + toStr(ctx, *rel.sym) + '\'';
110+
diag << "; references '" << rel.sym << '\'';
107111
else if (auto *d = dyn_cast<Defined>(rel.sym))
108-
hint = ("; references section '" + d->section->name + "'").str();
112+
diag << "; references section '" << d->section->name << "'";
109113

110114
if (ctx.arg.emachine == EM_X86_64 && rel.type == R_X86_64_PC32 &&
111115
rel.sym->getOutputSection() &&
112116
(rel.sym->getOutputSection()->flags & SHF_X86_64_LARGE)) {
113-
hint += "; R_X86_64_PC32 should not reference a section marked "
117+
diag << "; R_X86_64_PC32 should not reference a section marked "
114118
"SHF_X86_64_LARGE";
115119
}
116120
}
117121
if (!errPlace.srcLoc.empty())
118-
hint += "\n>>> referenced by " + errPlace.srcLoc;
122+
diag << "\n>>> referenced by " << errPlace.srcLoc;
119123
if (rel.sym && !rel.sym->isSection())
120-
hint += getDefinedLocation(ctx, *rel.sym);
124+
diag << getDefinedLocation(ctx, *rel.sym);
121125

122126
if (errPlace.isec && errPlace.isec->name.starts_with(".debug"))
123-
hint += "; consider recompiling with -fdebug-types-section to reduce size "
127+
diag << "; consider recompiling with -fdebug-types-section to reduce size "
124128
"of debug sections";
125-
126-
Err(ctx) << errPlace.loc << "relocation " << rel.type
127-
<< " out of range: " << v.str() << " is not in [" << Twine(min).str()
128-
<< ", " << Twine(max).str() << "]" << hint;
129129
}
130130

131131
void elf::reportRangeError(Ctx &ctx, uint8_t *loc, int64_t v, int n,
132132
const Symbol &sym, const Twine &msg) {
133-
ErrorPlace errPlace = getErrorPlace(ctx, loc);
134-
std::string hint;
133+
auto diag = Err(ctx);
134+
diag << getErrorPlace(ctx, loc).loc << msg << " is out of range: " << v
135+
<< " is not in [" << llvm::minIntN(n) << ", " << llvm::maxIntN(n) << "]";
135136
if (!sym.getName().empty())
136-
hint = "; references '" + toStr(ctx, sym) + '\'' +
137-
getDefinedLocation(ctx, sym);
138-
Err(ctx) << errPlace.loc << msg << " is out of range: " << Twine(v)
139-
<< " is not in [" << Twine(llvm::minIntN(n)) << ", "
140-
<< Twine(llvm::maxIntN(n)) << "]" << hint;
137+
diag << "; references '" << &sym << '\'' << getDefinedLocation(ctx, sym);
141138
}
142139

143140
// Build a bitmask with one bit set for each 64 subset of RelExpr.

lld/ELF/Symbols.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -548,15 +548,14 @@ void elf::reportDuplicate(Ctx &ctx, const Symbol &sym, const InputFile *newFile,
548548
std::string src2 = errSec->getSrcMsg(sym, errOffset);
549549
std::string obj2 = errSec->getObjMsg(errOffset);
550550

551-
std::string msg =
552-
"duplicate symbol: " + toStr(ctx, sym) + "\n>>> defined at ";
551+
auto diag = Err(ctx);
552+
diag << "duplicate symbol: " << &sym << "\n>>> defined at ";
553553
if (!src1.empty())
554-
msg += src1 + "\n>>> ";
555-
msg += obj1 + "\n>>> defined at ";
554+
diag << src1 << "\n>>> ";
555+
diag << obj1 << "\n>>> defined at ";
556556
if (!src2.empty())
557-
msg += src2 + "\n>>> ";
558-
msg += obj2;
559-
Err(ctx) << msg;
557+
diag << src2 << "\n>>> ";
558+
diag << obj2;
560559
}
561560

562561
void Symbol::checkDuplicate(Ctx &ctx, const Defined &other) const {

0 commit comments

Comments
 (0)