r235669 - [modules] Refactor creation of ModuleMacros and create them when importing from local submodules.

Richard Smith richard-llvm at metafoo.co.uk
Thu Apr 23 16:29:05 PDT 2015


Author: rsmith
Date: Thu Apr 23 18:29:05 2015
New Revision: 235669

URL: http://llvm.org/viewvc/llvm-project?rev=235669&view=rev
Log:
[modules] Refactor creation of ModuleMacros and create them when importing from local submodules.

Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/lib/Lex/PPLexerChange.cpp
    cfe/trunk/lib/Serialization/ASTReader.cpp

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=235669&r1=235668&r2=235669&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Thu Apr 23 18:29:05 2015
@@ -428,11 +428,14 @@ class Preprocessor : public RefCountedBa
 
   /// \brief Information about a submodule that we're currently building.
   struct BuildingSubmoduleInfo {
-    BuildingSubmoduleInfo(Module *M) : M(M) {}
+    BuildingSubmoduleInfo(Module *M, SourceLocation ImportLoc)
+        : M(M), ImportLoc(ImportLoc) {}
 
-    // The module that we are building.
+    /// The module that we are building.
     Module *M;
-    // The macros that were visible before we entered the module.
+    /// The location at which the module was included.
+    SourceLocation ImportLoc;
+    /// The macros that were visible before we entered the module.
     MacroMap Macros;
 
     // FIXME: VisibleModules?
@@ -441,7 +444,7 @@ class Preprocessor : public RefCountedBa
   };
   SmallVector<BuildingSubmoduleInfo, 8> BuildingSubmoduleStack;
 
-  void EnterSubmodule(Module *M);
+  void EnterSubmodule(Module *M, SourceLocation ImportLoc);
   void LeaveSubmodule();
 
   /// The set of known macros exported from modules.
@@ -715,9 +718,8 @@ public:
   /// \brief Add a directive to the macro directive history for this identifier.
   void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD);
   DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI,
-                                             SourceLocation Loc,
-                                             ModuleMacro *MM = nullptr) {
-    DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc, MM);
+                                             SourceLocation Loc) {
+    DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc);
     appendMacroDirective(II, MD);
     return MD;
   }
@@ -1509,13 +1511,14 @@ private:
   MacroInfo *AllocateMacroInfo();
 
   DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI,
-                                               SourceLocation Loc,
-                                               ModuleMacro *MM = nullptr);
-  UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc,
-                                                   ModuleMacro *MM = nullptr);
+                                               SourceLocation Loc);
+  UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc);
   VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc,
                                                              bool isPublic);
 
+  MacroDirective *AllocateImportedMacroDirective(ModuleMacro *MM,
+                                                 SourceLocation Loc);
+
   /// \brief Lex and validate a macro name, which occurs after a
   /// \#define or \#undef.
   ///

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=235669&r1=235668&r2=235669&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Thu Apr 23 18:29:05 2015
@@ -62,17 +62,13 @@ MacroInfo *Preprocessor::AllocateDeseria
   return MI;
 }
 
-DefMacroDirective *
-Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc,
-    ModuleMacro *MM) {
-  if (MM) return DefMacroDirective::createImported(*this, MI, Loc, MM);
+DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
+                                                           SourceLocation Loc) {
   return new (BP) DefMacroDirective(MI, Loc);
 }
 
 UndefMacroDirective *
-Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc,
-                                          ModuleMacro *MM) {
-  if (MM) return UndefMacroDirective::createImported(*this, UndefLoc, MM);
+Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
   return new (BP) UndefMacroDirective(UndefLoc);
 }
 
@@ -82,6 +78,15 @@ Preprocessor::AllocateVisibilityMacroDir
   return new (BP) VisibilityMacroDirective(Loc, isPublic);
 }
 
+MacroDirective *
+Preprocessor::AllocateImportedMacroDirective(ModuleMacro *MM,
+                                             SourceLocation Loc) {
+  if (auto *MI = MM->getMacroInfo())
+    return DefMacroDirective::createImported(*this, MI, Loc, MM);
+  else
+    return UndefMacroDirective::createImported(*this, Loc, MM);
+}
+
 /// \brief Read and discard all tokens remaining on the current line until
 /// the tok::eod token is found.
 void Preprocessor::DiscardUntilEndOfDirective() {
@@ -1784,7 +1789,7 @@ void Preprocessor::HandleIncludeDirectiv
     assert(!CurSubmodule && "should not have marked this as a module yet");
     CurSubmodule = BuildingModule.getModule();
 
-    EnterSubmodule(CurSubmodule);
+    EnterSubmodule(CurSubmodule, HashLoc);
 
     EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin,
                          CurSubmodule);

Modified: cfe/trunk/lib/Lex/PPLexerChange.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPLexerChange.cpp?rev=235669&r1=235668&r2=235669&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPLexerChange.cpp (original)
+++ cfe/trunk/lib/Lex/PPLexerChange.cpp Thu Apr 23 18:29:05 2015
@@ -609,9 +609,9 @@ void Preprocessor::HandleMicrosoftCommen
   assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode");
 }
 
-void Preprocessor::EnterSubmodule(Module *M) {
+void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc) {
   // Save the current state for future imports.
-  BuildingSubmoduleStack.push_back(BuildingSubmoduleInfo(M));
+  BuildingSubmoduleStack.push_back(BuildingSubmoduleInfo(M, ImportLoc));
 
   auto &Info = BuildingSubmoduleStack.back();
   // Copy across our macros and start the submodule with the current state.
@@ -630,6 +630,7 @@ void Preprocessor::LeaveSubmodule() {
     // This module may have exported a new macro. If so, create a ModuleMacro
     // representing that fact.
     bool ExplicitlyPublic = false;
+    ModuleMacro *MM = nullptr;
     for (auto *MD = Macro.second.getLatest(); MD != State.getLatest();
          MD = MD->getPrevious()) {
       // Skip macros defined in other submodules we #included along the way.
@@ -653,8 +654,8 @@ void Preprocessor::LeaveSubmodule() {
         // FIXME: Issue a warning if multiple headers for the same submodule
         // define a macro, rather than silently ignoring all but the first.
         bool IsNew;
-        addModuleMacro(Info.M, II, Def, Macro.second.getOverriddenMacros(),
-                       IsNew);
+        MM = addModuleMacro(Info.M, II, Def, Macro.second.getOverriddenMacros(),
+                            IsNew);
         break;
       }
     }
@@ -664,6 +665,12 @@ void Preprocessor::LeaveSubmodule() {
 
     // Restore the old macro state.
     Macro.second = State;
+
+    // If our submodule defined a macro, import it.
+    // FIXME: Do this lazily.
+    if (MM)
+      appendMacroDirective(II,
+                           AllocateImportedMacroDirective(MM, Info.ImportLoc));
   }
 
   BuildingSubmoduleStack.pop_back();

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=235669&r1=235668&r2=235669&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Thu Apr 23 18:29:05 2015
@@ -1735,9 +1735,7 @@ struct ASTReader::ModuleMacroInfo {
   }
 
   MacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
-    if (auto *MI = MM->getMacroInfo())
-      return PP.AllocateDefMacroDirective(MI, ImportLoc, MM);
-    return PP.AllocateUndefMacroDirective(ImportLoc, MM);
+    return PP.AllocateImportedMacroDirective(MM, ImportLoc);
   }
 };
 
@@ -1843,16 +1841,17 @@ void ASTReader::resolvePendingMacro(Iden
       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;
+      MD = MM ? PP.AllocateImportedMacroDirective(MM, Loc)
+              : PP.AllocateDefMacroDirective(MI, Loc);
+      cast<DefMacroDirective>(MD)->setAmbiguous(IsAmbiguous);
       break;
     }
     case MacroDirective::MD_Undefine: {
       ModuleMacro *MM = nullptr;
       if (SubmoduleID ImportedFrom = getGlobalSubmoduleID(M, Record[Idx++]))
         MM = PP.getModuleMacro(getSubmodule(ImportedFrom), II);
-      MD = PP.AllocateUndefMacroDirective(Loc, MM);
+      MD = MM ? PP.AllocateImportedMacroDirective(MM, Loc)
+              : PP.AllocateUndefMacroDirective(Loc);
       break;
     }
     case MacroDirective::MD_Visibility:





More information about the cfe-commits mailing list