LLVM  14.0.0git
Option.h
Go to the documentation of this file.
1 //===- Option.h - Abstract Driver Options -----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_OPTION_OPTION_H
10 #define LLVM_OPTION_OPTION_H
11 
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Option/OptTable.h"
17 #include <cassert>
18 #include <string>
19 
20 namespace llvm {
21 
22 class raw_ostream;
23 
24 namespace opt {
25 
26 class Arg;
27 class ArgList;
28 
29 /// ArgStringList - Type used for constructing argv lists for subprocesses.
31 
32 /// Base flags for all options. Custom flags may be added after.
33 enum DriverFlag {
34  HelpHidden = (1 << 0),
35  RenderAsInput = (1 << 1),
36  RenderJoined = (1 << 2),
37  RenderSeparate = (1 << 3)
38 };
39 
40 /// Option - Abstract representation for a single form of driver
41 /// argument.
42 ///
43 /// An Option class represents a form of option that the driver
44 /// takes, for example how many arguments the option has and how
45 /// they can be provided. Individual option instances store
46 /// additional information about what group the option is a member
47 /// of (if any), if the option is an alias, and a number of
48 /// flags. At runtime the driver parses the command line into
49 /// concrete Arg instances, each of which corresponds to a
50 /// particular Option instance.
51 class Option {
52 public:
53  enum OptionClass {
67  };
68 
74  };
75 
76 protected:
78  const OptTable *Owner;
79 
80 public:
81  Option(const OptTable::Info *Info, const OptTable *Owner);
82 
83  bool isValid() const {
84  return Info != nullptr;
85  }
86 
87  unsigned getID() const {
88  assert(Info && "Must have a valid info!");
89  return Info->ID;
90  }
91 
92  OptionClass getKind() const {
93  assert(Info && "Must have a valid info!");
94  return OptionClass(Info->Kind);
95  }
96 
97  /// Get the name of this option without any prefix.
98  StringRef getName() const {
99  assert(Info && "Must have a valid info!");
100  return Info->Name;
101  }
102 
103  const Option getGroup() const {
104  assert(Info && "Must have a valid info!");
105  assert(Owner && "Must have a valid owner!");
106  return Owner->getOption(Info->GroupID);
107  }
108 
109  const Option getAlias() const {
110  assert(Info && "Must have a valid info!");
111  assert(Owner && "Must have a valid owner!");
112  return Owner->getOption(Info->AliasID);
113  }
114 
115  /// Get the alias arguments as a \0 separated list.
116  /// E.g. ["foo", "bar"] would be returned as "foo\0bar\0".
117  const char *getAliasArgs() const {
118  assert(Info && "Must have a valid info!");
119  assert((!Info->AliasArgs || Info->AliasArgs[0] != 0) &&
120  "AliasArgs should be either 0 or non-empty.");
121 
122  return Info->AliasArgs;
123  }
124 
125  /// Get the default prefix for this option.
127  const char *Prefix = *Info->Prefixes;
128  return Prefix ? Prefix : StringRef();
129  }
130 
131  /// Get the name of this option with the default prefix.
132  std::string getPrefixedName() const {
133  std::string Ret(getPrefix());
134  Ret += getName();
135  return Ret;
136  }
137 
138  /// Get the help text for this option.
140  assert(Info && "Must have a valid info!");
141  return Info->HelpText;
142  }
143 
144  /// Get the meta-variable list for this option.
146  assert(Info && "Must have a valid info!");
147  return Info->MetaVar;
148  }
149 
150  unsigned getNumArgs() const { return Info->Param; }
151 
152  bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
153 
155  if (Info->Flags & RenderJoined)
156  return RenderJoinedStyle;
157  if (Info->Flags & RenderSeparate)
158  return RenderSeparateStyle;
159  switch (getKind()) {
160  case GroupClass:
161  case InputClass:
162  case UnknownClass:
163  return RenderValuesStyle;
164  case JoinedClass:
166  return RenderJoinedStyle;
167  case CommaJoinedClass:
168  return RenderCommaJoinedStyle;
169  case FlagClass:
170  case ValuesClass:
171  case SeparateClass:
172  case MultiArgClass:
174  case RemainingArgsClass:
176  return RenderSeparateStyle;
177  }
178  llvm_unreachable("Unexpected kind!");
179  }
180 
181  /// Test if this option has the flag \a Val.
182  bool hasFlag(unsigned Val) const {
183  return Info->Flags & Val;
184  }
185 
186  /// getUnaliasedOption - Return the final option this option
187  /// aliases (itself, if the option has no alias).
188  const Option getUnaliasedOption() const {
189  const Option Alias = getAlias();
190  if (Alias.isValid()) return Alias.getUnaliasedOption();
191  return *this;
192  }
193 
194  /// getRenderName - Return the name to use when rendering this
195  /// option.
197  return getUnaliasedOption().getName();
198  }
199 
200  /// matches - Predicate for whether this option is part of the
201  /// given option (which may be a group).
202  ///
203  /// Note that matches against options which are an alias should never be
204  /// done -- aliases do not participate in matching and so such a query will
205  /// always be false.
206  bool matches(OptSpecifier ID) const;
207 
208  /// Potentially accept the current argument, returning a new Arg instance,
209  /// or 0 if the option does not accept this argument (or the argument is
210  /// missing values).
211  ///
212  /// If the option accepts the current argument, accept() sets
213  /// Index to the position where argument parsing should resume
214  /// (even if the argument is missing values).
215  ///
216  /// \p CurArg The argument to be matched. It may be shorter than the
217  /// underlying storage to represent a Joined argument.
218  /// \p GroupedShortOption If true, we are handling the fallback case of
219  /// parsing a prefix of the current argument as a short option.
220  std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
221  bool GroupedShortOption, unsigned &Index) const;
222 
223 private:
224  std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
225  unsigned &Index) const;
226 
227 public:
228  void print(raw_ostream &O) const;
229  void dump() const;
230 };
231 
232 } // end namespace opt
233 
234 } // end namespace llvm
235 
236 #endif // LLVM_OPTION_OPTION_H
llvm::opt::Option::dump
void dump() const
Definition: Option.cpp:90
llvm::opt::OptTable
Provide access to the Option info table.
Definition: OptTable.h:40
llvm::opt::Option::Option
Option(const OptTable::Info *Info, const OptTable *Owner)
Definition: Option.cpp:26
llvm::opt::OptTable::getOption
const Option getOption(OptSpecifier Opt) const
Get the given Opt's Option instance, lazily creating it if necessary.
Definition: OptTable.cpp:162
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::opt::Option::MultiArgClass
@ MultiArgClass
Definition: Option.h:64
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::opt::RenderJoined
@ RenderJoined
Definition: Option.h:36
llvm::opt::Option::CommaJoinedClass
@ CommaJoinedClass
Definition: Option.h:63
StringRef.h
llvm::opt::ArgList
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:116
llvm::SmallVector< const char *, 16 >
llvm::opt::OptTable::Info::Param
unsigned char Param
Definition: OptTable.h:52
ErrorHandling.h
llvm::opt::OptTable::Info::GroupID
unsigned short GroupID
Definition: OptTable.h:54
llvm::opt::Option::RenderCommaJoinedStyle
@ RenderCommaJoinedStyle
Definition: Option.h:70
llvm::opt::Option::getPrefixedName
std::string getPrefixedName() const
Get the name of this option with the default prefix.
Definition: Option.h:132
llvm::opt::Option::getUnaliasedOption
const Option getUnaliasedOption() const
getUnaliasedOption - Return the final option this option aliases (itself, if the option has no alias)...
Definition: Option.h:188
llvm::opt::Option::Info
const OptTable::Info * Info
Definition: Option.h:77
llvm::opt::Option::InputClass
@ InputClass
Definition: Option.h:55
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
llvm::opt::Option::accept
std::unique_ptr< Arg > accept(const ArgList &Args, StringRef CurArg, bool GroupedShortOption, unsigned &Index) const
Potentially accept the current argument, returning a new Arg instance, or 0 if the option does not ac...
Definition: Option.cpp:236
llvm::opt::Option::Owner
const OptTable * Owner
Definition: Option.h:78
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::opt::Option::getAliasArgs
const char * getAliasArgs() const
Get the alias arguments as a \0 separated list.
Definition: Option.h:117
llvm::opt::OptTable::Info::MetaVar
const char * MetaVar
Definition: OptTable.h:49
llvm::opt::OptTable::Info::Name
const char * Name
Definition: OptTable.h:47
llvm::opt::Option::RenderStyleKind
RenderStyleKind
Definition: Option.h:69
llvm::opt::Option::RemainingArgsJoinedClass
@ RemainingArgsJoinedClass
Definition: Option.h:62
llvm::opt::Option::getID
unsigned getID() const
Definition: Option.h:87
llvm::opt::OptTable::Info::AliasID
unsigned short AliasID
Definition: OptTable.h:55
opt
arm prera ldst opt
Definition: ARMLoadStoreOptimizer.cpp:2191
llvm::opt::OptTable::Info::ID
unsigned ID
Definition: OptTable.h:50
llvm::opt::Option::matches
bool matches(OptSpecifier ID) const
matches - Predicate for whether this option is part of the given option (which may be a group).
Definition: Option.cpp:93
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::opt::Option::SeparateClass
@ SeparateClass
Definition: Option.h:60
llvm::opt::Option::JoinedAndSeparateClass
@ JoinedAndSeparateClass
Definition: Option.h:66
llvm::opt::Option::RenderValuesStyle
@ RenderValuesStyle
Definition: Option.h:73
llvm::opt::Option::print
void print(raw_ostream &O) const
Definition: Option.cpp:41
llvm::opt::OptSpecifier
OptSpecifier - Wrapper class for abstracting references to option IDs.
Definition: OptSpecifier.h:18
llvm::opt::Option::getMetaVar
StringRef getMetaVar() const
Get the meta-variable list for this option.
Definition: Option.h:145
llvm::opt::Option::UnknownClass
@ UnknownClass
Definition: Option.h:56
llvm::opt::Option::RenderJoinedStyle
@ RenderJoinedStyle
Definition: Option.h:71
llvm::opt::Option::getKind
OptionClass getKind() const
Definition: Option.h:92
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:197
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
OptTable.h
llvm::opt::Option::getName
StringRef getName() const
Get the name of this option without any prefix.
Definition: Option.h:98
llvm::opt::Option::getPrefix
StringRef getPrefix() const
Get the default prefix for this option.
Definition: Option.h:126
llvm::opt::DriverFlag
DriverFlag
Base flags for all options. Custom flags may be added after.
Definition: Option.h:33
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::opt::Option::isValid
bool isValid() const
Definition: Option.h:83
llvm::opt::Option::ValuesClass
@ ValuesClass
Definition: Option.h:59
llvm::opt::Option::getNumArgs
unsigned getNumArgs() const
Definition: Option.h:150
llvm::opt::Option::RenderSeparateStyle
@ RenderSeparateStyle
Definition: Option.h:72
llvm::opt::Option::GroupClass
@ GroupClass
Definition: Option.h:54
llvm::opt::HelpHidden
@ HelpHidden
Definition: Option.h:34
OptSpecifier.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::opt::Option::RemainingArgsClass
@ RemainingArgsClass
Definition: Option.h:61
llvm::opt::Option::getHelpText
StringRef getHelpText() const
Get the help text for this option.
Definition: Option.h:139
llvm::opt::OptTable::Info::Prefixes
const char *const * Prefixes
A null terminated array of prefix strings to apply to name while matching.
Definition: OptTable.h:46
llvm::opt::RenderAsInput
@ RenderAsInput
Definition: Option.h:35
llvm::opt::OptTable::Info::Kind
unsigned char Kind
Definition: OptTable.h:51
llvm::opt::OptTable::Info::Flags
unsigned int Flags
Definition: OptTable.h:53
llvm::opt::Option::hasFlag
bool hasFlag(unsigned Val) const
Test if this option has the flag Val.
Definition: Option.h:182
llvm::opt::Option
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:51
llvm::opt::Option::JoinedOrSeparateClass
@ JoinedOrSeparateClass
Definition: Option.h:65
llvm::opt::Option::FlagClass
@ FlagClass
Definition: Option.h:57
llvm::opt::OptTable::Info::HelpText
const char * HelpText
Definition: OptTable.h:48
llvm::opt::OptTable::Info::AliasArgs
const char * AliasArgs
Definition: OptTable.h:56
llvm::opt::Option::getGroup
const Option getGroup() const
Definition: Option.h:103
SmallVector.h
llvm::opt::OptTable::Info
Entry for a single option instance in the option data table.
Definition: OptTable.h:43
llvm::opt::Option::OptionClass
OptionClass
Definition: Option.h:53
llvm::opt::Option::hasNoOptAsInput
bool hasNoOptAsInput() const
Definition: Option.h:152
llvm::opt::Option::getRenderStyle
RenderStyleKind getRenderStyle() const
Definition: Option.h:154
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::opt::Option::getAlias
const Option getAlias() const
Definition: Option.h:109
llvm::opt::Option::JoinedClass
@ JoinedClass
Definition: Option.h:58
llvm::opt::Option::getRenderName
StringRef getRenderName() const
getRenderName - Return the name to use when rendering this option.
Definition: Option.h:196
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37
llvm::opt::RenderSeparate
@ RenderSeparate
Definition: Option.h:37