#include "llvm/Support/CommandLine.h"
#include "llvm-c/Support.h"
#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/StringSaver.h"
#include "llvm/Support/raw_ostream.h"
-#include <cerrno>
#include <cstdlib>
#include <map>
-#include <system_error>
using namespace llvm;
using namespace cl;
//
namespace llvm {
namespace cl {
-TEMPLATE_INSTANTIATION(class basic_parser<bool>);
-TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
-TEMPLATE_INSTANTIATION(class basic_parser<int>);
-TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
-TEMPLATE_INSTANTIATION(class basic_parser<unsigned long long>);
-TEMPLATE_INSTANTIATION(class basic_parser<double>);
-TEMPLATE_INSTANTIATION(class basic_parser<float>);
-TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
-TEMPLATE_INSTANTIATION(class basic_parser<char>);
-
-TEMPLATE_INSTANTIATION(class opt<unsigned>);
-TEMPLATE_INSTANTIATION(class opt<int>);
-TEMPLATE_INSTANTIATION(class opt<std::string>);
-TEMPLATE_INSTANTIATION(class opt<char>);
-TEMPLATE_INSTANTIATION(class opt<bool>);
+template class basic_parser<bool>;
+template class basic_parser<boolOrDefault>;
+template class basic_parser<int>;
+template class basic_parser<unsigned>;
+template class basic_parser<unsigned long long>;
+template class basic_parser<double>;
+template class basic_parser<float>;
+template class basic_parser<std::string>;
+template class basic_parser<char>;
+
+template class opt<unsigned>;
+template class opt<int>;
+template class opt<std::string>;
+template class opt<char>;
+template class opt<bool>;
}
} // end namespace llvm::cl
void parser<float>::anchor() {}
void parser<std::string>::anchor() {}
void parser<char>::anchor() {}
-void StringSaver::anchor() {}
//===----------------------------------------------------------------------===//
Option *ConsumeAfterOpt; // The ConsumeAfter option if it exists.
+ // This collects the different option categories that have been registered.
+ SmallPtrSet<OptionCategory *, 16> RegisteredOptionCategories;
+
CommandLineParser() : ProgramOverview(nullptr), ConsumeAfterOpt(nullptr) {}
void ParseCommandLineOptions(int argc, const char *const *argv,
}
void printOptionValues();
+
+ void registerCategory(OptionCategory *cat) {
+ assert(std::count_if(RegisteredOptionCategories.begin(),
+ RegisteredOptionCategories.end(),
+ [cat](const OptionCategory *Category) {
+ return cat->getName() == Category->getName();
+ }) == 0 &&
+ "Duplicate option categories");
+
+ RegisteredOptionCategories.insert(cat);
+ }
+
+private:
+ Option *LookupOption(StringRef &Arg, StringRef &Value);
};
} // namespace
ArgStr = S;
}
-// This collects the different option categories that have been registered.
-typedef SmallPtrSet<OptionCategory *, 16> OptionCatSet;
-static ManagedStatic<OptionCatSet> RegisteredOptionCategories;
-
// Initialise the general option category.
OptionCategory llvm::cl::GeneralCategory("General options");
void OptionCategory::registerCategory() {
- assert(std::count_if(RegisteredOptionCategories->begin(),
- RegisteredOptionCategories->end(),
- [this](const OptionCategory *Category) {
- return getName() == Category->getName();
- }) == 0 &&
- "Duplicate option categories");
-
- RegisteredOptionCategories->insert(this);
+ GlobalParser->registerCategory(this);
}
//===----------------------------------------------------------------------===//
/// LookupOption - Lookup the option specified by the specified option on the
/// command line. If there is a value specified (after an equal sign) return
/// that as well. This assumes that leading dashes have already been stripped.
-static Option *LookupOption(StringRef &Arg, StringRef &Value,
- const StringMap<Option *> &OptionsMap) {
+Option *CommandLineParser::LookupOption(StringRef &Arg, StringRef &Value) {
// Reject all dashes.
if (Arg.empty())
return nullptr;
if (RHS.empty() || !PermitValue)
NearestString = OptionNames[i];
else
- NearestString = std::string(OptionNames[i]) + "=" + RHS.str();
+ NearestString = (Twine(OptionNames[i]) + "=" + RHS).str();
}
}
}
// End the token if this is whitespace.
if (isWhitespace(Src[I])) {
if (!Token.empty())
- NewArgv.push_back(Saver.SaveString(Token.c_str()));
+ NewArgv.push_back(Saver.save(Token.c_str()));
Token.clear();
continue;
}
// Append the last token after hitting EOF with no whitespace.
if (!Token.empty())
- NewArgv.push_back(Saver.SaveString(Token.c_str()));
+ NewArgv.push_back(Saver.save(Token.c_str()));
// Mark the end of response files
if (MarkEOLs)
NewArgv.push_back(nullptr);
if (State == UNQUOTED) {
// Whitespace means the end of the token.
if (isWhitespace(Src[I])) {
- NewArgv.push_back(Saver.SaveString(Token.c_str()));
+ NewArgv.push_back(Saver.save(Token.c_str()));
Token.clear();
State = INIT;
// Mark the end of lines in response files
}
// Append the last token after hitting EOF with no whitespace.
if (!Token.empty())
- NewArgv.push_back(Saver.SaveString(Token.c_str()));
+ NewArgv.push_back(Saver.save(Token.c_str()));
// Mark the end of response files
if (MarkEOLs)
NewArgv.push_back(nullptr);
// It is called byte order marker but the UTF-8 BOM is actually not affected
// by the host system's endianness.
static bool hasUTF8ByteOrderMark(ArrayRef<char> S) {
- return (S.size() >= 3 &&
- S[0] == '\xef' && S[1] == '\xbb' && S[2] == '\xbf');
+ return (S.size() >= 3 && S[0] == '\xef' && S[1] == '\xbb' && S[2] == '\xbf');
}
static bool ExpandResponseFile(const char *FName, StringSaver &Saver,
return AllExpanded;
}
-namespace {
-class StrDupSaver : public StringSaver {
- std::vector<char *> Dups;
-
-public:
- ~StrDupSaver() {
- for (std::vector<char *>::iterator I = Dups.begin(), E = Dups.end(); I != E;
- ++I) {
- char *Dup = *I;
- free(Dup);
- }
- }
- const char *SaveString(const char *Str) override {
- char *Dup = strdup(Str);
- Dups.push_back(Dup);
- return Dup;
- }
-};
-}
-
/// ParseEnvironmentOptions - An alternative entry point to the
/// CommandLine library, which allows you to read the program's name
/// from the caller (as PROGNAME) and its command-line arguments from
// Get program's "name", which we wouldn't know without the caller
// telling us.
SmallVector<const char *, 20> newArgv;
- StrDupSaver Saver;
- newArgv.push_back(Saver.SaveString(progName));
+ BumpPtrAllocator A;
+ BumpPtrStringSaver Saver(A);
+ newArgv.push_back(Saver.save(progName));
// Parse the value of the environment variable into a "command line"
// and hand it off to ParseCommandLineOptions().
assert(hasOptions() && "No options specified!");
// Expand response files.
- SmallVector<const char *, 20> newArgv;
- for (int i = 0; i != argc; ++i)
- newArgv.push_back(argv[i]);
- StrDupSaver Saver;
+ SmallVector<const char *, 20> newArgv(argv, argv + argc);
+ BumpPtrAllocator A;
+ BumpPtrStringSaver Saver(A);
ExpandResponseFiles(Saver, TokenizeGNUCommandLine, newArgv);
argv = &newArgv[0];
argc = static_cast<int>(newArgv.size());
while (!ArgName.empty() && ArgName[0] == '-')
ArgName = ArgName.substr(1);
- Handler = LookupOption(ArgName, Value, OptionsMap);
+ Handler = LookupOption(ArgName, Value);
if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
ProvidePositionalOption(ActivePositionalArg, argv[i], i);
continue; // We are done!
while (!ArgName.empty() && ArgName[0] == '-')
ArgName = ArgName.substr(1);
- Handler = LookupOption(ArgName, Value, OptionsMap);
+ Handler = LookupOption(ArgName, Value);
// Check to see if this "option" is really a prefixed or grouped argument.
if (!Handler)
// -help and -help-hidden option implementation
//
-static int OptNameCompare(const void *LHS, const void *RHS) {
- typedef std::pair<const char *, Option *> pair_ty;
-
- return strcmp(((const pair_ty *)LHS)->first, ((const pair_ty *)RHS)->first);
+static int OptNameCompare(const std::pair<const char *, Option *> *LHS,
+ const std::pair<const char *, Option *> *RHS) {
+ return strcmp(LHS->first, RHS->first);
}
// Copy Options into a vector so we can sort them as we like.
}
// Sort the options list alphabetically.
- qsort(Opts.data(), Opts.size(), sizeof(Opts[0]), OptNameCompare);
+ array_pod_sort(Opts.begin(), Opts.end(), OptNameCompare);
}
namespace {
// Invoke the printer.
void operator=(bool Value) {
- if (Value == false)
+ if (!Value)
return;
StrOptionPairVector Opts;
explicit CategorizedHelpPrinter(bool showHidden) : HelpPrinter(showHidden) {}
// Helper function for printOptions().
- // It shall return true if A's name should be lexographically
- // ordered before B's name. It returns false otherwise.
- static bool OptionCategoryCompare(OptionCategory *A, OptionCategory *B) {
- return strcmp(A->getName(), B->getName()) < 0;
+ // It shall return a negative value if A's name should be lexicographically
+ // ordered before B's name. It returns a value greater equal zero otherwise.
+ static int OptionCategoryCompare(OptionCategory *const *A,
+ OptionCategory *const *B) {
+ return strcmp((*A)->getName(), (*B)->getName());
}
// Make sure we inherit our base class's operator=()
// Collect registered option categories into vector in preparation for
// sorting.
- for (OptionCatSet::const_iterator I = RegisteredOptionCategories->begin(),
- E = RegisteredOptionCategories->end();
+ for (auto I = GlobalParser->RegisteredOptionCategories.begin(),
+ E = GlobalParser->RegisteredOptionCategories.end();
I != E; ++I) {
SortedCategories.push_back(*I);
}
// Sort the different option categories alphabetically.
assert(SortedCategories.size() > 0 && "No option categories registered!");
- std::sort(SortedCategories.begin(), SortedCategories.end(),
- OptionCategoryCompare);
+ array_pod_sort(SortedCategories.begin(), SortedCategories.end(),
+ OptionCategoryCompare);
// Create map to empty vectors.
for (std::vector<OptionCategory *>::const_iterator
cl::init(false), cl::cat(GenericCategory));
void HelpPrinterWrapper::operator=(bool Value) {
- if (Value == false)
+ if (!Value)
return;
// Decide which printer to invoke. If more than one option category is
// registered then it is useful to show the categorized help instead of
// uncategorized help.
- if (RegisteredOptionCategories->size() > 1) {
+ if (GlobalParser->RegisteredOptionCategories.size() > 1) {
// unhide -help-list option so user can have uncategorized output if they
// want it.
HLOp.setHiddenFlag(NotHidden);