Main Content

Run Polyspace as You Code in Visual Studio Code and Review Results

You can choose to run Polyspace as You Code on each save in the Visual Studio Code editor, or at will. The analysis runs on the file that is currently active in the editor. After you run the analysis, you see bugs and coding standard rule violations as source code markers and as a list in the Problems panel.

Check Installation and Start Extension

To confirm that the Polyspace® as You Code extension is installed in Visual Studio Code, check the INSTALLED section in the Extensions view.

To open this view, in Visual Studio Code, select View > Extensions or click Visual Studio Code extensions icon in the activity bar on the left and look for Polyspace as You Code in the list of installed extensions.

To start the Polyspace as You Code extension, open a C or C++ file in the editor. The extension does not start automatically when you open Visual Studio Code when no C or C++ file is open in the editor. To check the status of the extension, do one of the following:

Look for Polyspace as You Code in the list of extensions installed.

Note

  • If you use Visual Studio Code version 1.57.1 or later, the Polyspace as You Code extension is disabled when you open a folder in Restricted Mode. To enable the extension, mark the folder as trusted. See Workspace Trust.

  • For better performance on Windows® systems, the files that you analyze should not be stored on network drives.

View Extension Information in Status Bar

Once you start the extension, Polyspace as You Code displays icons in the status bar (lower left) to provide information about the state of the extension, such as errors in the configuration or whether an analysis is running on the currently selected file.

  • Place your cursor over an icon to view a tooltip containing additional information.

  • Click an icon (when applicable) to perform common operations, such as opening the extension settings or viewing the Output view.

The information that you see in the status bar is either global (VS Code global icon) or specific to the currently selected file (VS Code file icon). The file specific status bar icons update when you select a different file.

To disable or reenable the Polyspace as You Code status bar icons, right-click the icons and select the appropriate option from the menu.

Open Additional Polyspace Views

The Polyspace as You Code extension provides different views in the side bar that show information about the state of your configuration and of the files that you select for analysis. Click Grey Polyspace icon in the activity bar on the left to show or hide these Polyspace views.

QUALITY MONITORING View

This view shows all the files that you have selected for analysis, the status of the analysis for each file, and the number of findings.

In this view, you can also:

  • Start a file-by-file analysis of all the files that you added to the view.

  • Start an analysis for only a specific file.

  • Create a local baseline for a specific file.

Use the Analysis Options: Add To Quality Monitoring On Save setting to enable or disable adding files to this view on save (Ctrl + S). See Configure Analysis Behavior On Save. To add a file manually, right-click the file in the editor, the EXPLORER view, or SOURCE CONTROL view in the side bar.

HEADERS view

When you analyze a file, Polyspace also analyzes header files that are included by the analyzed file, if those headers are in the same folder as the analyzed file. If Polyspace reports findings in a header file, you see an H next to the analyzed file in the Quality Monitoring view. Click the analyzed file in that view to list the header files in HEADERS view.

Polyspace as You Code Headers view in VSCode

Header files are listed only if they contain findings. Click a header file in the HEADERS view to open its finding in the PROBLEMS panel and start inspecting those findings in the Editor.

Result Details View

When you select a finding in the Output view or in the editor, this view shows additional information about the finding, including, when available, the traceback and the review information imported from the baseline.

If you select multiple lines of code, the view lists additional details for all the findings reported in the selected code.

Click open documentation icon to open the contextual help for a finding.

Whenever you select a finding, the Result Details view updates to show information about the selected finding. To lock the view and keep it from updating when you browse through findings in the source code or the PROBLEMS panel, click the Unlocked icon icon in the upper right. To unlock the view, click locked icon.

BASELINE View

View current filter setting (show new results only or show all results), any available local baseline, and the status of the download for the integration (Access) baseline download. If you have configured the baseline settings, you see information about the Polyspace Access server and the project you use as an integration baseline. See Set Baseline Polyspace as You Code Results in Visual Studio Code.

VSCode baseline view

In this view, you can:

  • Click the filter button to enable or disable filtering for only new results compared to a baseline.

  • Click the save all button to create a local baseline for all files currently in the quality monitoring list or click the refresh button to update all local baselines.

  • Click Download baseline to download an integration baseline.

  • Click Open basline in Access UI to open the integration baseline project in the Polyspace Access™ interface

  • Click settings icon to open the baseline settings.

To disable the use of a baseline, click baseline close button next to the name of the baseline for local baselines, or next to Access baseline for an integration baseline.

If the results from the baseline run are generated with a Polyspace product that is older than your version of Polyspace as You Code, this view shows a warning.

CONFIGURATION View

This view shows your current Analysis Options settings and the status of the generated build options file, if applicable. For instance, you can see fast analysis mode

In this view, you can:

Run Analysis

On Save

By default, Polyspace as You Code adds the current file to the Quality Monitoring list and analyzes this file on save. Analysis results appear within a few seconds on the source code. In case of an error, you see a popup with the error message. To diagnose further, select View > Output. On the Output view, from the dropdown on the upper right, select Polyspace as You Code.

After analysis, results appear as source code markers (wavy lines below source code tokens). You also see the error locations as red marks on the scroll bar. Click an error location to navigate to the corresponding source code.

To disable analysis on save:

  1. Open the settings by pressing Ctrl + , (comma) and type polyspace.analysisOptions OnSave in the settings search bar.

  2. Deselect the setting for Analysis Of Files On Save.

If results do not appear, see Troubleshoot Failed Analysis or Unexpected Results in Polyspace as You Code.

On Demand

You can also explicitly start a Polyspace analysis. To start an analysis, do one of the following:

  • Right click a source file in the EXPLORER view or right-click on the file content itself in the editor and select Run Polyspace Analysis.

  • Click the play button in the QUALITY MONITORING view.

  • With your cursor in the source file, press Ctrl + Shift + Alt + A.

Review Results

After analysis, the results appear in two forms:

  • As source code markers (with a wavy line below source code tokens).

    You can hover on a source code token to see more details about a result.

  • In a list on the PROBLEMS panel.

    To open the panel, select View > Problems.

    When you select a finding you can view additional details about the result or an event trace in the Result Details view.

    Click open documentation icon in this view to open the contextual help for this finding.

Problems panel and result details view

Apply Suggested Fix for Common Defects or Coding Rule Violations

If Polyspace reports certain common defects or coding rule violations in your code, the Polyspace as You Code extension proposes one or more fixes that you can apply to these findings.

To apply a quick fix, select a finding in the editor or the Problems panel, and then, from the light bulb menu, click the appropriate option. The quick fix options have the format Fix checkerName: proposedFix. Here, checkerName is the name of the selected defect checker or coding rule, and proposedFix is the fix that Polyspace applies to your code. Polyspace edits your code and fixes the issue or all instances of the issue in the currently active file, depending on the option that you select. If there is more than one proposed fix, you see all available fixes in the menu.

VSCode light bulb menu

This table lists the common defects and coding rule violations for which Polyspace proposes a fix, along with the Polyspace checkers that report these issues.

Common Defect or Coding Rule ViolationPolyspace Checkers That Report the Issue
Function that could be evaluated a compile time missing constexpr specifier
Inefficient use of std::any_cast to cast object by value
Inefficient use of std::string operator+()
Inefficient use of the [] operator to insert or assign a value to a key in a std::map or std::unordered_map (C++ 17 and later only)
Parameter passed by value results in expensive copy
Unmodified expensive-to-copy parameter passed by value
Unmodified local variable is created by expensive copy from a const reference
Unmodified variable not declared const
Unnecessary call to std::move in a return statement
Unused include
Unused parameter
Use of std::endl

Justify Results Using Code Annotations

If you decide not to fix a result, you can add code annotations to the result to avoid having to fix the result again. If the annotations follow a specific syntax, subsequent Polyspace as You Code runs can read these annotations and suppress the corresponding results.

  • To add a code annotation, click the light bulb icon beside the source code token containing a result and select Justify result_name with annotation. The annotation is entered on the same line as the result.

  • To mass-justify findings of the same defect or coding rule violations in the current file, select an instance of that finding and click the Justify all result_name option from the light bulb menu. Polyspace inserts an annotation in your code on each line that contains this finding.

  • If you import review information from a baseline run, select Annotate result_name with baseline comment to include any status, severity, or comment assigned to that result in the baseline run. If the imported status is set to To investigate, To fix, or Other, the annotation does not suppress the result.

If you select multiple lines of code, the light bulb menu show annotation options for each finding in your selection.

Polyspace as You Code inserts code annotations as comments enclosed in "/* */" for C files and prefixed with "//" in C++ files (C++ style). The comment format in C files complies with MISRA C:2004 rule 2.2.

Justified results are removed from the PROBLEMS panel and are suppressed in subsequent analyses.

Click light bulb icon to justify result with annotation

See also:

View Context-Sensitive Help for Result

You can see more information on a type of result by visiting the context-sensitive help page for the result.

  • To open the context-sensitive help for the currently selected finding, click open documentation icon in the Result Details view. see Open Additional Polyspace Views.

  • You can also view the context-sensitive help for any Polyspace as You Code result by going to the Command Palette (Ctrl + Shift + P) and selecting Polyspace: Open Contextual Help. Choose a result family from the menu and then enter a result name, for instance UNMODIFIED_VAR_NOT_CONST.

To open the full searchable documentation for the Polyspace as You Code extension in your system browser, go to the Command Palette (Ctrl + Shift + P) and select Polyspace: Open Documentation.

Configure Checkers and Other Settings

By default, Polyspace as You Code checks for defects that are likely to be of most interest to developers. You can expand the set of checkers and perform other configuration through the Polyspace as You Code extension settings in Visual Studio Code.

Open the settings by pressing Ctrl + , (comma) and type polyspace in the settings search bar.

For instance, you might want to:

For the full list of settings, see Configure Polyspace as You Code Extension in Visual Studio Code.