[cfe-commits] r145727 - in /cfe/trunk: 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

Douglas Gregor dgregor at apple.com
Fri Dec 2 15:42:12 PST 2011


Author: dgregor
Date: Fri Dec  2 17:42:12 2011
New Revision: 145727

URL: http://llvm.org/viewvc/llvm-project?rev=145727&view=rev
Log:
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.

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

Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=145727&r1=145726&r2=145727&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Fri Dec  2 17:42:12 2011
@@ -782,7 +782,8 @@
   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;
   }

Modified: cfe/trunk/include/clang/Frontend/CompilerInstance.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CompilerInstance.h?rev=145727&r1=145726&r2=145727&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/CompilerInstance.h (original)
+++ cfe/trunk/include/clang/Frontend/CompilerInstance.h Fri Dec  2 17:42:12 2011
@@ -642,7 +642,8 @@
   /// }
   
   virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
-                             Module::NameVisibilityKind Visibility);
+                             Module::NameVisibilityKind Visibility,
+                             bool IsInclusionDirective);
 };
 
 } // end namespace clang

Modified: cfe/trunk/include/clang/Lex/ModuleLoader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/ModuleLoader.h?rev=145727&r1=145726&r2=145727&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/ModuleLoader.h (original)
+++ cfe/trunk/include/clang/Lex/ModuleLoader.h Fri Dec  2 17:42:12 2011
@@ -48,10 +48,15 @@
   /// \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;
 };
   
 }

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=145727&r1=145726&r2=145727&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Fri Dec  2 17:42:12 2011
@@ -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;
 }

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=145727&r1=145726&r2=145727&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Fri Dec  2 17:42:12 2011
@@ -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;

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=145727&r1=145726&r2=145727&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Fri Dec  2 17:42:12 2011
@@ -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;
   }
   

Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=145727&r1=145726&r2=145727&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Fri Dec  2 17:42:12 2011
@@ -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) {

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=145727&r1=145726&r2=145727&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Dec  2 17:42:12 2011
@@ -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;
   





More information about the cfe-commits mailing list