Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:22

0001 //===- Option.h - Abstract Driver Options -----------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_OPTION_OPTION_H
0010 #define LLVM_OPTION_OPTION_H
0011 
0012 #include "llvm/ADT/SmallVector.h"
0013 #include "llvm/ADT/StringRef.h"
0014 #include "llvm/Option/OptSpecifier.h"
0015 #include "llvm/Option/OptTable.h"
0016 #include "llvm/Support/ErrorHandling.h"
0017 #include <cassert>
0018 
0019 namespace llvm {
0020 
0021 class raw_ostream;
0022 
0023 namespace opt {
0024 
0025 class Arg;
0026 class ArgList;
0027 
0028 /// ArgStringList - Type used for constructing argv lists for subprocesses.
0029 using ArgStringList = SmallVector<const char *, 16>;
0030 
0031 /// Base flags for all options. Custom flags may be added after.
0032 enum DriverFlag {
0033   HelpHidden       = (1 << 0),
0034   RenderAsInput    = (1 << 1),
0035   RenderJoined     = (1 << 2),
0036   RenderSeparate   = (1 << 3)
0037 };
0038 
0039 enum DriverVisibility {
0040   DefaultVis = (1 << 0),
0041 };
0042 
0043 /// Option - Abstract representation for a single form of driver
0044 /// argument.
0045 ///
0046 /// An Option class represents a form of option that the driver
0047 /// takes, for example how many arguments the option has and how
0048 /// they can be provided. Individual option instances store
0049 /// additional information about what group the option is a member
0050 /// of (if any), if the option is an alias, and a number of
0051 /// flags. At runtime the driver parses the command line into
0052 /// concrete Arg instances, each of which corresponds to a
0053 /// particular Option instance.
0054 class Option {
0055 public:
0056   enum OptionClass {
0057     GroupClass = 0,
0058     InputClass,
0059     UnknownClass,
0060     FlagClass,
0061     JoinedClass,
0062     ValuesClass,
0063     SeparateClass,
0064     RemainingArgsClass,
0065     RemainingArgsJoinedClass,
0066     CommaJoinedClass,
0067     MultiArgClass,
0068     JoinedOrSeparateClass,
0069     JoinedAndSeparateClass
0070   };
0071 
0072   enum RenderStyleKind {
0073     RenderCommaJoinedStyle,
0074     RenderJoinedStyle,
0075     RenderSeparateStyle,
0076     RenderValuesStyle
0077   };
0078 
0079 protected:
0080   const OptTable::Info *Info;
0081   const OptTable *Owner;
0082 
0083 public:
0084   Option(const OptTable::Info *Info, const OptTable *Owner);
0085 
0086   bool isValid() const {
0087     return Info != nullptr;
0088   }
0089 
0090   unsigned getID() const {
0091     assert(Info && "Must have a valid info!");
0092     return Info->ID;
0093   }
0094 
0095   OptionClass getKind() const {
0096     assert(Info && "Must have a valid info!");
0097     return OptionClass(Info->Kind);
0098   }
0099 
0100   /// Get the name of this option without any prefix.
0101   StringRef getName() const {
0102     assert(Info && "Must have a valid info!");
0103     assert(Owner && "Must have a valid owner!");
0104     return Owner->getOptionName(Info->ID);
0105   }
0106 
0107   const Option getGroup() const {
0108     assert(Info && "Must have a valid info!");
0109     assert(Owner && "Must have a valid owner!");
0110     return Owner->getOption(Info->GroupID);
0111   }
0112 
0113   const Option getAlias() const {
0114     assert(Info && "Must have a valid info!");
0115     assert(Owner && "Must have a valid owner!");
0116     return Owner->getOption(Info->AliasID);
0117   }
0118 
0119   /// Get the alias arguments as a \0 separated list.
0120   /// E.g. ["foo", "bar"] would be returned as "foo\0bar\0".
0121   const char *getAliasArgs() const {
0122     assert(Info && "Must have a valid info!");
0123     assert((!Info->AliasArgs || Info->AliasArgs[0] != 0) &&
0124            "AliasArgs should be either 0 or non-empty.");
0125 
0126     return Info->AliasArgs;
0127   }
0128 
0129   /// Get the default prefix for this option.
0130   StringRef getPrefix() const {
0131     assert(Info && "Must have a valid info!");
0132     assert(Owner && "Must have a valid owner!");
0133     return Owner->getOptionPrefix(Info->ID);
0134   }
0135 
0136   /// Get the name of this option with the default prefix.
0137   StringRef getPrefixedName() const {
0138     assert(Info && "Must have a valid info!");
0139     assert(Owner && "Must have a valid owner!");
0140     return Owner->getOptionPrefixedName(Info->ID);
0141   }
0142 
0143   /// Get the help text for this option.
0144   StringRef getHelpText() const {
0145     assert(Info && "Must have a valid info!");
0146     return Info->HelpText;
0147   }
0148 
0149   /// Get the meta-variable list for this option.
0150   StringRef getMetaVar() const {
0151     assert(Info && "Must have a valid info!");
0152     return Info->MetaVar;
0153   }
0154 
0155   unsigned getNumArgs() const { return Info->Param; }
0156 
0157   bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
0158 
0159   RenderStyleKind getRenderStyle() const {
0160     if (Info->Flags & RenderJoined)
0161       return RenderJoinedStyle;
0162     if (Info->Flags & RenderSeparate)
0163       return RenderSeparateStyle;
0164     switch (getKind()) {
0165     case GroupClass:
0166     case InputClass:
0167     case UnknownClass:
0168       return RenderValuesStyle;
0169     case JoinedClass:
0170     case JoinedAndSeparateClass:
0171       return RenderJoinedStyle;
0172     case CommaJoinedClass:
0173       return RenderCommaJoinedStyle;
0174     case FlagClass:
0175     case ValuesClass:
0176     case SeparateClass:
0177     case MultiArgClass:
0178     case JoinedOrSeparateClass:
0179     case RemainingArgsClass:
0180     case RemainingArgsJoinedClass:
0181       return RenderSeparateStyle;
0182     }
0183     llvm_unreachable("Unexpected kind!");
0184   }
0185 
0186   /// Test if this option has the flag \a Val.
0187   bool hasFlag(unsigned Val) const {
0188     return Info->Flags & Val;
0189   }
0190 
0191   /// Test if this option has the visibility flag \a Val.
0192   bool hasVisibilityFlag(unsigned Val) const {
0193     return Info->Visibility & Val;
0194   }
0195 
0196   /// getUnaliasedOption - Return the final option this option
0197   /// aliases (itself, if the option has no alias).
0198   const Option getUnaliasedOption() const {
0199     const Option Alias = getAlias();
0200     if (Alias.isValid()) return Alias.getUnaliasedOption();
0201     return *this;
0202   }
0203 
0204   /// getRenderName - Return the name to use when rendering this
0205   /// option.
0206   StringRef getRenderName() const {
0207     return getUnaliasedOption().getName();
0208   }
0209 
0210   /// matches - Predicate for whether this option is part of the
0211   /// given option (which may be a group).
0212   ///
0213   /// Note that matches against options which are an alias should never be
0214   /// done -- aliases do not participate in matching and so such a query will
0215   /// always be false.
0216   bool matches(OptSpecifier ID) const;
0217 
0218   /// Potentially accept the current argument, returning a new Arg instance,
0219   /// or 0 if the option does not accept this argument (or the argument is
0220   /// missing values).
0221   ///
0222   /// If the option accepts the current argument, accept() sets
0223   /// Index to the position where argument parsing should resume
0224   /// (even if the argument is missing values).
0225   ///
0226   /// \p CurArg The argument to be matched. It may be shorter than the
0227   /// underlying storage to represent a Joined argument.
0228   /// \p GroupedShortOption If true, we are handling the fallback case of
0229   /// parsing a prefix of the current argument as a short option.
0230   std::unique_ptr<Arg> accept(const ArgList &Args, StringRef CurArg,
0231                               bool GroupedShortOption, unsigned &Index) const;
0232 
0233 private:
0234   std::unique_ptr<Arg> acceptInternal(const ArgList &Args, StringRef CurArg,
0235                                       unsigned &Index) const;
0236 
0237 public:
0238   void print(raw_ostream &O, bool AddNewLine = true) const;
0239   void dump() const;
0240 };
0241 
0242 } // end namespace opt
0243 
0244 } // end namespace llvm
0245 
0246 #endif // LLVM_OPTION_OPTION_H