LLVM 24.0.0git
CommandLine.cpp File Reference
#include "llvm/Support/CommandLine.h"
#include "DebugOptions.h"
#include "llvm-c/Support.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Config/config.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/TypeSize.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdlib>
#include <optional>
#include <string>

Go to the source code of this file.

Namespaces

namespace  llvm
 This is an optimization pass for GlobalISel generic memory operations.
namespace  llvm::cl
 This namespace contains all of the command line option processing machinery.

Macros

#define DEBUG_TYPE   "commandline"
#define PRINT_OPT_DIFF(T)
#define LLVM_IS_DEBUG_BUILD   0

Typedefs

using OptionsMapTy = DenseMap<StringRef, Option *>

Functions

void opt_bool_anchor ()
void opt_char_anchor ()
void opt_int_anchor ()
void opt_unsigned_anchor ()
static size_t argPlusPrefixesSize (StringRef ArgName, size_t Pad=DefaultPad)
static SmallString< 8 > argPrefix (StringRef ArgName, size_t Pad=DefaultPad)
static CommandLineParser & globalParser ()
template<typename T, T TrueVal, T FalseVal>
static bool parseBool (Option &O, StringRef ArgName, StringRef Arg, T &Value)
static Option * LookupNearestOption (StringRef Arg, const OptionsMapTy &OptionsMap, std::string &NearestString)
 LookupNearestOption - Lookup the closest match to the option specified by the specified option on the command line.
static bool CommaSeparateAndAddOccurrence (Option *Handler, unsigned pos, StringRef ArgName, StringRef Value)
 CommaSeparateAndAddOccurrence - A wrapper around Handler->addOccurrence() that does special handling of cl::CommaSeparated options.
static bool ProvideOption (Option *Handler, StringRef ArgName, StringRef Value, int argc, const char *const *argv, int &i)
 ProvideOption - For Value, this differentiates between an empty value ("") and a null value (StringRef()).
static Option * findPrefixOption (StringRef Name, size_t &Length, const OptionsMapTy &OptionsMap)
static Option * HandlePrefixedOption (StringRef &Arg, StringRef &Value, const OptionsMapTy &OptionsMap)
 The specified argument string (which started with at least one '-') does not fully match an available option.
static bool RequiresValue (const Option *O)
static bool EatsUnboundedNumberOfValues (const Option *O)
static bool isWhitespace (char C)
static bool isWhitespaceOrNull (char C)
static bool isQuote (char C)
static size_t parseBackslash (StringRef Src, size_t I, SmallString< 128 > &Token)
 Backslashes are interpreted in a rather complicated way in the Windows-style command line, because backslashes are used both to separate path and to escape double quote.
static bool isWindowsSpecialChar (char C)
static bool isWindowsSpecialCharInCommandName (char C)
static void tokenizeWindowsCommandLineImpl (StringRef Src, StringSaver &Saver, function_ref< void(StringRef)> AddToken, bool AlwaysCopy, function_ref< void()> MarkEOL, bool InitialCommandName)
static bool hasUTF8ByteOrderMark (ArrayRef< char > S)
static void ExpandBasePaths (StringRef BasePath, StringSaver &Saver, const char *&Arg)
static void initCommonOptions ()
static StringRef getValueStr (const Option &O, StringRef DefaultMsg)
template<typename FixedOrScalableQuantityT>
static bool parseFixedOrScalableQuantity (Option &O, StringRef Arg, StringRef ValueKind, FixedOrScalableQuantityT &Value)
static bool parseDouble (Option &O, StringRef Arg, double &Value)
static size_t getOptionPrefixesSize ()
static bool shouldPrintOption (StringRef Name, StringRef Description, const Option &O)
static raw_ostream & llvm::cl::operator<< (raw_ostream &OS, boolOrDefault V)
static int OptNameCompare (const std::pair< const char *, Option * > *LHS, const std::pair< const char *, Option * > *RHS)
static int SubNameCompare (const std::pair< const char *, SubCommand * > *LHS, const std::pair< const char *, SubCommand * > *RHS)
static void sortOpts (OptionsMapTy &OptMap, SmallVectorImpl< std::pair< const char *, Option * > > &Opts, bool ShowHidden)
static void sortSubCommands (const SmallPtrSetImpl< SubCommand * > &SubMap, SmallVectorImpl< std::pair< const char *, SubCommand * > > &Subs)
void LLVMParseCommandLineOptions (int argc, const char *const *argv, const char *Overview)
 This function parses the given arguments using the LLVM command line parser.

Variables

static const size_t DefaultPad = 2
static StringRef ArgPrefix = "-"
static StringRef ArgPrefixLong = "--"
static StringRef ArgHelpPrefix = " - "
static StringRef EqValue = "=<value>"
static StringRef EmptyOption = "<empty>"
static StringRef OptionPrefix = " ="
static const size_t MaxOptWidth = 8
static ManagedStatic< CommandLineCommonOptions > CommonOptions

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "commandline"

Definition at line 52 of file CommandLine.cpp.

◆ LLVM_IS_DEBUG_BUILD

#define LLVM_IS_DEBUG_BUILD   0

Definition at line 2485 of file CommandLine.cpp.

◆ PRINT_OPT_DIFF

#define PRINT_OPT_DIFF ( T)
Value:
size_t GlobalWidth) const { \
printOptionName(O, GlobalWidth); \
std::string Str; \
{ \
raw_string_ostream SS(Str); \
SS << V; \
} \
outs() << "= " << Str; \
size_t NumSpaces = \
MaxOptWidth > Str.size() ? MaxOptWidth - Str.size() : 0; \
outs().indent(NumSpaces) << " (default: "; \
if (D.hasValue()) \
outs() << D.getValue(); \
else \
outs() << "*no default*"; \
outs() << ")\n"; \
}
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static const size_t MaxOptWidth
A raw_ostream that writes to an std::string.

Definition at line 2147 of file CommandLine.cpp.

Typedef Documentation

◆ OptionsMapTy

Definition at line 143 of file CommandLine.cpp.

Function Documentation

◆ argPlusPrefixesSize()

◆ argPrefix()

SmallString< 8 > argPrefix ( StringRef ArgName,
size_t Pad = DefaultPad )
static

◆ CommaSeparateAndAddOccurrence()

bool CommaSeparateAndAddOccurrence ( Option * Handler,
unsigned pos,
StringRef ArgName,
StringRef Value )
static

CommaSeparateAndAddOccurrence - A wrapper around Handler->addOccurrence() that does special handling of cl::CommaSeparated options.

Definition at line 612 of file CommandLine.cpp.

References llvm::cl::Option::addOccurrence(), llvm::cl::CommaSeparated, llvm::StringRef::find(), llvm::cl::Option::getMiscFlags(), llvm::StringRef::npos, and llvm::StringRef::substr().

Referenced by ProvideOption().

◆ EatsUnboundedNumberOfValues()

bool EatsUnboundedNumberOfValues ( const Option * O)
static

Definition at line 717 of file CommandLine.cpp.

References llvm::cl::OneOrMore, and llvm::cl::ZeroOrMore.

◆ ExpandBasePaths()

◆ findPrefixOption()

Option * findPrefixOption ( StringRef Name,
size_t & Length,
const OptionsMapTy & OptionsMap )
static

◆ getOptionPrefixesSize()

size_t getOptionPrefixesSize ( )
static

◆ getValueStr()

StringRef getValueStr ( const Option & O,
StringRef DefaultMsg )
static

◆ globalParser()

◆ HandlePrefixedOption()

Option * HandlePrefixedOption ( StringRef & Arg,
StringRef & Value,
const OptionsMapTy & OptionsMap )
static

The specified argument string (which started with at least one '-') does not fully match an available option.

Check to see if this is a prefix option. If so, split arg into output an Arg/Value pair and return the Option to parse it with.

Definition at line 691 of file CommandLine.cpp.

References llvm::StringRef::drop_front(), findPrefixOption(), llvm::cl::Option::getFormattingFlag(), llvm::Length, llvm::cl::Prefix, llvm::StringRef::size(), llvm::StringRef::starts_with(), and llvm::StringRef::substr().

◆ hasUTF8ByteOrderMark()

bool hasUTF8ByteOrderMark ( ArrayRef< char > S)
static

Definition at line 1027 of file CommandLine.cpp.

References llvm::ArrayRef< T >::size().

◆ initCommonOptions()

◆ isQuote()

bool isQuote ( char C)
static

Definition at line 730 of file CommandLine.cpp.

References C().

Referenced by llvm::cl::TokenizeGNUCommandLine().

◆ isWhitespace()

bool isWhitespace ( char C)
static

◆ isWhitespaceOrNull()

bool isWhitespaceOrNull ( char C)
static

◆ isWindowsSpecialChar()

bool isWindowsSpecialChar ( char C)
static

Definition at line 836 of file CommandLine.cpp.

References C(), and isWhitespaceOrNull().

Referenced by tokenizeWindowsCommandLineImpl().

◆ isWindowsSpecialCharInCommandName()

bool isWindowsSpecialCharInCommandName ( char C)
static

Definition at line 839 of file CommandLine.cpp.

References C(), and isWhitespaceOrNull().

Referenced by tokenizeWindowsCommandLineImpl().

◆ LookupNearestOption()

Option * LookupNearestOption ( StringRef Arg,
const OptionsMapTy & OptionsMap,
std::string & NearestString )
static

LookupNearestOption - Lookup the closest match to the option specified by the specified option on the command line.

If there is a value specified (after an equal sign) return that as well. This assumes that leading dashes have already been stripped.

Definition at line 566 of file CommandLine.cpp.

References _, llvm::StringRef::edit_distance(), llvm::StringRef::empty(), LHS, llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::cl::ReallyHidden, RHS, llvm::StringRef::split(), and llvm::cl::ValueDisallowed.

◆ opt_bool_anchor()

void opt_bool_anchor ( )

Definition at line 114 of file CommandLine.cpp.

◆ opt_char_anchor()

void opt_char_anchor ( )

Definition at line 115 of file CommandLine.cpp.

◆ opt_int_anchor()

void opt_int_anchor ( )

Definition at line 116 of file CommandLine.cpp.

◆ opt_unsigned_anchor()

void opt_unsigned_anchor ( )

Definition at line 117 of file CommandLine.cpp.

◆ OptNameCompare()

int OptNameCompare ( const std::pair< const char *, Option * > * LHS,
const std::pair< const char *, Option * > * RHS )
static

Definition at line 2221 of file CommandLine.cpp.

References LHS, and RHS.

Referenced by sortOpts().

◆ parseBackslash()

size_t parseBackslash ( StringRef Src,
size_t I,
SmallString< 128 > & Token )
static

Backslashes are interpreted in a rather complicated way in the Windows-style command line, because backslashes are used both to separate path and to escape double quote.

This method consumes runs of backslashes as well as the following double quote if it's escaped.

  • If an even number of backslashes is followed by a double quote, one backslash is output for every pair of backslashes, and the last double quote remains unconsumed. The double quote will later be interpreted as the start or end of a quoted string in the main loop outside of this function.
  • If an odd number of backslashes is followed by a double quote, one backslash is output for every pair of backslashes, and a double quote is output for the last pair of backslash-double quote. The double quote is consumed in this case.
  • Otherwise, backslashes are interpreted literally.

Definition at line 812 of file CommandLine.cpp.

References llvm::SmallString< InternalLen >::append(), E(), I, and llvm::SmallVectorTemplateBase< T, bool >::push_back().

Referenced by tokenizeWindowsCommandLineImpl().

◆ parseBool()

template<typename T, T TrueVal, T FalseVal>
bool parseBool ( Option & O,
StringRef ArgName,
StringRef Arg,
T & Value )
static

Definition at line 399 of file CommandLine.cpp.

References llvm::StringRef::data(), and T.

◆ parseDouble()

bool parseDouble ( Option & O,
StringRef Arg,
double & Value )
static

Definition at line 1984 of file CommandLine.cpp.

References llvm::to_float().

◆ parseFixedOrScalableQuantity()

template<typename FixedOrScalableQuantityT>
bool parseFixedOrScalableQuantity ( Option & O,
StringRef Arg,
StringRef ValueKind,
FixedOrScalableQuantityT & Value )
static

◆ ProvideOption()

bool ProvideOption ( Option * Handler,
StringRef ArgName,
StringRef Value,
int argc,
const char *const * argv,
int & i )
inlinestatic

ProvideOption - For Value, this differentiates between an empty value ("") and a null value (StringRef()).

The later is accepted for arguments that don't allow a value (-foo) the former is rejected (-foo=).

Definition at line 639 of file CommandLine.cpp.

References llvm::cl::AlwaysPrefix, assert(), CommaSeparateAndAddOccurrence(), llvm::cl::Option::error(), llvm::cl::Option::getFormattingFlag(), llvm::cl::Option::getValueExpectedFlag(), llvm::cl::ValueDisallowed, llvm::cl::ValueOptional, and llvm::cl::ValueRequired.

Referenced by llvm::cl::ProvidePositionalOption().

◆ RequiresValue()

bool RequiresValue ( const Option * O)
static

Definition at line 712 of file CommandLine.cpp.

References llvm::cl::OneOrMore, and llvm::cl::Required.

◆ shouldPrintOption()

bool shouldPrintOption ( StringRef Name,
StringRef Description,
const Option & O )
static

◆ sortOpts()

◆ sortSubCommands()

void sortSubCommands ( const SmallPtrSetImpl< SubCommand * > & SubMap,
SmallVectorImpl< std::pair< const char *, SubCommand * > > & Subs )
static

Definition at line 2259 of file CommandLine.cpp.

References llvm::array_pod_sort(), and SubNameCompare().

◆ SubNameCompare()

int SubNameCompare ( const std::pair< const char *, SubCommand * > * LHS,
const std::pair< const char *, SubCommand * > * RHS )
static

Definition at line 2226 of file CommandLine.cpp.

References LHS, and RHS.

Referenced by sortSubCommands().

◆ tokenizeWindowsCommandLineImpl()

Variable Documentation

◆ ArgHelpPrefix

◆ ArgPrefix

StringRef ArgPrefix = "-"
static

Definition at line 123 of file CommandLine.cpp.

Referenced by argPlusPrefixesSize(), and argPrefix().

◆ ArgPrefixLong

StringRef ArgPrefixLong = "--"
static

Definition at line 124 of file CommandLine.cpp.

Referenced by argPlusPrefixesSize(), and argPrefix().

◆ CommonOptions

◆ DefaultPad

const size_t DefaultPad = 2
static

Definition at line 121 of file CommandLine.cpp.

Referenced by argPlusPrefixesSize(), and argPrefix().

◆ EmptyOption

StringRef EmptyOption = "<empty>"
static

◆ EqValue

StringRef EqValue = "=<value>"
static

◆ MaxOptWidth

const size_t MaxOptWidth = 8
static

◆ OptionPrefix

StringRef OptionPrefix = " ="
static