4

I have some codes about Linux kernel driver. I use bear make to generate compile_commands.json.

this is some of my compile_commands.json:

"directory": "/usr/src/linux-headers-5.4.0-90-generic",
"file": "../../../home/ubuntu/programs/linuxKLearn/2/2.1/PrintingDriver/DriverFileOperations.c"

this is my Makefiles:

ifneq ($(KERNELRELEASE),)
    obj-m := PrintModule.o
    PrintModule-objs := DriverMain.o DriverFileOperations.o
EXTRA_CFLAGS := -DTEST_DEBUG -ggdb -O0
else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    rm *.order *.symvers *.mod.c *.o .*.o.cmd .*.cmd .tmp_versions -rf
endif

But when I use Vscode and clangd to read them, clangd shows that there is some problems when including <linux/mm.h>, the problems are as below:

Unknown argument: '-mno-fp-ret-in-387'
Unknown argument: '-mpreferred-stack-boundary=3'
Unknown argument: '-mskip-rax-setup'
Unknown argument: '-mindirect-branch=thunk-extern'
Unknown argument: '-mindirect-branch-register'
......

And clangd cannot find ssize_t. Unknown type name 'ssize_t'clang(unknown_typename) .the inode also has warning Declaration of 'struct inode' will not be visible outside of this functionclang(-Wvisibility),

How to fix include <linux/mm.h> and find ssize_t?

All help is welcome, thanks :)

lucky-star
  • 41
  • 1
  • 2
  • Does this answer your question? [Linux Kernel generate compile-commands.json for module](https://stackoverflow.com/questions/59324820/linux-kernel-generate-compile-commands-json-for-module) – 0andriy Jan 23 '22 at 12:24

2 Answers2

2

What compiler did you use to build the code? I had the same issue, and building with Clang seemed to fix it. GCC and Clang have some options that are only implemented by one of the two compilers or options that are named differently, so clangd doesn't understand all the arguments used by GCC.

For example,

make CC=clang defconfig
make CC=clang

Or to cross compile:

make CC=clang ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig
make CC=clang ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-

Then you can run scripts/clang-tools/gen_compile_commands.py to generate a compile_commands.json. After restarting VSCode or clangd and opening a C file in the project, you should see the indexing begin again, and this time there shouldn't be any errors in the output and cross-references should work when it's finished.

gsgx
  • 12,020
  • 25
  • 98
  • 149
0

Building with Clang is surely a solution, but sometimes building with GCC is necessary. Under such circumstance, clangd's CompileFlags option is helpful.

This is an example configuration of .clangd, which tell the clangd to ignore the options, then warnings disappeared.

CompileFlags:
  Remove: [-fconserve-stack, -fno-allow-store-data-races, -mfunction-return=thunk-extern, -mindirect-branch-cs-prefix, -mindirect-branch-register, -mindirect-branch=thunk-extern, -mskip-rax-setup, -mpreferred-stack-boundary=3, -mno-fp-ret-in-387]
Bachelar Hu
  • 100
  • 1
  • 5