Skip to content

Commit 6e77e37

Browse files
committed
[clang][modules] Allow including module maps to be non-affecting
1 parent 80f510b commit 6e77e37

File tree

7 files changed

+86
-48
lines changed

7 files changed

+86
-48
lines changed

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace serialization {
4141
/// Version 4 of AST files also requires that the version control branch and
4242
/// revision match exactly, since there is no backward compatibility of
4343
/// AST files at this time.
44-
const unsigned VERSION_MAJOR = 30;
44+
const unsigned VERSION_MAJOR = 31;
4545

4646
/// AST file minor version number supported by this version of
4747
/// Clang.
@@ -51,7 +51,7 @@ const unsigned VERSION_MAJOR = 30;
5151
/// for the previous version could still support reading the new
5252
/// version by ignoring new kinds of subblocks), this number
5353
/// should be increased.
54-
const unsigned VERSION_MINOR = 1;
54+
const unsigned VERSION_MINOR = 0;
5555

5656
/// An ID number that refers to an identifier in an AST file.
5757
///

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
@@ -491,6 +495,11 @@ class ASTWriter : public ASTDeserializationListener,
491495
/// during \c SourceManager serialization.
492496
void computeNonAffectingInputFiles();
493497

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

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct InputFileInfo {
6969
bool Overridden;
7070
bool Transient;
7171
bool TopLevel;
72+
bool TopLevelAmongAffecting;
7273
bool ModuleMap;
7374
};
7475

clang/lib/Serialization/ASTReader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,9 +2444,10 @@ InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
24442444
R.Overridden = static_cast<bool>(Record[3]);
24452445
R.Transient = static_cast<bool>(Record[4]);
24462446
R.TopLevel = static_cast<bool>(Record[5]);
2447-
R.ModuleMap = static_cast<bool>(Record[6]);
2447+
R.TopLevelAmongAffecting = static_cast<bool>(Record[6]);
2448+
R.ModuleMap = static_cast<bool>(Record[7]);
24482449
std::tie(R.FilenameAsRequested, R.Filename) = [&]() {
2449-
uint16_t AsRequestedLength = Record[7];
2450+
uint16_t AsRequestedLength = Record[8];
24502451

24512452
std::string NameAsRequested = Blob.substr(0, AsRequestedLength).str();
24522453
std::string Name = Blob.substr(AsRequestedLength).str();

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 62 additions & 31 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

@@ -1631,6 +1645,7 @@ struct InputFileEntry {
16311645
bool IsTransient;
16321646
bool BufferOverridden;
16331647
bool IsTopLevel;
1648+
bool IsTopLevelAmongAffecting;
16341649
bool IsModuleMap;
16351650
uint32_t ContentHash[2];
16361651

@@ -1639,6 +1654,18 @@ struct InputFileEntry {
16391654

16401655
} // namespace
16411656

1657+
SourceLocation ASTWriter::getAffectingIncludeLoc(const SourceManager &SourceMgr,
1658+
const SrcMgr::FileInfo &File) {
1659+
SourceLocation IncludeLoc = File.getIncludeLoc();
1660+
if (IncludeLoc.isValid()) {
1661+
FileID IncludeFID = SourceMgr.getFileID(IncludeLoc);
1662+
assert(IncludeFID.isValid() && "IncludeLoc in invalid file");
1663+
if (!IsSLocAffecting[IncludeFID.ID])
1664+
IncludeLoc = SourceLocation();
1665+
}
1666+
return IncludeLoc;
1667+
}
1668+
16421669
void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
16431670
HeaderSearchOptions &HSOpts) {
16441671
using namespace llvm;
@@ -1654,6 +1681,7 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
16541681
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
16551682
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
16561683
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level
1684+
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level affect
16571685
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
16581686
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Name as req. len
16591687
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name as req. + name
@@ -1693,6 +1721,8 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
16931721
Entry.IsTransient = Cache->IsTransient;
16941722
Entry.BufferOverridden = Cache->BufferOverridden;
16951723
Entry.IsTopLevel = File.getIncludeLoc().isInvalid();
1724+
Entry.IsTopLevelAmongAffecting =
1725+
getAffectingIncludeLoc(SourceMgr, File).isInvalid();
16961726
Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
16971727

16981728
auto ContentHash = hash_code(-1);
@@ -1758,6 +1788,7 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
17581788
Entry.BufferOverridden,
17591789
Entry.IsTransient,
17601790
Entry.IsTopLevel,
1791+
Entry.IsTopLevelAmongAffecting,
17611792
Entry.IsModuleMap,
17621793
NameAsRequested.size()};
17631794

@@ -2219,7 +2250,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
22192250
SLocEntryOffsets.push_back(Offset);
22202251
// Starting offset of this entry within this module, so skip the dummy.
22212252
Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2222-
AddSourceLocation(File.getIncludeLoc(), Record);
2253+
AddSourceLocation(getAffectingIncludeLoc(SourceMgr, File), Record);
22232254
Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
22242255
Record.push_back(File.hasLineDirectives());
22252256

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
616616
MDC.ScanInstance.getASTReader()->visitInputFileInfos(
617617
*MF, /*IncludeSystem=*/true,
618618
[&](const serialization::InputFileInfo &IFI, bool IsSystem) {
619-
if (!(IFI.TopLevel && IFI.ModuleMap))
619+
if (!(IFI.TopLevelAmongAffecting && IFI.ModuleMap))
620620
return;
621621
if (StringRef(IFI.FilenameAsRequested)
622622
.ends_with("__inferred_module.map"))

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 parent module maps that do not define any related
2+
// modules are not 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)