LLVM 22.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
13#include "llvm/ADT/StringRef.h"
18#include <cassert>
19
20namespace llvm {
21
22class raw_ostream;
23
24namespace opt {
25
26class Arg;
27class 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.
34 HelpHidden = (1 << 0),
35 RenderAsInput = (1 << 1),
36 RenderJoined = (1 << 2),
37 RenderSeparate = (1 << 3)
38};
39
41 DefaultVis = (1 << 0),
42};
43
44/// Option - Abstract representation for a single form of driver
45/// argument.
46///
47/// An Option class represents a form of option that the driver
48/// takes, for example how many arguments the option has and how
49/// they can be provided. Individual option instances store
50/// additional information about what group the option is a member
51/// of (if any), if the option is an alias, and a number of
52/// flags. At runtime the driver parses the command line into
53/// concrete Arg instances, each of which corresponds to a
54/// particular Option instance.
55class Option {
56public:
71 };
72
78 };
79
80protected:
83
84public:
86
87 bool isValid() const {
88 return Info != nullptr;
89 }
90
91 unsigned getID() const {
92 assert(Info && "Must have a valid info!");
93 return Info->ID;
94 }
95
97 assert(Info && "Must have a valid info!");
98 return OptionClass(Info->Kind);
99 }
100
101 /// Get the name of this option without any prefix.
103 assert(Info && "Must have a valid info!");
104 assert(Owner && "Must have a valid owner!");
105 return Owner->getOptionName(Info->ID);
106 }
107
108 const Option getGroup() const {
109 assert(Info && "Must have a valid info!");
110 assert(Owner && "Must have a valid owner!");
111 return Owner->getOption(Info->GroupID);
112 }
113
114 const Option getAlias() const {
115 assert(Info && "Must have a valid info!");
116 assert(Owner && "Must have a valid owner!");
117 return Owner->getOption(Info->AliasID);
118 }
119
120 /// Get the alias arguments as a \0 separated list.
121 /// E.g. ["foo", "bar"] would be returned as "foo\0bar\0".
122 const char *getAliasArgs() const {
123 assert(Info && "Must have a valid info!");
124 assert((!Info->AliasArgs || Info->AliasArgs[0] != 0) &&
125 "AliasArgs should be either 0 or non-empty.");
126
127 return Info->AliasArgs;
128 }
129
130 /// Get the default prefix for this option.
132 assert(Info && "Must have a valid info!");
133 assert(Owner && "Must have a valid owner!");
134 return Owner->getOptionPrefix(Info->ID);
135 }
136
137 /// Get the name of this option with the default prefix.
139 assert(Info && "Must have a valid info!");
140 assert(Owner && "Must have a valid owner!");
142 }
143
144 /// Get the help text for this option.
146 assert(Info && "Must have a valid info!");
147 return Info->HelpText;
148 }
149
150 /// Get the meta-variable list for this option.
152 assert(Info && "Must have a valid info!");
153 return Info->MetaVar;
154 }
155
156 unsigned getNumArgs() const { return Info->Param; }
157
158 bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
159
161 if (Info->Flags & RenderJoined)
162 return RenderJoinedStyle;
164 return RenderSeparateStyle;
165 switch (getKind()) {
166 case GroupClass:
167 case InputClass:
168 case UnknownClass:
169 return RenderValuesStyle;
170 case JoinedClass:
172 return RenderJoinedStyle;
173 case CommaJoinedClass:
175 case FlagClass:
176 case ValuesClass:
177 case SeparateClass:
178 case MultiArgClass:
182 return RenderSeparateStyle;
183 }
184 llvm_unreachable("Unexpected kind!");
185 }
186
187 /// Test if this option has the flag \a Val.
188 bool hasFlag(unsigned Val) const {
189 return Info->Flags & Val;
190 }
191
192 /// Test if this option has the visibility flag \a Val.
193 bool hasVisibilityFlag(unsigned Val) const {
194 return Info->Visibility & Val;
195 }
196
197 /// getUnaliasedOption - Return the final option this option
198 /// aliases (itself, if the option has no alias).
200 const Option Alias = getAlias();
201 if (Alias.isValid()) return Alias.getUnaliasedOption();
202 return *this;
203 }
204
205 /// getRenderName - Return the name to use when rendering this
206 /// option.
208 return getUnaliasedOption().getName();
209 }
210
211 /// matches - Predicate for whether this option is part of the
212 /// given option (which may be a group).
213 ///
214 /// Note that matches against options which are an alias should never be
215 /// done -- aliases do not participate in matching and so such a query will
216 /// always be false.
217 LLVM_ABI bool matches(OptSpecifier ID) const;
218
219 /// Potentially accept the current argument, returning a new Arg instance,
220 /// or 0 if the option does not accept this argument (or the argument is
221 /// missing values).
222 ///
223 /// If the option accepts the current argument, accept() sets
224 /// Index to the position where argument parsing should resume
225 /// (even if the argument is missing values).
226 ///
227 /// \p CurArg The argument to be matched. It may be shorter than the
228 /// underlying storage to represent a Joined argument.
229 /// \p GroupedShortOption If true, we are handling the fallback case of
230 /// parsing a prefix of the current argument as a short option.
231 LLVM_ABI std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
232 bool GroupedShortOption,
233 unsigned &Index) const;
234
235private:
236 std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
237 unsigned &Index) const;
238
239public:
240 LLVM_ABI void print(raw_ostream &O, bool AddNewLine = true) const;
241 LLVM_ABI void dump() const;
242};
243
244} // end namespace opt
245
246} // end namespace llvm
247
248#endif // LLVM_OPTION_OPTION_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
arm prera ldst opt
#define LLVM_ABI
Definition: Compiler.h:213
uint32_t Index
This file defines the SmallVector class.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:117
OptSpecifier - Wrapper class for abstracting references to option IDs.
Definition: OptSpecifier.h:20
Provide access to the Option info table.
Definition: OptTable.h:54
StringRef getOptionName(OptSpecifier id) const
Lookup the name of the given option.
Definition: OptTable.h:197
const Option getOption(OptSpecifier Opt) const
Get the given Opt's Option instance, lazily creating it if necessary.
Definition: OptTable.cpp:138
StringRef getOptionPrefix(OptSpecifier id) const
Lookup the prefix of the given option.
Definition: OptTable.h:202
StringRef getOptionPrefixedName(OptSpecifier id) const
Lookup the prefixed name of the given option.
Definition: OptTable.h:215
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:55
const Option getAlias() const
Definition: Option.h:114
LLVM_ABI void dump() const
Definition: Option.cpp:93
unsigned getNumArgs() const
Definition: Option.h:156
const char * getAliasArgs() const
Get the alias arguments as a \0 separated list.
Definition: Option.h:122
RenderStyleKind getRenderStyle() const
Definition: Option.h:160
const Option getGroup() const
Definition: Option.h:108
const OptTable * Owner
Definition: Option.h:82
const Option getUnaliasedOption() const
getUnaliasedOption - Return the final option this option aliases (itself, if the option has no alias)...
Definition: Option.h:199
LLVM_ABI 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:96
StringRef getRenderName() const
getRenderName - Return the name to use when rendering this option.
Definition: Option.h:207
bool hasFlag(unsigned Val) const
Test if this option has the flag Val.
Definition: Option.h:188
bool hasNoOptAsInput() const
Definition: Option.h:158
@ JoinedOrSeparateClass
Definition: Option.h:69
@ RemainingArgsClass
Definition: Option.h:65
@ JoinedAndSeparateClass
Definition: Option.h:70
@ RemainingArgsJoinedClass
Definition: Option.h:66
StringRef getPrefix() const
Get the default prefix for this option.
Definition: Option.h:131
@ RenderSeparateStyle
Definition: Option.h:76
@ RenderCommaJoinedStyle
Definition: Option.h:74
bool hasVisibilityFlag(unsigned Val) const
Test if this option has the visibility flag Val.
Definition: Option.h:193
const OptTable::Info * Info
Definition: Option.h:81
StringRef getPrefixedName() const
Get the name of this option with the default prefix.
Definition: Option.h:138
StringRef getMetaVar() const
Get the meta-variable list for this option.
Definition: Option.h:151
bool isValid() const
Definition: Option.h:87
unsigned getID() const
Definition: Option.h:91
StringRef getHelpText() const
Get the help text for this option.
Definition: Option.h:145
StringRef getName() const
Get the name of this option without any prefix.
Definition: Option.h:102
LLVM_ABI 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
OptionClass getKind() const
Definition: Option.h:96
LLVM_ABI void print(raw_ostream &O, bool AddNewLine=true) const
Definition: Option.cpp:40
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
DriverVisibility
Definition: Option.h:40
@ DefaultVis
Definition: Option.h:41
DriverFlag
Base flags for all options. Custom flags may be added after.
Definition: Option.h:33
@ RenderSeparate
Definition: Option.h:37
@ HelpHidden
Definition: Option.h:34
@ RenderJoined
Definition: Option.h:36
@ RenderAsInput
Definition: Option.h:35
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Entry for a single option instance in the option data table.
Definition: OptTable.h:57
unsigned char Param
Definition: OptTable.h:75
unsigned int Visibility
Definition: OptTable.h:77
unsigned short AliasID
Definition: OptTable.h:79
unsigned short GroupID
Definition: OptTable.h:78
unsigned int Flags
Definition: OptTable.h:76
const char * HelpText
Definition: OptTable.h:60
const char * MetaVar
Definition: OptTable.h:72
unsigned char Kind
Definition: OptTable.h:74
const char * AliasArgs
Definition: OptTable.h:80