[lld] fcb6b13 - [lld] Use context-aware outs() and errs()
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 16 21:37:40 PST 2024
Author: Fangrui Song
Date: 2024-11-16T21:37:34-08:00
New Revision: fcb6b132fa7284426349d6d0063d3a0ed8864683
URL: https://github.com/llvm/llvm-project/commit/fcb6b132fa7284426349d6d0063d3a0ed8864683
DIFF: https://github.com/llvm/llvm-project/commit/fcb6b132fa7284426349d6d0063d3a0ed8864683.diff
LOG: [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().
Added:
Modified:
lld/COFF/DriverUtils.cpp
lld/Common/ErrorHandler.cpp
lld/MachO/Driver.cpp
lld/MachO/Driver.h
lld/MachO/DriverUtils.cpp
lld/include/lld/Common/ErrorHandler.h
lld/include/lld/Common/LLVM.h
lld/wasm/Driver.cpp
Removed:
################################################################################
diff --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp
index ce73506db68aca..8a72d74bd27bac 100644
--- a/lld/COFF/DriverUtils.cpp
+++ b/lld/COFF/DriverUtils.cpp
@@ -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);
}
diff --git a/lld/Common/ErrorHandler.cpp b/lld/Common/ErrorHandler.cpp
index e80b69c9c30669..831f4ee0837ec9 100644
--- a/lld/Common/ErrorHandler.cpp
+++ b/lld/Common/ErrorHandler.cpp
@@ -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();
diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index e9482f85bdc078..be0ee7ad8dff9b 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -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];
diff --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h
index 82cd1880ecab1b..bd1cd960768bf5 100644
--- a/lld/MachO/Driver.h
+++ b/lld/MachO/Driver.h
@@ -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
diff --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp
index 077a639bf7ab1e..858a4bb34029cf 100644
--- a/lld/MachO/DriverUtils.cpp
+++ b/lld/MachO/DriverUtils.cpp
@@ -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) {
diff --git a/lld/include/lld/Common/ErrorHandler.h b/lld/include/lld/Common/ErrorHandler.h
index 607092477ec766..ee11f178939710 100644
--- a/lld/include/lld/Common/ErrorHandler.h
+++ b/lld/include/lld/Common/ErrorHandler.h
@@ -83,7 +83,6 @@ class DiagnosticInfo;
namespace lld {
llvm::raw_ostream &outs();
-llvm::raw_ostream &errs();
enum class ErrorTag { LibNotFound, SymbolNotFound };
diff --git a/lld/include/lld/Common/LLVM.h b/lld/include/lld/Common/LLVM.h
index 6872adf8d00f40..e11f98025c5343 100644
--- a/lld/include/lld/Common/LLVM.h
+++ b/lld/include/lld/Common/LLVM.h
@@ -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;
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 43e13c3a5ca22d..8d01ff839ddfcf 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -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)) {
More information about the llvm-commits
mailing list