clangd / clangd

clangd language server
https://clangd.llvm.org
Apache License 2.0
1.54k stars 65 forks source link

Crash on parsing compile_commands.json #1033

Closed jacknicklenson closed 2 years ago

jacknicklenson commented 2 years ago

Please describe the problem.

When parsing compile_commands.json file, it crashes 5 times and stop working. However code compiles fine and there is no error. If I add .clangd file in root of the project with the content of:

CompileFlags:
  CompilationDatabase: ./compile_commands.json

clangd did not crashed but this time just system header files are regonized, not the project files itself, it did not see raylib headers for example.

Here is the project folder structure:

.
├── LICENSE
├── README.md
├── build
│   ├── build.ninja
│   ├── build_cnode_Debug_x64.ninja
│   ├── build_cnode_Release_x64.ninja
│   ├── cnode
│   └── obj
│       └── x64
│           ├── Debug
│           │   ├── main.o
│           │   └── window.o
│           └── Release
│               └── main.o
├── build_linux.sh
├── build_windows.bat
├── compile_commands.json
├── dep
│   ├── getopt
│   │   └── xgetopt.h
│   ├── linux
│   │   └── raylib
│   │       ├── inc
│   │       │   ├── raygui.h
│   │       │   ├── raylib.h
│   │       │   ├── raymath.h
│   │       │   └── rlgl.h
│   │       └── lib
│   │           └── libraylib.a
│   └── windows
│       └── raylib
│           ├── inc
│           │   ├── raygui.h
│           │   ├── raylib.h
│           │   ├── raymath.h
│           │   └── rlgl.h
│           └── lib
│               └── libraylib.a
├── inc
│   └── window.h
├── premake5.lua
├── src
     ├── main.c
     └── window.c

If you can, provide a minimal chunk of code that shows the problem (either inline, or attach it if larger).

[
  {
    "directory": "/home/cx/Documents/Github/cnode/build",
    "command": "gcc -pedantic -m64 -g -Wall -Wextra -std=c99 -DDEBUG -I../inc -I../src -I../dep -I../dep/linux/raylib/inc -MMD -MF obj/x64/Debug/main.o.d -c -o obj/x64/Debug/main.o ../src/main.c",
    "file": "../src/main.c",
    "output": "obj/x64/Debug/main.o"
  },
  {
    "directory": "/home/cx/Documents/Github/cnode/build",
    "command": "gcc -pedantic -m64 -g -Wall -Wextra -std=c99 -DDEBUG -I../inc -I../src -I../dep -I../dep/linux/raylib/inc -MMD -MF obj/x64/Debug/window.o.d -c -o obj/x64/Debug/window.o ../src/window.c",
    "file": "../src/window.c",
    "output": "obj/x64/Debug/window.o"
  },
  {
    "directory": "/home/cx/Documents/Github/cnode/build",
    "command": "gcc -o cnode obj/x64/Debug/main.o obj/x64/Debug/window.o  -lraylib -lGL -lm -ldl -lpthread  -L../dep/linux/raylib/lib -L/usr/lib64 -m64",
    "file": "obj/x64/Debug/main.o",
    "output": "cnode"
  },
  {
    "directory": "/home/cx/Documents/Github/cnode/build",
    "command": "",
    "file": "cnode",
    "output": "cnode_Debug"
  }
]

This compile_commands.json file is created with premake and then ninja -f build_cnode_Debug_x64.ninja -t compdb command in build/ directory and compile_commands.json put it on the root of the project.

Logs

Please attach the clangd stderr log if you can. (Usually available from the editor) If possible, run with --log=verbose - note that the logs will include the contents of open files! If this is a crash, try to put llvm-symbolizer on your PATH per the troubleshooting instructions.

log file -> clangd_log.txt

System information

Output of clangd --version:

clangd version 13.0.1
Features: linux
Platform: x86_64-pc-linux-gnu

Editor/LSP plugin: VSCode

Operating system: Arch Linux

sam-mccall commented 2 years ago

I'll try to repro, but any chance you can put llvm-symbolizer on your path so we can get a useful stack trace of the crash? It doesn't look like it's crashing in JSON parsing per see, but probably after starting to use the commands for indexing or so.

CompilationDatabase: ./compile_commands.json

This isn't valid, it should point at the directory containing compile_commands.json. Probably with this configuration it won't find any CDB at all.

jacknicklenson commented 2 years ago

Here is with the llvm-symbolizer:

clangd_log_with_llvm_symbolizer.txt

thx for interest,

rwols commented 2 years ago

At least this stands out:

V[13:20:21.899] BackgroundIndex: serving version 1 (2401874 bytes)
I[13:20:21.900] --> $/progress
V[13:20:21.900] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"0/3","percentage":0}}}

I[13:20:21.900] --> $/progress
V[13:20:21.900] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"1/3","percentage":33}}}

I[13:20:21.900] --> $/progress
V[13:20:21.900] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"1/3","percentage":33}}}

I[13:20:21.900] --> $/progress
V[13:20:21.901] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"1/3","percentage":33}}}

LLVM ERROR: SmallVector unable to grow. Requested capacity (18446744073709551615) is larger than maximum value for size type (4294967295)

18446744073709551615 is 2^64 − 1.

jacknicklenson commented 2 years ago

i have used another compile_commands generator which is this site https://texttoolkit.com/compilation-database-generator. I just copy-paste the output of make -nw into the site and it gave me the compilation database file which its content is:

[
  {
    "directory": "/home/cx/Documents/Github/cnode/build",
    "file": "../src/main.c",
    "command": "cc   -MMD -MP -DDEBUG -I../inc -I../src -I../dep -I../dep/linux/raylib/inc -m64 -g -Wall -Wextra -std=c99 -pedantic  -o \"obj/x64/Debug/main.o\" -MF \"obj/x64/Debug/main.d\" -c \"../src/main.c\""
  },
  {
    "directory": "/home/cx/Documents/Github/cnode/build",
    "file": "../src/window.c",
    "command": "cc   -MMD -MP -DDEBUG -I../inc -I../src -I../dep -I../dep/linux/raylib/inc -m64 -g -Wall -Wextra -std=c99 -pedantic  -o \"obj/x64/Debug/window.o\" -MF \"obj/x64/Debug/window.d\" -c \"../src/window.c\""
  }
]

This time clangd did not crashed and worked great. Probably either ninja's built-in compilation database generator or the premake's ninja build file generator has a problem i think.

By the way i am using third-party ninja generator module for premake because premake does not have built-in ninja generator. Module is this: https://github.com/jimon/premake-ninja After ninja build files generated, runing ninja -f build_cnode_Debug_x64.ninja -t compdb > compile_commands.json to obtain database.

sam-mccall commented 2 years ago

From the crashing log file (there are no crash dumps there though):

Indexing obj/x64/Debug/main.o (digest:=7BC91203B88461BC)

It looks like the compile_commands.json produced there includes commands to "parse" object files! It would be nice if clang didn't crash when trying to parse binary data as C++ code I suppose!

jacknicklenson commented 2 years ago

Hi, To be more helpful, i just created dummy project with all the source and build files in it. You can investigate by yourself. Just open folder in vscode and Ctrl + Shift + B, it will generate binary and compile_commands.json and then when restarting clangd it crashes. However program runs well with F5.

dummy.zip

BTW Linux environment.

HighCommander4 commented 2 years ago

I built the attached project (had to add "dl" and "pthread" to the list of links in premake5.lua to get it to link) and restarted clangd, no crash for me. Do you have a particular file open when it crashes?

From the crashing log file (there are no crash dumps there though):

Indexing obj/x64/Debug/main.o (digest:=7BC91203B88461BC)

It looks like the compile_commands.json produced there includes commands to "parse" object files! It would be nice if clang didn't crash when trying to parse binary data as C++ code I suppose!

The message I see is:

Indexing <workspace>/build/obj/main.o failed: Couldn't build compiler invocation

I think that suggests the indexer bails out before it gets to a stage where it would try to parse the binary as C++ code.

jacknicklenson commented 2 years ago

Do you have a particular file open when it crashes?

no just main.c and to be sure, i have downloaded my dummy.zip, builded it and restarted clangd server to run in a clean environment but still same issue it crashes :(

clangd version: 13.0.1 -- Arch Linux

HighCommander4 commented 2 years ago

I was testing with a trunk build of clangd. Tried again with clangd 13, and now I get the same crash.

This is good news, as it means the issue has been fixed on trunk (I also checked the 14.x branch, fixed there too).

LLVM 14 should be released soon (it's currently in the release candidate stage), in the meantime you can use a weekly snapshot build from https://github.com/clangd/clangd/releases to pick up the fix.

jacknicklenson commented 2 years ago

Oohhh very nice thank you 👍