diff options
author | David Blaikie <dblaikie@gmail.com> | 2015-06-21 06:32:10 +0000 |
---|---|---|
committer | David Blaikie <dblaikie@gmail.com> | 2015-06-21 06:32:10 +0000 |
commit | b2b1c7c3e1d82147421b6df5fa1417675ce271a8 (patch) | |
tree | a7706e484a1d3d6b60ca4e3e38bfed954e09fafe /lld/lib/Driver/WinLinkDriver.cpp | |
parent | 8da889f1a5c5826ed56c07d0182634296598bee3 (diff) | |
download | bcm5719-llvm-b2b1c7c3e1d82147421b6df5fa1417675ce271a8.tar.gz bcm5719-llvm-b2b1c7c3e1d82147421b6df5fa1417675ce271a8.zip |
ArrayRef-ify Driver::parse and related functions.
llvm-svn: 240236
Diffstat (limited to 'lld/lib/Driver/WinLinkDriver.cpp')
-rw-r--r-- | lld/lib/Driver/WinLinkDriver.cpp | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/lld/lib/Driver/WinLinkDriver.cpp b/lld/lib/Driver/WinLinkDriver.cpp index d3c1e347c7b..cda35f425d9 100644 --- a/lld/lib/Driver/WinLinkDriver.cpp +++ b/lld/lib/Driver/WinLinkDriver.cpp @@ -666,12 +666,12 @@ handleFailIfMismatchOption(StringRef option, // Process "LINK" environment variable. If defined, the value of the variable // should be processed as command line arguments. static std::vector<const char *> processLinkEnv(PECOFFLinkingContext &ctx, - int argc, const char **argv) { + llvm::ArrayRef<const char*> args) { std::vector<const char *> ret; // The first argument is the name of the command. This should stay at the head // of the argument list. - assert(argc > 0); - ret.push_back(argv[0]); + assert(!args.empty()); + ret.push_back(args[0]); // Add arguments specified by the LINK environment variable. llvm::Optional<std::string> env = llvm::sys::Process::GetEnv("LINK"); @@ -680,8 +680,8 @@ static std::vector<const char *> processLinkEnv(PECOFFLinkingContext &ctx, ret.push_back(ctx.allocate(arg).data()); // Add the rest of arguments passed via the command line. - for (int i = 1; i < argc; ++i) - ret.push_back(argv[i]); + for (const char* arg : args.slice(1)) + ret.push_back(arg); ret.push_back(nullptr); return ret; } @@ -713,16 +713,16 @@ static bool readResponseFile(StringRef path, PECOFFLinkingContext &ctx, // Expand arguments starting with "@". It's an error if a specified file does // not exist. Returns true on success. -static bool expandResponseFiles(int &argc, const char **&argv, +static bool expandResponseFiles(llvm::ArrayRef<const char*> &args, PECOFFLinkingContext &ctx, raw_ostream &diag, bool &expanded) { std::vector<const char *> newArgv; - for (int i = 0; i < argc; ++i) { - if (argv[i][0] != '@') { - newArgv.push_back(argv[i]); + for (const char *arg : args) { + if (arg[0] != '@') { + newArgv.push_back(arg); continue; } - StringRef filename = StringRef(argv[i] + 1); + StringRef filename = StringRef(arg + 1); if (!readResponseFile(filename, ctx, newArgv)) { diag << "error: cannot read response file: " << filename << "\n"; return false; @@ -731,20 +731,19 @@ static bool expandResponseFiles(int &argc, const char **&argv, } if (!expanded) return true; - argc = newArgv.size(); newArgv.push_back(nullptr); - argv = &ctx.allocateCopy(newArgv)[0]; + args = llvm::makeArrayRef(&ctx.allocateCopy(newArgv)[0], newArgv.size() - 1); return true; } // Parses the given command line options and returns the result. Returns NULL if // there's an error in the options. static std::unique_ptr<llvm::opt::InputArgList> -parseArgs(int argc, const char **argv, PECOFFLinkingContext &ctx, +parseArgs(llvm::ArrayRef<const char*> args, PECOFFLinkingContext &ctx, raw_ostream &diag, bool isReadingDirectiveSection) { // Expand arguments starting with "@". bool expanded = false; - if (!expandResponseFiles(argc, argv, ctx, diag, expanded)) + if (!expandResponseFiles(args, ctx, diag, expanded)) return nullptr; // Parse command line options using WinLinkOptions.td @@ -752,7 +751,7 @@ parseArgs(int argc, const char **argv, PECOFFLinkingContext &ctx, WinLinkOptTable table; unsigned missingIndex; unsigned missingCount; - parsedArgs.reset(table.ParseArgs(llvm::makeArrayRef(argv, argc).slice(1), + parsedArgs.reset(table.ParseArgs(args.slice(1), missingIndex, missingCount)); if (missingCount) { diag << "error: missing arg value for '" @@ -780,8 +779,8 @@ parseArgs(int argc, const char **argv, PECOFFLinkingContext &ctx, if (!isReadingDirectiveSection && expanded && parsedArgs->getLastArg(OPT_verbose)) { diag << "Command line:"; - for (int i = 0; i < argc; ++i) - diag << " " << argv[i]; + for (const char *arg : args) + diag << " " << arg; diag << "\n\n"; } @@ -802,10 +801,10 @@ static bool hasLibrary(PECOFFLinkingContext &ctx, File *file) { // If the first command line argument is "/lib", link.exe acts as if it's // "lib.exe" command. This is for backward compatibility. // http://msdn.microsoft.com/en-us/library/h34w59b3.aspx -static bool maybeRunLibCommand(int argc, const char **argv, raw_ostream &diag) { - if (argc <= 1) +static bool maybeRunLibCommand(llvm::ArrayRef<const char*> args, raw_ostream &diag) { + if (args.size() <= 1) return false; - if (!StringRef(argv[1]).equals_lower("/lib")) + if (!StringRef(args[1]).equals_lower("/lib")) return false; ErrorOr<std::string> pathOrErr = llvm::sys::findProgramByName("lib.exe"); if (!pathOrErr) { @@ -817,8 +816,8 @@ static bool maybeRunLibCommand(int argc, const char **argv, raw_ostream &diag) { // Run lib.exe std::vector<const char *> vec; vec.push_back(path.c_str()); - for (int i = 2; i < argc; ++i) - vec.push_back(argv[i]); + for (const char *arg : args.slice(2)) + vec.push_back(arg); vec.push_back(nullptr); if (llvm::sys::ExecuteAndWait(path.c_str(), &vec[0]) != 0) @@ -840,8 +839,8 @@ void addFiles(PECOFFLinkingContext &ctx, StringRef path, raw_ostream &diag, // Main driver // -bool WinLinkDriver::linkPECOFF(int argc, const char **argv, raw_ostream &diag) { - if (maybeRunLibCommand(argc, argv, diag)) +bool WinLinkDriver::linkPECOFF(llvm::ArrayRef<const char*> args, raw_ostream &diag) { + if (maybeRunLibCommand(args, diag)) return true; PECOFFLinkingContext ctx; @@ -851,9 +850,9 @@ bool WinLinkDriver::linkPECOFF(int argc, const char **argv, raw_ostream &diag) { ctx.registry().addSupportArchives(ctx.logInputFiles()); ctx.registry().addSupportYamlFiles(); - std::vector<const char *> newargv = processLinkEnv(ctx, argc, argv); + std::vector<const char *> newargv = processLinkEnv(ctx, args); processLibEnv(ctx); - if (!parse(newargv.size() - 1, &newargv[0], ctx, diag)) + if (!parse(llvm::makeArrayRef(newargv).drop_back(1), ctx, diag)) return false; // Create the file if needed. @@ -864,7 +863,7 @@ bool WinLinkDriver::linkPECOFF(int argc, const char **argv, raw_ostream &diag) { return link(ctx, diag); } -bool WinLinkDriver::parse(int argc, const char *argv[], +bool WinLinkDriver::parse(llvm::ArrayRef<const char*> args, PECOFFLinkingContext &ctx, raw_ostream &diag, bool isReadingDirectiveSection) { // Parse may be called from multiple threads simultaneously to parse .drectve @@ -875,7 +874,7 @@ bool WinLinkDriver::parse(int argc, const char *argv[], std::map<StringRef, StringRef> failIfMismatchMap; // Parse the options. std::unique_ptr<llvm::opt::InputArgList> parsedArgs = - parseArgs(argc, argv, ctx, diag, isReadingDirectiveSection); + parseArgs(args, ctx, diag, isReadingDirectiveSection); if (!parsedArgs) return false; @@ -886,7 +885,7 @@ bool WinLinkDriver::parse(int argc, const char *argv[], // Handle /help if (parsedArgs->hasArg(OPT_help)) { WinLinkOptTable table; - table.PrintHelp(llvm::outs(), argv[0], "LLVM Linker", false); + table.PrintHelp(llvm::outs(), args[0], "LLVM Linker", false); return false; } |