LLVM 22.0.0git
llvm::opt::OptTable Class Reference

Provide access to the Option info table. More...

#include "llvm/Option/OptTable.h"

Inheritance diagram for llvm::opt::OptTable:
[legend]

Classes

struct  Info
 Entry for a single option instance in the option data table. More...

Public Member Functions

virtual ~OptTable ()
const StringTablegetStrTable () const
 Return the string table used for option names.
ArrayRef< StringTable::OffsetgetPrefixesTable () const
 Return the prefixes table used for option names.
unsigned getNumOptions () const
 Return the total number of option classes.
const Option getOption (OptSpecifier Opt) const
 Get the given Opt's Option instance, lazily creating it if necessary.
StringRef getOptionName (OptSpecifier id) const
 Lookup the name of the given option.
StringRef getOptionPrefix (OptSpecifier id) const
 Lookup the prefix of the given option.
void appendOptionPrefixes (OptSpecifier id, SmallVectorImpl< StringRef > &Prefixes) const
StringRef getOptionPrefixedName (OptSpecifier id) const
 Lookup the prefixed name of the given option.
unsigned getOptionKind (OptSpecifier id) const
 Get the kind of the given option.
unsigned getOptionGroupID (OptSpecifier id) const
 Get the group id for the given option.
const chargetOptionHelpText (OptSpecifier id) const
 Get the help text to use to describe this option.
const chargetOptionHelpText (OptSpecifier id, Visibility VisibilityMask) const
const chargetOptionMetaVar (OptSpecifier id) const
 Get the meta-variable name to use when describing this options values in the help text.
void setInitialOptionsFromEnvironment (const char *E)
 Specify the environment variable where initial options should be read.
void setGroupedShortOptions (bool Value)
 Support grouped short options. e.g. -ab represents -a -b.
void setDashDashParsing (bool Value)
 Set whether "--" stops option parsing and treats all subsequent arguments as positional.
std::vector< std::string > suggestValueCompletions (StringRef Option, StringRef Arg) const
 Find possible value for given flags.
std::vector< std::string > findByPrefix (StringRef Cur, Visibility VisibilityMask, unsigned int DisableFlags) const
 Find flags from OptTable which starts with Cur.
unsigned findNearest (StringRef Option, std::string &NearestString, Visibility VisibilityMask=Visibility(), unsigned MinimumLength=4, unsigned MaximumDistance=UINT_MAX) const
 Find the OptTable option that most closely matches the given string.
unsigned findNearest (StringRef Option, std::string &NearestString, unsigned FlagsToInclude, unsigned FlagsToExclude=0, unsigned MinimumLength=4, unsigned MaximumDistance=UINT_MAX) const
bool findExact (StringRef Option, std::string &ExactString, Visibility VisibilityMask=Visibility()) const
bool findExact (StringRef Option, std::string &ExactString, unsigned FlagsToInclude, unsigned FlagsToExclude=0) const
std::unique_ptr< ArgParseOneArg (const ArgList &Args, unsigned &Index, Visibility VisibilityMask=Visibility()) const
 Parse a single argument; returning the new argument and updating Index.
std::unique_ptr< ArgParseOneArg (const ArgList &Args, unsigned &Index, unsigned FlagsToInclude, unsigned FlagsToExclude) const
InputArgList ParseArgs (ArrayRef< const char * > Args, unsigned &MissingArgIndex, unsigned &MissingArgCount, Visibility VisibilityMask=Visibility()) const
 Parse an list of arguments into an InputArgList.
InputArgList ParseArgs (ArrayRef< const char * > Args, unsigned &MissingArgIndex, unsigned &MissingArgCount, unsigned FlagsToInclude, unsigned FlagsToExclude=0) const
InputArgList parseArgs (int Argc, char *const *Argv, OptSpecifier Unknown, StringSaver &Saver, std::function< void(StringRef)> ErrorFn) const
 A convenience helper which handles optional initial options populated from an environment variable, expands response files recursively and parses options.
void printHelp (raw_ostream &OS, const char *Usage, const char *Title, bool ShowHidden=false, bool ShowAllAliases=false, Visibility VisibilityMask=Visibility()) const
 Render the help text for an option table.
void printHelp (raw_ostream &OS, const char *Usage, const char *Title, unsigned FlagsToInclude, unsigned FlagsToExclude, bool ShowAllAliases) const

Protected Member Functions

 OptTable (const StringTable &StrTable, ArrayRef< StringTable::Offset > PrefixesTable, ArrayRef< Info > OptionInfos, bool IgnoreCase=false)
 Initialize OptTable using Tablegen'ed OptionInfos.
void buildPrefixChars ()
 Build (or rebuild) the PrefixChars member.

Protected Attributes

unsigned FirstSearchableIndex = 0
 The index of the first option which can be parsed (i.e., is not a special option like 'input' or 'unknown', and is not an option group).
SmallVector< StringRefPrefixesUnion
 The union of all option prefixes.
SmallString< 8 > PrefixChars
 The union of the first element of all option prefixes.

Detailed Description

Provide access to the Option info table.

The OptTable class provides a layer of indirection which allows Option instance to be created lazily. In the common case, only a few options will be needed at runtime; the OptTable class maintains enough information to parse command lines without instantiating Options, while letting other parts of the driver still use Option instances where convenient.

Definition at line 54 of file OptTable.h.

Constructor & Destructor Documentation

◆ OptTable()

OptTable::OptTable ( const StringTable & StrTable,
ArrayRef< StringTable::Offset > PrefixesTable,
ArrayRef< Info > OptionInfos,
bool IgnoreCase = false )
protected

Initialize OptTable using Tablegen'ed OptionInfos.

Child class must manually call buildPrefixChars once they are fully constructed.

Definition at line 80 of file OptTable.cpp.

References assert(), llvm::opt::Option::dump(), FirstSearchableIndex, getNumOptions(), getOption(), llvm::opt::Option::GroupClass, llvm::opt::Option::InputClass, llvm_unreachable, and llvm::opt::Option::UnknownClass.

Referenced by llvm::opt::GenericOptTable::GenericOptTable(), and llvm::opt::PrecomputedOptTable::PrecomputedOptTable().

◆ ~OptTable()

OptTable::~OptTable ( )
virtualdefault

Member Function Documentation

◆ appendOptionPrefixes()

void llvm::opt::OptTable::appendOptionPrefixes ( OptSpecifier id,
SmallVectorImpl< StringRef > & Prefixes ) const
inline

Definition at line 208 of file OptTable.h.

References I.

◆ buildPrefixChars()

void OptTable::buildPrefixChars ( )
protected

◆ findByPrefix()

std::vector< std::string > OptTable::findByPrefix ( StringRef Cur,
Visibility VisibilityMask,
unsigned int DisableFlags ) const

Find flags from OptTable which starts with Cur.

Parameters
[in]Cur- String prefix that all returned flags need
Returns
The vector of flags which start with Cur.

Definition at line 210 of file OptTable.cpp.

References FirstSearchableIndex, I, and starts_with().

◆ findExact() [1/2]

bool llvm::opt::OptTable::findExact ( StringRef Option,
std::string & ExactString,
unsigned FlagsToInclude,
unsigned FlagsToExclude = 0 ) const
inline

Definition at line 324 of file OptTable.h.

References findNearest().

◆ findExact() [2/2]

bool llvm::opt::OptTable::findExact ( StringRef Option,
std::string & ExactString,
Visibility VisibilityMask = Visibility() ) const
inline

Definition at line 319 of file OptTable.h.

References findNearest().

◆ findNearest() [1/2]

unsigned OptTable::findNearest ( StringRef Option,
std::string & NearestString,
unsigned FlagsToInclude,
unsigned FlagsToExclude = 0,
unsigned MinimumLength = 4,
unsigned MaximumDistance = UINT_MAX ) const

Definition at line 246 of file OptTable.cpp.

References llvm::opt::OptTable::Info::Flags.

◆ findNearest() [2/2]

unsigned OptTable::findNearest ( StringRef Option,
std::string & NearestString,
Visibility VisibilityMask = Visibility(),
unsigned MinimumLength = 4,
unsigned MaximumDistance = UINT_MAX ) const

Find the OptTable option that most closely matches the given string.

Parameters
[in]Option- A string, such as "-stdlibs=l", that represents user input of an option that may not exist in the OptTable. Note that the string includes prefix dashes "-" as well as values "=l".
[out]NearestString- The nearest option string found in the OptTable.
[in]VisibilityMask- Only include options with any of these visibility flags set.
[in]MinimumLength- Don't find options shorter than this length. For example, a minimum length of 3 prevents "-x" from being considered near to "-S".
[in]MaximumDistance- Don't find options whose distance is greater than this value.
Returns
The edit distance of the nearest string found.

Definition at line 235 of file OptTable.cpp.

References llvm::opt::OptTable::Info::Visibility.

Referenced by findExact(), findExact(), and parseArgs().

◆ getNumOptions()

unsigned llvm::opt::OptTable::getNumOptions ( ) const
inline

Return the total number of option classes.

Definition at line 188 of file OptTable.h.

Referenced by getOption(), and OptTable().

◆ getOption()

const Option OptTable::getOption ( OptSpecifier Opt) const

Get the given Opt's Option instance, lazily creating it if necessary.

Returns
The option, or null for the INVALID option id.

Definition at line 138 of file OptTable.cpp.

References assert(), llvm::opt::OptSpecifier::getID(), and getNumOptions().

Referenced by getOptionHelpName(), and OptTable().

◆ getOptionGroupID()

unsigned llvm::opt::OptTable::getOptionGroupID ( OptSpecifier id) const
inline

Get the group id for the given option.

Definition at line 225 of file OptTable.h.

Referenced by getOptionHelpGroup().

◆ getOptionHelpText() [1/2]

const char * llvm::opt::OptTable::getOptionHelpText ( OptSpecifier id) const
inline

Get the help text to use to describe this option.

Definition at line 230 of file OptTable.h.

References getOptionHelpText().

Referenced by getOptionHelpGroup(), and getOptionHelpText().

◆ getOptionHelpText() [2/2]

const char * llvm::opt::OptTable::getOptionHelpText ( OptSpecifier id,
Visibility VisibilityMask ) const
inline

◆ getOptionKind()

unsigned llvm::opt::OptTable::getOptionKind ( OptSpecifier id) const
inline

Get the kind of the given option.

Definition at line 220 of file OptTable.h.

◆ getOptionMetaVar()

const char * llvm::opt::OptTable::getOptionMetaVar ( OptSpecifier id) const
inline

Get the meta-variable name to use when describing this options values in the help text.

Definition at line 249 of file OptTable.h.

Referenced by getOptionHelpName().

◆ getOptionName()

StringRef llvm::opt::OptTable::getOptionName ( OptSpecifier id) const
inline

Lookup the name of the given option.

Definition at line 197 of file OptTable.h.

◆ getOptionPrefix()

StringRef llvm::opt::OptTable::getOptionPrefix ( OptSpecifier id) const
inline

Lookup the prefix of the given option.

Definition at line 202 of file OptTable.h.

References I.

◆ getOptionPrefixedName()

StringRef llvm::opt::OptTable::getOptionPrefixedName ( OptSpecifier id) const
inline

Lookup the prefixed name of the given option.

Definition at line 215 of file OptTable.h.

◆ getPrefixesTable()

ArrayRef< StringTable::Offset > llvm::opt::OptTable::getPrefixesTable ( ) const
inline

Return the prefixes table used for option names.

Definition at line 183 of file OptTable.h.

◆ getStrTable()

const StringTable & llvm::opt::OptTable::getStrTable ( ) const
inline

Return the string table used for option names.

Definition at line 180 of file OptTable.h.

◆ ParseArgs() [1/2]

InputArgList OptTable::ParseArgs ( ArrayRef< const char * > Args,
unsigned & MissingArgIndex,
unsigned & MissingArgCount,
unsigned FlagsToInclude,
unsigned FlagsToExclude = 0 ) const

Definition at line 511 of file OptTable.cpp.

References llvm::opt::Option::hasFlag().

◆ ParseArgs() [2/2]

InputArgList OptTable::ParseArgs ( ArrayRef< const char * > Args,
unsigned & MissingArgIndex,
unsigned & MissingArgCount,
Visibility VisibilityMask = Visibility() ) const

Parse an list of arguments into an InputArgList.

The resulting InputArgList will reference the strings in [ArgBegin, ArgEnd), and their lifetime should extend past that of the returned InputArgList.

The only error that can occur in this routine is if an argument is missing values; in this case MissingArgCount will be non-zero.

Parameters
MissingArgIndex- On error, the index of the option which could not be parsed.
MissingArgCount- On error, the number of missing options.
VisibilityMask- Only include options with any of these visibility flags set.
Returns
An InputArgList; on error this will contain all the options which could be parsed.

Definition at line 500 of file OptTable.cpp.

References llvm::opt::Option::hasVisibilityFlag().

Referenced by parseArgs().

◆ parseArgs()

InputArgList OptTable::parseArgs ( int Argc,
char *const * Argv,
OptSpecifier Unknown,
StringSaver & Saver,
std::function< void(StringRef)> ErrorFn ) const

A convenience helper which handles optional initial options populated from an environment variable, expands response files recursively and parses options.

Parameters
ErrorFn- Called on a formatted error message for missing arguments or unknown options.
Returns
An InputArgList; on error this will contain all the options which could be parsed.

Definition at line 582 of file OptTable.cpp.

References A(), llvm::ArrayRef(), llvm::cl::expandResponseFiles(), findNearest(), ParseArgs(), and llvm::Unknown.

◆ ParseOneArg() [1/2]

std::unique_ptr< Arg > OptTable::ParseOneArg ( const ArgList & Args,
unsigned & Index,
unsigned FlagsToInclude,
unsigned FlagsToExclude ) const

Definition at line 423 of file OptTable.cpp.

References llvm::opt::Option::hasFlag().

◆ ParseOneArg() [2/2]

std::unique_ptr< Arg > OptTable::ParseOneArg ( const ArgList & Args,
unsigned & Index,
Visibility VisibilityMask = Visibility() ) const

Parse a single argument; returning the new argument and updating Index.

Parameters
[in,out]Index- The current parsing position in the argument string list; on return this will be the index of the next argument string to parse.
[in]VisibilityMask- Only include options with any of these visibility flags set.
Returns
The parsed argument, or 0 if the argument is missing values (in which case Index still points at the conceptual next argument string to parse).

Definition at line 416 of file OptTable.cpp.

References llvm::opt::Option::hasVisibilityFlag().

◆ printHelp() [1/2]

void OptTable::printHelp ( raw_ostream & OS,
const char * Usage,
const char * Title,
bool ShowHidden = false,
bool ShowAllAliases = false,
Visibility VisibilityMask = Visibility() ) const

Render the help text for an option table.

Parameters
OS- The stream to write the help text to.
Usage- USAGE: Usage
Title- OVERVIEW: Title
VisibilityMask- Only in Visibility VisibilityMask,clude options with any of these visibility flags set.
ShowHidden- If true, display options marked as HelpHidden
ShowAllAliases- If true, display all options including aliases that don't have help texts. By default, we display only options that are not hidden and have help texts.

Definition at line 716 of file OptTable.cpp.

References llvm::opt::OptTable::Info::Visibility.

◆ printHelp() [2/2]

void OptTable::printHelp ( raw_ostream & OS,
const char * Usage,
const char * Title,
unsigned FlagsToInclude,
unsigned FlagsToExclude,
bool ShowAllAliases ) const

Definition at line 727 of file OptTable.cpp.

References llvm::opt::OptTable::Info::Flags, and llvm::opt::HelpHidden.

◆ setDashDashParsing()

void llvm::opt::OptTable::setDashDashParsing ( bool Value)
inline

Set whether "--" stops option parsing and treats all subsequent arguments as positional.

E.g. – -a -b gives two positional inputs.

Definition at line 261 of file OptTable.h.

◆ setGroupedShortOptions()

void llvm::opt::OptTable::setGroupedShortOptions ( bool Value)
inline

Support grouped short options. e.g. -ab represents -a -b.

Definition at line 257 of file OptTable.h.

◆ setInitialOptionsFromEnvironment()

void llvm::opt::OptTable::setInitialOptionsFromEnvironment ( const char * E)
inline

Specify the environment variable where initial options should be read.

Definition at line 254 of file OptTable.h.

References E().

◆ suggestValueCompletions()

std::vector< std::string > OptTable::suggestValueCompletions ( StringRef Option,
StringRef Arg ) const

Find possible value for given flags.

This is used for shell autocompletion.

Parameters
[in]Option- Key flag like "-stdlib=" when "-stdlib=l" was passed to clang.
[in]Arg- Value which we want to autocomplete like "l" when "-stdlib=l" was passed to clang.
Returns
The vector of possible values.

Definition at line 190 of file OptTable.cpp.

References FirstSearchableIndex, I, optionMatches(), and llvm::StringRef::split().

Member Data Documentation

◆ FirstSearchableIndex

unsigned llvm::opt::OptTable::FirstSearchableIndex = 0
protected

The index of the first option which can be parsed (i.e., is not a special option like 'input' or 'unknown', and is not an option group).

Definition at line 147 of file OptTable.h.

Referenced by findByPrefix(), llvm::opt::GenericOptTable::GenericOptTable(), OptTable(), and suggestValueCompletions().

◆ PrefixChars

SmallString<8> llvm::opt::OptTable::PrefixChars
protected

The union of the first element of all option prefixes.

Definition at line 154 of file OptTable.h.

Referenced by buildPrefixChars().

◆ PrefixesUnion

SmallVector<StringRef> llvm::opt::OptTable::PrefixesUnion
protected

The union of all option prefixes.

If an argument does not begin with one of these, it is an input.

Definition at line 151 of file OptTable.h.

Referenced by buildPrefixChars(), llvm::opt::GenericOptTable::GenericOptTable(), and llvm::opt::PrecomputedOptTable::PrecomputedOptTable().


The documentation for this class was generated from the following files: