Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jump into header files instead of source file #1592

Closed
Eplankton opened this issue Apr 13, 2023 · 46 comments
Closed

Jump into header files instead of source file #1592

Eplankton opened this issue Apr 13, 2023 · 46 comments

Comments

@Eplankton
Copy link

I have already read many people report that their clangd doesn't jump to defintion unless they open that file for at least one time, I have checked my compile_commands.json and ensure that certain src files are in there,
however, when trying to go to the definition, the log will say something like:

I[15:00:21.519] <-- textDocument/hover(187)
I[15:00:21.520] --> reply:textDocument/hover(187) 0 ms
I[15:00:21.520] --> textDocument/clangd.fileStatus
I[15:00:21.943] <-- textDocument/hover(188)
I[15:00:21.944] --> reply:textDocument/hover(188) 1 ms
I[15:00:21.944] --> textDocument/clangd.fileStatus
I[15:00:22.224] <-- textDocument/hover(189)
I[15:00:22.225] --> reply:textDocument/hover(189) 0 ms
I[15:00:22.225] --> textDocument/clangd.fileStatus
I[15:00:22.849] <-- textDocument/definition(190)
I[15:00:22.849] --> reply:textDocument/definition(190) 0 ms
I[15:00:22.849] --> textDocument/clangd.fileStatus
I[15:00:23.023] <-- textDocument/didOpen
I[15:00:23.025] <-- textDocument/didClose
I[15:00:23.025] --> textDocument/publishDiagnostics
I[15:00:23.026] Failed to find compilation database for d:\code\EIDE\template\CORE\stm32f4xx.h
I[15:00:23.027] ASTWorker building file d:\code\EIDE\template\CORE\stm32f4xx.h version 1 with command inferred from d:\code\EIDE\template\USR\main.cpp
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -I.cmsis/include -IRTE/_Target -I.eide/deps -Ic:/Keil_v5/ARM/ARMCLANG/include -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -Oz -flto -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -fno-exceptions -fno-rtti -g -Wno-empty-body -Wno-deprecated-volatile -MMD "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -x c++-header -std=c++2b -- "d:\\code\\EIDE\\template\\CORE\\stm32f4xx.h"

You can see that the clangd start in template folder, I set the compiler_commands_dir as nothing to avoid disturb, when I open the main.cpp file, it strangely read the database from USR/build:

I[15:05:19.603] clangd version 16.0.1
I[15:05:19.603] Features: windows
I[15:05:19.603] PID: 15044
I[15:05:19.603] Working directory: d:\code\EIDE\template
I[15:05:19.603] argv[0]: C:\Program Files\LLVM\bin\clangd.EXE
I[15:05:19.603] argv[1]: --header-insertion=never
I[15:05:19.608] Starting LSP over stdin/stdout
I[15:05:19.609] <-- initialize(0)
I[15:05:19.610] --> reply:initialize(0) 1 ms
I[15:05:19.611] <-- initialized
I[15:05:19.615] <-- textDocument/didOpen
I[15:05:19.616] --> textDocument/publishDiagnostics
I[15:05:19.617] Loaded compilation database from d:\code\EIDE\template\USR\build/compile_commands.json
@Eplankton
Copy link
Author

Eplankton commented Apr 13, 2023

The real problem is, I tried to create a new build folder under template and then move the compile_commands.json into such folder, and the complaints disappear:

I[15:14:15.998] <-- textDocument/definition(92)
I[15:14:15.999] --> reply:textDocument/definition(92) 0 ms
I[15:14:15.999] --> textDocument/clangd.fileStatus
I[15:14:16.013] <-- textDocument/didOpen
I[15:14:16.014] <-- textDocument/didClose
I[15:14:16.014] --> textDocument/publishDiagnostics
I[15:14:16.014] ASTWorker building file d:\code\EIDE\template\Driver\inc\stm32f4xx_syscfg.h version 1 with command inferred from d:\code\EIDE\template\USR\main.cpp
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -I.cmsis/include -IRTE/_Target -I.eide/deps -Ic:/Keil_v5/ARM/ARMCLANG/include -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -Oz -flto -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -fno-exceptions -fno-rtti -g -Wno-empty-body -Wno-deprecated-volatile -MMD "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -x c++-header -std=c++2b -- "d:\\code\\EIDE\\template\\Driver\\inc\\stm32f4xx_syscfg.h"
I[15:14:16.147] <-- textDocument/documentHighlight(93)
I[15:14:16.147] --> reply:textDocument/documentHighlight(93) 0 ms
I[15:14:16.147] --> textDocument/clangd.fileStatus
I[15:14:16.208] <-- textDocument/definition(94)
I[15:14:16.208] --> reply:textDocument/definition(94) 0 ms
I[15:14:16.208] --> textDocument/clangd.fileStatus
I[15:14:16.253] <-- textDocument/didOpen
I[15:14:16.254] ASTWorker building file d:\code\EIDE\template\Driver\inc\stm32f4xx_syscfg.h version 1 with command inferred from d:\code\EIDE\template\USR\main.cpp
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -I.cmsis/include -IRTE/_Target -I.eide/deps -Ic:/Keil_v5/ARM/ARMCLANG/include -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -Oz -flto -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -fno-exceptions -fno-rtti -g -Wno-empty-body -Wno-deprecated-volatile -MMD "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -x c++-header -std=c++2b -- "d:\\code\\EIDE\\template\\Driver\\inc\\stm32f4xx_syscfg.h"
I[15:14:16.255] --> textDocument/clangd.fileStatus
I[15:14:16.270] <-- textDocument/codeAction(95)
I[15:14:16.270] <-- textDocument/documentSymbol(96)
I[15:14:16.309] Built preamble of size 353756 for file d:\code\EIDE\template\Driver\inc\stm32f4xx_syscfg.h version 1 in 0.05 seconds

So we can know that the database is successfully imported here, however the Go to Definition still doesn't work, it jumps into the header file as usual.

@Eplankton
Copy link
Author

Another example, directly open certain file, no error tells Can't find header file or No database

I[15:23:12.151] <-- textDocument/didOpen
I[15:23:12.153] ASTWorker building file d:\code\EIDE\template\Driver\src\stm32f4xx_gpio.c version 1 with command 
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -xc -std=c11 -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -I.cmsis/include -IRTE/_Target -I.eide/deps -Ic:/Keil_v5/ARM/ARMCLANG/include -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -Oz -flto -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -Wno-empty-body -Wno-deprecated-volatile -o ./build/Release/__/Driver/src/stm32f4xx_gpio.o -MMD "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -- "d:\\code\\EIDE\\template\\Driver\\src\\stm32f4xx_gpio.c"
I[15:23:12.153] --> textDocument/clangd.fileStatus
I[15:23:12.173] <-- textDocument/codeAction(227)
I[15:23:12.174] <-- textDocument/documentSymbol(228)
I[15:23:12.185] <-- textDocument/semanticTokens/full(229)
I[15:23:12.188] <-- textDocument/documentLink(230)
I[15:23:12.197] <-- textDocument/inlayHint(231)
I[15:23:12.314] Built preamble of size 2002708 for file d:\code\EIDE\template\Driver\src\stm32f4xx_gpio.c version 1 in 0.16 seconds

@zyn0217
Copy link

zyn0217 commented Apr 13, 2023

I suggest you could develop a tiny demo project to present what you've encountered -- Sorry but we really have no idea what kind of stuff like build, template, USR and template\USR\main.cpp are.

@Eplankton
Copy link
Author

I suggest you could develop a tiny demo project to present what you've encountered -- Sorry but we really have no idea what kind of stuff like build, template, USR and template\USR\main.cpp are.

Sorry ,I can hardly do such thing... Because this project is imported from damn Keil, a embedded develop environment, which has something called Virtual Folder, It will disturb the whole structure of system, I can hardly know which one is the workspace...

@HighCommander4
Copy link

Could you share a full set of logs (from clangd startup, until after you've performed the jump that goes to the unexpected file) please? In addition, please mention the path of the file you expected to jump to.

@Eplankton
Copy link
Author

Eplankton commented Apr 14, 2023

Could you share a full set of logs (from clangd startup, until after you've performed the jump that goes to the unexpected file) please? In addition, please mention the path of the file you expected to jump to.

Sure, this is the project structure, I'd like to jump from gpio.hpp(here #include "stm32f4xx.h") to stm32f4xx_gpio.c:

template/
       |_____ .build/
       |           |______ compile_commands.json
       |_____ USR/
       |         |_____ main.cpp
       |         |_____nuts/
       |                 |______ gpio.hpp
       |_____Driver/
       |        |______inc/
       |                  |_______ stm32f4xx_gpio.h
       |        |______src/
       |                  |_______stm32f4xx_gpio.c
       |_____Project/
                    |___ .vscode
                    |___ .cache
I[20:59:11.252] clangd version 16.0.1
I[20:59:11.253] Features: windows
I[20:59:11.253] PID: 9728
I[20:59:11.253] Working directory: d:\code\EIDE\template
I[20:59:11.253] argv[0]: C:\Program Files\LLVM\bin\clangd.EXE
I[20:59:11.253] argv[1]: --header-insertion=never
I[20:59:11.257] Starting LSP over stdin/stdout
I[20:59:11.257] <-- initialize(0)
I[20:59:11.259] --> reply:initialize(0) 1 ms
I[20:59:11.260] <-- initialized
I[20:59:11.264] <-- textDocument/didOpen
I[20:59:11.265] --> textDocument/publishDiagnostics
I[20:59:11.266] Loaded compilation database from d:\code\EIDE\template\build/compile_commands.json
I[20:59:11.266] ASTWorker building file d:\code\EIDE\template\USR\nuts\gpio.hpp version 1 with command inferred from d:\code\EIDE\template\USR\main.cpp
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -Ic:/Keil_v5/ARM/ARMCLANG/include -I.cmsis/include -IRTE/_Target -I.eide/deps -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -Oz -flto -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -fno-exceptions -fno-rtti -g -Wno-empty-body -Wno-deprecated-volatile -MMD -std=c++2b --target=arm-arm-none-eabi -DSTM32F429_439xx -D__ARMCOMPILER_VERSION=6190004 -I../Driver/inc -IC:/Keil_v5/ARM/ARMCLANG/include -Ietl/ "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -- "d:\\code\\EIDE\\template\\USR\\nuts\\gpio.hpp"
I[20:59:11.267] --> window/workDoneProgress/create(0)
I[20:59:11.267] Enqueueing 47 commands for indexing
I[20:59:11.267] --> textDocument/clangd.fileStatus
I[20:59:11.267] <-- reply(0)
I[20:59:11.267] --> $/progress
I[20:59:11.267] --> $/progress
I[20:59:11.281] --> textDocument/publishDiagnostics
I[20:59:11.326] --> $/progress
I[20:59:11.327] --> $/progress
I[20:59:11.327] --> $/progress
E[20:59:11.337] Indexing d:\code\EIDE\template\INIT\startup_stm32f429_439xx.s failed: Couldn't build compiler invocation
I[20:59:11.337] --> $/progress
I[20:59:11.430] Built preamble of size 2134340 for file d:\code\EIDE\template\USR\nuts\gpio.hpp version 1 in 0.16 seconds
I[20:59:11.430] --> workspace/semanticTokens/refresh(1)
I[20:59:11.430] --> textDocument/clangd.fileStatus
I[20:59:11.431] <-- reply(1)
I[20:59:11.456] <-- $/setTrace
I[20:59:11.456] unhandled notification $/setTrace
I[20:59:11.465] --> textDocument/publishDiagnostics
I[20:59:11.465] --> textDocument/clangd.fileStatus
I[20:59:11.468] <-- textDocument/codeAction(1)
I[20:59:11.468] --> reply:textDocument/codeAction(1) 0 ms
I[20:59:11.468] --> textDocument/clangd.fileStatus
I[20:59:11.548] <-- textDocument/documentLink(2)
I[20:59:11.548] --> reply:textDocument/documentLink(2) 0 ms
I[20:59:11.548] --> textDocument/clangd.fileStatus
I[20:59:11.549] <-- textDocument/inlayHint(3)
I[20:59:11.549] --> reply:textDocument/inlayHint(3) 0 ms
I[20:59:11.549] --> textDocument/clangd.fileStatus
I[20:59:11.550] <-- textDocument/semanticTokens/full(4)
I[20:59:11.551] --> reply:textDocument/semanticTokens/full(4) 0 ms
I[20:59:11.551] --> textDocument/clangd.fileStatus
I[20:59:11.631] <-- $/setTrace
I[20:59:11.631] unhandled notification $/setTrace
I[20:59:12.908] <-- textDocument/foldingRange(5)
I[20:59:12.909] --> reply:textDocument/foldingRange(5) 0 ms
I[20:59:12.927] <-- $/setTrace
I[20:59:12.927] unhandled notification $/setTrace
I[20:59:12.937] <-- textDocument/codeAction(6)
I[20:59:12.937] --> reply:textDocument/codeAction(6) 0 ms
I[20:59:12.937] <-- $/cancelRequest
I[20:59:12.937] --> textDocument/clangd.fileStatus
I[20:59:12.938] <-- textDocument/codeAction(7)
I[20:59:12.938] --> reply:textDocument/codeAction(7) 0 ms
I[20:59:12.938] --> textDocument/clangd.fileStatus
I[20:59:12.938] <-- $/setTrace
I[20:59:12.938] unhandled notification $/setTrace
I[20:59:12.938] <-- $/setTrace
I[20:59:12.938] unhandled notification $/setTrace
I[20:59:12.939] <-- textDocument/inlayHint(8)
I[20:59:12.939] --> reply:textDocument/inlayHint(8) 0 ms
I[20:59:12.939] --> textDocument/clangd.fileStatus
I[20:59:13.238] <-- textDocument/documentLink(9)
I[20:59:13.238] --> reply:textDocument/documentLink(9) 0 ms
I[20:59:13.238] --> textDocument/clangd.fileStatus
I[20:59:13.492] <-- textDocument/codeAction(10)
I[20:59:13.492] --> reply:textDocument/codeAction(10) 0 ms
I[20:59:13.492] --> textDocument/clangd.fileStatus
I[20:59:13.493] <-- textDocument/documentSymbol(11)
I[20:59:13.494] --> reply:textDocument/documentSymbol(11) 0 ms
I[20:59:13.495] --> textDocument/clangd.fileStatus
I[20:59:14.677] <-- textDocument/inlayHint(12)
I[20:59:14.678] --> reply:textDocument/inlayHint(12) 0 ms
I[20:59:14.678] --> textDocument/clangd.fileStatus
I[20:59:15.084] <-- textDocument/inlayHint(13)
I[20:59:15.085] --> reply:textDocument/inlayHint(13) 0 ms
I[20:59:15.085] --> textDocument/clangd.fileStatus
I[20:59:15.585] <-- textDocument/definition(14)
I[20:59:15.585] --> reply:textDocument/definition(14) 0 ms
I[20:59:15.585] --> textDocument/clangd.fileStatus
I[20:59:15.595] <-- textDocument/didOpen
I[20:59:15.596] ASTWorker building file d:\code\EIDE\template\Driver\inc\stm32f4xx_gpio.h version 1 with command inferred from d:\code\EIDE\template\Driver\src\stm32f4xx_gpio.c
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -Ic:/Keil_v5/ARM/ARMCLANG/include -I.cmsis/include -IRTE/_Target -I.eide/deps -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -Oz -flto -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -Wno-empty-body -Wno-deprecated-volatile -MMD -x c-header -std=c11 "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -- "d:\\code\\EIDE\\template\\Driver\\inc\\stm32f4xx_gpio.h"
I[20:59:15.597] <-- textDocument/didClose
I[20:59:15.597] --> textDocument/publishDiagnostics
I[20:59:15.786] <-- textDocument/documentHighlight(15)
I[20:59:15.786] --> reply:textDocument/documentHighlight(15) 0 ms
I[20:59:15.786] --> textDocument/clangd.fileStatus
I[20:59:15.859] <-- textDocument/definition(16)
I[20:59:15.860] --> reply:textDocument/definition(16) 0 ms
I[20:59:15.860] --> textDocument/clangd.fileStatus
I[20:59:15.897] <-- textDocument/didOpen
I[20:59:15.897] ASTWorker building file d:\code\EIDE\template\Driver\inc\stm32f4xx_gpio.h version 1 with command inferred from d:\code\EIDE\template\Driver\src\stm32f4xx_gpio.c
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -Ic:/Keil_v5/ARM/ARMCLANG/include -I.cmsis/include -IRTE/_Target -I.eide/deps -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -Oz -flto -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -Wno-empty-body -Wno-deprecated-volatile -MMD -x c-header -std=c11 "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -- "d:\\code\\EIDE\\template\\Driver\\inc\\stm32f4xx_gpio.h"
I[20:59:15.898] --> textDocument/clangd.fileStatus
I[20:59:15.911] Indexing c17 standard library in the context of d:\code\EIDE\template\Driver\inc\stm32f4xx_gpio.h
I[20:59:15.926] <-- textDocument/codeAction(17)
I[20:59:15.926] <-- textDocument/documentSymbol(18)
I[20:59:15.946] Indexed c17 standard library: 1318 symbols, 3 filtered
I[20:59:15.949] Built preamble of size 344520 for file d:\code\EIDE\template\Driver\inc\stm32f4xx_gpio.h version 1 in 0.05 seconds
I[20:59:15.949] --> workspace/semanticTokens/refresh(2)
I[20:59:15.949] <-- reply(2)
I[20:59:15.988] <-- textDocument/semanticTokens/full(19)
I[20:59:15.998] <-- textDocument/documentLink(20)
I[20:59:16.013] --> textDocument/publishDiagnostics
I[20:59:16.013] --> reply:textDocument/codeAction(17) 87 ms
I[20:59:16.015] --> reply:textDocument/documentSymbol(18) 89 ms
I[20:59:16.016] --> reply:textDocument/semanticTokens/full(19) 27 ms
I[20:59:16.016] --> reply:textDocument/documentLink(20) 18 ms
I[20:59:16.016] --> textDocument/clangd.fileStatus
I[20:59:16.021] <-- textDocument/inlayHint(21)
I[20:59:16.021] --> reply:textDocument/inlayHint(21) 0 ms
I[20:59:16.021] --> textDocument/clangd.fileStatus
I[20:59:16.028] <-- textDocument/semanticTokens/full/delta(22)
I[20:59:16.029] --> reply:textDocument/semanticTokens/full/delta(22) 0 ms
I[20:59:16.029] --> textDocument/clangd.fileStatus
I[20:59:16.180] <-- textDocument/codeAction(23)
I[20:59:16.180] --> reply:textDocument/codeAction(23) 0 ms
I[20:59:16.180] --> textDocument/clangd.fileStatus
I[20:59:16.334] <-- textDocument/semanticTokens/full/delta(24)
I[20:59:16.335] --> reply:textDocument/semanticTokens/full/delta(24) 1 ms
I[20:59:16.335] --> textDocument/clangd.fileStatus
I[20:59:16.528] <-- textDocument/foldingRange(25)
I[20:59:16.530] --> reply:textDocument/foldingRange(25) 2 ms

@HighCommander4
Copy link

HighCommander4 commented Apr 15, 2023

A couple of follow-up questions:

  • Does stm32f4xx_gpio.c have an entry in d:\code\EIDE\template\build/compile_commands.json?
  • If you open stm32f4xx_gpio.c in the editor, do you get any error diagnostics? (A theory is, maybe when indexing this file, clangd can't parse it well enough to recognize the definitions?)

@Eplankton
Copy link
Author

Eplankton commented Apr 15, 2023

A couple of follow-up questions:

* Does `stm32f4xx_gpio.c` have an entry in `d:\code\EIDE\template\build/compile_commands.json`?

* If you open `stm32f4xx_gpio.c` in the editor, do you get any error diagnostics? (A theory is, maybe when indexing this file, clangd can't parse it well enough to recognize the definitions?)

For question1: yes
For question2: no error at all

I wonder is there any problem with mixed c/c++ programming, because I use both in this project, maybe the clangd failed to recognize .h file as a c header

@Eplankton
Copy link
Author

An interesting phenomenon is that if I try to open every source file once, it can jump to correct position then... But if I reload the vscode, it failed again.

@daiyousei-qz
Copy link

An interesting phenomenon is that if I try to open every source file once, it can jump to correct position then... But if I reload the vscode, it failed again.

This is actually because how clangd finds definition of a symbol. The following is my explanation (I may be partially wrong as I'm not a clangd dev)

  • If you have opened the source file, clangd will parse it and store symbols into a dynamic index in memory. Since dynamic index is prioritized, you can now jump to the source file.
  • If you have not, then clangd will find from static index, which is usually computed at language server initialization (background index). In your case, somehow static index failed to include the symbol you requested (usually because of failed compilation). Therefore, clangd could only direct you to the declaration in the header file, which is technically part of the source code that you have opened.

If my theory were correct, you might see output in clangd extension like below:

I[19:47:49.757] Indexed E:\Project\llvm-project\clang\lib\Parse\ParseDecl.cpp (3007 symbols, 57447 refs, 413 files)
I[19:47:49.757] Failed to compile E:\Project\llvm-project\clang\lib\Parse\ParseDecl.cpp, index may be incomplete

@Eplankton
Copy link
Author

An interesting phenomenon is that if I try to open every source file once, it can jump to correct position then... But if I reload the vscode, it failed again.

This is actually because how clangd finds definition of a symbol. The following is my explanation (I may be partially wrong as I'm not a clangd dev)

* If you have opened the source file, clangd will parse it and store symbols into a dynamic index in memory. Since dynamic index is prioritized, you can now jump to the source file.

* If you have not, then clangd will find from static index, which is usually computed at language server initialization (background index). In your case, somehow static index failed to include the symbol you requested (usually because of failed compilation). Therefore, clangd could only direct you to the declaration in the header file, which is technically part of the source code that you have opened.

If my theory were correct, you might see output in clangd extension like below:

I[19:47:49.757] Indexed E:\Project\llvm-project\clang\lib\Parse\ParseDecl.cpp (3007 symbols, 57447 refs, 413 files)
I[19:47:49.757] Failed to compile E:\Project\llvm-project\clang\lib\Parse\ParseDecl.cpp, index may be incomplete

I did't find any clue for "Failed to compile", beside, I tried to delete the .cache then reload the vscode to observe how the indexing process is going, and here's the log, clearly the indexing process was perfectly completed:

I[13:15:03.246] clangd version 16.0.1
I[13:15:03.246] Features: windows
I[13:15:03.246] PID: 4812
I[13:15:03.246] Working directory: d:\code\EIDE\template
I[13:15:03.246] argv[0]: C:\Program Files\LLVM\bin\clangd.EXE
I[13:15:03.246] argv[1]: --header-insertion=never
I[13:15:03.252] Starting LSP over stdin/stdout
I[13:15:03.252] <-- initialize(0)
I[13:15:03.254] --> reply:initialize(0) 1 ms
I[13:15:04.585] <-- initialized
I[13:15:04.604] <-- textDocument/didOpen
I[13:15:04.607] Loaded compilation database from d:\code\EIDE\template\build/compile_commands.json
I[13:15:04.608] <-- textDocument/codeAction(1)
I[13:15:04.608] ASTWorker building file d:\code\EIDE\template\Driver\inc\stm32f4xx_syscfg.h version 1 with command inferred from d:\code\EIDE\template\Driver\src\stm32f4xx_syscfg.c
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -I.cmsis/include -IRTE/_Target -I.eide/deps -Ic:/Keil_v5/ARM/ARMCLANG/include -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -O0 -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -Wno-deprecated-volatile -Wno-empty-body -MMD -x c-header -std=c11 "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -- "d:\\code\\EIDE\\template\\Driver\\inc\\stm32f4xx_syscfg.h"
I[13:15:04.609] --> window/workDoneProgress/create(0)
I[13:15:04.609] Enqueueing 47 commands for indexing
I[13:15:04.610] --> textDocument/clangd.fileStatus
I[13:15:04.611] <-- reply(0)
I[13:15:04.611] --> $/progress
I[13:15:04.611] --> $/progress
I[13:15:04.619] --> $/progress
I[13:15:04.619] --> $/progress
I[13:15:04.619] --> $/progress
I[13:15:04.619] --> $/progress
I[13:15:04.619] --> $/progress
I[13:15:04.619] --> $/progress
I[13:15:04.619] --> $/progress
I[13:15:04.619] --> $/progress
I[13:15:04.619] --> $/progress
I[13:15:04.620] --> $/progress
I[13:15:04.627] <-- textDocument/documentLink(2)
I[13:15:04.629] <-- textDocument/inlayHint(3)
I[13:15:04.636] Indexing c17 standard library in the context of d:\code\EIDE\template\Driver\inc\stm32f4xx_syscfg.h
I[13:15:04.637] Built preamble of size 344436 for file d:\code\EIDE\template\Driver\inc\stm32f4xx_syscfg.h version 1 in 0.03 seconds
I[13:15:04.638] --> workspace/semanticTokens/refresh(1)
I[13:15:04.638] --> textDocument/clangd.fileStatus
I[13:15:04.697] Indexed c17 standard library: 1318 symbols, 3 filtered
I[13:15:04.723] <-- reply(1)
I[13:15:04.726] <-- textDocument/semanticTokens/full(4)
I[13:15:04.737] --> textDocument/publishDiagnostics
I[13:15:04.737] --> reply:textDocument/codeAction(1) 129 ms
I[13:15:04.737] --> reply:textDocument/documentLink(2) 110 ms
I[13:15:04.737] --> reply:textDocument/inlayHint(3) 108 ms
I[13:15:04.737] --> reply:textDocument/semanticTokens/full(4) 11 ms
I[13:15:04.737] --> textDocument/clangd.fileStatus
I[13:15:04.766] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_crc.c (2127 symbols, 15006 refs, 39 files)
I[13:15:04.766] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_pwr.c (2127 symbols, 15191 refs, 39 files)
I[13:15:04.769] Indexed d:\code\EIDE\template\USR\nuts\io.c (2191 symbols, 15197 refs, 41 files)
I[13:15:04.771] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_fmpi2c.c (2127 symbols, 14957 refs, 40 files)
I[13:15:04.774] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_syscfg.c (2127 symbols, 15028 refs, 39 files)
I[13:15:04.774] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_fmc.c (2128 symbols, 16255 refs, 39 files)
I[13:15:04.779] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_can.c (2128 symbols, 16025 refs, 39 files)
I[13:15:04.781] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_adc.c (2127 symbols, 15483 refs, 39 files)
I[13:15:04.990] <-- textDocument/foldingRange(5)
I[13:15:04.991] --> reply:textDocument/foldingRange(5) 1 ms
I[13:15:05.035] <-- textDocument/documentLink(6)
I[13:15:05.035] --> reply:textDocument/documentLink(6) 0 ms
I[13:15:05.035] --> textDocument/clangd.fileStatus
I[13:15:05.360] <-- textDocument/codeAction(7)
I[13:15:05.360] --> reply:textDocument/codeAction(7) 0 ms
I[13:15:05.360] --> textDocument/clangd.fileStatus
I[13:15:05.373] --> $/progress
I[13:15:05.373] --> $/progress
I[13:15:05.378] --> $/progress
I[13:15:05.378] --> $/progress
I[13:15:05.385] <-- textDocument/documentSymbol(8)
I[13:15:05.385] --> reply:textDocument/documentSymbol(8) 0 ms
I[13:15:05.386] --> textDocument/clangd.fileStatus
I[13:15:05.426] --> $/progress
I[13:15:05.426] --> $/progress
I[13:15:05.431] --> $/progress
I[13:15:05.431] --> $/progress
I[13:15:05.440] --> $/progress
I[13:15:05.440] --> $/progress
I[13:15:05.443] --> $/progress
I[13:15:05.443] --> $/progress
I[13:15:05.446] --> $/progress
I[13:15:05.446] --> $/progress
I[13:15:05.455] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_iwdg.c (6 symbols, 9946 refs, 39 files)
I[13:15:05.461] --> $/progress
I[13:15:05.461] --> $/progress
I[13:15:05.465] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_cryp.c (19 symbols, 10540 refs, 39 files)
I[13:15:05.472] --> $/progress
I[13:15:05.472] --> $/progress
I[13:15:05.476] --> $/progress
I[13:15:05.476] --> $/progress
I[13:15:05.525] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_usart.c (29 symbols, 10416 refs, 39 files)
I[13:15:05.526] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_sdio.c (30 symbols, 10217 refs, 39 files)
I[13:15:05.529] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_hash.c (18 symbols, 10204 refs, 39 files)
I[13:15:05.533] --> $/progress
I[13:15:05.533] --> $/progress
I[13:15:05.536] --> $/progress
I[13:15:05.536] --> $/progress
I[13:15:05.541] --> $/progress
I[13:15:05.541] --> $/progress
I[13:15:05.543] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_qspi.c (0 symbols, 9899 refs, 40 files)
I[13:15:05.544] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_flash.c (40 symbols, 10698 refs, 39 files)
I[13:15:05.550] --> $/progress
I[13:15:05.550] --> $/progress
I[13:15:05.556] --> $/progress
I[13:15:05.556] --> $/progress
I[13:15:05.562] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_wwdg.c (8 symbols, 9979 refs, 39 files)
I[13:15:05.573] --> $/progress
I[13:15:05.573] --> $/progress
I[13:15:05.580] Indexed d:\code\EIDE\template\CORE\system_stm32f4xx.c (5 symbols, 10148 refs, 39 files)
I[13:15:05.587] --> $/progress
I[13:15:05.587] --> $/progress
I[13:15:05.589] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_dma.c (19 symbols, 10421 refs, 39 files)
I[13:15:05.595] --> $/progress
I[13:15:05.595] --> $/progress
I[13:15:05.629] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_dsi.c (0 symbols, 9899 refs, 40 files)
I[13:15:05.641] --> $/progress
I[13:15:05.641] --> $/progress
I[13:15:05.644] Indexed d:\code\EIDE\template\Driver\src\misc.c (5 symbols, 9977 refs, 39 files)
I[13:15:05.651] --> $/progress
I[13:15:05.651] --> $/progress
I[13:15:05.655] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_flash_ramfunc.c (2 symbols, 9942 refs, 40 files)
I[13:15:05.665] --> $/progress
I[13:15:05.665] --> $/progress
I[13:15:05.668] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_cec.c (0 symbols, 9899 refs, 40 files)
I[13:15:05.681] --> $/progress
I[13:15:05.681] --> $/progress
I[13:15:05.685] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_dfsdm.c (0 symbols, 9900 refs, 40 files)
I[13:15:05.698] --> $/progress
I[13:15:05.698] --> $/progress
I[13:15:05.714] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_tim.c (92 symbols, 11520 refs, 39 files)
I[13:15:05.720] --> $/progress
I[13:15:05.720] --> $/progress
I[13:15:05.738] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_rng.c (8 symbols, 9993 refs, 39 files)
I[13:15:05.745] --> $/progress
I[13:15:05.745] --> $/progress
I[13:15:05.756] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_dbgmcu.c (5 symbols, 9954 refs, 39 files)
I[13:15:05.761] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_rtc.c (57 symbols, 11749 refs, 39 files)
I[13:15:05.764] --> $/progress
I[13:15:05.764] --> $/progress
I[13:15:05.770] --> $/progress
I[13:15:05.770] --> $/progress
I[13:15:05.773] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_cryp_tdes.c (2 symbols, 10056 refs, 39 files)
I[13:15:05.781] --> $/progress
I[13:15:05.781] --> $/progress
I[13:15:05.791] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_lptim.c (0 symbols, 9899 refs, 40 files)
I[13:15:05.802] --> $/progress
I[13:15:05.802] --> $/progress
I[13:15:05.818] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_dac.c (17 symbols, 10159 refs, 39 files)
I[13:15:05.825] --> $/progress
I[13:15:05.825] --> $/progress
I[13:15:05.842] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_exti.c (8 symbols, 10038 refs, 39 files)
I[13:15:05.849] --> $/progress
I[13:15:05.850] --> $/progress
E[13:15:05.867] Indexing d:\code\EIDE\template\INIT\startup_stm32f429_439xx.s failed: Couldn't build compiler invocation
I[13:15:05.867] --> $/progress
I[13:15:05.867] --> $/progress
I[13:15:05.868] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_ltdc.c (30 symbols, 10554 refs, 39 files)
I[13:15:05.876] --> $/progress
I[13:15:05.876] --> $/progress
I[13:15:05.881] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_hash_md5.c (2 symbols, 10034 refs, 39 files)
I[13:15:05.888] --> $/progress
I[13:15:05.888] --> $/progress
I[13:15:05.891] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_cryp_aes.c (5 symbols, 10982 refs, 39 files)
I[13:15:05.900] --> $/progress
I[13:15:05.900] --> $/progress
I[13:15:05.909] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_sai.c (25 symbols, 10220 refs, 39 files)
I[13:15:05.916] --> $/progress
I[13:15:05.916] --> $/progress
I[13:15:05.917] Indexed d:\code\EIDE\template\USR\stm32f4xx_it.cpp (4701 symbols, 22210 refs, 104 files)
I[13:15:05.923] Indexed d:\code\EIDE\template\USR\main.cpp (2545 symbols, 19304 refs, 103 files)
I[13:15:05.926] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_spi.c (25 symbols, 10404 refs, 39 files)
I[13:15:05.934] --> $/progress
I[13:15:05.934] --> $/progress
I[13:15:05.964] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_dma2d.c (19 symbols, 10377 refs, 39 files)
I[13:15:05.970] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_dcmi.c (15 symbols, 10182 refs, 39 files)
I[13:15:05.974] --> $/progress
I[13:15:05.974] --> $/progress
I[13:15:05.977] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_spdifrx.c (0 symbols, 9899 refs, 40 files)
I[13:15:05.981] --> $/progress
I[13:15:05.981] --> $/progress
I[13:15:05.989] --> $/progress
I[13:15:05.992] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_cryp_des.c (2 symbols, 10040 refs, 39 files)
I[13:15:05.999] --> $/progress
I[13:15:06.009] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_hash_sha1.c (2 symbols, 10038 refs, 39 files)
I[13:15:06.015] --> $/progress
I[13:15:06.024] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_i2c.c (35 symbols, 10432 refs, 39 files)
I[13:15:06.030] --> $/progress
I[13:15:06.058] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_gpio.c (14 symbols, 10187 refs, 39 files)
I[13:15:06.065] --> $/progress
I[13:15:06.071] Indexed d:\code\EIDE\template\Driver\src\stm32f4xx_rcc.c (54 symbols, 10690 refs, 39 files)
I[13:15:06.079] --> $/progress
I[13:15:06.168] --> $/progress
I[13:15:06.207] --> $/progress

@Eplankton
Copy link
Author

By the way, I'd like to provide a typical entry in compile_commands.json, does any error go here? :

    {
        "directory": "d:\\code\\EIDE\\template\\Project",
        "file": "d:\\code\\EIDE\\template\\Driver\\src\\stm32f4xx_gpio.c",
        "command": "\"c:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe\" --target=arm-arm-none-eabi -c -xc -std=c11 
-I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -I.cmsis/include -IRTE/_Target -I.eide/deps -Ic:/Keil_v5/ARM/ARMCLANG/include -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -O0 -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -Wno-deprecated-volatile -Wno-empty-body -o ./build/Target/__/Driver/src/stm32f4xx_gpio.o -MMD ./../Driver/src/stm32f4xx_gpio.c"
    },

@Eplankton
Copy link
Author

I have looked into another issue
#1348 (comment)

which the problem is caused by mismatch version of clang and clangd, I use armclang for compile and clangd 16.0.1 for database, I wonder whether the critical point is here.

@HighCommander4
Copy link

In theory, a version match between the build compiler and clangd shouldn't be required. In #1348, the bug reporter said they solved the problem by changing clangd version, but we never got to the bottom of why things didn't work with the other version.

That said, I don't really have any theories based on the posted information. If you're able to replicate the issue in a project small enough that you can zip up the whole project and share it, I'm happy to try and replicate it locally and investigate further.

@Eplankton
Copy link
Author

In theory, a version match between the build compiler and clangd shouldn't be required. In #1348, the bug reporter said they solved the problem by changing clangd version, but we never got to the bottom of why things didn't work with the other version.

That said, I don't really have any theories based on the posted information. If you're able to replicate the issue in a project small enough that you can zip up the whole project and share it, I'm happy to try and replicate it locally and investigate further.

Thanks, I'll find a way to shrink down my project later.
And I'd like to ask a simple question: how does the compile_commands.json link the .c and .h files?
Example, does this entry properly works? :

    {
        "directory": "d:\\code\\EIDE\\template\\Project",
        "file": "d:\\code\\EIDE\\template\\Driver\\src\\stm32f4xx_gpio.c",
        "command": "\"c:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe\" --target=arm-arm-none-eabi -c -xc -std=c11 
-I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -I.cmsis/include -IRTE/_Target -I.eide/deps -Ic:/Keil_v5/ARM/ARMCLANG/include -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -O0 -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -Wno-deprecated-volatile -Wno-empty-body -o ./build/Target/__/Driver/src/stm32f4xx_gpio.o -MMD ./../Driver/src/stm32f4xx_gpio.c"
    },

@HighCommander4
Copy link

The entry looks fine to me.

For an .h file, clangd chooses a .c file from the log (preferring one with the same name, if it can find one), and uses its compile command. For example, an earlier log included:

I[20:59:15.596] ASTWorker building file d:\code\EIDE\template\Driver\inc\stm32f4xx_gpio.h version 1 with command inferred from d:\code\EIDE\template\Driver\src\stm32f4xx_gpio.c
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -Ic:/Keil_v5/ARM/ARMCLANG/include -I.cmsis/include -IRTE/_Target -I.eide/deps -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -Oz -flto -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -Wno-empty-body -Wno-deprecated-volatile -MMD -x c-header -std=c11 "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -- "d:\\code\\EIDE\\template\\Driver\\inc\\stm32f4xx_gpio.h"

Notice the with command inferred from d:\code\EIDE\template\Driver\src\stm32f4xx_gpio.c part, meaning clangd chose stm32f4xx_gpio.c as the file whose command to use to parse stm32f4xx_gpio.h.

@Eplankton
Copy link
Author

Eplankton commented Apr 17, 2023

As you asked for, this is a very small project which suffers exactly the same problem, forgive me because the project structure is a little bit different from what I have described upon.
new.zip

@Eplankton
Copy link
Author

The entry looks fine to me.

For an .h file, clangd chooses a .c file from the log (preferring one with the same name, if it can find one), and uses its compile command. For example, an earlier log included:

I[20:59:15.596] ASTWorker building file d:\code\EIDE\template\Driver\inc\stm32f4xx_gpio.h version 1 with command inferred from d:\code\EIDE\template\Driver\src\stm32f4xx_gpio.c
[d:\code\EIDE\template\Project]
"C:\\Keil_v5\\ARM\\ARMCLANG\\bin\\armclang.exe" --target=arm-arm-none-eabi -c -Ic:/Keil_v5/ARM/ARMCLANG/include -I.cmsis/include -IRTE/_Target -I.eide/deps -I../Driver/inc -I../CMSIS/Include -I../CORE -I../USR -DUSE_STDPERIPH_DRIVER -DSTM32F429_439xx -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4 -mlittle-endian -D__MICROLIB -Oz -flto -ffunction-sections -funsigned-char -fshort-enums -fshort-wchar -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 -Wno-empty-body -Wno-deprecated-volatile -MMD -x c-header -std=c11 "-resource-dir=C:\\Program Files\\LLVM\\lib\\clang\\16" -- "d:\\code\\EIDE\\template\\Driver\\inc\\stm32f4xx_gpio.h"

Notice the with command inferred from d:\code\EIDE\template\Driver\src\stm32f4xx_gpio.c part, meaning clangd chose stm32f4xx_gpio.c as the file whose command to use to parse stm32f4xx_gpio.h.

If you find any confusion about the project struct, please inform me...

@HighCommander4
Copy link

When I run make, I get:

$ make
make: *** No rule to make target 'build/main.o', needed by 'build/temp.elf'.  Stop.

I'm a bit rusty on my make knowledge, do you know why this might be? I do see the $(BUILD_DIR)/%.o rule in the makefile which sounds like it should fit in this case.

@Eplankton
Copy link
Author

When I run make, I get:

$ make
make: *** No rule to make target 'build/main.o', needed by 'build/temp.elf'.  Stop.

I'm a bit rusty on my make knowledge, do you know why this might be? I do see the $(BUILD_DIR)/%.o rule in the makefile which sounds like it should fit in this case.

I believe this is because I use the armclang compiler, it's cross-compiler, you may have missed it.

@Eplankton
Copy link
Author

When I run make, I get:

$ make
make: *** No rule to make target 'build/main.o', needed by 'build/temp.elf'.  Stop.

I'm a bit rusty on my make knowledge, do you know why this might be? I do see the $(BUILD_DIR)/%.o rule in the makefile which sounds like it should fit in this case.

Any way, I have built the project before I upload it, it should contain the build/ folder already.

@HighCommander4
Copy link

I don't think I can use your compile_commands.json, I need to re-generate it so it contains paths appropriate to my system.

@HighCommander4
Copy link

I believe this is because I use the armclang compiler, it's cross-compiler, you may have missed it.

Do you mean arm-none-eabi-gcc (since this is the compiler that appears in the commands in compile_commands.json)? I have installed it, but the make error persists.

@Eplankton
Copy link
Author

Eplankton commented Apr 18, 2023

I believe this is because I use the armclang compiler, it's cross-compiler, you may have missed it.

Do you mean arm-none-eabi-gcc (since this is the compiler that appears in the commands in compile_commands.json)? I have installed it, but the make error persists.

Yes, I do mean arm-none-eabi-gcc.

@Eplankton
Copy link
Author

Eplankton commented Apr 18, 2023

I believe this is because I use the armclang compiler, it's cross-compiler, you may have missed it.

Do you mean arm-none-eabi-gcc (since this is the compiler that appears in the commands in compile_commands.json)? I have installed it, but the make error persists.

If you don't mind, you can install this extension and setup following here, then double click the new.workspace to enter the project.

@HighCommander4
Copy link

I believe this is because I use the armclang compiler, it's cross-compiler, you may have missed it.

Do you mean arm-none-eabi-gcc (since this is the compiler that appears in the commands in compile_commands.json)? I have installed it, but the make error persists.

If you don't mind, you can install this extension and setup following here, then double click the new.workspace to enter the project.

I've done that, but I don't see how it helps with the previous issue. I still need to re-generate the compile_commands.json file.

@HighCommander4
Copy link

The Makefile doesn't seem right for the project. For example, main.h is found at Inc/main.h, but Inc is never added to C_INCLUDES.

@HighCommander4
Copy link

I took a different approach: I took your build/compile_commands.json, and used search-and-replace to replace the paths in that file with paths appropriate for my system.

Now clangd seems to be able to parse the project files successfully, and I can reproduce the bug!

To give a concrete example:

  • Open src/main.cpp
  • On line 58, perform go-to-def on the function name HAL_RCC_ClockConfig
  • The opened file is Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h
  • Open Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hall_rcc.c
  • Go back to src/main.cpp and perform the same go-to-def again
  • The opened file now is Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hall_rcc.c

And stm32f4xx_hall_rcc.c has an entry in the compile_commands.json, suggesting that it should be part of the project's index.

I will investigate further when I get a chance.

@zyn0217
Copy link

zyn0217 commented Apr 19, 2023

The opened file now is Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hall_rcc.c

Sorry but I still don't understand. Did the author report that it's opening a header instead of a source? If clangd opens stm32f4xx_hall_rcc.c on go-to-def, does it mean it works as expected?

From #1592 (comment):

however the Go to Definition still doesn't work, it jumps into the header file as usual.


...unless they open that file for at least one time..

Sorry I missed it. So, it is reported that we're jumping into headers all the time unless we manually open the counterpart source at least once.

@Eplankton
Copy link
Author

Great!

@Eplankton
Copy link
Author

I DO mean something like that.

@HighCommander4
Copy link

My <project>/.cache/clangd/index directory is empty, suggesting that indexing is failing for some reason.

@HighCommander4
Copy link

HighCommander4 commented Apr 20, 2023

My log shows errors like this:

Indexing /path/to/project/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c failed: No such file or directory

but the file does exist. Very strange.

@HighCommander4
Copy link

HighCommander4 commented Apr 20, 2023

It turns out my problem was that due to the the search-and-replace I had performed in the compile_commands.json file, my paths had a mix of forward and backslashes, e.g. the actual value in "file" was like

/path/to/project\\Drivers\\STM32F4xx_HAL_Driver\\Src\\stm32f4xx_hal_rcc.c

and it seems the indexer does not like mixed-slash paths like this.

(But, interestingly, in the error message the path is normalized to be all forward slashes! You'd think, that if the machinery for printing the error message can normalize the path like this, then the code for actually processing the file in the indexer could too...)

Anyways, after correcting the path to consistently use forward slashes, the bug is fixed: now, the first go-to-def after starting vscode goes directly to stm32f4xx_hal_rcc.c.

@Eplankton I am curious if this is the same issue you're experiencing: is your project's .cache/clangd/index subdirectory also empty (or at least missing entries for .c files such as a file named stm32f4xx_hal_rcc.c.<HASH>.idx)?

@Eplankton
Copy link
Author

Eplankton commented Apr 20, 2023

Yet, I have checked and ... as you can see:
捕获

I also tried to remove all the \\ in the path and change to / but it just doesn't work...

@Eplankton
Copy link
Author

Eplankton commented Apr 20, 2023

It turns out my problem was that due to the the search-and-replace I had performed in the compile_commands.json file, my paths had a mix of forward and backslashes, e.g. the actual value in "file" was like

/path/to/project\\Drivers\\STM32F4xx_HAL_Driver\\Src\\stm32f4xx_hal_rcc.c

and it seems the indexer does not like mixed-slash paths like this.

(But, interestingly, in the error message the path is normalized to be all forward slashes! You'd think, that if the machinery for printing the error message can normalize the path like this, then the code for actually processing the file in the indexer could too...)

Anyways, after correcting the path to consistently use forward slashes, the bug is fixed: now, the first go-to-def after starting vscode goes directly to stm32f4xx_hal_rcc.c.

@Eplankton I am curious if this is the same issue you're experiencing: is your project's .cache/clangd/index subdirectory also empty (or at least missing entries for .c files such as a file named stm32f4xx_hal_rcc.c.<HASH>.idx)?

I quote some part of the log that might be useful to analysis,does it mean that the index process is correct?:

I[15:30:54.691] ASTWorker building file d:\code\EIDE\template\Driver\inc\stm32f4xx_gpio.h version 1 with command inferred from d:/code/EIDE/template/Driver/src/stm32f4xx_gpio.c
[d:/code/EIDE/template/Project]

@HighCommander4
Copy link

Ok, since you do have index files, I guess you're running into a different issue than I was.

Unfortunately, I can't reproduce your issue (it works for me after fixing my mixed-slash issue).

Perhaps your issue is specific to Windows? Maybe someone else who is a Windows user can try to reproduce and investigate further.

@Eplankton
Copy link
Author

Ok, since you do have index files, I guess you're running into a different issue than I was.

Unfortunately, I can't reproduce your issue (it works for me after fixing my mixed-slash issue).

Perhaps your issue is specific to Windows? Maybe someone else who is a Windows user can try to reproduce and investigate further.

OK, I'll continue to work on it.

@Eplankton
Copy link
Author

Ok, since you do have index files, I guess you're running into a different issue than I was.

Unfortunately, I can't reproduce your issue (it works for me after fixing my mixed-slash issue).

Perhaps your issue is specific to Windows? Maybe someone else who is a Windows user can try to reproduce and investigate further.

Well, how about #1138 (comment) ?

@Eplankton
Copy link
Author

Eplankton commented Apr 20, 2023

Ok, since you do have index files, I guess you're running into a different issue than I was.

Unfortunately, I can't reproduce your issue (it works for me after fixing my mixed-slash issue).

Perhaps your issue is specific to Windows? Maybe someone else who is a Windows user can try to reproduce and investigate further.

Now I discover a weird way to get the correct jump and requires only steps, i have already tested it with no change to my complie_commands.json (that one with \\ in it)

  1. I always put my class/struct in a .hpp file like gpio.hpp, that's where we will start
  2. open the project with no changes to anything at all
  3. open gpio.hpp and try to go to the definition of GPIO_SetBits() in stm32f4xx_gpio.h/.c
    NOTE: I didn't give the stm32f4xx_gpio.h in gpio.hpp, and all the symbols there are imported by compile_commands.json
  4. open .cache/clangd/index, and delete both stm32f4xx_gpio.h/.c.<HASH>.index
  5. reload the window and we can go to the definition correctly.
  6. stm32f4xx_gpio.h/.c.<HASH>.index will be generated again, but this time they behave good.

@Eplankton
Copy link
Author

Ok, since you do have index files, I guess you're running into a different issue than I was.

Unfortunately, I can't reproduce your issue (it works for me after fixing my mixed-slash issue).

Perhaps your issue is specific to Windows? Maybe someone else who is a Windows user can try to reproduce and investigate further.

Also, I wonder whether #1317 (comment) is a problem.

@zyn0217
Copy link

zyn0217 commented Apr 20, 2023

I don't think #1317 relates to your issue as it was closed early in 2022/11 and the clangd you're using is released a few weeks ago.

Neither does #1138 because a workaround for that is to add -x c to every compile command (suggested by @HighCommander4 ) and apparently you have such argument in your compile_commands.json.

... --target=arm-arm-none-eabi -c -xc -std=c11 ...

But you could take a try, just add it to your .clangd file -- though I don't think it would make any difference.

@Eplankton
Copy link
Author

@HighCommander4

I don't think #1317 relates to your issue as it was closed early in 2022/11 and the clangd you're using is released a few weeks ago.

Neither does #1138 because a workaround for that is to add -x c to every compile command (suggested by @HighCommander4 ) and apparently you have such argument in your compile_commands.json.

... --target=arm-arm-none-eabi -c -xc -std=c11 ...

But you could take a try, just add it to your .clangd file -- though I don't think it would make any difference.

After days and days of work and check and waste of my life, I have to say that I'm so exhausted to find out the real problem is case-sensitive on Windows platform, it was mentioned here #108 (comment), so there's a very simple way to solve : JUST CHANGE EVERY c:/d: in compile_commands.json TO C:/D: and let's close down this damn stupid issue.

@Eplankton
Copy link
Author

@HighCommander4

I don't think #1317 relates to your issue as it was closed early in 2022/11 and the clangd you're using is released a few weeks ago.
Neither does #1138 because a workaround for that is to add -x c to every compile command (suggested by @HighCommander4 ) and apparently you have such argument in your compile_commands.json.

... --target=arm-arm-none-eabi -c -xc -std=c11 ...

But you could take a try, just add it to your .clangd file -- though I don't think it would make any difference.

After days and days of work and check and waste of my life, I have to say that I'm so exhausted to find out the real problem is case-sensitive on Windows platform, it was mentioned here #108 (comment), so there's a very simple way to solve : JUST CHANGE EVERY c:/d: in compile_commands.json TO C:/D: and let's close down this damn stupid issue.

Many people has also complain about it: #102 (comment)

@zyn0217
Copy link

zyn0217 commented Apr 20, 2023

Happy to hear that you've worked it out. BTW, did you generate your compile_commands.json from a build tool like CMake, Bear or anything else? If so, perhaps they might have some workarounds for Windows =)

@Eplankton
Copy link
Author

Happy to hear that you've worked it out. BTW, did you generate your compile_commands.json from a build tool like CMake, Bear or anything else? If so, perhaps they might have some workarounds for Windows =)

Yes, I'm now working on it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants