//===----------------------------------------------------------------------===//
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
-#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Config/config.h"
#include <cerrno>
/// GetOptionInfo - Scan the list of registered options, turning them into data
/// structures that are easier to handle.
-static void GetOptionInfo(std::vector<Option*> &PositionalOpts,
- std::vector<Option*> &SinkOpts,
+static void GetOptionInfo(SmallVectorImpl<Option*> &PositionalOpts,
+ SmallVectorImpl<Option*> &SinkOpts,
StringMap<Option*> &OptionsMap) {
- std::vector<const char*> OptionNames;
+ SmallVector<const char*, 16> OptionNames;
Option *CAOpt = 0; // The ConsumeAfter option if it exists.
for (Option *O = RegisteredOptionList; O; O = O->getNextRegisteredOption()) {
// If this option wants to handle multiple option names, get the full set.
memcpy(NewStr, WorkStr.data(), Pos);
NewStr[Pos] = 0;
OutputVector.push_back(NewStr);
+
+ WorkStr = WorkStr.substr(Pos);
}
}
void cl::ParseCommandLineOptions(int argc, char **argv,
const char *Overview, bool ReadResponseFiles) {
// Process all registered options.
- std::vector<Option*> PositionalOpts;
- std::vector<Option*> SinkOpts;
+ SmallVector<Option*, 4> PositionalOpts;
+ SmallVector<Option*, 4> SinkOpts;
StringMap<Option*> Opts;
GetOptionInfo(PositionalOpts, SinkOpts, Opts);
<< argv[i] << "'. Try: '" << argv[0] << " --help'\n";
ErrorParsing = true;
} else {
- for (std::vector<Option*>::iterator I = SinkOpts.begin(),
+ for (SmallVectorImpl<Option*>::iterator I = SinkOpts.begin(),
E = SinkOpts.end(); I != E ; ++I)
(*I)->addOccurrence(i, "", argv[i]);
}
// Check for another comma.
Pos = Val.find(',');
}
+ Value = Val;
}
// If this is a named positional argument, just remember that it is the
// Free all the strdup()ed strings.
for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
i != e; ++i)
- free (*i);
+ free(*i);
}
+ DEBUG(errs() << "\nArgs: ";
+ for (int i = 0; i < argc; ++i)
+ errs() << argv[i] << ' ';
+ );
+
// If we had an error processing our arguments, don't let the program execute
if (ErrorParsing) exit(1);
}
// --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(((pair_ty*)LHS)->first, ((pair_ty*)RHS)->first);
+}
+
namespace {
class HelpPrinter {
if (Value == false) return;
// Get all the options.
- std::vector<Option*> PositionalOpts;
- std::vector<Option*> SinkOpts;
+ SmallVector<Option*, 4> PositionalOpts;
+ SmallVector<Option*, 4> SinkOpts;
StringMap<Option*> OptMap;
GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
// Copy Options into a vector so we can sort them as we like.
- std::vector<Option*> Opts;
+ SmallVector<std::pair<const char *, Option*>, 128> Opts;
SmallPtrSet<Option*, 128> OptionSet; // Duplicate option detection.
for (StringMap<Option*>::iterator I = OptMap.begin(), E = OptMap.end();
continue;
// If we've already seen this option, don't add it to the list again.
- if (OptionSet.insert(I->second))
+ if (!OptionSet.insert(I->second))
continue;
- Opts.push_back(I->second);
+ Opts.push_back(std::pair<const char *, Option*>(I->getKey().data(),
+ I->second));
}
+
+ // Sort the options list alphabetically.
+ qsort(Opts.data(), Opts.size(), sizeof(Opts[0]), OptNameCompare);
if (ProgramOverview)
outs() << "OVERVIEW: " << ProgramOverview << "\n";
// Compute the maximum argument length...
MaxArgLen = 0;
for (size_t i = 0, e = Opts.size(); i != e; ++i)
- MaxArgLen = std::max(MaxArgLen, Opts[i]->getOptionWidth());
+ MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
outs() << "OPTIONS:\n";
for (size_t i = 0, e = Opts.size(); i != e; ++i)
- Opts[i]->printOptionInfo(MaxArgLen);
+ Opts[i].second->printOptionInfo(MaxArgLen);
// Print any extra help the user has declared.
for (std::vector<const char *>::iterator I = MoreHelp->begin(),
static void (*OverrideVersionPrinter)() = 0;
+static int TargetArraySortFn(const void *LHS, const void *RHS) {
+ typedef std::pair<const char *, const Target*> pair_ty;
+ return strcmp(((const pair_ty*)LHS)->first, ((const pair_ty*)RHS)->first);
+}
+
namespace {
class VersionPrinter {
public:
void print() {
- outs() << "Low Level Virtual Machine (http://llvm.org/):\n"
- << " " << PACKAGE_NAME << " version " << PACKAGE_VERSION;
+ raw_ostream &OS = outs();
+ OS << "Low Level Virtual Machine (http://llvm.org/):\n"
+ << " " << PACKAGE_NAME << " version " << PACKAGE_VERSION;
#ifdef LLVM_VERSION_INFO
- outs() << LLVM_VERSION_INFO;
+ OS << LLVM_VERSION_INFO;
#endif
- outs() << "\n ";
+ OS << "\n ";
#ifndef __OPTIMIZE__
- outs() << "DEBUG build";
+ OS << "DEBUG build";
#else
- outs() << "Optimized build";
+ OS << "Optimized build";
#endif
#ifndef NDEBUG
- outs() << " with assertions";
+ OS << " with assertions";
#endif
- outs() << ".\n"
- << " Built " << __DATE__ << " (" << __TIME__ << ").\n"
- << " Host: " << sys::getHostTriple() << '\n'
- << "\n"
- << " Registered Targets:\n";
+ OS << ".\n"
+ << " Built " << __DATE__ << " (" << __TIME__ << ").\n"
+ << " Host: " << sys::getHostTriple() << '\n'
+ << '\n'
+ << " Registered Targets:\n";
- std::vector<std::pair<std::string, const Target*> > Targets;
+ std::vector<std::pair<const char *, const Target*> > Targets;
size_t Width = 0;
for (TargetRegistry::iterator it = TargetRegistry::begin(),
ie = TargetRegistry::end(); it != ie; ++it) {
Targets.push_back(std::make_pair(it->getName(), &*it));
- Width = std::max(Width, Targets.back().first.length());
+ Width = std::max(Width, strlen(Targets.back().first));
}
- array_pod_sort(Targets.begin(), Targets.end());
+ if (!Targets.empty())
+ qsort(&Targets[0], Targets.size(), sizeof(Targets[0]),
+ TargetArraySortFn);
for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
- outs() << " " << Targets[i].first;
- outs().indent(Width - Targets[i].first.length()) << " - "
+ OS << " " << Targets[i].first;
+ OS.indent(Width - strlen(Targets[i].first)) << " - "
<< Targets[i].second->getShortDescription() << '\n';
}
if (Targets.empty())
- outs() << " (none)\n";
+ OS << " (none)\n";
}
void operator=(bool OptionWasSpecified) {
- if (OptionWasSpecified) {
- if (OverrideVersionPrinter == 0) {
- print();
- exit(1);
- } else {
- (*OverrideVersionPrinter)();
- exit(1);
- }
+ if (!OptionWasSpecified) return;
+
+ if (OverrideVersionPrinter == 0) {
+ print();
+ exit(1);
}
+ (*OverrideVersionPrinter)();
+ exit(1);
}
};
} // End anonymous namespace