From: Rui Ueyama Date: Thu, 28 May 2015 20:30:06 +0000 (+0000) Subject: COFF: Split Driver.cpp to Driver.cpp and DriverUtils.cpp. NFC. X-Git-Tag: llvmorg-3.7.0-rc1~3550 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3500f6667a9e40bbacb17c9c70a4b91fe638385b;p=platform%2Fupstream%2Fllvm.git COFF: Split Driver.cpp to Driver.cpp and DriverUtils.cpp. NFC. The previous implementation's driver file is cluttered by lots of small functions, and it was hard to find important functions. Make a separate file to prevent that issue. llvm-svn: 238482 --- diff --git a/lld/COFF/CMakeLists.txt b/lld/COFF/CMakeLists.txt index eab8c3e..b089833 100644 --- a/lld/COFF/CMakeLists.txt +++ b/lld/COFF/CMakeLists.txt @@ -5,6 +5,7 @@ add_public_tablegen_target(COFFOptionsTableGen) add_llvm_library(lldCOFF Chunks.cpp Driver.cpp + DriverUtils.cpp InputFiles.cpp SymbolTable.cpp Symbols.cpp diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index 739494e..9336506 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -16,67 +16,21 @@ #include "lld/Core/Error.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/Object/COFF.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/Format.h" #include "llvm/Support/Path.h" -#include "llvm/Support/Process.h" #include "llvm/Support/raw_ostream.h" #include using namespace llvm; -// Create enum with OPT_xxx values for each option in Options.td -enum { - OPT_INVALID = 0, -#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ - HELP, META) \ - OPT_##ID, -#include "Options.inc" -#undef OPTION -}; - -// Create prefix string literals used in Options.td -#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; -#include "Options.inc" -#undef PREFIX - -// Create table mapping all options defined in Options.td -static const llvm::opt::OptTable::Info infoTable[] = { -#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ - HELPTEXT, METAVAR) \ - { \ - PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, llvm::opt::Option::KIND##Class, \ - PARAM, FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS \ - } \ - , -#include "Options.inc" -#undef OPTION -}; - -namespace { - -class COFFOptTable : public llvm::opt::OptTable { -public: - COFFOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {} -}; - -class BumpPtrStringSaver : public llvm::cl::StringSaver { -public: - BumpPtrStringSaver(lld::coff::StringAllocator *A) : Alloc(A) {} - - const char *SaveString(const char *S) override { - return Alloc->save(S).data(); - } +namespace lld { +namespace coff { -private: - lld::coff::StringAllocator *Alloc; -}; -} +Configuration *Config; static std::string getOutputPath(llvm::opt::InputArgList *Args) { if (auto *Arg = Args->getLastArg(OPT_out)) @@ -91,86 +45,23 @@ static std::string getOutputPath(llvm::opt::InputArgList *Args) { llvm_unreachable("internal error"); } -// Split the given string with the path separator. -static std::vector splitPathList(StringRef str) { - std::vector ret; - while (!str.empty()) { - StringRef path; - std::tie(path, str) = str.split(';'); - ret.push_back(path); - } - return ret; -} - -namespace lld { -namespace coff { - -Configuration *Config; - -ErrorOr> -parseArgs(int Argc, const char *Argv[]) { - COFFOptTable Table; - unsigned MissingIndex; - unsigned MissingCount; - std::unique_ptr Args( - Table.ParseArgs(&Argv[1], &Argv[Argc], MissingIndex, MissingCount)); - if (MissingCount) { - std::string S; - llvm::raw_string_ostream OS(S); - OS << llvm::format("missing arg value for \"%s\", expected %d argument%s.", - Args->getArgString(MissingIndex), MissingCount, - (MissingCount == 1 ? "" : "s")); - OS.flush(); - return make_dynamic_error_code(StringRef(S)); - } - for (auto *Arg : Args->filtered(OPT_UNKNOWN)) - llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n"; - return std::move(Args); -} - -std::string findLib(StringRef Filename) { - if (llvm::sys::fs::exists(Filename)) - return Filename; - std::string Name; - if (Filename.endswith_lower(".lib")) { - Name = Filename; - } else { - Name = (Filename + ".lib").str(); - } - - llvm::Optional Env = llvm::sys::Process::GetEnv("LIB"); - if (!Env.hasValue()) - return Filename; - for (StringRef Dir : splitPathList(*Env)) { - SmallString<128> Path = Dir; - llvm::sys::path::append(Path, Name); - if (llvm::sys::fs::exists(Path.str())) - return Path.str(); - } - return Filename; -} - -std::string findFile(StringRef Filename) { - if (llvm::sys::fs::exists(Filename)) - return Filename; - llvm::Optional Env = llvm::sys::Process::GetEnv("LIB"); - if (!Env.hasValue()) - return Filename; - for (StringRef Dir : splitPathList(*Env)) { - SmallString<128> Path = Dir; - llvm::sys::path::append(Path, Filename); - if (llvm::sys::fs::exists(Path.str())) - return Path.str(); - } - return Filename; -} - std::unique_ptr createFile(StringRef Path) { if (StringRef(Path).endswith_lower(".lib")) return llvm::make_unique(Path); return llvm::make_unique(Path); } +namespace { +class BumpPtrStringSaver : public llvm::cl::StringSaver { +public: + BumpPtrStringSaver(lld::coff::StringAllocator *A) : Alloc(A) {} + const char *SaveString(const char *S) override { + return Alloc->save(S).data(); + } + lld::coff::StringAllocator *Alloc; +}; +} + // Parses .drectve section contents and returns a list of files // specified by /defaultlib. std::error_code parseDirectives(StringRef S, diff --git a/lld/COFF/Driver.h b/lld/COFF/Driver.h index 43c88a5..8743c3b 100644 --- a/lld/COFF/Driver.h +++ b/lld/COFF/Driver.h @@ -12,6 +12,9 @@ #include "Memory.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Object/COFF.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" #include #include #include @@ -21,10 +24,27 @@ namespace coff { class InputFile; +ErrorOr> +parseArgs(int Argc, const char *Argv[]); + std::error_code parseDirectives(StringRef S, std::vector> *Res, StringAllocator *Alloc); +// Functions below this line are defined in DriverUtils.cpp. + +// "ENV" environment variable-aware file finders. +std::string findLib(StringRef Filename); +std::string findFile(StringRef Filename); + +// Create enum with OPT_xxx values for each option in Options.td +enum { + OPT_INVALID = 0, +#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID, +#include "Options.inc" +#undef OPTION +}; + } // namespace coff } // namespace lld diff --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp new file mode 100644 index 0000000..44319e7 --- /dev/null +++ b/lld/COFF/DriverUtils.cpp @@ -0,0 +1,135 @@ +//===- DriverUtils.cpp ----------------------------------------------------===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains utility functions for the driver. Because there +// are so many small functions, we created this separate file to make +// Driver.cpp less cluttered. +// +//===----------------------------------------------------------------------===// + +#include "Driver.h" +#include "Memory.h" +#include "lld/Core/Error.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/Object/COFF.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Process.h" +#include "llvm/Support/raw_ostream.h" +#include + +using namespace llvm::COFF; +using namespace llvm; +using llvm::sys::Process; +using llvm::sys::fs::file_magic; +using llvm::sys::fs::identify_magic; + +namespace lld { +namespace coff { + +// Split the given string with the path separator. +static std::vector splitPathList(StringRef str) { + std::vector ret; + while (!str.empty()) { + StringRef path; + std::tie(path, str) = str.split(';'); + ret.push_back(path); + } + return ret; +} + +std::string findLib(StringRef Filename) { + if (llvm::sys::fs::exists(Filename)) + return Filename; + std::string Name; + if (Filename.endswith_lower(".lib")) { + Name = Filename; + } else { + Name = (Filename + ".lib").str(); + } + + Optional Env = Process::GetEnv("LIB"); + if (!Env.hasValue()) + return Filename; + for (StringRef Dir : splitPathList(*Env)) { + SmallString<128> Path = Dir; + llvm::sys::path::append(Path, Name); + if (llvm::sys::fs::exists(Path.str())) + return Path.str(); + } + return Filename; +} + +std::string findFile(StringRef Filename) { + if (llvm::sys::fs::exists(Filename)) + return Filename; + Optional Env = Process::GetEnv("LIB"); + if (!Env.hasValue()) + return Filename; + for (StringRef Dir : splitPathList(*Env)) { + SmallString<128> Path = Dir; + llvm::sys::path::append(Path, Filename); + if (llvm::sys::fs::exists(Path.str())) + return Path.str(); + } + return Filename; +} + +// Create OptTable + +// Create prefix string literals used in Options.td +#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#include "Options.inc" +#undef PREFIX + +// Create table mapping all options defined in Options.td +static const llvm::opt::OptTable::Info infoTable[] = { +#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ + { \ + X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \ + OPT_##GROUP, OPT_##ALIAS, X6 \ + }, +#include "Options.inc" +#undef OPTION +}; + +class COFFOptTable : public llvm::opt::OptTable { +public: + COFFOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {} +}; + +ErrorOr> +parseArgs(int Argc, const char *Argv[]) { + COFFOptTable Table; + unsigned MissingIndex; + unsigned MissingCount; + std::unique_ptr Args( + Table.ParseArgs(&Argv[1], &Argv[Argc], MissingIndex, MissingCount)); + if (MissingCount) { + std::string S; + llvm::raw_string_ostream OS(S); + OS << llvm::format("missing arg value for \"%s\", expected %d argument%s.", + Args->getArgString(MissingIndex), MissingCount, + (MissingCount == 1 ? "" : "s")); + OS.flush(); + return make_dynamic_error_code(StringRef(S)); + } + for (auto *Arg : Args->filtered(OPT_UNKNOWN)) + llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n"; + return std::move(Args); +} + +} // namespace coff +} // namespace lld