From: Richard Smith Date: Sun, 9 Aug 2015 04:46:57 +0000 (+0000) Subject: [modules] When building a dependency file, include module maps parsed in the X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3a421c44a488e2a346ef1347e89c32e580a20023;p=clang [modules] When building a dependency file, include module maps parsed in the 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 --- diff --git a/include/clang/Lex/ModuleMap.h b/include/clang/Lex/ModuleMap.h index 0b03c4aa1c..2365a895a0 100644 --- a/include/clang/Lex/ModuleMap.h +++ b/include/clang/Lex/ModuleMap.h @@ -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, 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 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. diff --git a/lib/Frontend/DependencyFile.cpp b/lib/Frontend/DependencyFile.cpp index 0995ab4bf0..72de73014a 100644 --- a/lib/Frontend/DependencyFile.cpp +++ b/lib/Frontend/DependencyFile.cpp @@ -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(*this, PP.getSourceManager())); + PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( + llvm::make_unique(*this)); } void DependencyCollector::attachToASTReader(ASTReader &R) { R.addListener(llvm::make_unique(*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(Callback)); + PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( + llvm::make_unique(*Callback)); return new DependencyFileGenerator(Callback); } diff --git a/lib/Lex/ModuleMap.cpp b/lib/Lex/ModuleMap.cpp index 96d3e4b8fe..995e1370ed 100644 --- a/lib/Lex/ModuleMap.cpp +++ b/lib/Lex/ModuleMap.cpp @@ -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; } diff --git a/test/Modules/dependency-gen-pch.m b/test/Modules/dependency-gen-pch.m index 4da054ff7d..589865e71d 100644 --- a/test/Modules/dependency-gen-pch.m +++ b/test/Modules/dependency-gen-pch.m @@ -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" diff --git a/test/Modules/dependency-gen.m b/test/Modules/dependency-gen.m index 60a7192ed5..cb0a875956 100644 --- a/test/Modules/dependency-gen.m +++ b/test/Modules/dependency-gen.m @@ -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 diff --git a/test/Modules/dependency-gen.modulemap b/test/Modules/dependency-gen.modulemap index 66ee94cc25..ae1804c0e3 100644 --- a/test/Modules/dependency-gen.modulemap +++ b/test/Modules/dependency-gen.modulemap @@ -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 diff --git a/test/Modules/relative-dep-gen.cpp b/test/Modules/relative-dep-gen.cpp index 86c4651477..5fbfcfa381 100644 --- a/test/Modules/relative-dep-gen.cpp +++ b/test/Modules/relative-dep-gen.cpp @@ -20,5 +20,11 @@ #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