r235644 - [modules] Store a ModuleMacro* on an imported macro directive rather than duplicating the info within it.

Richard Smith richard-llvm at metafoo.co.uk
Thu Apr 23 13:40:50 PDT 2015


Author: rsmith
Date: Thu Apr 23 15:40:50 2015
New Revision: 235644

URL: http://llvm.org/viewvc/llvm-project?rev=235644&view=rev
Log:
[modules] Store a ModuleMacro* on an imported macro directive rather than duplicating the info within it.

Modified:
    cfe/trunk/include/clang/Lex/MacroInfo.h
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/include/clang/Serialization/ASTReader.h
    cfe/trunk/lib/Lex/MacroInfo.cpp
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/lib/Lex/Pragma.cpp
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp

Modified: cfe/trunk/include/clang/Lex/MacroInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/MacroInfo.h?rev=235644&r1=235643&r2=235644&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/MacroInfo.h (original)
+++ cfe/trunk/include/clang/Lex/MacroInfo.h Thu Apr 23 15:40:50 2015
@@ -24,6 +24,7 @@
 
 namespace clang {
 class Module;
+class ModuleMacro;
 class Preprocessor;
 
 /// \brief Encapsulates the data about a macro definition (e.g. its tokens).
@@ -350,29 +351,20 @@ protected:
   /// \brief True if this macro was imported from a module.
   bool IsImported : 1;
 
-  /// \brief For an imported directive, the number of modules whose macros are
-  /// overridden by this directive. Only used if IsImported.
-  unsigned NumOverrides : 26;
-
-  unsigned *getModuleDataStart();
-  const unsigned *getModuleDataStart() const {
-    return const_cast<MacroDirective*>(this)->getModuleDataStart();
+  struct ImportData {
+    ModuleMacro *ImportedFrom;
+  };
+  ImportData *getImportData();
+  const ImportData *getImportData() const {
+    return const_cast<MacroDirective*>(this)->getImportData();
   }
 
   MacroDirective(Kind K, SourceLocation Loc,
-                 unsigned ImportedFromModuleID = 0,
-                 ArrayRef<unsigned> Overrides = None)
+                 ModuleMacro *ImportedFrom = nullptr)
       : Previous(nullptr), Loc(Loc), MDKind(K), IsFromPCH(false),
-        IsAmbiguous(false), IsPublic(true), IsImported(ImportedFromModuleID),
-        NumOverrides(Overrides.size()) {
-    assert(NumOverrides == Overrides.size() && "too many overrides");
-    assert((IsImported || !NumOverrides) && "overrides for non-module macro");
-
-    if (IsImported) {
-      unsigned *Extra = getModuleDataStart();
-      *Extra++ = ImportedFromModuleID;
-      std::copy(Overrides.begin(), Overrides.end(), Extra);
-    }
+        IsAmbiguous(false), IsPublic(true), IsImported(ImportedFrom) {
+    if (IsImported)
+      getImportData()->ImportedFrom = ImportedFrom;
   }
 
 public:
@@ -400,23 +392,14 @@ public:
   /// Note that this is never the case for a VisibilityMacroDirective.
   bool isImported() const { return IsImported; }
 
-  /// \brief If this directive was imported from a module, get the submodule
-  /// whose directive this is. Note that this may be different from the module
-  /// that owns the MacroInfo for a DefMacroDirective due to #pragma pop_macro
-  /// and similar effects.
-  unsigned getOwningModuleID() const {
+  /// \brief If this directive was imported from a module, get the module
+  /// macro from which this directive was created.
+  ModuleMacro *getOwningModuleMacro() const {
     if (isImported())
-      return *getModuleDataStart();
+      return getImportData()->ImportedFrom;
     return 0;
   }
 
-  /// \brief Get the module IDs of modules whose macros are overridden by this
-  /// directive. Only valid if this is an imported directive.
-  ArrayRef<unsigned> getOverriddenModules() const {
-    assert(IsImported && "can only get overridden modules for imported macro");
-    return llvm::makeArrayRef(getModuleDataStart() + 1, NumOverrides);
-  }
-
   class DefInfo {
     DefMacroDirective *DefDirective;
     SourceLocation UndefLoc;
@@ -488,20 +471,21 @@ public:
 class DefMacroDirective : public MacroDirective {
   MacroInfo *Info;
 
-public:
-  explicit DefMacroDirective(MacroInfo *MI)
-      : MacroDirective(MD_Define, MI->getDefinitionLoc()), Info(MI) {
-    assert(MI && "MacroInfo is null");
-  }
-
   DefMacroDirective(MacroInfo *MI, SourceLocation Loc,
-                    unsigned ImportedFromModuleID = 0,
-                    ArrayRef<unsigned> Overrides = None)
-      : MacroDirective(MD_Define, Loc, ImportedFromModuleID, Overrides),
-        Info(MI) {
+                    ModuleMacro *ImportedFrom)
+      : MacroDirective(MD_Define, Loc, ImportedFrom), Info(MI) {
     assert(MI && "MacroInfo is null");
   }
 
+public:
+  DefMacroDirective(MacroInfo *MI, SourceLocation Loc)
+      : DefMacroDirective(MI, Loc, nullptr) {}
+  explicit DefMacroDirective(MacroInfo *MI)
+      : DefMacroDirective(MI, MI->getDefinitionLoc()) {}
+  static DefMacroDirective *createImported(Preprocessor &PP, MacroInfo *MI,
+                                           SourceLocation Loc,
+                                           ModuleMacro *ImportedFrom);
+
   /// \brief The data for the macro definition.
   const MacroInfo *getInfo() const { return Info; }
   MacroInfo *getInfo() { return Info; }
@@ -521,14 +505,19 @@ public:
 
 /// \brief A directive for an undefined macro.
 class UndefMacroDirective : public MacroDirective  {
-public:
-  explicit UndefMacroDirective(SourceLocation UndefLoc,
-                               unsigned ImportedFromModuleID = 0,
-                               ArrayRef<unsigned> Overrides = None)
-      : MacroDirective(MD_Undefine, UndefLoc, ImportedFromModuleID, Overrides) {
-    assert((UndefLoc.isValid() || ImportedFromModuleID) && "Invalid UndefLoc!");
+  UndefMacroDirective(SourceLocation UndefLoc, ModuleMacro *ImportedFrom)
+      : MacroDirective(MD_Undefine, UndefLoc, ImportedFrom) {
+    // FIXME: We should have a valid UndefLoc even for an imported macro.
+    assert((UndefLoc.isValid() || ImportedFrom) && "Invalid UndefLoc!");
   }
 
+public:
+  explicit UndefMacroDirective(SourceLocation UndefLoc)
+      : UndefMacroDirective(UndefLoc, nullptr) {}
+  static UndefMacroDirective *createImported(Preprocessor &PP,
+                                             SourceLocation UndefLoc,
+                                             ModuleMacro *ImportedFrom);
+
   static bool classof(const MacroDirective *MD) {
     return MD->getKind() == MD_Undefine;
   }
@@ -539,7 +528,7 @@ public:
 class VisibilityMacroDirective : public MacroDirective  {
 public:
   explicit VisibilityMacroDirective(SourceLocation Loc, bool Public)
-    : MacroDirective(MD_Visibility, Loc) {
+      : MacroDirective(MD_Visibility, Loc) {
     IsPublic = Public;
   }
 
@@ -553,11 +542,12 @@ public:
   static bool classof(const VisibilityMacroDirective *) { return true; }
 };
 
-inline unsigned *MacroDirective::getModuleDataStart() {
+inline MacroDirective::ImportData *MacroDirective::getImportData() {
+  assert(IsImported && "only an imported macro has import data");
   if (auto *Def = dyn_cast<DefMacroDirective>(this))
-    return reinterpret_cast<unsigned*>(Def + 1);
+    return reinterpret_cast<ImportData*>(Def + 1);
   else
-    return reinterpret_cast<unsigned*>(cast<UndefMacroDirective>(this) + 1);
+    return reinterpret_cast<ImportData*>(cast<UndefMacroDirective>(this) + 1);
 }
 
 inline SourceLocation MacroDirective::DefInfo::getLocation() const {
@@ -639,8 +629,8 @@ public:
   overrides_iterator overrides_end() const {
     return overrides_begin() + NumOverrides;
   }
-  llvm::iterator_range<overrides_iterator> overrides() const {
-    return llvm::make_range(overrides_begin(), overrides_end());
+  ArrayRef<ModuleMacro *> overrides() const {
+    return llvm::makeArrayRef(overrides_begin(), overrides_end());
   }
   /// \}
 

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=235644&r1=235643&r2=235644&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Thu Apr 23 15:40:50 2015
@@ -716,15 +716,14 @@ public:
   void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD);
   DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI,
                                              SourceLocation Loc,
-                                             unsigned ImportedFromModuleID,
-                                             ArrayRef<unsigned> Overrides) {
-    DefMacroDirective *MD =
-        AllocateDefMacroDirective(MI, Loc, ImportedFromModuleID, Overrides);
+                                             ModuleMacro *MM = nullptr) {
+    DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc, MM);
     appendMacroDirective(II, MD);
     return MD;
   }
-  DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI){
-    return appendDefMacroDirective(II, MI, MI->getDefinitionLoc(), 0, None);
+  DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II,
+                                             MacroInfo *MI) {
+    return appendDefMacroDirective(II, MI, MI->getDefinitionLoc());
   }
   /// \brief Set a MacroDirective that was loaded from a PCH file.
   void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *MD);
@@ -1509,14 +1508,11 @@ private:
   /// \brief Allocate a new MacroInfo object.
   MacroInfo *AllocateMacroInfo();
 
-  DefMacroDirective *
-  AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc,
-                            unsigned ImportedFromModuleID = 0,
-                            ArrayRef<unsigned> Overrides = None);
-  UndefMacroDirective *
-  AllocateUndefMacroDirective(SourceLocation UndefLoc,
-                              unsigned ImportedFromModuleID = 0,
-                              ArrayRef<unsigned> Overrides = None);
+  DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI,
+                                               SourceLocation Loc,
+                                               ModuleMacro *MM = nullptr);
+  UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc,
+                                                   ModuleMacro *MM = nullptr);
   VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc,
                                                              bool isPublic);
 

Modified: cfe/trunk/include/clang/Serialization/ASTReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTReader.h?rev=235644&r1=235643&r2=235644&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTReader.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTReader.h Thu Apr 23 15:40:50 2015
@@ -73,6 +73,7 @@ class GlobalModuleIndex;
 class GotoStmt;
 class MacroDefinition;
 class MacroDirective;
+class ModuleMacro;
 class NamedDecl;
 class OpaqueValueExpr;
 class Preprocessor;
@@ -1856,14 +1857,13 @@ public:
   typedef llvm::TinyPtrVector<DefMacroDirective *> AmbiguousMacros;
   llvm::DenseMap<IdentifierInfo*, AmbiguousMacros> AmbiguousMacroDefs;
 
-  void
-  removeOverriddenMacros(IdentifierInfo *II, SourceLocation Loc,
-                         AmbiguousMacros &Ambig,
-                         ArrayRef<serialization::SubmoduleID> Overrides);
-
-  AmbiguousMacros *
-  removeOverriddenMacros(IdentifierInfo *II, SourceLocation Loc,
-                         ArrayRef<serialization::SubmoduleID> Overrides);
+  void removeOverriddenMacros(IdentifierInfo *II, SourceLocation Loc,
+                              AmbiguousMacros &Ambig,
+                              ArrayRef<ModuleMacro *> Overrides);
+
+  AmbiguousMacros *removeOverriddenMacros(IdentifierInfo *II,
+                                          SourceLocation Loc,
+                                          ArrayRef<ModuleMacro *> Overrides);
 
   /// \brief Retrieve the macro with the given ID.
   MacroInfo *getMacro(serialization::MacroID ID);

Modified: cfe/trunk/lib/Lex/MacroInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/MacroInfo.cpp?rev=235644&r1=235643&r2=235644&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/MacroInfo.cpp (original)
+++ cfe/trunk/lib/Lex/MacroInfo.cpp Thu Apr 23 15:40:50 2015
@@ -235,6 +235,25 @@ void MacroDirective::dump() const {
   Out << "\n";
 }
 
+DefMacroDirective *
+DefMacroDirective::createImported(Preprocessor &PP, MacroInfo *MI,
+                                  SourceLocation Loc,
+                                  ModuleMacro *ImportedFrom) {
+  void *Mem = PP.getPreprocessorAllocator().Allocate(
+      sizeof(DefMacroDirective) + sizeof(MacroDirective::ImportData),
+      llvm::alignOf<DefMacroDirective>());
+  return new (Mem) DefMacroDirective(MI, Loc, ImportedFrom);
+}
+
+UndefMacroDirective *
+UndefMacroDirective::createImported(Preprocessor &PP, SourceLocation Loc,
+                                    ModuleMacro *ImportedFrom) {
+  void *Mem = PP.getPreprocessorAllocator().Allocate(
+      sizeof(UndefMacroDirective) + sizeof(MacroDirective::ImportData),
+      llvm::alignOf<UndefMacroDirective>());
+  return new (Mem) UndefMacroDirective(Loc, ImportedFrom);
+}
+
 ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule,
                                  IdentifierInfo *II, MacroInfo *Macro,
                                  ArrayRef<ModuleMacro *> Overrides) {

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=235644&r1=235643&r2=235644&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Thu Apr 23 15:40:50 2015
@@ -64,24 +64,16 @@ MacroInfo *Preprocessor::AllocateDeseria
 
 DefMacroDirective *
 Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc,
-                                        unsigned ImportedFromModuleID,
-                                        ArrayRef<unsigned> Overrides) {
-  unsigned NumExtra = (ImportedFromModuleID ? 1 : 0) + Overrides.size();
-  return new (BP.Allocate(sizeof(DefMacroDirective) +
-                              sizeof(unsigned) * NumExtra,
-                          llvm::alignOf<DefMacroDirective>()))
-      DefMacroDirective(MI, Loc, ImportedFromModuleID, Overrides);
+    ModuleMacro *MM) {
+  if (MM) return DefMacroDirective::createImported(*this, MI, Loc, MM);
+  return new (BP) DefMacroDirective(MI, Loc);
 }
 
 UndefMacroDirective *
 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc,
-                                          unsigned ImportedFromModuleID,
-                                          ArrayRef<unsigned> Overrides) {
-  unsigned NumExtra = (ImportedFromModuleID ? 1 : 0) + Overrides.size();
-  return new (BP.Allocate(sizeof(UndefMacroDirective) +
-                              sizeof(unsigned) * NumExtra,
-                          llvm::alignOf<UndefMacroDirective>()))
-      UndefMacroDirective(UndefLoc, ImportedFromModuleID, Overrides);
+                                          ModuleMacro *MM) {
+  if (MM) return UndefMacroDirective::createImported(*this, UndefLoc, MM);
+  return new (BP) UndefMacroDirective(UndefLoc);
 }
 
 VisibilityMacroDirective *

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=235644&r1=235643&r2=235644&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Thu Apr 23 15:40:50 2015
@@ -65,21 +65,18 @@ void Preprocessor::appendMacroDirective(
       return;
 
     for (auto *PrevMD = OldMD; PrevMD; PrevMD = PrevMD->getPrevious()) {
-      // FIXME: Store a ModuleMacro * on an imported directive.
       Module *DirectiveMod = getModuleForLocation(PrevMD->getLocation());
-      Module *PrevOwningMod =
-          PrevMD->isImported()
-              ? getExternalSource()->getModule(PrevMD->getOwningModuleID())
-              : DirectiveMod;
-      auto *MM = getModuleMacro(PrevOwningMod, II);
-      if (!MM) {
+      if (ModuleMacro *PrevMM = PrevMD->getOwningModuleMacro())
+        StoredMD.addOverriddenMacro(*this, PrevMM);
+      else if (ModuleMacro *PrevMM = getModuleMacro(DirectiveMod, II))
+        // The previous macro was from another submodule that we #included.
+        // FIXME: Create an import directive when importing a macro from a local
+        // submodule.
+        StoredMD.addOverriddenMacro(*this, PrevMM);
+      else
         // We're still within the module defining the previous macro. We don't
         // override it.
-        assert(!PrevMD->isImported() &&
-               "imported macro with no corresponding ModuleMacro");
         break;
-      }
-      StoredMD.addOverriddenMacro(*this, MM);
 
       // Stop once we leave the original macro's submodule.
       //

Modified: cfe/trunk/lib/Lex/Pragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Pragma.cpp?rev=235644&r1=235643&r2=235644&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Pragma.cpp (original)
+++ cfe/trunk/lib/Lex/Pragma.cpp Thu Apr 23 15:40:50 2015
@@ -600,11 +600,9 @@ void Preprocessor::HandlePragmaPopMacro(
     // Get the MacroInfo we want to reinstall.
     MacroInfo *MacroToReInstall = iter->second.back();
 
-    if (MacroToReInstall) {
+    if (MacroToReInstall)
       // Reinstall the previously pushed macro.
-      appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
-                              /*isImported=*/false, /*Overrides*/None);
-    }
+      appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc);
 
     // Pop PragmaPushMacroInfo stack.
     iter->second.pop_back();

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=235644&r1=235643&r2=235644&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Thu Apr 23 15:40:50 2015
@@ -1724,24 +1724,20 @@ void ASTReader::markIdentifierUpToDate(I
 }
 
 struct ASTReader::ModuleMacroInfo {
-  SubmoduleID SubModID;
-  MacroInfo *MI;
-  ArrayRef<SubmoduleID> Overrides;
+  ModuleMacro *MM;
   // FIXME: Remove this.
   ModuleFile *F;
 
-  bool isDefine() const { return MI; }
+  bool isDefine() const { return MM->getMacroInfo(); }
 
-  SubmoduleID getSubmoduleID() const { return SubModID; }
-
-  ArrayRef<SubmoduleID> getOverriddenSubmodules() const { return Overrides; }
+  ArrayRef<ModuleMacro *> getOverriddenMacros() const {
+    return MM->overrides();
+  }
 
   MacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
-    if (!MI)
-      return PP.AllocateUndefMacroDirective(ImportLoc, SubModID,
-                                            getOverriddenSubmodules());
-    return PP.AllocateDefMacroDirective(MI, ImportLoc, SubModID,
-                                        getOverriddenSubmodules());
+    if (auto *MI = MM->getMacroInfo())
+      return PP.AllocateDefMacroDirective(MI, ImportLoc, MM);
+    return PP.AllocateUndefMacroDirective(ImportLoc, MM);
   }
 };
 
@@ -1753,7 +1749,12 @@ void ASTReader::resolvePendingMacro(Iden
   SavedStreamPosition SavedPosition(Cursor);
   Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
 
-  llvm::SmallVector<ModuleMacroInfo, 8> ModuleMacros;
+  struct ModuleMacroRecord {
+    SubmoduleID SubModID;
+    MacroInfo *MI;
+    SmallVector<SubmoduleID, 8> Overrides;
+  };
+  llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
 
   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
@@ -1773,20 +1774,12 @@ void ASTReader::resolvePendingMacro(Iden
       break;
 
     case PP_MODULE_MACRO: {
-      ModuleMacroInfo Info;
+      ModuleMacros.push_back(ModuleMacroRecord());
+      auto &Info = ModuleMacros.back();
       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
-      Info.F = &M;
-
-      if (Record.size() > 2) {
-        auto *Overrides = new (Context) SubmoduleID[Record.size() - 2];
-        for (int I = 2, N = Record.size(); I != N; ++I)
-          Overrides[I - 2] = getGlobalSubmoduleID(M, Record[I]);
-        Info.Overrides =
-            llvm::makeArrayRef(Overrides, Overrides + Record.size() - 2);
-      }
-
-      ModuleMacros.push_back(Info);
+      for (int I = 2, N = Record.size(); I != N; ++I)
+        Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
       continue;
     }
 
@@ -1804,9 +1797,9 @@ void ASTReader::resolvePendingMacro(Iden
   {
     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
     llvm::SmallVector<ModuleMacro*, 8> Overrides;
-    for (auto &MMI : ModuleMacros) {
+    for (auto &MMR : ModuleMacros) {
       Overrides.clear();
-      for (unsigned ModID : MMI.Overrides) {
+      for (unsigned ModID : MMR.Overrides) {
         Module *Mod = getSubmodule(ModID);
         auto *Macro = PP.getModuleMacro(Mod, II);
         assert(Macro && "missing definition for overridden macro");
@@ -1814,11 +1807,12 @@ void ASTReader::resolvePendingMacro(Iden
       }
 
       bool Inserted = false;
-      Module *Owner = getSubmodule(MMI.getSubmoduleID());
-      PP.addModuleMacro(Owner, II, MMI.MI, Overrides, Inserted);
+      Module *Owner = getSubmodule(MMR.SubModID);
+      auto *MM = PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
       if (!Inserted)
         continue;
 
+      ModuleMacroInfo MMI = { MM, &M };
       if (Owner->NameVisibility == Module::Hidden) {
         // Macros in the owning module are hidden. Just remember this macro to
         // install if we make this module visible.
@@ -1844,35 +1838,21 @@ void ASTReader::resolvePendingMacro(Iden
     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
     switch (K) {
     case MacroDirective::MD_Define: {
-      GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
-      MacroInfo *MI = getMacro(GMacID);
-      SubmoduleID ImportedFrom = getGlobalSubmoduleID(M, Record[Idx++]);
+      MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
       bool IsAmbiguous = Record[Idx++];
-      llvm::SmallVector<unsigned, 4> Overrides;
-      if (ImportedFrom) {
-        Overrides.insert(Overrides.end(),
-                         &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
-        for (auto &ID : Overrides)
-          ID = getGlobalSubmoduleID(M, ID);
-        Idx += Overrides.size() + 1;
-      }
-      DefMacroDirective *DefMD =
-          PP.AllocateDefMacroDirective(MI, Loc, ImportedFrom, Overrides);
+      ModuleMacro *MM = nullptr;
+      if (SubmoduleID ImportedFrom = getGlobalSubmoduleID(M, Record[Idx++]))
+        MM = PP.getModuleMacro(getSubmodule(ImportedFrom), II);
+      DefMacroDirective *DefMD = PP.AllocateDefMacroDirective(MI, Loc, MM);
       DefMD->setAmbiguous(IsAmbiguous);
       MD = DefMD;
       break;
     }
     case MacroDirective::MD_Undefine: {
-      SubmoduleID ImportedFrom = getGlobalSubmoduleID(M, Record[Idx++]);
-      llvm::SmallVector<unsigned, 4> Overrides;
-      if (ImportedFrom) {
-        Overrides.insert(Overrides.end(),
-                         &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
-        for (auto &ID : Overrides)
-          ID = getGlobalSubmoduleID(M, ID);
-        Idx += Overrides.size() + 1;
-      }
-      MD = PP.AllocateUndefMacroDirective(Loc, ImportedFrom, Overrides);
+      ModuleMacro *MM = nullptr;
+      if (SubmoduleID ImportedFrom = getGlobalSubmoduleID(M, Record[Idx++]))
+        MM = PP.getModuleMacro(getSubmodule(ImportedFrom), II);
+      MD = PP.AllocateUndefMacroDirective(Loc, MM);
       break;
     }
     case MacroDirective::MD_Visibility:
@@ -1912,13 +1892,11 @@ static bool areDefinedInSystemModules(Ma
 void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
                                        SourceLocation ImportLoc,
                                        AmbiguousMacros &Ambig,
-                                       ArrayRef<SubmoduleID> Overrides) {
-  for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
-    SubmoduleID OwnerID = Overrides[OI];
-
+                                       ArrayRef<ModuleMacro *> Overrides) {
+  for (ModuleMacro *Overridden : Overrides) {
+    Module *Owner = Overridden->getOwningModule();
     // If this macro is not yet visible, remove it from the hidden names list.
     // It won't be there if we're in the middle of making the owner visible.
-    Module *Owner = getSubmodule(OwnerID);
     auto HiddenIt = HiddenNamesMap.find(Owner);
     if (HiddenIt != HiddenNamesMap.end()) {
       HiddenNames &Hidden = HiddenIt->second;
@@ -1927,7 +1905,7 @@ void ASTReader::removeOverriddenMacros(I
         // Register the macro now so we don't lose it when we re-export.
         PP.appendMacroDirective(II, HI->second->import(PP, ImportLoc));
 
-        auto SubOverrides = HI->second->getOverriddenSubmodules();
+        auto SubOverrides = HI->second->getOverriddenMacros();
         Hidden.HiddenMacros.erase(HI);
         removeOverriddenMacros(II, ImportLoc, Ambig, SubOverrides);
       }
@@ -1936,7 +1914,7 @@ void ASTReader::removeOverriddenMacros(I
     // If this macro is already in our list of conflicts, remove it from there.
     Ambig.erase(
         std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
-          return MD->getInfo()->getOwningModuleID() == OwnerID;
+          return getSubmodule(MD->getInfo()->getOwningModuleID()) == Owner;
         }),
         Ambig.end());
   }
@@ -1945,7 +1923,7 @@ void ASTReader::removeOverriddenMacros(I
 ASTReader::AmbiguousMacros *
 ASTReader::removeOverriddenMacros(IdentifierInfo *II,
                                   SourceLocation ImportLoc,
-                                  ArrayRef<SubmoduleID> Overrides) {
+                                  ArrayRef<ModuleMacro *> Overrides) {
   MacroDirective *Prev = PP.getMacroDirective(II);
   if (!Prev && Overrides.empty())
     return nullptr;
@@ -1997,7 +1975,7 @@ void ASTReader::installImportedMacro(Ide
   }
 
   AmbiguousMacros *Prev =
-      removeOverriddenMacros(II, ImportLoc, MMI.getOverriddenSubmodules());
+      removeOverriddenMacros(II, ImportLoc, MMI.getOverriddenMacros());
 
   // Create a synthetic macro definition corresponding to the import (or null
   // if this was an undefinition of the macro).

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=235644&r1=235643&r2=235644&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Thu Apr 23 15:40:50 2015
@@ -2068,20 +2068,17 @@ void ASTWriter::WritePreprocessor(const
       if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
         MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
         Record.push_back(InfoID);
-        Record.push_back(DefMD->getOwningModuleID());
         Record.push_back(DefMD->isAmbiguous());
-      } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
-        Record.push_back(UndefMD->getOwningModuleID());
-      } else {
-        auto *VisMD = cast<VisibilityMacroDirective>(MD);
+      } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
         Record.push_back(VisMD->isPublic());
+        // No owning module macro.
+        continue;
       }
 
-      if (MD->isImported()) {
-        auto Overrides = MD->getOverriddenModules();
-        Record.push_back(Overrides.size());
-        Record.append(Overrides.begin(), Overrides.end());
-      }
+      if (auto *MM = MD->getOwningModuleMacro())
+        Record.push_back(getSubmoduleID(MM->getOwningModule()));
+      else
+        Record.push_back(0);
     }
 
     // Write out any exported module macros.





More information about the cfe-commits mailing list