本文主要是介绍使用 Cppcheck 进行静态代码分析:一个完整的shell脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 使用 Cppcheck 进行静态代码分析:一个完整的 shell脚本
- 安装 Cppcheck
- 1. 克隆 Cppcheck 仓库
- 2. 构建和安装 Cppcheck
- 检测原理
- 脚本介绍
- 脚本详解
- 1. 基本选项
- 2. 额外选项
- 3. 抑制选项
- 4. 包含路径
- 检测
- 1. 使用 CMake 生成 `compile_commands.json`
- 2. 运行 Cppcheck
- 结果示例
使用 Cppcheck 进行静态代码分析:一个完整的 shell脚本
本文将介绍如何使用 Cppcheck 进行静态代码分析,并提供一个完整的shell脚本。
安装 Cppcheck
1. 克隆 Cppcheck 仓库
首先,从 GitHub 克隆 Cppcheck 仓库:
git clone https://github.com/danmar/cppcheck.git
cd cppcheck
2. 构建和安装 Cppcheck
使用以下命令构建和安装 Cppcheck:
mkdir build
cd build
cmake ..
make
sudo make install
检测原理
Cppcheck 使用多种技术来分析 C/C++ 代码,包括但不限于以下几种:
-
语法解析:Cppcheck 使用自定义的语法解析器来理解 C/C++ 代码的结构。这包括函数、类、变量的定义和使用等。
-
数据流分析:Cppcheck 分析变量和数据在代码中的流动情况,以检测未初始化变量、空指针引用等问题。
-
符号执行:通过模拟代码执行路径,Cppcheck 能够发现逻辑错误和潜在的运行时问题。
-
配置驱动:Cppcheck 允许用户通过配置文件和命令行参数定制检查规则和抑制特定的警告,以减少误报并专注于真正的问题。
-
编译数据库:Cppcheck 支持读取编译数据库(如
compile_commands.json
),从而准确地了解项目的编译选项和文件依赖关系。
通过以上技术,Cppcheck 能够在不需要编译代码的情况下,提供深入且全面的代码质量检查。
脚本介绍
下面是一个用于运行 Cppcheck 的 Bash 脚本。该脚本配置了多个选项和抑制规则,以减少误报并生成详细的检查器报告。
#!/bin/bash# cppcheck 命令行选项
OPTIONS="--enable=style \--enable=performance \--enable=portability \--check-level=exhaustive"# 根据你的配置,添加相应的检查项
EXTRA_OPTIONS="--enable=information \--enable=unusedFunction"# 根据你的配置,添加相应的抑制选项
SUPPRESS="--suppress=information \--suppress=missingInclude \--suppress=missingIncludeSystem \--suppress=unmatchedSuppression \--suppress=passedByValue \--suppress=unusedFunction \--suppress=uninitMemberVar \--suppress=uninitvar \--suppress=nullPointer \--suppress=shadowVariable \--suppress=variableScope \--suppress=duplicateExpression \--suppress=useInitializationList"# 包含路径
INCLUDE="-I /usr/include -I/usr/local/include"# 运行 cppcheck
cppcheck --project=compile_commands.json $OPTIONS $EXTRA_OPTIONS $SUPPRESS $INCLUDE --checkers-report=checkers_report.txt
脚本详解
1. 基本选项
OPTIONS="--enable=style \--enable=performance \--enable=portability \--check-level=exhaustive"
--enable=style
:启用代码风格检查。--enable=performance
:启用性能检查。--enable=portability
:启用可移植性检查。--check-level=exhaustive
:进行详尽的检查,分析所有代码分支,尽管这样可能会增加分析时间。
2. 额外选项
EXTRA_OPTIONS="--enable=information \--enable=unusedFunction"
--enable=information
:启用信息级别的检查。--enable=unusedFunction
:启用未使用函数的检查。
3. 抑制选项
SUPPRESS="--suppress=information \--suppress=missingInclude \--suppress=missingIncludeSystem \--suppress=unmatchedSuppression \--suppress=passedByValue \--suppress=unusedFunction \--suppress=uninitMemberVar \--suppress=uninitvar \--suppress=nullPointer \--suppress=shadowVariable \--suppress=variableScope \--suppress=duplicateExpression \--suppress=useInitializationList"
这些选项用于抑制特定类型的警告,减少误报。例如:
--suppress=missingIncludeSystem
:忽略缺少系统包含文件的警告。--suppress=uninitMemberVar
:忽略未初始化成员变量的警告。
4. 包含路径
INCLUDE="-I /usr/include -I/usr/local/include"
指定包含路径,以确保分析过程中能找到所有必要的头文件。
检测
1. 使用 CMake 生成 compile_commands.json
为了使用 Cppcheck 的 --project
选项,你需要一个 compile_commands.json
文件。这个文件是一个编译数据库,包含项目中所有源文件的编译信息。
如果你的项目使用 CMake 构建,可以通过以下命令生成 compile_commands.json
文件:
cd /path/to/your/project
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
运行上述命令后,会在项目的构建目录中生成一个 compile_commands.json
文件。
2. 运行 Cppcheck
cppcheck --project=compile_commands.json $OPTIONS $EXTRA_OPTIONS $SUPPRESS $INCLUDE --checkers-report=checkers_report.txt
--project=compile_commands.json
:指定项目的编译数据库文件。--checkers-report=checkers_report.txt
:生成包含所有检查器详细信息的报告文件。
结果示例
Checking /home/test/cppcheck/example.cpp ...
Checking /home/test/cppcheck/example.cpp: ALLOC_HOOK_VERSION=0xfe9abbf;__PIC__=1...
/home/test/cppcheck/example.cpp:13:10: style: Variable 'intPtr' can be declared as pointer to const [constVariablePointer]int* intPtr = new int[100]; // 分配int类型的内存^
/home/test/cppcheck/example.cpp:18:13: style: Variable 'doublePtr' can be declared as pointer to const [constVariablePointer]double* doublePtr = new double(3.14); // 分配double类型的内存^
/home/test/cppcheck/example.cpp:23:18: style: Variable 'strPtr' can be declared as pointer to const [constVariablePointer]std::string* strPtr = new std::string("Hello, World!"); // 分配字符串类型的内存^
/home/test/cppcheck/example.cpp:40:11: style: Variable 'mmapPtr' can be declared as pointer to const [constVariablePointer]void* mmapPtr = mmap(nullptr, mmapSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
这篇关于使用 Cppcheck 进行静态代码分析:一个完整的shell脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!