Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- Arg.h - Parsed Argument Classes --------------------------*- 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 /// \file
0010 /// Defines the llvm::Arg class for parsed arguments.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_OPTION_ARG_H
0015 #define LLVM_OPTION_ARG_H
0016 
0017 #include "llvm/ADT/SmallVector.h"
0018 #include "llvm/ADT/StringRef.h"
0019 #include "llvm/Option/Option.h"
0020 #include <string>
0021 
0022 namespace llvm {
0023 
0024 class raw_ostream;
0025 
0026 namespace opt {
0027 
0028 class ArgList;
0029 
0030 /// A concrete instance of a particular driver option.
0031 ///
0032 /// The Arg class encodes just enough information to be able to
0033 /// derive the argument values efficiently.
0034 class Arg {
0035 private:
0036   /// The option this argument is an instance of.
0037   const Option Opt;
0038 
0039   /// The argument this argument was derived from (during tool chain
0040   /// argument translation), if any.
0041   const Arg *BaseArg;
0042 
0043   /// How this instance of the option was spelled.
0044   StringRef Spelling;
0045 
0046   /// The index at which this argument appears in the containing
0047   /// ArgList.
0048   unsigned Index;
0049 
0050   /// Was this argument used to affect compilation?
0051   ///
0052   /// This is used to generate an "argument unused" warning (without
0053   /// clang::driver::options::TargetSpecific) or "unsupported option" error
0054   /// (with TargetSpecific).
0055   mutable unsigned Claimed : 1;
0056 
0057   /// Used by an unclaimed option with the TargetSpecific flag. If set, report
0058   /// an "argument unused" warning instead of an "unsupported option" error.
0059   unsigned IgnoredTargetSpecific : 1;
0060 
0061   /// Does this argument own its values?
0062   mutable unsigned OwnsValues : 1;
0063 
0064   /// The argument values, as C strings.
0065   SmallVector<const char *, 2> Values;
0066 
0067   /// If this arg was created through an alias, this is the original alias arg.
0068   /// For example, *this might be "-finput-charset=utf-8" and Alias might
0069   /// point to an arg representing "/source-charset:utf-8".
0070   std::unique_ptr<Arg> Alias;
0071 
0072 public:
0073   Arg(const Option Opt, StringRef Spelling, unsigned Index,
0074       const Arg *BaseArg = nullptr);
0075   Arg(const Option Opt, StringRef Spelling, unsigned Index,
0076       const char *Value0, const Arg *BaseArg = nullptr);
0077   Arg(const Option Opt, StringRef Spelling, unsigned Index,
0078       const char *Value0, const char *Value1, const Arg *BaseArg = nullptr);
0079   Arg(const Arg &) = delete;
0080   Arg &operator=(const Arg &) = delete;
0081   ~Arg();
0082 
0083   const Option &getOption() const { return Opt; }
0084 
0085   /// Returns the used prefix and name of the option:
0086   /// For `--foo=bar`, returns `--foo=`.
0087   /// This is often the wrong function to call:
0088   /// * Use `getValue()` to get `bar`.
0089   /// * Use `getAsString()` to get a string suitable for printing an Arg in
0090   ///   a diagnostic.
0091   StringRef getSpelling() const { return Spelling; }
0092 
0093   unsigned getIndex() const { return Index; }
0094 
0095   /// Return the base argument which generated this arg.
0096   ///
0097   /// This is either the argument itself or the argument it was
0098   /// derived from during tool chain specific argument translation.
0099   const Arg &getBaseArg() const {
0100     return BaseArg ? *BaseArg : *this;
0101   }
0102   Arg &getBaseArg() { return BaseArg ? const_cast<Arg &>(*BaseArg) : *this; }
0103   void setBaseArg(const Arg *BaseArg) { this->BaseArg = BaseArg; }
0104 
0105   /// Args are converted to their unaliased form.  For args that originally
0106   /// came from an alias, this returns the alias the arg was produced from.
0107   const Arg* getAlias() const { return Alias.get(); }
0108   void setAlias(std::unique_ptr<Arg> Alias) { this->Alias = std::move(Alias); }
0109 
0110   bool getOwnsValues() const { return OwnsValues; }
0111   void setOwnsValues(bool Value) const { OwnsValues = Value; }
0112 
0113   bool isClaimed() const { return getBaseArg().Claimed; }
0114   void claim() const { getBaseArg().Claimed = true; }
0115 
0116   bool isIgnoredTargetSpecific() const {
0117     return getBaseArg().IgnoredTargetSpecific;
0118   }
0119   void ignoreTargetSpecific() {
0120     getBaseArg().IgnoredTargetSpecific = true;
0121   }
0122 
0123   unsigned getNumValues() const { return Values.size(); }
0124 
0125   const char *getValue(unsigned N = 0) const {
0126     return Values[N];
0127   }
0128 
0129   SmallVectorImpl<const char *> &getValues() { return Values; }
0130   const SmallVectorImpl<const char *> &getValues() const { return Values; }
0131 
0132   bool containsValue(StringRef Value) const {
0133     return llvm::is_contained(Values, Value);
0134   }
0135 
0136   /// Append the argument onto the given array as strings.
0137   void render(const ArgList &Args, ArgStringList &Output) const;
0138 
0139   /// Append the argument, render as an input, onto the given
0140   /// array as strings.
0141   ///
0142   /// The distinction is that some options only render their values
0143   /// when rendered as a input (e.g., Xlinker).
0144   void renderAsInput(const ArgList &Args, ArgStringList &Output) const;
0145 
0146   void print(raw_ostream &O) const;
0147   void dump() const;
0148 
0149   /// Return a formatted version of the argument and its values, for
0150   /// diagnostics. Since this is for diagnostics, if this Arg was produced
0151   /// through an alias, this returns the string representation of the alias
0152   /// that the user wrote.
0153   std::string getAsString(const ArgList &Args) const;
0154 };
0155 
0156 } // end namespace opt
0157 
0158 } // end namespace llvm
0159 
0160 #endif // LLVM_OPTION_ARG_H