Since a few months, I’ve been refactoring my old C++/OpenGL project. Thus far, I used compilers (MSVC and Clang), my knowledge or free tools. At some point, I also got a chance to leverage a solid static analysis tool - PVS-Studio. The tool helped me with identifying 8 critical issues not to mention good code style and performance enhancements (in total 137 warnings)

Read on to see my report.

Starting With PVS-Studio

This post is sponsored by PVS-Studio but all opinions, code and the article idea come from me.

I’m working on a project which is a visualisation of various sorting algorithms, written in Win32Api, C++, OpenGL. I always put a nice GIF that presents how it works:

You can read my previous articles that describe the project in detail:

After doing some basic refactoring, using some modern features and even checking code with C++ Core Guideline Checkers (available in Visual Studio) I also run a professional static analysis tool: PVS Studio - I used the latest version: PVS-Studio 7.09 (August 27, 2020)

Running the analyser is very simple. Inside Visual Studio 2019 you have to select:

Extensions->PVS-Studio->Check->SolutionThis action starts the PVS process which can last a dozen of seconds (for small projects) or a couple of minutes… or longer - depending on your project size.

After the check completes, you can see the following window with all of the messages:

This shows all issues that the tool has found for the solution (You can also check a single project or a single compilation unit).

As you can see, the numbers are not large, because my project is relatively small (5kloc), yet it helped me with improving the code in several places.

What I like about PVS-Studio is its super handy UI: it’s just a single window with lots of easy to use shortcuts (for example filtering between severity level). It’s easy to filter through files or even skip some errors entirely.

For example, here’s a screenshot where I could easily disable warnings found inside gtest.h which is a part of Google testing framework:

I won’t be able to fix those issues (as it’s third party code), so it’s best to make them silent.

Depending on your project size, you’ll probably need some time to adjust the output to your needs. After those adjustments, you’ll be able to focus on the major problems and limit the number of false positives or non-essential issues.

Here’s some more documentation if you want to start with your project.

What’s more, you can also try PVS-Studio for free through Compiler Explorer! Have a look at this website how to start: Online Examples (C, C++).

Ok, but let’s see what the tool reported for my project.

#windows #iot #c++ #tutorials #pvs-studio #graphics #visual c++ #opengl #vc++ #legacy c++ project

How I Improved My Legacy C++ Project With PVS-Studio
1.25 GEEK