使用环境

<!-- 必须 告知插件运行的环境,帮助开发者定位问题 -->

  • 操作系统版本:Windows_NT x64 10.0.22631
  • 操作系统平台(x86/x64):x64
  • VsCode 版本:版本: 1.93.1
  • EIDE 插件版本:v3.20.0
  • C/C++ 插件版本:未使用,
    • 使用的clangd,插件版本:v0.1.29
    • clang版本 :18.1.6
  • 何种编译器(keil_c51/sdcc/armcc5/armgcc/...):armv6(keil中的v6,应该是clang?)
  • 编译器版本(非编译问题可忽略):

描述问题

<!-- 使用简明清晰的语言描述您的问题 -->
使用STM32CubeMX创建的keil v5工程,可以正常编译,但无法跳转函数定义,尝试了Clangd 无法跳转至源文件,只能进入头文件中提到的解决方法并未奏效
我自行添加了如下任务

"afterBuildTasks": [
            {
              "name": "生成的hex地址",
              "disable": false,
              "abortAfterFailed": false,
              "command": "echo \"firmware: ${OutDir}/${ProjectName}.hex\""
            },
            {
              "name": "添加compile_commands.json",
              "disable": false,
              "abortAfterFailed": false,
              "command": "bash -c \"cp build/${ConfigName}/compile_commands.json build/compile_commands.json\""
            },
            {
              "name": "替换compile_commands.json中的盘符为大写并转换路径分隔符",
              "disable": false,
              "abortAfterFailed": false,
              "command": "bash ./update_compile_commands.sh"
            }
          ],
          "global": {
            "output-debug-info": "enable"
          },
          "c/cpp-compiler": {
            "optimization": "level-0",
            "language-c": "c99",
            "language-cpp": "c++14",
            "warnings": "ac5-like-warnings"
          },
          "asm-compiler": {
            "$use": "asm-auto"
          },
          "linker": {
            "output-format": "elf",
            "misc-controls": "--diag_suppress=L6329"
          }

试图进行compile_commands.json文件的生成以及大小写和转义符处理

但是从clangd输出来看,并没有成功大小写转换,

V[01:08:39.150] <<< {
  "id": 150,
  "jsonrpc": "2.0",
  "method": "textDocument/documentHighlight",
  "params": {
    "position": {
      "character": 10,
      "line": 103
    },
    "textDocument": {
      "uri": "file:///d%3A/Codes/Project%20ARM/HAL/Development_Boards/DM_MC02/ClangdTest/Src/main.c"
    }
  }
}

I[01:08:39.150] <-- textDocument/documentHighlight(150)
V[01:08:39.153] ASTWorker running Highlights on version 3 of d:/Codes/Project ARM/HAL/Development_Boards/DM_MC02/ClangdTest/Src/main.c
I[01:08:39.153] --> reply:textDocument/documentHighlight(150) 3 ms
V[01:08:39.153] >>> {
  "id": 150,
  "jsonrpc": "2.0",
  "result": [
    {
      "kind": 1,
      "range": {
        "end": {
          "character": 13,
          "line": 103
        },
        "start": {
          "character": 4,
          "line": 103
        }
      }
    }
  ]
}

I[01:08:39.154] --> textDocument/clangd.fileStatus
V[01:08:39.154] >>> {
  "jsonrpc": "2.0",
  "method": "textDocument/clangd.fileStatus",
  "params": {
    "state": "idle",
    "uri": "file:///d:/Codes/Project%20ARM/HAL/Development_Boards/DM_MC02/ClangdTest/Src/main.c"
  }
}

uri依然是小写,但是compile_commands.json中已经是大写了(等下,会不会是我的路径中包含空格,我明天尝试修改一下),以下是compile_commands.json的一部分

[{"directory":"D:/Codes/Project ARM/HAL/Development_Boards/DM_MC02/ClangdTest","file":"D:/Codes/Project ARM/HAL/Development_Boards/DM_MC02/ClangdTest/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal.c","command":"\"D:/Keil_v5/ARM/ARMCLANG/bin/armclang.exe\" --target=arm-arm-none-eabi -c -xc -std=c99 -IInc -IDrivers/STM32H7xx_HAL_Driver/Inc -IDrivers/STM32H7xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32H7xx/Include -IDrivers/CMSIS/Include -I.cmsis/include -IMDK-ARM/RTE/_ClangdTest -DUSE_HAL_DRIVER -DSTM32H723xx -mfloat-abi=hard -mfpu=fpv5-d16 -mcpu=cortex-m7 -mlittle-endian -O0 -fno-function-sections -funsigned-char -fno-rtti -Wno-packed -Wno-missing-variable-declarations -Wno-missing-prototypes -Wno-missing-noreturn -Wno-sign-conversion -Wno-nonportable-include-path -Wno-reserved-id-macro -Wno-unused-macros -Wno-documentation-unknown-command -Wno-documentation -Wno-license-management -Wno-parentheses-equality -g -o ./build/ClangdTest/.obj/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal.o -MMD ./Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal.c"},

屏幕截图

<!--使用一些截图能够更好地展现问题 -->


强行跳转就得到下图

期望现象

<!--您期望应该产生的,但实际上却没有发生的结果-->
可以跳转到.c文件而不是只能跳转到.h

我已经移动到无空格的路径,问题依然存在

    CaFeZn_ 你每次从compiler_commands.json被更新之后,就要把那个原先的.cache/index文件整个删掉,然后关闭再打开vscode, 让其重新生成index。不然后面clangd使用的会是原来的index。

      Eplankton 您好,我刚刚尝试过了这个操作,删除后重新打开并没有再生成,不过再次打开后生成了,但是问题依然没有解决,我想确认一下是否是这个路径
      build\.cache\clangd\index
      而且我尝试过了Clangd的Restart language server
      也没有解决问题

        Eplankton 我突然发现我有两个compiler_commands.json文件

        我修改的是build\compile_commands.json这个文件,刚刚突然发现build\ClangdTest\compile_commands.json这里还有一个,我现在不确定clangd使用了哪一个,我想我可能找到原因了

          CaFeZn_
          我现在的构建后命令是bash ./update_compile_commands.sh ${ConfigName}
          我的update_compile_commands.sh内容是

          #!/bin/bash
          
          # 使用位置参数 $1 获取传递的 ConfigName 变量
          ConfigName="$1"
          
          # 复制compile_commands.json文件
          cp build/${ConfigName}/compile_commands.json build/compile_commands.json
          
          # 检查文件是否存在
          if [ -f "build/compile_commands.json" ]; then
              # 使用sed命令替换盘符为大写
              sed -i 's|[abcdefghijklmnopqrstuvwxyz]:\\|\U&|g' build/compile_commands.json
              # 使用sed命令替换转义符
              sed -i 's|\\\\|/|g' build/compile_commands.json
              echo "Drive letter replaced to uppercase in compile_commands.json."
          else
              echo "File not found: build//compile_commands.json"
          fi
          
          if [ -f "build/${ConfigName}/compile_commands.json" ]; then
              # 使用sed命令替换盘符为大写
              sed -i 's|[abcdefghijklmnopqrstuvwxyz]:\\|\U&|g' build/${ConfigName}/compile_commands.json
              # 使用sed命令替换转义符
              sed -i 's|\\\\|/|g' build/${ConfigName}/compile_commands.json
              echo "Drive letter replaced to uppercase in compile_commands.json."
          else
              echo "File not found: build//${ConfigName}/compile_commands.json"
          fi

          因为我不确定clangd使用了哪一个compile_commands.json,所以我保留了两个compile_commands.json,并且都替换了,但是依然没有生效,与最开始效果相同,我也尝试过了清除.cache/index以及使用了Clangd的Restart language server命令

          CaFeZn_ Clangd的Restart language server

          Write a Reply...