[lld-link] Simplify some << toString

This commit is contained in:
Fangrui Song 2024-12-05 20:56:19 -08:00
parent 983f88c1ec
commit c7caab2238
3 changed files with 25 additions and 34 deletions

View File

@ -621,7 +621,7 @@ void ObjFile::handleComdatSelection(
// seems better though.
// (This behavior matches ModuleLinker::getComdatResult().)
if (selection != leaderSelection) {
Log(ctx) << "conflicting comdat type for " << toString(ctx, *leader) << ": "
Log(ctx) << "conflicting comdat type for " << leader << ": "
<< (int)leaderSelection << " in " << leader->getFile() << " and "
<< (int)selection << " in " << this;
ctx.symtab.reportDuplicate(leader, this);

View File

@ -272,9 +272,8 @@ struct UndefinedDiag {
static void reportUndefinedSymbol(COFFLinkerContext &ctx,
const UndefinedDiag &undefDiag) {
std::string out;
llvm::raw_string_ostream os(out);
os << "undefined symbol: " << toString(ctx, *undefDiag.sym);
auto diag = errorOrWarn(ctx);
diag << "undefined symbol: " << undefDiag.sym;
const size_t maxUndefReferences = 3;
size_t numDisplayedRefs = 0, numRefs = 0;
@ -284,13 +283,11 @@ static void reportUndefinedSymbol(COFFLinkerContext &ctx,
numRefs += totalLocations;
numDisplayedRefs += symbolLocations.size();
for (const std::string &s : symbolLocations) {
os << s;
}
for (const std::string &s : symbolLocations)
diag << s;
}
if (numDisplayedRefs < numRefs)
os << "\n>>> referenced " << numRefs - numDisplayedRefs << " more times";
errorOrWarn(ctx) << out;
diag << "\n>>> referenced " << numRefs - numDisplayedRefs << " more times";
}
void SymbolTable::loadMinGWSymbols() {
@ -422,12 +419,12 @@ static void reportProblemSymbols(
for (Symbol *b : ctx.config.gcroot) {
if (undefs.count(b))
errorOrWarn(ctx) << "<root>: undefined symbol: " << toString(ctx, *b);
errorOrWarn(ctx) << "<root>: undefined symbol: " << b;
if (localImports)
if (Symbol *imp = localImports->lookup(b))
Warn(ctx) << "<root>: locally defined symbol imported: "
<< toString(ctx, *imp) << " (defined in "
<< toString(imp->getFile()) << ") [LNK4217]";
Warn(ctx) << "<root>: locally defined symbol imported: " << imp
<< " (defined in " << toString(imp->getFile())
<< ") [LNK4217]";
}
std::vector<UndefinedDiag> undefDiags;
@ -448,9 +445,8 @@ static void reportProblemSymbols(
}
if (localImports)
if (Symbol *imp = localImports->lookup(sym))
Warn(ctx) << file << ": locally defined symbol imported: "
<< toString(ctx, *imp) << " (defined in " << imp->getFile()
<< ") [LNK4217]";
Warn(ctx) << file << ": locally defined symbol imported: " << imp
<< " (defined in " << imp->getFile() << ") [LNK4217]";
}
};
@ -800,24 +796,19 @@ static std::string getSourceLocation(InputFile *file, SectionChunk *sc,
void SymbolTable::reportDuplicate(Symbol *existing, InputFile *newFile,
SectionChunk *newSc,
uint32_t newSectionOffset) {
std::string msg;
llvm::raw_string_ostream os(msg);
os << "duplicate symbol: " << toString(ctx, *existing);
COFFSyncStream diag(ctx, ctx.config.forceMultiple ? DiagLevel::Warn
: DiagLevel::Err);
diag << "duplicate symbol: " << existing;
DefinedRegular *d = dyn_cast<DefinedRegular>(existing);
if (d && isa<ObjFile>(d->getFile())) {
os << getSourceLocation(d->getFile(), d->getChunk(), d->getValue(),
existing->getName());
diag << getSourceLocation(d->getFile(), d->getChunk(), d->getValue(),
existing->getName());
} else {
os << getSourceLocation(existing->getFile(), nullptr, 0, "");
diag << getSourceLocation(existing->getFile(), nullptr, 0, "");
}
os << getSourceLocation(newFile, newSc, newSectionOffset,
existing->getName());
if (ctx.config.forceMultiple)
Warn(ctx) << msg;
else
Err(ctx) << msg;
diag << getSourceLocation(newFile, newSc, newSectionOffset,
existing->getName());
}
Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {

View File

@ -1256,12 +1256,12 @@ void Writer::createImportTables() {
ctx.config.dllOrder[dll] = ctx.config.dllOrder.size();
if (file->impSym && !isa<DefinedImportData>(file->impSym))
Fatal(ctx) << toString(ctx, *file->impSym) << " was replaced";
Fatal(ctx) << file->impSym << " was replaced";
DefinedImportData *impSym = cast_or_null<DefinedImportData>(file->impSym);
if (ctx.config.delayLoads.count(StringRef(file->dllName).lower())) {
if (!file->thunkSym)
Fatal(ctx) << "cannot delay-load " << toString(file)
<< " due to import of data: " << toString(ctx, *impSym);
<< " due to import of data: " << impSym;
delayIdata.add(impSym);
} else {
idata.add(impSym);
@ -1280,7 +1280,7 @@ void Writer::appendImportThunks() {
if (file->thunkSym) {
if (!isa<DefinedImportThunk>(file->thunkSym))
Fatal(ctx) << toString(ctx, *file->thunkSym) << " was replaced";
Fatal(ctx) << file->thunkSym << " was replaced";
auto *chunk = cast<DefinedImportThunk>(file->thunkSym)->getChunk();
if (chunk->live)
textSec->addChunk(chunk);
@ -1288,7 +1288,7 @@ void Writer::appendImportThunks() {
if (file->auxThunkSym) {
if (!isa<DefinedImportThunk>(file->auxThunkSym))
Fatal(ctx) << toString(ctx, *file->auxThunkSym) << " was replaced";
Fatal(ctx) << file->auxThunkSym << " was replaced";
auto *chunk = cast<DefinedImportThunk>(file->auxThunkSym)->getChunk();
if (chunk->live)
textSec->addChunk(chunk);
@ -1334,7 +1334,7 @@ void Writer::createExportTable() {
// Warn on exported deleting destructor.
for (auto e : ctx.config.exports)
if (e.sym && e.sym->getName().starts_with("??_G"))
Warn(ctx) << "export of deleting dtor: " << toString(ctx, *e.sym);
Warn(ctx) << "export of deleting dtor: " << e.sym;
}
void Writer::removeUnusedSections() {