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