iarsystems / cmake-tutorial

Build and test embedded software using the IAR C/C++ Compiler alongside CMake
https://iar.com
MIT License
78 stars 15 forks source link

iccarm compatibility with clangd #39

Closed s1syph0s closed 4 weeks ago

s1syph0s commented 3 months ago

Hi, I'm wondering if the iccarm compiler is compatible to be read from clangd LSP? When using cmake, we can create the compile_commands.json and use this for the clangd. I am trying to solve this problem by whitelisting the iccarm compiler to query using --query-driver= for clangd, but I still can't get my LSP to correctly work..

.clangd file:

CompileFlags:
  Remove: [--silent]
  Compiler: "C:\\PROGRA~1\\IARSYS~1\\EMBEDD~1.2\\arm\\bin\\iccarm.exe"

clangd is executed using this command:

clangd --query-driver=**

Screenshot 2024-08-13 150053

stdio should not be pointing to visual studio, instead it should point to the header files from iccarm.

clangd log:

[START][2024-08-13 15:02:16] LSP logging initiated
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.838] clangd version 18.1.3 (https://github.com/llvm/llvm-project c13b7485b87909fcf739f62cfa382b55407433c0)\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.839] Features: windows+grpc\r\nI[15:02:16.839] PID: 5408\r\nI[15:02:16.839] Working directory: C:\\Users\\some-user\\src\\co\\iar-cmake\\examples\\libs\r\nI[15:02:16.839] argv[0]: C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\packages\\clangd\\clangd_18.1.3\\bin\\clangd.exe\r\nI[15:02:16.839] argv[1]: --query-driver=**\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.845] Starting LSP over stdin/stdout\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.845] <-- initialize(1)\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.847] --> reply:initialize(1) 2 ms\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.848] <-- initialized\r\nI[15:02:16.848] <-- textDocument/didOpen\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    'I[15:02:16.972] System includes extractor: successfully executed C:\\Users\\some-user\\scoop\\apps\\mingw-winlibs-llvm\\current\\bin\\clang.exe\r\n\tgot includes: "C:/Users/some-user/scoop/apps/mingw-winlibs-llvm/current/x86_64-w64-mingw32/include, C:/Users/some-user/scoop/apps/mingw-winlibs-llvm/current/include"\r\n\tgot target: "x86_64-w64-windows-gnu"\r\n'
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.972] <-- textDocument/semanticTokens/full(2)\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.977] --> textDocument/publishDiagnostics\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.977] Loaded compilation database from C:\\Users\\some-user\\src\\co\\iar-cmake\\examples\\libs\\compile_commands.json\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.979] --> window/workDoneProgress/create(0)\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.979] Enqueueing 2 commands for indexing\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.979] <-- reply(0)\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.979] --> $/progress\r\nI[15:02:16.979] --> $/progress\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.986] --> $/progress\r\n"
[ERROR][2024-08-13 15:02:16] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:16.986] --> $/progress\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "E[15:02:17.011] System include extraction: driver execution failed with return code: 2 - ''. Args: [\"C:\\\\PROGRA~1\\\\IARSYS~1\\\\EMBEDD~1.2\\\\arm\\\\bin\\\\iccarm.exe\" -E -v -x c -]\r\nI[15:02:17.012] ASTWorker building file C:\\Users\\some-user\\src\\co\\iar-cmake\\examples\\libs\\main.c version 0 with command \r\n[C:/Users/some-user/src/co/iar-cmake/examples/libs/build]\r\n\"C:\\\\Program Files\\\\IAR Systems\\\\Embedded Workbench 9.2\\\\arm\\\\bin\\\\iccarm.exe\" \"-IC:\\\\Users\\\\some-user\\\\src\\\\co\\\\iar-cmake\\\\examples\\\\libs\\\\lib\\\\inc\" -e -o \"-resource-dir=C:\\\\Users\\\\some-user\\\\AppData\\\\Local\\\\nvim-data\\\\mason\\\\packages\\\\clangd\\\\clangd_18.1.3\\\\lib\\\\clang\\\\18\" -- \"C:\\\\Users\\\\some-user\\\\src\\\\co\\\\iar-cmake\\\\examples\\\\libs\\\\main.c\"\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:17.050] Built preamble of size 519816 for file C:\\Users\\some-user\\src\\co\\iar-cmake\\examples\\libs\\main.c version 0 in 0.03 seconds\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:17.050] --> workspace/semanticTokens/refresh(1)\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:17.050] Indexing c17 standard library in the context of C:\\Users\\some-user\\src\\co\\iar-cmake\\examples\\libs\\main.c\r\nI[15:02:17.051] <-- $/cancelRequest\r\nI[15:02:17.051] <-- textDocument/semanticTokens/full(3)\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:17.051] <-- reply(1)\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:17.080] --> textDocument/publishDiagnostics\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:17.080] --> reply:textDocument/semanticTokens/full(2) 107 ms, error: Task was cancelled.\r\nI[15:02:17.080] --> reply:textDocument/semanticTokens/full(3) 29 ms\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:17.100] <-- textDocument/documentHighlight(4)\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:17.100] --> reply:textDocument/documentHighlight(4) 0 ms\r\n"
[ERROR][2024-08-13 15:02:17] .../vim/lsp/rpc.lua:770    "rpc"   "C:\\Users\\some-user\\AppData\\Local\\nvim-data\\mason\\bin\\clangd.cmd"   "stderr"    "I[15:02:17.130] Indexed c17 standard library (incomplete due to errors): 2173 symbols, 873 filtered\r\n"
HampusAdolfsson commented 2 months ago

Hi,

Clangd isn't able to query iccarm for include paths, since it doesn't use the same command-line interface as clang. In your logs, we can see clangd trying and failing to invoke iccarm with clang-style arguments:

"E[15:02:17.011] System include extraction: driver execution failed with return code: 2 - ''. Args: [\"C:\\\\PROGRA~1\\\\IARSYS~1\\\\EMBEDD~1.2\\\\arm\\\\bin\\\\iccarm.exe\" -E -v -x c -]

I believe the best way to achieve what you want is to remove the --query-driver argument from clangd, and manually specify the system include paths in your .clangd file, for example:

CompileFlags:
  Remove: [--silent]
  Add:
    - "-isystem"
    - "C:/path/to/ewarm/arm/inc/"
    - "-isystem"
    - "C:/path/to/ewarm/arm/inc/c"
    - "-isystem"
    - "C:/path/to/ewarm/arm/inc/c/aarch32"

Please let me know whether that works for you! Future versions of Embedded Workbench may include a command-line utility for generating clangd-compatible compile_commands.json files, which would spare you the trouble of doing this manually.

s1syph0s commented 2 months ago

Thank you for the help! iccarm has an extension over the C language which clang might not understand and gives an error (The arm include oftentimes use this and i receive many errors). How would you overcome this problem?

Screenshot 2024-08-23 133128

HampusAdolfsson commented 2 months ago

It depends on the specific extension. For most keywords, like __root, you can simply add a preprocessor define to "remove" them in your .clangd file:

CompileFlags:
    Add:
      - "-D__root="

Intrinsic functions can be treated similarly:

CompileFlags:
    Add:
      - "-D__section_begin(...)=((void*)0)"

Other extensions are trickier to handle, most notably the @ operator which is used in lots of device- and vendor-specific headers. Technically, you can fix these by rewriting them using #pragma location instead, but it would be a lot of work and we can't recommend modifying system headers.

As I mentioned in my last post, we've been looking at improving clangd compatibility, and handling IAR compiler extensions is one of the things we've addressed. When this is released, Embedded Workbench will come with a custom version of clangd which is able to handle most IAR compiler extensions.

hbacelar8 commented 2 months ago

I wonder how VSCode handles it so properly. I too work with the same setup as OP and after compiling with CMake, I have 0 errors on VSCode. Doesn't it also use clangd as it's C++ LSP, or it uses something else?

HampusAdolfsson commented 2 months ago

VSCode's C/C++ extension uses Microsoft's proprietary IntelliSense engine. It doesn't support IAR language extensions either (there is a feature request for it here: https://github.com/microsoft/vscode-cpptools/issues/7653). Their engine is a bit more fault-tolerant in this case, and IIRC it sometimes hides parsing errors in system headers. However, if you try to use a symbol that is defined with the @ operator, you'll usually see an error in VS Code too.

hbacelar8 commented 2 months ago

Thanks for the explanation, and indeed some files that uses the @ operator have an error, I missed that. Any estimation of when we could have a iccarm version full compatible with clangd?

HampusAdolfsson commented 2 months ago

Unfortunately no, that is out of my hands. If you want an estimate, I'd suggest you contact IAR support to ask about it.

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 30 days with no activity. If there is no further activity, the ticket will be automatically closed in 14 days.

github-actions[bot] commented 4 weeks ago

This issue was closed because it has been inactive for 14 days since being marked as stale.