]> granicus.if.org Git - clang/commitdiff
When we treat an #include or #import as a module import, create an
authorDouglas Gregor <dgregor@apple.com>
Fri, 2 Dec 2011 23:42:12 +0000 (23:42 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 2 Dec 2011 23:42:12 +0000 (23:42 +0000)
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

include/clang/Frontend/ASTUnit.h
include/clang/Frontend/CompilerInstance.h
include/clang/Lex/ModuleLoader.h
lib/AST/Decl.cpp
lib/Frontend/CompilerInstance.cpp
lib/Lex/PPDirectives.cpp
lib/Lex/Preprocessor.cpp
lib/Sema/SemaDecl.cpp

index d65da5d67bd98b7f0a95fcc6c3ab6d75cc5e4feb..8feab307ea581f63659ee090458345cea9f78060 100644 (file)
@@ -782,7 +782,8 @@ public:
   bool serialize(raw_ostream &OS);
   
   virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
-                             Module::NameVisibilityKind Visibility) {
+                             Module::NameVisibilityKind Visibility,
+                             bool IsInclusionDirective) {
     // ASTUnit doesn't know how to load modules (not that this matters).
     return 0;
   }
index 21f8eeead3d6b74903df290ce6dc645b3fb236f3..b488cde6e6eaad48d87891194cff7ec9453d70e6 100644 (file)
@@ -642,7 +642,8 @@ public:
   /// }
   
   virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
-                             Module::NameVisibilityKind Visibility);
+                             Module::NameVisibilityKind Visibility,
+                             bool IsInclusionDirective);
 };
 
 } // end namespace clang
index 0bd97b6bd4b9e362e7445ac06f7527b03ba1b65c..b2fb722b94dcee31b6db836872b1158f6308097f 100644 (file)
@@ -48,10 +48,15 @@ public:
   /// \param Visibility The visibility provided for the names in the loaded
   /// module.
   ///
+  /// \param IsInclusionDirective Indicates that this module is being loaded
+  /// implicitly, due to the presence of an inclusion directive. Otherwise,
+  /// it is being loaded due to an import declaration.
+  ///
   /// \returns If successful, returns the loaded module. Otherwise, returns 
   /// NULL to indicate that the module could not be loaded.
   virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
-                             Module::NameVisibilityKind Visibility) = 0;
+                             Module::NameVisibilityKind Visibility,
+                             bool IsInclusionDirective) = 0;
 };
   
 }
index 2f167eedd152b34a8c18cb7a21bfd93032ee70ba..1ed149b68f2a077b58ad9358d250c6e51432596a 100644 (file)
@@ -2657,9 +2657,7 @@ ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
                                        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;
 }
index 746c00a3619ee4795a93aee2a79365215a4a7f23..6a060e036fbf8e75e40e7e6036bd17a7695faa5b 100644 (file)
@@ -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 @@ static void compileModule(CompilerInstance &ImportingInstance,
 
 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 @@ Module *CompilerInstance::loadModule(SourceLocation ImportLoc,
   }
   
   // 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;
index 836d21b20475595e6afe276cc93d651297ca41d1..b44a0a2934fc994ea5dc46f88b612c5cb3c4aa88 100644 (file)
@@ -1363,7 +1363,8 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
     // 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;
   }
   
index 6cf34e226afa18642e31a2399e42c05e20f06e47..dea7efc7658fecfa8e9b4a8e9aac469920458cbd 100644 (file)
@@ -599,7 +599,8 @@ void Preprocessor::LexAfterModuleImport(Token &Result) {
   // 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) {
index 01c7906e800022ee04ea925d66c3f5da8421cd36..fdb2cf5d9641c6e8c22c170493e18a6ca4975109 100644 (file)
@@ -9893,7 +9893,8 @@ Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr,
 
 DeclResult Sema::ActOnModuleImport(SourceLocation ImportLoc, ModuleIdPath Path) {
   Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, 
-                                                Module::AllVisible);
+                                                Module::AllVisible,
+                                                /*IsIncludeDirective=*/false);
   if (!Mod)
     return true;