LLVM 18.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#include <string>
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:
85 Option(const OptTable::Info *Info, const OptTable *Owner);
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 return Info->getName();
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 return Info->Prefixes.empty()
132 ? StringRef()
133 : static_cast<const StringRef &>(Info->Prefixes[0]);
134 }
135
136 /// Get the name of this option with the default prefix.
138 assert(Info && "Must have a valid info!");
139 return Info->PrefixedName;
140 }
141
142 /// Get the help text for this option.
144 assert(Info && "Must have a valid info!");
145 return Info->HelpText;
146 }
147
148 /// Get the meta-variable list for this option.
150 assert(Info && "Must have a valid info!");
151 return Info->MetaVar;
152 }
153
154 unsigned getNumArgs() const { return Info->Param; }
155
156 bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
157
159 if (Info->Flags & RenderJoined)
160 return RenderJoinedStyle;
162 return RenderSeparateStyle;
163 switch (getKind()) {
164 case GroupClass:
165 case InputClass:
166 case UnknownClass:
167 return RenderValuesStyle;
168 case JoinedClass:
170 return RenderJoinedStyle;
171 case CommaJoinedClass:
173 case FlagClass:
174 case ValuesClass:
175 case SeparateClass:
176 case MultiArgClass:
180 return RenderSeparateStyle;
181 }
182 llvm_unreachable("Unexpected kind!");
183 }
184
185 /// Test if this option has the flag \a Val.
186 bool hasFlag(unsigned Val) const {
187 return Info->Flags & Val;
188 }
189
190 /// Test if this option has the visibility flag \a Val.
191 bool hasVisibilityFlag(unsigned Val) const {
192 return Info->Visibility & Val;
193 }
194
195 /// getUnaliasedOption - Return the final option this option
196 /// aliases (itself, if the option has no alias).
198 const Option Alias = getAlias();
199 if (Alias.isValid()) return Alias.getUnaliasedOption();
200 return *this;
201 }
202
203 /// getRenderName - Return the name to use when rendering this
204 /// option.
206 return getUnaliasedOption().getName();
207 }
208
209 /// matches - Predicate for whether this option is part of the
210 /// given option (which may be a group).
211 ///
212 /// Note that matches against options which are an alias should never be
213 /// done -- aliases do not participate in matching and so such a query will
214 /// always be false.
215 bool matches(OptSpecifier ID) const;
216
217 /// Potentially accept the current argument, returning a new Arg instance,
218 /// or 0 if the option does not accept this argument (or the argument is
219 /// missing values).
220 ///
221 /// If the option accepts the current argument, accept() sets
222 /// Index to the position where argument parsing should resume
223 /// (even if the argument is missing values).
224 ///
225 /// \p CurArg The argument to be matched. It may be shorter than the
226 /// underlying storage to represent a Joined argument.
227 /// \p GroupedShortOption If true, we are handling the fallback case of
228 /// parsing a prefix of the current argument as a short option.
229 std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
230 bool GroupedShortOption, unsigned &Index) const;
231
232private:
233 std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
234 unsigned &Index) const;
235
236public:
237 void print(raw_ostream &O, bool AddNewLine = true) const;
238 void dump() const;
239};
240
241} // end namespace opt
242
243} // end namespace llvm
244
245#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:857
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:55
const Option getAlias() const
Definition: Option.h:113
void dump() const
Definition: Option.cpp:91
unsigned getNumArgs() const
Definition: Option.h:154
const char * getAliasArgs() const
Get the alias arguments as a \0 separated list.
Definition: Option.h:121
RenderStyleKind getRenderStyle() const
Definition: Option.h:158
StringLiteral getPrefixedName() const
Get the name of this option with the default prefix.
Definition: Option.h:137
const Option getGroup() const
Definition: Option.h:107
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:197
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:205
bool hasFlag(unsigned Val) const
Test if this option has the flag Val.
Definition: Option.h:186
bool hasNoOptAsInput() const
Definition: Option.h:156
@ 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:130
@ 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:191
const OptTable::Info * Info
Definition: Option.h:81
StringRef getMetaVar() const
Get the meta-variable list for this option.
Definition: Option.h:149
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:143
StringRef getName() const
Get the name of this option without any prefix.
Definition: Option.h:102
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:96
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: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:55
StringLiteral PrefixedName
Definition: OptTable.h:59
unsigned char Param
Definition: OptTable.h:64
unsigned int Visibility
Definition: OptTable.h:66
unsigned short AliasID
Definition: OptTable.h:68
StringRef getName() const
Definition: OptTable.h:72
unsigned short GroupID
Definition: OptTable.h:67
unsigned int Flags
Definition: OptTable.h:65
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:61
unsigned char Kind
Definition: OptTable.h:63
const char * AliasArgs
Definition: OptTable.h:69