Linux 静态代码检查工具:sparse 的安装和使用
sparse 是用于 C 语言的语法分析器,用以对 C 代码进行静态检查,它不但可以检查 ANSI C 而且还能检查具有 gcc 扩展的 C 。在 Linux 中,不但可以检查用户端代码,还可以检查内核代码。起初它由 Linus 编写,后来交给其他人维护。
获得一个新的sparse:
$ git clone git://git.kernel.org/pub/scm/devel/sparse/sparse.git
官方文档:
Documentation/dev-tools/sparse.rst
https://linux.die.net/man/1/sparse
LWN.net article: http://lwn.net/Articles/87538/
编译方法:
cd sparse; make; make install
由于kernel默认使用Sparse的@ top Makefile:
CHECK = sparse
CHECKFLAGS := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__
-Wbitwise -Wno-return-void $(CF)
所以在内核编译时,只要 make C=1/2 就可以了。
make C=1 会检查被编译的文件。
make C=2 会检查所有文件,不管是否被编译。
编译后,检查报告即可:
$ make -j4 C=2 > PORJECT_sparse_kernel_error.log 2>&1
$ cat ./PORJECT_sparse_kernel.log | grep “error:” | cut -d ‘:’ -f4- | sort | uniq -c | sort -g -r > PORJECT_sparse_kernel_error.log
PORJECT检测到的错误类型:
455 error: directive in argument list42 error: incompatible types in comparison expression (different type sizes)32 error: Expected ; at end of declaration31 error: bad integer constant expression19 error: cannot dereference this type16 error: got +=14 error: got buf13 error: return with no return value13 error: incompatible types in comparison expression (different signedness)13 error: Expected ) in function declarator12 error: cannot size expression8 error: got -=7 error: Trying to use reserved word 'break' as identifier5 error: Trying to use reserved word 'case' as identifier5 error: got }5 error: Expected ; at the end of type declaration4 error: return expression in void function3 error: Trying to use reserved word 'return' as identifier3 error: marked inline, but without a definition3 error: incompatible types for operation (<)2 error: Trying to use reserved word 'if' as identifier2 error: too many errors2 error: subtraction of different types can't work (different address spaces)2 error: incompatible types for operation (!=)2 error: incompatible types for operation (<=)2 error: got 32 error: got 22 error: got ==2 error: dubious one-bit signed bitfield1 error: symbol 'panic' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/kernel.h:208) - different modifiers1 error: symbol 'mtk_wcn_stp_psm_notify_stp' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/kernel/drivers/conn_soc/common/core/../core/include/stp_core.h:504) - incompatible argument 1 (different signedness)1 error: symbol 'mt_get_chip_sw_ver' redeclared with different type (originally declared at /home/project/PORJECT/kernel/../mediatek/platform/mt6752/kernel/core/include//mach/mt_boot.h:58) - different signedness1 error: symbol '__module_put_and_exit' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/module.h:449) - different modifiers1 error: symbol 'lcm' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/lcm.h:6) - different modifiers1 error: symbol 'kbase_pm_register_inuse_cores' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/gpu/mali/drivers/gpu/arm/midgard/mali_kbase_pm_policy.h:224) - different signedness1 error: symbol 'gcd' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/gcd.h:6) - different modifiers1 error: symbol 'g_call_state' redeclared with different type (originally declared at /home/project/PORJECT/kernel/../mediatek/kernel/include//mach/battery_common.h:185) - different signedness1 error: symbol 'dprec_logger_trigger' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/dispsys/display_recorder.h:145) - incompatible argument 1 (different signedness)1 error: symbol 'dprec_logger_submit' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/dispsys/display_recorder.h:155) - incompatible argument 1 (different signedness)1 error: symbol 'dprec_logger_start' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/dispsys/display_recorder.h:146) - incompatible argument 1 (different signedness)1 error: symbol 'dprec_logger_done' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/dispsys/display_recorder.h:147) - incompatible argument 1 (different signedness)1 error: symbol 'do_exit' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/kernel.h:214) - different modifiers1 error: symbol 'crc32_le' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/crc32.h:11) - different modifiers1 error: symbol '__crc32c_le' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/crc32.h:14) - different modifiers1 error: symbol 'crc32_be' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/crc32.h:12) - different modifiers1 error: symbol 'complete_and_exit' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/kernel.h:216) - different modifiers1 error: symbol '_bin2bcd' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/bcd.h:20) - different modifiers1 error: symbol '_bcd2bin' redeclared with different type (originally declared at /home/project/PORJECT/kernel/include/linux/bcd.h:19) - different modifiers1 error: incompatible types in comparison expression (different address spaces)1 error: incompatible types for operation (==)1 error: got 01 error: got \1 error: Expected ) in function call
[分析]
455 error: directive in argument list //语法问题,non-issue.32 error: Expected ; at end of declaration //使用了__restrict__,优化编译器的限定符引起的误报19 error: incompatible types in comparison expression (different type sizes) //fixed16 error: got += //使用了__restrict__,优化编译器的限定符引起的误报14 error: got buf //使用了__restrict__,优化编译器的限定符引起的误报13 error: return with no return value //有些是误报,没有明显逻辑错误,暂时不改13 error: incompatible types in comparison expression (different signedness) //fixed13 error: Expected ) in function declarator //使用了__restrict__,优化编译器的限定符引起的误报8 error: got -= //使用了__restrict__,优化编译器的限定符引起的误报7 error: Trying to use reserved word 'break' as identifier //使用了__restrict__,优化编译器的限定符引起的误报5 error: Trying to use reserved word 'case' as identifier //使用了__restrict__,优化编译器的限定符引起的误报5 error: got } //使用了__restrict__,优化编译器的限定符引起的误报5 error: Expected ; at the end of type declaration //使用了__restrict__,优化编译器的限定符引起的误报3 error: Trying to use reserved word 'return' as identifier //使用了__restrict__,优化编译器的限定符引起的误报3 error: incompatible types for operation (<) //误报2 error: Trying to use reserved word 'if' as identifier //使用了__restrict__,优化编译器的限定符引起的误报2 error: subtraction of different types can't work (different address spaces) // 使用了__user限定符的指针page和指向page的的指针ptr作运算,误报说不同地址空间不能作运算2 error: return expression in void function //void函数return 0, 不需要改2 error: marked inline, but without a definition //focaltech_driver 驱动close api 声明inline 但实现没有inline,TP一般不会close,也没有逻辑性问题,暂时不改2 error: incompatible types for operation (<=) //误报,void __iomem * p 使用__iomem时,告诉编译器忽略对变量的检查,但Smatch/Sparse不知道2 error: got 3 //使用了__restrict__,优化编译器的限定符引起的误报2 error: got 2 //使用了__restrict__,优化编译器的限定符引起的误报2 error: got == //使用了__restrict__,优化编译器的限定符引起的误报1 error: too many errors //使用了__restrict__,优化编译器的限定符引起的误报1 error: symbol 'mtk_wcn_stp_psm_notify_stp' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/kernel/drivers/conn_soc/common/core/../core/include/stp_core.h:504) - incompatible argument 1 (different signedness) //声明使用UINT32 , 实现使用MTKSTP_PSM_ACTION_T enum,忽略1 error: symbol 'mt_get_chip_sw_ver' redeclared with different type (originally declared at /home/project/PORJECT/kernel/../mediatek/platform/mt6752/kernel/core/include//mach/mt_boot.h:58) - different signedness //使用了__weak 限定符,误报1 error: symbol 'kbase_pm_register_inuse_cores' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/gpu/mali/drivers/gpu/arm/midgard/mali_kbase_pm_policy.h:224) - different signedness //无逻辑错误,忽略1 error: symbol 'g_call_state' redeclared with different type (originally declared at /home/project/PORJECT/kernel/../mediatek/kernel/include//mach/battery_common.h:185) - different signedness //无逻辑错误,忽略1 error: symbol 'dprec_logger_trigger' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/dispsys/display_recorder.h:145) - incompatible argument 1 (different signedness) //无逻辑错误,忽略1 error: symbol 'dprec_logger_submit' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/dispsys/display_recorder.h:155) - incompatible argument 1 (different signedness) //无逻辑错误,忽略1 error: symbol 'dprec_logger_start' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/dispsys/display_recorder.h:146) - incompatible argument 1 (different signedness) //无逻辑错误,忽略1 error: symbol 'dprec_logger_done' redeclared with different type (originally declared at /home/project/PORJECT/kernel/mediatek/platform/mt6752/kernel/drivers/dispsys/display_recorder.h:147) - incompatible argument 1 (different signedness) //无逻辑错误,忽略1 error: incompatible types in comparison expression (different address spaces) //误报1 error: incompatible types for operation (!=) //非指针和NULL 比较,语法问题,暂时不改1 error: incompatible types for operation (==) //extern vramsize; 没有指定类型,然后if(vramsize == 0), fixed1 error: got 0 //使用了__restrict__,优化编译器的限定符引起的误报1 error: got \ //使用了__restrict__,优化编译器的限定符引起的误报1 error: Expected ) in function call //使用了__restrict__,优化编译器的限定符引起的误报1 error: cannot size expression //使用了__restrict__,优化编译器的限定符引起的误报
Linux 静态代码检查工具:sparse 的安装和使用相关推荐
- linux静态代码检查工具,linux下splint检测C语言代码质量
在linux下并没有pclint,可以使用splint代替.splint使用一.splint介绍splint是一个静态检查C语言程序安全弱点和编写错误的工具.splint会进行多种常规检查,包括未使 ...
- Python 常用静态代码检查工具简介
对于我这种习惯了 Java 这种编译型语言,在使用 Python 这种动态语言的时候,发现错误经常只能在执行的时候发现,总感觉有点不放心. 而且有一些错误由于隐藏的比较深,只有特定逻辑才会触发,往往导 ...
- 静态代码检查工具简介
静态代码检查工具简介 在 Java 软件开发过程中,开发团队往往要花费大量的时间和精力发现并修改代码缺陷.传统的代码复审.同行评审,通过人工方式来检查缺陷仍然是一件耗时耗力的事情.Java 静态代码分 ...
- 静态代码检查工具 cppcheck 的使用
CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们写的 ...
- vscode中对flake8(python静态代码检查工具)和yapf(python代码格式化工具)的设置
在命令行下安装好flake8和yapf后在vscode中的用户设置(settings.json)中添加以下两行即可: "python.linting.flake8Enabled": ...
- 静态代码检查工具-PMD
静态代码检查工具-PMD 分类: 网络安全/ 工具使用/ 文章 提高代码的质量,除了要提高逻辑上的控制以及业务流程的理解外,代码本身也存在提高的空间,例如一些潜在的问题可以很早的就避免.类似于编码规范 ...
- Java 静态代码检查工具及其原理
Intellij IDEA 静态代码检查工具: checkstyle.pmd.findbugs. Alibaba Java Coding Guidelines; 三种工具的对比 工具 目的 检查项 F ...
- Python 进阶 — Pylint 静态代码检查工具
目录 文章目录 目录 Pylint 错误类型 安装 使用 Pylint 与 Flake8 一般,Pylint 也是一款 Python 的静态代码检查工具,它会分析 Python 代码中的错误,查找不符 ...
- Python静态代码检查工具Flake8
简介 Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具,相对于目前热度比较高的Pylint来说,Flake8检查规则灵活,支持集成额外插件,扩展性强.Flake8是对 ...
最新文章
- 创新时代的管理:《创新赢天下》
- 搭建集群时的问题总结
- (11)FPGA跨时钟域问题导致数据偶尔异常(学无止境)
- [iOS] 响应式编程开发-ReactiveCocoa(一)
- ubuntu下安装ettercap
- 阶段3 2.Spring_01.Spring框架简介_06.spring的体系结构
- 《软件项目管理案例教程》应考概念要点及重点整理
- 2021年流动式起重机司机模拟考试题库及流动式起重机司机模拟考试系统
- foxmail本地文件夹同步服务器,foxmail同步QQ邮箱里的所有文件夹
- mathtype2022数学公式编辑器快捷键及操作技巧分享教程
- 基于PHP语言的汉语转拼音的类
- GD32F103RBT6开发笔记
- 基于机器学习的电信套餐个性化推荐模型的设计与实现
- 武汉大学计算机学院应时老师,肖春霞 - 教师简历 CV- 武汉大学计算机学院
- Python 菜单中的分割线
- 小E开发板wifi音箱二实现网络助手发送整个WAV音乐
- “企业级零代码黑客马拉松”决赛圆满落幕
- 序列化(Serialization)
- cesium实现飞线特效
- windows server 2016桌面添加 此电脑 我的电脑 计算机 图标