15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file defines the MacroInfo interface. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_MACROINFO_H 155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_MACROINFO_H 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner#include "clang/Lex/Token.h" 18f46f68b5587b6933a92260220567ea7c36924a80Chris Lattner#include "llvm/ADT/SmallVector.h" 19685befeb5f6472585bae473a6389e47cab9eac67Chris Lattner#include "llvm/Support/Allocator.h" 209dc62f044a6ba21f503bd56607d94b32704e7945Chris Lattner#include <cassert> 215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang { 235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer class Preprocessor; 241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2563333619fa68c8d1d8219f6d7f2d3c36f4356346James Dennett/// MacroInfo - Each identifier that is \#define'd has an instance of this class 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// associated with it, used to implement macro expansion. 275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass MacroInfo { 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer //===--------------------------------------------------------------------===// 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // State set when the macro is defined. 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// Location - This is the place the macro is defined. 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation Location; 332451b528fe114595d0f10ef2c05047928558ab0fChris Lattner /// EndLocation - The location of the last token in the macro. 342451b528fe114595d0f10ef2c05047928558ab0fChris Lattner SourceLocation EndLocation; 358a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko /// \brief The location where the macro was #undef'd, or an invalid location 368a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko /// for macros that haven't been undefined. 378a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko SourceLocation UndefLocation; 388a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko /// \brief Previous definition, the identifier of this macro was defined to, 398a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko /// or NULL. 408a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko MacroInfo *PreviousDefinition; 415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// Arguments - The list of arguments for a function-like macro. This can be 435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this 4463333619fa68c8d1d8219f6d7f2d3c36f4356346James Dennett /// includes the \c __VA_ARGS__ identifier on the list. 4525c9648909193d380a4e135d2e3d25394ba12922Chris Lattner IdentifierInfo **ArgumentList; 4625c9648909193d380a4e135d2e3d25394ba12922Chris Lattner unsigned NumArguments; 471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 48aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor /// \brief The location at which this macro was either explicitly exported 49aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor /// from its module or marked as private. 507143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor /// 51aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor /// If invalid, this macro has not been explicitly given any visibility. 52aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor SourceLocation VisibilityLocation; 537143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor 5463333619fa68c8d1d8219f6d7f2d3c36f4356346James Dennett /// \brief This is the list of tokens that the macro is defined to. 55686775deca8b8685eb90801495880e3abdd844c2Chris Lattner SmallVector<Token, 8> ReplacementTokens; 565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 57b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// \brief Length in characters of the macro definition. 58b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis mutable unsigned DefinitionLength; 59b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis mutable bool IsDefinitionLengthCached : 1; 60b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 6163333619fa68c8d1d8219f6d7f2d3c36f4356346James Dennett /// \brief True if this macro is a function-like macro, false if it 625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// is an object-like macro. 635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool IsFunctionLike : 1; 641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// IsC99Varargs - True if this macro is of the form "#define X(...)" or 665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// "#define X(Y,Z,...)". The __VA_ARGS__ token should be replaced with the 675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// contents of "..." in an invocation. 685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool IsC99Varargs : 1; 691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// IsGNUVarargs - True if this macro is of the form "#define X(a...)". The 7112fe558612303cb2188c321a77a21f7df23b6ae8Chris Lattner /// "a" identifier in the replacement list will be replaced with all arguments 725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// of the macro starting with the specified one. 735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool IsGNUVarargs : 1; 741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if 765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// it has not yet been redefined or undefined. 775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool IsBuiltinMacro : 1; 781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7963333619fa68c8d1d8219f6d7f2d3c36f4356346James Dennett /// \brief True if this macro was loaded from an AST file. 803c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl bool IsFromAST : 1; 81083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl 827143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor /// \brief Whether this macro changed after it was loaded from an AST file. 837143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor bool ChangedAfterLoad : 1; 847143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor 855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate: 865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer //===--------------------------------------------------------------------===// 875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // State that changes as the macro is used. 885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// IsDisabled - True if we have started an expansion of this macro already. 9063333619fa68c8d1d8219f6d7f2d3c36f4356346James Dennett /// This disables recursive expansion, which would be quite bad for things 9163333619fa68c8d1d8219f6d7f2d3c36f4356346James Dennett /// like \#define A A. 925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool IsDisabled : 1; 931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// IsUsed - True if this macro is either defined in the main file and has 951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump /// been used, or if it is not defined in the main file. This is used to 965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// emit -Wunused-macros diagnostics. 975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool IsUsed : 1; 981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 99f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined 100f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner /// without emitting a warning. 101f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner bool IsAllowRedefinitionsWithoutWarning : 1; 1020827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1030827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis /// \brief Must warn if the macro is unused at the end of translation unit. 1040827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis bool IsWarnIfUnused : 1; 105f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner 106aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor /// \brief Whether the macro has public (when described in a module). 107aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor bool IsPublic : 1; 108aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor 109f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner ~MacroInfo() { 1100301b3ff132a4d986c092d161cb77d74b04cd2a6Chris Lattner assert(ArgumentList == 0 && "Didn't call destroy before dtor!"); 1110301b3ff132a4d986c092d161cb77d74b04cd2a6Chris Lattner } 1121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 113cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattnerpublic: 114cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner MacroInfo(SourceLocation DefLoc); 115f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator); 116f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner 117cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner /// FreeArgumentList - Free the argument list of the macro, restoring it to a 118cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner /// state where it can be reused for other devious purposes. 1192c1ab9079cb117dc0470ab423fe0bc5177546339Chris Lattner void FreeArgumentList() { 1200301b3ff132a4d986c092d161cb77d74b04cd2a6Chris Lattner ArgumentList = 0; 121cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner NumArguments = 0; 122cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner } 1231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 124cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner /// Destroy - destroy this MacroInfo object. 1252c1ab9079cb117dc0470ab423fe0bc5177546339Chris Lattner void Destroy() { 1262c1ab9079cb117dc0470ab423fe0bc5177546339Chris Lattner FreeArgumentList(); 127cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner this->~MacroInfo(); 12825c9648909193d380a4e135d2e3d25394ba12922Chris Lattner } 1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// getDefinitionLoc - Return the location that the macro was defined at. 1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation getDefinitionLoc() const { return Location; } 1332451b528fe114595d0f10ef2c05047928558ab0fChris Lattner 1342451b528fe114595d0f10ef2c05047928558ab0fChris Lattner /// setDefinitionEndLoc - Set the location of the last token in the macro. 1352451b528fe114595d0f10ef2c05047928558ab0fChris Lattner /// 1362451b528fe114595d0f10ef2c05047928558ab0fChris Lattner void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; } 1378a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko 1382451b528fe114595d0f10ef2c05047928558ab0fChris Lattner /// getDefinitionEndLoc - Return the location of the last token in the macro. 1392451b528fe114595d0f10ef2c05047928558ab0fChris Lattner /// 1402451b528fe114595d0f10ef2c05047928558ab0fChris Lattner SourceLocation getDefinitionEndLoc() const { return EndLocation; } 1418a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko 1428a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko /// \brief Set the location where macro was undefined. Can only be set once. 1438a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko void setUndefLoc(SourceLocation UndefLoc) { 1448a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko assert(UndefLocation.isInvalid() && "UndefLocation is already set!"); 1458a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko assert(UndefLoc.isValid() && "Invalid UndefLoc!"); 1468a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko UndefLocation = UndefLoc; 1478a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko } 1488a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko 1498a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko /// \brief Get the location where macro was undefined. 1508a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko SourceLocation getUndefLoc() const { return UndefLocation; } 1518a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko 1528a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko /// \brief Set previous definition of the macro with the same name. Can only 1538a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko /// be set once. 1548a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko void setPreviousDefinition(MacroInfo *PreviousDef) { 1558a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko assert(!PreviousDefinition && "PreviousDefiniton is already set!"); 1568a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko PreviousDefinition = PreviousDef; 1578a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko } 1588a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko 1598a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko /// \brief Get previous definition of the macro with the same name. 1608a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko MacroInfo *getPreviousDefinition() { return PreviousDefinition; } 1618a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko 162b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// \brief Get length in characters of the macro definition. 163b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis unsigned getDefinitionLength(SourceManager &SM) const { 164b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis if (IsDefinitionLengthCached) 165b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis return DefinitionLength; 166b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis return getDefinitionLengthSlow(SM); 167b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis } 1682451b528fe114595d0f10ef2c05047928558ab0fChris Lattner 1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// isIdenticalTo - Return true if the specified macro definition is equal to 1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// this macro in spelling, arguments, and whitespace. This is used to emit 1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// duplicate definition warnings. This implements the rules in C99 6.10.3. 1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const; 1731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag. 1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void setIsBuiltinMacro(bool Val = true) { 1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsBuiltinMacro = Val; 1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// setIsUsed - Set the value of the IsUsed flag. 1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void setIsUsed(bool Val) { 1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsUsed = Val; 1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 186f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner /// setIsAllowRedefinitionsWithoutWarning - Set the value of the 187f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner /// IsAllowRedefinitionsWithoutWarning flag. 188f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner void setIsAllowRedefinitionsWithoutWarning(bool Val) { 189f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner IsAllowRedefinitionsWithoutWarning = Val; 190f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner } 191f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner 1920827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis /// \brief Set the value of the IsWarnIfUnused flag. 1930827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis void setIsWarnIfUnused(bool val) { 1940827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis IsWarnIfUnused = val; 1950827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis } 1960827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 19725c9648909193d380a4e135d2e3d25394ba12922Chris Lattner /// setArgumentList - Set the specified list of identifiers as the argument 19825c9648909193d380a4e135d2e3d25394ba12922Chris Lattner /// list for this macro. 199685befeb5f6472585bae473a6389e47cab9eac67Chris Lattner void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs, 200685befeb5f6472585bae473a6389e47cab9eac67Chris Lattner llvm::BumpPtrAllocator &PPAllocator) { 201cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner assert(ArgumentList == 0 && NumArguments == 0 && 202cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner "Argument list already set!"); 20325c9648909193d380a4e135d2e3d25394ba12922Chris Lattner if (NumArgs == 0) return; 2041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20525c9648909193d380a4e135d2e3d25394ba12922Chris Lattner NumArguments = NumArgs; 206685befeb5f6472585bae473a6389e47cab9eac67Chris Lattner ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs); 207cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner for (unsigned i = 0; i != NumArgs; ++i) 208cf29e0716bb3ecbbc15b74cd648367d6b075fdf0Chris Lattner ArgumentList[i] = List[i]; 2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21125c9648909193d380a4e135d2e3d25394ba12922Chris Lattner /// Arguments - The list of arguments for a function-like macro. This can be 21225c9648909193d380a4e135d2e3d25394ba12922Chris Lattner /// empty, for, e.g. "#define X()". 21325c9648909193d380a4e135d2e3d25394ba12922Chris Lattner typedef IdentifierInfo* const *arg_iterator; 214f73903a1ded46748e1dfda151f5d037b7b3d31f9Chris Lattner bool arg_empty() const { return NumArguments == 0; } 21525c9648909193d380a4e135d2e3d25394ba12922Chris Lattner arg_iterator arg_begin() const { return ArgumentList; } 21625c9648909193d380a4e135d2e3d25394ba12922Chris Lattner arg_iterator arg_end() const { return ArgumentList+NumArguments; } 21725c9648909193d380a4e135d2e3d25394ba12922Chris Lattner unsigned getNumArgs() const { return NumArguments; } 2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// getArgumentNum - Return the argument number of the specified identifier, 2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// or -1 if the identifier is not a formal argument identifier. 22125c9648909193d380a4e135d2e3d25394ba12922Chris Lattner int getArgumentNum(IdentifierInfo *Arg) const { 22225c9648909193d380a4e135d2e3d25394ba12922Chris Lattner for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I) 22325c9648909193d380a4e135d2e3d25394ba12922Chris Lattner if (*I == Arg) return I-arg_begin(); 2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return -1; 2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// Function/Object-likeness. Keep track of whether this macro has formal 2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// parameters. 2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void setIsFunctionLike() { IsFunctionLike = true; } 2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isFunctionLike() const { return IsFunctionLike; } 2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isObjectLike() const { return !IsFunctionLike; } 2321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// Varargs querying methods. This can only be set for function-like macros. 2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void setIsC99Varargs() { IsC99Varargs = true; } 2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void setIsGNUVarargs() { IsGNUVarargs = true; } 2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isC99Varargs() const { return IsC99Varargs; } 2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isGNUVarargs() const { return IsGNUVarargs; } 2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } 2391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// isBuiltinMacro - Return true if this macro is a builtin macro, such as 2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// __LINE__, which requires processing before expansion. 2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isBuiltinMacro() const { return IsBuiltinMacro; } 2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2443c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl /// isFromAST - Return true if this macro was loaded from an AST file. 2453c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl bool isFromAST() const { return IsFromAST; } 246083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl 2473c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl /// setIsFromAST - Set whether this macro was loaded from an AST file. 2483c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; } 249083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl 2507143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor /// \brief Determine whether this macro has changed since it was loaded from 2517143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor /// an AST file. 2527143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor bool hasChangedAfterLoad() const { return ChangedAfterLoad; } 2537143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor 2547143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor /// \brief Note whether this macro has changed after it was loaded from an 2557143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor /// AST file. 2567143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; } 2577143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor 2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// isUsed - Return false if this macro is defined in the main file and has 2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// not yet been used. 2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isUsed() const { return IsUsed; } 2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 262f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be 263f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner /// redefined without warning. 264f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner bool isAllowRedefinitionsWithoutWarning() const { 265f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner return IsAllowRedefinitionsWithoutWarning; 266f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner } 267f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner 2680827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis /// \brief Return true if we should emit a warning if the macro is unused. 2690827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis bool isWarnIfUnused() const { 2700827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis return IsWarnIfUnused; 2710827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis } 2720827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// getNumTokens - Return the number of tokens that this macro expands to. 2745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 2755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned getNumTokens() const { 2765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return ReplacementTokens.size(); 2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 279d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner const Token &getReplacementToken(unsigned Tok) const { 2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(Tok < ReplacementTokens.size() && "Invalid token #"); 2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return ReplacementTokens[Tok]; 2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 284686775deca8b8685eb90801495880e3abdd844c2Chris Lattner typedef SmallVector<Token, 8>::const_iterator tokens_iterator; 285c215bd659d8266a1d6b66ce231a63405a4c61dafChris Lattner tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); } 286c215bd659d8266a1d6b66ce231a63405a4c61dafChris Lattner tokens_iterator tokens_end() const { return ReplacementTokens.end(); } 287f73903a1ded46748e1dfda151f5d037b7b3d31f9Chris Lattner bool tokens_empty() const { return ReplacementTokens.empty(); } 2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// AddTokenToBody - Add the specified token to the replacement text for the 2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// macro. 291d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner void AddTokenToBody(const Token &Tok) { 292b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis assert(!IsDefinitionLengthCached && 293b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis "Changing replacement tokens after definition length got calculated"); 2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ReplacementTokens.push_back(Tok); 2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// isEnabled - Return true if this macro is enabled: in other words, that we 2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// are not currently in an expansion of this macro. 2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isEnabled() const { return !IsDisabled; } 3001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void EnableMacro() { 3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(IsDisabled && "Cannot enable an already-enabled macro!"); 3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsDisabled = false; 3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void DisableMacro() { 3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(!IsDisabled && "Cannot disable an already-disabled macro!"); 3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsDisabled = true; 3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 310b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 3117143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor /// \brief Set the export location for this macro. 312aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor void setVisibility(bool Public, SourceLocation Loc) { 313aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor VisibilityLocation = Loc; 314aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor IsPublic = Public; 3157143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor } 3167143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor 317aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor /// \brief Determine whether this macro is part of the public API of its 3187143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor /// module. 319aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor bool isPublic() const { return IsPublic; } 3207143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor 321aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor /// \brief Determine the location where this macro was explicitly made 322aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor /// public or private within its module. 323aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor SourceLocation getVisibilityLocation() { return VisibilityLocation; } 3247143aab97c6e849a5a5005b7853b8c7d5af008edDouglas Gregor 325b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidisprivate: 326b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis unsigned getDefinitionLengthSlow(SourceManager &SM) const; 3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}; 3281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} // end namespace clang 3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 332