MacroInfo.h revision 42e6737f2efb113563140ad794c21c7709250402
1//===--- MacroInfo.h - Information about #defined identifiers ---*- 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// This file defines the MacroInfo interface. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_MACROINFO_H 15#define LLVM_CLANG_MACROINFO_H 16 17#include "clang/Lex/Token.h" 18#include "llvm/ADT/SmallVector.h" 19#include <vector> 20#include <cassert> 21 22namespace clang { 23 class Preprocessor; 24 25/// MacroInfo - Each identifier that is #define'd has an instance of this class 26/// associated with it, used to implement macro expansion. 27class MacroInfo { 28 //===--------------------------------------------------------------------===// 29 // State set when the macro is defined. 30 31 /// Location - This is the place the macro is defined. 32 SourceLocation Location; 33 34 /// Arguments - The list of arguments for a function-like macro. This can be 35 /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this 36 /// includes the __VA_ARGS__ identifier on the list. 37 IdentifierInfo **ArgumentList; 38 unsigned NumArguments; 39 40 /// ReplacementTokens - This is the list of tokens that the macro is defined 41 /// to. 42 llvm::SmallVector<Token, 8> ReplacementTokens; 43 44 /// IsFunctionLike - True if this macro is a function-like macro, false if it 45 /// is an object-like macro. 46 bool IsFunctionLike : 1; 47 48 /// IsC99Varargs - True if this macro is of the form "#define X(...)" or 49 /// "#define X(Y,Z,...)". The __VA_ARGS__ token should be replaced with the 50 /// contents of "..." in an invocation. 51 bool IsC99Varargs : 1; 52 53 /// IsGNUVarargs - True if this macro is of the form "#define X(a...)". The 54 /// "a" identifier in the replacement list will be replaced with all arguments 55 /// of the macro starting with the specified one. 56 bool IsGNUVarargs : 1; 57 58 /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if 59 /// it has not yet been redefined or undefined. 60 bool IsBuiltinMacro : 1; 61 62private: 63 //===--------------------------------------------------------------------===// 64 // State that changes as the macro is used. 65 66 /// IsDisabled - True if we have started an expansion of this macro already. 67 /// This disbles recursive expansion, which would be quite bad for things like 68 /// #define A A. 69 bool IsDisabled : 1; 70 71 /// IsUsed - True if this macro is either defined in the main file and has 72 /// been used, or if it is not defined in the main file. This is used to 73 /// emit -Wunused-macros diagnostics. 74 bool IsUsed : 1; 75public: 76 MacroInfo(SourceLocation DefLoc); 77 78 ~MacroInfo() { 79 delete[] ArgumentList; 80 } 81 82 /// getDefinitionLoc - Return the location that the macro was defined at. 83 /// 84 SourceLocation getDefinitionLoc() const { return Location; } 85 86 /// isIdenticalTo - Return true if the specified macro definition is equal to 87 /// this macro in spelling, arguments, and whitespace. This is used to emit 88 /// duplicate definition warnings. This implements the rules in C99 6.10.3. 89 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const; 90 91 /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag. 92 /// 93 void setIsBuiltinMacro(bool Val = true) { 94 IsBuiltinMacro = Val; 95 } 96 97 /// setIsUsed - Set the value of the IsUsed flag. 98 /// 99 void setIsUsed(bool Val) { 100 IsUsed = Val; 101 } 102 103 /// setArgumentList - Set the specified list of identifiers as the argument 104 /// list for this macro. 105 template<typename ItTy> 106 void setArgumentList(ItTy ArgBegin, ItTy ArgEnd) { 107 assert(ArgumentList == 0 && "Argument list already set!"); 108 unsigned NumArgs = ArgEnd-ArgBegin; 109 if (NumArgs == 0) return; 110 NumArguments = NumArgs; 111 ArgumentList = new IdentifierInfo*[NumArgs]; 112 for (unsigned i = 0; ArgBegin != ArgEnd; ++i, ++ArgBegin) 113 ArgumentList[i] = *ArgBegin; 114 } 115 116 /// Arguments - The list of arguments for a function-like macro. This can be 117 /// empty, for, e.g. "#define X()". 118 typedef IdentifierInfo* const *arg_iterator; 119 arg_iterator arg_begin() const { return ArgumentList; } 120 arg_iterator arg_end() const { return ArgumentList+NumArguments; } 121 unsigned getNumArgs() const { return NumArguments; } 122 123 /// getArgumentNum - Return the argument number of the specified identifier, 124 /// or -1 if the identifier is not a formal argument identifier. 125 int getArgumentNum(IdentifierInfo *Arg) const { 126 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I) 127 if (*I == Arg) return I-arg_begin(); 128 return -1; 129 } 130 131 /// Function/Object-likeness. Keep track of whether this macro has formal 132 /// parameters. 133 void setIsFunctionLike() { IsFunctionLike = true; } 134 bool isFunctionLike() const { return IsFunctionLike; } 135 bool isObjectLike() const { return !IsFunctionLike; } 136 137 /// Varargs querying methods. This can only be set for function-like macros. 138 void setIsC99Varargs() { IsC99Varargs = true; } 139 void setIsGNUVarargs() { IsGNUVarargs = true; } 140 bool isC99Varargs() const { return IsC99Varargs; } 141 bool isGNUVarargs() const { return IsGNUVarargs; } 142 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } 143 144 /// isBuiltinMacro - Return true if this macro is a builtin macro, such as 145 /// __LINE__, which requires processing before expansion. 146 bool isBuiltinMacro() const { return IsBuiltinMacro; } 147 148 /// isUsed - Return false if this macro is defined in the main file and has 149 /// not yet been used. 150 bool isUsed() const { return IsUsed; } 151 152 /// getNumTokens - Return the number of tokens that this macro expands to. 153 /// 154 unsigned getNumTokens() const { 155 return ReplacementTokens.size(); 156 } 157 158 const Token &getReplacementToken(unsigned Tok) const { 159 assert(Tok < ReplacementTokens.size() && "Invalid token #"); 160 return ReplacementTokens[Tok]; 161 } 162 163 typedef llvm::SmallVector<Token, 8>::const_iterator tokens_iterator; 164 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); } 165 tokens_iterator tokens_end() const { return ReplacementTokens.end(); } 166 167 /// AddTokenToBody - Add the specified token to the replacement text for the 168 /// macro. 169 void AddTokenToBody(const Token &Tok) { 170 ReplacementTokens.push_back(Tok); 171 } 172 173 /// isEnabled - Return true if this macro is enabled: in other words, that we 174 /// are not currently in an expansion of this macro. 175 bool isEnabled() const { return !IsDisabled; } 176 177 void EnableMacro() { 178 assert(IsDisabled && "Cannot enable an already-enabled macro!"); 179 IsDisabled = false; 180 } 181 182 void DisableMacro() { 183 assert(!IsDisabled && "Cannot disable an already-disabled macro!"); 184 IsDisabled = true; 185 } 186}; 187 188} // end namespace clang 189 190#endif 191