跳转至

插件冲突问题

已诊断并修复 VSCode 中"跳转到定义"无法工作的问题。

根本原因

项目同时安装了 vscode-clangdms-vscode.cpptools 两个 C/C++ 扩展,二者冲突。clangd 扩展启用后会自动接管 IntelliSense 并忽略 C_Cpp.default.compileCommands 设置,导致没有有效的编译数据库可用,跳转失效。

已完成的改动

  1. 创建 .clangd 配置文件(项目根目录)
  2. 显式指定 CompilationDatabase: build 指向 build/compile_commands.json
  3. 移除会导致 clangd 报错的 -Werror 标志
  4. 开启后台索引、Inlay Hints、ClangTidy

  5. 更新 .vscode/settings.json

  6. 配置 clangd 启动参数(--compile-commands-dir=build--background-index 等)
  7. 显式禁用 cpptools 的 IntelliSense/自动补全/错误波浪线,避免与 clangd 冲突
  8. 添加 MLIR/TableGen/LLVM 文件关联
  9. 从搜索中排除大型第三方目录(llvm-project/iree/torch-mlir)以加速索引

  10. 创建 compile_commands.json 符号链接到项目根目录(双保险,clangd 默认查找路径)

  11. 清理根目录残留的旧 settings.json

验证结果

  • compile_commands.json 有效(5908 条目,其中 594 个属于项目自身代码)
  • PoolingDescriptors.cpp 等项目文件的编译命令存在且所有 -I include 路径均存在
  • build/include/ 生成的头文件目录(含 *.h.inc)存在
  • clangd 15.0.7 已安装,与项目使用的 clang++ 15 版本匹配

你需要做的操作

在 VSCode 中按 Ctrl+Shift+P 执行 "Developer: Reload Window" 重新加载窗口。重载后 clangd 会开始后台索引(首次可能需 1-2 分钟),完成后"跳转到定义"(F12) 即可正常工作。

注意:本项目的 .gitignore 中已包含 .vscode/,所以这些配置文件不会被提交到仓库,属于本地开发环境配置。


问题诊断

经过探索,我发现"跳转到定义"无法工作的根本原因是:你同时安装了两个互相冲突的 C/C++ 扩展,而且当前配置只对其中一个生效。

关键发现

  1. .vscode/settings.json 已就位(你已移动)✓,其中配置了:

    JSON
    "C_Cpp.default.compileCommands": "/home/ken/workspace/cix-compiler/build/compile_commands.json"
    
    这是 cpptools (ms-vscode.cpptools) 扩展的设置。

  2. compile_commands.json 存在且有效

  3. 位于 build/compile_commands.json(11MB,5908 个条目)
  4. 项目自身的文件(如 PoolingDescriptors.cpp)都有对应编译命令
  5. 使用 /home/ken/workspace/llvm15/build/bin/clang++(LLVM 15)

  6. ⚠️ 扩展冲突(核心问题)

  7. 你安装了 llvm-vs-code-extensions.vscode-clangd(0.6.0)
  8. 你安装了 ms-vscode.cpptools(1.32.2)
  9. clangd 扩展启用后会自动接管 C/C++ IntelliSense,并禁用 cpptools 的 IntelliSense。此时 C_Cpp.default.compileCommands 设置被忽略,而 clangd 走自己的路径查找 compile_commands.json

  10. clangd 环境可用

  11. clangd 15.0.7 已安装于 /home/ken/workspace/llvm15/build/bin/clangd
  12. 与项目使用的 clang++ 15 版本匹配
  13. 但项目缺少 .clangd 配置文件,且项目根目录没有 compile_commands.json 符号链接(clangd 默认会查找 build/compile_commands.json,理论上能找到,但建议显式配置更稳妥)

  14. 项目规模较大compile_commands.json 中 5787 个条目属于 third_party/(LLVM/MLIR/IREE),仅 116 个属于项目自身代码。clangd 若无过滤会索引大量第三方文件,影响性能。

推荐方案:使用 clangd(方案A,推荐)

理由:clangd 性能更好,更适合 LLVM/MLIR 这类大型项目,且版本已匹配。

需要做的改动: 1. 在 .vscode/settings.json 中确认 clangd 启用、cpptools IntelliSense 禁用 2. 创建 .clangd 配置文件,显式指定 compile_commands.json 路径并优化索引范围 3. 在项目根目录创建 compile_commands.json 符号链接指向 build/compile_commands.json(双保险) 4. (可选)清理根目录残留的旧 settings.json

备选方案:使用 cpptools(方案B)

如果你想继续用 cpptools: 1. 禁用/卸载 vscode-clangd 扩展 2. 当前 .vscode/settings.json 配置已正确,重新加载窗口即可


我推荐方案A(clangd)。请问你倾向哪个方案?如果同意方案A,请 toggle to Act mode,我将创建 .clangd 配置文件并更新 .vscode/settings.json