MacroInfo.h revision e8219a655128b98d0573658a139de5d848451fda
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 22namespace 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. 27class 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 /// \brief The location where the macro was #undef'd, or an invalid location 36 /// for macros that haven't been undefined. 37 SourceLocation UndefLocation; 38 /// \brief Previous definition, the identifier of this macro was defined to, 39 /// or NULL. 40 MacroInfo *PreviousDefinition; 41 42 /// Arguments - The list of arguments for a function-like macro. This can be 43 /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this 44 /// includes the \c __VA_ARGS__ identifier on the list. 45 IdentifierInfo **ArgumentList; 46 unsigned NumArguments; 47 48 /// \brief The location at which this macro was either explicitly exported 49 /// from its module or marked as private. 50 /// 51 /// If invalid, this macro has not been explicitly given any visibility. 52 SourceLocation VisibilityLocation; 53 54 /// \brief This is the list of tokens that the macro is defined to. 55 SmallVector<Token, 8> ReplacementTokens; 56 57 /// \brief Length in characters of the macro definition. 58 mutable unsigned DefinitionLength; 59 mutable bool IsDefinitionLengthCached : 1; 60 61 /// \brief True if this macro is a function-like macro, false if it 62 /// is an object-like macro. 63 bool IsFunctionLike : 1; 64 65 /// IsC99Varargs - True if this macro is of the form "#define X(...)" or 66 /// "#define X(Y,Z,...)". The __VA_ARGS__ token should be replaced with the 67 /// contents of "..." in an invocation. 68 bool IsC99Varargs : 1; 69 70 /// IsGNUVarargs - True if this macro is of the form "#define X(a...)". The 71 /// "a" identifier in the replacement list will be replaced with all arguments 72 /// of the macro starting with the specified one. 73 bool IsGNUVarargs : 1; 74 75 /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if 76 /// it has not yet been redefined or undefined. 77 bool IsBuiltinMacro : 1; 78 79 /// \brief True if this macro was loaded from an AST file. 80 bool IsFromAST : 1; 81 82 /// \brief Whether this macro changed after it was loaded from an AST file. 83 bool ChangedAfterLoad : 1; 84 85private: 86 //===--------------------------------------------------------------------===// 87 // State that changes as the macro is used. 88 89 /// IsDisabled - True if we have started an expansion of this macro already. 90 /// This disables recursive expansion, which would be quite bad for things 91 /// like \#define A A. 92 bool IsDisabled : 1; 93 94 /// IsUsed - True if this macro is either defined in the main file and has 95 /// been used, or if it is not defined in the main file. This is used to 96 /// emit -Wunused-macros diagnostics. 97 bool IsUsed : 1; 98 99 /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined 100 /// without emitting a warning. 101 bool IsAllowRedefinitionsWithoutWarning : 1; 102 103 /// \brief Must warn if the macro is unused at the end of translation unit. 104 bool IsWarnIfUnused : 1; 105 106 /// \brief Whether the macro has public (when described in a module). 107 bool IsPublic : 1; 108 109 /// \brief Whether the macro definition is currently "hidden". 110 /// Note that this is transient state that is never serialized to the AST 111 /// file. 112 bool IsHidden : 1; 113 114 /// \brief Whether the definition of this macro is ambiguous, due to 115 /// multiple definitions coming in from multiple modules. 116 bool IsAmbiguous : 1; 117 118 ~MacroInfo() { 119 assert(ArgumentList == 0 && "Didn't call destroy before dtor!"); 120 } 121 122public: 123 MacroInfo(SourceLocation DefLoc); 124 MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator); 125 126 /// FreeArgumentList - Free the argument list of the macro, restoring it to a 127 /// state where it can be reused for other devious purposes. 128 void FreeArgumentList() { 129 ArgumentList = 0; 130 NumArguments = 0; 131 } 132 133 /// Destroy - destroy this MacroInfo object. 134 void Destroy() { 135 FreeArgumentList(); 136 this->~MacroInfo(); 137 } 138 139 /// getDefinitionLoc - Return the location that the macro was defined at. 140 /// 141 SourceLocation getDefinitionLoc() const { return Location; } 142 143 /// setDefinitionEndLoc - Set the location of the last token in the macro. 144 /// 145 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; } 146 147 /// getDefinitionEndLoc - Return the location of the last token in the macro. 148 /// 149 SourceLocation getDefinitionEndLoc() const { return EndLocation; } 150 151 /// \brief Set the location where macro was undefined. Can only be set once. 152 void setUndefLoc(SourceLocation UndefLoc) { 153 assert(UndefLocation.isInvalid() && "UndefLocation is already set!"); 154 assert(UndefLoc.isValid() && "Invalid UndefLoc!"); 155 UndefLocation = UndefLoc; 156 } 157 158 /// \brief Get the location where macro was undefined. 159 SourceLocation getUndefLoc() const { return UndefLocation; } 160 161 /// \brief Set previous definition of the macro with the same name. 162 void setPreviousDefinition(MacroInfo *PreviousDef) { 163 PreviousDefinition = PreviousDef; 164 } 165 166 /// \brief Get previous definition of the macro with the same name. 167 MacroInfo *getPreviousDefinition() { return PreviousDefinition; } 168 169 /// \brief Find macro definition active in the specified source location. If 170 /// this macro was not defined there, return NULL. 171 const MacroInfo *findDefinitionAtLoc(SourceLocation L, 172 SourceManager &SM) const; 173 174 /// \brief Get length in characters of the macro definition. 175 unsigned getDefinitionLength(SourceManager &SM) const { 176 if (IsDefinitionLengthCached) 177 return DefinitionLength; 178 return getDefinitionLengthSlow(SM); 179 } 180 181 /// isIdenticalTo - Return true if the specified macro definition is equal to 182 /// this macro in spelling, arguments, and whitespace. This is used to emit 183 /// duplicate definition warnings. This implements the rules in C99 6.10.3. 184 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const; 185 186 /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag. 187 /// 188 void setIsBuiltinMacro(bool Val = true) { 189 IsBuiltinMacro = Val; 190 } 191 192 /// setIsUsed - Set the value of the IsUsed flag. 193 /// 194 void setIsUsed(bool Val) { 195 IsUsed = Val; 196 } 197 198 /// setIsAllowRedefinitionsWithoutWarning - Set the value of the 199 /// IsAllowRedefinitionsWithoutWarning flag. 200 void setIsAllowRedefinitionsWithoutWarning(bool Val) { 201 IsAllowRedefinitionsWithoutWarning = Val; 202 } 203 204 /// \brief Set the value of the IsWarnIfUnused flag. 205 void setIsWarnIfUnused(bool val) { 206 IsWarnIfUnused = val; 207 } 208 209 /// setArgumentList - Set the specified list of identifiers as the argument 210 /// list for this macro. 211 void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs, 212 llvm::BumpPtrAllocator &PPAllocator) { 213 assert(ArgumentList == 0 && NumArguments == 0 && 214 "Argument list already set!"); 215 if (NumArgs == 0) return; 216 217 NumArguments = NumArgs; 218 ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs); 219 for (unsigned i = 0; i != NumArgs; ++i) 220 ArgumentList[i] = List[i]; 221 } 222 223 /// Arguments - The list of arguments for a function-like macro. This can be 224 /// empty, for, e.g. "#define X()". 225 typedef IdentifierInfo* const *arg_iterator; 226 bool arg_empty() const { return NumArguments == 0; } 227 arg_iterator arg_begin() const { return ArgumentList; } 228 arg_iterator arg_end() const { return ArgumentList+NumArguments; } 229 unsigned getNumArgs() const { return NumArguments; } 230 231 /// getArgumentNum - Return the argument number of the specified identifier, 232 /// or -1 if the identifier is not a formal argument identifier. 233 int getArgumentNum(IdentifierInfo *Arg) const { 234 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I) 235 if (*I == Arg) return I-arg_begin(); 236 return -1; 237 } 238 239 /// Function/Object-likeness. Keep track of whether this macro has formal 240 /// parameters. 241 void setIsFunctionLike() { IsFunctionLike = true; } 242 bool isFunctionLike() const { return IsFunctionLike; } 243 bool isObjectLike() const { return !IsFunctionLike; } 244 245 /// Varargs querying methods. This can only be set for function-like macros. 246 void setIsC99Varargs() { IsC99Varargs = true; } 247 void setIsGNUVarargs() { IsGNUVarargs = true; } 248 bool isC99Varargs() const { return IsC99Varargs; } 249 bool isGNUVarargs() const { return IsGNUVarargs; } 250 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } 251 252 /// isBuiltinMacro - Return true if this macro is a builtin macro, such as 253 /// __LINE__, which requires processing before expansion. 254 bool isBuiltinMacro() const { return IsBuiltinMacro; } 255 256 /// isFromAST - Return true if this macro was loaded from an AST file. 257 bool isFromAST() const { return IsFromAST; } 258 259 /// setIsFromAST - Set whether this macro was loaded from an AST file. 260 void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; } 261 262 /// \brief Determine whether this macro has changed since it was loaded from 263 /// an AST file. 264 bool hasChangedAfterLoad() const { return ChangedAfterLoad; } 265 266 /// \brief Note whether this macro has changed after it was loaded from an 267 /// AST file. 268 void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; } 269 270 /// isUsed - Return false if this macro is defined in the main file and has 271 /// not yet been used. 272 bool isUsed() const { return IsUsed; } 273 274 /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be 275 /// redefined without warning. 276 bool isAllowRedefinitionsWithoutWarning() const { 277 return IsAllowRedefinitionsWithoutWarning; 278 } 279 280 /// \brief Return true if we should emit a warning if the macro is unused. 281 bool isWarnIfUnused() const { 282 return IsWarnIfUnused; 283 } 284 285 /// getNumTokens - Return the number of tokens that this macro expands to. 286 /// 287 unsigned getNumTokens() const { 288 return ReplacementTokens.size(); 289 } 290 291 const Token &getReplacementToken(unsigned Tok) const { 292 assert(Tok < ReplacementTokens.size() && "Invalid token #"); 293 return ReplacementTokens[Tok]; 294 } 295 296 typedef SmallVector<Token, 8>::const_iterator tokens_iterator; 297 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); } 298 tokens_iterator tokens_end() const { return ReplacementTokens.end(); } 299 bool tokens_empty() const { return ReplacementTokens.empty(); } 300 301 /// AddTokenToBody - Add the specified token to the replacement text for the 302 /// macro. 303 void AddTokenToBody(const Token &Tok) { 304 assert(!IsDefinitionLengthCached && 305 "Changing replacement tokens after definition length got calculated"); 306 ReplacementTokens.push_back(Tok); 307 } 308 309 /// isEnabled - Return true if this macro is enabled: in other words, that we 310 /// are not currently in an expansion of this macro. 311 bool isEnabled() const { return !IsDisabled; } 312 313 void EnableMacro() { 314 assert(IsDisabled && "Cannot enable an already-enabled macro!"); 315 IsDisabled = false; 316 } 317 318 void DisableMacro() { 319 assert(!IsDisabled && "Cannot disable an already-disabled macro!"); 320 IsDisabled = true; 321 } 322 323 /// \brief Set the export location for this macro. 324 void setVisibility(bool Public, SourceLocation Loc) { 325 VisibilityLocation = Loc; 326 IsPublic = Public; 327 } 328 329 /// \brief Determine whether this macro is part of the public API of its 330 /// module. 331 bool isPublic() const { return IsPublic; } 332 333 /// \brief Determine the location where this macro was explicitly made 334 /// public or private within its module. 335 SourceLocation getVisibilityLocation() { return VisibilityLocation; } 336 337 /// \brief Determine whether this macro is currently defined (and has not 338 /// been #undef'd) or has been hidden. 339 bool isDefined() const { return UndefLocation.isInvalid() && !IsHidden; } 340 341 /// \brief Determine whether this macro definition is hidden. 342 bool isHidden() const { return IsHidden; } 343 344 /// \brief Set whether this macro definition is hidden. 345 void setHidden(bool Val) { IsHidden = Val; } 346 347 /// \brief Determine whether this macro definition is ambiguous with 348 /// other macro definitions. 349 bool isAmbiguous() const { return IsAmbiguous; } 350 351 /// \brief Set whether this macro definition is ambiguous. 352 void setAmbiguous(bool Val) { IsAmbiguous = Val; } 353 354private: 355 unsigned getDefinitionLengthSlow(SourceManager &SM) const; 356}; 357 358} // end namespace clang 359 360#endif 361