pvs-stdio ue4_PlatformIO中的PVS-Studio集成

本文展示了如何在PlatformIO环境中使用PVS-Studio静态分析器检查微控制器项目,以增强嵌入式系统的代码质量。通过一个Arduino Mega板上的六脚架控制程序示例,解释了从项目导入到配置分析器的步骤,并举例说明了检查过程中发现的潜在错误。
摘要由CSDN通过智能技术生成

pvs-stdio ue4

Picture 5

Recently, the PlatformIO development environment of embedded systems has supported PVS-Studio. In this article, you'll find out how to check your code with the static analyzer on the example of an open project.

最近,嵌入式系统的PlatformIO开发环境已支持PVS-Studio。 在本文中,您将在一个打开的项目示例中找到如何使用静态分析器检查代码。

什么是PlatformIO? (What is PlatformIO?)

PlatformIO is a cross-platform tool for microcontroller programming. The core of PlatformIO is a command-line tool, however it is recommended to use it as a plugin for Visual Studio Code. It supports a large number of modern microchips, and boards based on them. It can automatically download suitable build systems. The site has a large collection of libraries for managing plug-in electronic components. There is support for several static code analyzers, including PVS-Studio. PlatformIO是用于微控制器编程的跨平台工具。 PlatformIO的核心是命令行工具,但是建议将其用作Visual Studio Code的插件。 它支持大量的现代微芯片和基于它们的板。 它可以自动下载合适的构建系统。 该站点具有大量用于管理插入式电子组件的库。 支持多种静态代码分析器,包括PVS-Studio。

项目导入 (Project import)

To demonstrate, let's take the controlling program for ArduPod — a hexapod on the Arduino Mega board.

为了演示,让我们采用ArduPod的控制程序-Arduino Mega板上的六脚架

Let's create a new project for the appropriate board and copy the source code:

让我们为相应的电路板创建一个新项目,然后复制源代码:

图片2

In the /arduino/AP_Utils/examples/ directory, there are several examples of programs for configuring and running the hexapod, we'll use servo_test.ino. Basically, the program for Arduino is created as sketches in the INO format, which in this case is not quite suitable. In order to make the correct .cpp file from it, it is usually enough to change the file extension, add the #include <Arduino.h> header at the beginning, and make sure that functions and global variables are declared before accessing them.

/ arduino / AP_Utils / examples /目录中,有几个用于配置和运行六脚架的程序示例,我们将使用Servo_test.ino。 基本上,用于Arduino的程序是以INO格式的草图创建的,在这种情况下不太适合。 为了从中创建正确的.cpp文件,通常足以更改文件扩展名,在开头添加#include <Arduino.h>标头,并确保在访问函数和全局变量之前已声明它们。

图片3

During the build process, some errors might occur indicating the lack of required third-party libraries. However, PlatformIO will help you find them in its repository.

在生成过程中,可能会发生一些错误,表明缺少所需的第三方库。 但是,PlatformIO将帮助您在其存储库中找到它们。

In file included from src\servo_test.cpp:20:0:
src/AP_Utils.h:10:37: fatal error: Adafruit_PWMServoDriver.h:
No such file or directory
*******************************************************************************
* Looking for Adafruit_PWMServoDriver.h dependency? Check our library registry!
*
* CLI> platformio lib search "header:Adafruit_PWMServoDriver.h"
* Web> https://platformio.org/lib/search?query=header:Adafruit_PWMServoDriver.h
*
*******************************************************************************
compilation terminated.

Suitable options are available by the link. The needed dependency is installed by the following command in the terminal:

链接提供了适当的选项。 所需的依赖项通过以下命令在终端中安装:

pio lib install "Adafruit PWM Servo Driver Library"

配置分析仪并开始检查 (Configuring analyzers and starting a check)

To configure analyzers, you need to edit the platformio.ini configuration file like this:

要配置分析器,您需要像这样编辑platformio.ini配置文件:

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
check_tool = pvs-studio
check_flags =
  pvs-studio:
    --analysis-mode=4 ; General analysis mode. Set to 32 for MISRA
    --exclude-path=/.pio/libdeps ; Ignore dependency libraries

The check_tool parameter indicates which code analyzers to apply. They can be configured in the check_flags parameter. More detailed instructions are available in the documentation on the official website: https://docs.platformio.org/en/latest/plus/check-tools/pvs-studio.html

check_tool参数指示要应用的代码分析器。 可以在check_flags参数中配置它们。 官方网站上的文档中提供了更详细的说明: https : //docs.platformio.org/en/latest/plus/check-tools/pvs-studio.html

Finally, you can run a project check with a command in the terminal. Before the first check, the environment itself will download the current analyzer distribution.

最后,您可以在终端中使用命令运行项目检查。 在第一次检查之前,环境本身将下载当前的分析仪发行版。

pio check

检查hexapod程序的结果 (The result of checking the hexapod program)

This time, the purpose of this article is to demonstrate the PVS-Studio integration with PlatformIO, not the analyzer's diagnostic abilities. Nevertheless, once the project is checked, let's take a quick look at a couple of errors to make sure the project's check was successful.

这次,本文的目的是演示PVS-Studio与PlatformIO的集成,而不是分析仪的诊断能力。 不过,一旦检查了项目,就让我们快速查看几个错误,以确保项目的检查成功。

V519 There are identical sub-expressions to the left and to the right of the '-' operator: pow(t, 2) — pow(t, 2). AP_Utils.cpp 176 V519在'-'运算符的左侧和右侧有相同的子表达式:pow(t,2)-pow(t,2)。 AP_Utils.cpp 176
pointLeg* AP_Utils::traceLeg(uint8_t leg, float phi, float z,
  int resolution, uint8_t shape) {
  ....
  if(shape == ELLIPTIC) {
    ....
    float v = sqrt(pow(phi - legs[leg].phi, 2) + pow(z - legs[leg].z, 2));
    float u = sqrt(pow(phi - phi0, 2) + pow(z - z0, 2));
    float t = sqrt(pow(phi0 - legs[leg].phi, 2) + pow(z0 - legs[leg].z, 2));
    theta = acos((pow(t, 2) - pow(t, 2) - pow(v, 2))/(-2.0*t*u));
    ....
  }
  ....
}

Two identical expressions are subtracted from one another. It is not clear what the mathematical sense of this difference is. Perhaps, the programmer simply didn't simplify the expression. Or maybe there is a typo — another argument has to be there instead of one of these ts.

彼此减去两个相同的表达式。 目前尚不清楚这种差异的数学意义是什么。 也许,程序员只是没有简化表达式。 或者,也许有一个错字-另一个参数必须是有代替的这些T一张

V550 An odd precise comparison: value != — 1. It's probably better to use a comparison with defined precision: fabs(A — B) > Epsilon. AP_Utils.cpp 574 V550奇怪的精确比较:值!= — 1.最好使用定义精度的比较:fabs(A — B)> Epsilon。 AP_Utils.cpp 574
float AP_Utils::sr04_average(uint8_t trig, uint8_t echo,
  int unit, int samples, int time) {
  ....
  float average, pause, value;
  ....
  for(int i=0; i<samples; i++) {
    value = sr04(trig, echo, unit);
    if(value != -1) { // <=
      total += value;
      delay(pause);
    } else {
      i--;
    }
  }
  average = total/samples;
  ....
  return average;
}

The warning indicates the sloppy comparison of floating point numbers. As it's impossible to accurately represent real numbers with a finite number of bits, it is safer to establish the equality of fractional numbers by comparing their difference with an epsilon. For example, as follows:

该警告表示浮点数的草率比较。 由于不可能用有限的位数来精确表示实数,因此通过将分数的差与epsilon进行比较来确定分数的相等性是更安全的。 例如,如下:

bool is_equal(double x, double y) {
  return std::fabs(x - y) < 0.001f;
}

The only safe option to directly compare non-integer numbers is to assign constants to the variables, and then compare their values with these constants. In this case, value variable is not assigned -1 anywhere. This is how the AP_Utils::sr04 callee method operates. It returns the checked value.

直接比较非整数数字的唯一安全选择是将常量分配给变量,然后将其值与这些常量进行比较。 在这种情况下, 变量不会在任何地方分配-1。 这就是AP_Utils :: sr04被调用者方法的操作方式。 它返回检查值。

float AP_Utils::sr04(uint8_t trig, uint8_t echo, int unit) {
  ....
  float duration, distance;
  ....
  duration = pulseIn(echo, HIGH);
  distance = (346.3*duration*0.000001*unit)/2; // <=
  
  if((distance >= 0.02*unit) && (distance <= 4*unit)) {
    ....
    return(distance);
  } else {
    ....
    return 0;
  }
}

As you can see, the result of some calculations will be written in value. Assignment of -1 is nowhere to be seen, whereas AP_Utils::sr04 can return 0. This implies that the wrong result is compared.

如您所见,某些计算的结果将写入value中 。 -1的分配是无处可见,而AP_Utils :: sr04可以返回0。这意味着比较了错误的结果。

结论 (Conclusion)

In this article, we've touched upon the process of checking projects on microcontrollers by the static code analyzer in the PlatformIO environment for programming embedded systems. Let me quickly remind you that everyone interested in trying PVS-Studio can avail of the trial mode and for open source projects it is possible to get a free license.

在本文中,我们介绍了通过PlatformIO环境中的静态代码分析器检查嵌入式系统上的微控制器上的项目的过程。 让我快速提醒您,任何有兴趣尝试PVS-Studio的人都可以使用试用模式 ,对于开源项目,可以获取免费许可证

For those who want to learn more about PVS-Studio features in more detail, I recommend checking out the following articles:

对于那些想更详细地了解PVS-Studio功能的人,我建议您阅读以下文章:

翻译自: https://habr.com/en/company/pvs-studio/blog/491220/

pvs-stdio ue4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值