Skip to content

Commit fcb6b13

Browse files
committed
[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().
1 parent 2f4572f commit fcb6b13

File tree

8 files changed

+39
-37
lines changed

8 files changed

+39
-37
lines changed

lld/COFF/DriverUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ std::vector<const char *> ArgParser::tokenize(StringRef s) {
10201020
}
10211021

10221022
void LinkerDriver::printHelp(const char *argv0) {
1023-
ctx.optTable.printHelp(lld::outs(),
1023+
ctx.optTable.printHelp(ctx.e.outs(),
10241024
(std::string(argv0) + " [options] file...").c_str(),
10251025
"LLVM Linker", false);
10261026
}

lld/Common/ErrorHandler.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ raw_ostream &lld::outs() {
7070
return e.outs();
7171
}
7272

73-
raw_ostream &lld::errs() {
74-
ErrorHandler &e = errorHandler();
75-
return e.errs();
76-
}
77-
7873
raw_ostream &ErrorHandler::outs() {
7974
if (disableOutput)
8075
return llvm::nulls();

lld/MachO/Driver.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,19 +1567,19 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
15671567
ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]);
15681568

15691569
MachOOptTable parser;
1570-
InputArgList args = parser.parse(argsArr.slice(1));
1570+
InputArgList args = parser.parse(*ctx, argsArr.slice(1));
15711571

15721572
ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now "
15731573
"(use --error-limit=0 to see all errors)";
15741574
ctx->e.errorLimit = args::getInteger(args, OPT_error_limit_eq, 20);
15751575
ctx->e.verbose = args.hasArg(OPT_verbose);
15761576

15771577
if (args.hasArg(OPT_help_hidden)) {
1578-
parser.printHelp(argsArr[0], /*showHidden=*/true);
1578+
parser.printHelp(*ctx, argsArr[0], /*showHidden=*/true);
15791579
return true;
15801580
}
15811581
if (args.hasArg(OPT_help)) {
1582-
parser.printHelp(argsArr[0], /*showHidden=*/false);
1582+
parser.printHelp(*ctx, argsArr[0], /*showHidden=*/false);
15831583
return true;
15841584
}
15851585
if (args.hasArg(OPT_version)) {
@@ -2040,17 +2040,17 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
20402040
shouldAdhocSignByDefault(config->arch(), config->platform()));
20412041

20422042
if (args.hasArg(OPT_v)) {
2043-
message(getLLDVersion(), lld::errs());
2043+
message(getLLDVersion(), ctx->e.errs());
20442044
message(StringRef("Library search paths:") +
20452045
(config->librarySearchPaths.empty()
20462046
? ""
20472047
: "\n\t" + join(config->librarySearchPaths, "\n\t")),
2048-
lld::errs());
2048+
ctx->e.errs());
20492049
message(StringRef("Framework search paths:") +
20502050
(config->frameworkSearchPaths.empty()
20512051
? ""
20522052
: "\n\t" + join(config->frameworkSearchPaths, "\n\t")),
2053-
lld::errs());
2053+
ctx->e.errs());
20542054
}
20552055

20562056
config->progName = argsArr[0];

lld/MachO/Driver.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include <set>
2121
#include <type_traits>
2222

23+
namespace lld {
24+
class CommonLinkerContext;
25+
}
2326
namespace lld::macho {
2427

2528
class DylibFile;
@@ -28,8 +31,10 @@ class InputFile;
2831
class MachOOptTable : public llvm::opt::GenericOptTable {
2932
public:
3033
MachOOptTable();
31-
llvm::opt::InputArgList parse(ArrayRef<const char *> argv);
32-
void printHelp(const char *argv0, bool showHidden) const;
34+
llvm::opt::InputArgList parse(CommonLinkerContext &ctx,
35+
ArrayRef<const char *> argv);
36+
void printHelp(CommonLinkerContext &ctx, const char *argv0,
37+
bool showHidden) const;
3338
};
3439

3540
// Create enum with OPT_xxx values for each option in Options.td

lld/MachO/DriverUtils.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,31 @@ MachOOptTable::MachOOptTable() : GenericOptTable(optInfo) {}
6969

7070
// Set color diagnostics according to --color-diagnostics={auto,always,never}
7171
// or --no-color-diagnostics flags.
72-
static void handleColorDiagnostics(InputArgList &args) {
72+
static void handleColorDiagnostics(CommonLinkerContext &ctx,
73+
InputArgList &args) {
7374
const Arg *arg =
7475
args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
7576
OPT_no_color_diagnostics);
7677
if (!arg)
7778
return;
79+
auto &errs = ctx.e.errs();
7880
if (arg->getOption().getID() == OPT_color_diagnostics) {
79-
lld::errs().enable_colors(true);
81+
errs.enable_colors(true);
8082
} else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
81-
lld::errs().enable_colors(false);
83+
errs.enable_colors(false);
8284
} else {
8385
StringRef s = arg->getValue();
8486
if (s == "always")
85-
lld::errs().enable_colors(true);
87+
errs.enable_colors(true);
8688
else if (s == "never")
87-
lld::errs().enable_colors(false);
89+
errs.enable_colors(false);
8890
else if (s != "auto")
8991
error("unknown option: --color-diagnostics=" + s);
9092
}
9193
}
9294

93-
InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
95+
InputArgList MachOOptTable::parse(CommonLinkerContext &ctx,
96+
ArrayRef<const char *> argv) {
9497
// Make InputArgList from string vectors.
9598
unsigned missingIndex;
9699
unsigned missingCount;
@@ -109,7 +112,7 @@ InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
109112
if (missingCount)
110113
error(Twine(args.getArgString(missingIndex)) + ": missing argument");
111114

112-
handleColorDiagnostics(args);
115+
handleColorDiagnostics(ctx, args);
113116

114117
for (const Arg *arg : args.filtered(OPT_UNKNOWN)) {
115118
std::string nearest;
@@ -122,11 +125,12 @@ InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
122125
return args;
123126
}
124127

125-
void MachOOptTable::printHelp(const char *argv0, bool showHidden) const {
126-
OptTable::printHelp(lld::outs(),
127-
(std::string(argv0) + " [options] file...").c_str(),
128+
void MachOOptTable::printHelp(CommonLinkerContext &ctx, const char *argv0,
129+
bool showHidden) const {
130+
auto &outs = ctx.e.outs();
131+
OptTable::printHelp(outs, (std::string(argv0) + " [options] file...").c_str(),
128132
"LLVM Linker", showHidden);
129-
lld::outs() << "\n";
133+
outs << '\n';
130134
}
131135

132136
static std::string rewritePath(StringRef s) {

lld/include/lld/Common/ErrorHandler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class DiagnosticInfo;
8383
namespace lld {
8484

8585
llvm::raw_ostream &outs();
86-
llvm::raw_ostream &errs();
8786

8887
enum class ErrorTag { LibNotFound, SymbolNotFound };
8988

lld/include/lld/Common/LLVM.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ struct WasmTableType;
5858
} // namespace llvm
5959

6060
namespace lld {
61-
llvm::raw_ostream &outs();
62-
llvm::raw_ostream &errs();
63-
6461
// Casting operators.
6562
using llvm::cast;
6663
using llvm::cast_or_null;

lld/wasm/Driver.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,17 @@ static void handleColorDiagnostics(opt::InputArgList &args) {
184184
OPT_no_color_diagnostics);
185185
if (!arg)
186186
return;
187+
auto &errs = errorHandler().errs();
187188
if (arg->getOption().getID() == OPT_color_diagnostics) {
188-
lld::errs().enable_colors(true);
189+
errs.enable_colors(true);
189190
} else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
190-
lld::errs().enable_colors(false);
191+
errs.enable_colors(false);
191192
} else {
192193
StringRef s = arg->getValue();
193194
if (s == "always")
194-
lld::errs().enable_colors(true);
195+
errs.enable_colors(true);
195196
else if (s == "never")
196-
lld::errs().enable_colors(false);
197+
errs.enable_colors(false);
197198
else if (s != "auto")
198199
error("unknown option: --color-diagnostics=" + s);
199200
}
@@ -1258,22 +1259,23 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
12581259
opt::InputArgList args = parser.parse(argsArr.slice(1));
12591260

12601261
// Interpret these flags early because error()/warn() depend on them.
1261-
errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
1262-
errorHandler().fatalWarnings =
1262+
auto &errHandler = errorHandler();
1263+
errHandler.errorLimit = args::getInteger(args, OPT_error_limit, 20);
1264+
errHandler.fatalWarnings =
12631265
args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
12641266
checkZOptions(args);
12651267

12661268
// Handle --help
12671269
if (args.hasArg(OPT_help)) {
1268-
parser.printHelp(lld::outs(),
1270+
parser.printHelp(errHandler.outs(),
12691271
(std::string(argsArr[0]) + " [options] file...").c_str(),
12701272
"LLVM Linker", false);
12711273
return;
12721274
}
12731275

12741276
// Handle -v or -version.
12751277
if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
1276-
lld::outs() << getLLDVersion() << "\n";
1278+
errHandler.outs() << getLLDVersion() << "\n";
12771279

12781280
// Handle --reproduce
12791281
if (const char *path = getReproduceOption(args)) {

0 commit comments

Comments
 (0)