clang API Documentation
00001 //===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 /// 00010 /// \file 00011 /// \brief Defines the clang::MacroInfo and clang::MacroDirective classes. 00012 /// 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CLANG_LEX_MACROINFO_H 00016 #define LLVM_CLANG_LEX_MACROINFO_H 00017 00018 #include "clang/Lex/Token.h" 00019 #include "llvm/ADT/ArrayRef.h" 00020 #include "llvm/ADT/SmallVector.h" 00021 #include "llvm/Support/Allocator.h" 00022 #include <cassert> 00023 00024 namespace clang { 00025 class Preprocessor; 00026 00027 /// \brief Encapsulates the data about a macro definition (e.g. its tokens). 00028 /// 00029 /// There's an instance of this class for every #define. 00030 class MacroInfo { 00031 //===--------------------------------------------------------------------===// 00032 // State set when the macro is defined. 00033 00034 /// \brief The location the macro is defined. 00035 SourceLocation Location; 00036 /// \brief The location of the last token in the macro. 00037 SourceLocation EndLocation; 00038 00039 /// \brief The list of arguments for a function-like macro. 00040 /// 00041 /// ArgumentList points to the first of NumArguments pointers. 00042 /// 00043 /// This can be empty, for, e.g. "#define X()". In a C99-style variadic macro, this 00044 /// includes the \c __VA_ARGS__ identifier on the list. 00045 IdentifierInfo **ArgumentList; 00046 00047 /// \see ArgumentList 00048 unsigned NumArguments; 00049 00050 /// \brief This is the list of tokens that the macro is defined to. 00051 SmallVector<Token, 8> ReplacementTokens; 00052 00053 /// \brief Length in characters of the macro definition. 00054 mutable unsigned DefinitionLength; 00055 mutable bool IsDefinitionLengthCached : 1; 00056 00057 /// \brief True if this macro is function-like, false if it is object-like. 00058 bool IsFunctionLike : 1; 00059 00060 /// \brief True if this macro is of the form "#define X(...)" or 00061 /// "#define X(Y,Z,...)". 00062 /// 00063 /// The __VA_ARGS__ token should be replaced with the contents of "..." in an 00064 /// invocation. 00065 bool IsC99Varargs : 1; 00066 00067 /// \brief True if this macro is of the form "#define X(a...)". 00068 /// 00069 /// The "a" identifier in the replacement list will be replaced with all arguments 00070 /// of the macro starting with the specified one. 00071 bool IsGNUVarargs : 1; 00072 00073 /// \brief True if this macro requires processing before expansion. 00074 /// 00075 /// This is the case for builtin macros such as __LINE__, so long as they have 00076 /// not been redefined, but not for regular predefined macros from the "<built-in>" 00077 /// memory buffer (see Preprocessing::getPredefinesFileID). 00078 bool IsBuiltinMacro : 1; 00079 00080 /// \brief Whether this macro contains the sequence ", ## __VA_ARGS__" 00081 bool HasCommaPasting : 1; 00082 00083 //===--------------------------------------------------------------------===// 00084 // State that changes as the macro is used. 00085 00086 /// \brief True if we have started an expansion of this macro already. 00087 /// 00088 /// This disables recursive expansion, which would be quite bad for things 00089 /// like \#define A A. 00090 bool IsDisabled : 1; 00091 00092 /// \brief True if this macro is either defined in the main file and has 00093 /// been used, or if it is not defined in the main file. 00094 /// 00095 /// This is used to emit -Wunused-macros diagnostics. 00096 bool IsUsed : 1; 00097 00098 /// \brief True if this macro can be redefined without emitting a warning. 00099 bool IsAllowRedefinitionsWithoutWarning : 1; 00100 00101 /// \brief Must warn if the macro is unused at the end of translation unit. 00102 bool IsWarnIfUnused : 1; 00103 00104 /// \brief Whether this macro info was loaded from an AST file. 00105 unsigned FromASTFile : 1; 00106 00107 /// \brief Whether this macro was used as header guard. 00108 bool UsedForHeaderGuard : 1; 00109 00110 // Only the Preprocessor gets to create and destroy these. 00111 MacroInfo(SourceLocation DefLoc); 00112 ~MacroInfo() {} 00113 00114 public: 00115 /// \brief Return the location that the macro was defined at. 00116 SourceLocation getDefinitionLoc() const { return Location; } 00117 00118 /// \brief Set the location of the last token in the macro. 00119 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; } 00120 00121 /// \brief Return the location of the last token in the macro. 00122 SourceLocation getDefinitionEndLoc() const { return EndLocation; } 00123 00124 /// \brief Get length in characters of the macro definition. 00125 unsigned getDefinitionLength(SourceManager &SM) const { 00126 if (IsDefinitionLengthCached) 00127 return DefinitionLength; 00128 return getDefinitionLengthSlow(SM); 00129 } 00130 00131 /// \brief Return true if the specified macro definition is equal to 00132 /// this macro in spelling, arguments, and whitespace. 00133 /// 00134 /// \param Syntactically if true, the macro definitions can be identical even 00135 /// if they use different identifiers for the function macro parameters. 00136 /// Otherwise the comparison is lexical and this implements the rules in 00137 /// C99 6.10.3. 00138 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP, 00139 bool Syntactically) const; 00140 00141 /// \brief Set or clear the isBuiltinMacro flag. 00142 void setIsBuiltinMacro(bool Val = true) { 00143 IsBuiltinMacro = Val; 00144 } 00145 00146 /// \brief Set the value of the IsUsed flag. 00147 void setIsUsed(bool Val) { 00148 IsUsed = Val; 00149 } 00150 00151 /// \brief Set the value of the IsAllowRedefinitionsWithoutWarning flag. 00152 void setIsAllowRedefinitionsWithoutWarning(bool Val) { 00153 IsAllowRedefinitionsWithoutWarning = Val; 00154 } 00155 00156 /// \brief Set the value of the IsWarnIfUnused flag. 00157 void setIsWarnIfUnused(bool val) { 00158 IsWarnIfUnused = val; 00159 } 00160 00161 /// \brief Set the specified list of identifiers as the argument list for 00162 /// this macro. 00163 void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs, 00164 llvm::BumpPtrAllocator &PPAllocator) { 00165 assert(ArgumentList == nullptr && NumArguments == 0 && 00166 "Argument list already set!"); 00167 if (NumArgs == 0) return; 00168 00169 NumArguments = NumArgs; 00170 ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs); 00171 for (unsigned i = 0; i != NumArgs; ++i) 00172 ArgumentList[i] = List[i]; 00173 } 00174 00175 /// Arguments - The list of arguments for a function-like macro. This can be 00176 /// empty, for, e.g. "#define X()". 00177 typedef IdentifierInfo* const *arg_iterator; 00178 bool arg_empty() const { return NumArguments == 0; } 00179 arg_iterator arg_begin() const { return ArgumentList; } 00180 arg_iterator arg_end() const { return ArgumentList+NumArguments; } 00181 unsigned getNumArgs() const { return NumArguments; } 00182 00183 /// \brief Return the argument number of the specified identifier, 00184 /// or -1 if the identifier is not a formal argument identifier. 00185 int getArgumentNum(IdentifierInfo *Arg) const { 00186 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I) 00187 if (*I == Arg) return I-arg_begin(); 00188 return -1; 00189 } 00190 00191 /// Function/Object-likeness. Keep track of whether this macro has formal 00192 /// parameters. 00193 void setIsFunctionLike() { IsFunctionLike = true; } 00194 bool isFunctionLike() const { return IsFunctionLike; } 00195 bool isObjectLike() const { return !IsFunctionLike; } 00196 00197 /// Varargs querying methods. This can only be set for function-like macros. 00198 void setIsC99Varargs() { IsC99Varargs = true; } 00199 void setIsGNUVarargs() { IsGNUVarargs = true; } 00200 bool isC99Varargs() const { return IsC99Varargs; } 00201 bool isGNUVarargs() const { return IsGNUVarargs; } 00202 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } 00203 00204 /// \brief Return true if this macro requires processing before expansion. 00205 /// 00206 /// This is true only for builtin macro, such as \__LINE__, whose values 00207 /// are not given by fixed textual expansions. Regular predefined macros 00208 /// from the "<built-in>" buffer are not reported as builtins by this 00209 /// function. 00210 bool isBuiltinMacro() const { return IsBuiltinMacro; } 00211 00212 bool hasCommaPasting() const { return HasCommaPasting; } 00213 void setHasCommaPasting() { HasCommaPasting = true; } 00214 00215 /// \brief Return false if this macro is defined in the main file and has 00216 /// not yet been used. 00217 bool isUsed() const { return IsUsed; } 00218 00219 /// \brief Return true if this macro can be redefined without warning. 00220 bool isAllowRedefinitionsWithoutWarning() const { 00221 return IsAllowRedefinitionsWithoutWarning; 00222 } 00223 00224 /// \brief Return true if we should emit a warning if the macro is unused. 00225 bool isWarnIfUnused() const { 00226 return IsWarnIfUnused; 00227 } 00228 00229 /// \brief Return the number of tokens that this macro expands to. 00230 /// 00231 unsigned getNumTokens() const { 00232 return ReplacementTokens.size(); 00233 } 00234 00235 const Token &getReplacementToken(unsigned Tok) const { 00236 assert(Tok < ReplacementTokens.size() && "Invalid token #"); 00237 return ReplacementTokens[Tok]; 00238 } 00239 00240 typedef SmallVectorImpl<Token>::const_iterator tokens_iterator; 00241 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); } 00242 tokens_iterator tokens_end() const { return ReplacementTokens.end(); } 00243 bool tokens_empty() const { return ReplacementTokens.empty(); } 00244 00245 /// \brief Add the specified token to the replacement text for the macro. 00246 void AddTokenToBody(const Token &Tok) { 00247 assert(!IsDefinitionLengthCached && 00248 "Changing replacement tokens after definition length got calculated"); 00249 ReplacementTokens.push_back(Tok); 00250 } 00251 00252 /// \brief Return true if this macro is enabled. 00253 /// 00254 /// In other words, that we are not currently in an expansion of this macro. 00255 bool isEnabled() const { return !IsDisabled; } 00256 00257 void EnableMacro() { 00258 assert(IsDisabled && "Cannot enable an already-enabled macro!"); 00259 IsDisabled = false; 00260 } 00261 00262 void DisableMacro() { 00263 assert(!IsDisabled && "Cannot disable an already-disabled macro!"); 00264 IsDisabled = true; 00265 } 00266 00267 /// \brief Determine whether this macro info came from an AST file (such as 00268 /// a precompiled header or module) rather than having been parsed. 00269 bool isFromASTFile() const { return FromASTFile; } 00270 00271 /// \brief Determine whether this macro was used for a header guard. 00272 bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; } 00273 00274 void setUsedForHeaderGuard(bool Val) { UsedForHeaderGuard = Val; } 00275 00276 /// \brief Retrieve the global ID of the module that owns this particular 00277 /// macro info. 00278 unsigned getOwningModuleID() const { 00279 if (isFromASTFile()) 00280 return *(const unsigned*)(this+1); 00281 00282 return 0; 00283 } 00284 00285 void dump() const; 00286 00287 private: 00288 unsigned getDefinitionLengthSlow(SourceManager &SM) const; 00289 00290 void setOwningModuleID(unsigned ID) { 00291 assert(isFromASTFile()); 00292 *(unsigned*)(this+1) = ID; 00293 } 00294 00295 friend class Preprocessor; 00296 }; 00297 00298 class DefMacroDirective; 00299 00300 /// \brief Encapsulates changes to the "macros namespace" (the location where 00301 /// the macro name became active, the location where it was undefined, etc.). 00302 /// 00303 /// MacroDirectives, associated with an identifier, are used to model the macro 00304 /// history. Usually a macro definition (MacroInfo) is where a macro name 00305 /// becomes active (MacroDirective) but modules can have their own macro 00306 /// history, separate from the local (current translation unit) macro history. 00307 /// 00308 /// For example, if "@import A;" imports macro FOO, there will be a new local 00309 /// MacroDirective created to indicate that "FOO" became active at the import 00310 /// location. Module "A" itself will contain another MacroDirective in its macro 00311 /// history (at the point of the definition of FOO) and both MacroDirectives 00312 /// will point to the same MacroInfo object. 00313 /// 00314 class MacroDirective { 00315 public: 00316 enum Kind { 00317 MD_Define, 00318 MD_Undefine, 00319 MD_Visibility 00320 }; 00321 00322 protected: 00323 /// \brief Previous macro directive for the same identifier, or NULL. 00324 MacroDirective *Previous; 00325 00326 SourceLocation Loc; 00327 00328 /// \brief MacroDirective kind. 00329 unsigned MDKind : 2; 00330 00331 /// \brief True if the macro directive was loaded from a PCH file. 00332 bool IsFromPCH : 1; 00333 00334 // Used by DefMacroDirective -----------------------------------------------// 00335 00336 /// \brief Whether the definition of this macro is ambiguous, due to 00337 /// multiple definitions coming in from multiple modules. 00338 bool IsAmbiguous : 1; 00339 00340 // Used by VisibilityMacroDirective ----------------------------------------// 00341 00342 /// \brief Whether the macro has public visibility (when described in a 00343 /// module). 00344 bool IsPublic : 1; 00345 00346 // Used by DefMacroDirective and UndefMacroDirective -----------------------// 00347 00348 /// \brief True if this macro was imported from a module. 00349 bool IsImported : 1; 00350 00351 /// \brief For an imported directive, the number of modules whose macros are 00352 /// overridden by this directive. Only used if IsImported. 00353 unsigned NumOverrides : 26; 00354 00355 unsigned *getModuleDataStart(); 00356 const unsigned *getModuleDataStart() const { 00357 return const_cast<MacroDirective*>(this)->getModuleDataStart(); 00358 } 00359 00360 MacroDirective(Kind K, SourceLocation Loc, 00361 unsigned ImportedFromModuleID = 0, 00362 ArrayRef<unsigned> Overrides = None) 00363 : Previous(nullptr), Loc(Loc), MDKind(K), IsFromPCH(false), 00364 IsAmbiguous(false), IsPublic(true), IsImported(ImportedFromModuleID), 00365 NumOverrides(Overrides.size()) { 00366 assert(NumOverrides == Overrides.size() && "too many overrides"); 00367 assert((IsImported || !NumOverrides) && "overrides for non-module macro"); 00368 00369 if (IsImported) { 00370 unsigned *Extra = getModuleDataStart(); 00371 *Extra++ = ImportedFromModuleID; 00372 std::copy(Overrides.begin(), Overrides.end(), Extra); 00373 } 00374 } 00375 00376 public: 00377 Kind getKind() const { return Kind(MDKind); } 00378 00379 SourceLocation getLocation() const { return Loc; } 00380 00381 /// \brief Set previous definition of the macro with the same name. 00382 void setPrevious(MacroDirective *Prev) { 00383 Previous = Prev; 00384 } 00385 00386 /// \brief Get previous definition of the macro with the same name. 00387 const MacroDirective *getPrevious() const { return Previous; } 00388 00389 /// \brief Get previous definition of the macro with the same name. 00390 MacroDirective *getPrevious() { return Previous; } 00391 00392 /// \brief Return true if the macro directive was loaded from a PCH file. 00393 bool isFromPCH() const { return IsFromPCH; } 00394 00395 void setIsFromPCH() { IsFromPCH = true; } 00396 00397 /// \brief True if this macro was imported from a module. 00398 /// Note that this is never the case for a VisibilityMacroDirective. 00399 bool isImported() const { return IsImported; } 00400 00401 /// \brief If this directive was imported from a module, get the submodule 00402 /// whose directive this is. Note that this may be different from the module 00403 /// that owns the MacroInfo for a DefMacroDirective due to #pragma pop_macro 00404 /// and similar effects. 00405 unsigned getOwningModuleID() const { 00406 if (isImported()) 00407 return *getModuleDataStart(); 00408 return 0; 00409 } 00410 00411 /// \brief Get the module IDs of modules whose macros are overridden by this 00412 /// directive. Only valid if this is an imported directive. 00413 ArrayRef<unsigned> getOverriddenModules() const { 00414 assert(IsImported && "can only get overridden modules for imported macro"); 00415 return llvm::makeArrayRef(getModuleDataStart() + 1, NumOverrides); 00416 } 00417 00418 class DefInfo { 00419 DefMacroDirective *DefDirective; 00420 SourceLocation UndefLoc; 00421 bool IsPublic; 00422 00423 public: 00424 DefInfo() : DefDirective(nullptr) { } 00425 00426 DefInfo(DefMacroDirective *DefDirective, SourceLocation UndefLoc, 00427 bool isPublic) 00428 : DefDirective(DefDirective), UndefLoc(UndefLoc), IsPublic(isPublic) { } 00429 00430 const DefMacroDirective *getDirective() const { return DefDirective; } 00431 DefMacroDirective *getDirective() { return DefDirective; } 00432 00433 inline SourceLocation getLocation() const; 00434 inline MacroInfo *getMacroInfo(); 00435 const MacroInfo *getMacroInfo() const { 00436 return const_cast<DefInfo*>(this)->getMacroInfo(); 00437 } 00438 00439 SourceLocation getUndefLocation() const { return UndefLoc; } 00440 bool isUndefined() const { return UndefLoc.isValid(); } 00441 00442 bool isPublic() const { return IsPublic; } 00443 00444 bool isValid() const { return DefDirective != nullptr; } 00445 bool isInvalid() const { return !isValid(); } 00446 00447 LLVM_EXPLICIT operator bool() const { return isValid(); } 00448 00449 inline DefInfo getPreviousDefinition(); 00450 const DefInfo getPreviousDefinition() const { 00451 return const_cast<DefInfo*>(this)->getPreviousDefinition(); 00452 } 00453 }; 00454 00455 /// \brief Traverses the macro directives history and returns the next 00456 /// macro definition directive along with info about its undefined location 00457 /// (if there is one) and if it is public or private. 00458 DefInfo getDefinition(); 00459 const DefInfo getDefinition() const { 00460 return const_cast<MacroDirective*>(this)->getDefinition(); 00461 } 00462 00463 bool isDefined() const { 00464 if (const DefInfo Def = getDefinition()) 00465 return !Def.isUndefined(); 00466 return false; 00467 } 00468 00469 const MacroInfo *getMacroInfo() const { 00470 return getDefinition().getMacroInfo(); 00471 } 00472 MacroInfo *getMacroInfo() { 00473 return getDefinition().getMacroInfo(); 00474 } 00475 00476 /// \brief Find macro definition active in the specified source location. If 00477 /// this macro was not defined there, return NULL. 00478 const DefInfo findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const; 00479 00480 void dump() const; 00481 00482 static bool classof(const MacroDirective *) { return true; } 00483 }; 00484 00485 /// \brief A directive for a defined macro or a macro imported from a module. 00486 class DefMacroDirective : public MacroDirective { 00487 MacroInfo *Info; 00488 00489 public: 00490 explicit DefMacroDirective(MacroInfo *MI) 00491 : MacroDirective(MD_Define, MI->getDefinitionLoc()), Info(MI) { 00492 assert(MI && "MacroInfo is null"); 00493 } 00494 00495 DefMacroDirective(MacroInfo *MI, SourceLocation Loc, 00496 unsigned ImportedFromModuleID = 0, 00497 ArrayRef<unsigned> Overrides = None) 00498 : MacroDirective(MD_Define, Loc, ImportedFromModuleID, Overrides), 00499 Info(MI) { 00500 assert(MI && "MacroInfo is null"); 00501 } 00502 00503 /// \brief The data for the macro definition. 00504 const MacroInfo *getInfo() const { return Info; } 00505 MacroInfo *getInfo() { return Info; } 00506 00507 /// \brief Determine whether this macro definition is ambiguous with 00508 /// other macro definitions. 00509 bool isAmbiguous() const { return IsAmbiguous; } 00510 00511 /// \brief Set whether this macro definition is ambiguous. 00512 void setAmbiguous(bool Val) { IsAmbiguous = Val; } 00513 00514 static bool classof(const MacroDirective *MD) { 00515 return MD->getKind() == MD_Define; 00516 } 00517 static bool classof(const DefMacroDirective *) { return true; } 00518 }; 00519 00520 /// \brief A directive for an undefined macro. 00521 class UndefMacroDirective : public MacroDirective { 00522 public: 00523 explicit UndefMacroDirective(SourceLocation UndefLoc, 00524 unsigned ImportedFromModuleID = 0, 00525 ArrayRef<unsigned> Overrides = None) 00526 : MacroDirective(MD_Undefine, UndefLoc, ImportedFromModuleID, Overrides) { 00527 assert((UndefLoc.isValid() || ImportedFromModuleID) && "Invalid UndefLoc!"); 00528 } 00529 00530 static bool classof(const MacroDirective *MD) { 00531 return MD->getKind() == MD_Undefine; 00532 } 00533 static bool classof(const UndefMacroDirective *) { return true; } 00534 }; 00535 00536 /// \brief A directive for setting the module visibility of a macro. 00537 class VisibilityMacroDirective : public MacroDirective { 00538 public: 00539 explicit VisibilityMacroDirective(SourceLocation Loc, bool Public) 00540 : MacroDirective(MD_Visibility, Loc) { 00541 IsPublic = Public; 00542 } 00543 00544 /// \brief Determine whether this macro is part of the public API of its 00545 /// module. 00546 bool isPublic() const { return IsPublic; } 00547 00548 static bool classof(const MacroDirective *MD) { 00549 return MD->getKind() == MD_Visibility; 00550 } 00551 static bool classof(const VisibilityMacroDirective *) { return true; } 00552 }; 00553 00554 inline unsigned *MacroDirective::getModuleDataStart() { 00555 if (auto *Def = dyn_cast<DefMacroDirective>(this)) 00556 return reinterpret_cast<unsigned*>(Def + 1); 00557 else 00558 return reinterpret_cast<unsigned*>(cast<UndefMacroDirective>(this) + 1); 00559 } 00560 00561 inline SourceLocation MacroDirective::DefInfo::getLocation() const { 00562 if (isInvalid()) 00563 return SourceLocation(); 00564 return DefDirective->getLocation(); 00565 } 00566 00567 inline MacroInfo *MacroDirective::DefInfo::getMacroInfo() { 00568 if (isInvalid()) 00569 return nullptr; 00570 return DefDirective->getInfo(); 00571 } 00572 00573 inline MacroDirective::DefInfo 00574 MacroDirective::DefInfo::getPreviousDefinition() { 00575 if (isInvalid() || DefDirective->getPrevious() == nullptr) 00576 return DefInfo(); 00577 return DefDirective->getPrevious()->getDefinition(); 00578 } 00579 00580 } // end namespace clang 00581 00582 #endif