X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FCommandLine.cpp;h=07900d8493655eb689cdcba5717f3663feb37cad;hb=4459145c2ccb5d063841a5d8c76b8b8ac9adaf2f;hp=681bc1d911e5a033fb750fe13281f93d19b1a333;hpb=d0062c6e7c870da1f5fa7e587be21aa8ac1188fb;p=oota-llvm.git diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 681bc1d911e..07900d84936 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -17,6 +17,7 @@ //===----------------------------------------------------------------------===// #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" @@ -28,7 +29,6 @@ #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 @@ -106,10 +106,10 @@ void Option::addArgument() { /// GetOptionInfo - Scan the list of registered options, turning them into data /// structures that are easier to handle. -static void GetOptionInfo(std::vector &PositionalOpts, - std::vector &SinkOpts, +static void GetOptionInfo(SmallVectorImpl &PositionalOpts, + SmallVectorImpl &SinkOpts, StringMap &OptionsMap) { - std::vector OptionNames; + SmallVector 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. @@ -376,6 +376,8 @@ static void ParseCStringVector(std::vector &OutputVector, memcpy(NewStr, WorkStr.data(), Pos); NewStr[Pos] = 0; OutputVector.push_back(NewStr); + + WorkStr = WorkStr.substr(Pos); } } @@ -454,8 +456,8 @@ static void ExpandResponseFiles(unsigned argc, char** argv, void cl::ParseCommandLineOptions(int argc, char **argv, const char *Overview, bool ReadResponseFiles) { // Process all registered options. - std::vector PositionalOpts; - std::vector SinkOpts; + SmallVector PositionalOpts; + SmallVector SinkOpts; StringMap Opts; GetOptionInfo(PositionalOpts, SinkOpts, Opts); @@ -618,7 +620,7 @@ void cl::ParseCommandLineOptions(int argc, char **argv, << argv[i] << "'. Try: '" << argv[0] << " --help'\n"; ErrorParsing = true; } else { - for (std::vector::iterator I = SinkOpts.begin(), + for (SmallVectorImpl::iterator I = SinkOpts.begin(), E = SinkOpts.end(); I != E ; ++I) (*I)->addOccurrence(i, "", argv[i]); } @@ -642,6 +644,7 @@ void cl::ParseCommandLineOptions(int argc, char **argv, // Check for another comma. Pos = Val.find(','); } + Value = Val; } // If this is a named positional argument, just remember that it is the @@ -760,9 +763,14 @@ void cl::ParseCommandLineOptions(int argc, char **argv, // Free all the strdup()ed strings. for (std::vector::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); } @@ -1010,6 +1018,12 @@ void generic_parser_base::printOptionInfo(const Option &O, // --help and --help-hidden option implementation // +static int OptNameCompare(const void *LHS, const void *RHS) { + typedef std::pair pair_ty; + + return strcmp(((pair_ty*)LHS)->first, ((pair_ty*)RHS)->first); +} + namespace { class HelpPrinter { @@ -1026,13 +1040,13 @@ public: if (Value == false) return; // Get all the options. - std::vector PositionalOpts; - std::vector SinkOpts; + SmallVector PositionalOpts; + SmallVector SinkOpts; StringMap OptMap; GetOptionInfo(PositionalOpts, SinkOpts, OptMap); // Copy Options into a vector so we can sort them as we like. - std::vector Opts; + SmallVector, 128> Opts; SmallPtrSet OptionSet; // Duplicate option detection. for (StringMap::iterator I = OptMap.begin(), E = OptMap.end(); @@ -1046,11 +1060,15 @@ public: 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(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"; @@ -1077,11 +1095,11 @@ public: // 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::iterator I = MoreHelp->begin(), @@ -1111,57 +1129,64 @@ HHOp("help-hidden", cl::desc("Display all available options"), static void (*OverrideVersionPrinter)() = 0; +static int TargetArraySortFn(const void *LHS, const void *RHS) { + typedef std::pair 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 > Targets; + std::vector > 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