[lld] Use context-aware outs() and errs()

For COFF and ELF that are mostly free of global states, lld::errs() and
lld::outs() should not be used. This migration change allows us to
remove lld::errs, which uses the global errorHandler().
This commit is contained in:
Fangrui Song 2024-11-16 21:37:34 -08:00
parent 2f4572f5e7
commit fcb6b132fa
8 changed files with 39 additions and 37 deletions

View File

@ -1020,7 +1020,7 @@ std::vector<const char *> ArgParser::tokenize(StringRef s) {
}
void LinkerDriver::printHelp(const char *argv0) {
ctx.optTable.printHelp(lld::outs(),
ctx.optTable.printHelp(ctx.e.outs(),
(std::string(argv0) + " [options] file...").c_str(),
"LLVM Linker", false);
}

View File

@ -70,11 +70,6 @@ raw_ostream &lld::outs() {
return e.outs();
}
raw_ostream &lld::errs() {
ErrorHandler &e = errorHandler();
return e.errs();
}
raw_ostream &ErrorHandler::outs() {
if (disableOutput)
return llvm::nulls();

View File

@ -1567,7 +1567,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]);
MachOOptTable parser;
InputArgList args = parser.parse(argsArr.slice(1));
InputArgList args = parser.parse(*ctx, argsArr.slice(1));
ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now "
"(use --error-limit=0 to see all errors)";
@ -1575,11 +1575,11 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
ctx->e.verbose = args.hasArg(OPT_verbose);
if (args.hasArg(OPT_help_hidden)) {
parser.printHelp(argsArr[0], /*showHidden=*/true);
parser.printHelp(*ctx, argsArr[0], /*showHidden=*/true);
return true;
}
if (args.hasArg(OPT_help)) {
parser.printHelp(argsArr[0], /*showHidden=*/false);
parser.printHelp(*ctx, argsArr[0], /*showHidden=*/false);
return true;
}
if (args.hasArg(OPT_version)) {
@ -2040,17 +2040,17 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
shouldAdhocSignByDefault(config->arch(), config->platform()));
if (args.hasArg(OPT_v)) {
message(getLLDVersion(), lld::errs());
message(getLLDVersion(), ctx->e.errs());
message(StringRef("Library search paths:") +
(config->librarySearchPaths.empty()
? ""
: "\n\t" + join(config->librarySearchPaths, "\n\t")),
lld::errs());
ctx->e.errs());
message(StringRef("Framework search paths:") +
(config->frameworkSearchPaths.empty()
? ""
: "\n\t" + join(config->frameworkSearchPaths, "\n\t")),
lld::errs());
ctx->e.errs());
}
config->progName = argsArr[0];

View File

@ -20,6 +20,9 @@
#include <set>
#include <type_traits>
namespace lld {
class CommonLinkerContext;
}
namespace lld::macho {
class DylibFile;
@ -28,8 +31,10 @@ class InputFile;
class MachOOptTable : public llvm::opt::GenericOptTable {
public:
MachOOptTable();
llvm::opt::InputArgList parse(ArrayRef<const char *> argv);
void printHelp(const char *argv0, bool showHidden) const;
llvm::opt::InputArgList parse(CommonLinkerContext &ctx,
ArrayRef<const char *> argv);
void printHelp(CommonLinkerContext &ctx, const char *argv0,
bool showHidden) const;
};
// Create enum with OPT_xxx values for each option in Options.td

View File

@ -69,28 +69,31 @@ MachOOptTable::MachOOptTable() : GenericOptTable(optInfo) {}
// Set color diagnostics according to --color-diagnostics={auto,always,never}
// or --no-color-diagnostics flags.
static void handleColorDiagnostics(InputArgList &args) {
static void handleColorDiagnostics(CommonLinkerContext &ctx,
InputArgList &args) {
const Arg *arg =
args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
OPT_no_color_diagnostics);
if (!arg)
return;
auto &errs = ctx.e.errs();
if (arg->getOption().getID() == OPT_color_diagnostics) {
lld::errs().enable_colors(true);
errs.enable_colors(true);
} else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
lld::errs().enable_colors(false);
errs.enable_colors(false);
} else {
StringRef s = arg->getValue();
if (s == "always")
lld::errs().enable_colors(true);
errs.enable_colors(true);
else if (s == "never")
lld::errs().enable_colors(false);
errs.enable_colors(false);
else if (s != "auto")
error("unknown option: --color-diagnostics=" + s);
}
}
InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
InputArgList MachOOptTable::parse(CommonLinkerContext &ctx,
ArrayRef<const char *> argv) {
// Make InputArgList from string vectors.
unsigned missingIndex;
unsigned missingCount;
@ -109,7 +112,7 @@ InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
if (missingCount)
error(Twine(args.getArgString(missingIndex)) + ": missing argument");
handleColorDiagnostics(args);
handleColorDiagnostics(ctx, args);
for (const Arg *arg : args.filtered(OPT_UNKNOWN)) {
std::string nearest;
@ -122,11 +125,12 @@ InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
return args;
}
void MachOOptTable::printHelp(const char *argv0, bool showHidden) const {
OptTable::printHelp(lld::outs(),
(std::string(argv0) + " [options] file...").c_str(),
void MachOOptTable::printHelp(CommonLinkerContext &ctx, const char *argv0,
bool showHidden) const {
auto &outs = ctx.e.outs();
OptTable::printHelp(outs, (std::string(argv0) + " [options] file...").c_str(),
"LLVM Linker", showHidden);
lld::outs() << "\n";
outs << '\n';
}
static std::string rewritePath(StringRef s) {

View File

@ -83,7 +83,6 @@ class DiagnosticInfo;
namespace lld {
llvm::raw_ostream &outs();
llvm::raw_ostream &errs();
enum class ErrorTag { LibNotFound, SymbolNotFound };

View File

@ -58,9 +58,6 @@ struct WasmTableType;
} // namespace llvm
namespace lld {
llvm::raw_ostream &outs();
llvm::raw_ostream &errs();
// Casting operators.
using llvm::cast;
using llvm::cast_or_null;

View File

@ -184,16 +184,17 @@ static void handleColorDiagnostics(opt::InputArgList &args) {
OPT_no_color_diagnostics);
if (!arg)
return;
auto &errs = errorHandler().errs();
if (arg->getOption().getID() == OPT_color_diagnostics) {
lld::errs().enable_colors(true);
errs.enable_colors(true);
} else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
lld::errs().enable_colors(false);
errs.enable_colors(false);
} else {
StringRef s = arg->getValue();
if (s == "always")
lld::errs().enable_colors(true);
errs.enable_colors(true);
else if (s == "never")
lld::errs().enable_colors(false);
errs.enable_colors(false);
else if (s != "auto")
error("unknown option: --color-diagnostics=" + s);
}
@ -1258,14 +1259,15 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
opt::InputArgList args = parser.parse(argsArr.slice(1));
// Interpret these flags early because error()/warn() depend on them.
errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
errorHandler().fatalWarnings =
auto &errHandler = errorHandler();
errHandler.errorLimit = args::getInteger(args, OPT_error_limit, 20);
errHandler.fatalWarnings =
args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
checkZOptions(args);
// Handle --help
if (args.hasArg(OPT_help)) {
parser.printHelp(lld::outs(),
parser.printHelp(errHandler.outs(),
(std::string(argsArr[0]) + " [options] file...").c_str(),
"LLVM Linker", false);
return;
@ -1273,7 +1275,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
// Handle -v or -version.
if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
lld::outs() << getLLDVersion() << "\n";
errHandler.outs() << getLLDVersion() << "\n";
// Handle --reproduce
if (const char *path = getReproduceOption(args)) {