Skip to content

Commit f42bbcc

Browse files
authored
[SYCL][clangd] Avoid clangd crash with -fsycl flag (#11795)
Add four extra option resets in `disableUnsupportedOptions` when parsing SYCL code. All four are needed to make clangd happily parse code when `-fsycl` is specified in compile commands. Without each of them, the following happens: * without `SYCLIsDevice=false`: crash on !nullptr assert * without `DeclareSPIRVBuiltins=false`: crash on !nullptr assert * without `SYCLUnnamedLambda=false`: `ref_non_value` error, `'T' does not refer to a value` * without `Triple=HostTriple`: `pp_file_not_found` error, `'gnu/stubs-32.h' file not found` Fixes #11088 and clangd/clangd#1097 Also add a LIT test which parses the following code: ```cpp #include <sycl/sycl.hpp> sycl::queue q{}; ``` and checks `symbolInfo` for the symbol `q` against a reference.
1 parent 2c931ff commit f42bbcc

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

clang-tools-extra/clangd/Compiler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ void disableUnsupportedOptions(CompilerInvocation &CI) {
8989
CI.getLangOpts().ProfileListFiles.clear();
9090
CI.getLangOpts().XRayAlwaysInstrumentFiles.clear();
9191
CI.getLangOpts().XRayNeverInstrumentFiles.clear();
92+
if (CI.getLangOpts().SYCLIsDevice) {
93+
CI.getLangOpts().SYCLIsDevice = false;
94+
CI.getLangOpts().SYCLUnnamedLambda = false;
95+
CI.getLangOpts().DeclareSPIRVBuiltins = false;
96+
CI.getTargetOpts().Triple = CI.getTargetOpts().HostTriple;
97+
}
9298
}
9399

94100
std::unique_ptr<CompilerInvocation>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
2+
# RUN: echo '[{"directory": "%/t.dir", "command": "clang++ -fsycl main.cpp", "file": "main.cpp"}]' > %t.dir/compile_commands.json
3+
# RUN: sed -e "s|INPUT_DIR|%/t.dir|g" %s > %t.test.1
4+
5+
# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
6+
# (with the extra slash in the front), so we add it here.
7+
# RUN: sed -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' %t.test.1 > %t.test
8+
9+
# RUN: clangd -lit-test < %t.test | FileCheck -strict-whitespace %t.test
10+
11+
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"initializationOptions":{"compilationDatabasePath":"INPUT_DIR"}}}
12+
---
13+
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///main.cpp","languageId":"cpp","version":1,"text":"#include <sycl/sycl.hpp>\nsycl::queue q{};"}}}
14+
---
15+
{"jsonrpc":"2.0","id":1,"method":"textDocument/symbolInfo","params":{
16+
"textDocument":{"uri":"test:///main.cpp"},
17+
"position":{"line":1,"character":13}
18+
}}
19+
# CHECK: "id": 1
20+
# CHECK-NEXT: "jsonrpc": "2.0",
21+
# CHECK-NEXT: "result": [
22+
# CHECK-NEXT: {
23+
# CHECK-NEXT: "containerName": "sycl::queue::",
24+
# CHECK-NEXT: "declarationRange": {
25+
# CHECK-NEXT: "range": {
26+
# CHECK-NEXT: "end": {
27+
# CHECK-NEXT: "character": 16,
28+
# CHECK-NEXT: "line": 124
29+
# CHECK-NEXT: },
30+
# CHECK-NEXT: "start": {
31+
# CHECK-NEXT: "character": 11,
32+
# CHECK-NEXT: "line": 124
33+
# CHECK-NEXT: }
34+
# CHECK-NEXT: },
35+
# CHECK-NEXT: "uri": "file://{{.*}}/include/sycl/queue.hpp"
36+
# CHECK-NEXT: },
37+
---
38+
{"jsonrpc":"2.0","id":3,"method":"shutdown"}
39+
---
40+
{"jsonrpc":"2.0","method":"exit"}

0 commit comments

Comments
 (0)