MacroInfo.h revision 5f016e2cb5d11daeb237544de1c5d59f20fe1a6e
1//===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Chris Lattner and is distributed under 6// the University of Illinois Open Source 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/LexerToken.h" 18#include <vector> 19 20namespace clang { 21 class Preprocessor; 22 23/// MacroInfo - Each identifier that is #define'd has an instance of this class 24/// associated with it, used to implement macro expansion. 25class MacroInfo { 26 //===--------------------------------------------------------------------===// 27 // State set when the macro is defined. 28 29 /// Location - This is the place the macro is defined. 30 SourceLocation Location; 31 32 /// Arguments - The list of arguments for a function-like macro. This can be 33 /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this 34 /// includes the __VA_ARGS__ identifier on the list. 35 std::vector<IdentifierInfo*> Arguments; 36 37 /// ReplacementTokens - This is the list of tokens that the macro is defined 38 /// to. 39 std::vector<LexerToken> ReplacementTokens; 40 41 /// IsFunctionLike - True if this macro is a function-like macro, false if it 42 /// is an object-like macro. 43 bool IsFunctionLike : 1; 44 45 /// IsC99Varargs - True if this macro is of the form "#define X(...)" or 46 /// "#define X(Y,Z,...)". The __VA_ARGS__ token should be replaced with the 47 /// contents of "..." in an invocation. 48 bool IsC99Varargs : 1; 49 50 /// IsGNUVarargs - True if this macro is of the form "#define X(a...)". The 51 /// "a" identifier in th replacement list will be replaced with all arguments 52 /// of the macro starting with the specified one. 53 bool IsGNUVarargs : 1; 54 55 /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if 56 /// it has not yet been redefined or undefined. 57 bool IsBuiltinMacro : 1; 58 59 /// IsTargetSpecific - True if this is a target-specific macro defined with 60 /// #define_target. 61 bool IsTargetSpecific : 1; 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 /// getDefinitionLoc - Return the location that the macro was defined at. 79 /// 80 SourceLocation getDefinitionLoc() const { return Location; } 81 82 /// isIdenticalTo - Return true if the specified macro definition is equal to 83 /// this macro in spelling, arguments, and whitespace. This is used to emit 84 /// duplicate definition warnings. This implements the rules in C99 6.10.3. 85 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const; 86 87 /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag. 88 /// 89 void setIsBuiltinMacro(bool Val = true) { 90 IsBuiltinMacro = Val; 91 } 92 93 /// setIsTargetSpecific - Set or clear the IsTargetSpecific flag. 94 /// 95 void setIsTargetSpecific(bool Val = true) { 96 IsTargetSpecific = Val; 97 } 98 bool isTargetSpecific() const { return IsTargetSpecific; } 99 100 /// setIsUsed - Set the value of the IsUsed flag. 101 /// 102 void setIsUsed(bool Val) { 103 IsUsed = Val; 104 } 105 106 /// addArgument - Add an argument to the list of formal arguments for this 107 /// function-like macro. 108 void addArgument(IdentifierInfo *Arg) { 109 Arguments.push_back(Arg); 110 } 111 112 /// getArgumentNum - Return the argument number of the specified identifier, 113 /// or -1 if the identifier is not a formal argument identifier. 114 int getArgumentNum(IdentifierInfo *Arg) { 115 for (unsigned i = 0, e = Arguments.size(); i != e; ++i) 116 if (Arguments[i] == Arg) return i; 117 return -1; 118 } 119 120 /// Arguments - The list of arguments for a function-like macro. This can be 121 /// empty, for, e.g. "#define X()". 122 typedef std::vector<IdentifierInfo*>::const_iterator arg_iterator; 123 arg_iterator arg_begin() const { return Arguments.begin(); } 124 arg_iterator arg_end() const { return Arguments.end(); } 125 unsigned getNumArgs() const { return Arguments.size(); } 126 127 /// Function/Object-likeness. Keep track of whether this macro has formal 128 /// parameters. 129 void setIsFunctionLike() { IsFunctionLike = true; } 130 bool isFunctionLike() const { return IsFunctionLike; } 131 bool isObjectLike() const { return !IsFunctionLike; } 132 133 /// Varargs querying methods. This can only be set for function-like macros. 134 void setIsC99Varargs() { IsC99Varargs = true; } 135 void setIsGNUVarargs() { IsGNUVarargs = true; } 136 bool isC99Varargs() const { return IsC99Varargs; } 137 bool isGNUVarargs() const { return IsGNUVarargs; } 138 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } 139 140 /// isBuiltinMacro - Return true if this macro is a builtin macro, such as 141 /// __LINE__, which requires processing before expansion. 142 bool isBuiltinMacro() const { return IsBuiltinMacro; } 143 144 /// isUsed - Return false if this macro is defined in the main file and has 145 /// not yet been used. 146 bool isUsed() const { return IsUsed; } 147 148 /// getNumTokens - Return the number of tokens that this macro expands to. 149 /// 150 unsigned getNumTokens() const { 151 return ReplacementTokens.size(); 152 } 153 154 const LexerToken &getReplacementToken(unsigned Tok) const { 155 assert(Tok < ReplacementTokens.size() && "Invalid token #"); 156 return ReplacementTokens[Tok]; 157 } 158 159 const std::vector<LexerToken> &getReplacementTokens() const { 160 return ReplacementTokens; 161 } 162 163 /// AddTokenToBody - Add the specified token to the replacement text for the 164 /// macro. 165 void AddTokenToBody(const LexerToken &Tok) { 166 ReplacementTokens.push_back(Tok); 167 } 168 169 /// isEnabled - Return true if this macro is enabled: in other words, that we 170 /// are not currently in an expansion of this macro. 171 bool isEnabled() const { return !IsDisabled; } 172 173 void EnableMacro() { 174 assert(IsDisabled && "Cannot enable an already-enabled macro!"); 175 IsDisabled = false; 176 } 177 178 void DisableMacro() { 179 assert(!IsDisabled && "Cannot disable an already-disabled macro!"); 180 IsDisabled = true; 181 } 182}; 183 184} // end namespace clang 185 186#endif 187