LLVM API Documentation

Option.h
Go to the documentation of this file.
00001 //===--- Option.h - Abstract Driver Options ---------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_OPTION_OPTION_H
00011 #define LLVM_OPTION_OPTION_H
00012 
00013 #include "llvm/ADT/SmallVector.h"
00014 #include "llvm/ADT/StringRef.h"
00015 #include "llvm/Option/OptTable.h"
00016 #include "llvm/Support/ErrorHandling.h"
00017 
00018 namespace llvm {
00019 namespace opt {
00020 class Arg;
00021 class ArgList;
00022 /// ArgStringList - Type used for constructing argv lists for subprocesses.
00023 typedef SmallVector<const char*, 16> ArgStringList;
00024 
00025 /// Base flags for all options. Custom flags may be added after.
00026 enum DriverFlag {
00027   HelpHidden       = (1 << 0),
00028   RenderAsInput    = (1 << 1),
00029   RenderJoined     = (1 << 2),
00030   RenderSeparate   = (1 << 3)
00031 };
00032 
00033 /// Option - Abstract representation for a single form of driver
00034 /// argument.
00035 ///
00036 /// An Option class represents a form of option that the driver
00037 /// takes, for example how many arguments the option has and how
00038 /// they can be provided. Individual option instances store
00039 /// additional information about what group the option is a member
00040 /// of (if any), if the option is an alias, and a number of
00041 /// flags. At runtime the driver parses the command line into
00042 /// concrete Arg instances, each of which corresponds to a
00043 /// particular Option instance.
00044 class Option {
00045 public:
00046   enum OptionClass {
00047     GroupClass = 0,
00048     InputClass,
00049     UnknownClass,
00050     FlagClass,
00051     JoinedClass,
00052     SeparateClass,
00053     RemainingArgsClass,
00054     CommaJoinedClass,
00055     MultiArgClass,
00056     JoinedOrSeparateClass,
00057     JoinedAndSeparateClass
00058   };
00059 
00060   enum RenderStyleKind {
00061     RenderCommaJoinedStyle,
00062     RenderJoinedStyle,
00063     RenderSeparateStyle,
00064     RenderValuesStyle
00065   };
00066 
00067 protected:
00068   const OptTable::Info *Info;
00069   const OptTable *Owner;
00070 
00071 public:
00072   Option(const OptTable::Info *Info, const OptTable *Owner);
00073   ~Option();
00074 
00075   bool isValid() const {
00076     return Info != nullptr;
00077   }
00078 
00079   unsigned getID() const {
00080     assert(Info && "Must have a valid info!");
00081     return Info->ID;
00082   }
00083 
00084   OptionClass getKind() const {
00085     assert(Info && "Must have a valid info!");
00086     return OptionClass(Info->Kind);
00087   }
00088 
00089   /// \brief Get the name of this option without any prefix.
00090   StringRef getName() const {
00091     assert(Info && "Must have a valid info!");
00092     return Info->Name;
00093   }
00094 
00095   const Option getGroup() const {
00096     assert(Info && "Must have a valid info!");
00097     assert(Owner && "Must have a valid owner!");
00098     return Owner->getOption(Info->GroupID);
00099   }
00100 
00101   const Option getAlias() const {
00102     assert(Info && "Must have a valid info!");
00103     assert(Owner && "Must have a valid owner!");
00104     return Owner->getOption(Info->AliasID);
00105   }
00106 
00107   /// \brief Get the alias arguments as a \0 separated list.
00108   /// E.g. ["foo", "bar"] would be returned as "foo\0bar\0".
00109   const char *getAliasArgs() const {
00110     assert(Info && "Must have a valid info!");
00111     assert((!Info->AliasArgs || Info->AliasArgs[0] != 0) &&
00112            "AliasArgs should be either 0 or non-empty.");
00113 
00114     return Info->AliasArgs;
00115   }
00116 
00117   /// \brief Get the default prefix for this option.
00118   StringRef getPrefix() const {
00119     const char *Prefix = *Info->Prefixes;
00120     return Prefix ? Prefix : StringRef();
00121   }
00122 
00123   /// \brief Get the name of this option with the default prefix.
00124   std::string getPrefixedName() const {
00125     std::string Ret = getPrefix();
00126     Ret += getName();
00127     return Ret;
00128   }
00129 
00130   unsigned getNumArgs() const { return Info->Param; }
00131 
00132   bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
00133 
00134   RenderStyleKind getRenderStyle() const {
00135     if (Info->Flags & RenderJoined)
00136       return RenderJoinedStyle;
00137     if (Info->Flags & RenderSeparate)
00138       return RenderSeparateStyle;
00139     switch (getKind()) {
00140     case GroupClass:
00141     case InputClass:
00142     case UnknownClass:
00143       return RenderValuesStyle;
00144     case JoinedClass:
00145     case JoinedAndSeparateClass:
00146       return RenderJoinedStyle;
00147     case CommaJoinedClass:
00148       return RenderCommaJoinedStyle;
00149     case FlagClass:
00150     case SeparateClass:
00151     case MultiArgClass:
00152     case JoinedOrSeparateClass:
00153     case RemainingArgsClass:
00154       return RenderSeparateStyle;
00155     }
00156     llvm_unreachable("Unexpected kind!");
00157   }
00158 
00159   /// Test if this option has the flag \a Val.
00160   bool hasFlag(unsigned Val) const {
00161     return Info->Flags & Val;
00162   }
00163 
00164   /// getUnaliasedOption - Return the final option this option
00165   /// aliases (itself, if the option has no alias).
00166   const Option getUnaliasedOption() const {
00167     const Option Alias = getAlias();
00168     if (Alias.isValid()) return Alias.getUnaliasedOption();
00169     return *this;
00170   }
00171 
00172   /// getRenderName - Return the name to use when rendering this
00173   /// option.
00174   StringRef getRenderName() const {
00175     return getUnaliasedOption().getName();
00176   }
00177 
00178   /// matches - Predicate for whether this option is part of the
00179   /// given option (which may be a group).
00180   ///
00181   /// Note that matches against options which are an alias should never be
00182   /// done -- aliases do not participate in matching and so such a query will
00183   /// always be false.
00184   bool matches(OptSpecifier ID) const;
00185 
00186   /// accept - Potentially accept the current argument, returning a
00187   /// new Arg instance, or 0 if the option does not accept this
00188   /// argument (or the argument is missing values).
00189   ///
00190   /// If the option accepts the current argument, accept() sets
00191   /// Index to the position where argument parsing should resume
00192   /// (even if the argument is missing values).
00193   ///
00194   /// \param ArgSize The number of bytes taken up by the matched Option prefix
00195   ///                and name. This is used to determine where joined values
00196   ///                start.
00197   Arg *accept(const ArgList &Args, unsigned &Index, unsigned ArgSize) const;
00198 
00199   void dump() const;
00200 };
00201 
00202 } // end namespace opt
00203 } // end namespace llvm
00204 
00205 #endif