LLVM 17.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
40/// Option - Abstract representation for a single form of driver
41/// argument.
42///
43/// An Option class represents a form of option that the driver
44/// takes, for example how many arguments the option has and how
45/// they can be provided. Individual option instances store
46/// additional information about what group the option is a member
47/// of (if any), if the option is an alias, and a number of
48/// flags. At runtime the driver parses the command line into
49/// concrete Arg instances, each of which corresponds to a
50/// particular Option instance.
51class Option {
52public:
67 };
68
74 };
75
76protected:
79
80public:
81 Option(const OptTable::Info *Info, const OptTable *Owner);
82
83 bool isValid() const {
84 return Info != nullptr;
85 }
86
87 unsigned getID() const {
88 assert(Info && "Must have a valid info!");
89 return Info->ID;
90 }
91
93 assert(Info && "Must have a valid info!");
94 return OptionClass(Info->Kind);
95 }
96
97 /// Get the name of this option without any prefix.
99 assert(Info && "Must have a valid info!");
100 return Info->Name;
101 }
102
103 const Option getGroup() const {
104 assert(Info && "Must have a valid info!");
105 assert(Owner && "Must have a valid owner!");
106 return Owner->getOption(Info->GroupID);
107 }
108
109 const Option getAlias() const {
110 assert(Info && "Must have a valid info!");
111 assert(Owner && "Must have a valid owner!");
112 return Owner->getOption(Info->AliasID);
113 }
114
115 /// Get the alias arguments as a \0 separated list.
116 /// E.g. ["foo", "bar"] would be returned as "foo\0bar\0".
117 const char *getAliasArgs() const {
118 assert(Info && "Must have a valid info!");
119 assert((!Info->AliasArgs || Info->AliasArgs[0] != 0) &&
120 "AliasArgs should be either 0 or non-empty.");
121
122 return Info->AliasArgs;
123 }
124
125 /// Get the default prefix for this option.
127 return Info->Prefixes.empty()
128 ? StringRef()
129 : static_cast<const StringRef &>(Info->Prefixes[0]);
130 }
131
132 /// Get the name of this option with the default prefix.
133 std::string getPrefixedName() const {
134 std::string Ret(getPrefix());
135 Ret += getName();
136 return Ret;
137 }
138
139 /// Get the help text for this option.
141 assert(Info && "Must have a valid info!");
142 return Info->HelpText;
143 }
144
145 /// Get the meta-variable list for this option.
147 assert(Info && "Must have a valid info!");
148 return Info->MetaVar;
149 }
150
151 unsigned getNumArgs() const { return Info->Param; }
152
153 bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
154
156 if (Info->Flags & RenderJoined)
157 return RenderJoinedStyle;
159 return RenderSeparateStyle;
160 switch (getKind()) {
161 case GroupClass:
162 case InputClass:
163 case UnknownClass:
164 return RenderValuesStyle;
165 case JoinedClass:
167 return RenderJoinedStyle;
168 case CommaJoinedClass:
170 case FlagClass:
171 case ValuesClass:
172 case SeparateClass:
173 case MultiArgClass:
177 return RenderSeparateStyle;
178 }
179 llvm_unreachable("Unexpected kind!");
180 }
181
182 /// Test if this option has the flag \a Val.
183 bool hasFlag(unsigned Val) const {
184 return Info->Flags & Val;
185 }
186
187 /// getUnaliasedOption - Return the final option this option
188 /// aliases (itself, if the option has no alias).
190 const Option Alias = getAlias();
191 if (Alias.isValid()) return Alias.getUnaliasedOption();
192 return *this;
193 }
194
195 /// getRenderName - Return the name to use when rendering this
196 /// option.
198 return getUnaliasedOption().getName();
199 }
200
201 /// matches - Predicate for whether this option is part of the
202 /// given option (which may be a group).
203 ///
204 /// Note that matches against options which are an alias should never be
205 /// done -- aliases do not participate in matching and so such a query will
206 /// always be false.
207 bool matches(OptSpecifier ID) const;
208
209 /// Potentially accept the current argument, returning a new Arg instance,
210 /// or 0 if the option does not accept this argument (or the argument is
211 /// missing values).
212 ///
213 /// If the option accepts the current argument, accept() sets
214 /// Index to the position where argument parsing should resume
215 /// (even if the argument is missing values).
216 ///
217 /// \p CurArg The argument to be matched. It may be shorter than the
218 /// underlying storage to represent a Joined argument.
219 /// \p GroupedShortOption If true, we are handling the fallback case of
220 /// parsing a prefix of the current argument as a short option.
221 std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
222 bool GroupedShortOption, unsigned &Index) const;
223
224private:
225 std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
226 unsigned &Index) const;
227
228public:
229 void print(raw_ostream &O) const;
230 void dump() const;
231};
232
233} // end namespace opt
234
235} // end namespace llvm
236
237#endif // LLVM_OPTION_OPTION_H
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
arm prera ldst opt
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: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:40
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:51
const Option getAlias() const
Definition: Option.h:109
void dump() const
Definition: Option.cpp:89
unsigned getNumArgs() const
Definition: Option.h:151
const char * getAliasArgs() const
Get the alias arguments as a \0 separated list.
Definition: Option.h:117
RenderStyleKind getRenderStyle() const
Definition: Option.h:155
const Option getGroup() const
Definition: Option.h:103
const OptTable * Owner
Definition: Option.h:78
const Option getUnaliasedOption() const
getUnaliasedOption - Return the final option this option aliases (itself, if the option has no alias)...
Definition: Option.h:189
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:92
StringRef getRenderName() const
getRenderName - Return the name to use when rendering this option.
Definition: Option.h:197
void print(raw_ostream &O) const
Definition: Option.cpp:41
bool hasFlag(unsigned Val) const
Test if this option has the flag Val.
Definition: Option.h:183
bool hasNoOptAsInput() const
Definition: Option.h:153
@ JoinedOrSeparateClass
Definition: Option.h:65
@ RemainingArgsClass
Definition: Option.h:61
@ JoinedAndSeparateClass
Definition: Option.h:66
@ RemainingArgsJoinedClass
Definition: Option.h:62
StringRef getPrefix() const
Get the default prefix for this option.
Definition: Option.h:126
@ RenderSeparateStyle
Definition: Option.h:72
@ RenderCommaJoinedStyle
Definition: Option.h:70
const OptTable::Info * Info
Definition: Option.h:77
std::string getPrefixedName() const
Get the name of this option with the default prefix.
Definition: Option.h:133
StringRef getMetaVar() const
Get the meta-variable list for this option.
Definition: Option.h:146
bool isValid() const
Definition: Option.h:83
unsigned getID() const
Definition: Option.h:87
StringRef getHelpText() const
Get the help text for this option.
Definition: Option.h:140
StringRef getName() const
Get the name of this option without any prefix.
Definition: Option.h:98
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:232
OptionClass getKind() const
Definition: Option.h:92
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.
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:43
unsigned char Param
Definition: OptTable.h:52
unsigned short AliasID
Definition: OptTable.h:55
unsigned short GroupID
Definition: OptTable.h:54
unsigned int Flags
Definition: OptTable.h:53
ArrayRef< StringLiteral > Prefixes
A null terminated array of prefix strings to apply to name while matching.
Definition: OptTable.h:46
const char * HelpText
Definition: OptTable.h:48
const char * MetaVar
Definition: OptTable.h:49
unsigned char Kind
Definition: OptTable.h:51
const char * AliasArgs
Definition: OptTable.h:56