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:
74
82
83protected:
86
87public:
89
90 bool isValid() const {
91 return Info != nullptr;
92 }
93
94 unsigned getID() const {
95 assert(Info && "Must have a valid info!");
96 assert(Owner && "Must have a valid owner!");
97 return Owner->getOptionID(*Info);
98 }
99
101 assert(Info && "Must have a valid info!");
102 return OptionClass(Info->Kind);
103 }
104
105 /// Get the name of this option without any prefix.
106 StringRef getName() const { return Owner->getOptionName(getID()); }
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 return Owner->getOptionAliasArgs(getID());
124 }
125
126 bool hasAliasArgs() const { return *getAliasArgs() != '\0'; }
127
128 /// Get the default prefix for this option.
129 StringRef getPrefix() const { return Owner->getOptionPrefix(getID()); }
130
131 /// Get the name of this option with the default prefix.
133 return Owner->getOptionPrefixedName(getID());
134 }
135
136 /// Get the help text for this option.
137 StringRef getHelpText() const { return Owner->getOptionHelpText(getID()); }
138
139 /// Get the meta-variable list for this option.
140 StringRef getMetaVar() const { return Owner->getOptionMetaVar(getID()); }
141
142 unsigned getNumArgs() const { return Owner->getExtra(*Info).Param; }
143
144 bool hasNoOptAsInput() const { return hasFlag(RenderAsInput); }
145
148 return RenderJoinedStyle;
150 return RenderSeparateStyle;
151 switch (getKind()) {
152 case GroupClass:
153 case InputClass:
154 case UnknownClass:
155 return RenderValuesStyle;
156 case JoinedClass:
158 return RenderJoinedStyle;
159 case FlagOrEqClass:
161 return RenderEqStyle;
162 case CommaJoinedClass:
164 case FlagClass:
165 case ValuesClass:
166 case SeparateClass:
167 case MultiArgClass:
171 return RenderSeparateStyle;
172 }
173 llvm_unreachable("Unexpected kind!");
174 }
175
176 /// Test if this option has the flag \a Val.
177 bool hasFlag(unsigned Val) const {
178 return Owner->getExtra(*Info).Flags & Val;
179 }
180
181 /// Test if this option has the visibility flag \a Val.
182 bool hasVisibilityFlag(unsigned Val) const {
183 return Owner->getExtra(*Info).Visibility & Val;
184 }
185
186 /// getUnaliasedOption - Return the final option this option
187 /// aliases (itself, if the option has no alias).
189 const Option Alias = getAlias();
190 if (Alias.isValid()) return Alias.getUnaliasedOption();
191 return *this;
192 }
193
194 /// getRenderName - Return the name to use when rendering this
195 /// option.
197 return getUnaliasedOption().getName();
198 }
199
200 /// matches - Predicate for whether this option is part of the
201 /// given option (which may be a group).
202 ///
203 /// Note that matches against options which are an alias should never be
204 /// done -- aliases do not participate in matching and so such a query will
205 /// always be false.
206 LLVM_ABI bool matches(OptSpecifier ID) const;
207
208 bool isRegisteredSC(StringRef SubCommand) const {
209 assert(Info && "Must have a valid info!");
210 assert(Owner && "Must have a valid owner!");
211 return Owner->isValidForSubCommand(Info, SubCommand);
212 }
213
214 /// Potentially accept the current argument, returning a new Arg instance,
215 /// or 0 if the option does not accept this argument (or the argument is
216 /// missing values).
217 ///
218 /// If the option accepts the current argument, accept() sets
219 /// Index to the position where argument parsing should resume
220 /// (even if the argument is missing values).
221 ///
222 /// \p CurArg The argument to be matched. It may be shorter than the
223 /// underlying storage to represent a Joined argument.
224 /// \p GroupedShortOption If true, we are handling the fallback case of
225 /// parsing a prefix of the current argument as a short option.
226 LLVM_ABI std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
227 bool GroupedShortOption,
228 unsigned &Index) const;
229
230private:
231 std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
232 unsigned &Index) const;
233
234public:
235 LLVM_ABI void print(raw_ostream &O, bool AddNewLine = true) const;
236 LLVM_ABI void dump() const;
237};
238
239} // end namespace opt
240
241} // end namespace llvm
242
243#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:55
const Option getAlias() const
Definition Option.h:114
LLVM_ABI void dump() const
Definition Option.cpp:96
unsigned getNumArgs() const
Definition Option.h:142
bool isRegisteredSC(StringRef SubCommand) const
Definition Option.h:208
const char * getAliasArgs() const
Get the alias arguments as a \0 separated list.
Definition Option.h:122
RenderStyleKind getRenderStyle() const
Definition Option.h:146
const Option getGroup() const
Definition Option.h:108
const OptTable * Owner
Definition Option.h:85
const Option getUnaliasedOption() const
getUnaliasedOption - Return the final option this option aliases (itself, if the option has no alias)...
Definition Option.h:188
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:99
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:196
bool hasFlag(unsigned Val) const
Test if this option has the flag Val.
Definition Option.h:177
bool hasNoOptAsInput() const
Definition Option.h:144
@ JoinedOrSeparateClass
Definition Option.h:71
@ JoinedAndSeparateClass
Definition Option.h:72
@ RemainingArgsJoinedClass
Definition Option.h:68
bool hasAliasArgs() const
Definition Option.h:126
StringRef getPrefix() const
Get the default prefix for this option.
Definition Option.h:129
@ RenderCommaJoinedStyle
Definition Option.h:76
bool hasVisibilityFlag(unsigned Val) const
Test if this option has the visibility flag Val.
Definition Option.h:182
const OptTable::Info * Info
Definition Option.h:84
StringRef getPrefixedName() const
Get the name of this option with the default prefix.
Definition Option.h:132
StringRef getMetaVar() const
Get the meta-variable list for this option.
Definition Option.h:140
bool isValid() const
Definition Option.h:90
unsigned getID() const
Definition Option.h:94
StringRef getHelpText() const
Get the help text for this option.
Definition Option.h:137
StringRef getName() const
Get the name of this option without any prefix.
Definition Option.h:106
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:256
OptionClass getKind() const
Definition Option.h:100
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:96