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