Skip to content

Commit 43df61c

Browse files
committed
[clang][cas] Handle include-tree for a file considered part of the module
When building a non-header file with -fmodule-name set, add the requisite modulemap file even though it is not imported as a module, and test that we are getting the right semantics for the header include.
1 parent bd61c2c commit 43df61c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ IncludeTreeBuilder::finishIncludeTree(CompilerInstance &ScanInstance,
486486
return std::move(E);
487487
}
488488

489+
for (StringRef ModuleMap : FrontendOpts.ModuleMapFiles)
490+
if (Error E = addFile(ModuleMap))
491+
return std::move(E);
492+
489493
auto FinishIncludeTree = [&]() -> Error {
490494
IntrusiveRefCntPtr<ASTReader> Reader = ScanInstance.getASTReader();
491495
if (!Reader)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// REQUIRES: ondisk_cas
2+
3+
// RUN: rm -rf %t
4+
// RUN: split-file %s %t
5+
// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
6+
7+
// RUN: clang-scan-deps -compilation-database %t/cdb.json \
8+
// RUN: -cas-path %t/cas -module-files-dir %t/outputs \
9+
// RUN: -format experimental-include-tree-full -mode preprocess-dependency-directives \
10+
// RUN: > %t/deps.json
11+
12+
// RUN: FileCheck %s -input-file %t/deps.json -check-prefix=NO_MODULES
13+
// NO_MODULES: "modules": []
14+
15+
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/tu.rsp
16+
// RUN: cat %t/tu.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/tu.casid
17+
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/tu.casid | FileCheck %s -DPREFIX=%/t
18+
// RUN: %clang @%t/tu.rsp
19+
20+
// CHECK: [[PREFIX]]/tu.c llvmcas://{{[[:xdigit:]]+}}
21+
// CHECK: 1:1 <built-in> llvmcas://{{[[:xdigit:]]+}}
22+
23+
// Note: this is surprising, but correct: when building the implementation files
24+
// of a module, the first include is textual but still uses the submodule
25+
// machinery. The second include is treated as a module import (unless in a PCH)
26+
// but will not actually import the module only trigger visibility changes.
27+
28+
// CHECK: 2:1 [[PREFIX]]/Mod.h llvmcas://{{[[:xdigit:]]+}}
29+
// CHECK: Submodule: Mod
30+
// CHECK: 3:1 (Module) Mod
31+
32+
// CHECK: Files:
33+
// CHECK: [[PREFIX]]/tu.c llvmcas://{{[[:xdigit:]]+}}
34+
// CHECK: [[PREFIX]]/Mod.h llvmcas://{{[[:xdigit:]]+}}
35+
36+
// Despite not importing the module, we need its modulemap for submodule info.
37+
// CHECK: [[PREFIX]]/module.modulemap llvmcas://{{[[:xdigit:]]+}}
38+
39+
//--- cdb.json.template
40+
[{
41+
"file": "DIR/tu.c",
42+
"directory": "DIR",
43+
"command": "clang -fsyntax-only DIR/tu.c -I DIR -fmodule-name=Mod -fmodules -fimplicit-modules -fimplicit-module-maps -fmodules-cache-path=DIR/module-cache"
44+
}]
45+
46+
//--- module.modulemap
47+
module Mod { header "Mod.h" }
48+
49+
//--- Mod.h
50+
#pragma once
51+
void top(void);
52+
53+
//--- tu.c
54+
#include "Mod.h"
55+
#include "Mod.h"
56+
void tu(void) {
57+
top();
58+
}

0 commit comments

Comments
 (0)