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