PVS-Studio Integration in PlatformIO

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.

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.

Project import

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

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.
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:

[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
pio check

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.

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));
....
}
....
}
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;
}
bool is_equal(double x, double y) {
return std::fabs(x - y) < 0.001f;
}
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;
}
}

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.

The developer, the debugger, the unicorn. I know all about static analysis and how to find bugs and errors in C++, C#, and Java source code.