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