2017-06-16 02:43:26 +02:00
|
|
|
//===- OptTable.h - Option Table --------------------------------*- C++ -*-===//
|
2012-12-05 01:29:32 +01:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-12-05 01:29:32 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 01:45:19 +01:00
|
|
|
#ifndef LLVM_OPTION_OPTTABLE_H
|
|
|
|
#define LLVM_OPTION_OPTTABLE_H
|
2012-12-05 01:29:32 +01:00
|
|
|
|
2015-06-21 08:31:53 +02:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2017-06-16 02:43:26 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-12-05 01:29:32 +01:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
|
|
|
#include "llvm/Option/OptSpecifier.h"
|
2020-08-04 17:51:24 +02:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2017-06-16 02:43:26 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-12-05 01:29:32 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
2017-06-16 02:43:26 +02:00
|
|
|
|
2012-12-05 01:29:32 +01:00
|
|
|
class raw_ostream;
|
2020-08-04 17:51:24 +02:00
|
|
|
template <typename Fn> class function_ref;
|
2017-06-16 02:43:26 +02:00
|
|
|
|
2012-12-05 01:29:32 +01:00
|
|
|
namespace opt {
|
2017-06-16 02:43:26 +02:00
|
|
|
|
2012-12-05 01:29:32 +01:00
|
|
|
class Arg;
|
|
|
|
class ArgList;
|
|
|
|
class InputArgList;
|
|
|
|
class Option;
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Provide access to the Option info table.
|
2012-12-05 01:29:32 +01:00
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
class OptTable {
|
|
|
|
public:
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Entry for a single option instance in the option data table.
|
2012-12-05 01:29:32 +01:00
|
|
|
struct Info {
|
|
|
|
/// A null terminated array of prefix strings to apply to name while
|
|
|
|
/// matching.
|
|
|
|
const char *const *Prefixes;
|
|
|
|
const char *Name;
|
|
|
|
const char *HelpText;
|
|
|
|
const char *MetaVar;
|
|
|
|
unsigned ID;
|
|
|
|
unsigned char Kind;
|
|
|
|
unsigned char Param;
|
2020-09-11 11:17:31 +02:00
|
|
|
unsigned int Flags;
|
2012-12-05 01:29:32 +01:00
|
|
|
unsigned short GroupID;
|
|
|
|
unsigned short AliasID;
|
2013-08-01 00:44:41 +02:00
|
|
|
const char *AliasArgs;
|
2017-06-20 18:31:31 +02:00
|
|
|
const char *Values;
|
2012-12-05 01:29:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2018-05-01 17:54:18 +02:00
|
|
|
/// The option information table.
|
2017-08-29 02:09:31 +02:00
|
|
|
std::vector<Info> OptionInfos;
|
2013-08-28 22:04:31 +02:00
|
|
|
bool IgnoreCase;
|
2020-07-17 18:18:24 +02:00
|
|
|
bool GroupedShortOptions = false;
|
2020-08-04 17:51:24 +02:00
|
|
|
const char *EnvVar = nullptr;
|
2012-12-05 01:29:32 +01:00
|
|
|
|
2017-06-16 02:43:26 +02:00
|
|
|
unsigned TheInputOptionID = 0;
|
|
|
|
unsigned TheUnknownOptionID = 0;
|
2012-12-05 01:29:32 +01:00
|
|
|
|
|
|
|
/// 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).
|
2017-06-16 02:43:26 +02:00
|
|
|
unsigned FirstSearchableIndex = 0;
|
2012-12-05 01:29:32 +01:00
|
|
|
|
|
|
|
/// The union of all option prefixes. If an argument does not begin with
|
|
|
|
/// one of these, it is an input.
|
|
|
|
StringSet<> PrefixesUnion;
|
|
|
|
std::string PrefixChars;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Info &getInfo(OptSpecifier Opt) const {
|
|
|
|
unsigned id = Opt.getID();
|
|
|
|
assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
|
|
|
|
return OptionInfos[id - 1];
|
|
|
|
}
|
|
|
|
|
2020-07-17 18:18:24 +02:00
|
|
|
Arg *parseOneArgGrouped(InputArgList &Args, unsigned &Index) const;
|
|
|
|
|
2012-12-05 01:29:32 +01:00
|
|
|
protected:
|
2015-10-21 18:30:42 +02:00
|
|
|
OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false);
|
2015-03-16 19:06:57 +01:00
|
|
|
|
2012-12-05 01:29:32 +01:00
|
|
|
public:
|
|
|
|
~OptTable();
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Return the total number of option classes.
|
2015-10-21 18:30:42 +02:00
|
|
|
unsigned getNumOptions() const { return OptionInfos.size(); }
|
2012-12-05 01:29:32 +01:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the given Opt's Option instance, lazily creating it
|
2012-12-05 01:29:32 +01:00
|
|
|
/// if necessary.
|
|
|
|
///
|
|
|
|
/// \return The option, or null for the INVALID option id.
|
|
|
|
const Option getOption(OptSpecifier Opt) const;
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Lookup the name of the given option.
|
2012-12-05 01:29:32 +01:00
|
|
|
const char *getOptionName(OptSpecifier id) const {
|
|
|
|
return getInfo(id).Name;
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the kind of the given option.
|
2012-12-05 01:29:32 +01:00
|
|
|
unsigned getOptionKind(OptSpecifier id) const {
|
|
|
|
return getInfo(id).Kind;
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the group id for the given option.
|
2012-12-05 01:29:32 +01:00
|
|
|
unsigned getOptionGroupID(OptSpecifier id) const {
|
|
|
|
return getInfo(id).GroupID;
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the help text to use to describe this option.
|
2012-12-05 01:29:32 +01:00
|
|
|
const char *getOptionHelpText(OptSpecifier id) const {
|
|
|
|
return getInfo(id).HelpText;
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the meta-variable name to use when describing
|
2012-12-05 01:29:32 +01:00
|
|
|
/// this options values in the help text.
|
|
|
|
const char *getOptionMetaVar(OptSpecifier id) const {
|
|
|
|
return getInfo(id).MetaVar;
|
|
|
|
}
|
|
|
|
|
2020-08-04 17:51:24 +02:00
|
|
|
/// Specify the environment variable where initial options should be read.
|
|
|
|
void setInitialOptionsFromEnvironment(const char *E) { EnvVar = E; }
|
|
|
|
|
2020-07-17 18:18:24 +02:00
|
|
|
/// Support grouped short options. e.g. -ab represents -a -b.
|
|
|
|
void setGroupedShortOptions(bool Value) { GroupedShortOptions = Value; }
|
|
|
|
|
2017-06-20 18:31:31 +02:00
|
|
|
/// Find possible value for given flags. This is used for shell
|
|
|
|
/// autocompletion.
|
|
|
|
///
|
|
|
|
/// \param [in] Option - Key flag like "-stdlib=" when "-stdlib=l"
|
|
|
|
/// was passed to clang.
|
|
|
|
///
|
|
|
|
/// \param [in] Arg - Value which we want to autocomplete like "l"
|
|
|
|
/// when "-stdlib=l" was passed to clang.
|
|
|
|
///
|
|
|
|
/// \return The vector of possible values.
|
|
|
|
std::vector<std::string> suggestValueCompletions(StringRef Option,
|
|
|
|
StringRef Arg) const;
|
|
|
|
|
2017-05-23 20:39:08 +02:00
|
|
|
/// Find flags from OptTable which starts with Cur.
|
|
|
|
///
|
|
|
|
/// \param [in] Cur - String prefix that all returned flags need
|
|
|
|
// to start with.
|
|
|
|
///
|
|
|
|
/// \return The vector of flags which start with Cur.
|
2017-07-08 19:48:59 +02:00
|
|
|
std::vector<std::string> findByPrefix(StringRef Cur,
|
2020-09-11 13:17:51 +02:00
|
|
|
unsigned int DisableFlags) const;
|
2017-05-23 20:39:08 +02:00
|
|
|
|
[Option] Add 'findNearest' method to catch typos
Summary:
Add a method `OptTable::findNearest`, which allows users of OptTable to
check user input for misspelled options. In addition, have llvm-mt
check for misspelled options. For example, if a user invokes
`llvm-mt /oyt:foo`, the error message will indicate that while an
option named `/oyt:` does not exist, `/out:` does.
The method ports the functionality of the `LookupNearestOption` method
from LLVM CommandLine to libLLVMOption. This allows tools like Clang
and Swift, which do not use CommandLine, to use this functionality to
suggest similarly spelled options.
As room for future improvement, the new method as-is cannot yet properly suggest
nearby "joined" options -- that is, for an option string "-FozBar", where
"-Foo" is the correct option name and "Bar" is the value being passed along
with the misspelled option, this method will calculate an edit distance of 4,
by deleting "Bar" and changing "z" to "o". It should instead calculate an edit
distance of just 1, by changing "z" to "o" and recognizing "Bar" as a
value. This commit includes a disabled test that expresses this limitation.
Test Plan: `check-llvm`
Reviewers: yamaguchi, v.g.vassilev, teemperor, ruiu, jroelofs
Reviewed By: jroelofs
Subscribers: jroelofs, llvm-commits
Differential Revision: https://reviews.llvm.org/D41732
llvm-svn: 321877
2018-01-05 18:10:39 +01:00
|
|
|
/// Find the OptTable option that most closely matches the given string.
|
|
|
|
///
|
|
|
|
/// \param [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".
|
|
|
|
/// \param [out] NearestString - The nearest option string found in the
|
|
|
|
/// OptTable.
|
|
|
|
/// \param [in] FlagsToInclude - Only find options with any of these flags.
|
|
|
|
/// Zero is the default, which includes all flags.
|
|
|
|
/// \param [in] FlagsToExclude - Don't find options with this flag. Zero
|
|
|
|
/// is the default, and means exclude nothing.
|
|
|
|
/// \param [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".
|
|
|
|
///
|
|
|
|
/// \return The edit distance of the nearest string found.
|
|
|
|
unsigned findNearest(StringRef Option, std::string &NearestString,
|
|
|
|
unsigned FlagsToInclude = 0, unsigned FlagsToExclude = 0,
|
|
|
|
unsigned MinimumLength = 4) const;
|
|
|
|
|
2017-08-29 02:09:31 +02:00
|
|
|
/// Add Values to Option's Values class
|
|
|
|
///
|
|
|
|
/// \param [in] Option - Prefix + Name of the flag which Values will be
|
|
|
|
/// changed. For example, "-analyzer-checker".
|
|
|
|
/// \param [in] Values - String of Values seperated by ",", such as
|
|
|
|
/// "foo, bar..", where foo and bar is the argument which the Option flag
|
|
|
|
/// takes
|
|
|
|
///
|
|
|
|
/// \return true in success, and false in fail.
|
|
|
|
bool addValues(const char *Option, const char *Values);
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Parse a single argument; returning the new argument and
|
2012-12-05 01:29:32 +01:00
|
|
|
/// updating Index.
|
|
|
|
///
|
|
|
|
/// \param [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.
|
2013-07-19 20:04:57 +02:00
|
|
|
/// \param [in] FlagsToInclude - Only parse options with any of these flags.
|
|
|
|
/// Zero is the default which includes all flags.
|
|
|
|
/// \param [in] FlagsToExclude - Don't parse options with this flag. Zero
|
|
|
|
/// is the default and means exclude nothing.
|
2012-12-05 01:29:32 +01:00
|
|
|
///
|
|
|
|
/// \return 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).
|
2013-07-19 20:04:57 +02:00
|
|
|
Arg *ParseOneArg(const ArgList &Args, unsigned &Index,
|
|
|
|
unsigned FlagsToInclude = 0,
|
|
|
|
unsigned FlagsToExclude = 0) const;
|
2012-12-05 01:29:32 +01:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Parse an list of arguments into an InputArgList.
|
2012-12-05 01:29:32 +01:00
|
|
|
///
|
|
|
|
/// The resulting InputArgList will reference the strings in [\p ArgBegin,
|
|
|
|
/// \p 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 \p MissingArgCount will be non-zero.
|
|
|
|
///
|
|
|
|
/// \param MissingArgIndex - On error, the index of the option which could
|
|
|
|
/// not be parsed.
|
|
|
|
/// \param MissingArgCount - On error, the number of missing options.
|
2013-07-19 20:04:57 +02:00
|
|
|
/// \param FlagsToInclude - Only parse options with any of these flags.
|
|
|
|
/// Zero is the default which includes all flags.
|
|
|
|
/// \param FlagsToExclude - Don't parse options with this flag. Zero
|
|
|
|
/// is the default and means exclude nothing.
|
2012-12-05 01:29:32 +01:00
|
|
|
/// \return An InputArgList; on error this will contain all the options
|
|
|
|
/// which could be parsed.
|
2015-06-23 00:06:37 +02:00
|
|
|
InputArgList ParseArgs(ArrayRef<const char *> Args, unsigned &MissingArgIndex,
|
|
|
|
unsigned &MissingArgCount, unsigned FlagsToInclude = 0,
|
|
|
|
unsigned FlagsToExclude = 0) const;
|
2012-12-05 01:29:32 +01:00
|
|
|
|
2020-08-04 17:51:24 +02:00
|
|
|
/// A convenience helper which handles optional initial options populated from
|
|
|
|
/// an environment variable, expands response files recursively and parses
|
|
|
|
/// options.
|
|
|
|
///
|
|
|
|
/// \param ErrorFn - Called on a formatted error message for missing arguments
|
|
|
|
/// or unknown options.
|
|
|
|
/// \return An InputArgList; on error this will contain all the options which
|
|
|
|
/// could be parsed.
|
|
|
|
InputArgList parseArgs(int Argc, char *const *Argv, OptSpecifier Unknown,
|
|
|
|
StringSaver &Saver,
|
|
|
|
function_ref<void(StringRef)> ErrorFn) const;
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Render the help text for an option table.
|
2012-12-05 01:29:32 +01:00
|
|
|
///
|
|
|
|
/// \param OS - The stream to write the help text to.
|
2018-10-10 02:15:31 +02:00
|
|
|
/// \param Usage - USAGE: Usage
|
|
|
|
/// \param Title - OVERVIEW: Title
|
2013-06-13 20:12:12 +02:00
|
|
|
/// \param FlagsToInclude - If non-zero, only include options with any
|
|
|
|
/// of these flags set.
|
|
|
|
/// \param FlagsToExclude - Exclude options with any of these flags set.
|
2017-07-26 11:09:56 +02:00
|
|
|
/// \param 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.
|
2021-06-24 23:47:03 +02:00
|
|
|
void printHelp(raw_ostream &OS, const char *Usage, const char *Title,
|
2017-07-26 11:09:56 +02:00
|
|
|
unsigned FlagsToInclude, unsigned FlagsToExclude,
|
|
|
|
bool ShowAllAliases) const;
|
|
|
|
|
2021-06-24 23:47:03 +02:00
|
|
|
void printHelp(raw_ostream &OS, const char *Usage, const char *Title,
|
2017-07-26 11:09:56 +02:00
|
|
|
bool ShowHidden = false, bool ShowAllAliases = false) const;
|
2012-12-05 01:29:32 +01:00
|
|
|
};
|
2017-06-16 02:43:26 +02:00
|
|
|
|
2012-12-05 01:29:32 +01:00
|
|
|
} // end namespace opt
|
2017-06-16 02:43:26 +02:00
|
|
|
|
2012-12-05 01:29:32 +01:00
|
|
|
} // end namespace llvm
|
|
|
|
|
2017-06-16 02:43:26 +02:00
|
|
|
#endif // LLVM_OPTION_OPTTABLE_H
|