Configure Polyspace as You Code in Visual Studio Code
Polyspace® as You Code™ is an extension that you can use with Visual Studio Code to run a Polyspace analysis directly in your IDE. Use Polyspace as You Code to find defects and coding standard violations at an earlier stage in development. This can save you time and help avoid compounding issues later in the development cycle.
This tutorial assumes you have a working knowledge of Visual Studio Code.
Before beginning the tutorial, confirm Polyspace as You Code is installed on your machine. See Visual Studio Code: Install Polyspace as You Code Extension.
Use your own code or copy and paste this code into Visual Studio Code to follow along with the tutorial.
Save this code as the source file example.h.
#define CALCULATOR_H
class Calculator {
public:
int divide(int numerator, int denominator);
Calculator() = default;
};Save this code as the source file example_source.cpp.
#include <iostream>
#include <vector>
#include "example.h"
int Calculator::divide(int numerator, int denominator) {
return numerator / denominator;
}
int getDenominator(bool triggerError) {
return triggerError ? 0 : 2;
}
int main() {
Calculator calc;
std::vector<int> numerators = {10, 20, 30};
bool causeError = true;
for (int num : numerators) {
int denom = getDenominator(causeError);
std::cout << "Dividing " << num << " by " << denom << "..." << std::endl;
int result = calc.divide(num, denom);
std::cout << "Result: " << result << std::endl;
}
return 0;
}
Before you begin your first analysis, configure the Polyspace as You Code extension. Configuration of the extension sets your analysis preferences and allows for some customization of your workspace. Settings are preserved between sessions.
Open your files in Visual Studio Code. Click the Polyspace button
in the Visual Studio Code sidebar to open the Polyspace sidebar. This sidebar has several panes that are referenced in this tutorial, such as Configuration, Quality Monitoring, and Headers.
Manually Configure Your Build
You have the option of manually configuring your build. Manual setup of the analysis involves specifying build options. You can extract build options from a Visual Studio Code build task or JSON Compilation Database file, or specify them in a build options file.
To set your build options, click the Configure button
in the Configuration pane of the Polyspace sidebar. Alternatively, open the Settings tab and search for polyspace.analysisOptions to view the various build options.
In this tutorial, select the Manual Setup: Build option Build options file not required.
For more information on build options such as using a JSON Compilation Database File or a Polyspace build options file, see Configure Analysis Settings.
Set Automatic Quality Monitoring
Polyspace as You Code has a Quality Monitoring list that tracks changes to files in the list. Add files to this list to continuously monitor the quality of your code changes while actively writing code. To add a file to the list, right-click inside the file editor with the file open and select Add file to the Polyspace Quality Monitoring list.
Add the example_source.cpp file to the Quality Monitoring list.
Alternatively, you can set Polyspace to automatically add a file to the Quality Monitoring list when you save the file.
Turn on automatic quality monitoring. Click the Configure button
in the Quality Monitoring pane of the Polyspace sidebar and select Add To Quality Monitoring On Save. Alternatively, open the Settings tab and search for the option polyspace.analysisOptions.addToQualityMonitoringOnSave. Select the check box to activate the setting.
Set Analysis on Save
You can run a Polyspace analysis on files in the Quality Monitoring list manually, or run an analysis automatically every time you save a file. To manually run an analysis of individual files or a group of files in the Quality Monitoring list, click the Run Polyspace analysis button
next to the file in the Quality Monitoring list or right-click inside the file editor and select Run Polyspace Analysis.
Turn on automatic analysis of files. Click the Configure button
in the Quality Monitoring pane of the Polyspace sidebar and select Analysis Of Files On Save. Alternatively, open the Settings tab and search for the option polyspace.analysisOptions.analysisOfFilesOnSave. Select the check box to activate the setting.
Configure Polyspace Checkers
Polyspace as You Code checks for a default set of defects and standards when you run an analysis. You can customize this set of standards and defects based on which certification standards you want to check for by using the checkers file.
To set your checkers file, open the Settings tab and search for the option polyspace.analysisOptions.checkersFile. If you already have a checkers file, you can provide the full path to the file in the text box.
If you need to create a checkers file, run the Polyspace: Configure Checkers command from the Command Palette or click the Configure Checkers button
in the Configuration section of the Polyspace sidebar to open the Checkers Selection window. Clicking
when you have no file path in the Analysis Options: Checkers File setting opens the Checkers Selection window and allows you to create a new checkers file.
In this tutorial, enable the MISRA™ C++:2023 checkers and all Bug Finder defects. In the Checkers Selection window:
Select MISRA C++:2023 in the left pane, and then select All in the right pane.
Select Defects in the left pane, and then select All in the right pane.
Click Save Changes and select a save location if prompted.
For more information about creating, saving, and opening checkers selection files, see Visual Studio Code: Configure Checkers and Coding Rules in Polyspace as You Code.
Analyze Header Files
You can run an analysis on header files as if they were source files.
First, define the extensions of your header files. Click the Configure button
in the Headers pane of the Polyspace sidebar. Alternatively, open the Settings tab and search for the option polyspace.otherSettings.headersExtensions.
Click Add Item and add the file extension .hpp if it is not already there. Click OK to save the file extension in the list of header file extensions.
Manually add the example.hpp header file to the Quality Monitoring list and run an analysis by clicking the Run Polyspace Analysis button
.
For more information on the scope of an analysis and which header files are included when you run an analysis, see Headers Included in Current File Not Analyzed.