diff options
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Support/CommandLine.cpp')
| -rw-r--r-- | contrib/llvm-project/llvm/lib/Support/CommandLine.cpp | 211 |
1 files changed, 58 insertions, 153 deletions
diff --git a/contrib/llvm-project/llvm/lib/Support/CommandLine.cpp b/contrib/llvm-project/llvm/lib/Support/CommandLine.cpp index cb73380ba383..25510fa58ff5 100644 --- a/contrib/llvm-project/llvm/lib/Support/CommandLine.cpp +++ b/contrib/llvm-project/llvm/lib/Support/CommandLine.cpp @@ -24,13 +24,11 @@ #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringMap.h" -#include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" #include "llvm/ADT/Twine.h" #include "llvm/Config/config.h" #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Host.h" @@ -39,11 +37,9 @@ #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" #include "llvm/Support/StringSaver.h" -#include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/raw_ostream.h" #include <cstdlib> #include <map> -#include <string> using namespace llvm; using namespace cl; @@ -57,8 +53,6 @@ namespace cl { template class basic_parser<bool>; template class basic_parser<boolOrDefault>; template class basic_parser<int>; -template class basic_parser<long>; -template class basic_parser<long long>; template class basic_parser<unsigned>; template class basic_parser<unsigned long>; template class basic_parser<unsigned long long>; @@ -84,8 +78,6 @@ void basic_parser_impl::anchor() {} void parser<bool>::anchor() {} void parser<boolOrDefault>::anchor() {} void parser<int>::anchor() {} -void parser<long>::anchor() {} -void parser<long long>::anchor() {} void parser<unsigned>::anchor() {} void parser<unsigned long>::anchor() {} void parser<unsigned long long>::anchor() {} @@ -96,26 +88,21 @@ void parser<char>::anchor() {} //===----------------------------------------------------------------------===// -const static size_t DefaultPad = 2; - -static StringRef ArgPrefix = "-"; -static StringRef ArgPrefixLong = "--"; +static StringRef ArgPrefix = " -"; +static StringRef ArgPrefixLong = " --"; static StringRef ArgHelpPrefix = " - "; -static size_t argPlusPrefixesSize(StringRef ArgName, size_t Pad = DefaultPad) { +static size_t argPlusPrefixesSize(StringRef ArgName) { size_t Len = ArgName.size(); if (Len == 1) - return Len + Pad + ArgPrefix.size() + ArgHelpPrefix.size(); - return Len + Pad + ArgPrefixLong.size() + ArgHelpPrefix.size(); + return Len + ArgPrefix.size() + ArgHelpPrefix.size(); + return Len + ArgPrefixLong.size() + ArgHelpPrefix.size(); } -static SmallString<8> argPrefix(StringRef ArgName, size_t Pad = DefaultPad) { - SmallString<8> Prefix; - for (size_t I = 0; I < Pad; ++I) { - Prefix.push_back(' '); - } - Prefix.append(ArgName.size() > 1 ? ArgPrefixLong : ArgPrefix); - return Prefix; +static StringRef argPrefix(StringRef ArgName) { + if (ArgName.size() == 1) + return ArgPrefix; + return ArgPrefixLong; } // Option predicates... @@ -132,14 +119,13 @@ namespace { class PrintArg { StringRef ArgName; - size_t Pad; public: - PrintArg(StringRef ArgName, size_t Pad = DefaultPad) : ArgName(ArgName), Pad(Pad) {} - friend raw_ostream &operator<<(raw_ostream &OS, const PrintArg &); + PrintArg(StringRef ArgName) : ArgName(ArgName) {} + friend raw_ostream &operator<<(raw_ostream &OS, const PrintArg&); }; raw_ostream &operator<<(raw_ostream &OS, const PrintArg& Arg) { - OS << argPrefix(Arg.ArgName, Arg.Pad) << Arg.ArgName; + OS << argPrefix(Arg.ArgName) << Arg.ArgName; return OS; } @@ -187,7 +173,7 @@ public: // If we're adding this to all sub-commands, add it to the ones that have // already been registered. if (SC == &*AllSubCommands) { - for (auto *Sub : RegisteredSubCommands) { + for (const auto &Sub : RegisteredSubCommands) { if (SC == Sub) continue; addLiteralOption(Opt, Sub, Name); @@ -243,7 +229,7 @@ public: // If we're adding this to all sub-commands, add it to the ones that have // already been registered. if (SC == &*AllSubCommands) { - for (auto *Sub : RegisteredSubCommands) { + for (const auto &Sub : RegisteredSubCommands) { if (SC == Sub) continue; addOption(O, Sub); @@ -318,7 +304,7 @@ public: } bool hasOptions() const { - for (const auto *S : RegisteredSubCommands) { + for (const auto &S : RegisteredSubCommands) { if (hasOptions(*S)) return true; } @@ -706,7 +692,7 @@ static inline bool ProvideOption(Option *Handler, StringRef ArgName, return false; } -bool llvm::cl::ProvidePositionalOption(Option *Handler, StringRef Arg, int i) { +static bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i) { int Dummy = i; return ProvideOption(Handler, Handler->ArgStr, Arg, 0, nullptr, Dummy); } @@ -1051,16 +1037,14 @@ static bool hasUTF8ByteOrderMark(ArrayRef<char> S) { return (S.size() >= 3 && S[0] == '\xef' && S[1] == '\xbb' && S[2] == '\xbf'); } -// FName must be an absolute path. -static llvm::Error ExpandResponseFile( - StringRef FName, StringSaver &Saver, TokenizerCallback Tokenizer, - SmallVectorImpl<const char *> &NewArgv, bool MarkEOLs, bool RelativeNames, - llvm::vfs::FileSystem &FS) { - assert(sys::path::is_absolute(FName)); - llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr = - FS.getBufferForFile(FName); +static bool ExpandResponseFile(StringRef FName, StringSaver &Saver, + TokenizerCallback Tokenizer, + SmallVectorImpl<const char *> &NewArgv, + bool MarkEOLs, bool RelativeNames) { + ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr = + MemoryBuffer::getFile(FName); if (!MemBufOrErr) - return llvm::errorCodeToError(MemBufOrErr.getError()); + return false; MemoryBuffer &MemBuf = *MemBufOrErr.get(); StringRef Str(MemBuf.getBufferStart(), MemBuf.getBufferSize()); @@ -1069,8 +1053,7 @@ static llvm::Error ExpandResponseFile( std::string UTF8Buf; if (hasUTF16ByteOrderMark(BufRef)) { if (!convertUTF16ToUTF8String(BufRef, UTF8Buf)) - return llvm::createStringError(std::errc::illegal_byte_sequence, - "Could not convert UTF16 to UTF8"); + return false; Str = StringRef(UTF8Buf); } // If we see UTF-8 BOM sequence at the beginning of a file, we shall remove @@ -1082,40 +1065,41 @@ static llvm::Error ExpandResponseFile( // Tokenize the contents into NewArgv. Tokenizer(Str, Saver, NewArgv, MarkEOLs); - if (!RelativeNames) - return Error::success(); - llvm::StringRef BasePath = llvm::sys::path::parent_path(FName); // If names of nested response files should be resolved relative to including // file, replace the included response file names with their full paths // obtained by required resolution. - for (auto &Arg : NewArgv) { - // Skip non-rsp file arguments. - if (!Arg || Arg[0] != '@') - continue; - - StringRef FileName(Arg + 1); - // Skip if non-relative. - if (!llvm::sys::path::is_relative(FileName)) - continue; + if (RelativeNames) + for (unsigned I = 0; I < NewArgv.size(); ++I) + if (NewArgv[I]) { + StringRef Arg = NewArgv[I]; + if (Arg.front() == '@') { + StringRef FileName = Arg.drop_front(); + if (llvm::sys::path::is_relative(FileName)) { + SmallString<128> ResponseFile; + ResponseFile.append(1, '@'); + if (llvm::sys::path::is_relative(FName)) { + SmallString<128> curr_dir; + llvm::sys::fs::current_path(curr_dir); + ResponseFile.append(curr_dir.str()); + } + llvm::sys::path::append( + ResponseFile, llvm::sys::path::parent_path(FName), FileName); + NewArgv[I] = Saver.save(ResponseFile.c_str()).data(); + } + } + } - SmallString<128> ResponseFile; - ResponseFile.push_back('@'); - ResponseFile.append(BasePath); - llvm::sys::path::append(ResponseFile, FileName); - Arg = Saver.save(ResponseFile.c_str()).data(); - } - return Error::success(); + return true; } /// Expand response files on a command line recursively using the given /// StringSaver and tokenization strategy. bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, - SmallVectorImpl<const char *> &Argv, bool MarkEOLs, - bool RelativeNames, llvm::vfs::FileSystem &FS, - llvm::Optional<llvm::StringRef> CurrentDir) { + SmallVectorImpl<const char *> &Argv, + bool MarkEOLs, bool RelativeNames) { bool AllExpanded = true; struct ResponseFileRecord { - std::string File; + const char *File; size_t End; }; @@ -1149,31 +1133,8 @@ bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, } const char *FName = Arg + 1; - // Note that CurrentDir is only used for top-level rsp files, the rest will - // always have an absolute path deduced from the containing file. - SmallString<128> CurrDir; - if (llvm::sys::path::is_relative(FName)) { - if (!CurrentDir) - llvm::sys::fs::current_path(CurrDir); - else - CurrDir = *CurrentDir; - llvm::sys::path::append(CurrDir, FName); - FName = CurrDir.c_str(); - } - auto IsEquivalent = [FName, &FS](const ResponseFileRecord &RFile) { - llvm::ErrorOr<llvm::vfs::Status> LHS = FS.status(FName); - if (!LHS) { - // TODO: The error should be propagated up the stack. - llvm::consumeError(llvm::errorCodeToError(LHS.getError())); - return false; - } - llvm::ErrorOr<llvm::vfs::Status> RHS = FS.status(RFile.File); - if (!RHS) { - // TODO: The error should be propagated up the stack. - llvm::consumeError(llvm::errorCodeToError(RHS.getError())); - return false; - } - return LHS->equivalent(*RHS); + auto IsEquivalent = [FName](const ResponseFileRecord &RFile) { + return sys::fs::equivalent(RFile.File, FName); }; // Check for recursive response files. @@ -1188,13 +1149,10 @@ bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, // Replace this response file argument with the tokenization of its // contents. Nested response files are expanded in subsequent iterations. SmallVector<const char *, 0> ExpandedArgv; - if (llvm::Error Err = - ExpandResponseFile(FName, Saver, Tokenizer, ExpandedArgv, MarkEOLs, - RelativeNames, FS)) { + if (!ExpandResponseFile(FName, Saver, Tokenizer, ExpandedArgv, MarkEOLs, + RelativeNames)) { // We couldn't read this file, so we leave it in the argument stream and // move on. - // TODO: The error should be propagated up the stack. - llvm::consumeError(std::move(Err)); AllExpanded = false; ++I; continue; @@ -1222,20 +1180,9 @@ bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, bool cl::readConfigFile(StringRef CfgFile, StringSaver &Saver, SmallVectorImpl<const char *> &Argv) { - SmallString<128> AbsPath; - if (sys::path::is_relative(CfgFile)) { - llvm::sys::fs::current_path(AbsPath); - llvm::sys::path::append(AbsPath, CfgFile); - CfgFile = AbsPath.str(); - } - if (llvm::Error Err = - ExpandResponseFile(CfgFile, Saver, cl::tokenizeConfigFile, Argv, - /*MarkEOLs*/ false, /*RelativeNames*/ true, - *llvm::vfs::getRealFileSystem())) { - // TODO: The error should be propagated up the stack. - llvm::consumeError(std::move(Err)); + if (!ExpandResponseFile(CfgFile, Saver, cl::tokenizeConfigFile, Argv, + /*MarkEOLs*/ false, /*RelativeNames*/ true)) return false; - } return ExpandResponseFiles(Saver, cl::tokenizeConfigFile, Argv, /*MarkEOLs*/ false, /*RelativeNames*/ true); } @@ -1500,7 +1447,7 @@ bool CommandLineParser::ParseCommandLineOptions(int argc, if (NearestHandler) { // If we know a near match, report it as well. *Errs << ProgramName << ": Did you mean '" - << PrintArg(NearestHandlerString, 0) << "'?\n"; + << PrintArg(NearestHandlerString) << "'?\n"; } ErrorParsing = true; @@ -1654,7 +1601,7 @@ bool Option::error(const Twine &Message, StringRef ArgName, raw_ostream &Errs) { if (ArgName.empty()) Errs << HelpStr; // Be nice for positional arguments else - Errs << GlobalParser->ProgramName << ": for the " << PrintArg(ArgName, 0); + Errs << GlobalParser->ProgramName << ": for the " << PrintArg(ArgName); Errs << " option: " << Message << "\n"; return true; @@ -1810,24 +1757,6 @@ bool parser<int>::parse(Option &O, StringRef ArgName, StringRef Arg, return false; } -// parser<long> implementation -// -bool parser<long>::parse(Option &O, StringRef ArgName, StringRef Arg, - long &Value) { - if (Arg.getAsInteger(0, Value)) - return O.error("'" + Arg + "' value invalid for long argument!"); - return false; -} - -// parser<long long> implementation -// -bool parser<long long>::parse(Option &O, StringRef ArgName, StringRef Arg, - long long &Value) { - if (Arg.getAsInteger(0, Value)) - return O.error("'" + Arg + "' value invalid for llong argument!"); - return false; -} - // parser<unsigned> implementation // bool parser<unsigned>::parse(Option &O, StringRef ArgName, StringRef Arg, @@ -2037,8 +1966,6 @@ void generic_parser_base::printGenericOptionDiff( PRINT_OPT_DIFF(bool) PRINT_OPT_DIFF(boolOrDefault) PRINT_OPT_DIFF(int) -PRINT_OPT_DIFF(long) -PRINT_OPT_DIFF(long long) PRINT_OPT_DIFF(unsigned) PRINT_OPT_DIFF(unsigned long) PRINT_OPT_DIFF(unsigned long long) @@ -2112,7 +2039,7 @@ static void sortOpts(StringMap<Option *> &OptMap, static void sortSubCommands(const SmallPtrSetImpl<SubCommand *> &SubMap, SmallVectorImpl<std::pair<const char *, SubCommand *>> &Subs) { - for (auto *S : SubMap) { + for (const auto &S : SubMap) { if (S->getName().empty()) continue; Subs.push_back(std::make_pair(S->getName().data(), S)); @@ -2436,28 +2363,6 @@ static VersionPrinterTy OverrideVersionPrinter = nullptr; static std::vector<VersionPrinterTy> *ExtraVersionPrinters = nullptr; -#if defined(__GNUC__) -// GCC and GCC-compatible compilers define __OPTIMIZE__ when optimizations are -// enabled. -# if defined(__OPTIMIZE__) -# define LLVM_IS_DEBUG_BUILD 0 -# else -# define LLVM_IS_DEBUG_BUILD 1 -# endif -#elif defined(_MSC_VER) -// MSVC doesn't have a predefined macro indicating if optimizations are enabled. -// Use _DEBUG instead. This macro actually corresponds to the choice between -// debug and release CRTs, but it is a reasonable proxy. -# if defined(_DEBUG) -# define LLVM_IS_DEBUG_BUILD 1 -# else -# define LLVM_IS_DEBUG_BUILD 0 -# endif -#else -// Otherwise, for an unknown compiler, assume this is an optimized build. -# define LLVM_IS_DEBUG_BUILD 0 -#endif - namespace { class VersionPrinter { public: @@ -2473,7 +2378,7 @@ public: OS << " " << LLVM_VERSION_INFO; #endif OS << "\n "; -#if LLVM_IS_DEBUG_BUILD +#ifndef __OPTIMIZE__ OS << "DEBUG build"; #else OS << "Optimized build"; |
