LLVM 24.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:
72
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(Owner && "Must have a valid owner!");
125 return Owner->getStrTable().getCString(Info->AliasArgsOffset);
126 }
127
128 bool hasAliasArgs() const {
129 assert(Info && "Must have a valid info!");
130 return Info->hasAliasArgs();
131 }
132
133 /// Get the default prefix for this option.
135 assert(Info && "Must have a valid info!");
136 assert(Owner && "Must have a valid owner!");
137 return Owner->getOptionPrefix(Info->ID);
138 }
139
140 /// Get the name of this option with the default prefix.
142 assert(Info && "Must have a valid info!");
143 assert(Owner && "Must have a valid owner!");
144 return Owner->getOptionPrefixedName(Info->ID);
145 }
146
147 /// Get the help text for this option.
149 assert(Info && "Must have a valid info!");
150 assert(Owner && "Must have a valid owner!");
151 return Owner->getOptionHelpText(Info->ID);
152 }
153
154 /// Get the meta-variable list for this option.
156 assert(Info && "Must have a valid info!");
157 assert(Owner && "Must have a valid owner!");
158 return Owner->getOptionMetaVar(Info->ID);
159 }
160
161 unsigned getNumArgs() const { return Info->Param; }
162
163 bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
164
166 if (Info->Flags & RenderJoined)
167 return RenderJoinedStyle;
168 if (Info->Flags & RenderSeparate)
169 return RenderSeparateStyle;
170 switch (getKind()) {
171 case GroupClass:
172 case InputClass:
173 case UnknownClass:
174 return RenderValuesStyle;
175 case JoinedClass:
177 return RenderJoinedStyle;
178 case CommaJoinedClass:
180 case FlagClass:
181 case ValuesClass:
182 case SeparateClass:
183 case MultiArgClass:
187 return RenderSeparateStyle;
188 }
189 llvm_unreachable("Unexpected kind!");
190 }
191
192 /// Test if this option has the flag \a Val.
193 bool hasFlag(unsigned Val) const {
194 return Info->Flags & Val;
195 }
196
197 /// Test if this option has the visibility flag \a Val.
198 bool hasVisibilityFlag(unsigned Val) const {
199 return Info->Visibility & Val;
200 }
201
202 /// getUnaliasedOption - Return the final option this option
203 /// aliases (itself, if the option has no alias).
205 const Option Alias = getAlias();
206 if (Alias.isValid()) return Alias.getUnaliasedOption();
207 return *this;
208 }
209
210 /// getRenderName - Return the name to use when rendering this
211 /// option.
213 return getUnaliasedOption().getName();
214 }
215
216 /// matches - Predicate for whether this option is part of the
217 /// given option (which may be a group).
218 ///
219 /// Note that matches against options which are an alias should never be
220 /// done -- aliases do not participate in matching and so such a query will
221 /// always be false.
222 LLVM_ABI bool matches(OptSpecifier ID) const;
223
224 bool isRegisteredSC(StringRef SubCommand) const {
225 assert(Info && "Must have a valid info!");
226 assert(Owner && "Must have a valid owner!");
227 return Owner->isValidForSubCommand(Info, SubCommand);
228 }
229
230 /// Potentially accept the current argument, returning a new Arg instance,
231 /// or 0 if the option does not accept this argument (or the argument is
232 /// missing values).
233 ///
234 /// If the option accepts the current argument, accept() sets
235 /// Index to the position where argument parsing should resume
236 /// (even if the argument is missing values).
237 ///
238 /// \p CurArg The argument to be matched. It may be shorter than the
239 /// underlying storage to represent a Joined argument.
240 /// \p GroupedShortOption If true, we are handling the fallback case of
241 /// parsing a prefix of the current argument as a short option.
242 LLVM_ABI std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
243 bool GroupedShortOption,
244 unsigned &Index) const;
245
246private:
247 std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
248 unsigned &Index) const;
249
250public:
251 LLVM_ABI void print(raw_ostream &O, bool AddNewLine = true) const;
252 LLVM_ABI void dump() const;
253};
254
255} // end namespace opt
256
257} // end namespace llvm
258
259#endif // LLVM_OPTION_OPTION_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
This file defines the SmallVector class.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
ArgList - Ordered collection of driver arguments.
Definition ArgList.h:118
A concrete instance of a particular driver option.
Definition Arg.h:35
OptSpecifier - Wrapper class for abstracting references to option IDs.
Provide access to the Option info table.
Definition OptTable.h:54
const Option getAlias() const
Definition Option.h:114
LLVM_ABI void dump() const
Definition Option.cpp:94
unsigned getNumArgs() const
Definition Option.h:161
bool isRegisteredSC(StringRef SubCommand) const
Definition Option.h:224
const char * getAliasArgs() const
Get the alias arguments as a \0 separated list.
Definition Option.h:122
RenderStyleKind getRenderStyle() const
Definition Option.h:165
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:204
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:97
LLVM_ABI Option(const OptTable::Info *Info, const OptTable *Owner)
Definition Option.cpp:26
StringRef getRenderName() const
getRenderName - Return the name to use when rendering this option.
Definition Option.h:212
bool hasFlag(unsigned Val) const
Test if this option has the flag Val.
Definition Option.h:193
bool hasNoOptAsInput() const
Definition Option.h:163
@ JoinedOrSeparateClass
Definition Option.h:69
@ JoinedAndSeparateClass
Definition Option.h:70
@ RemainingArgsJoinedClass
Definition Option.h:66
bool hasAliasArgs() const
Definition Option.h:128
StringRef getPrefix() const
Get the default prefix for this option.
Definition Option.h:134
@ RenderCommaJoinedStyle
Definition Option.h:74
bool hasVisibilityFlag(unsigned Val) const
Test if this option has the visibility flag Val.
Definition Option.h:198
const OptTable::Info * Info
Definition Option.h:81
StringRef getPrefixedName() const
Get the name of this option with the default prefix.
Definition Option.h:141
StringRef getMetaVar() const
Get the meta-variable list for this option.
Definition Option.h:155
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:148
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:237
OptionClass getKind() const
Definition Option.h:96
LLVM_ABI void print(raw_ostream &O, bool AddNewLine=true) const
Definition Option.cpp:41
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.
SmallVector< const char *, 16 > ArgStringList
ArgStringList - Type used for constructing argv lists for subprocesses.
Definition Option.h:30
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.
Entry for a single option instance in the option data table.
Definition OptTable.h:69