[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. // seems better though.
// (This behavior matches ModuleLinker::getComdatResult().) // (This behavior matches ModuleLinker::getComdatResult().)
if (selection != leaderSelection) { 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)leaderSelection << " in " << leader->getFile() << " and "
<< (int)selection << " in " << this; << (int)selection << " in " << this;
ctx.symtab.reportDuplicate(leader, this); ctx.symtab.reportDuplicate(leader, this);

View File

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

View File

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