To get a trial key
fill out the form below
Team License (standard version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Message submitted.

Your message has been sent. We will email you at

If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

PVS-Studio Integration in PlatformIO

PVS-Studio Integration in PlatformIO

March 5, 2020

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.

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


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.


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.

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

platform = atmelavr
board = megaatmega2560
framework = arduino
check_tool = pvs-studio
check_flags =
    --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:

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

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.

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

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.

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

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;
    } else {
  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:

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.

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)) {
  } 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.


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.

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

Popular related articles
Appreciate Static Code Analysis!

Date: 10.16.2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
Free PVS-Studio for those who develops open source projects

Date: 12.22.2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
The Ultimate Question of Programming, Refactoring, and Everything

Date: 04.14.2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
The Evil within the Comparison Functions

Date: 05.19.2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: 07.31.2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
The Last Line Effect

Date: 05.31.2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: 10.22.2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
PVS-Studio for Java

Date: 01.17.2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
PVS-Studio ROI

Date: 01.30.2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: 11.21.2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…

Comments (0)

Next comments

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →