• C++静态代码分析


    这里记录一下使用cppcheck进行C++代码静态检测的方法和步骤。

    本机安装cppcheck

    sudo apt-get update && sudo apt-get install cppcheck
    
    • 1

    使用cppcheck来检查代码

    新建一个目录,并在目录中加入如下内容的cpp文件,用于测试静态代码分析工具。

    测试代码

    #include 
    
    using namespace std;
    
    int test_fun()
    {
      int a;
      return a;
    }
    
    
    int main(int argc, char* argv[]) {
      int num = argc - 1;
    
      int * a = nullptr; // intentional mistake
      *a = 9;
    
      if (num = 0) {
        cout << "No arguments provided\n";
      } else if (num == 0) {  // intentional mistake
        cout << "1 argument provided\n";
      } else if (num == 2) {
        cout << "2 arguments provided\n";
      } else {
        cout << num << " arguments provided\n";
      }
      if (argv != 0) {
        cout << "argv not null\n";;  // intentional extra-semicolon
      }
      if (argv == nullptr) {
        return **argv;  // intentional nullptr dereference
      }
    
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    使用cppcheck检测效果如下:

    ➜  test_git git:(master) ✗ cppcheck ./src                                                                                                                                                                                                      
    Checking src/main.cpp ...
    src/main.cpp:8:2: error: Null pointer dereference: a [nullPointer]
    *a = 9;
     ^
    src/main.cpp:7:11: note: Assignment 'a=nullptr', assigned value is 0
    int * a = nullptr;
              ^
    src/main.cpp:8:2: note: Null pointer dereference
    *a = 9;
     ^
    ➜  test_git git:(master) ✗ ament_cppcheck ./src                                                                                                                                                                                              
    [src/main.cpp:8]: (error: nullPointer) Null pointer dereference: a
    1 errors
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    基于dockercppcheck

    docker pull neszt/cppcheck-docker
    
    #在代码根目录运行
    docker run -t -v $(pwd):/src neszt/cppcheck-docker
    
    • 1
    • 2
    • 3
    • 4

    vscode中进行静态代码检测

    在安装了ROS2后,可使用下面的命令安装ament linters

    sudo apt-get install ros-$ROS_DISTRO-ament-lint
    
    • 1

    运行完后,系统目录/opt/ros/galactic/bin下就会被安装下图所示的文件。

    其中的ament_cppcheck即可用于C++静态代码检测。

    image-20220727215654381

    可直接运行ament_cppcheck命令。效果与之前的cppcheck类似。

    ➜  test_git git:(master) ✗ ament_cppcheck src/                git:(master|✚1…3 
    [src/main.cpp:19]: (error: nullPointer) Null pointer dereference: a
    [src/main.cpp:8]: (error: uninitvar) Uninitialized variable: a
    2 errors
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这里我们将其加入到vscodetasks.json文件中。这样就可以直接在vscode中直接运行静态代码检测任务了。

            {
                "label": "cppcheck",
                "detail": "Run static code checker cppcheck.",
                "type": "shell",
                "command": "ament_cppcheck src/",
                "presentation": {
                    "panel": "dedicated",
                    "reveal": "silent",
                    "clear": true
                },
                "problemMatcher": [
                    {
                        "owner": "cppcheck",
                        "source": "cppcheck",
                        "pattern": [
                            {
                                "regexp": "^\\[(.+):(\\d+)\\]:\\s+(.+)$",
                                "file": 1,
                                "line": 2,
                                "message": 3
                            }
                        ]
                    }
                ]
            },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    Shift+Ctrl+P打开命令面板,输入task并回车即可看到任务列表。

    完整的tasks.json文件还包含代码格式化,代码格式检测以及其他有用的任务。文件过长,可到下面的链接中查看。

    https://github.com/shoufei403/ros2_galactic_ws/blob/master/.vscode/tasks.json


    觉得有用就点赞吧!

    我是首飞,一个帮大家填坑的机器人开发攻城狮。

    另外在公众号《首飞》内回复“机器人”获取精心推荐的C/C++,Python,Docker,Qt,ROS1/2等机器人行业常用技术资料。

  • 相关阅读:
    原生实现.NET 5.0+ 自定义日志
    (ICCV 2021) Hierarchical Aggregation for 3D Instance Segmentation
    [题] 前缀和 (含输入输出的耗时对比)
    软件设计模式系列之十六——命令模式
    《痞子衡嵌入式半月刊》 第 40 期
    元胞自动机( Cellular Automata)研究 (Python代码实现)
    换掉ES!Redis官方搜索引擎来了,性能炸裂!
    浅聊历史——战国
    ThreadLocal源码解析学习
    HashMap源码解析二
  • 原文地址:https://blog.csdn.net/shoufei403/article/details/126089932