Using the PVS-Studio extension for Visual Studio Code
- Installing the PVS-Studio extension
- Handling analysis results
- Plugin settings
You can view PVS-Studio reports in Visual Studio Code with the help of a special PVS-Studio extension. This documentation explains how to install the extension, upload the PVS-Studio report, and handle it.
Note. You cannot run the analysis with the help of PVS-Studio directly from Visual Studio Code. To analyze the code and get the analyzer report you can use:
- console utilities PVS-Studio_Cmd.exe (C++, C# Windows), pvs-studio-dotnet (C# Linux/macOS), CompilerCommandsAnalyzer.exe (C++ Windows) / pvs-studio-analyzer (C++ Linux/macOS);
- JSON Compilation Database for C++ projects;
- PVS-Studio plugins for IDE: Visual Studio, JetBrains Rider and CLion, IntelliJ IDEA;
- compiler monitoring system;
- console utility pvs-studio.jar for Java projects.
Installing the PVS-Studio extension
To install the PVS-Studio extension, open Visual Studio Code and click the 'Extensions' tab. Then, type 'PVS-Studio' in the search box and click 'Install':
When installing the analyzer on Windows, you can select 'Integration with Visual Studio Code' and then the extension will be added to Visual Studio Code automatically.
You can also install the extension on Windows with the help of the pvs-studio-vscode-*.vsix file located in the PVS-Studio directory (%PROGRAMFILES(x86)%\PVS-Studio by default).
To install the plugin from the .vsix file, click the three dots in the upper right corner of the 'Extensions' tab. Then, select 'Install from VSIX...':
Select the .vsix file of the PVS-Studio plugin in the appeared window. After installing the plugin, restart Visual Studio Code.
You can also install the extension or download the .vsix file from the PVS-Studio page in the Visual Studio Marketplace.
Handling analysis results
Converting a PVS-Studio report to the JSON format
Note: the PVS-Studio extension for Visual Studio Code supports reports in JSON format only. Depending on the type of checked project and the method of running the analysis, PVS-Studio can generate a report in several formats. To display the report in the extension, you need to convert it to JSON format.
To convert it, you can use command-line utilities: PlogConverter.exe for Windows and plog-converter for Linux and macOS. These utilities allow not only to convert the PVS-Studio report to different formats but also to additionally process it. For example, to filter warnings. You can read more about this here.
Here's an example of a command used to convert the PVS-Studio report to JSON format with the help of PlogConverter.exe (Windows):
PlogConverter.exe path\to\report.plog -t json ^ -n PVS-Studio
Here's an example of a command used to convert the PVS-Studio report to JSON format with the help of plog-converter (Linux and macOS):
plog-converter path/to/report/file.json -t json \ -o PVS-Studio.json
Viewing a converted PVS-Studio report in VS Code
To view the report in Visual Studio Code, click 'Open report' on the PVS-Studio panel and select the required file. You can also open the report using the keyboard shortcut 'Ctrl+Shift+]', or press 'Ctrl+Shift+P' and type 'PVS-Studio: Show Report'.
After that, the warnings from the report will be displayed on the panel:
For convenient work with the table, attach it to the panel. To do this, right-click the panel header and select "Keep 'PVS-Studio'".
To handle a report containing relative paths, you need to use the 'Source Tree Root' setting. To do this, open the Settings panel, choose the 'Other tab', click 'Browse', and select the directory relative to which all paths in the report file will be expanded.
PVS-Studio output window filtering mechanisms allow to quickly find and display diagnostic messages. They can be displayed separately and in groups. The toolbar of the window contains a number of buttons that allow to enable or disable the display of warnings from their corresponding groups.
All buttons can be divided into 3 groups: filters by the warnings' levels of certainty, message filters by diagnostic groups, filters by keywords. You can filter warnings by the message code, by the message text, and by the file containing the analyzer message.
You can find a detailed description of the levels of certainty and groups of diagnostic rules in the following documentation section: "Getting acquainted with the PVS-Studio static code analyzer on Windows".
To open the filtering panel by groups, click 'Show filters' on the window toolbar.
You can combine all of these warning filtering mechanisms with each other. For example, you can filter messages by the level and groups of displayed warnings, or exclude messages marked as false alarms, etc.
Navigation and sorting
The PVS-Studio output window is primarily designed for easier code navigation. It also simplifies jumping to code fragments containing potential errors. Double-clicking on any of the warnings in the list will automatically open the file for which the analyzer issued a message.
To mark interesting warnings, for example, those that you want to view later, you can use the "asterisk" in the corresponding column.
Right-clicking on the table header opens a context menu where you can customize the displayed columns.
The table supports multiple selection with the help of standard 'Ctrl' and 'Shift' combinations. The context menu of the warning table contains several items:
- Mark as Favorite — marks selected messages as favorites;
- Mark as False Alarm — marks selected messages as false alarms and adds a special comment to the source code;
- Copy message — copies the diagnostic number, the warning message, and the file for which the warning is issued to the clipboard;
- Exclude diagnostic — excludes the display of all warnings with the same diagnostic number as the highlighted one;
- Exclude paths — allows to exclude the path to the selected file or a part of this path. All warnings issued for files containing the selected path will not be shown.
The items on the right side of the panel are designed, respectively, to save the report, open a new report, open the extension settings. If a report contains some changes, the save report item will become red, as shown in the screenshot below.
To open the settings window, click on the rightmost item of the panel, or use 'Ctrl+Shift+P' and enter 'PVS-Studio: Show Settings'.
The 'Save settings' item saves the settings in JSON format. To load the saved settings, use the 'Load settings' item.
In the False Alarms tab, you can choose whether to:
- show False alarms or not;
- save the source files automatically when the warning is marked as False Alarm;
- add an additional message to false alarm commentary.
This tab allows you to mark columns that should be displayed in the table of warnings.
The Diagnostics tab allows to mark the warnings that should be displayed in the table of warnings. All warnings are divided into groups. To search for a specific diagnostic rule, you can use the 'Code and Messages' field. 'Check all/Uncheck all' are designed to enable/disable the display of all diagnostic rules from a certain group. When disabling all diagnostic rules from the corresponding group, the button of this group is removed from the filtering panel.
The Exclude paths tab allows to enter paths or path masks in the 'New excluded path' field. Messages issued for files that meet the mask conditions will not be displayed in the warnings panel.
Documentation language. The setting allows to set the language for the built-in documentation of PVS-Studio diagnostic rules available on our website.
This setting does not change the interface language of the PVS-Studio extension or the diagnostic messages issued by the analyzer.
Source Tree Root. To handle the PVS-Studio report containing relative file paths, you need to replace them with absolute ones. The setting allows to set the directory relative to which all paths in the report file are disclosed.
You can find a detailed description of using relative paths in the PVS-Studio report files here.