Skip to content

Commit 6161886

Browse files
committed
Adding a test case of multiple name lookup using the shared compiler instance.
1 parent a5e8e16 commit 6161886

File tree

7 files changed

+142
-18
lines changed

7 files changed

+142
-18
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,11 @@ llvm::Error CompilerInstanceWithContext::initialize() {
777777
PrebuiltModuleASTMap = std::move(*MaybePrebuiltModulesASTMap);
778778
OutputOpts = takeDependencyOutputOptionsFrom(CI);
779779

780+
// We do not create the target in initializeScanCompilerInstance because
781+
// setting it here is unique for by-name lookups. We create the target only
782+
// once here, and the information is reused for all computeDependencies calls.
783+
// We do not need to call createTarget explicitly if we go through
784+
// CompilerInstance::ExecuteAction to perform scanning.
780785
CI.createTarget();
781786

782787
return llvm::Error::success();
@@ -787,7 +792,6 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
787792
DependencyActionController &Controller) {
788793
auto &CI = *CIPtr;
789794

790-
CI.clearDependencyCollectors();
791795
auto MDC = initializeScanInstanceDependencyCollector(
792796
CI, std::make_unique<DependencyOutputOptions>(*OutputOpts), CWD, Consumer,
793797
Worker.Service,
@@ -856,7 +860,9 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
856860
Consumer.handleBuildCommand(
857861
{CommandLine[0], ModuleInvocation.getCC1CommandLine()});
858862

859-
// Remove the PPCallbacks since they are going out of scope.
863+
// Remove the DependencyCollecgtors and PPCallbacks since they are going out
864+
// of scope.
865+
CI.clearDependencyCollectors();
860866
CI.getPreprocessor().removePPCallbacks();
861867
return llvm::Error::success();
862868
}

clang/test/ClangScanDeps/link-libraries.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module transitive {
3232
}]
3333

3434
// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
35-
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full -module-name=root > %t/result.json
35+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full -module-names=root > %t/result.json
3636
// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s
3737

3838
// CHECK: {

clang/test/ClangScanDeps/modules-full-by-mod-name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module transitive { header "transitive.h" }
2525
}]
2626

2727
// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
28-
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full -module-name=root > %t/result.json
28+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full -module-names=root > %t/result.json
2929
// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s
3030

3131
// CHECK: {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
//--- module.modulemap
5+
module root { header "root.h" }
6+
module direct { header "direct.h" }
7+
module transitive { header "transitive.h" }
8+
module root1 { header "root1.h"}
9+
//--- root.h
10+
#include "direct.h"
11+
#include "root/textual.h"
12+
13+
//--- root1.h
14+
#include "direct.h"
15+
16+
//--- direct.h
17+
#include "transitive.h"
18+
//--- transitive.h
19+
// empty
20+
21+
//--- root/textual.h
22+
// This is here to verify that the "root" directory doesn't clash with name of
23+
// the "root" module.
24+
25+
//--- cdb.json.template
26+
[{
27+
"file": "",
28+
"directory": "DIR",
29+
"command": "clang -fmodules -fmodules-cache-path=DIR/cache -I DIR -x c"
30+
}]
31+
32+
// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
33+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full -module-names=root,root1,direct > %t/result.json
34+
// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s
35+
36+
// CHECK: {
37+
// CHECK-NEXT: "modules": [
38+
// CHECK-NEXT: {
39+
// CHECK-NEXT: "clang-module-deps": [
40+
// CHECK-NEXT: {
41+
// CHECK-NEXT: "context-hash": "{{.*}}",
42+
// CHECK-NEXT: "module-name": "transitive"
43+
// CHECK-NEXT: }
44+
// CHECK-NEXT: ],
45+
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/module.modulemap",
46+
// CHECK-NEXT: "command-line": [
47+
// CHECK: ],
48+
// CHECK-NEXT: "context-hash": "{{.*}}",
49+
// CHECK-NEXT: "file-deps": [
50+
// CHECK-NEXT: "[[PREFIX]]/module.modulemap",
51+
// CHECK-NEXT: "[[PREFIX]]/direct.h"
52+
// CHECK-NEXT: ],
53+
// CHECK-NEXT: "link-libraries": [],
54+
// CHECK-NEXT: "name": "direct"
55+
// CHECK-NEXT: },
56+
// CHECK-NEXT: {
57+
// CHECK-NEXT: "clang-module-deps": [
58+
// CHECK-NEXT: {
59+
// CHECK-NEXT: "context-hash": "{{.*}}",
60+
// CHECK-NEXT: "module-name": "direct"
61+
// CHECK-NEXT: }
62+
// CHECK-NEXT: ],
63+
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/module.modulemap",
64+
// CHECK-NEXT: "command-line": [
65+
// CHECK: ],
66+
// CHECK-NEXT: "context-hash": "{{.*}}",
67+
// CHECK-NEXT: "file-deps": [
68+
// CHECK-NEXT: "[[PREFIX]]/module.modulemap",
69+
// CHECK-NEXT: "[[PREFIX]]/root.h",
70+
// CHECK-NEXT: "[[PREFIX]]/root/textual.h"
71+
// CHECK-NEXT: ],
72+
// CHECK-NEXT: "link-libraries": [],
73+
// CHECK-NEXT: "name": "root"
74+
// CHECK-NEXT: },
75+
// CHECK-NEXT: {
76+
// CHECK-NEXT: "clang-module-deps": [
77+
// CHECK-NEXT: {
78+
// CHECK-NEXT: "context-hash": "{{.*}}",
79+
// CHECK-NEXT: "module-name": "direct"
80+
// CHECK-NEXT: }
81+
// CHECK-NEXT: ],
82+
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/module.modulemap",
83+
// CHECK-NEXT: "command-line": [
84+
// CHECK: ],
85+
// CHECK-NEXT: "context-hash": "{{.*}}",
86+
// CHECK-NEXT: "file-deps": [
87+
// CHECK-NEXT: "[[PREFIX]]/module.modulemap",
88+
// CHECK-NEXT: "[[PREFIX]]/root1.h"
89+
// CHECK-NEXT: ],
90+
// CHECK-NEXT: "link-libraries": [],
91+
// CHECK-NEXT: "name": "root1"
92+
// CHECK-NEXT: },
93+
// CHECK-NEXT: {
94+
// CHECK-NEXT: "clang-module-deps": [],
95+
// CHECK-NEXT: "clang-modulemap-file": "[[PREFIX]]/module.modulemap",
96+
// CHECK-NEXT: "command-line": [
97+
// CHECK: ],
98+
// CHECK-NEXT: "context-hash": "{{.*}}",
99+
// CHECK-NEXT: "file-deps": [
100+
// CHECK-NEXT: "[[PREFIX]]/module.modulemap",
101+
// CHECK-NEXT: "[[PREFIX]]/transitive.h"
102+
// CHECK-NEXT: ],
103+
// CHECK-NEXT: "link-libraries": [],
104+
// CHECK-NEXT: "name": "transitive"
105+
// CHECK-NEXT: }
106+
// CHECK-NEXT: ],
107+
// CHECK-NEXT: "translation-units": []
108+
// CHECK-NEXT: }

clang/test/Modules/transitive-system.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// RUN: split-file %s %t
33

44
// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
5-
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full -module-name=direct > %t/result1.json
5+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full -module-names=direct > %t/result1.json
66
// RUN: rm -rf %t/cache
7-
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full -module-name=transitive > %t/result2.json
7+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full -module-names=transitive > %t/result2.json
88
// RUN: %deps-to-rsp %t/result1.json --module-name transitive > %t/1.rsp
99
// RUN: %deps-to-rsp %t/result2.json --module-name transitive > %t/2.rsp
1010
// RUN: diff %t/1.rsp %t/2.rsp

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static std::string ModuleFilesDir;
8787
static bool EagerLoadModules;
8888
static unsigned NumThreads = 0;
8989
static std::string CompilationDB;
90-
static std::optional<std::string> ModuleName;
90+
static std::optional<std::string> ModuleNames;
9191
static std::vector<std::string> ModuleDepTargets;
9292
static std::string TranslationUnitFile;
9393
static bool DeprecatedDriverCommand;
@@ -205,8 +205,8 @@ static void ParseArgs(int argc, char **argv) {
205205
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_compilation_database_EQ))
206206
CompilationDB = A->getValue();
207207

208-
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_module_name_EQ))
209-
ModuleName = A->getValue();
208+
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_module_names_EQ))
209+
ModuleNames = A->getValue();
210210

211211
for (const llvm::opt::Arg *A : Args.filtered(OPT_dependency_target_EQ))
212212
ModuleDepTargets.emplace_back(A->getValue());
@@ -1018,7 +1018,7 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
10181018
};
10191019

10201020
if (Format == ScanningOutputFormat::Full)
1021-
FD.emplace(!ModuleName ? Inputs.size() : 0);
1021+
FD.emplace(!ModuleNames ? Inputs.size() : 0);
10221022

10231023
std::atomic<size_t> NumStatusCalls = 0;
10241024
std::atomic<size_t> NumOpenFileForReadCalls = 0;
@@ -1092,7 +1092,11 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
10921092
MakeformatOS, Errs))
10931093
HadErrors = true;
10941094
}
1095-
} else if (ModuleName) {
1095+
} else if (ModuleNames) {
1096+
StringRef ModuleNameRef(*ModuleNames);
1097+
SmallVector<StringRef> Names;
1098+
ModuleNameRef.split(Names, ',');
1099+
10961100
if (llvm::Error Err = WorkerTool.initializeCompilerInstanceWithContext(
10971101
CWD, Input->CommandLine)) {
10981102
handleErrorWithInfoString(
@@ -1102,12 +1106,16 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
11021106
continue;
11031107
}
11041108

1105-
auto MaybeModuleDepsGraph =
1106-
WorkerTool.computeDependenciesByNameWithContext(
1107-
*ModuleName, AlreadySeenModules, LookupOutput);
1108-
if (handleModuleResult(*ModuleName, MaybeModuleDepsGraph, *FD,
1109-
LocalIndex, DependencyOS, Errs))
1110-
HadErrors = true;
1109+
for (auto N : Names) {
1110+
auto MaybeModuleDepsGraph =
1111+
WorkerTool.computeDependenciesByNameWithContext(
1112+
N, AlreadySeenModules, LookupOutput);
1113+
if (handleModuleResult(N, MaybeModuleDepsGraph, *FD, LocalIndex,
1114+
DependencyOS, Errs)) {
1115+
HadErrors = true;
1116+
break;
1117+
}
1118+
}
11111119

11121120
if (llvm::Error Err =
11131121
WorkerTool.finalizeCompilerInstanceWithContext()) {

clang/tools/clang-scan-deps/Opts.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def eager_load_pcm : F<"eager-load-pcm", "Load PCM files eagerly (instead of laz
2626
def j : Arg<"j", "Number of worker threads to use (default: use all concurrent threads)">;
2727

2828
defm compilation_database : Eq<"compilation-database", "Compilation database">;
29-
defm module_name : Eq<"module-name", "the module of which the dependencies are to be computed">;
29+
defm module_names
30+
: Eq<"module-names", "A comma separated list of names of modules of which "
31+
"the dependencies are to be computed">;
3032
defm dependency_target : Eq<"dependency-target", "The names of dependency targets for the dependency file">;
3133

3234
defm tu_buffer_path: Eq<"tu-buffer-path", "The path to the translation unit for depscan. Not compatible with -module-name">;

0 commit comments

Comments
 (0)