【问题标题】:How do I configure clang_complete for linux kernel module development?如何为 linux 内核模块开发配置 clang_complete?
【发布时间】:2024-01-21 03:43:01
【问题描述】:

我试图让 gcc 到 dump the include paths,而我的 .clang_complete 文件现在看起来像:

-std=gnu89
-nostdinc
-I/usr/src/linux-headers-3.0.0-16-generic/arch/x86/include
-I/lib/modules/3.0.0-16-generic/build/arch/x86/include/generated
-I/lib/modules/3.0.0-16-generic/build/include
-I/lib/modules/3.0.0-16-generic/build/ubuntu/include
-I/usr/lib/gcc/i686-linux-gnu/4.6.1/include
-D__KERNEL__
-DMODULE

libclang 在编辑一个简单的 hello world 内核模块时仍然会抱怨大量错误和警告。

1 /lib/modules/3.0.0-16-generic/build/include/linux/list.h|24 col 42 warning| declaration of 'struct list_head' will not be visible outside of this function
2 /lib/modules/3.0.0-16-generic/build/include/linux/list.h|26 col 6 error| incomplete definition of type 'struct list_head'
3 /lib/modules/3.0.0-16-generic/build/include/linux/list.h|24 col 42 info| forward declaration of 'struct list_head'
4 /lib/modules/3.0.0-16-generic/build/include/linux/list.h|27 col 6 error| incomplete definition of type 'struct list_head'
5 /lib/modules/3.0.0-16-generic/build/include/linux/list.h|24 col 42 info| forward declaration of 'struct list_head'
6 /lib/modules/3.0.0-16-generic/build/include/linux/list.h|37 col 38 warning| declaration of 'struct list_head' will not be visible outside of this function
...

<linux/types.h> 中的struct list_head 的定义已经包含在<linux/list.h> 中。两者都在/lib/modules/3.0.0-16-generic/build/include

【问题讨论】:

    标签: c linux vim clang kernel-module


    【解决方案1】:

    与 clang_complete 捆绑的 cc_args.py 可能对您有用。它应该构建一个有效的 .clang_complete 文件。不幸的是(我刚刚测试过它,我会尝试修复它),它在这里不起作用(无法创建 .clang_complete 文件)。

    但是,通过调整脚本及其结果,我已经能够生成有效的 .clang_complete 文件:

    -D__KERNEL__
    -DCONFIG_AS_CFI=1
    -DCONFIG_AS_CFI_SIGNAL_FRAME=1
    -DCONFIG_AS_CFI_SECTIONS=1
    -DCONFIG_AS_FXSAVEQ=1
    -nostdinc
    -I/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.3/include
    -I/usr/src/linux-3.2.12-1-ARCH/arch/x86/include
    -I/usr/src/linux-3.2.12-1-ARCH/arch/x86/include/generated
    -I/usr/src/linux-3.2.12-1-ARCH/include
    -DMODULE
    -DKBUILD_STR(s)=#s
    -include /usr/src/linux-3.2.12-1-ARCH/include/linux/kconfig.h
    

    有了这个.clang_complete,只剩下一个错误,与__kfree_rcu有关,更多信息见:https://lkml.org/lkml/2012/1/12/2

    【讨论】: