coc-clangd icon indicating copy to clipboard operation
coc-clangd copied to clipboard

Include errors

Open ItzZisker opened this issue 2 years ago • 9 comments

Recently i've been migrating to work with cli text-editors for fun, but i got problems into using it. the problem is including libraries from linux modules api.

Result from CocInfo

versions:

node version: v18.7.0
coc.nvim version: 0.0.82-5fce9a51 2022-08-17 07:04:08 +0800
coc.nvim directory: /home/kalix/.local/share/nvim/site/pack/coc/start/coc.nvim
term: xterm-256color
platform: linux

Log of coc.nvim:

2022-08-18T19:21:12.579 INFO (pid:98204) [services] - registered service "clangd"
2022-08-18T19:21:12.580 INFO (pid:98204) [services] - clangd state change: stopped => starting
2022-08-18T19:21:12.585 INFO (pid:98204) [language-client-index] - Language server "clangd" started with 98216
2022-08-18T19:21:12.616 INFO (pid:98204) [services] - clangd state change: starting => running
2022-08-18T19:21:12.625 INFO (pid:98204) [services] - service clangd started
2022-08-18T19:24:24.350 INFO (pid:98204) [attach] - receive notification: showInfo []

Describe the bug

I added all include paths into both compile_flags.txt & compile_commands.json

compile_flags.txt:

-I/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/include -I/usr/lib/modules/5.18.16-arch1-1/build/arch/x86/include -I/usr/lib/modules/5.18.16-arch1-1/build/arch/x86/include/generated -I/usr/lib/modules/5.18.16-arch1-1/build/include -I/usr/lib/modules/5.18.16-arch1-1/build/arch/x86/include/uapi -I/usr/lib/modules/5.18.16-arch1-1/build/arch/x86/include/generated/uapi -I/usr/lib/modules/5.18.16-arch1-1/build/include/uapi -I/usr/lib/modules/5.18.16-arch1-1/build/include/generated/uapi

compile_commands.json:

[
{
  "directory": "/home/kalix/rickroll-driver",
  "command": "clang -std=c11 -I/home/kalix/rickroll-driver -I/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/include -I/usr/lib/modules/5.18.16-arch1-1/build/arch/x86/include -I/usr/lib/modules/5.18.16-arch1-1/build/arch/x86/include/generated -I/usr/lib/modules/5.18.16-arch1-1/build/include -I/usr/lib/modules/5.18.16-arch1-1/build/arch/x86/include/uapi -I/usr/lib/modules/5.18.16-arch1-1/build/arch/x86/include/generated/uapi -I/usr/lib/modules/5.18.16-arch1-1/build/include/uapi -I/usr/lib/modules/5.18.16-arch1-1/build/include/generated/uapi -I/usr/lib/modules/5.18.16-arch1-1/build/include/linux",
  "file": "/home/kalix/rickroll-driver/rickroll.c"
}
]

i checked all of the directories, and they were okay, i can switch into, i can list the files inside, and it works on vscode already.

Reproduce the bug

.wimrc:

let data_dir = has('nvim') ? stdpath('data') . '/site' : '~/.vim'
if empty(glob(data_dir . '/autoload/plug.vim'))
  silent execute '!curl -fLo '.data_dir.'/autoload/plug.vim --create-dirs  https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

call plug#begin('~/.vim/plugged')

Plug 'neoclide/coc.nvim', {'branch': 'release'}

call plug#end()

Screenshots:

image inside init.h: image compiler.h: image compiler_types.h (no errors): image

and whenever i try to include them inside the header itself (readonly) (just by testing to see if the completion works or not): image it just doesn't find it, while the init.h and compiler.h and compiler_types.h, together are exactly in the same directory.

last words: the code compiles, and works with no problems (CMake), it also can be edited by vscode with no errors, the code style is in C11 standard.

ItzZisker avatar Aug 18 '22 15:08 ItzZisker

Where is your compile_commands.json located? It should be in your project's root, or use clangd.compilationDatabasePath to specifies the directory containing the compilation database.

fannheyward avatar Aug 24 '22 10:08 fannheyward

Where is your compile_commands.json located? It should be in your project's root, or use clangd.compilationDatabasePath to specifies the directory containing the compilation database.

it is in root path, yes i did, it has problem reading these two header files init.h and module.h, other ones are okay, the completion also works (for example netfilter header enums, etc), but it spits errors whenever i include init.h & module.h, if i delete compile_commands.json, they all would be red.

ItzZisker avatar Aug 25 '22 03:08 ItzZisker

these two header files init.h and module.h, other ones are okay

it's clangd's issue, cc @sam-mccall

fannheyward avatar Aug 25 '22 03:08 fannheyward

it's clangd's issue, cc

wait i think i found the issue, it says "C99", while i need C11 standard, i tried adding it to compile_commands.json as i mentioned, but it doesn't work, it still thinks its C99, any solution? image

ItzZisker avatar Aug 28 '22 08:08 ItzZisker

Add it to clangd.arguments and try again.

fannheyward avatar Aug 28 '22 08:08 fannheyward

Add it to clangd.arguments and try again.

image image i tried this, but it doesn't even start at all

ItzZisker avatar Aug 28 '22 08:08 ItzZisker

Try clangd.fallbackFlags, https://github.com/clangd/coc-clangd/issues/20#issuecomment-670946925

fannheyward avatar Aug 29 '22 06:08 fannheyward

Try clangd.fallbackFlags, #20 (comment)

i tried adding "clangd.fallbackFlags": [ "-std=c11" ] to coc-settings.json but still got back to the first place image i wonder does this even support changing c standard to c11 at all or just idk?

ItzZisker avatar Sep 03 '22 16:09 ItzZisker

Looks like newer versions of clangd drop supporting -std option

illia-danko avatar Oct 07 '22 22:10 illia-danko

You need to set -std in compile_commands.json, https://github.com/clangd/coc-clangd/issues/516

fannheyward avatar Oct 20 '22 10:10 fannheyward

To clarify clangd's behavior here:

-std is a clang flag (not a clangd flag) and can be set in any of the usual places, {compile_commands.json, compiler_flags.txt} (only ONE of these will be used), .clangd (can add flags), fallback flags (ONLY if there's no compile_commands or compile_flags)

https://clangd.llvm.org/design/compile-commands

If you're editing a header file, it's likely compile_commands.json didn't have flags for it. (Most build systems don't compile headers separately). Clangd will try to guess based on a similarly-named file that does have flags. Details are in the logs.

If you want to see which C version is being used, type __STDC_VERSION__ and hover over it. Checking to see whether there are any diagnostics is not a reliable method, as you could have several problems.

sam-mccall avatar Oct 20 '22 11:10 sam-mccall