Arg.h revision 96a564f2beec8c1930f0640844eec433e03bbce8
1//===--- Arg.h - Parsed Argument Classes ------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9/// 10/// \file 11/// \brief Defines the llvm::Arg class for parsed arguments. 12/// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_SUPPORT_ARG_H_ 16#define LLVM_SUPPORT_ARG_H_ 17 18#include "llvm/ADT/SmallVector.h" 19#include "llvm/ADT/StringRef.h" 20#include "llvm/Option/Option.h" 21#include <string> 22 23namespace llvm { 24namespace opt { 25class ArgList; 26 27/// \brief A concrete instance of a particular driver option. 28/// 29/// The Arg class encodes just enough information to be able to 30/// derive the argument values efficiently. In addition, Arg 31/// instances have an intrusive double linked list which is used by 32/// ArgList to provide efficient iteration over all instances of a 33/// particular option. 34class Arg { 35 Arg(const Arg &) LLVM_DELETED_FUNCTION; 36 void operator=(const Arg &) LLVM_DELETED_FUNCTION; 37 38private: 39 /// \brief The option this argument is an instance of. 40 const Option Opt; 41 42 /// \brief The argument this argument was derived from (during tool chain 43 /// argument translation), if any. 44 const Arg *BaseArg; 45 46 /// \brief How this instance of the option was spelled. 47 StringRef Spelling; 48 49 /// \brief The index at which this argument appears in the containing 50 /// ArgList. 51 unsigned Index; 52 53 /// \brief Was this argument used to effect compilation? 54 /// 55 /// This is used for generating "argument unused" diagnostics. 56 mutable unsigned Claimed : 1; 57 58 /// \brief Does this argument own its values? 59 mutable unsigned OwnsValues : 1; 60 61 /// \brief The argument values, as C strings. 62 SmallVector<const char *, 2> Values; 63 64public: 65 Arg(const Option Opt, StringRef Spelling, unsigned Index, 66 const Arg *BaseArg = 0); 67 Arg(const Option Opt, StringRef Spelling, unsigned Index, 68 const char *Value0, const Arg *BaseArg = 0); 69 Arg(const Option Opt, StringRef Spelling, unsigned Index, 70 const char *Value0, const char *Value1, const Arg *BaseArg = 0); 71 ~Arg(); 72 73 const Option getOption() const { return Opt; } 74 StringRef getSpelling() const { return Spelling; } 75 unsigned getIndex() const { return Index; } 76 77 /// \brief Return the base argument which generated this arg. 78 /// 79 /// This is either the argument itself or the argument it was 80 /// derived from during tool chain specific argument translation. 81 const Arg &getBaseArg() const { 82 return BaseArg ? *BaseArg : *this; 83 } 84 void setBaseArg(const Arg *_BaseArg) { 85 BaseArg = _BaseArg; 86 } 87 88 bool getOwnsValues() const { return OwnsValues; } 89 void setOwnsValues(bool Value) const { OwnsValues = Value; } 90 91 bool isClaimed() const { return getBaseArg().Claimed; } 92 93 /// \brief Set the Arg claimed bit. 94 void claim() const { getBaseArg().Claimed = true; } 95 96 unsigned getNumValues() const { return Values.size(); } 97 const char *getValue(unsigned N = 0) const { 98 return Values[N]; 99 } 100 101 SmallVectorImpl<const char*> &getValues() { 102 return Values; 103 } 104 105 bool containsValue(StringRef Value) const { 106 for (unsigned i = 0, e = getNumValues(); i != e; ++i) 107 if (Values[i] == Value) 108 return true; 109 return false; 110 } 111 112 /// \brief Append the argument onto the given array as strings. 113 void render(const ArgList &Args, ArgStringList &Output) const; 114 115 /// \brief Append the argument, render as an input, onto the given 116 /// array as strings. 117 /// 118 /// The distinction is that some options only render their values 119 /// when rendered as a input (e.g., Xlinker). 120 void renderAsInput(const ArgList &Args, ArgStringList &Output) const; 121 122 void dump() const; 123 124 /// \brief Return a formatted version of the argument and 125 /// its values, for debugging and diagnostics. 126 std::string getAsString(const ArgList &Args) const; 127}; 128 129} // end namespace opt 130} // end namespace llvm 131 132#endif 133