Incremental analysis mode in PVS-Studio
- Watch, don't read (YouTube)
- Windows: C, C++, C#
- Linux/macOS: C, C++
- Linux/macOS: C#
- Windows/Linux/macOS: Java
- References
Server incremental analysis mode from command line is available only under PVS-Studio Enterprise license. You can request the trial Enterprise license here. IDE incremental analysis on developer's machine is available under all PVS-Studio license types.
It is possible to run analysis on the entire code base independently – say, once a day during night builds. However, to get the most out of the analyzer, you need to be able to find and fix bugs as early as possible. In other words, the optimal way to use a static analyzer is to run it on freshly written code right away. Of course, having to manually run a check every time you modify a few files and wait for it to finish makes this scenario complicated and incompatible with the idea of intense development and debugging of new code. It's simply inconvenient, after all. However, PVS-Studio has a solution to this problem.
Note that it is advisable to examine all the diagnostic messages generated after the very first full analysis of the code base, and fix any bugs found. As for the remaining warnings, you can either mark them as false positives, turn off irrelevant diagnostics or diagnostic sets, or suppress whatever messages you haven't addressed to get back to them some other time. This approach allows you to keep the warning list uncluttered by meaningless and irrelevant warnings.
Watch, don't read (YouTube)
Windows: C, C++, C#
Microsoft Visual Studio
To enable the post-build incremental analysis mode, click Extensions > PVS-Studio > Analysis after Build (Modified Files Only):
This option is enabled by default.
Once this mode is activated, PVS-Studio will automatically analyze all recently modified files in the background immediately after the build is finished. When the analysis starts, an animated PVS-Studio icon will appear in the Windows taskbar notification area:
The drop-down menu from the notification area includes commands that allow you to pause or abort the current check.
To keep track of modified files, the analyzer relies on the build system. A complete rebuild will cause it to check all the files comprising the project, so you need to use incremental build to be able to check only modified files. If any bugs are detected during incremental analysis, their number will be displayed on the tab of the PVS-Studio window in Visual Studio, and Windows notification will pop-up:
Clicking on the icon in the notification area (or on the notification itself) will take you to the PVS-Studio Output window.
When working within Visual Studio, you can set an incremental analysis timeout or the maximum level of analyzer warnings. These settings can be tweaked in PVS-Studio > Options > Specific Analyzer Settings > IncrementalAnalysisTimeout and PVS-Studio > Options > Special Analyzer Settings > IncrementalResultsDisplayDepth.
Command-line analyzer for MSBuild projects (PVS-Studio_Cmd.exe)
The incremental analysis mode can also be used with Visual Studio solutions when using the command-line utility (PVS-Studio_Cmd.exe). This practice is good for speeding up analysis on the CI server and employs incremental build approaches similar to those used in MSBuild.
To set up incremental analysis on the server, use the following commands:
PVS-Studio_Cmd.exe ... --incremental Scan ...
MSBuild.exe ... -t:Build ...
PVS-Studio_Cmd.exe ... --incremental Analyze ...
Here's a complete description of all the modes of incremental analysis:
- Scan – scan all dependencies to determine which files need to be analyzed. No actual check will be run. This step must be performed before building a solution or project. The scan results will be saved to temporary.pvs-studio directories, located in the same directories where the project files are stored. Only changes made since the last build will be taken into account; all earlier change history stored in the .pvs-studio directory will be erased.
- AppendScan – scan all dependencies to determine which files need to be analyzed. No actual check will be run. This step must be performed before building a solution or project. The scan results will be saved to temporary.pvs-studio directories located in the same directories where the project files are stored. All changes made since the last build and all earlier changes will be taken into account.
- Analyze – run incremental analysis. This step must follow the Scan or AppendScan step and can be performed either before or after building a solution or project. Only the files on the list generated at the Scan or AppendScan step will be analyzed. If the 'Remove Intermediate Files' option of PVS-Studio's settings is set to True, the temporary .pvs-studio directories created at the scan step will be removed at the end of the check.
- ScanAndAnalyze – scan all dependencies to determine which files need to be analyzed and run incremental analysis on the modified source files immediately. This step must be performed before building a solution or project. Only changes made since the last build will be taken into account.
Compiler Monitoring UI
If you need to use incremental analysis along with the compiler monitoring system, you simply need to "trace" the incremental build, i.e. the compilation of files modified since the previous build. This way, you will be able to analyze only modified or new code.
This scenario is natural to the compiler monitoring system, as it is based on the tracing of compiler invocations during the build process, and, thus, collects all the information needed to analyze the source files the compilation of which has been traced. Therefore, which type of analysis will be performed depends on which type of build is being traced: full or incremental.
To learn more about the compiler monitoring system, see the article "Compiler monitoring system in PVS-Studio".
Linux/macOS: C, C++
CMake projects
To check a CMake project, you can use a JSON Compilation Database file. To have the required compile_commands.json file generated, add the following flag to the CMake call:
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=On <src-tree-root>
To enable incremental analysis for such projects, add the ‑‑incremental flag to the analyze command:
pvs-studio-analyzer analyze ... --incremental ...
File dependencies and modification history will be stored in the .PVS-Studio directory as the analyzer's work does not depend on the build system in this mode. This directory must be preserved for the analyzer to be able to work in this analysis mode.
If your CMake generator doesn't allow generating a compile_commands.json file, or if this file can't be generated conveniently, you may directly integrate PVS-Studio into CMake: using the direct integration module will allow you to run incremental analysis along with incremental build.
Makefile projects
You can specify an analyzer invocation command after the compiler command in the scripts of the Make build system or other similar systems:
$(CXX) $(CFLAGS) $< ...
pvs-studio --source-file $< ...
This will let incremental analysis and incremental build run together, with the information about modified files retrieved from the build system.
A collection of examples demonstrating the integration of PVS-Studio into Makefile can be found in the GitHub repository: pvs-studio-makefile-examples.
Other projects (Linux only)
You can check any project without integrating the analyzer into a build system by running the following commands:
pvs-studio-analyzer trace – make
pvs-studio-analyzer analyze ...
Any build command with all the necessary parameters can be substituted instead of make.
In this mode, the analyzer traces and logs child processes of the build system and spots compilation processes among them. If you build the project in incremental build mode, only modified files will be analyzed as well.
Linux/macOS: C#
The incremental analysis mode of C# projects under Linux and macOS is the same as the one described above in the section "Command line analyzer for MSBuild projects (PVS-Studio_Cmd.exe)" except for the following:
- 'pvs-studio-dotnet' is used instead of 'PVS-Studio_Cmd.exe';
- 'dotnet' is used for the build, not 'MSBuild.exe'.
Windows/Linux/macOS: Java
IntelliJ IDEA
To turn on the post-build incremental analysis mode in the plugin for IntelliJ IDEA, click Analyze > PVS-Studio > Settings > PVS-Studio > Misc > Run incremental analysis on every build:
Once this mode is activated, PVS-Studio will automatically analyze all recently modified files in the background immediately after the build is finished. All issued warnings will be collected in the PVS-Studio window:
Maven plugin
To enable incremental analysis in the maven plugin, set the incremental flag:
<plugin>
<groupId>com.pvsstudio</groupId>
<artifactId>pvsstudio-maven-plugin</artifactId>
....
<configuration>
<analyzer>
....
<incremental>true</incremental>
....
</analyzer>
</configuration>
</plugin>
Once this mode is activated, the pvsstudio:pvsAnalyze command will start the analysis of only those files that have been modified since the last check.
Gradle plugin
To enable incremental analysis in the gradle plugin, set the incremental flag:
apply plugin: com.pvsstudio.PvsStudioGradlePlugin
pvsstudio {
....
incremental = true
....
}
Once this mode is activated, the pvsAnalyze command will start the analysis of only those files that have been modified since the last check.