Don't pass llvm::errs() all over the place. Diagnostics always go to stderr.

llvm-svn: 185657
This commit is contained in:
Rafael Espindola 2013-07-04 17:06:04 +00:00
parent 658a378542
commit 2530f67116
7 changed files with 75 additions and 98 deletions

View File

@ -20,7 +20,6 @@
#include "lld/Core/LLVM.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
#include <vector>
@ -37,8 +36,7 @@ class Driver {
protected:
/// Performs link using specified options.
static bool link(const TargetInfo &targetInfo,
raw_ostream &diagnostics = llvm::errs());
static bool link(const TargetInfo &targetInfo);
private:
Driver() LLVM_DELETED_FUNCTION;
};
@ -49,8 +47,7 @@ private:
class UniversalDriver : public Driver {
public:
/// Determine flavor and pass control to Driver for that flavor.
static bool link(int argc, const char *argv[],
raw_ostream &diagnostics = llvm::errs());
static bool link(int argc, const char *argv[]);
private:
UniversalDriver() LLVM_DELETED_FUNCTION;
@ -62,14 +59,12 @@ class GnuLdDriver : public Driver {
public:
/// Parses command line arguments same as gnu/binutils ld and performs link.
/// Returns true iff an error occurred.
static bool linkELF(int argc, const char *argv[],
raw_ostream &diagnostics = llvm::errs());
static bool linkELF(int argc, const char *argv[]);
/// Uses gnu/binutils style ld command line options to fill in options struct.
/// Returns true iff there was an error.
static bool parse(int argc, const char *argv[],
std::unique_ptr<ELFTargetInfo> &targetInfo,
raw_ostream &diagnostics = llvm::errs());
std::unique_ptr<ELFTargetInfo> &targetInfo);
private:
static llvm::Triple getDefaultTarget(const char *progName);
@ -83,13 +78,11 @@ class DarwinLdDriver : public Driver {
public:
/// Parses command line arguments same as darwin's ld and performs link.
/// Returns true iff there was an error.
static bool linkMachO(int argc, const char *argv[],
raw_ostream &diagnostics = llvm::errs());
static bool linkMachO(int argc, const char *argv[]);
/// Uses darwin style ld command line options to update targetInfo object.
/// Returns true iff there was an error.
static bool parse(int argc, const char *argv[], MachOTargetInfo &info,
raw_ostream &diagnostics = llvm::errs());
static bool parse(int argc, const char *argv[], MachOTargetInfo &info);
private:
DarwinLdDriver() LLVM_DELETED_FUNCTION;
};
@ -100,13 +93,11 @@ class WinLinkDriver : public Driver {
public:
/// Parses command line arguments same as Windows link.exe and performs link.
/// Returns true iff there was an error.
static bool linkPECOFF(int argc, const char *argv[],
raw_ostream &diagnostics = llvm::errs());
static bool linkPECOFF(int argc, const char *argv[]);
/// Uses Windows style link command line options to fill in options struct.
/// Returns true iff there was an error.
static bool parse(int argc, const char *argv[], PECOFFTargetInfo &info,
raw_ostream &diagnostics = llvm::errs());
static bool parse(int argc, const char *argv[], PECOFFTargetInfo &info);
private:
WinLinkDriver() LLVM_DELETED_FUNCTION;
@ -119,13 +110,11 @@ public:
/// Parses command line arguments same as lld-core and performs link.
/// Returns true iff there was an error.
static bool link(int argc, const char *argv[],
raw_ostream &diagnostics = llvm::errs());
static bool link(int argc, const char *argv[]);
/// Uses lld-core command line options to fill in options struct.
/// Returns true iff there was an error.
static bool parse(int argc, const char *argv[], CoreTargetInfo &info,
raw_ostream &diagnostics = llvm::errs());
static bool parse(int argc, const char *argv[], CoreTargetInfo &info);
private:
CoreDriver() LLVM_DELETED_FUNCTION;

View File

@ -67,7 +67,7 @@ public:
namespace lld {
bool CoreDriver::link(int argc, const char *argv[], raw_ostream &diagnostics) {
bool CoreDriver::link(int argc, const char *argv[]) {
CoreTargetInfo info;
if (parse(argc, argv, info))
return true;
@ -75,9 +75,7 @@ bool CoreDriver::link(int argc, const char *argv[], raw_ostream &diagnostics) {
return Driver::link(info);
}
bool CoreDriver::parse(int argc, const char *argv[],
CoreTargetInfo &info, raw_ostream &diagnostics) {
bool CoreDriver::parse(int argc, const char *argv[], CoreTargetInfo &info) {
// Parse command line options using CoreOptions.td
std::unique_ptr<llvm::opt::InputArgList> parsedArgs;
CoreOptTable table;
@ -86,15 +84,15 @@ bool CoreDriver::parse(int argc, const char *argv[],
parsedArgs.reset(table.ParseArgs(&argv[1], &argv[argc],
missingIndex, missingCount));
if (missingCount) {
diagnostics << "error: missing arg value for '"
<< parsedArgs->getArgString(missingIndex)
<< "' expected " << missingCount << " argument(s).\n";
llvm::errs() << "error: missing arg value for '"
<< parsedArgs->getArgString(missingIndex) << "' expected "
<< missingCount << " argument(s).\n";
return true;
}
for (auto it = parsedArgs->filtered_begin(OPT_UNKNOWN),
ie = parsedArgs->filtered_end(); it != ie; ++it) {
diagnostics << "warning: ignoring unknown argument: "
llvm::errs() << "warning: ignoring unknown argument: "
<< (*it)->getAsString(*parsedArgs) << "\n";
}

View File

@ -70,19 +70,16 @@ public:
namespace lld {
bool DarwinLdDriver::linkMachO(int argc, const char *argv[],
raw_ostream &diagnostics) {
bool DarwinLdDriver::linkMachO(int argc, const char *argv[]) {
MachOTargetInfo info;
if (parse(argc, argv, info, diagnostics))
if (parse(argc, argv, info))
return true;
return link(info, diagnostics);
return link(info);
}
bool DarwinLdDriver::parse(int argc, const char *argv[],
MachOTargetInfo &info, raw_ostream &diagnostics) {
MachOTargetInfo &info) {
// Parse command line options using DarwinOptions.td
std::unique_ptr<llvm::opt::InputArgList> parsedArgs;
DarwinLdOptTable table;
@ -91,15 +88,15 @@ bool DarwinLdDriver::parse(int argc, const char *argv[],
parsedArgs.reset(table.ParseArgs(&argv[1], &argv[argc],
missingIndex, missingCount));
if (missingCount) {
diagnostics << "error: missing arg value for '"
<< parsedArgs->getArgString(missingIndex)
<< "' expected " << missingCount << " argument(s).\n";
llvm::errs() << "error: missing arg value for '"
<< parsedArgs->getArgString(missingIndex) << "' expected "
<< missingCount << " argument(s).\n";
return true;
}
for (auto it = parsedArgs->filtered_begin(OPT_UNKNOWN),
ie = parsedArgs->filtered_end(); it != ie; ++it) {
diagnostics << "warning: ignoring unknown argument: "
llvm::errs() << "warning: ignoring unknown argument: "
<< (*it)->getAsString(*parsedArgs) << "\n";
}
@ -158,19 +155,19 @@ bool DarwinLdDriver::parse(int argc, const char *argv[],
switch (minOS->getOption().getID()) {
case OPT_macosx_version_min:
if (info.setOS(MachOTargetInfo::OS::macOSX, minOS->getValue())) {
diagnostics << "error: malformed macosx_version_min value\n";
llvm::errs() << "error: malformed macosx_version_min value\n";
return true;
}
break;
case OPT_ios_version_min:
if (info.setOS(MachOTargetInfo::OS::iOS, minOS->getValue())) {
diagnostics << "error: malformed ios_version_min value\n";
llvm::errs() << "error: malformed ios_version_min value\n";
return true;
}
break;
case OPT_ios_simulator_version_min:
if (info.setOS(MachOTargetInfo::OS::iOS_simulator, minOS->getValue())) {
diagnostics << "error: malformed ios_simulator_version_min value\n";
llvm::errs() << "error: malformed ios_simulator_version_min value\n";
return true;
}
break;
@ -189,7 +186,7 @@ bool DarwinLdDriver::parse(int argc, const char *argv[],
}
// Validate the combination of options used.
if (info.validate(diagnostics))
if (info.validate(llvm::errs()))
return true;
return false;

View File

@ -29,7 +29,7 @@
namespace lld {
/// This is where the link is actually performed.
bool Driver::link(const TargetInfo &targetInfo, raw_ostream &diagnostics) {
bool Driver::link(const TargetInfo &targetInfo) {
// Honor -mllvm
if (!targetInfo.llvmOptions().empty()) {
unsigned numArgs = targetInfo.llvmOptions().size();
@ -52,10 +52,10 @@ bool Driver::link(const TargetInfo &targetInfo, raw_ostream &diagnostics) {
if (targetInfo.logInputFiles())
llvm::outs() << input.getPath() << "\n";
tg.spawn([&, index] {
tg.spawn([ &, index]{
if (error_code ec = targetInfo.readFile(input.getPath(), files[index])) {
diagnostics << "Failed to read file: " << input.getPath()
<< ": " << ec.message() << "\n";
llvm::errs() << "Failed to read file: " << input.getPath() << ": "
<< ec.message() << "\n";
fail = true;
return;
}
@ -98,8 +98,8 @@ bool Driver::link(const TargetInfo &targetInfo, raw_ostream &diagnostics) {
// Give linked atoms to Writer to generate output file.
ScopedTask writeTask(getDefaultDomain(), "Write");
if (error_code ec = targetInfo.writeFile(merged)) {
diagnostics << "Failed to write file '" << targetInfo.outputPath()
<< "': " << ec.message() << "\n";
llvm::errs() << "Failed to write file '" << targetInfo.outputPath()
<< "': " << ec.message() << "\n";
return true;
}

View File

@ -70,23 +70,19 @@ public:
} // namespace
bool GnuLdDriver::linkELF(int argc, const char *argv[],
raw_ostream &diagnostics) {
bool GnuLdDriver::linkELF(int argc, const char *argv[]) {
std::unique_ptr<ELFTargetInfo> options;
bool error = parse(argc, argv, options, diagnostics);
bool error = parse(argc, argv, options);
if (error)
return true;
if (!options)
return false;
return link(*options, diagnostics);
return link(*options);
}
bool GnuLdDriver::parse(int argc, const char *argv[],
std::unique_ptr<ELFTargetInfo> &targetInfo,
raw_ostream &diagnostics) {
std::unique_ptr<ELFTargetInfo> &targetInfo) {
// Parse command line options using LDOptions.td
std::unique_ptr<llvm::opt::InputArgList> parsedArgs;
GnuLdOptTable table;
@ -95,17 +91,16 @@ bool GnuLdDriver::parse(int argc, const char *argv[],
parsedArgs.reset(
table.ParseArgs(&argv[1], &argv[argc], missingIndex, missingCount));
if (missingCount) {
diagnostics << "error: missing arg value for '"
<< parsedArgs->getArgString(missingIndex) << "' expected "
<< missingCount << " argument(s).\n";
llvm::errs() << "error: missing arg value for '"
<< parsedArgs->getArgString(missingIndex) << "' expected "
<< missingCount << " argument(s).\n";
return true;
}
for (auto it = parsedArgs->filtered_begin(OPT_UNKNOWN),
ie = parsedArgs->filtered_end(); it != ie; ++it) {
diagnostics << "warning: ignoring unknown argument: " << (*it)->getAsString(
*parsedArgs)
<< "\n";
llvm::errs() << "warning: ignoring unknown argument: "
<< (*it)->getAsString(*parsedArgs) << "\n";
}
// Handle --help
@ -123,7 +118,7 @@ bool GnuLdDriver::parse(int argc, const char *argv[],
std::unique_ptr<ELFTargetInfo> options(ELFTargetInfo::create(triple));
if (!options) {
diagnostics << "unknown target triple\n";
llvm::errs() << "unknown target triple\n";
return true;
}
@ -253,8 +248,8 @@ bool GnuLdDriver::parse(int argc, const char *argv[],
break;
case OPT_l:
if (options->appendLibrary((*it)->getValue())) {
diagnostics << "Failed to find library for " << (*it)->getValue()
<< "\n";
llvm::errs() << "Failed to find library for " << (*it)->getValue()
<< "\n";
return true;
}
break;
@ -264,7 +259,7 @@ bool GnuLdDriver::parse(int argc, const char *argv[],
}
// Validate the combination of options used.
if (options->validate(diagnostics))
if (options->validate(llvm::errs()))
return true;
targetInfo.swap(options);

View File

@ -80,7 +80,7 @@ ProgramNameParts parseProgramName(StringRef programName) {
return ret;
}
Flavor selectFlavor(std::vector<const char *> &args, raw_ostream &diag) {
Flavor selectFlavor(std::vector<const char *> &args) {
// -core as first arg is shorthand for -flavor core.
if (args.size() > 1 && StringRef(args[1]) == "-core") {
args.erase(args.begin() + 1);
@ -92,7 +92,7 @@ Flavor selectFlavor(std::vector<const char *> &args, raw_ostream &diag) {
args.erase(args.begin() + 1);
args.erase(args.begin() + 1);
if (flavor == Flavor::invalid)
diag << "error: '" << args[2] << "' invalid value for -flavor.\n";
llvm::errs() << "error: '" << args[2] << "' invalid value for -flavor.\n";
return flavor;
}
@ -101,33 +101,32 @@ Flavor selectFlavor(std::vector<const char *> &args, raw_ostream &diag) {
// If flavor still undetermined, then error out.
if (flavor == Flavor::invalid)
diag << "error: failed to determine driver flavor from program name"
<< " '" << args[0] << "'.\n"
<< "select a flavor with -flavor [gnu|darwin|link|core].\n";
llvm::errs() << "error: failed to determine driver flavor from program name"
<< " '" << args[0] << "'.\n"
<< "select a flavor with -flavor [gnu|darwin|link|core].\n";
return flavor;
}
}
namespace lld {
bool UniversalDriver::link(int argc, const char *argv[],
raw_ostream &diagnostics) {
bool UniversalDriver::link(int argc, const char *argv[]) {
// Convert argv[] C-array to vector.
std::vector<const char *> args(argv, argv + argc);
// Determine flavor of link based on command name or -flavor argument.
// Note: 'args' is modified to remove -flavor option.
Flavor flavor = selectFlavor(args, diagnostics);
Flavor flavor = selectFlavor(args);
// Switch to appropriate driver.
switch (flavor) {
case Flavor::gnu_ld:
return GnuLdDriver::linkELF(args.size(), args.data(), diagnostics);
return GnuLdDriver::linkELF(args.size(), args.data());
case Flavor::darwin_ld:
return DarwinLdDriver::linkMachO(args.size(), args.data(), diagnostics);
return DarwinLdDriver::linkMachO(args.size(), args.data());
case Flavor::win_link:
return WinLinkDriver::linkPECOFF(args.size(), args.data(), diagnostics);
return WinLinkDriver::linkPECOFF(args.size(), args.data());
case Flavor::core:
return CoreDriver::link(args.size(), args.data(), diagnostics);
return CoreDriver::link(args.size(), args.data());
case Flavor::invalid:
return true;
}

View File

@ -185,16 +185,15 @@ StringRef getDefaultOutputFileName(PECOFFTargetInfo &info, std::string path) {
} // namespace
bool WinLinkDriver::linkPECOFF(int argc, const char *argv[],
raw_ostream &diagnostics) {
bool WinLinkDriver::linkPECOFF(int argc, const char *argv[]) {
PECOFFTargetInfo info;
if (parse(argc, argv, info, diagnostics))
if (parse(argc, argv, info))
return true;
return link(info, diagnostics);
return link(info);
}
bool WinLinkDriver::parse(int argc, const char *argv[],
PECOFFTargetInfo &info, raw_ostream &diagnostics) {
PECOFFTargetInfo &info) {
// Arguments after "--" are interpreted as filenames even if they start with
// a hyphen or a slash. This is not compatible with link.exe but useful for
// us to test lld on Unix.
@ -209,9 +208,9 @@ bool WinLinkDriver::parse(int argc, const char *argv[],
parsedArgs.reset(
table.ParseArgs(&argv[1], &argv[argEnd], missingIndex, missingCount));
if (missingCount) {
diagnostics << "error: missing arg value for '"
<< parsedArgs->getArgString(missingIndex) << "' expected "
<< missingCount << " argument(s).\n";
llvm::errs() << "error: missing arg value for '"
<< parsedArgs->getArgString(missingIndex) << "' expected "
<< missingCount << " argument(s).\n";
return true;
}
@ -224,8 +223,8 @@ bool WinLinkDriver::parse(int argc, const char *argv[],
// Show warning for unknown arguments
for (auto it = parsedArgs->filtered_begin(OPT_UNKNOWN),
ie = parsedArgs->filtered_end(); it != ie; ++it) {
diagnostics << "warning: ignoring unknown argument: "
<< (*it)->getAsString(*parsedArgs) << "\n";
llvm::errs() << "warning: ignoring unknown argument: "
<< (*it)->getAsString(*parsedArgs) << "\n";
}
// Copy -mllvm
@ -237,17 +236,17 @@ bool WinLinkDriver::parse(int argc, const char *argv[],
// Handle -stack
if (llvm::opt::Arg *arg = parsedArgs->getLastArg(OPT_stack))
if (!parseStackOption(info, arg->getValue(), diagnostics))
if (!parseStackOption(info, arg->getValue(), llvm::errs()))
return true;
// Handle -heap
if (llvm::opt::Arg *arg = parsedArgs->getLastArg(OPT_heap))
if (!parseHeapOption(info, arg->getValue(), diagnostics))
if (!parseHeapOption(info, arg->getValue(), llvm::errs()))
return true;
// Handle -subsystem
if (llvm::opt::Arg *arg = parsedArgs->getLastArg(OPT_subsystem))
if (!parseSubsystemOption(info, arg->getValue(), diagnostics))
if (!parseSubsystemOption(info, arg->getValue(), llvm::errs()))
return true;
// Handle -entry
@ -289,7 +288,7 @@ bool WinLinkDriver::parse(int argc, const char *argv[],
info.setOutputPath(getDefaultOutputFileName(info, inputPaths[0]));
// Validate the combination of options used.
return info.validate(diagnostics);
return info.validate(llvm::errs());
}
} // namespace lld