MacroInfo.h revision 2c1ab9079cb117dc0470ab423fe0bc5177546339
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 "llvm/Support/Allocator.h" 20#include <vector> 21#include <cassert> 22 23namespace clang { 24 class Preprocessor; 25 26/// MacroInfo - Each identifier that is #define'd has an instance of this class 27/// associated with it, used to implement macro expansion. 28class MacroInfo { 29 //===--------------------------------------------------------------------===// 30 // State set when the macro is defined. 31 32 /// Location - This is the place the macro is defined. 33 SourceLocation Location; 34 /// EndLocation - The location of the last token in the macro. 35 SourceLocation EndLocation; 36 37 /// Arguments - The list of arguments for a function-like macro. This can be 38 /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this 39 /// includes the __VA_ARGS__ identifier on the list. 40 IdentifierInfo **ArgumentList; 41 unsigned NumArguments; 42 43 /// ReplacementTokens - This is the list of tokens that the macro is defined 44 /// to. 45 llvm::SmallVector<Token, 8> ReplacementTokens; 46 47 /// IsFunctionLike - True if this macro is a function-like macro, false if it 48 /// is an object-like macro. 49 bool IsFunctionLike : 1; 50 51 /// IsC99Varargs - True if this macro is of the form "#define X(...)" or 52 /// "#define X(Y,Z,...)". The __VA_ARGS__ token should be replaced with the 53 /// contents of "..." in an invocation. 54 bool IsC99Varargs : 1; 55 56 /// IsGNUVarargs - True if this macro is of the form "#define X(a...)". The 57 /// "a" identifier in the replacement list will be replaced with all arguments 58 /// of the macro starting with the specified one. 59 bool IsGNUVarargs : 1; 60 61 /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if 62 /// it has not yet been redefined or undefined. 63 bool IsBuiltinMacro : 1; 64 65 /// IsFromPCH - True if this macro was loaded from a PCH file. 66 bool IsFromPCH : 1; 67 68private: 69 //===--------------------------------------------------------------------===// 70 // State that changes as the macro is used. 71 72 /// IsDisabled - True if we have started an expansion of this macro already. 73 /// This disbles recursive expansion, which would be quite bad for things like 74 /// #define A A. 75 bool IsDisabled : 1; 76 77 /// IsUsed - True if this macro is either defined in the main file and has 78 /// been used, or if it is not defined in the main file. This is used to 79 /// emit -Wunused-macros diagnostics. 80 bool IsUsed : 1; 81 82 /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined 83 /// without emitting a warning. 84 bool IsAllowRedefinitionsWithoutWarning : 1; 85 86 ~MacroInfo() { 87 assert(ArgumentList == 0 && "Didn't call destroy before dtor!"); 88 } 89 90public: 91 MacroInfo(SourceLocation DefLoc); 92 MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator); 93 94 /// FreeArgumentList - Free the argument list of the macro, restoring it to a 95 /// state where it can be reused for other devious purposes. 96 void FreeArgumentList() { 97 ArgumentList = 0; 98 NumArguments = 0; 99 } 100 101 /// Destroy - destroy this MacroInfo object. 102 void Destroy() { 103 FreeArgumentList(); 104 this->~MacroInfo(); 105 } 106 107 /// getDefinitionLoc - Return the location that the macro was defined at. 108 /// 109 SourceLocation getDefinitionLoc() const { return Location; } 110 111 /// setDefinitionEndLoc - Set the location of the last token in the macro. 112 /// 113 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; } 114 /// getDefinitionEndLoc - Return the location of the last token in the macro. 115 /// 116 SourceLocation getDefinitionEndLoc() const { return EndLocation; } 117 118 /// isIdenticalTo - Return true if the specified macro definition is equal to 119 /// this macro in spelling, arguments, and whitespace. This is used to emit 120 /// duplicate definition warnings. This implements the rules in C99 6.10.3. 121 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const; 122 123 /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag. 124 /// 125 void setIsBuiltinMacro(bool Val = true) { 126 IsBuiltinMacro = Val; 127 } 128 129 /// setIsUsed - Set the value of the IsUsed flag. 130 /// 131 void setIsUsed(bool Val) { 132 IsUsed = Val; 133 } 134 135 /// setIsAllowRedefinitionsWithoutWarning - Set the value of the 136 /// IsAllowRedefinitionsWithoutWarning flag. 137 void setIsAllowRedefinitionsWithoutWarning(bool Val) { 138 IsAllowRedefinitionsWithoutWarning = Val; 139 } 140 141 /// setArgumentList - Set the specified list of identifiers as the argument 142 /// list for this macro. 143 void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs, 144 llvm::BumpPtrAllocator &PPAllocator) { 145 assert(ArgumentList == 0 && NumArguments == 0 && 146 "Argument list already set!"); 147 if (NumArgs == 0) return; 148 149 NumArguments = NumArgs; 150 ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs); 151 for (unsigned i = 0; i != NumArgs; ++i) 152 ArgumentList[i] = List[i]; 153 } 154 155 /// Arguments - The list of arguments for a function-like macro. This can be 156 /// empty, for, e.g. "#define X()". 157 typedef IdentifierInfo* const *arg_iterator; 158 bool arg_empty() const { return NumArguments == 0; } 159 arg_iterator arg_begin() const { return ArgumentList; } 160 arg_iterator arg_end() const { return ArgumentList+NumArguments; } 161 unsigned getNumArgs() const { return NumArguments; } 162 163 /// getArgumentNum - Return the argument number of the specified identifier, 164 /// or -1 if the identifier is not a formal argument identifier. 165 int getArgumentNum(IdentifierInfo *Arg) const { 166 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I) 167 if (*I == Arg) return I-arg_begin(); 168 return -1; 169 } 170 171 /// Function/Object-likeness. Keep track of whether this macro has formal 172 /// parameters. 173 void setIsFunctionLike() { IsFunctionLike = true; } 174 bool isFunctionLike() const { return IsFunctionLike; } 175 bool isObjectLike() const { return !IsFunctionLike; } 176 177 /// Varargs querying methods. This can only be set for function-like macros. 178 void setIsC99Varargs() { IsC99Varargs = true; } 179 void setIsGNUVarargs() { IsGNUVarargs = true; } 180 bool isC99Varargs() const { return IsC99Varargs; } 181 bool isGNUVarargs() const { return IsGNUVarargs; } 182 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } 183 184 /// isBuiltinMacro - Return true if this macro is a builtin macro, such as 185 /// __LINE__, which requires processing before expansion. 186 bool isBuiltinMacro() const { return IsBuiltinMacro; } 187 188 /// isFromPCH - Return true if this macro was loaded from a PCH file. 189 bool isFromPCH() const { return IsFromPCH; } 190 191 /// setIsFromPCH - Set whether this macro was loaded from a PCH file. 192 void setIsFromPCH(bool FromPCH = true) { IsFromPCH = FromPCH; } 193 194 /// isUsed - Return false if this macro is defined in the main file and has 195 /// not yet been used. 196 bool isUsed() const { return IsUsed; } 197 198 /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be 199 /// redefined without warning. 200 bool isAllowRedefinitionsWithoutWarning() const { 201 return IsAllowRedefinitionsWithoutWarning; 202 } 203 204 /// getNumTokens - Return the number of tokens that this macro expands to. 205 /// 206 unsigned getNumTokens() const { 207 return ReplacementTokens.size(); 208 } 209 210 const Token &getReplacementToken(unsigned Tok) const { 211 assert(Tok < ReplacementTokens.size() && "Invalid token #"); 212 return ReplacementTokens[Tok]; 213 } 214 215 typedef llvm::SmallVector<Token, 8>::const_iterator tokens_iterator; 216 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); } 217 tokens_iterator tokens_end() const { return ReplacementTokens.end(); } 218 bool tokens_empty() const { return ReplacementTokens.empty(); } 219 220 /// AddTokenToBody - Add the specified token to the replacement text for the 221 /// macro. 222 void AddTokenToBody(const Token &Tok) { 223 ReplacementTokens.push_back(Tok); 224 } 225 226 /// isEnabled - Return true if this macro is enabled: in other words, that we 227 /// are not currently in an expansion of this macro. 228 bool isEnabled() const { return !IsDisabled; } 229 230 void EnableMacro() { 231 assert(IsDisabled && "Cannot enable an already-enabled macro!"); 232 IsDisabled = false; 233 } 234 235 void DisableMacro() { 236 assert(!IsDisabled && "Cannot disable an already-disabled macro!"); 237 IsDisabled = true; 238 } 239}; 240 241} // end namespace clang 242 243#endif 244