]> granicus.if.org Git - clang/commitdiff
[modules] When building a dependency file, include module maps parsed in the
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 9 Aug 2015 04:46:57 +0000 (04:46 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 9 Aug 2015 04:46:57 +0000 (04:46 +0000)
current compilation, not just those from imported modules.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@244413 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Lex/ModuleMap.h
lib/Frontend/DependencyFile.cpp
lib/Lex/ModuleMap.cpp
test/Modules/dependency-gen-pch.m
test/Modules/dependency-gen.m
test/Modules/dependency-gen.modulemap
test/Modules/relative-dep-gen.cpp

index 0b03c4aa1cd763936122f48e570953c6c70f5ffc..2365a895a05daeb86df222e5d6f1c308a9ad002d 100644 (file)
@@ -35,6 +35,22 @@ class DiagnosticConsumer;
 class DiagnosticsEngine;
 class HeaderSearch;
 class ModuleMapParser;
+
+/// \brief A mechanism to observe the actions of the module map parser as it
+/// reads module map files.
+class ModuleMapCallbacks {
+public:
+  virtual ~ModuleMapCallbacks() {}
+
+  /// \brief Called when a module map file has been read.
+  ///
+  /// \param FileStart A SourceLocation referring to the start of the file's
+  /// contents.
+  /// \param File The file itself.
+  /// \param IsSystem Whether this is a module map from a system include path.
+  virtual void moduleMapFileRead(SourceLocation FileStart,
+                                 const FileEntry &File, bool IsSystem) {}
+};
   
 class ModuleMap {
   SourceManager &SourceMgr;
@@ -42,6 +58,8 @@ class ModuleMap {
   const LangOptions &LangOpts;
   const TargetInfo *Target;
   HeaderSearch &HeaderInfo;
+
+  llvm::SmallVector<std::unique_ptr<ModuleMapCallbacks>, 1> Callbacks;
   
   /// \brief The directory used for Clang-supplied, builtin include headers,
   /// such as "stdint.h".
@@ -263,6 +281,11 @@ public:
     BuiltinIncludeDir = Dir;
   }
 
+  /// \brief Add a module map callback.
+  void addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback) {
+    Callbacks.push_back(std::move(Callback));
+  }
+
   /// \brief Retrieve the module that owns the given header file, if any.
   ///
   /// \param File The header file that is likely to be included.
index 0995ab4bf0778e83a5e7700da43ab638b0450736..72de73014adfd6d64eba130abd101315fa9038e8 100644 (file)
@@ -18,6 +18,7 @@
 #include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/Lex/DirectoryLookup.h"
 #include "clang/Lex/LexDiagnostic.h"
+#include "clang/Lex/ModuleMap.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Serialization/ASTReader.h"
@@ -82,6 +83,20 @@ struct DepCollectorPPCallbacks : public PPCallbacks {
   }
 };
 
+struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
+  DependencyCollector &DepCollector;
+  DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
+
+  void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
+                         bool IsSystem) override {
+    StringRef Filename = Entry.getName();
+    DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
+                                    /*IsSystem*/IsSystem,
+                                    /*IsModuleFile*/false,
+                                    /*IsMissing*/false);
+  }
+};
+
 struct DepCollectorASTListener : public ASTReaderListener {
   DependencyCollector &DepCollector;
   DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
@@ -132,6 +147,8 @@ DependencyCollector::~DependencyCollector() { }
 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
   PP.addPPCallbacks(
       llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
+  PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
+      llvm::make_unique<DepCollectorMMCallbacks>(*this));
 }
 void DependencyCollector::attachToASTReader(ASTReader &R) {
   R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
@@ -185,6 +202,17 @@ public:
   bool includeModuleFiles() const { return IncludeModuleFiles; }
 };
 
+class DFGMMCallback : public ModuleMapCallbacks {
+  DFGImpl &Parent;
+public:
+  DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
+  void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
+                         bool IsSystem) override {
+    if (!IsSystem || Parent.includeSystemHeaders())
+      Parent.AddFilename(Entry.getName());
+  }
+};
+
 class DFGASTReaderListener : public ASTReaderListener {
   DFGImpl &Parent;
 public:
@@ -217,6 +245,8 @@ DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
 
   DFGImpl *Callback = new DFGImpl(&PP, Opts);
   PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
+  PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
+      llvm::make_unique<DFGMMCallback>(*Callback));
   return new DependencyFileGenerator(Callback);
 }
 
index 96d3e4b8fe6502c54421a9ae4d872da360c6c1f9..995e1370ede945017ede48ee5899cea1cbd9b647 100644 (file)
@@ -2335,9 +2335,14 @@ bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
 
   // Parse this module map file.
   Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts);
+  SourceLocation Start = L.getSourceLocation();
   ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir,
                          BuiltinIncludeDir, IsSystem);
   bool Result = Parser.parseModuleMapFile();
   ParsedModuleMap[File] = Result;
+
+  // Notify callbacks that we parsed it.
+  for (const auto &Cb : Callbacks)
+    Cb->moduleMapFileRead(Start, *File, IsSystem);
   return Result;
 }
index 4da054ff7dc83d34a75a457270b7269734a33257..589865e71d68aedb97ef28e5fdd2b38c2ab680ee 100644 (file)
@@ -6,8 +6,8 @@
 // RUN: FileCheck %s < %t.d
 // CHECK: dependency-gen-pch.m.o
 // CHECK-NEXT: dependency-gen-pch.m
+// CHECK-NEXT: Inputs{{.}}module.map
 // CHECK-NEXT: diamond_top.pcm
 // CHECK-NEXT: Inputs{{.}}diamond_top.h
-// CHECK-NEXT: Inputs{{.}}module.map
 
 #import "diamond_top.h"
index 60a7192ed5a15601a0b242c7a1f0525497e7976f..cb0a87595605216b017816fb80844109c91703e4 100644 (file)
@@ -4,8 +4,8 @@
 // RUN: %clang_cc1 -x objective-c -isystem %S/Inputs/System/usr/include -dependency-file %t.d.1 -MT %s.o -I %S/Inputs -fsyntax-only -fmodules -fimplicit-module-maps -fmodules-cache-path=%t-mcp %s
 // RUN: FileCheck %s < %t.d.1
 // CHECK: dependency-gen.m
-// CHECK: Inputs{{.}}diamond_top.h
 // CHECK: Inputs{{.}}module.map
+// CHECK: Inputs{{.}}diamond_top.h
 // CHECK-NOT: usr{{.}}include{{.}}module.map
 // CHECK-NOT: stdint.h
 
@@ -13,8 +13,8 @@
 // RUN: %clang_cc1 -x objective-c -isystem %S/Inputs/System/usr/include -dependency-file %t.d.2 -MT %s.o -I %S/Inputs -sys-header-deps -fsyntax-only -fmodules -fimplicit-module-maps -fmodules-cache-path=%t-mcp %s
 // RUN: FileCheck %s -check-prefix=CHECK-SYS < %t.d.2
 // CHECK-SYS: dependency-gen.m
-// CHECK-SYS: Inputs{{.}}diamond_top.h
 // CHECK-SYS: Inputs{{.}}module.map
+// CHECK-SYS: Inputs{{.}}diamond_top.h
 // CHECK-SYS: usr{{.}}include{{.}}module.map
 // CHECK-SYS: stdint.h
 
index 66ee94cc25e7f7d8cae9792d63ceebc56e666fc9..ae1804c0e33faff5b63bdcd8f2ee17526e850341 100644 (file)
@@ -15,5 +15,10 @@ module "test" {
 extern module "test-base2" "Inputs/dependency-gen-base2.modulemap"
 extern module "test-base" "Inputs/dependency-gen-base.modulemap"
 
-// CHECK: {{ |\.[/\\]}}Inputs{{[/\\]}}dependency-gen-included2.h
-// CHECK: {{ |\.[/\\]}}Inputs{{[/\\]}}dependency-gen-base.modulemap
+// CHECK-DAG: {{[/\\]}}dependency-gen.modulemap
+// CHECK-DAG: {{ |\.[/\\]}}Inputs{{[/\\]}}dependency-gen-base.modulemap
+// CHECK-DAG: {{ |\.[/\\]}}Inputs{{[/\\]}}dependency-gen-base2.modulemap
+
+// CHECK-DAG: {{ |\.[/\\]}}Inputs{{[/\\]}}dependency-gen.h
+// CHECK-DAG: {{ |\.[/\\]}}Inputs{{[/\\]}}dependency-gen-included.h
+// CHECK-DAG: {{ |\.[/\\]}}Inputs{{[/\\]}}dependency-gen-included2.h
index 86c465147742685916ffea666b7c987e363b7223..5fbfcfa38149e61b36b749a1f547043ed98f1ebc 100644 (file)
 
 #include "Inputs/relative-dep-gen-1.h"
 
-// CHECK-BUILD: mod.pcm: Inputs/relative-dep-gen-1.h Inputs/relative-dep-gen-2.h
-// CHECK-USE: use.o: relative-dep-gen.cpp Inputs/relative-dep-gen-1.h
+// CHECK-BUILD: mod.pcm:
+// CHECK-BUILD:   Inputs/relative-dep-gen{{(-cwd)?}}.modulemap
+// CHECK-BUILD:   Inputs/relative-dep-gen-1.h
+// CHECK-BUILD:   Inputs/relative-dep-gen-2.h
+// CHECK-USE: use.o:
+// CHECK-USE:   Inputs/relative-dep-gen{{(-cwd)?}}.modulemap
+// CHECK-USE:   relative-dep-gen.cpp
+// CHECK-USE:   Inputs/relative-dep-gen-1.h