1 //===--- Option.h - Abstract Driver Options ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef CLANG_DRIVER_OPTION_H_
11 #define CLANG_DRIVER_OPTION_H_
13 #include "clang/Driver/OptTable.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "clang/Basic/LLVM.h"
24 /// Base flags for all options. Custom flags may be added after.
26 HelpHidden = (1 << 0),
27 RenderAsInput = (1 << 1),
28 RenderJoined = (1 << 2),
29 RenderSeparate = (1 << 3)
32 /// Flags specifically for clang options.
34 DriverOption = (1 << 4),
35 LinkerInput = (1 << 5),
36 NoArgumentUnused = (1 << 6),
38 Unsupported = (1 << 8),
40 NoDriverOption = (1 << 10)
44 /// Option - Abstract representation for a single form of driver
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.
66 JoinedOrSeparateClass,
67 JoinedAndSeparateClass
70 enum RenderStyleKind {
71 RenderCommaJoinedStyle,
78 const OptTable::Info *Info;
79 const OptTable *Owner;
82 Option(const OptTable::Info *Info, const OptTable *Owner);
85 bool isValid() const {
89 unsigned getID() const {
90 assert(Info && "Must have a valid info!");
94 OptionClass getKind() const {
95 assert(Info && "Must have a valid info!");
96 return OptionClass(Info->Kind);
99 /// \brief Get the name of this option without any prefix.
100 StringRef getName() const {
101 assert(Info && "Must have a valid info!");
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);
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);
117 /// \brief Get the default prefix for this option.
118 StringRef getPrefix() const {
119 const char *Prefix = *Info->Prefixes;
120 return Prefix ? Prefix : StringRef();
123 /// \brief Get the name of this option with the default prefix.
124 std::string getPrefixedName() const {
125 std::string Ret = getPrefix();
130 unsigned getNumArgs() const { return Info->Param; }
132 bool hasNoOptAsInput() const { return Info->Flags & options::RenderAsInput;}
134 RenderStyleKind getRenderStyle() const {
135 if (Info->Flags & options::RenderJoined)
136 return RenderJoinedStyle;
137 if (Info->Flags & options::RenderSeparate)
138 return RenderSeparateStyle;
143 return RenderValuesStyle;
145 case JoinedAndSeparateClass:
146 return RenderJoinedStyle;
147 case CommaJoinedClass:
148 return RenderCommaJoinedStyle;
152 case JoinedOrSeparateClass:
153 return RenderSeparateStyle;
155 llvm_unreachable("Unexpected kind!");
158 /// Test if this option has the flag \a Val.
159 bool hasFlag(unsigned Val) const {
160 return Info->Flags & Val;
163 /// getUnaliasedOption - Return the final option this option
164 /// aliases (itself, if the option has no alias).
165 const Option getUnaliasedOption() const {
166 const Option Alias = getAlias();
167 if (Alias.isValid()) return Alias.getUnaliasedOption();
171 /// getRenderName - Return the name to use when rendering this
173 StringRef getRenderName() const {
174 return getUnaliasedOption().getName();
177 /// matches - Predicate for whether this option is part of the
178 /// given option (which may be a group).
180 /// Note that matches against options which are an alias should never be
181 /// done -- aliases do not participate in matching and so such a query will
183 bool matches(OptSpecifier ID) const;
185 /// accept - Potentially accept the current argument, returning a
186 /// new Arg instance, or 0 if the option does not accept this
187 /// argument (or the argument is missing values).
189 /// If the option accepts the current argument, accept() sets
190 /// Index to the position where argument parsing should resume
191 /// (even if the argument is missing values).
193 /// \parm ArgSize The number of bytes taken up by the matched Option prefix
194 /// and name. This is used to determine where joined values
196 Arg *accept(const ArgList &Args, unsigned &Index, unsigned ArgSize) const;
201 } // end namespace driver
202 } // end namespace clang