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 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:
Open the Problems panel and select
Polyspace as You Code
from the dropdown on the right.Place your cursor over the icons in the status bar on the lower left. See View Extension Information in Status Bar.
Open the Polyspace views. See Open Additional Polyspace Views.
Look for Polyspace as You Code in the list of extensions installed.
Note
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 () or specific to the currently selected file (). 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 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 or you can choose to analyze only 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.
Header files are listed only if they contain findings. Click a header file in the Headers view to open its finding in the Problems view and start inspecting those findings in the Editor.
Result Details View
When you select a finding in the Problems view or in the editor, this view shows additional information about the finding, including, when available, any fixes or justifications, 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 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 view, click the icon in the upper right. To unlock the view, click .
Problems View
The Problems view shows the all the analysis findings, listed one finding per line. Each line shows additional information, such as:
The name of the defect or coding rule, family, and description
Name of the file containing the finding
Review information
The findings are grouped by file. Use the filters in the right side corner of the Problems view to narrow your review focus. You can change the Problems view to show results in a tree format or a table format.
The results that show in the Problems view represent the results for the files that you select in the Quality Monitoring view. The number of results in the Problems view can change and does not represent results for all analyzed files.
When you select a file in the Quality Monitoring view, in the Problems view you see results from:
Each file listed in the Quality Monitoring view including the selected file.
The header files of the file you select in the Quality Monitoring view.
If you select a different file in the Quality Monitoring view:
Polyspace removes the previously selected file's header file results from the Problems view.
The newly selected file's header file results show in the Problems view.
Baseline View
View the current mode for the baseline (whether you are using the baseline or not) and, if you use the baseline, the status of the baseline download. If you have configured the baseline settings, you see information about the Polyspace Access server and the project you use as a baseline. See Set Baseline Polyspace as You Code Results in Visual Studio Code.
In this view, you can:
Click to download a baseline.
Click to open the baseline project in the Polyspace Access™ interface
Click to open the baseline settings.
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:
Click to open the Analysis Options settings. See Configure Analysis Settings.
Click to open the Checkers selection interface. See Configure Checkers for Polyspace as You Code in Visual Studio Code.
Toggle on or off fast analysis mode by clicking the icon.
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:
Open the settings by pressing Ctrl + , (comma) and type
polyspace.analysisOptions OnSave
in the settings search bar.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 in this view to open the contextual help for this finding.
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 suggested fix, first select the finding in the editor or in the Problems panel. Suggested fixes appear in the Result Details panel. Point to a suggested fix and select one of these options:
Show fix in diff view — Show a comparison between the current file and the file with the suggested fix.
Fix the current result — Apply the suggested fix to the selected finding.
Fix all instances of the selected finding — Apply the suggested fix to all instances of the same defect or coding rule violation in the current file.
You can also apply a suggested fix from the light bulb menu. 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.
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 Violation | Polyspace 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 apply a justification, first select a result in the editor or the Problems panel. Justifications appear in the Result Details panel. Point to a justification and select one of these options:
Justify the current result — Append a justification annotation to the selected result.
Justify all instances of the selected finding — Append a justification annotation to all instances of the same defect or coding rule violation in the current file.
Copy to clipboard — Copy the family and acronym of the result to your clipboard. You can use this information to create a justification catalog. See Use a Justification Catalog to Autocomplete Annotations in Polyspace as You Code plugins.
You can also apply a justification from the light bulb menu.
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 toTo investigate
,To fix
, orOther
, 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.
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 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:
Enable or disable certain checkers.
See Configure Checkers for Polyspace as You Code in Visual Studio Code.
See only new results.
See Set Baseline Polyspace as You Code Results in Visual Studio Code.
For the full list of settings, see Configure Polyspace as You Code Extension in Visual Studio Code.