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 return Info->getName();
104 }
105
106 const Option getGroup() const {
107 assert(Info && "Must have a valid info!");
108 assert(Owner && "Must have a valid owner!");
109 return Owner->getOption(Info->GroupID);
110 }
111
112 const Option getAlias() const {
113 assert(Info && "Must have a valid info!");
114 assert(Owner && "Must have a valid owner!");
115 return Owner->getOption(Info->AliasID);
116 }
117
118 /// Get the alias arguments as a \0 separated list.
119 /// E.g. ["foo", "bar"] would be returned as "foo\0bar\0".
120 const char *getAliasArgs() const {
121 assert(Info && "Must have a valid info!");
122 assert((!Info->AliasArgs || Info->AliasArgs[0] != 0) &&
123 "AliasArgs should be either 0 or non-empty.");
124
125 return Info->AliasArgs;
126 }
127
128 /// Get the default prefix for this option.
130 return Info->Prefixes.empty()
131 ? StringRef()
132 : static_cast<const StringRef &>(Info->Prefixes[0]);
133 }
134
135 /// Get the name of this option with the default prefix.
137 assert(Info && "Must have a valid info!");
138 return Info->PrefixedName;
139 }
140
141 /// Get the help text for this option.
143 assert(Info && "Must have a valid info!");
144 return Info->HelpText;
145 }
146
147 /// Get the meta-variable list for this option.
149 assert(Info && "Must have a valid info!");
150 return Info->MetaVar;
151 }
152
153 unsigned getNumArgs() const { return Info->Param; }
154
155 bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
156
158 if (Info->Flags & RenderJoined)
159 return RenderJoinedStyle;
161 return RenderSeparateStyle;
162 switch (getKind()) {
163 case GroupClass:
164 case InputClass:
165 case UnknownClass:
166 return RenderValuesStyle;
167 case JoinedClass:
169 return RenderJoinedStyle;
170 case CommaJoinedClass:
172 case FlagClass:
173 case ValuesClass:
174 case SeparateClass:
175 case MultiArgClass:
179 return RenderSeparateStyle;
180 }
181 llvm_unreachable("Unexpected kind!");
182 }
183
184 /// Test if this option has the flag \a Val.
185 bool hasFlag(unsigned Val) const {
186 return Info->Flags & Val;
187 }
188
189 /// Test if this option has the visibility flag \a Val.
190 bool hasVisibilityFlag(unsigned Val) const {
191 return Info->Visibility & Val;
192 }
193
194 /// getUnaliasedOption - Return the final option this option
195 /// aliases (itself, if the option has no alias).
197 const Option Alias = getAlias();
198 if (Alias.isValid()) return Alias.getUnaliasedOption();
199 return *this;
200 }
201
202 /// getRenderName - Return the name to use when rendering this
203 /// option.
205 return getUnaliasedOption().getName();
206 }
207
208 /// matches - Predicate for whether this option is part of the
209 /// given option (which may be a group).
210 ///
211 /// Note that matches against options which are an alias should never be
212 /// done -- aliases do not participate in matching and so such a query will
213 /// always be false.
214 bool matches(OptSpecifier ID) const;
215
216 /// Potentially accept the current argument, returning a new Arg instance,
217 /// or 0 if the option does not accept this argument (or the argument is
218 /// missing values).
219 ///
220 /// If the option accepts the current argument, accept() sets
221 /// Index to the position where argument parsing should resume
222 /// (even if the argument is missing values).
223 ///
224 /// \p CurArg The argument to be matched. It may be shorter than the
225 /// underlying storage to represent a Joined argument.
226 /// \p GroupedShortOption If true, we are handling the fallback case of
227 /// parsing a prefix of the current argument as a short option.
228 std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
229 bool GroupedShortOption, unsigned &Index) const;
230
231private:
232 std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
233 unsigned &Index) const;
234
235public:
236 void print(raw_ostream &O, bool AddNewLine = true) const;
237 void dump() const;
238};
239
240} // end namespace opt
241
242} // end namespace llvm
243
244#endif // LLVM_OPTION_OPTION_H
arm prera ldst opt
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:838
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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
const Option getOption(OptSpecifier Opt) const
Get the given Opt's Option instance, lazily creating it if necessary.
Definition: OptTable.cpp:143
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:54
const Option getAlias() const
Definition: Option.h:112
void dump() const
Definition: Option.cpp:91
unsigned getNumArgs() const
Definition: Option.h:153
const char * getAliasArgs() const
Get the alias arguments as a \0 separated list.
Definition: Option.h:120
RenderStyleKind getRenderStyle() const
Definition: Option.h:157
StringLiteral getPrefixedName() const
Get the name of this option with the default prefix.
Definition: Option.h:136
const Option getGroup() const
Definition: Option.h:106
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:196
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:94
StringRef getRenderName() const
getRenderName - Return the name to use when rendering this option.
Definition: Option.h:204
bool hasFlag(unsigned Val) const
Test if this option has the flag Val.
Definition: Option.h:185
bool hasNoOptAsInput() const
Definition: Option.h:155
@ 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:129
@ 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:190
const OptTable::Info * Info
Definition: Option.h:80
StringRef getMetaVar() const
Get the meta-variable list for this option.
Definition: Option.h:148
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:142
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:234
OptionClass getKind() const
Definition: Option.h:95
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: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
StringLiteral PrefixedName
Definition: OptTable.h:59
unsigned char Param
Definition: OptTable.h:75
unsigned int Visibility
Definition: OptTable.h:77
unsigned short AliasID
Definition: OptTable.h:79
StringRef getName() const
Definition: OptTable.h:83
unsigned short GroupID
Definition: OptTable.h:78
unsigned int Flags
Definition: OptTable.h:76
ArrayRef< StringLiteral > Prefixes
A null terminated array of prefix strings to apply to name while matching.
Definition: OptTable.h:58
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