LLVM API Documentation

Arg.h
Go to the documentation of this file.
00001 //===--- Arg.h - Parsed Argument Classes ------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief Defines the llvm::Arg class for parsed arguments.
00012 ///
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_OPTION_ARG_H
00016 #define LLVM_OPTION_ARG_H
00017 
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/ADT/StringRef.h"
00020 #include "llvm/Option/Option.h"
00021 #include <string>
00022 
00023 namespace llvm {
00024 namespace opt {
00025 class ArgList;
00026 
00027 /// \brief A concrete instance of a particular driver option.
00028 ///
00029 /// The Arg class encodes just enough information to be able to
00030 /// derive the argument values efficiently.
00031 class Arg {
00032   Arg(const Arg &) LLVM_DELETED_FUNCTION;
00033   void operator=(const Arg &) LLVM_DELETED_FUNCTION;
00034 
00035 private:
00036   /// \brief The option this argument is an instance of.
00037   const Option Opt;
00038 
00039   /// \brief The argument this argument was derived from (during tool chain
00040   /// argument translation), if any.
00041   const Arg *BaseArg;
00042 
00043   /// \brief How this instance of the option was spelled.
00044   StringRef Spelling;
00045 
00046   /// \brief The index at which this argument appears in the containing
00047   /// ArgList.
00048   unsigned Index;
00049 
00050   /// \brief Was this argument used to effect compilation?
00051   ///
00052   /// This is used for generating "argument unused" diagnostics.
00053   mutable unsigned Claimed : 1;
00054 
00055   /// \brief Does this argument own its values?
00056   mutable unsigned OwnsValues : 1;
00057 
00058   /// \brief The argument values, as C strings.
00059   SmallVector<const char *, 2> Values;
00060 
00061 public:
00062   Arg(const Option Opt, StringRef Spelling, unsigned Index,
00063       const Arg *BaseArg = nullptr);
00064   Arg(const Option Opt, StringRef Spelling, unsigned Index,
00065       const char *Value0, const Arg *BaseArg = nullptr);
00066   Arg(const Option Opt, StringRef Spelling, unsigned Index,
00067       const char *Value0, const char *Value1, const Arg *BaseArg = nullptr);
00068   ~Arg();
00069 
00070   const Option &getOption() const { return Opt; }
00071   StringRef getSpelling() const { return Spelling; }
00072   unsigned getIndex() const { return Index; }
00073 
00074   /// \brief Return the base argument which generated this arg.
00075   ///
00076   /// This is either the argument itself or the argument it was
00077   /// derived from during tool chain specific argument translation.
00078   const Arg &getBaseArg() const {
00079     return BaseArg ? *BaseArg : *this;
00080   }
00081   void setBaseArg(const Arg *_BaseArg) {
00082     BaseArg = _BaseArg;
00083   }
00084 
00085   bool getOwnsValues() const { return OwnsValues; }
00086   void setOwnsValues(bool Value) const { OwnsValues = Value; }
00087 
00088   bool isClaimed() const { return getBaseArg().Claimed; }
00089 
00090   /// \brief Set the Arg claimed bit.
00091   void claim() const { getBaseArg().Claimed = true; }
00092 
00093   unsigned getNumValues() const { return Values.size(); }
00094   const char *getValue(unsigned N = 0) const {
00095     return Values[N];
00096   }
00097 
00098   SmallVectorImpl<const char*> &getValues() {
00099     return Values;
00100   }
00101 
00102   bool containsValue(StringRef Value) const {
00103     for (unsigned i = 0, e = getNumValues(); i != e; ++i)
00104       if (Values[i] == Value)
00105         return true;
00106     return false;
00107   }
00108 
00109   /// \brief Append the argument onto the given array as strings.
00110   void render(const ArgList &Args, ArgStringList &Output) const;
00111 
00112   /// \brief Append the argument, render as an input, onto the given
00113   /// array as strings.
00114   ///
00115   /// The distinction is that some options only render their values
00116   /// when rendered as a input (e.g., Xlinker).
00117   void renderAsInput(const ArgList &Args, ArgStringList &Output) const;
00118 
00119   void dump() const;
00120 
00121   /// \brief Return a formatted version of the argument and
00122   /// its values, for debugging and diagnostics.
00123   std::string getAsString(const ArgList &Args) const;
00124 };
00125 
00126 } // end namespace opt
00127 } // end namespace llvm
00128 
00129 #endif