Main Content

Run Analysis and Review Results in Visual Studio Code

After you configure the Polyspace® as You Code™ extension, you are ready to run an analysis and fix or justify any results. Before continuing this tutorial, verify that you have added example.hpp and example_source.cpp to the Quality Monitoring list.

Run Polyspace as You Code Analysis in Visual Studio Code

Run an analysis on example_source.cpp by using one of these methods.

  • If you set Analysis Of Files On Save, save your edited file.

  • Right-click the file and select Run Polyspace Analysis.

  • In the Quality Monitoring list, click the Run Polyspace Analysis button next to your file.

View, Fix, or Justify Results

After Polyspace runs the analysis, the filename in the Quality Monitoring list appears green if there are no results and red if there are results. A number to the right of the filename indicates the number of results. View your results in the Problems pane. Open the Problems pane by clicking the filename in the Quality Monitoring list.

The Problems pane shows all results in a list. Use the filter in the Problems pane to search for specific results. Each result contains a line and column number that indicate the location in the code where Polyspace found the defect or coding rule violation. Click on a result to view more information about the result in the Result Details pane. Click the expander for the result in the Result Details pane to show potential fixes or justifications to apply to the result.

After Polyspace as You Code completes its analysis on example_source.cpp, open the results in the Problems pane. Use the filter to search for 6.5.1. Polyspace finds a violation of MISR C++:2023 Rule 6.5.1 on line 13.

Click the result in the Problems pane to open the file where the rule violation occurs. Each result is noted in the file with a red underline. Hover over the underlined sections of code to see a list of each result associated with the underlined section.

In example.h, add this line of code on line 9 after the class definition:

int getDenominator(bool triggerError);
Save the file to run another analysis. The violation of MISRA C++:2003 Rule 6.5.1 in example_source.cpp is no longer present.

With automatic analysis active, you can find and justify or fix results during the code authoring process. Each time you save changes, a Polyspace analysis begins in the background and displays any new results or removes fixed results from the list.

Justify Individual Results

You can justify a result from the Problems pane or from the location of the violation in the code.

To add a justification to a result, hover over a result in the Problems pane and click the light bulb icon. A menu of justification options appears. you can justify an individual instance of a result or all instances of the result in the same file. Select the appropriate justification for your scenario from the menu. Selecting a justification adds an amendable comment to your code at the location where the code violation occurs.

Adding a justification removes the result from the Problems pane. To show the result again, remove the justification comment from the file and perform a new Polyspace analysis.

For this tutorial, after running an analysis:

  1. Locate the result for the defect integer division by zero on line 6 in the Problems pane.

  2. Hover over the result and select the light bulb icon.

  3. Select the option Justify defect:INT_ZERO_DIV with annotation.

Use Predefined Justifications

You can add a catalog of predefined justifications to Polyspace as You Code. If you use a catalog of predefined justifications, you can select a comment from the catalog to append to the justification instead of manually typing a justification comment.

Open the Visual Studio Code Settings and search for polyspace.justification.catalog. Enter the path to your justification catalog in the text box relative to the install location. The catalog must be in JSON format. See Create and Edit Justification Catalog.

In this tutorial, use the example JSON file example_catalog.json to create a justification catalog:

{
	"justifications": [
		{
		"family": "Defect",
		"acronym": "INT_ZERO_DIV",
		"comment": "This is my justification for division"
		},
		{
		"family": "Defect",
		"acronym": "INT_ZERO_DIV",
		"comment": "Alternative justification for division"
		}
	]
}

Provide the location of the catalog in the Visual Studio Code settings.

After running an analysis:

  1. Locate the result for the defect integer division by zero on line 6 in the Problems pane.

  2. Hover over the result and select the light bulb icon.

  3. Select the option Justify defect:INT_ZERO_DIV with annotation.

  4. Select either justification comment.

View Header Results

When you analyze source files, Polyspace as You Code shows any header file results in the Problems pane. If a header contains a result, it is noted with a red H next to the source file in the Quality Monitoring list.

Click a header result in the Problems pane to go to the header file that contains the result. To find which source file analysis caused the header result, click the expander in the Result Details pane for the result.

After you run an analysis on the example code, open the results in the Problems pane. Use the filter to search for 19.2.1. There is a violation of MISRA C++:2023 Rule 19.2.1 in the file example.h.

Replace the example header file with this code:

#ifndef CALCULATOR_H
#define CALCULATOR_H

class Calculator {
public:
    int divide(int numerator, int denominator);

    Calculator() = default;
};

#endif // CALCULATOR_H

Save the file and run another analysis. The violation of MISRA C++:2023 Rule 19.2.1 is no longer in the results list.

Compare Results Against a Baseline

For more efficient bug fixing, compare your Polyspace as You Code single-file analysis results against a set of baseline results so you can focus on new or unreviewed results only. You can create a local baseline or download a Polyspace Bug Finder™ analysis from Polyspace Access™ to use as a baseline.

For more information on creating or downloading a baseline in Visual Studio Code, see Visual Studio Code: Configure Baseline Results in Polyspace as You Code.

See Also

Topics