Skip to content

Commit 477c705

Browse files
authored
[clang][modules] Allow including module maps to be non-affecting (#89992)
The dependency scanner only puts top-level affecting module map files on the command line for explicitly building a module. This is done because any affecting child module map files should be referenced by the top-level one, meaning listing them explicitly does not have any meaning and only makes the command lines longer. However, a problem arises whenever the definition of an affecting module lives in a module map that is not top-level. Considering the rules explained above, such module map file would not make it to the command line. That's why 83973cf started marking the parents of an affecting module map file as affecting too. This way, the top-level file does make it into the command line. This can be problematic, though. On macOS, for example, the Darwin module lives in "/usr/include/Darwin.modulemap" one of many module map files included by "/usr/include/module.modulemap". Reporting the parent on the command line forces explicit builds to parse all the other module map files included by it, which is not necessary and can get expensive in terms of file system traffic. This patch solves that performance issue by stopping marking parent module map files as affecting, and marking module map files as top-level whenever they are top-level among the set of affecting files, not among the set of all known files. This means that the top-level "/usr/include/module.modulemap" is now not marked as affecting and "/usr/include/Darwin.modulemap" is.
1 parent 754072e commit 477c705

File tree

3 files changed

+75
-44
lines changed

3 files changed

+75
-44
lines changed

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ class StoredDeclsList;
7676
class SwitchCase;
7777
class Token;
7878

79+
namespace SrcMgr {
80+
class FileInfo;
81+
} // namespace SrcMgr
82+
7983
/// Writes an AST file containing the contents of a translation unit.
8084
///
8185
/// The ASTWriter class produces a bitstream containing the serialized
@@ -490,6 +494,11 @@ class ASTWriter : public ASTDeserializationListener,
490494
/// during \c SourceManager serialization.
491495
void computeNonAffectingInputFiles();
492496

497+
/// Some affecting files can be included from files that are not affecting.
498+
/// This function erases source locations pointing into such files.
499+
SourceLocation getAffectingIncludeLoc(const SourceManager &SourceMgr,
500+
const SrcMgr::FileInfo &File);
501+
493502
/// Returns an adjusted \c FileID, accounting for any non-affecting input
494503
/// files.
495504
FileID getAdjustedFileID(FileID FID) const;

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -173,54 +173,50 @@ GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
173173

174174
const HeaderSearch &HS = PP.getHeaderSearchInfo();
175175
const ModuleMap &MM = HS.getModuleMap();
176-
const SourceManager &SourceMgr = PP.getSourceManager();
177176

178177
std::set<const FileEntry *> ModuleMaps;
179-
auto CollectIncludingModuleMaps = [&](FileID FID, FileEntryRef F) {
180-
if (!ModuleMaps.insert(F).second)
178+
std::set<const Module *> ProcessedModules;
179+
auto CollectModuleMapsForHierarchy = [&](const Module *M) {
180+
M = M->getTopLevelModule();
181+
182+
if (!ProcessedModules.insert(M).second)
181183
return;
182-
SourceLocation Loc = SourceMgr.getIncludeLoc(FID);
183-
// The include location of inferred module maps can point into the header
184-
// file that triggered the inferring. Cut off the walk if that's the case.
185-
while (Loc.isValid() && isModuleMap(SourceMgr.getFileCharacteristic(Loc))) {
186-
FID = SourceMgr.getFileID(Loc);
187-
F = *SourceMgr.getFileEntryRefForID(FID);
188-
if (!ModuleMaps.insert(F).second)
189-
break;
190-
Loc = SourceMgr.getIncludeLoc(FID);
191-
}
192-
};
193184

194-
std::set<const Module *> ProcessedModules;
195-
auto CollectIncludingMapsFromAncestors = [&](const Module *M) {
196-
for (const Module *Mod = M; Mod; Mod = Mod->Parent) {
197-
if (!ProcessedModules.insert(Mod).second)
198-
break;
185+
std::queue<const Module *> Q;
186+
Q.push(M);
187+
while (!Q.empty()) {
188+
const Module *Mod = Q.front();
189+
Q.pop();
190+
199191
// The containing module map is affecting, because it's being pointed
200192
// into by Module::DefinitionLoc.
201-
if (FileID FID = MM.getContainingModuleMapFileID(Mod); FID.isValid())
202-
CollectIncludingModuleMaps(FID, *SourceMgr.getFileEntryRefForID(FID));
203-
// For inferred modules, the module map that allowed inferring is not in
204-
// the include chain of the virtual containing module map file. It did
205-
// affect the compilation, though.
206-
if (FileID FID = MM.getModuleMapFileIDForUniquing(Mod); FID.isValid())
207-
CollectIncludingModuleMaps(FID, *SourceMgr.getFileEntryRefForID(FID));
193+
if (auto FE = MM.getContainingModuleMapFile(Mod))
194+
ModuleMaps.insert(*FE);
195+
// For inferred modules, the module map that allowed inferring is not
196+
// related to the virtual containing module map file. It did affect the
197+
// compilation, though.
198+
if (auto FE = MM.getModuleMapFileForUniquing(Mod))
199+
ModuleMaps.insert(*FE);
200+
201+
for (auto *SubM : Mod->submodules())
202+
Q.push(SubM);
208203
}
209204
};
210205

211206
// Handle all the affecting modules referenced from the root module.
212207

208+
CollectModuleMapsForHierarchy(RootModule);
209+
213210
std::queue<const Module *> Q;
214211
Q.push(RootModule);
215212
while (!Q.empty()) {
216213
const Module *CurrentModule = Q.front();
217214
Q.pop();
218215

219-
CollectIncludingMapsFromAncestors(CurrentModule);
220216
for (const Module *ImportedModule : CurrentModule->Imports)
221-
CollectIncludingMapsFromAncestors(ImportedModule);
217+
CollectModuleMapsForHierarchy(ImportedModule);
222218
for (const Module *UndeclaredModule : CurrentModule->UndeclaredUses)
223-
CollectIncludingMapsFromAncestors(UndeclaredModule);
219+
CollectModuleMapsForHierarchy(UndeclaredModule);
224220

225221
for (auto *M : CurrentModule->submodules())
226222
Q.push(M);
@@ -249,9 +245,27 @@ GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
249245

250246
for (const auto &KH : HS.findResolvedModulesForHeader(*File))
251247
if (const Module *M = KH.getModule())
252-
CollectIncludingMapsFromAncestors(M);
248+
CollectModuleMapsForHierarchy(M);
253249
}
254250

251+
// FIXME: This algorithm is not correct for module map hierarchies where
252+
// module map file defining a (sub)module of a top-level module X includes
253+
// a module map file that defines a (sub)module of another top-level module Y.
254+
// Whenever X is affecting and Y is not, "replaying" this PCM file will fail
255+
// when parsing module map files for X due to not knowing about the `extern`
256+
// module map for Y.
257+
//
258+
// We don't have a good way to fix it here. We could mark all children of
259+
// affecting module map files as being affecting as well, but that's
260+
// expensive. SourceManager does not model the edge from parent to child
261+
// SLocEntries, so instead, we would need to iterate over leaf module map
262+
// files, walk up their include hierarchy and check whether we arrive at an
263+
// affecting module map.
264+
//
265+
// Instead of complicating and slowing down this function, we should probably
266+
// just ban module map hierarchies where module map defining a (sub)module X
267+
// includes a module map defining a module that's not a submodule of X.
268+
255269
return ModuleMaps;
256270
}
257271

@@ -1665,6 +1679,18 @@ struct InputFileEntry {
16651679

16661680
} // namespace
16671681

1682+
SourceLocation ASTWriter::getAffectingIncludeLoc(const SourceManager &SourceMgr,
1683+
const SrcMgr::FileInfo &File) {
1684+
SourceLocation IncludeLoc = File.getIncludeLoc();
1685+
if (IncludeLoc.isValid()) {
1686+
FileID IncludeFID = SourceMgr.getFileID(IncludeLoc);
1687+
assert(IncludeFID.isValid() && "IncludeLoc in invalid file");
1688+
if (!IsSLocAffecting[IncludeFID.ID])
1689+
IncludeLoc = SourceLocation();
1690+
}
1691+
return IncludeLoc;
1692+
}
1693+
16681694
void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
16691695
HeaderSearchOptions &HSOpts) {
16701696
using namespace llvm;
@@ -1718,7 +1744,7 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
17181744
Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
17191745
Entry.IsTransient = Cache->IsTransient;
17201746
Entry.BufferOverridden = Cache->BufferOverridden;
1721-
Entry.IsTopLevel = File.getIncludeLoc().isInvalid();
1747+
Entry.IsTopLevel = getAffectingIncludeLoc(SourceMgr, File).isInvalid();
17221748
Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
17231749

17241750
auto ContentHash = hash_code(-1);
@@ -2245,7 +2271,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
22452271
SLocEntryOffsets.push_back(Offset);
22462272
// Starting offset of this entry within this module, so skip the dummy.
22472273
Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2248-
AddSourceLocation(File.getIncludeLoc(), Record);
2274+
AddSourceLocation(getAffectingIncludeLoc(SourceMgr, File), Record);
22492275
Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
22502276
Record.push_back(File.hasLineDirectives());
22512277

clang/test/ClangScanDeps/modules-extern-unrelated.m

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// This test checks that only module map files defining affecting modules are
2+
// affecting.
3+
14
// RUN: rm -rf %t
25
// RUN: split-file %s %t
36

@@ -22,15 +25,8 @@
2225
//--- second/second.h
2326
#include "first_other.h"
2427

25-
//--- cdb.json.template
26-
[{
27-
"directory": "DIR",
28-
"file": "DIR/tu.m",
29-
"command": "clang -fmodules -fmodules-cache-path=DIR/cache -I DIR/zeroth -I DIR/first -I DIR/second -c DIR/tu.m -o DIR/tu.o"
30-
}]
31-
32-
// RUN: sed -e "s|DIR|%/t|g" -e "s|INPUTS|%/S/Inputs|g" %t/cdb.json.template > %t/cdb.json
33-
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full > %t/result.json
28+
// RUN: clang-scan-deps -format experimental-full -o %t/result.json \
29+
// RUN: -- %clang -fmodules -fmodules-cache-path=%t/cache -I %t/zeroth -I %t/first -I %t/second -c %t/tu.m -o %t/tu.o
3430
// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
3531

3632
// CHECK: {
@@ -67,11 +63,11 @@
6763
// CHECK-NEXT: ],
6864
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/second/second.modulemap",
6965
// CHECK-NEXT: "command-line": [
66+
// CHECK-NOT: "-fmodule-map-file=[[PREFIX]]/second/module.modulemap"
7067
// CHECK: ],
7168
// CHECK-NEXT: "context-hash": "{{.*}}",
7269
// CHECK-NEXT: "file-deps": [
7370
// CHECK-NEXT: "[[PREFIX]]/first/module.modulemap",
74-
// CHECK-NEXT: "[[PREFIX]]/second/module.modulemap",
7571
// CHECK-NEXT: "[[PREFIX]]/second/second.h",
7672
// CHECK-NEXT: "[[PREFIX]]/second/second.modulemap"
7773
// CHECK-NEXT: ],
@@ -90,11 +86,11 @@
9086
// CHECK-NEXT: ],
9187
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/zeroth/module.modulemap",
9288
// CHECK-NEXT: "command-line": [
89+
// CHECK-NOT: "-fmodule-map-file=[[PREFIX]]/second/module.modulemap"
9390
// CHECK: ],
9491
// CHECK-NEXT: "context-hash": "{{.*}}",
9592
// CHECK-NEXT: "file-deps": [
9693
// CHECK-NEXT: "[[PREFIX]]/first/module.modulemap",
97-
// CHECK-NEXT: "[[PREFIX]]/second/module.modulemap",
9894
// CHECK-NEXT: "[[PREFIX]]/second/second.modulemap",
9995
// CHECK-NEXT: "[[PREFIX]]/zeroth/module.modulemap",
10096
// CHECK-NEXT: "[[PREFIX]]/zeroth/zeroth.h"
@@ -115,7 +111,7 @@
115111
// CHECK-NEXT: ],
116112
// CHECK-NEXT: "command-line": [
117113
// CHECK: ],
118-
// CHECK-NEXT: "executable": "clang",
114+
// CHECK-NEXT: "executable": "{{.*}}",
119115
// CHECK-NEXT: "file-deps": [
120116
// CHECK-NEXT: "[[PREFIX]]/tu.m"
121117
// CHECK-NEXT: ],

0 commit comments

Comments
 (0)