clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
592 stars 97 forks source link

Clang generate wrong compile_commands.json that are unusable by clangd #640

Closed MatthieuHernandez closed 2 weeks ago

MatthieuHernandez commented 2 weeks ago

I'm using clang 18.1.6 and clangd extension v0.1.28 under Windows. When I run this command:

clang++ .\\examples\\example_1.cpp -std=c++23 -o example_1.exe -MJ compile_commands.json

It generates the following compile_commands.json:

{
    "directory": "C:\\Programming\\Git\\cpp-is-magic",
    "file": ".\\examples\\example_1.cpp",
    "output": "C:\\Users\\matth\\AppData\\Local\\Temp\\example_1-fd8c61.o",
    "arguments": [
        "C:\\Program Files\\LLVM\\bin\\clang++.exe",
        "-xc++",
        ".\\examples\\example_1.cpp",
        "-o",
        "C:\\Users\\matth\\AppData\\Local\\Temp\\example_1-fd8c61.o",
        "--driver-mode=g++",
        "-std=c++23",
        "-dumpdir",
        "example_1.exe-",
        "--target=x86_64-pc-windows-msvc19.35.32217"
    ]
},

and clangd under VS Code return this error:

Failed to load compilation database from C:/Programming/Git/cpp-is-magic/compile_commands.json: Expected array.

If I add brackets [] in my compile_commands.json it works perfectly. How can I correct this problem? Does this bug come from clang, clangd or the VS Code extension?

MatthieuHernandez commented 2 weeks ago

It seems that the problem is related solely to the clang compiler.