Configure Polyspace as You Code in Visual Studio
Polyspace® as You Code™ helps you find defects and coding standard violations while you write code in the Visual Studio® IDE. You can run an analysis and fix results as you code, saving you from finding bugs late in the development cycle. When connected to the Polyspace Access™ central repository, Polyspace as You Code highlights new issues compared to the development baseline.
This tutorial assumes you have a working knowledge of Visual Studio.
Before you begin the tutorial, confirm Polyspace as You Code is installed on your machine. See Visual Studio: Install Polyspace as You Code Extension.
Use your own code or copy and paste this code into Visual Studio 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 plugin. Configuration of the plugin sets your analysis preferences and allows for some customization of your workspace. Settings are preserved between sessions.
Open your files in Visual Studio. Open the Polyspace as You Code perspective by going to Extensions > Polyspace > Open Polyspace Windows. The perspective contains different 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 build command, a Visual Studio C/C++ project, or a JSON Compilation Database file or specify them in a Polyspace build options file.
Click the configure options icon
in the Configuration pane or go to Tools > Options > Polyspace as You Code > Analysis Options. Choose how to provide your options file using the Build setting under Manual setup. To provide the path to your options file, use the relevant option of the same file type. For example, if your options file is a JSON Compilation Database file, select Get from JSON Compilation Database file and enter the path to the file.
Some options require you to generate your build options after specifying your options file. Click the
icon in the Configuration view to generate your build options.
For this tutorial, leave Build set as Build options file not required.
For more information on build options, see Configure Analysis Settings.
Set Automatically 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 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 options icon
in the Configuration pane and select the check box for Add to Quality Monitoring list on save.
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 Analyze all files in the list sequentially button
in the Quality Monitoring list, or right-click inside the file editor and select Polyspace > Run Polyspace Analysis.
Turn on automatic analysis of files. Click the configure options icon
in the Configuration pane and select the check box for Start analysis on save.
Configure Polyspace Checkers
Polyspace as You Code checks for a default set of defects and coding 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, click the configure options icon
in the Configuration pane. If you already have a checkers file, you can provide the full path to the file in the text box for the Checkers File setting.
If you need to create a checkers file, in the Configuration pane, click the Configure Checkers icon
to open the Checkers Selection window. Clicking
when you have no file path in the 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: 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 by adding them to the Quality Monitoring list and running an analysis.
Manually add the example.hpp header file to the Quality Monitoring list and initiate an analysis by clicking the
icon.
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.