Skip to content

Commit cb731be

Browse files
fleeting-xxrorth
authored andcommitted
[clangd] [Modules] Fix to correctly handle module dependencies (llvm#142828)
This is a re-application of llvm#142090 without the unit test changes. A subsequent PR will follow that adds a unit test for module dependencies. ### Changes - Fix dangling string references in the return value of getAllRequiredModules() - Change a couple of calls in getOrBuildModuleFile() to use the loop variable instead of the ModuleName parameter. @ChuanqiXu9 for review
1 parent a341f53 commit cb731be

File tree

2 files changed

+108
-10
lines changed

2 files changed

+108
-10
lines changed

clang-tools-extra/clangd/ModulesBuilder.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ class CachingProjectModules : public ProjectModules {
430430
/// Collect the directly and indirectly required module names for \param
431431
/// ModuleName in topological order. The \param ModuleName is guaranteed to
432432
/// be the last element in \param ModuleNames.
433-
llvm::SmallVector<StringRef> getAllRequiredModules(PathRef RequiredSource,
434-
CachingProjectModules &MDB,
435-
StringRef ModuleName) {
436-
llvm::SmallVector<llvm::StringRef> ModuleNames;
433+
llvm::SmallVector<std::string> getAllRequiredModules(PathRef RequiredSource,
434+
CachingProjectModules &MDB,
435+
StringRef ModuleName) {
436+
llvm::SmallVector<std::string> ModuleNames;
437437
llvm::StringSet<> ModuleNamesSet;
438438

439439
auto VisitDeps = [&](StringRef ModuleName, auto Visitor) -> void {
@@ -444,7 +444,7 @@ llvm::SmallVector<StringRef> getAllRequiredModules(PathRef RequiredSource,
444444
if (ModuleNamesSet.insert(RequiredModuleName).second)
445445
Visitor(RequiredModuleName, Visitor);
446446

447-
ModuleNames.push_back(ModuleName);
447+
ModuleNames.push_back(ModuleName.str());
448448
};
449449
VisitDeps(ModuleName, VisitDeps);
450450

@@ -494,28 +494,30 @@ llvm::Error ModulesBuilder::ModulesBuilderImpl::getOrBuildModuleFile(
494494
// Get Required modules in topological order.
495495
auto ReqModuleNames = getAllRequiredModules(RequiredSource, MDB, ModuleName);
496496
for (llvm::StringRef ReqModuleName : ReqModuleNames) {
497-
if (BuiltModuleFiles.isModuleUnitBuilt(ModuleName))
497+
if (BuiltModuleFiles.isModuleUnitBuilt(ReqModuleName))
498498
continue;
499499

500500
if (auto Cached = Cache.getModule(ReqModuleName)) {
501501
if (IsModuleFileUpToDate(Cached->getModuleFilePath(), BuiltModuleFiles,
502502
TFS.view(std::nullopt))) {
503-
log("Reusing module {0} from {1}", ModuleName,
503+
log("Reusing module {0} from {1}", ReqModuleName,
504504
Cached->getModuleFilePath());
505505
BuiltModuleFiles.addModuleFile(std::move(Cached));
506506
continue;
507507
}
508508
Cache.remove(ReqModuleName);
509509
}
510510

511+
std::string ReqFileName =
512+
MDB.getSourceForModuleName(ReqModuleName, RequiredSource);
511513
llvm::Expected<ModuleFile> MF = buildModuleFile(
512-
ModuleName, ModuleUnitFileName, getCDB(), TFS, BuiltModuleFiles);
514+
ReqModuleName, ReqFileName, getCDB(), TFS, BuiltModuleFiles);
513515
if (llvm::Error Err = MF.takeError())
514516
return Err;
515517

516-
log("Built module {0} to {1}", ModuleName, MF->getModuleFilePath());
518+
log("Built module {0} to {1}", ReqModuleName, MF->getModuleFilePath());
517519
auto BuiltModuleFile = std::make_shared<const ModuleFile>(std::move(*MF));
518-
Cache.add(ModuleName, BuiltModuleFile);
520+
Cache.add(ReqModuleName, BuiltModuleFile);
519521
BuiltModuleFiles.addModuleFile(std::move(BuiltModuleFile));
520522
}
521523

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# A smoke test to check that a simple dependency chain for modules can work.
2+
#
3+
# FIXME: This fails on the Windows ARM64 build server. Not entirely sure why as it has been tested on
4+
# an ARM64 Windows VM and appears to work there.
5+
# UNSUPPORTED: host=aarch64-pc-windows-msvc
6+
#
7+
# RUN: rm -fr %t
8+
# RUN: mkdir -p %t
9+
# RUN: split-file %s %t
10+
#
11+
# RUN: sed -e "s|DIR|%/t|g" %t/compile_commands.json.tmpl > %t/compile_commands.json.tmp
12+
# RUN: sed -e "s|CLANG_CC|%clang|g" %t/compile_commands.json.tmp > %t/compile_commands.json
13+
# RUN: sed -e "s|DIR|%/t|g" %t/definition.jsonrpc.tmpl > %t/definition.jsonrpc.tmp
14+
#
15+
# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
16+
# (with the extra slash in the front), so we add it here.
17+
# RUN: sed -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' %/t/definition.jsonrpc.tmp > %/t/definition.jsonrpc
18+
#
19+
# RUN: clangd -experimental-modules-support -lit-test < %t/definition.jsonrpc \
20+
# RUN: | FileCheck -strict-whitespace %t/definition.jsonrpc
21+
22+
#--- A-frag.cppm
23+
export module A:frag;
24+
export void printA() {}
25+
26+
#--- A.cppm
27+
export module A;
28+
export import :frag;
29+
30+
#--- Use.cpp
31+
import A;
32+
void foo() {
33+
print
34+
}
35+
36+
#--- compile_commands.json.tmpl
37+
[
38+
{
39+
"directory": "DIR",
40+
"command": "CLANG_CC -fprebuilt-module-path=DIR -std=c++20 -o DIR/main.cpp.o -c DIR/Use.cpp",
41+
"file": "DIR/Use.cpp"
42+
},
43+
{
44+
"directory": "DIR",
45+
"command": "CLANG_CC -std=c++20 DIR/A.cppm --precompile -o DIR/A.pcm",
46+
"file": "DIR/A.cppm"
47+
},
48+
{
49+
"directory": "DIR",
50+
"command": "CLANG_CC -std=c++20 DIR/A-frag.cppm --precompile -o DIR/A-frag.pcm",
51+
"file": "DIR/A-frag.cppm"
52+
}
53+
]
54+
55+
#--- definition.jsonrpc.tmpl
56+
{
57+
"jsonrpc": "2.0",
58+
"id": 0,
59+
"method": "initialize",
60+
"params": {
61+
"processId": 123,
62+
"rootPath": "clangd",
63+
"capabilities": {
64+
"textDocument": {
65+
"completion": {
66+
"completionItem": {
67+
"snippetSupport": true
68+
}
69+
}
70+
}
71+
},
72+
"trace": "off"
73+
}
74+
}
75+
---
76+
{
77+
"jsonrpc": "2.0",
78+
"method": "textDocument/didOpen",
79+
"params": {
80+
"textDocument": {
81+
"uri": "file://DIR/Use.cpp",
82+
"languageId": "cpp",
83+
"version": 1,
84+
"text": "import A;\nvoid foo() {\n print\n}\n"
85+
}
86+
}
87+
}
88+
89+
# CHECK: "message"{{.*}}printA{{.*}}(fix available)
90+
91+
---
92+
{"jsonrpc":"2.0","id":1,"method":"textDocument/completion","params":{"textDocument":{"uri":"file://DIR/Use.cpp"},"context":{"triggerKind":1},"position":{"line":2,"character":6}}}
93+
---
94+
{"jsonrpc":"2.0","id":2,"method":"shutdown"}
95+
---
96+
{"jsonrpc":"2.0","method":"exit"}

0 commit comments

Comments
 (0)