diff options
-rw-r--r-- | clang/include/clang/Lex/MacroInfo.h | 171 | ||||
-rw-r--r-- | clang/include/clang/Lex/PPMutationListener.h | 4 | ||||
-rw-r--r-- | clang/include/clang/Lex/Preprocessor.h | 44 | ||||
-rw-r--r-- | clang/include/clang/Serialization/ASTDeserializationListener.h | 4 | ||||
-rw-r--r-- | clang/include/clang/Serialization/ASTReader.h | 21 | ||||
-rw-r--r-- | clang/include/clang/Serialization/ASTWriter.h | 14 | ||||
-rw-r--r-- | clang/lib/Frontend/PrintPreprocessedOutput.cpp | 2 | ||||
-rw-r--r-- | clang/lib/Lex/MacroInfo.cpp | 59 | ||||
-rw-r--r-- | clang/lib/Lex/PPDirectives.cpp | 53 | ||||
-rw-r--r-- | clang/lib/Lex/PPMacroExpansion.cpp | 95 | ||||
-rw-r--r-- | clang/lib/Lex/Pragma.cpp | 17 | ||||
-rw-r--r-- | clang/lib/Lex/Preprocessor.cpp | 11 | ||||
-rw-r--r-- | clang/lib/Sema/Sema.cpp | 3 | ||||
-rw-r--r-- | clang/lib/Sema/SemaCodeComplete.cpp | 5 | ||||
-rw-r--r-- | clang/lib/Serialization/ASTReader.cpp | 52 | ||||
-rw-r--r-- | clang/lib/Serialization/ASTWriter.cpp | 56 | ||||
-rw-r--r-- | clang/tools/libclang/CIndex.cpp | 16 |
17 files changed, 322 insertions, 305 deletions
diff --git a/clang/include/clang/Lex/MacroInfo.h b/clang/include/clang/Lex/MacroInfo.h index aeedd735e39..80b66e4a26e 100644 --- a/clang/include/clang/Lex/MacroInfo.h +++ b/clang/include/clang/Lex/MacroInfo.h @@ -32,24 +32,12 @@ class MacroInfo { SourceLocation Location; /// EndLocation - The location of the last token in the macro. SourceLocation EndLocation; - /// \brief The location where the macro was #undef'd, or an invalid location - /// for macros that haven't been undefined. - SourceLocation UndefLocation; - /// \brief Previous definition, the identifier of this macro was defined to, - /// or NULL. - MacroInfo *PreviousDefinition; /// Arguments - The list of arguments for a function-like macro. This can be /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this /// includes the \c __VA_ARGS__ identifier on the list. IdentifierInfo **ArgumentList; unsigned NumArguments; - - /// \brief The location at which this macro was either explicitly exported - /// from its module or marked as private. - /// - /// If invalid, this macro has not been explicitly given any visibility. - SourceLocation VisibilityLocation; /// \brief This is the list of tokens that the macro is defined to. SmallVector<Token, 8> ReplacementTokens; @@ -78,12 +66,6 @@ class MacroInfo { /// \brief Whether this macro contains the sequence ", ## __VA_ARGS__" bool HasCommaPasting : 1; - - /// \brief True if this macro was loaded from an AST file. - bool IsFromAST : 1; - - /// \brief Whether this macro changed after it was loaded from an AST file. - bool ChangedAfterLoad : 1; private: //===--------------------------------------------------------------------===// @@ -105,18 +87,6 @@ private: /// \brief Must warn if the macro is unused at the end of translation unit. bool IsWarnIfUnused : 1; - - /// \brief Whether the macro has public (when described in a module). - bool IsPublic : 1; - - /// \brief Whether the macro definition is currently "hidden". - /// Note that this is transient state that is never serialized to the AST - /// file. - bool IsHidden : 1; - - /// \brief Whether the definition of this macro is ambiguous, due to - /// multiple definitions coming in from multiple modules. - bool IsAmbiguous : 1; ~MacroInfo() { assert(ArgumentList == 0 && "Didn't call destroy before dtor!"); @@ -124,7 +94,6 @@ private: public: MacroInfo(SourceLocation DefLoc); - MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator); /// FreeArgumentList - Free the argument list of the macro, restoring it to a /// state where it can be reused for other devious purposes. @@ -151,29 +120,6 @@ public: /// SourceLocation getDefinitionEndLoc() const { return EndLocation; } - /// \brief Set the location where macro was undefined. Can only be set once. - void setUndefLoc(SourceLocation UndefLoc) { - assert(UndefLocation.isInvalid() && "UndefLocation is already set!"); - assert(UndefLoc.isValid() && "Invalid UndefLoc!"); - UndefLocation = UndefLoc; - } - - /// \brief Get the location where macro was undefined. - SourceLocation getUndefLoc() const { return UndefLocation; } - - /// \brief Set previous definition of the macro with the same name. - void setPreviousDefinition(MacroInfo *PreviousDef) { - PreviousDefinition = PreviousDef; - } - - /// \brief Get previous definition of the macro with the same name. - MacroInfo *getPreviousDefinition() { return PreviousDefinition; } - - /// \brief Find macro definition active in the specified source location. If - /// this macro was not defined there, return NULL. - const MacroInfo *findDefinitionAtLoc(SourceLocation L, - SourceManager &SM) const; - /// \brief Get length in characters of the macro definition. unsigned getDefinitionLength(SourceManager &SM) const { if (IsDefinitionLengthCached) @@ -259,20 +205,6 @@ public: bool hasCommaPasting() const { return HasCommaPasting; } void setHasCommaPasting() { HasCommaPasting = true; } - /// isFromAST - Return true if this macro was loaded from an AST file. - bool isFromAST() const { return IsFromAST; } - - /// setIsFromAST - Set whether this macro was loaded from an AST file. - void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; } - - /// \brief Determine whether this macro has changed since it was loaded from - /// an AST file. - bool hasChangedAfterLoad() const { return ChangedAfterLoad; } - - /// \brief Note whether this macro has changed after it was loaded from an - /// AST file. - void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; } - /// isUsed - Return false if this macro is defined in the main file and has /// not yet been used. bool isUsed() const { return IsUsed; } @@ -326,6 +258,93 @@ public: IsDisabled = true; } +private: + unsigned getDefinitionLengthSlow(SourceManager &SM) const; +}; + +class MacroDirective { + MacroInfo *Info; + + /// \brief Previous definition, the identifier of this macro was defined to, + /// or NULL. + MacroDirective *Previous; + + SourceLocation Loc; + + /// \brief The location where the macro was #undef'd, or an invalid location + /// for macros that haven't been undefined. + SourceLocation UndefLocation; + + /// \brief The location at which this macro was either explicitly exported + /// from its module or marked as private. + /// + /// If invalid, this macro has not been explicitly given any visibility. + SourceLocation VisibilityLocation; + + /// \brief True if this macro was loaded from an AST file. + bool IsImported : 1; + + /// \brief Whether the macro has public (when described in a module). + bool IsPublic : 1; + + /// \brief Whether the macro definition is currently "hidden". + /// Note that this is transient state that is never serialized to the AST + /// file. + bool IsHidden : 1; + + /// \brief Whether the definition of this macro is ambiguous, due to + /// multiple definitions coming in from multiple modules. + bool IsAmbiguous : 1; + + /// \brief Whether this macro changed after it was loaded from an AST file. + bool ChangedAfterLoad : 1; + +public: + explicit MacroDirective(MacroInfo *MI) + : Info(MI), Previous(0), Loc(MI->getDefinitionLoc()), + IsImported(false), IsPublic(true), IsHidden(false), IsAmbiguous(false), + ChangedAfterLoad(false) { + assert(MI && "MacroInfo is null"); + } + + MacroDirective(MacroInfo *MI, SourceLocation Loc, bool isImported) + : Info(MI), Previous(0), Loc(Loc), + IsImported(isImported), IsPublic(true), IsHidden(false), + IsAmbiguous(false), ChangedAfterLoad(false) { + assert(MI && "MacroInfo is null"); + } + + SourceLocation getLocation() const { return Loc; } + + /// \brief Set the location where macro was undefined. Can only be set once. + void setUndefLoc(SourceLocation UndefLoc) { + assert(UndefLocation.isInvalid() && "UndefLocation is already set!"); + assert(UndefLoc.isValid() && "Invalid UndefLoc!"); + UndefLocation = UndefLoc; + } + + const MacroInfo *getInfo() const { return Info; } + MacroInfo *getInfo() { return Info; } + + /// \brief Get the location where macro was undefined. + SourceLocation getUndefLoc() const { return UndefLocation; } + + /// \brief Set previous definition of the macro with the same name. + void setPrevious(MacroDirective *Prev) { + Previous = Prev; + } + + /// \brief Get previous definition of the macro with the same name. + const MacroDirective *getPrevious() const { return Previous; } + + /// \brief Get previous definition of the macro with the same name. + MacroDirective *getPrevious() { return Previous; } + + /// \brief Find macro definition active in the specified source location. If + /// this macro was not defined there, return NULL. + const MacroDirective *findDirectiveAtLoc(SourceLocation L, + SourceManager &SM) const; + /// \brief Set the export location for this macro. void setVisibility(bool Public, SourceLocation Loc) { VisibilityLocation = Loc; @@ -338,7 +357,10 @@ public: /// \brief Determine the location where this macro was explicitly made /// public or private within its module. - SourceLocation getVisibilityLocation() { return VisibilityLocation; } + SourceLocation getVisibilityLocation() const { return VisibilityLocation; } + + /// \brief True if this macro was loaded from an AST file. + bool isImported() const { return IsImported; } /// \brief Determine whether this macro is currently defined (and has not /// been #undef'd) or has been hidden. @@ -356,9 +378,14 @@ public: /// \brief Set whether this macro definition is ambiguous. void setAmbiguous(bool Val) { IsAmbiguous = Val; } - -private: - unsigned getDefinitionLengthSlow(SourceManager &SM) const; + + /// \brief Determine whether this macro has changed since it was loaded from + /// an AST file. + bool hasChangedAfterLoad() const { return ChangedAfterLoad; } + + /// \brief Note whether this macro has changed after it was loaded from an + /// AST file. + void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; } }; } // end namespace clang diff --git a/clang/include/clang/Lex/PPMutationListener.h b/clang/include/clang/Lex/PPMutationListener.h index 5319c66fa27..995842974a9 100644 --- a/clang/include/clang/Lex/PPMutationListener.h +++ b/clang/include/clang/Lex/PPMutationListener.h @@ -17,7 +17,7 @@ namespace clang { -class MacroInfo; +class MacroDirective; /// \brief A record that describes an update to a macro that was /// originally loaded to an AST file and has been modified within the @@ -35,7 +35,7 @@ public: virtual ~PPMutationListener(); /// \brief A macro has been #undef'd. - virtual void UndefinedMacro(MacroInfo *MI) { } + virtual void UndefinedMacro(MacroDirective *MD) { } }; } // end namespace clang diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index df243f97d81..c1c55db41a8 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -313,7 +313,7 @@ class Preprocessor : public RefCountedBase<Preprocessor> { /// Macros - For each IdentifierInfo that was associated with a macro, we /// keep a mapping to the history of all macro definitions and #undefs in /// the reverse order (the latest one is in the head of the list). - llvm::DenseMap<const IdentifierInfo*, MacroInfo*> Macros; + llvm::DenseMap<const IdentifierInfo*, MacroDirective*> Macros; friend class ASTReader; /// \brief Macros that we want to warn because they are not used at the end @@ -534,29 +534,43 @@ public: /// \brief Given an identifier, return the MacroInfo it is \#defined to /// or null if it isn't \#define'd. - MacroInfo *getMacroInfo(IdentifierInfo *II) const { + MacroDirective *getMacroDirective(IdentifierInfo *II) const { if (!II->hasMacroDefinition()) return 0; - MacroInfo *MI = getMacroInfoHistory(II); - assert(MI->getUndefLoc().isInvalid() && "Macro is undefined!"); - return MI; + MacroDirective *MD = getMacroDirectiveHistory(II); + assert(MD->getUndefLoc().isInvalid() && "Macro is undefined!"); + return MD; + } + + const MacroInfo *getMacroInfo(IdentifierInfo *II) const { + return const_cast<Preprocessor*>(this)->getMacroInfo(II); + } + + MacroInfo *getMacroInfo(IdentifierInfo *II) { + if (MacroDirective *MD = getMacroDirective(II)) + return MD->getInfo(); + return 0; } /// \brief Given an identifier, return the (probably #undef'd) MacroInfo /// representing the most recent macro definition. One can iterate over all /// previous macro definitions from it. This method should only be called for /// identifiers that hadMacroDefinition(). - MacroInfo *getMacroInfoHistory(const IdentifierInfo *II) const; + MacroDirective *getMacroDirectiveHistory(const IdentifierInfo *II) const; /// \brief Specify a macro for this identifier. - void setMacroInfo(IdentifierInfo *II, MacroInfo *MI); + void setMacroDirective(IdentifierInfo *II, MacroInfo *MI, + SourceLocation Loc, bool isImported); + void setMacroDirective(IdentifierInfo *II, MacroInfo *MI) { + setMacroDirective(II, MI, MI->getDefinitionLoc(), false); + } /// \brief Add a MacroInfo that was loaded from an AST file. - void addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI, - MacroInfo *Hint = 0); + void addLoadedMacroInfo(IdentifierInfo *II, MacroDirective *MD, + MacroDirective *Hint = 0); /// \brief Make the given MacroInfo, that was loaded from an AST file and /// previously hidden, visible. - void makeLoadedMacroInfoVisible(IdentifierInfo *II, MacroInfo *MI); + void makeLoadedMacroInfoVisible(IdentifierInfo *II, MacroDirective *MD); /// \brief Undefine a macro for this identifier. void clearMacroInfo(IdentifierInfo *II); @@ -565,7 +579,7 @@ public: /// IdentifierInfo::hasMacroDefinition() set and an empty /// MacroInfo::getUndefLoc() at the head of the list. typedef llvm::DenseMap<const IdentifierInfo *, - MacroInfo*>::const_iterator macro_iterator; + MacroDirective*>::const_iterator macro_iterator; macro_iterator macro_begin(bool IncludeExternalMacros = true) const; macro_iterator macro_end(bool IncludeExternalMacros = true) const; @@ -1194,9 +1208,6 @@ public: /// \brief Allocate a new MacroInfo object with the provided SourceLocation. MacroInfo *AllocateMacroInfo(SourceLocation L); - /// \brief Allocate a new MacroInfo object which is clone of \p MI. - MacroInfo *CloneMacroInfo(const MacroInfo &MI); - /// \brief Turn the specified lexer token into a fully checked and spelled /// filename, e.g. as an operand of \#include. /// @@ -1272,6 +1283,9 @@ private: /// \brief Allocate a new MacroInfo object. MacroInfo *AllocateMacroInfo(); + MacroDirective *AllocateMacroDirective(MacroInfo *MI, SourceLocation Loc, + bool isImported); + /// \brief Release the specified MacroInfo for re-use. /// /// This memory will be reused for allocating new MacroInfo objects. @@ -1424,7 +1438,7 @@ private: // Macro handling. void HandleDefineDirective(Token &Tok); void HandleUndefDirective(Token &Tok); - void UndefineMacro(IdentifierInfo *II, MacroInfo *MI, + void UndefineMacro(IdentifierInfo *II, MacroDirective *MD, SourceLocation UndefLoc); // Conditional Inclusion. diff --git a/clang/include/clang/Serialization/ASTDeserializationListener.h b/clang/include/clang/Serialization/ASTDeserializationListener.h index 0218129fb62..b54f7919946 100644 --- a/clang/include/clang/Serialization/ASTDeserializationListener.h +++ b/clang/include/clang/Serialization/ASTDeserializationListener.h @@ -23,7 +23,7 @@ class Decl; class ASTReader; class QualType; class MacroDefinition; -class MacroInfo; +class MacroDirective; class Module; class ASTDeserializationListener { @@ -39,7 +39,7 @@ public: virtual void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) { } /// \brief A macro was read from the AST file. - virtual void MacroRead(serialization::MacroID ID, MacroInfo *MI) { } + virtual void MacroRead(serialization::MacroID ID, MacroDirective *MD) { } /// \brief A type was deserialized from the AST file. The ID here has the /// qualifier bits already removed, and T is guaranteed to be locally /// unqualified. diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h index b8943e73473..68f77207716 100644 --- a/clang/include/clang/Serialization/ASTReader.h +++ b/clang/include/clang/Serialization/ASTReader.h @@ -71,6 +71,7 @@ class CXXCtorInitializer; class GlobalModuleIndex; class GotoStmt; class MacroDefinition; +class MacroDirective; class NamedDecl; class OpaqueValueExpr; class Preprocessor; @@ -426,7 +427,7 @@ private: /// If the pointer at index I is non-NULL, then it refers to the /// MacroInfo for the identifier with ID=I+1 that has already /// been loaded. - std::vector<MacroInfo *> MacrosLoaded; + std::vector<MacroDirective *> MacrosLoaded; typedef ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4> GlobalMacroMapType; @@ -473,7 +474,7 @@ private: union { Decl *D; - MacroInfo *MI; + MacroDirective *MD; }; IdentifierInfo *Id; @@ -481,11 +482,11 @@ private: public: HiddenName(Decl *D) : Kind(Declaration), Loc(), D(D), Id() { } - HiddenName(IdentifierInfo *II, MacroInfo *MI) - : Kind(MacroVisibility), Loc(), MI(MI), Id(II) { } + HiddenName(IdentifierInfo *II, MacroDirective *MD) + : Kind(MacroVisibility), Loc(), MD(MD), Id(II) { } - HiddenName(IdentifierInfo *II, MacroInfo *MI, SourceLocation Loc) - : Kind(MacroUndef), Loc(Loc.getRawEncoding()), MI(MI), Id(II) { } + HiddenName(IdentifierInfo *II, MacroDirective *MD, SourceLocation Loc) + : Kind(MacroUndef), Loc(Loc.getRawEncoding()), MD(MD), Id(II) { } NameKind getKind() const { return Kind; } @@ -494,10 +495,10 @@ private: return D; } - std::pair<IdentifierInfo *, MacroInfo *> getMacro() const { + std::pair<IdentifierInfo *, MacroDirective *> getMacro() const { assert((getKind() == MacroUndef || getKind() == MacroVisibility) && "Hidden name is not a macro!"); - return std::make_pair(Id, MI); + return std::make_pair(Id, MD); } SourceLocation getMacroUndefLoc() const { @@ -1604,7 +1605,7 @@ public: unsigned LocalID); /// \brief Retrieve the macro with the given ID. - MacroInfo *getMacro(serialization::MacroID ID, MacroInfo *Hint = 0); + MacroDirective *getMacro(serialization::MacroID ID, MacroDirective *Hint = 0); /// \brief Retrieve the global macro ID corresponding to the given local /// ID within the given module file. @@ -1763,7 +1764,7 @@ public: Expr *ReadSubExpr(); /// \brief Reads the macro record located at the given offset. - void ReadMacroRecord(ModuleFile &F, uint64_t Offset, MacroInfo *Hint = 0); + void ReadMacroRecord(ModuleFile &F, uint64_t Offset, MacroDirective *Hint = 0); /// \brief Determine the global preprocessed entity ID that corresponds to /// the given local ID within the given module. diff --git a/clang/include/clang/Serialization/ASTWriter.h b/clang/include/clang/Serialization/ASTWriter.h index d632ba5c6e4..b966ae1228f 100644 --- a/clang/include/clang/Serialization/ASTWriter.h +++ b/clang/include/clang/Serialization/ASTWriter.h @@ -53,7 +53,7 @@ class MacroDefinition; class OpaqueValueExpr; class OpenCLOptions; class ASTReader; -class MacroInfo; +class MacroDirective; class Module; class PreprocessedEntity; class PreprocessingRecord; @@ -230,7 +230,7 @@ private: serialization::MacroID NextMacroID; /// \brief Map that provides the ID numbers of each macro. - llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs; + llvm::DenseMap<MacroDirective *, serialization::MacroID> MacroIDs; /// @name FlushStmt Caches /// @{ @@ -267,7 +267,7 @@ private: /// table, indexed by the Selector ID (-1). std::vector<uint32_t> SelectorOffsets; - typedef llvm::MapVector<MacroInfo *, MacroUpdate> MacroUpdatesMap; + typedef llvm::MapVector<MacroDirective *, MacroUpdate> MacroUpdatesMap; /// \brief Updates to macro definitions that were loaded from an AST file. MacroUpdatesMap MacroUpdates; @@ -510,7 +510,7 @@ public: void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record); /// \brief Emit a reference to a macro. - void addMacroRef(MacroInfo *MI, RecordDataImpl &Record); + void addMacroRef(MacroDirective *MI, RecordDataImpl &Record); /// \brief Emit a Selector (which is a smart pointer reference). void AddSelectorRef(Selector, RecordDataImpl &Record); @@ -530,7 +530,7 @@ public: serialization::IdentID getIdentifierRef(const IdentifierInfo *II); /// \brief Get the unique number used to refer to the given macro. - serialization::MacroID getMacroRef(MacroInfo *MI); + serialization::MacroID getMacroRef(MacroDirective *MI); /// \brief Emit a reference to a type. void AddTypeRef(QualType T, RecordDataImpl &Record); @@ -693,7 +693,7 @@ public: // ASTDeserializationListener implementation void ReaderInitialized(ASTReader *Reader); void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II); - void MacroRead(serialization::MacroID ID, MacroInfo *MI); + void MacroRead(serialization::MacroID ID, MacroDirective *MI); void TypeRead(serialization::TypeIdx Idx, QualType T); void SelectorRead(serialization::SelectorID ID, Selector Sel); void MacroDefinitionRead(serialization::PreprocessedEntityID ID, @@ -701,7 +701,7 @@ public: void ModuleRead(serialization::SubmoduleID ID, Module *Mod); // PPMutationListener implementation. - virtual void UndefinedMacro(MacroInfo *MI); + virtual void UndefinedMacro(MacroDirective *MD); // ASTMutationListener implementation. virtual void CompletedTagDefinition(const TagDecl *D); diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp index 40240931388..c85945b8941 100644 --- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp @@ -592,7 +592,7 @@ static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) { for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); I != E; ++I) { if (I->first->hasMacroDefinition()) - MacrosByID.push_back(id_macro_pair(I->first, I->second)); + MacrosByID.push_back(id_macro_pair(I->first, I->second->getInfo())); } llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare); diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp index d1875c79d0d..ed6cc6edafb 100644 --- a/clang/lib/Lex/MacroInfo.cpp +++ b/clang/lib/Lex/MacroInfo.cpp @@ -17,7 +17,6 @@ using namespace clang; MacroInfo::MacroInfo(SourceLocation DefLoc) : Location(DefLoc), - PreviousDefinition(0), ArgumentList(0), NumArguments(0), IsDefinitionLengthCached(false), @@ -26,54 +25,10 @@ MacroInfo::MacroInfo(SourceLocation DefLoc) IsGNUVarargs(false), IsBuiltinMacro(false), HasCommaPasting(false), - IsFromAST(false), - ChangedAfterLoad(false), IsDisabled(false), IsUsed(false), IsAllowRedefinitionsWithoutWarning(false), - IsWarnIfUnused(false), - IsPublic(true), - IsHidden(false), - IsAmbiguous(false) { -} - -MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator) - : Location(MI.Location), - EndLocation(MI.EndLocation), - UndefLocation(MI.UndefLocation), - PreviousDefinition(0), - ArgumentList(0), - NumArguments(0), - ReplacementTokens(MI.ReplacementTokens), - DefinitionLength(MI.DefinitionLength), - IsDefinitionLengthCached(MI.IsDefinitionLengthCached), - IsFunctionLike(MI.IsFunctionLike), - IsC99Varargs(MI.IsC99Varargs), - IsGNUVarargs(MI.IsGNUVarargs), - IsBuiltinMacro(MI.IsBuiltinMacro), - HasCommaPasting(MI.HasCommaPasting), - IsFromAST(MI.IsFromAST), - ChangedAfterLoad(MI.ChangedAfterLoad), - IsDisabled(MI.IsDisabled), - IsUsed(MI.IsUsed), - IsAllowRedefinitionsWithoutWarning(MI.IsAllowRedefinitionsWithoutWarning), - IsWarnIfUnused(MI.IsWarnIfUnused), - IsPublic(MI.IsPublic), - IsHidden(MI.IsHidden), - IsAmbiguous(MI.IsAmbiguous) { - setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator); -} - -const MacroInfo *MacroInfo::findDefinitionAtLoc(SourceLocation L, - SourceManager &SM) const { - assert(L.isValid() && "SourceLocation is invalid."); - for (const MacroInfo *MI = this; MI; MI = MI->PreviousDefinition) { - if (MI->Location.isInvalid() || // For macros defined on the command line. - SM.isBeforeInTranslationUnit(MI->Location, L)) - return (MI->UndefLocation.isInvalid() || - SM.isBeforeInTranslationUnit(L, MI->UndefLocation)) ? MI : NULL; - } - return NULL; + IsWarnIfUnused(false) { } unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const { @@ -151,3 +106,15 @@ bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const { return true; } + +const MacroDirective * +MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const { + assert(L.isValid() && "SourceLocation is invalid."); + for (const MacroDirective *MD = this; MD; MD = MD->Previous) { + if (MD->getLocation().isInvalid() || // For macros defined on the command line. + SM.isBeforeInTranslationUnit(MD->getLocation(), L)) + return (MD->UndefLocation.isInvalid() || + SM.isBeforeInTranslationUnit(L, MD->UndefLocation)) ? MD : NULL; + } + return NULL; +} diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index c8e1f4fc3e2..18250281c1d 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -57,10 +57,12 @@ MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { return MI; } -MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) { - MacroInfo *MI = AllocateMacroInfo(); - new (MI) MacroInfo(MacroToClone, BP); - return MI; +MacroDirective *Preprocessor::AllocateMacroDirective(MacroInfo *MI, + SourceLocation Loc, + bool isImported) { + MacroDirective *MD = BP.Allocate<MacroDirective>(); + new (MD) MacroDirective(MI, Loc, isImported); + return MD; } /// \brief Release the specified MacroInfo to be reused for allocating @@ -1110,22 +1112,22 @@ void Preprocessor::HandleMacroPublicDirective(Token &Tok) { CheckEndOfDirective("__public_macro"); // Okay, we finally have a valid identifier to undef. - MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); + MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); // If the macro is not defined, this is an error. - if (MI == 0) { + if (MD == 0) { Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << MacroNameTok.getIdentifierInfo(); return; } // Note that this macro has now been exported. - MI->setVisibility(/*IsPublic=*/true, MacroNameTok.getLocation()); - + MD->setVisibility(/*IsPublic=*/true, MacroNameTok.getLocation()); + // If this macro definition came from a PCH file, mark it // as having changed since serialization. - if (MI->isFromAST()) - MI->setChangedAfterLoad(); + if (MD->isImported()) + MD->setChangedAfterLoad(); } /// \brief Handle a #private directive. @@ -1141,22 +1143,22 @@ void Preprocessor::HandleMacroPrivateDirective(Token &Tok) { CheckEndOfDirective("__private_macro"); // Okay, we finally have a valid identifier to undef. - MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); + MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); // If the macro is not defined, this is an error. - if (MI == 0) { + if (MD == 0) { Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << MacroNameTok.getIdentifierInfo(); return; } // Note that this macro has now been marked private. - MI->setVisibility(/*IsPublic=*/false, MacroNameTok.getLocation()); - + MD->setVisibility(/*IsPublic=*/false, MacroNameTok.getLocation()); + // If this macro definition came from a PCH file, mark it // as having changed since serialization. - if (MI->isFromAST()) - MI->setChangedAfterLoad(); + if (MD->isImported()) + MD->setChangedAfterLoad(); } //===----------------------------------------------------------------------===// @@ -1918,7 +1920,7 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok) { // Finally, if this identifier already had a macro defined for it, verify that // the macro bodies are identical, and issue diagnostics if they are not. - if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { + if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) { // It is very common for system headers to have tons of macro redefinitions // and for warnings to be disabled in system headers. If this is the case, // then don't bother calling MacroInfo::isIdenticalTo. @@ -1940,7 +1942,7 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok) { WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc()); } - setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); + setMacroDirective(MacroNameTok.getIdentifierInfo(), MI); assert(!MI->isUsed()); // If we need warning for not using the macro, add its location in the @@ -1973,7 +1975,8 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) { CheckEndOfDirective("undef"); // Okay, we finally have a valid identifier to undef. - MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); + MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); + const MacroInfo *MI = MD ? MD->getInfo() : 0; // If the callbacks want to know, tell them about the macro #undef. // Note: no matter if the macro was defined or not. @@ -1989,17 +1992,17 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) { if (MI->isWarnIfUnused()) WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); - UndefineMacro(MacroNameTok.getIdentifierInfo(), MI, + UndefineMacro(MacroNameTok.getIdentifierInfo(), MD, MacroNameTok.getLocation()); } -void Preprocessor::UndefineMacro(IdentifierInfo *II, MacroInfo *MI, +void Preprocessor::UndefineMacro(IdentifierInfo *II, MacroDirective *MD, SourceLocation UndefLoc) { - MI->setUndefLoc(UndefLoc); - if (MI->isFromAST()) { - MI->setChangedAfterLoad(); + MD->setUndefLoc(UndefLoc); + if (MD->isImported()) { + MD->setChangedAfterLoad(); if (Listener) - Listener->UndefinedMacro(MI); + Listener->UndefinedMacro(MD); } clearMacroInfo(II); diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index bda31ed294a..3e68fbdf011 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -32,7 +32,8 @@ #include <ctime> using namespace clang; -MacroInfo *Preprocessor::getMacroInfoHistory(const IdentifierInfo *II) const { +MacroDirective * +Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const { assert(II->hadMacroDefinition() && "Identifier has not been not a macro!"); macro_iterator Pos = Macros.find(II); @@ -42,32 +43,32 @@ MacroInfo *Preprocessor::getMacroInfoHistory(const IdentifierInfo *II) const { /// setMacroInfo - Specify a macro for this identifier. /// -void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) { +void Preprocessor::setMacroDirective(IdentifierInfo *II, MacroInfo *MI, + SourceLocation Loc, bool isImported) { assert(MI && "MacroInfo should be non-zero!"); - assert(MI->getUndefLoc().isInvalid() && - "Undefined macros cannot be registered"); - MacroInfo *&StoredMI = Macros[II]; - MI->setPreviousDefinition(StoredMI); - StoredMI = MI; - II->setHasMacroDefinition(MI->getUndefLoc().isInvalid()); + MacroDirective *MD = AllocateMacroDirective(MI, Loc, isImported); + MacroDirective *&StoredMD = Macros[II]; + MD->setPrevious(StoredMD); + StoredMD = MD; + II->setHasMacroDefinition(true); if (II->isFromAST()) II->setChangedSinceDeserialization(); } -void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI, - MacroInfo *Hint) { - assert(MI && "Missing macro?"); - assert(MI->isFromAST() && "Macro is not from an AST?"); - assert(!MI->getPreviousDefinition() && "Macro already in chain?"); +void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroDirective *MD, + MacroDirective *Hint) { + assert(MD && "Missing macro?"); + assert(MD->isImported() && "Macro is not from an AST?"); + assert(!MD->getPrevious() && "Macro already in chain?"); - MacroInfo *&StoredMI = Macros[II]; + MacroDirective *&StoredMD = Macros[II]; // Easy case: this is the first macro definition for this macro. - if (!StoredMI) { - StoredMI = MI; + if (!StoredMD) { + StoredMD = MD; - if (MI->isDefined()) + if (MD->isDefined()) II->setHasMacroDefinition(true); return; } @@ -75,79 +76,79 @@ void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI, // If this macro is a definition and this identifier has been neither // defined nor undef'd in the current translation unit, add this macro // to the end of the chain of definitions. - if (MI->isDefined() && StoredMI->isFromAST()) { + if (MD->isDefined() && StoredMD->isImported()) { // Simple case: if this is the first actual definition, just put it at // th beginning. - if (!StoredMI->isDefined()) { - MI->setPreviousDefinition(StoredMI); - StoredMI = MI; + if (!StoredMD->isDefined()) { + MD->setPrevious(StoredMD); + StoredMD = MD; II->setHasMacroDefinition(true); return; } // Find the end of the definition chain. - MacroInfo *Prev; - MacroInfo *PrevPrev = StoredMI; - bool Ambiguous = StoredMI->isAmbiguous(); + MacroDirective *Prev; + MacroDirective *PrevPrev = StoredMD; + bool Ambiguous = StoredMD->isAmbiguous(); bool MatchedOther = false; do { Prev = PrevPrev; // If the macros are not identical, we have an ambiguity. - if (!Prev->isIdenticalTo(*MI, *this)) { + if (!Prev->getInfo()->isIdenticalTo(*MD->getInfo(), *this)) { if (!Ambiguous) { Ambiguous = true; - StoredMI->setAmbiguous(true); + StoredMD->setAmbiguous(true); } } else { MatchedOther = true; } - } while ((PrevPrev = Prev->getPreviousDefinition()) && + } while ((PrevPrev = Prev->getPrevious()) && PrevPrev->isDefined()); // If there are ambiguous definitions, and we didn't match any other // definition, then mark us as ambiguous. if (Ambiguous && !MatchedOther) - MI->setAmbiguous(true); + MD->setAmbiguous(true); // Wire this macro information into the chain. - MI->setPreviousDefinition(Prev->getPreviousDefinition()); - Prev->setPreviousDefinition(MI); + MD->setPrevious(Prev->getPrevious()); + Prev->setPrevious(MD); return; } // The macro is not a definition; put it at the end of the list. - MacroInfo *Prev = Hint? Hint : StoredMI; - while (Prev->getPreviousDefinition()) - Prev = Prev->getPreviousDefinition(); - Prev->setPreviousDefinition(MI); + MacroDirective *Prev = Hint? Hint : StoredMD; + while (Prev->getPrevious()) + Prev = Prev->getPrevious(); + Prev->setPrevious(MD); } void Preprocessor::makeLoadedMacroInfoVisible(IdentifierInfo *II, - MacroInfo *MI) { - assert(MI->isFromAST() && "Macro must be from the AST"); + MacroDirective *MD) { + assert(MD->isImported() && "Macro must be from the AST"); - MacroInfo *&StoredMI = Macros[II]; - if (StoredMI == MI) { + MacroDirective *&StoredMD = Macros[II]; + if (StoredMD == MD) { // Easy case: this is the first macro anyway. - II->setHasMacroDefinition(MI->isDefined()); + II->setHasMacroDefinition(MD->isDefined()); return; } // Go find the macro and pull it out of the list. // FIXME: Yes, this is O(N), and making a pile of macros visible or hidden // would be quadratic, but it's extremely rare. - MacroInfo *Prev = StoredMI; - while (Prev->getPreviousDefinition() != MI) - Prev = Prev->getPreviousDefinition(); - Prev->setPreviousDefinition(MI->getPreviousDefinition()); - MI->setPreviousDefinition(0); + MacroDirective *Prev = StoredMD; + while (Prev->getPrevious() != MD) + Prev = Prev->getPrevious(); + Prev->setPrevious(MD->getPrevious()); + MD->setPrevious(0); // Add the macro back to the list. - addLoadedMacroInfo(II, MI); + addLoadedMacroInfo(II, MD); - II->setHasMacroDefinition(StoredMI->isDefined()); + II->setHasMacroDefinition(StoredMD->isDefined()); if (II->isFromAST()) II->setChangedSinceDeserialization(); } @@ -170,7 +171,7 @@ static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ // Mark it as being a macro that is builtin. MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); MI->setIsBuiltinMacro(); - PP.setMacroInfo(Id, MI); + PP.setMacroDirective(Id, MI); return Id; } diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp index 092216aef56..23d088a9fb2 100644 --- a/clang/lib/Lex/Pragma.cpp +++ b/clang/lib/Lex/Pragma.cpp @@ -650,17 +650,13 @@ void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { // Get the MacroInfo associated with IdentInfo. MacroInfo *MI = getMacroInfo(IdentInfo); - MacroInfo *MacroCopyToPush = 0; if (MI) { - // Make a clone of MI. - MacroCopyToPush = CloneMacroInfo(*MI); - // Allow the original MacroInfo to be redefined later. MI->setIsAllowRedefinitionsWithoutWarning(true); } // Push the cloned MacroInfo so we can retrieve it later. - PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush); + PragmaPushMacroInfo[IdentInfo].push_back(MI); } /// \brief Handle \#pragma pop_macro. @@ -681,10 +677,10 @@ void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { PragmaPushMacroInfo.find(IdentInfo); if (iter != PragmaPushMacroInfo.end()) { // Forget the MacroInfo currently associated with IdentInfo. - if (MacroInfo *CurrentMI = getMacroInfo(IdentInfo)) { - if (CurrentMI->isWarnIfUnused()) - WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc()); - UndefineMacro(IdentInfo, CurrentMI, MessageLoc); + if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) { + if (CurrentMD->getInfo()->isWarnIfUnused()) + WarnUnusedMacroLocs.erase(CurrentMD->getInfo()->getDefinitionLoc()); + UndefineMacro(IdentInfo, CurrentMD, MessageLoc); } // Get the MacroInfo we want to reinstall. @@ -692,7 +688,8 @@ void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { if (MacroToReInstall) { // Reinstall the previously pushed macro. - setMacroInfo(IdentInfo, MacroToReInstall); + setMacroDirective(IdentInfo, MacroToReInstall, MessageLoc, + /*isImported=*/false); } else if (IdentInfo->hasMacroDefinition()) { clearMacroInfo(IdentInfo); } diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index 155074c9f81..8209c3c1365 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -306,14 +306,15 @@ StringRef Preprocessor::getLastMacroWithSpelling( StringRef BestSpelling; for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end(); I != E; ++I) { - if (!I->second->isObjectLike()) + if (!I->second->getInfo()->isObjectLike()) continue; - const MacroInfo *MI = I->second->findDefinitionAtLoc(Loc, SourceMgr); - if (!MI) + const MacroDirective * + MD = I->second->findDirectiveAtLoc(Loc, SourceMgr); + if (!MD) continue; - if (!MacroDefinitionEquals(MI, Tokens)) + if (!MacroDefinitionEquals(MD->getInfo(), Tokens)) continue; - SourceLocation Location = I->second->getDefinitionLoc(); + SourceLocation Location = I->second->getInfo()->getDefinitionLoc(); // Choose the macro defined latest. if (BestLocation.isInvalid() || (Location.isValid() && diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 5e5011b7f68..a52d1f27517 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -49,7 +49,8 @@ PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context, PrintingPolicy Policy = Context.getPrintingPolicy(); Policy.Bool = Context.getLangOpts().Bool; if (!Policy.Bool) { - if (MacroInfo *BoolMacro = PP.getMacroInfo(&Context.Idents.get("bool"))) { + if (const MacroInfo * + BoolMacro = PP.getMacroInfo(&Context.Idents.get("bool"))) { Policy.Bool = BoolMacro->isObjectLike() && BoolMacro->getNumTokens() == 1 && BoolMacro->getReplacementToken(0).is(tok::kw__Bool); diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp index f894a0ba279..2cc7b85a7f5 100644 --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -2552,8 +2552,9 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx, } if (Kind == RK_Macro) { - MacroInfo *MI = PP.getMacroInfoHistory(Macro); - assert(MI && "Not a macro?"); + const MacroDirective *MD = PP.getMacroDirectiveHistory(Macro); + assert(MD && "Not a macro?"); + const MacroInfo *MI = MD->getInfo(); Result.AddTypedTextChunk( Result.getAllocator().CopyString(Macro->getName())); diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 7c394ab2e2c..f8e1c3ed9ca 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -1062,7 +1062,7 @@ bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) { } void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, - MacroInfo *Hint) { + MacroDirective *Hint) { BitstreamCursor &Stream = F.MacroCursor; // Keep track of where we are in the stream, then jump back there @@ -1078,16 +1078,16 @@ void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, // adding tokens. struct AddLoadedMacroInfoRAII { Preprocessor &PP; - MacroInfo *Hint; - MacroInfo *MI; + MacroDirective *Hint; + MacroDirective *MD; IdentifierInfo *II; - AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint) - : PP(PP), Hint(Hint), MI(), II() { } + AddLoadedMacroInfoRAII(Preprocessor &PP, MacroDirective *Hint) + : PP(PP), Hint(Hint), MD(), II() { } ~AddLoadedMacroInfoRAII( ) { - if (MI) { + if (MD) { // Finally, install the macro. - PP.addLoadedMacroInfo(II, MI, Hint); + PP.addLoadedMacroInfo(II, MD, Hint); } } } AddLoadedMacroInfo(PP, Hint); @@ -1140,20 +1140,22 @@ void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, unsigned NextIndex = 3; SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); MacroInfo *MI = PP.AllocateMacroInfo(Loc); + // FIXME: Location should be import location in case of module. + MacroDirective *MD = PP.AllocateMacroDirective(MI, Loc, + /*isImported=*/true); MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); // Record this macro. - MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI; + MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MD; SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex); if (UndefLoc.isValid()) - MI->setUndefLoc(UndefLoc); + MD->setUndefLoc(UndefLoc); MI->setIsUsed(Record[NextIndex++]); - MI->setIsFromAST(); bool IsPublic = Record[NextIndex++]; - MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex)); + MD->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex)); if (RecType == PP_MACRO_FUNCTION_LIKE) { // Decode function-like macro info. @@ -1175,13 +1177,13 @@ void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, } if (DeserializationListener) - DeserializationListener->MacroRead(GlobalID, MI); + DeserializationListener->MacroRead(GlobalID, MD); // If an update record marked this as undefined, do so now. // FIXME: Only if the submodule this update came from is visible? MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID); if (Update != MacroUpdates.end()) { - if (MI->getUndefLoc().isInvalid()) { + if (MD->getUndefLoc().isInvalid()) { for (unsigned I = 0, N = Update->second.size(); I != N; ++I) { bool Hidden = false; if (unsigned SubmoduleID = Update->second[I].first) { @@ -1192,15 +1194,15 @@ void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, // Record this hiding for later. HiddenNamesMap[Owner].push_back( - HiddenName(II, MI, Update->second[I].second.UndefLoc)); + HiddenName(II, MD, Update->second[I].second.UndefLoc)); } } } if (!Hidden) { - MI->setUndefLoc(Update->second[I].second.UndefLoc); + MD->setUndefLoc(Update->second[I].second.UndefLoc); if (PPMutationListener *Listener = PP.getPPMutationListener()) - Listener->UndefinedMacro(MI); + Listener->UndefinedMacro(MD); break; } } @@ -1209,7 +1211,7 @@ void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, } // Determine whether this macro definition is visible. - bool Hidden = !MI->isPublic(); + bool Hidden = !MD->isPublic(); if (!Hidden && GlobalSubmoduleID) { if (Module *Owner = getSubmodule(GlobalSubmoduleID)) { if (Owner->NameVisibility == Module::Hidden) { @@ -1219,14 +1221,14 @@ void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, // Note that this macro definition was hidden because its owning // module is not yet visible. - HiddenNamesMap[Owner].push_back(HiddenName(II, MI)); + HiddenNamesMap[Owner].push_back(HiddenName(II, MD)); } } } - MI->setHidden(Hidden); + MD->setHidden(Hidden); // Make sure we install the macro once we're done. - AddLoadedMacroInfo.MI = MI; + AddLoadedMacroInfo.MD = MD; AddLoadedMacroInfo.II = II; // Remember that we saw this macro last so that we add the tokens that @@ -2639,7 +2641,7 @@ void ASTReader::makeNamesVisible(const HiddenNames &Names) { break; } case HiddenName::MacroVisibility: { - std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro(); + std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro(); Macro.second->setHidden(!Macro.second->isPublic()); if (Macro.second->isDefined()) { PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second); @@ -2648,7 +2650,7 @@ void ASTReader::makeNamesVisible(const HiddenNames &Names) { } case HiddenName::MacroUndef: { - std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro(); + std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro(); if (Macro.second->isDefined()) { Macro.second->setUndefLoc(Names[I].getMacroUndefLoc()); if (PPMutationListener *Listener = PP.getPPMutationListener()) @@ -5638,7 +5640,7 @@ void ASTReader::PrintStats() { unsigned NumMacrosLoaded = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), MacrosLoaded.end(), - (MacroInfo *)0); + (MacroDirective *)0); unsigned NumSelectorsLoaded = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), SelectorsLoaded.end(), @@ -6247,7 +6249,7 @@ IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { return LocalID + I->second; } -MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) { +MacroDirective *ASTReader::getMacro(MacroID ID, MacroDirective *Hint) { if (ID == 0) return 0; @@ -6979,7 +6981,7 @@ void ASTReader::finishPendingActions() { for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { // FIXME: std::move here SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second; - MacroInfo *Hint = 0; + MacroDirective *Hint = 0; for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; ++IDIdx) { Hint = getMacro(GlobalIDs[IDIdx], Hint); diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 8747bc85122..f33fbc23d8d 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -1746,7 +1746,7 @@ void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) { // emitting each to the PP section. // Construct the list of macro definitions that need to be serialized. - SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2> + SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2> MacrosToEmit; llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen; for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0), @@ -1774,19 +1774,19 @@ void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) { for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) { const IdentifierInfo *Name = MacrosToEmit[I].first; - for (MacroInfo *MI = MacrosToEmit[I].second; MI; - MI = MI->getPreviousDefinition()) { - MacroID ID = getMacroRef(MI); + for (MacroDirective *MD = MacrosToEmit[I].second; MD; + MD = MD->getPrevious()) { + MacroID ID = getMacroRef(MD); if (!ID) continue; // Skip macros from a AST file if we're chaining. - if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad()) + if (Chain && MD->isImported() && !MD->hasChangedAfterLoad()) continue; if (ID < FirstMacroID) { // This will have been dealt with via an update record. - assert(MacroUpdates.count(MI) > 0 && "Missing macro update"); + assert(MacroUpdates.count(MD) > 0 && "Missing macro update"); continue; } @@ -1802,14 +1802,15 @@ void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) { } AddIdentifierRef(Name, Record); - addMacroRef(MI, Record); + addMacroRef(MD, Record); + const MacroInfo *MI = MD->getInfo(); Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc())); AddSourceLocation(MI->getDefinitionLoc(), Record); AddSourceLocation(MI->getDefinitionEndLoc(), Record); - AddSourceLocation(MI->getUndefLoc(), Record); + AddSourceLocation(MD->getUndefLoc(), Record); Record.push_back(MI->isUsed()); - Record.push_back(MI->isPublic()); - AddSourceLocation(MI->getVisibilityLocation(), Record); + Record.push_back(MD->isPublic()); + AddSourceLocation(MD->getVisibilityLocation(), Record); unsigned Code; if (MI->isObjectLike()) { Code = PP_MACRO_OBJECT_LIKE; @@ -2643,7 +2644,7 @@ class ASTIdentifierTableTrait { /// \brief Determines whether this is an "interesting" identifier /// that needs a full IdentifierInfo structure written into the hash /// table. - bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) { + bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) { if (II->isPoisoned() || II->isExtensionToken() || II->getObjCOrBuiltinID() || @@ -2654,12 +2655,13 @@ class ASTIdentifierTableTrait { return hadMacroDefinition(II, Macro); } - bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) { + bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) { if (!II->hadMacroDefinition()) return false; - if (Macro || (Macro = PP.getMacroInfoHistory(II))) - return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic()); + if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) + return !Macro->getInfo()->isBuiltinMacro() && + (!IsModule || Macro->isPublic()); return false; } @@ -2683,12 +2685,12 @@ public: EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) { unsigned KeyLen = II->getLength() + 1; unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 - MacroInfo *Macro = 0; + MacroDirective *Macro = 0; if (isInterestingIdentifier(II, Macro)) { DataLen += 2; // 2 bytes for builtin ID DataLen += 2; // 2 bytes for flags if (hadMacroDefinition(II, Macro)) { - for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) { + for (MacroDirective *M = Macro; M; M = M->getPrevious()) { if (Writer.getMacroRef(M) != 0) DataLen += 4; } @@ -2719,7 +2721,7 @@ public: void EmitData(raw_ostream& Out, IdentifierInfo* II, IdentID ID, unsigned) { - MacroInfo *Macro = 0; + MacroDirective *Macro = 0; if (!isInterestingIdentifier(II, Macro)) { clang::io::Emit32(Out, ID << 1); return; @@ -2740,7 +2742,7 @@ public: if (HadMacroDefinition) { // Write all of the macro IDs associated with this identifier. - for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) { + for (MacroDirective *M = Macro; M; M = M->getPrevious()) { if (MacroID ID = Writer.getMacroRef(M)) clang::io::Emit32(Out, ID); } @@ -3943,8 +3945,8 @@ void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Recor Record.push_back(getIdentifierRef(II)); } -void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) { - Record.push_back(getMacroRef(MI)); +void ASTWriter::addMacroRef(MacroDirective *MD, RecordDataImpl &Record) { + Record.push_back(getMacroRef(MD)); } IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { @@ -3957,14 +3959,14 @@ IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { return ID; } -MacroID ASTWriter::getMacroRef(MacroInfo *MI) { +MacroID ASTWriter::getMacroRef(MacroDirective *MD) { // Don't emit builtin macros like __LINE__ to the AST file unless they // have been redefined by the header (in which case they are not // isBuiltinMacro). - if (MI == 0 || MI->isBuiltinMacro()) + if (MD == 0 || MD->getInfo()->isBuiltinMacro()) return 0; - MacroID &ID = MacroIDs[MI]; + MacroID &ID = MacroIDs[MD]; if (ID == 0) ID = NextMacroID++; return ID; @@ -4709,9 +4711,9 @@ void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) { StoredID = ID; } -void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) { +void ASTWriter::MacroRead(serialization::MacroID ID, MacroDirective *MD) { // Always keep the highest ID. See \p TypeRead() for more information. - MacroID &StoredID = MacroIDs[MI]; + MacroID &StoredID = MacroIDs[MD]; if (ID > StoredID) StoredID = ID; } @@ -4745,8 +4747,8 @@ void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) { SubmoduleIDs[Mod] = ID; } -void ASTWriter::UndefinedMacro(MacroInfo *MI) { - MacroUpdates[MI].UndefLoc = MI->getUndefLoc(); +void ASTWriter::UndefinedMacro(MacroDirective *MD) { + MacroUpdates[MD].UndefLoc = MD->getUndefLoc(); } void ASTWriter::CompletedTagDefinition(const TagDecl *D) { diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index e8e3f1d31e6..368301e9632 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -6289,11 +6289,11 @@ MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II, ASTUnit *Unit = cxtu::getASTUnit(TU); Preprocessor &PP = Unit->getPreprocessor(); - MacroInfo *MI = PP.getMacroInfoHistory(&II); - while (MI) { - if (MacroDefLoc == MI->getDefinitionLoc()) - return MI; - MI = MI->getPreviousDefinition(); + MacroDirective *MD = PP.getMacroDirectiveHistory(&II); + while (MD) { + if (MacroDefLoc == MD->getInfo()->getDefinitionLoc()) + return MD->getInfo(); + MD = MD->getPrevious(); } return 0; @@ -6345,11 +6345,11 @@ MacroDefinition *cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, if (std::find(MI->arg_begin(), MI->arg_end(), &II) != MI->arg_end()) return 0; - MacroInfo *InnerMI = PP.getMacroInfoHistory(&II); - if (!InnerMI) + MacroDirective *InnerMD = PP.getMacroDirectiveHistory(&II); + if (!InnerMD) return 0; - return PPRec->findMacroDefinition(InnerMI); + return PPRec->findMacroDefinition(InnerMD->getInfo()); } MacroDefinition *cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, |