MacroInfo.h revision 81ba9d11609132d1d804f2b78d321daf97c081f5
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/// \file 11/// \brief Defines the clang::MacroInfo and clang::MacroDirective classes. 12/// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_MACROINFO_H 16#define LLVM_CLANG_MACROINFO_H 17 18#include "clang/Lex/Token.h" 19#include "llvm/ADT/SmallVector.h" 20#include "llvm/Support/Allocator.h" 21#include <cassert> 22 23namespace clang { 24 class Preprocessor; 25 26/// \brief Encapsulates the data about a macro definition (e.g. its tokens). 27/// 28/// There's an instance of this class for every #define. 29class MacroInfo { 30 //===--------------------------------------------------------------------===// 31 // State set when the macro is defined. 32 33 /// \brief The location the macro is defined. 34 SourceLocation Location; 35 /// \brief The location of the last token in the macro. 36 SourceLocation EndLocation; 37 38 /// \brief The list of arguments for a function-like macro. 39 /// 40 /// ArgumentList points to the first of NumArguments pointers. 41 /// 42 /// This can be empty, for, e.g. "#define X()". In a C99-style variadic macro, this 43 /// includes the \c __VA_ARGS__ identifier on the list. 44 IdentifierInfo **ArgumentList; 45 46 /// \see ArgumentList 47 unsigned NumArguments; 48 49 /// \brief This is the list of tokens that the macro is defined to. 50 SmallVector<Token, 8> ReplacementTokens; 51 52 /// \brief Length in characters of the macro definition. 53 mutable unsigned DefinitionLength; 54 mutable bool IsDefinitionLengthCached : 1; 55 56 /// \brief True if this macro is function-like, false if it is object-like. 57 bool IsFunctionLike : 1; 58 59 /// \brief True if this macro is of the form "#define X(...)" or 60 /// "#define X(Y,Z,...)". 61 /// 62 /// The __VA_ARGS__ token should be replaced with the contents of "..." in an 63 /// invocation. 64 bool IsC99Varargs : 1; 65 66 /// \brief True if this macro is of the form "#define X(a...)". 67 /// 68 /// The "a" identifier in the replacement list will be replaced with all arguments 69 /// of the macro starting with the specified one. 70 bool IsGNUVarargs : 1; 71 72 /// \brief True if this macro requires processing before expansion. 73 /// 74 /// This is the case for builtin macros such as __LINE__, so long as they have 75 /// not been redefined, but not for regular predefined macros from the "<built-in>" 76 /// memory buffer (see Preprocessing::getPredefinesFileID). 77 bool IsBuiltinMacro : 1; 78 79 /// \brief Whether this macro contains the sequence ", ## __VA_ARGS__" 80 bool HasCommaPasting : 1; 81 82private: 83 //===--------------------------------------------------------------------===// 84 // State that changes as the macro is used. 85 86 /// \brief True if we have started an expansion of this macro already. 87 /// 88 /// This disables recursive expansion, which would be quite bad for things 89 /// like \#define A A. 90 bool IsDisabled : 1; 91 92 /// \brief True if this macro is either defined in the main file and has 93 /// been used, or if it is not defined in the main file. 94 /// 95 /// This is used to emit -Wunused-macros diagnostics. 96 bool IsUsed : 1; 97 98 /// \brief True if this macro can be redefined without emitting a warning. 99 bool IsAllowRedefinitionsWithoutWarning : 1; 100 101 /// \brief Must warn if the macro is unused at the end of translation unit. 102 bool IsWarnIfUnused : 1; 103 104 ~MacroInfo() { 105 assert(ArgumentList == 0 && "Didn't call destroy before dtor!"); 106 } 107 108public: 109 MacroInfo(SourceLocation DefLoc); 110 111 /// \brief Free the argument list of the macro. 112 /// 113 /// This restores this MacroInfo to a state where it can be reused for other 114 /// devious purposes. 115 void FreeArgumentList() { 116 ArgumentList = 0; 117 NumArguments = 0; 118 } 119 120 /// \brief Destroy this MacroInfo object. 121 void Destroy() { 122 FreeArgumentList(); 123 this->~MacroInfo(); 124 } 125 126 /// \brief Return the location that the macro was defined at. 127 SourceLocation getDefinitionLoc() const { return Location; } 128 129 /// \brief Set the location of the last token in the macro. 130 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; } 131 132 /// \brief Return the location of the last token in the macro. 133 SourceLocation getDefinitionEndLoc() const { return EndLocation; } 134 135 /// \brief Get length in characters of the macro definition. 136 unsigned getDefinitionLength(SourceManager &SM) const { 137 if (IsDefinitionLengthCached) 138 return DefinitionLength; 139 return getDefinitionLengthSlow(SM); 140 } 141 142 /// \brief Return true if the specified macro definition is equal to 143 /// this macro in spelling, arguments, and whitespace. 144 /// 145 /// This is used to emit duplicate definition warnings. This implements the rules 146 /// in C99 6.10.3. 147 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const; 148 149 /// \brief Set or clear the isBuiltinMacro flag. 150 void setIsBuiltinMacro(bool Val = true) { 151 IsBuiltinMacro = Val; 152 } 153 154 /// \brief Set the value of the IsUsed flag. 155 void setIsUsed(bool Val) { 156 IsUsed = Val; 157 } 158 159 /// \brief Set the value of the IsAllowRedefinitionsWithoutWarning flag. 160 void setIsAllowRedefinitionsWithoutWarning(bool Val) { 161 IsAllowRedefinitionsWithoutWarning = Val; 162 } 163 164 /// \brief Set the value of the IsWarnIfUnused flag. 165 void setIsWarnIfUnused(bool val) { 166 IsWarnIfUnused = val; 167 } 168 169 /// \brief Set the specified list of identifiers as the argument list for 170 /// this macro. 171 void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs, 172 llvm::BumpPtrAllocator &PPAllocator) { 173 assert(ArgumentList == 0 && NumArguments == 0 && 174 "Argument list already set!"); 175 if (NumArgs == 0) return; 176 177 NumArguments = NumArgs; 178 ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs); 179 for (unsigned i = 0; i != NumArgs; ++i) 180 ArgumentList[i] = List[i]; 181 } 182 183 /// Arguments - The list of arguments for a function-like macro. This can be 184 /// empty, for, e.g. "#define X()". 185 typedef IdentifierInfo* const *arg_iterator; 186 bool arg_empty() const { return NumArguments == 0; } 187 arg_iterator arg_begin() const { return ArgumentList; } 188 arg_iterator arg_end() const { return ArgumentList+NumArguments; } 189 unsigned getNumArgs() const { return NumArguments; } 190 191 /// \brief Return the argument number of the specified identifier, 192 /// or -1 if the identifier is not a formal argument identifier. 193 int getArgumentNum(IdentifierInfo *Arg) const { 194 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I) 195 if (*I == Arg) return I-arg_begin(); 196 return -1; 197 } 198 199 /// Function/Object-likeness. Keep track of whether this macro has formal 200 /// parameters. 201 void setIsFunctionLike() { IsFunctionLike = true; } 202 bool isFunctionLike() const { return IsFunctionLike; } 203 bool isObjectLike() const { return !IsFunctionLike; } 204 205 /// Varargs querying methods. This can only be set for function-like macros. 206 void setIsC99Varargs() { IsC99Varargs = true; } 207 void setIsGNUVarargs() { IsGNUVarargs = true; } 208 bool isC99Varargs() const { return IsC99Varargs; } 209 bool isGNUVarargs() const { return IsGNUVarargs; } 210 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } 211 212 /// \brief Return true if this macro requires processing before expansion. 213 /// 214 /// This is true only for builtin macro, such as \__LINE__, whose values 215 /// are not given by fixed textual expansions. Regular predefined macros 216 /// from the "<built-in>" buffer are not reported as builtins by this 217 /// function. 218 bool isBuiltinMacro() const { return IsBuiltinMacro; } 219 220 bool hasCommaPasting() const { return HasCommaPasting; } 221 void setHasCommaPasting() { HasCommaPasting = true; } 222 223 /// \brief Return false if this macro is defined in the main file and has 224 /// not yet been used. 225 bool isUsed() const { return IsUsed; } 226 227 /// \brief Return true if this macro can be redefined without warning. 228 bool isAllowRedefinitionsWithoutWarning() const { 229 return IsAllowRedefinitionsWithoutWarning; 230 } 231 232 /// \brief Return true if we should emit a warning if the macro is unused. 233 bool isWarnIfUnused() const { 234 return IsWarnIfUnused; 235 } 236 237 /// \brief Return the number of tokens that this macro expands to. 238 /// 239 unsigned getNumTokens() const { 240 return ReplacementTokens.size(); 241 } 242 243 const Token &getReplacementToken(unsigned Tok) const { 244 assert(Tok < ReplacementTokens.size() && "Invalid token #"); 245 return ReplacementTokens[Tok]; 246 } 247 248 typedef SmallVector<Token, 8>::const_iterator tokens_iterator; 249 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); } 250 tokens_iterator tokens_end() const { return ReplacementTokens.end(); } 251 bool tokens_empty() const { return ReplacementTokens.empty(); } 252 253 /// \brief Add the specified token to the replacement text for the macro. 254 void AddTokenToBody(const Token &Tok) { 255 assert(!IsDefinitionLengthCached && 256 "Changing replacement tokens after definition length got calculated"); 257 ReplacementTokens.push_back(Tok); 258 } 259 260 /// \brief Return true if this macro is enabled. 261 /// 262 /// In other words, that we are not currently in an expansion of this macro. 263 bool isEnabled() const { return !IsDisabled; } 264 265 void EnableMacro() { 266 assert(IsDisabled && "Cannot enable an already-enabled macro!"); 267 IsDisabled = false; 268 } 269 270 void DisableMacro() { 271 assert(!IsDisabled && "Cannot disable an already-disabled macro!"); 272 IsDisabled = true; 273 } 274 275private: 276 unsigned getDefinitionLengthSlow(SourceManager &SM) const; 277}; 278 279/// \brief Encapsulates changes to the "macros namespace" (the location where 280/// the macro name became active, the location where it was undefined, etc.). 281/// 282/// MacroDirectives, associated with an identifier, are used to model the macro 283/// history. Usually a macro definition (MacroInfo) is where a macro name 284/// becomes active (MacroDirective) but modules can have their own macro 285/// history, separate from the local (current translation unit) macro history. 286/// 287/// For example, if "@import A;" imports macro FOO, there will be a new local 288/// MacroDirective created to indicate that "FOO" became active at the import 289/// location. Module "A" itself will contain another MacroDirective in its macro 290/// history (at the point of the definition of FOO) and both MacroDirectives 291/// will point to the same MacroInfo object. 292/// 293class MacroDirective { 294 MacroInfo *Info; 295 296 /// \brief Previous definition, the identifier of this macro was defined to, 297 /// or NULL. 298 MacroDirective *Previous; 299 300 SourceLocation Loc; 301 302 /// \brief The location where the macro was #undef'd, or an invalid location 303 /// for macros that haven't been undefined. 304 SourceLocation UndefLocation; 305 306 /// \brief The location at which this macro was either explicitly exported 307 /// from its module or marked as private. 308 /// 309 /// If invalid, this macro has not been explicitly given any visibility. 310 SourceLocation VisibilityLocation; 311 312 /// \brief True if this macro was loaded from an AST file. 313 bool IsImported : 1; 314 315 /// \brief Whether the macro has public (when described in a module). 316 bool IsPublic : 1; 317 318 /// \brief Whether the macro definition is currently "hidden". 319 /// 320 /// Note that this is transient state that is never serialized to the AST 321 /// file. 322 bool IsHidden : 1; 323 324 /// \brief Whether the definition of this macro is ambiguous, due to 325 /// multiple definitions coming in from multiple modules. 326 bool IsAmbiguous : 1; 327 328 /// \brief Whether this macro changed after it was loaded from an AST file. 329 bool ChangedAfterLoad : 1; 330 331public: 332 explicit MacroDirective(MacroInfo *MI) 333 : Info(MI), Previous(0), Loc(MI->getDefinitionLoc()), 334 IsImported(false), IsPublic(true), IsHidden(false), IsAmbiguous(false), 335 ChangedAfterLoad(false) { 336 assert(MI && "MacroInfo is null"); 337 } 338 339 MacroDirective(MacroInfo *MI, SourceLocation Loc, bool isImported) 340 : Info(MI), Previous(0), Loc(Loc), 341 IsImported(isImported), IsPublic(true), IsHidden(false), 342 IsAmbiguous(false), ChangedAfterLoad(false) { 343 assert(MI && "MacroInfo is null"); 344 } 345 346 SourceLocation getLocation() const { return Loc; } 347 348 /// \brief Set the location where macro was undefined. Can only be set once. 349 void setUndefLoc(SourceLocation UndefLoc) { 350 assert(UndefLocation.isInvalid() && "UndefLocation is already set!"); 351 assert(UndefLoc.isValid() && "Invalid UndefLoc!"); 352 UndefLocation = UndefLoc; 353 } 354 355 /// \brief The data for the macro definition. 356 const MacroInfo *getInfo() const { return Info; } 357 MacroInfo *getInfo() { return Info; } 358 359 /// \brief Get the location where macro was undefined. 360 SourceLocation getUndefLoc() const { return UndefLocation; } 361 362 /// \brief Set previous definition of the macro with the same name. 363 void setPrevious(MacroDirective *Prev) { 364 Previous = Prev; 365 } 366 367 /// \brief Get previous definition of the macro with the same name. 368 const MacroDirective *getPrevious() const { return Previous; } 369 370 /// \brief Get previous definition of the macro with the same name. 371 MacroDirective *getPrevious() { return Previous; } 372 373 /// \brief Find macro definition active in the specified source location. If 374 /// this macro was not defined there, return NULL. 375 const MacroDirective *findDirectiveAtLoc(SourceLocation L, 376 SourceManager &SM) const; 377 378 /// \brief Set the export location for this macro. 379 void setVisibility(bool Public, SourceLocation Loc) { 380 VisibilityLocation = Loc; 381 IsPublic = Public; 382 } 383 384 /// \brief Determine whether this macro is part of the public API of its 385 /// module. 386 bool isPublic() const { return IsPublic; } 387 388 /// \brief Determine the location where this macro was explicitly made 389 /// public or private within its module. 390 SourceLocation getVisibilityLocation() const { return VisibilityLocation; } 391 392 /// \brief True if this macro was loaded from an AST file. 393 bool isImported() const { return IsImported; } 394 395 /// \brief Determine whether this macro is currently defined (and has not 396 /// been #undef'd) or has been hidden. 397 bool isDefined() const { return UndefLocation.isInvalid() && !IsHidden; } 398 399 /// \brief Determine whether this macro definition is hidden. 400 bool isHidden() const { return IsHidden; } 401 402 /// \brief Set whether this macro definition is hidden. 403 void setHidden(bool Val) { IsHidden = Val; } 404 405 /// \brief Determine whether this macro definition is ambiguous with 406 /// other macro definitions. 407 bool isAmbiguous() const { return IsAmbiguous; } 408 409 /// \brief Set whether this macro definition is ambiguous. 410 void setAmbiguous(bool Val) { IsAmbiguous = Val; } 411 412 /// \brief Determine whether this macro has changed since it was loaded from 413 /// an AST file. 414 bool hasChangedAfterLoad() const { return ChangedAfterLoad; } 415 416 /// \brief Note whether this macro has changed after it was loaded from an 417 /// AST file. 418 void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; } 419}; 420 421} // end namespace clang 422 423#endif 424