MacroInfo.h revision 083abdf67f157e9d2ab5a8c9d5e71240479d3c99
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 ~MacroInfo() { 83 assert(ArgumentList == 0 && "Didn't call destroy before dtor!"); 84 } 85 86public: 87 MacroInfo(SourceLocation DefLoc); 88 89 /// FreeArgumentList - Free the argument list of the macro, restoring it to a 90 /// state where it can be reused for other devious purposes. 91 void FreeArgumentList(llvm::BumpPtrAllocator &PPAllocator) { 92 PPAllocator.Deallocate(ArgumentList); 93 ArgumentList = 0; 94 NumArguments = 0; 95 } 96 97 /// Destroy - destroy this MacroInfo object. 98 void Destroy(llvm::BumpPtrAllocator &PPAllocator) { 99 FreeArgumentList(PPAllocator); 100 this->~MacroInfo(); 101 } 102 103 /// getDefinitionLoc - Return the location that the macro was defined at. 104 /// 105 SourceLocation getDefinitionLoc() const { return Location; } 106 107 /// setDefinitionEndLoc - Set the location of the last token in the macro. 108 /// 109 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; } 110 /// getDefinitionEndLoc - Return the location of the last token in the macro. 111 /// 112 SourceLocation getDefinitionEndLoc() const { return EndLocation; } 113 114 /// isIdenticalTo - Return true if the specified macro definition is equal to 115 /// this macro in spelling, arguments, and whitespace. This is used to emit 116 /// duplicate definition warnings. This implements the rules in C99 6.10.3. 117 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const; 118 119 /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag. 120 /// 121 void setIsBuiltinMacro(bool Val = true) { 122 IsBuiltinMacro = Val; 123 } 124 125 /// setIsUsed - Set the value of the IsUsed flag. 126 /// 127 void setIsUsed(bool Val) { 128 IsUsed = Val; 129 } 130 131 /// setArgumentList - Set the specified list of identifiers as the argument 132 /// list for this macro. 133 void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs, 134 llvm::BumpPtrAllocator &PPAllocator) { 135 assert(ArgumentList == 0 && NumArguments == 0 && 136 "Argument list already set!"); 137 if (NumArgs == 0) return; 138 139 NumArguments = NumArgs; 140 ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs); 141 for (unsigned i = 0; i != NumArgs; ++i) 142 ArgumentList[i] = List[i]; 143 } 144 145 /// Arguments - The list of arguments for a function-like macro. This can be 146 /// empty, for, e.g. "#define X()". 147 typedef IdentifierInfo* const *arg_iterator; 148 bool arg_empty() const { return NumArguments == 0; } 149 arg_iterator arg_begin() const { return ArgumentList; } 150 arg_iterator arg_end() const { return ArgumentList+NumArguments; } 151 unsigned getNumArgs() const { return NumArguments; } 152 153 /// getArgumentNum - Return the argument number of the specified identifier, 154 /// or -1 if the identifier is not a formal argument identifier. 155 int getArgumentNum(IdentifierInfo *Arg) const { 156 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I) 157 if (*I == Arg) return I-arg_begin(); 158 return -1; 159 } 160 161 /// Function/Object-likeness. Keep track of whether this macro has formal 162 /// parameters. 163 void setIsFunctionLike() { IsFunctionLike = true; } 164 bool isFunctionLike() const { return IsFunctionLike; } 165 bool isObjectLike() const { return !IsFunctionLike; } 166 167 /// Varargs querying methods. This can only be set for function-like macros. 168 void setIsC99Varargs() { IsC99Varargs = true; } 169 void setIsGNUVarargs() { IsGNUVarargs = true; } 170 bool isC99Varargs() const { return IsC99Varargs; } 171 bool isGNUVarargs() const { return IsGNUVarargs; } 172 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } 173 174 /// isBuiltinMacro - Return true if this macro is a builtin macro, such as 175 /// __LINE__, which requires processing before expansion. 176 bool isBuiltinMacro() const { return IsBuiltinMacro; } 177 178 /// isFromPCH - Return true if this macro was loaded from a PCH file. 179 bool isFromPCH() const { return IsFromPCH; } 180 181 /// setIsFromPCH - Set whether this macro was loaded from a PCH file. 182 void setIsFromPCH(bool FromPCH = true) { IsFromPCH = FromPCH; } 183 184 /// isUsed - Return false if this macro is defined in the main file and has 185 /// not yet been used. 186 bool isUsed() const { return IsUsed; } 187 188 /// getNumTokens - Return the number of tokens that this macro expands to. 189 /// 190 unsigned getNumTokens() const { 191 return ReplacementTokens.size(); 192 } 193 194 const Token &getReplacementToken(unsigned Tok) const { 195 assert(Tok < ReplacementTokens.size() && "Invalid token #"); 196 return ReplacementTokens[Tok]; 197 } 198 199 typedef llvm::SmallVector<Token, 8>::const_iterator tokens_iterator; 200 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); } 201 tokens_iterator tokens_end() const { return ReplacementTokens.end(); } 202 bool tokens_empty() const { return ReplacementTokens.empty(); } 203 204 /// AddTokenToBody - Add the specified token to the replacement text for the 205 /// macro. 206 void AddTokenToBody(const Token &Tok) { 207 ReplacementTokens.push_back(Tok); 208 } 209 210 /// isEnabled - Return true if this macro is enabled: in other words, that we 211 /// are not currently in an expansion of this macro. 212 bool isEnabled() const { return !IsDisabled; } 213 214 void EnableMacro() { 215 assert(IsDisabled && "Cannot enable an already-enabled macro!"); 216 IsDisabled = false; 217 } 218 219 void DisableMacro() { 220 assert(!IsDisabled && "Cannot disable an already-disabled macro!"); 221 IsDisabled = true; 222 } 223}; 224 225} // end namespace clang 226 227#endif 228