PVS-Studio's incremental analysis mode

Introduction

One of the main problems when using a static analyzer is the necessity to spend much time on analyzing project files after each project modification. It is especially relevant to large projects that are actively developing.

Total analysis can be launched regularly in separate cases - for instance, once a day during night builds. But the greatest effect of using the analyzer can be achieved only through earlier detection and fix of found defects. The earlier you can find an error, the less amount of code you will have to fix. That is, the most proper way to use a static analyzer is to analyze a new code right after it is written. Having to launch analysis of all the modified files manually and waiting for it to finish each time surely complicates this scheme. It is incompatible with the intense development and debugging of new code. It's just inconvenient, after all. But PVS-Studio offers a solution of this issue.

Two tasks solved by incremental analysis

  • Automation of the analyzer launch procedure right after compilation on the developer's computer.
  • Gaining profit from integrating static analysis in a large project WITHOUT having to analyze the whole project. You can virtually start integrating static code analysis checking only that code (those files) currently modified by developers and don't get into a heap of old files which are not modified.

Using incremental analysis

PVS-Studio's incremental analysis mode allows you to solve problems regarding regular launch of static analysis. The user starts getting practical benefit without having to perform primary analysis of the whole project and marking a lot of false positives in unused code. This mode virtually makes PVS-Studio similar to the /analyze switch in certain Visual Studio versions regarding usability. It also provides you with a more convenient interface and use cases. Now you can just work on your code, compile it and get messages from time to time about possible issues. Since the analyzer works in background (you can set the number of processor cores to perform analysis using the ThreadCount option), PVS-Studio doesn't interfere with work of other programs. And it means that you can easily install the tool on computers of many (or all) developers in your department to detect issues in the code right after they appear.

You can turn on the afterbuild incremental analysis in the PVS-Studio menu: Incremental Analysis After Build/Incremental Analysis After Make (Figure 1). This option is on by default.

Figure 1 — Managing PVS-Studio's incremental analysis mode

Figure 1 — Managing PVS-Studio's incremental analysis mode

Once the incremental analysis option is enabled, PVS-Studio will automatically perform background analysis of all the modified files after building the project. If PVS-Studio detects such modifications, incremental analysis will be launched automatically, and an animated PVS-Studio icon will appear in the notification area (Figure 2). Note that new icons may be often hidden in Windows's notification area.

Figure 2 — PVS-Studio performing incremental analysis

Figure 2 — PVS-Studio performing incremental analysis

The notification area's shortcut menu allows you to pause or abort current check (commands Pause and Abort respectively). You may find it useful sometimes to turn off incremental analysis for some time. For instance, you may need it when editing base h-files, which causes recompilation of many files. If you wish to turn off incremental analysis just for some time, you may use commands of the shortcut menu and PVS-Studio's main menu 'Disable incremental analysis until IDE restart' (Figures 1 and 2).

If the analyzer detects errors in code while performing incremental analysis, the number of detected errors will be shown in the title of the PVS-Studio's window tab in background. Clicking on the icon in the notification area (or the window itself) opens the PVS-Studio Output window where you can start handling the errors without even waiting for analysis to finish.

Figure 3 – The result of incremental analysis: 15 suspect fragments of code are found

Figure 3 – The result of incremental analysis: 15 suspect fragments of code are found

Keep in mind that after the first total check of your project you should review all the diagnostic messages for the required files and fix the errors found in your code. Regarding the rest messages, you should either mark them as false positives or disable those messages or analyzer types which are not relevant to your project. This approach will allow you to get a message list clear of meaningless and unnecessary messages.

Incremental analysis operational principle inside the IDE

To detect modifications in files with source code PVS-Studio monitors the state of object files (obj/o files) generated by the C++ compiler for each C/C++ file. At the moment before the very start of building the project in the environment, PVS-Studio IDE plugin registers the presence of object files and time of modification for all the C/C++ project files. In case the project contains a large number of files, it may cause a slightly noticeable hang of IDE at the moment of build start. After the build, IDE compares their final state with the one registered before the build. Then it launches incremental analysis for those C/C++ files whose object files have been regenerated or generated anew. We should mention that only those files will be analyzed for which the compiler has generated object files after the build. If there are compilation or linking errors, the corresponding C/C++ files won't be analyzed as the compiler hasn't regenerated their object files.

Disadvantages of incremental analysis in Microsoft Visual Studio

If PVS-Studio uses the CL.exe C++ preprocessor integrated into Visual Studio, an unpleasant file lock issue might occur in rare cases. This is how it may look: you edit a file and compile it. Incremental analysis is launched. At this moment you notice some small defect, fix it quickly and try to recompile the file again. Visual Studio warns you that the edited file cannot be written because some program has locked it. This is CL.exe which is performing the preprocessing operation now. Unfortunately, we cannot manage this lock in any way. If you come across this issue, you should wait a few seconds and continue the aborted work. Note that this situation is much rarer when using the Clang preprocessor (Preprocessor settings option). That's why you'd better use Clang as the preprocessor when possible.


Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++ and C#

goto PVS-Studio;