1f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===- OptTable.h - Option Table --------------------------------*- C++ -*-===// 2f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 3f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// The LLVM Compiler Infrastructure 4f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 5f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// This file is distributed under the University of Illinois Open Source 6f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// License. See LICENSE.TXT for details. 7f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 8f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===----------------------------------------------------------------------===// 9f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 10f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#ifndef LLVM_OPTION_OPTTABLE_H 11f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#define LLVM_OPTION_OPTTABLE_H 12f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 13f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/ADT/ArrayRef.h" 14f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/ADT/StringRef.h" 15f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/ADT/StringSet.h" 16f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/Option/OptSpecifier.h" 17f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include <cassert> 18f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include <string> 19f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include <vector> 20f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 21f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotnamespace llvm { 22f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 23f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass raw_ostream; 24f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 25f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotnamespace opt { 26f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 27f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass Arg; 28f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass ArgList; 29f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass InputArgList; 30f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass Option; 31f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 32f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// \brief Provide access to the Option info table. 33f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 34f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// The OptTable class provides a layer of indirection which allows Option 35f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// instance to be created lazily. In the common case, only a few options will 36f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// be needed at runtime; the OptTable class maintains enough information to 37f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// parse command lines without instantiating Options, while letting other 38f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// parts of the driver still use Option instances where convenient. 39f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass OptTable { 40f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotpublic: 41f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Entry for a single option instance in the option data table. 42f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot struct Info { 43f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// A null terminated array of prefix strings to apply to name while 44f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// matching. 45f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const char *const *Prefixes; 46f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const char *Name; 47f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const char *HelpText; 48f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const char *MetaVar; 49f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned ID; 50f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned char Kind; 51f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned char Param; 52f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned short Flags; 53f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned short GroupID; 54f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned short AliasID; 55f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const char *AliasArgs; 56f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const char *Values; 57f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot }; 58f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 59f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotprivate: 60f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief The option information table. 61f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot std::vector<Info> OptionInfos; 62f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool IgnoreCase; 63f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 64f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned TheInputOptionID = 0; 65f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned TheUnknownOptionID = 0; 66f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 67f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// The index of the first option which can be parsed (i.e., is not a 68f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// special option like 'input' or 'unknown', and is not an option group). 69f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned FirstSearchableIndex = 0; 70f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 71f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// The union of all option prefixes. If an argument does not begin with 72f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// one of these, it is an input. 73f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot StringSet<> PrefixesUnion; 74f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot std::string PrefixChars; 75f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 76f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotprivate: 77f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const Info &getInfo(OptSpecifier Opt) const { 78f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned id = Opt.getID(); 79f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID."); 80f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return OptionInfos[id - 1]; 81f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 82f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 83f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotprotected: 84f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false); 85f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 86f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotpublic: 87f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot ~OptTable(); 88f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 89f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Return the total number of option classes. 90f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned getNumOptions() const { return OptionInfos.size(); } 91f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 92f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Get the given Opt's Option instance, lazily creating it 93f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// if necessary. 94f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 95f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \return The option, or null for the INVALID option id. 96f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const Option getOption(OptSpecifier Opt) const; 97f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 98f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Lookup the name of the given option. 99f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const char *getOptionName(OptSpecifier id) const { 100f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return getInfo(id).Name; 101f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 102f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 103f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Get the kind of the given option. 104f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned getOptionKind(OptSpecifier id) const { 105f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return getInfo(id).Kind; 106f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 107f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 108f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Get the group id for the given option. 109f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned getOptionGroupID(OptSpecifier id) const { 110f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return getInfo(id).GroupID; 111f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 112f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 113f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Get the help text to use to describe this option. 114f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const char *getOptionHelpText(OptSpecifier id) const { 115f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return getInfo(id).HelpText; 116f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 117f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 118f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Get the meta-variable name to use when describing 119f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// this options values in the help text. 120f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const char *getOptionMetaVar(OptSpecifier id) const { 121f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return getInfo(id).MetaVar; 122f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 123f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 124f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Find possible value for given flags. This is used for shell 125f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// autocompletion. 126f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 127f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param [in] Option - Key flag like "-stdlib=" when "-stdlib=l" 128f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// was passed to clang. 129f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 130f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param [in] Arg - Value which we want to autocomplete like "l" 131f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// when "-stdlib=l" was passed to clang. 132f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 133f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \return The vector of possible values. 134f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot std::vector<std::string> suggestValueCompletions(StringRef Option, 135f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot StringRef Arg) const; 136f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 137f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Find flags from OptTable which starts with Cur. 138f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 139f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param [in] Cur - String prefix that all returned flags need 140f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot // to start with. 141f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 142f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \return The vector of flags which start with Cur. 143f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot std::vector<std::string> findByPrefix(StringRef Cur, 144f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned short DisableFlags) const; 145f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 146f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Add Values to Option's Values class 147f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 148f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param [in] Option - Prefix + Name of the flag which Values will be 149f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// changed. For example, "-analyzer-checker". 150f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param [in] Values - String of Values seperated by ",", such as 151f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// "foo, bar..", where foo and bar is the argument which the Option flag 152f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// takes 153f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 154f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \return true in success, and false in fail. 155f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool addValues(const char *Option, const char *Values); 156f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 157f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Parse a single argument; returning the new argument and 158f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// updating Index. 159f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 160f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param [in,out] Index - The current parsing position in the argument 161f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// string list; on return this will be the index of the next argument 162f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// string to parse. 163f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param [in] FlagsToInclude - Only parse options with any of these flags. 164f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Zero is the default which includes all flags. 165f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param [in] FlagsToExclude - Don't parse options with this flag. Zero 166f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// is the default and means exclude nothing. 167f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 168f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \return The parsed argument, or 0 if the argument is missing values 169f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// (in which case Index still points at the conceptual next argument string 170f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// to parse). 171f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Arg *ParseOneArg(const ArgList &Args, unsigned &Index, 172f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned FlagsToInclude = 0, 173f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned FlagsToExclude = 0) const; 174f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 175f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Parse an list of arguments into an InputArgList. 176f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 177f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// The resulting InputArgList will reference the strings in [\p ArgBegin, 178f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \p ArgEnd), and their lifetime should extend past that of the returned 179f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// InputArgList. 180f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 181f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// The only error that can occur in this routine is if an argument is 182f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// missing values; in this case \p MissingArgCount will be non-zero. 183f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 184f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param MissingArgIndex - On error, the index of the option which could 185f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// not be parsed. 186f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param MissingArgCount - On error, the number of missing options. 187f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param FlagsToInclude - Only parse options with any of these flags. 188f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Zero is the default which includes all flags. 189f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param FlagsToExclude - Don't parse options with this flag. Zero 190f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// is the default and means exclude nothing. 191f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \return An InputArgList; on error this will contain all the options 192f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// which could be parsed. 193f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot InputArgList ParseArgs(ArrayRef<const char *> Args, unsigned &MissingArgIndex, 194f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned &MissingArgCount, unsigned FlagsToInclude = 0, 195f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned FlagsToExclude = 0) const; 196f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 197f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Render the help text for an option table. 198f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 199f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param OS - The stream to write the help text to. 200f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param Name - The name to use in the usage line. 201f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param Title - The title to use in the usage line. 202f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param FlagsToInclude - If non-zero, only include options with any 203f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// of these flags set. 204f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param FlagsToExclude - Exclude options with any of these flags set. 205f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \param ShowAllAliases - If true, display all options including aliases 206f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// that don't have help texts. By default, we display 207f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// only options that are not hidden and have help 208f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// texts. 209f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void PrintHelp(raw_ostream &OS, const char *Name, const char *Title, 210f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot unsigned FlagsToInclude, unsigned FlagsToExclude, 211f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool ShowAllAliases) const; 212f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 213f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void PrintHelp(raw_ostream &OS, const char *Name, const char *Title, 214f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool ShowHidden = false, bool ShowAllAliases = false) const; 215f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot}; 216f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 217f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot} // end namespace opt 218f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 219f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot} // end namespace llvm 220f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 221f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#endif // LLVM_OPTION_OPTTABLE_H 222