Skip to content
CommandLine.cpp 35.5 KiB
Newer Older
Chris Lattner's avatar
Chris Lattner committed
//===-- CommandLine.cpp - Command line parser implementation --------------===//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//===----------------------------------------------------------------------===//
Chris Lattner's avatar
Chris Lattner committed
//
// This class implements a command line argument processor that is useful when
// creating a tool.  It provides a simple, minimalistic interface that is easily
// extensible and supports nonlocal (library) command line options.
//
// Note that rather than trying to figure out what this code does, you could try
// reading the library documentation located in docs/CommandLine.html
//
Chris Lattner's avatar
Chris Lattner committed
//===----------------------------------------------------------------------===//

Reid Spencer's avatar
Reid Spencer committed
#include "llvm/Config/config.h"
#include "llvm/Support/CommandLine.h"
Chris Lattner's avatar
Chris Lattner committed
#include <algorithm>
Chris Lattner's avatar
Chris Lattner committed
#include <map>
#include <set>
#include <iostream>
#include <cstdlib>
#include <cerrno>
#include <cstring>
using namespace llvm;
Chris Lattner's avatar
Chris Lattner committed
using namespace cl;

// Globals for name and overview of program
static const char *ProgramName = "<unknown>";
static const char *ProgramOverview = 0;

// This collects additional help to be printed.
static std::vector<const char*> &MoreHelp() {
  static std::vector<const char*> moreHelp;
  return moreHelp;
}

extrahelp::extrahelp(const char* Help)
  : morehelp(Help) {
  MoreHelp().push_back(Help);
}

Chris Lattner's avatar
Chris Lattner committed
//===----------------------------------------------------------------------===//
// Basic, shared command line option processing machinery...
//

Chris Lattner's avatar
Chris Lattner committed
// Return the global command line option vector.  Making it a function scoped
// static ensures that it will be initialized correctly before its first use.
Chris Lattner's avatar
Chris Lattner committed
//
static std::map<std::string, Option*> &getOpts() {
  static std::map<std::string, Option*> CommandLineOptions;
  return CommandLineOptions;
static Option *getOption(const std::string &Str) {
  std::map<std::string,Option*>::iterator I = getOpts().find(Str);
  return I != getOpts().end() ? I->second : 0;
Chris Lattner's avatar
Chris Lattner committed
}

static std::vector<Option*> &getPositionalOpts() {
  static std::vector<Option*> Positional;
  return Positional;
static void AddArgument(const char *ArgName, Option *Opt) {
  if (getOption(ArgName)) {
    std::cerr << ProgramName << ": CommandLine Error: Argument '"
              << ArgName << "' defined more than once!\n";
Chris Lattner's avatar
Chris Lattner committed
  } else {
    getOpts()[ArgName] = Opt;
  }
}

// RemoveArgument - It's possible that the argument is no longer in the map if
// options have already been processed and the map has been deleted!
static void RemoveArgument(const char *ArgName, Option *Opt) {
#ifndef NDEBUG
  // This disgusting HACK is brought to you courtesy of GCC 3.3.2, which ICE's
  // If we pass ArgName directly into getOption here.
  std::string Tmp = ArgName;
  assert(getOption(Tmp) == Opt && "Arg not in map!");
#endif
Chris Lattner's avatar
Chris Lattner committed
}

static inline bool ProvideOption(Option *Handler, const char *ArgName,
                                 const char *Value, int argc, char **argv,
                                 int &i) {
  // Enforce value requirements
  switch (Handler->getValueExpectedFlag()) {
  case ValueRequired:
    if (Value == 0) {       // No value specified?
      if (i+1 < argc) {     // Steal the next argument, like for '-o filename'
        Value = argv[++i];
      } else {
        return Handler->error(" requires a value!");
      }
    }
    break;
  case ValueDisallowed:
      return Handler->error(" does not allow a value! '" +
                            std::string(Value) + "' specified.");
  case ValueOptional:
  default:
    std::cerr << ProgramName
              << ": Bad ValueMask flag! CommandLine usage error:"
              << Handler->getValueExpectedFlag() << "\n";
  return Handler->addOccurrence(i, ArgName, Value ? Value : "");
static bool ProvidePositionalOption(Option *Handler, const std::string &Arg,
  return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner's avatar
Chris Lattner committed
}


// Option predicates...
static inline bool isGrouping(const Option *O) {
  return O->getFormattingFlag() == cl::Grouping;
}
static inline bool isPrefixedOrGrouping(const Option *O) {
  return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
}

// getOptionPred - Check to see if there are any options that satisfy the
// specified predicate with names that are the prefixes in Name.  This is
// checked by progressively stripping characters off of the name, checking to
// see if there options that satisfy the predicate.  If we find one, return it,
// otherwise return null.
Chris Lattner's avatar
Chris Lattner committed
static Option *getOptionPred(std::string Name, unsigned &Length,
                             bool (*Pred)(const Option*)) {
  Option *Op = getOption(Name);
  if (Op && Pred(Op)) {
Chris Lattner's avatar
Chris Lattner committed
    Length = Name.length();
Chris Lattner's avatar
Chris Lattner committed
  if (Name.size() == 1) return 0;
  do {
    Name.erase(Name.end()-1, Name.end());   // Chop off the last character...
Chris Lattner's avatar
Chris Lattner committed

    // Loop while we haven't found an option and Name still has at least two
    // characters in it (so that the next iteration will not be the empty
    // string...
  } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner's avatar
Chris Lattner committed
    Length = Name.length();
Chris Lattner's avatar
Chris Lattner committed
  return 0;                // No option found!
}
Chris Lattner's avatar
Chris Lattner committed
static bool RequiresValue(const Option *O) {
  return O->getNumOccurrencesFlag() == cl::Required ||
         O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner's avatar
Chris Lattner committed
}

static bool EatsUnboundedNumberOfValues(const Option *O) {
  return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
         O->getNumOccurrencesFlag() == cl::OneOrMore;
/// ParseCStringVector - Break INPUT up wherever one or more
/// whitespace characters are found, and store the resulting tokens in
/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
/// using strdup (), so it is the caller's responsibility to free ()
/// them later.
static void ParseCStringVector (std::vector<char *> &output,
  // Characters which will be treated as token separators:
  static const char *delims = " \v\f\t\r\n";

  // Skip past any delims at head of input string.
  size_t pos = work.find_first_not_of (delims);
  // If the string consists entirely of delims, then exit early.
  if (pos == std::string::npos) return;
Loading
Loading full blame...