Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 16cfd7f

Browse files
committed
[Modules] Skip adding unused module maps to the dependency file
Current generation of dependency files when modules is on take into account all parsed module maps found while searching for a specific module. Add a new cc1 flag that allows changing that behavior: only add module maps that actually answer for the modules being searched for. rdar://problem/38394973 (cherry picked from commit 4c357ef) Conflicts: include/clang/Frontend/DependencyOutputOptions.h include/clang/Lex/ModuleMap.h lib/Frontend/DependencyFile.cpp
1 parent cbdb155 commit 16cfd7f

File tree

10 files changed

+45
-3
lines changed

10 files changed

+45
-3
lines changed

include/clang/Driver/CC1Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ def fno_dllexport_inlines : Flag<["-"], "fno-dllexport-inlines">;
385385

386386
def sys_header_deps : Flag<["-"], "sys-header-deps">,
387387
HelpText<"Include system headers in dependency output">;
388+
def skip_unused_modulemap_file_deps : Flag<["-"], "skip-unused-modulemap-deps">,
389+
HelpText<"Include module map files only for imported modules in dependency output">;
388390
def module_file_deps : Flag<["-"], "module-file-deps">,
389391
HelpText<"Include module files in dependency output">;
390392
def header_include_file : Separate<["-"], "header-include-file">,

include/clang/Frontend/DependencyOutputOptions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class DependencyOutputOptions {
3232
/// problems.
3333
unsigned AddMissingHeaderDeps : 1; ///< Add missing headers to dependency list
3434
unsigned IncludeModuleFiles : 1; ///< Include module file dependencies.
35+
unsigned SkipUnusedModuleMaps : 1; ///< Skip unused module map dependencies.
3536

3637
/// Destination of cl.exe style /showIncludes info.
3738
ShowIncludesDestination ShowIncludesDest = ShowIncludesDestination::None;
@@ -67,7 +68,7 @@ class DependencyOutputOptions {
6768
public:
6869
DependencyOutputOptions()
6970
: IncludeSystemHeaders(0), ShowHeaderIncludes(0), UsePhonyTargets(0),
70-
AddMissingHeaderDeps(0), IncludeModuleFiles(0) {}
71+
AddMissingHeaderDeps(0), IncludeModuleFiles(0), SkipUnusedModuleMaps(0) {}
7172
};
7273

7374
} // end namespace clang

include/clang/Lex/ModuleMap.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ class ModuleMapCallbacks {
5959
virtual void moduleMapFileRead(SourceLocation FileStart,
6060
const FileEntry &File, bool IsSystem) {}
6161

62+
/// Called when a module map file matches a module lookup
63+
///
64+
/// \param File The file itself.
65+
/// \param M The module found that matches this module map.
66+
/// \param IsSystem Whether this is a module map from a system include path.
67+
virtual void moduleMapFoundForModule(const FileEntry &File, const Module *M,
68+
bool IsSystem) {}
69+
6270
/// Called when a header is added during module map parsing.
6371
///
6472
/// \param Filename The header file itself.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,7 @@ static void ParseDependencyOutputArgs(DependencyOutputOptions &Opts,
13371337
Opts.Targets = Args.getAllArgValues(OPT_MT);
13381338
Opts.IncludeSystemHeaders = Args.hasArg(OPT_sys_header_deps);
13391339
Opts.IncludeModuleFiles = Args.hasArg(OPT_module_file_deps);
1340+
Opts.SkipUnusedModuleMaps = Args.hasArg(OPT_skip_unused_modulemap_file_deps);
13401341
Opts.UsePhonyTargets = Args.hasArg(OPT_MP);
13411342
Opts.ShowHeaderIncludes = Args.hasArg(OPT_H);
13421343
Opts.HeaderIncludeOutputFile = Args.getLastArgValue(OPT_header_include_file);

lib/Frontend/DependencyFile.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class DFGImpl : public PPCallbacks {
161161
bool AddMissingHeaderDeps;
162162
bool SeenMissingHeader;
163163
bool IncludeModuleFiles;
164+
bool SkipUnusedModuleMaps;
164165
DependencyOutputFormat OutputFormat;
165166
unsigned InputFileIndex;
166167

@@ -177,6 +178,7 @@ class DFGImpl : public PPCallbacks {
177178
AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
178179
SeenMissingHeader(false),
179180
IncludeModuleFiles(Opts.IncludeModuleFiles),
181+
SkipUnusedModuleMaps(Opts.SkipUnusedModuleMaps),
180182
OutputFormat(Opts.OutputFormat),
181183
InputFileIndex(0) {
182184
for (const auto &ExtraDep : Opts.ExtraDeps) {
@@ -210,6 +212,7 @@ class DFGImpl : public PPCallbacks {
210212
bool AddFilename(StringRef Filename);
211213
bool includeSystemHeaders() const { return IncludeSystemHeaders; }
212214
bool includeModuleFiles() const { return IncludeModuleFiles; }
215+
bool skipUnusedModuleMaps() const { return SkipUnusedModuleMaps; }
213216
};
214217

215218
class DFGMMCallback : public ModuleMapCallbacks {
@@ -218,9 +221,17 @@ class DFGMMCallback : public ModuleMapCallbacks {
218221
DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
219222
void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
220223
bool IsSystem) override {
224+
if (Parent.skipUnusedModuleMaps())
225+
return;
221226
if (!IsSystem || Parent.includeSystemHeaders())
222227
Parent.AddFilename(Entry.getName());
223228
}
229+
void moduleMapFoundForModule(const FileEntry &Entry, const Module *M,
230+
bool IsSystem) override {
231+
if (Parent.skipUnusedModuleMaps() &&
232+
(!IsSystem || Parent.includeSystemHeaders()))
233+
Parent.AddFilename(Entry.getName());
234+
}
224235
};
225236

226237
class DFGASTReaderListener : public ASTReaderListener {

lib/Lex/ModuleMap.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,17 @@ ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header,
764764

765765
Module *ModuleMap::findModule(StringRef Name) const {
766766
llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
767-
if (Known != Modules.end())
768-
return Known->getValue();
767+
if (Known != Modules.end()) {
768+
Module *M = Known->getValue();
769+
// Notify callbacks that we found a module map for the module.
770+
if (!M->DefinitionLoc.isInvalid())
771+
for (const auto &Cb : Callbacks)
772+
Cb->moduleMapFoundForModule(
773+
*getContainingModuleMapFile(M), M,
774+
SourceMgr.getFileCharacteristic(M->DefinitionLoc) ==
775+
SrcMgr::C_System_ModuleMap);
776+
return M;
777+
}
769778

770779
return nullptr;
771780
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module A {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module X {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module Y {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: rm -rf %t.cache %t.d
2+
// RUN: %clang_cc1 -fsyntax-only -fmodules -fimplicit-module-maps -fmodules-cache-path=%t.cache -dependency-file %t.d -MT dependencies -I%S/Inputs/dependency-skip-unused/x -I%S/Inputs/dependency-skip-unused/y -I%S/Inputs/dependency-skip-unused -skip-unused-modulemap-deps %s
3+
// RUN: FileCheck %s < %t.d
4+
// CHECK-NOT: dependency-skip-unused{{.}}x{{.}}module.modulemap
5+
// CHECK-NOT: dependency-skip-unused{{.}}y{{.}}module.modulemap
6+
7+
@import A;

0 commit comments

Comments
 (0)