clang API Documentation

OptTable.h
Go to the documentation of this file.
00001 //===--- OptTable.h - Option Table ------------------------------*- 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 CLANG_DRIVER_OPTTABLE_H
00011 #define CLANG_DRIVER_OPTTABLE_H
00012 
00013 #include "clang/Basic/LLVM.h"
00014 #include "clang/Driver/OptSpecifier.h"
00015 
00016 namespace clang {
00017 namespace driver {
00018 namespace options {
00019   enum DriverFlag {
00020     DriverOption     = (1 << 0),
00021     HelpHidden       = (1 << 1),
00022     LinkerInput      = (1 << 2),
00023     NoArgumentUnused = (1 << 3),
00024     NoForward        = (1 << 4),
00025     RenderAsInput    = (1 << 5),
00026     RenderJoined     = (1 << 6),
00027     RenderSeparate   = (1 << 7),
00028     Unsupported      = (1 << 8),
00029     CC1Option        = (1 << 9)
00030   };
00031 }
00032 
00033   class Arg;
00034   class ArgList;
00035   class InputArgList;
00036   class Option;
00037 
00038   /// OptTable - Provide access to the Option info table.
00039   ///
00040   /// The OptTable class provides a layer of indirection which allows Option
00041   /// instance to be created lazily. In the common case, only a few options will
00042   /// be needed at runtime; the OptTable class maintains enough information to
00043   /// parse command lines without instantiating Options, while letting other
00044   /// parts of the driver still use Option instances where convenient.
00045   class OptTable {
00046   public:
00047     /// Info - Entry for a single option instance in the option data table.
00048     struct Info {
00049       const char *Name;
00050       const char *HelpText;
00051       const char *MetaVar;
00052       unsigned char Kind;
00053       unsigned char Param;
00054       unsigned short Flags;
00055       unsigned short GroupID;
00056       unsigned short AliasID;
00057     };
00058 
00059   private:
00060     /// The static option information table.
00061     const Info *OptionInfos;
00062     unsigned NumOptionInfos;
00063 
00064     /// The lazily constructed options table, indexed by option::ID - 1.
00065     mutable Option **Options;
00066 
00067     /// Prebound input option instance.
00068     const Option *TheInputOption;
00069 
00070     /// Prebound unknown option instance.
00071     const Option *TheUnknownOption;
00072 
00073     /// The index of the first option which can be parsed (i.e., is not a
00074     /// special option like 'input' or 'unknown', and is not an option group).
00075     unsigned FirstSearchableIndex;
00076 
00077   private:
00078     const Info &getInfo(OptSpecifier Opt) const {
00079       unsigned id = Opt.getID();
00080       assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
00081       return OptionInfos[id - 1];
00082     }
00083 
00084     Option *CreateOption(unsigned id) const;
00085 
00086   protected:
00087     OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos);
00088   public:
00089     ~OptTable();
00090 
00091     /// getNumOptions - Return the total number of option classes.
00092     unsigned getNumOptions() const { return NumOptionInfos; }
00093 
00094     /// getOption - Get the given \arg id's Option instance, lazily creating it
00095     /// if necessary.
00096     ///
00097     /// \return The option, or null for the INVALID option id.
00098     const Option *getOption(OptSpecifier Opt) const {
00099       unsigned id = Opt.getID();
00100       if (id == 0)
00101         return 0;
00102 
00103       assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
00104       Option *&Entry = Options[id - 1];
00105       if (!Entry)
00106         Entry = CreateOption(id);
00107       return Entry;
00108     }
00109 
00110     /// getOptionName - Lookup the name of the given option.
00111     const char *getOptionName(OptSpecifier id) const {
00112       return getInfo(id).Name;
00113     }
00114 
00115     /// getOptionKind - Get the kind of the given option.
00116     unsigned getOptionKind(OptSpecifier id) const {
00117       return getInfo(id).Kind;
00118     }
00119 
00120     /// getOptionGroupID - Get the group id for the given option.
00121     unsigned getOptionGroupID(OptSpecifier id) const {
00122       return getInfo(id).GroupID;
00123     }
00124 
00125     /// isOptionHelpHidden - Should the help for the given option be hidden by
00126     /// default.
00127     bool isOptionHelpHidden(OptSpecifier id) const {
00128       return getInfo(id).Flags & options::HelpHidden;
00129     }
00130 
00131     /// getOptionHelpText - Get the help text to use to describe this option.
00132     const char *getOptionHelpText(OptSpecifier id) const {
00133       return getInfo(id).HelpText;
00134     }
00135 
00136     /// getOptionMetaVar - Get the meta-variable name to use when describing
00137     /// this options values in the help text.
00138     const char *getOptionMetaVar(OptSpecifier id) const {
00139       return getInfo(id).MetaVar;
00140     }
00141 
00142     /// ParseOneArg - Parse a single argument; returning the new argument and
00143     /// updating Index.
00144     ///
00145     /// \param [in] [out] Index - The current parsing position in the argument
00146     /// string list; on return this will be the index of the next argument
00147     /// string to parse.
00148     ///
00149     /// \return - The parsed argument, or 0 if the argument is missing values
00150     /// (in which case Index still points at the conceptual next argument string
00151     /// to parse).
00152     Arg *ParseOneArg(const ArgList &Args, unsigned &Index) const;
00153 
00154     /// ParseArgs - Parse an list of arguments into an InputArgList.
00155     ///
00156     /// The resulting InputArgList will reference the strings in [ArgBegin,
00157     /// ArgEnd), and their lifetime should extend past that of the returned
00158     /// InputArgList.
00159     ///
00160     /// The only error that can occur in this routine is if an argument is
00161     /// missing values; in this case \arg MissingArgCount will be non-zero.
00162     ///
00163     /// \param ArgBegin - The beginning of the argument vector.
00164     /// \param ArgEnd - The end of the argument vector.
00165     /// \param MissingArgIndex - On error, the index of the option which could
00166     /// not be parsed.
00167     /// \param MissingArgCount - On error, the number of missing options.
00168     /// \return - An InputArgList; on error this will contain all the options
00169     /// which could be parsed.
00170     InputArgList *ParseArgs(const char* const *ArgBegin,
00171                             const char* const *ArgEnd,
00172                             unsigned &MissingArgIndex,
00173                             unsigned &MissingArgCount) const;
00174 
00175     /// PrintHelp - Render the help text for an option table.
00176     ///
00177     /// \param OS - The stream to write the help text to.
00178     /// \param Name - The name to use in the usage line.
00179     /// \param Title - The title to use in the usage line.
00180     /// \param ShowHidden - Whether help-hidden arguments should be shown.
00181     void PrintHelp(raw_ostream &OS, const char *Name,
00182                    const char *Title, bool ShowHidden = false) const;
00183   };
00184 }
00185 }
00186 
00187 #endif