Skip to content

Commit 33af4b2

Browse files
committed
[clang][deps] Stop sharing FileManager across module builds in scanner
Sharing the FileManager across implicit module builds currently leaks paths looked up in an importer into the built module itself. This can cause non-deterministic results across scans. It is especially bad for modules since the path can be saved into the pcm file itself, leading to stateful behaviour if the cache is shared. This should not impact the number of real filesystem accesses in the scanner, since it is already caching in the DependencyScanningWorkerFilesystem. Note: this change does not affect whether or not the FileManager is shared across TUs in the scanner, which is a separate issue. Differential Revision: https://reviews.llvm.org/D131412
1 parent b71b22e commit 33af4b2

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class DependencyScanningAction : public tooling::ToolAction {
170170

171171
ScanInstance.getFrontendOpts().GenerateGlobalModuleIndex = false;
172172
ScanInstance.getFrontendOpts().UseGlobalModuleIndex = false;
173+
ScanInstance.getFrontendOpts().ModulesShareFileManager = false;
173174

174175
FileMgr->getFileSystemOpts().WorkingDir = std::string(WorkingDirectory);
175176
ScanInstance.setFileManager(FileMgr);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Ensure that the spelling of a path seen outside a module (e.g. header via
2+
// symlink) does not leak into the compilation of that module unnecessarily.
3+
// Note: the spelling of the modulemap path still depends on the includer, since
4+
// that is the only source of information about it.
5+
6+
// REQUIRES: shell
7+
8+
// RUN: rm -rf %t
9+
// RUN: split-file %s %t
10+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.in > %t/cdb.json
11+
// RUN: ln -s A.h %t/Z.h
12+
13+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -j 1 -format experimental-full \
14+
// RUN: -mode preprocess-dependency-directives -generate-modules-path-args > %t/output
15+
// RUN: FileCheck %s < %t/output
16+
17+
// CHECK: "modules": [
18+
// CHECK-NEXT: {
19+
// CHECK: "file-deps": [
20+
// CHECK-NEXT: "{{.*}}A.h",
21+
// CHECK-NEXT: "{{.*}}module.modulemap"
22+
// CHECK-NEXT: ],
23+
// CHECK-NEXT: "name": "A"
24+
// CHECK-NEXT: }
25+
26+
//--- cdb.json.in
27+
[{
28+
"directory": "DIR",
29+
"command": "clang -fsyntax-only DIR/tu.c -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -fimplicit-module-maps",
30+
"file": "DIR/tu.c"
31+
}]
32+
33+
//--- module.modulemap
34+
module A { header "A.h" }
35+
module B { header "B.h" }
36+
module C { header "C.h" }
37+
38+
//--- A.h
39+
40+
//--- B.h
41+
#include "Z.h"
42+
43+
//--- tu.c
44+
#include "B.h"

0 commit comments

Comments
 (0)