Skip to content

Commit 5b55899

Browse files
[clangd] Clangd running with --experimental-modules-support crashes when the compilation database is unavailable (llvm#153802)
fixes llvm#132413
1 parent 84ab301 commit 5b55899

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

clang-tools-extra/clangd/GlobalCompilationDatabase.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,10 @@ bool OverlayCDB::setCompileCommand(PathRef File,
833833
std::unique_ptr<ProjectModules>
834834
OverlayCDB::getProjectModules(PathRef File) const {
835835
auto MDB = DelegatingCDB::getProjectModules(File);
836+
if (!MDB) {
837+
log("Failed to get compilation Database for {0}", File);
838+
return {};
839+
}
836840
MDB->setCommandMangler([&Mangler = Mangler](tooling::CompileCommand &Command,
837841
PathRef CommandPath) {
838842
Mangler(Command, CommandPath);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# A smoke test to check that clangd works without compilation database
2+
#
3+
# Windows have different escaping modes.
4+
# FIXME: We should add one for windows.
5+
# UNSUPPORTED: system-windows
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/definition.jsonrpc.tmpl > %t/definition.jsonrpc
12+
#
13+
# RUN: clangd -experimental-modules-support -lit-test < %t/definition.jsonrpc \
14+
# RUN: | FileCheck -strict-whitespace %t/definition.jsonrpc
15+
16+
#--- A.h
17+
void printA();
18+
19+
#--- Use.cpp
20+
#include "A.h"
21+
void foo() {
22+
print
23+
}
24+
25+
#--- definition.jsonrpc.tmpl
26+
{
27+
"jsonrpc": "2.0",
28+
"id": 0,
29+
"method": "initialize",
30+
"params": {
31+
"processId": 123,
32+
"rootPath": "clangd",
33+
"capabilities": {
34+
"textDocument": {
35+
"completion": {
36+
"completionItem": {
37+
"snippetSupport": true
38+
}
39+
}
40+
}
41+
},
42+
"trace": "off"
43+
}
44+
}
45+
---
46+
{
47+
"jsonrpc": "2.0",
48+
"method": "textDocument/didOpen",
49+
"params": {
50+
"textDocument": {
51+
"uri": "file://DIR/Use.cpp",
52+
"languageId": "cpp",
53+
"version": 1,
54+
"text": "#include \"A.h\"\nvoid foo() {\n print\n}\n"
55+
}
56+
}
57+
}
58+
59+
# CHECK: "message"{{.*}}printA{{.*}}(fix available)
60+
61+
---
62+
{"jsonrpc":"2.0","id":1,"method":"textDocument/completion","params":{"textDocument":{"uri":"file://DIR/Use.cpp"},"context":{"triggerKind":1},"position":{"line":2,"character":6}}}
63+
---
64+
{"jsonrpc":"2.0","id":2,"method":"shutdown"}
65+
---
66+
{"jsonrpc":"2.0","method":"exit"}

0 commit comments

Comments
 (0)