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