When we treat an #include or #import as a module import, create an
implicit ImportDecl in the translation unit to record the presence of
the import.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145727 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index 2f167ee..1ed149b 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -2657,9 +2657,7 @@
                                        Module *Imported, 
                                        SourceLocation EndLoc) {
   void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
-  ImportDecl *Import
-    = new (Mem) ImportDecl(DC, ImportLoc, Imported, 
-                           ArrayRef<SourceLocation>(&EndLoc, 1));
+  ImportDecl *Import = new (Mem) ImportDecl(DC, ImportLoc, Imported, EndLoc);
   Import->setImplicit();
   return Import;
 }
diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp
index 746c00a..6a060e0 100644
--- a/lib/Frontend/CompilerInstance.cpp
+++ b/lib/Frontend/CompilerInstance.cpp
@@ -11,6 +11,7 @@
 #include "clang/Sema/Sema.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
@@ -1070,7 +1071,8 @@
 
 Module *CompilerInstance::loadModule(SourceLocation ImportLoc, 
                                      ModuleIdPath Path,
-                                     Module::NameVisibilityKind Visibility) {
+                                     Module::NameVisibilityKind Visibility,
+                                     bool IsInclusionDirective) {
   // If we've already handled this import, just return the cached result.
   // This one-element cache is important to eliminate redundant diagnostics
   // when both the preprocessor and parser see the same import declaration.
@@ -1259,8 +1261,16 @@
   }
   
   // Make the named module visible.
-  if (Module)
-    ModuleManager->makeModuleVisible(Module, Visibility);
+  ModuleManager->makeModuleVisible(Module, Visibility);
+
+  // If this module import was due to an inclusion directive, create an 
+  // implicit import declaration to capture it in the AST.
+  if (IsInclusionDirective && hasASTContext()) {
+    TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
+    TU->addDecl(ImportDecl::CreateImplicit(getASTContext(), TU,
+                                           ImportLoc, Module, 
+                                           Path.back().second));
+  }
   
   LastModuleImportLoc = ImportLoc;
   LastModuleImportResult = Module;
diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp
index 836d21b..b44a0a2 100644
--- a/lib/Lex/PPDirectives.cpp
+++ b/lib/Lex/PPDirectives.cpp
@@ -1363,7 +1363,8 @@
     // If this was an #__include_macros directive, only make macros visible.
     Module::NameVisibilityKind Visibility 
       = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible;
-    TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility);
+    TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
+                               /*IsIncludeDirective=*/true);
     return;
   }
   
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index 6cf34e2..dea7efc 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -599,7 +599,8 @@
   // If we have a non-empty module path, load the named module.
   if (!ModuleImportPath.empty())
     (void)TheModuleLoader.loadModule(ModuleImportLoc, ModuleImportPath,
-                                     Module::MacrosVisible);
+                                     Module::MacrosVisible,
+                                     /*IsIncludeDirective=*/false);
 }
 
 void Preprocessor::AddCommentHandler(CommentHandler *Handler) {
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 01c7906..fdb2cf5 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -9893,7 +9893,8 @@
 
 DeclResult Sema::ActOnModuleImport(SourceLocation ImportLoc, ModuleIdPath Path) {
   Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, 
-                                                Module::AllVisible);
+                                                Module::AllVisible,
+                                                /*IsIncludeDirective=*/false);
   if (!Mod)
     return true;