1//===--- OptTable.h - Option Table ------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef CLANG_DRIVER_OPTTABLE_H
11#define CLANG_DRIVER_OPTTABLE_H
12
13#include "clang/Basic/LLVM.h"
14#include "clang/Driver/OptSpecifier.h"
15#include "llvm/ADT/StringSet.h"
16
17namespace clang {
18namespace driver {
19  class Arg;
20  class ArgList;
21  class InputArgList;
22  class Option;
23
24  /// \brief Provide access to the Option info table.
25  ///
26  /// The OptTable class provides a layer of indirection which allows Option
27  /// instance to be created lazily. In the common case, only a few options will
28  /// be needed at runtime; the OptTable class maintains enough information to
29  /// parse command lines without instantiating Options, while letting other
30  /// parts of the driver still use Option instances where convenient.
31  class OptTable {
32  public:
33    /// \brief Entry for a single option instance in the option data table.
34    struct Info {
35      /// A null terminated array of prefix strings to apply to name while
36      /// matching.
37      const char *const *Prefixes;
38      const char *Name;
39      const char *HelpText;
40      const char *MetaVar;
41      unsigned ID;
42      unsigned char Kind;
43      unsigned char Param;
44      unsigned short Flags;
45      unsigned short GroupID;
46      unsigned short AliasID;
47    };
48
49  private:
50    /// \brief The static option information table.
51    const Info *OptionInfos;
52    unsigned NumOptionInfos;
53
54    unsigned TheInputOptionID;
55    unsigned TheUnknownOptionID;
56
57    /// The index of the first option which can be parsed (i.e., is not a
58    /// special option like 'input' or 'unknown', and is not an option group).
59    unsigned FirstSearchableIndex;
60
61    /// The union of all option prefixes. If an argument does not begin with
62    /// one of these, it is an input.
63    llvm::StringSet<> PrefixesUnion;
64    std::string PrefixChars;
65
66  private:
67    const Info &getInfo(OptSpecifier Opt) const {
68      unsigned id = Opt.getID();
69      assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
70      return OptionInfos[id - 1];
71    }
72
73  protected:
74    OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos);
75  public:
76    ~OptTable();
77
78    /// \brief Return the total number of option classes.
79    unsigned getNumOptions() const { return NumOptionInfos; }
80
81    /// \brief Get the given Opt's Option instance, lazily creating it
82    /// if necessary.
83    ///
84    /// \return The option, or null for the INVALID option id.
85    const Option getOption(OptSpecifier Opt) const;
86
87    /// \brief Lookup the name of the given option.
88    const char *getOptionName(OptSpecifier id) const {
89      return getInfo(id).Name;
90    }
91
92    /// \brief Get the kind of the given option.
93    unsigned getOptionKind(OptSpecifier id) const {
94      return getInfo(id).Kind;
95    }
96
97    /// \brief Get the group id for the given option.
98    unsigned getOptionGroupID(OptSpecifier id) const {
99      return getInfo(id).GroupID;
100    }
101
102    /// \brief Get the help text to use to describe this option.
103    const char *getOptionHelpText(OptSpecifier id) const {
104      return getInfo(id).HelpText;
105    }
106
107    /// \brief Get the meta-variable name to use when describing
108    /// this options values in the help text.
109    const char *getOptionMetaVar(OptSpecifier id) const {
110      return getInfo(id).MetaVar;
111    }
112
113    /// \brief Parse a single argument; returning the new argument and
114    /// updating Index.
115    ///
116    /// \param [in,out] Index - The current parsing position in the argument
117    /// string list; on return this will be the index of the next argument
118    /// string to parse.
119    ///
120    /// \return The parsed argument, or 0 if the argument is missing values
121    /// (in which case Index still points at the conceptual next argument string
122    /// to parse).
123    Arg *ParseOneArg(const ArgList &Args, unsigned &Index) const;
124
125    /// \brief Parse an list of arguments into an InputArgList.
126    ///
127    /// The resulting InputArgList will reference the strings in [\p ArgBegin,
128    /// \p ArgEnd), and their lifetime should extend past that of the returned
129    /// InputArgList.
130    ///
131    /// The only error that can occur in this routine is if an argument is
132    /// missing values; in this case \p MissingArgCount will be non-zero.
133    ///
134    /// \param ArgBegin - The beginning of the argument vector.
135    /// \param ArgEnd - The end of the argument vector.
136    /// \param MissingArgIndex - On error, the index of the option which could
137    /// not be parsed.
138    /// \param MissingArgCount - On error, the number of missing options.
139    /// \return An InputArgList; on error this will contain all the options
140    /// which could be parsed.
141    InputArgList *ParseArgs(const char* const *ArgBegin,
142                            const char* const *ArgEnd,
143                            unsigned &MissingArgIndex,
144                            unsigned &MissingArgCount) const;
145
146    /// \brief Render the help text for an option table.
147    ///
148    /// \param OS - The stream to write the help text to.
149    /// \param Name - The name to use in the usage line.
150    /// \param Title - The title to use in the usage line.
151    /// \param FlagsToInclude - If non-zero, only include options with any
152    ///                         of these flags set.
153    /// \param FlagsToExclude - Exclude options with any of these flags set.
154    void PrintHelp(raw_ostream &OS, const char *Name,
155                   const char *Title, unsigned short FlagsToInclude = 0,
156                   unsigned short FlagsToExclude = 0) const;
157  };
158}
159}
160
161#endif
162