Check for and Review Coding Standard Violations
With Polyspace®, you can check your C/C++ code for violations of coding rules such as MISRA C™:2012 rules. Adhering to coding rules can reduce the number of defects and improve the quality of your code.
Polyspace can detect coding rule violations for these standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA™ C++
JSF AV C++
AUTOSAR C++14
CERT® C
CERT C++
CWE™
ISO®/IEC TS 17961
Guidelines
Configure Coding Rules Checking
Specify Standard and Predefined Checker Subsets
Specify the coding rules through Polyspace analysis options. When you run Bug Finder, the analysis looks for coding rule violations in addition to other checks. You can disable the other checks and look for coding rule violations only.
In the Polyspace user interface (desktop products), the options are on the Configuration pane under the Coding Standards & Code Metrics node.
For C code, use one of these options:
For generated code, enable the option specific to generated code.
For generated code, enable the option specific to generated code.
For C++ code, use one of these options:
You can specify a predefined subset of rules, for instance,
mandatory
for MISRA C:2012. These subsets are typically defined by the standard.
You can also define naming conventions for identifiers using regular expressions. See Enforce Naming Conventions for C/C++ Variables and Functions.
Customize Checker Subsets
Instead of the predefined subsets, you can specify your own subset of rules from a coding standard.
User Interface (Desktop Products Only)
Select the coding standard. From the drop-down list for the subset of rules, select
from-file
. Click Edit.In the Checkers selection window, the coding standard is highlighted on the left pane. On the right pane, select the rules that you want to include in your analysis.
When selecting Guidelines > Software Complexity checkers, review their thresholds. If the default thresholds are not acceptable, specify a suitable threshold in the Threshold column. See
Check guidelines (-guidelines)
.When selecting Custom rules, review the Pattern and Convention for the rules. See
Check custom rules (-custom-rules)
.
When you save the rule selections, the configuration is saved in an XML file that you can reuse for multiple analyses. The same file contains rules selected for all coding standards. You can reuse this file across multiple projects to enforce common coding standards in a team or organization. To reuse this file in another project in the Polyspace user interface:
Choose a coding standard in the project configuration. From the drop-down list for the subset of rules, select
from-file
.Click Edit and browse to the file location. Alternatively, enter the file name as argument for the option
Set checkers by file (-checkers-selection-file)
.
Command Line. With the Polyspace desktop products, you can create a coding standard XML file in
the user interface and then use this file for command-line analysis. Provide
this XML file with the option Set checkers by file
(-checkers-selection-file)
.
With the Polyspace Server products, you have to create a coding standard XML from scratch. Depending on the standard that you want to enable, make a writeable copy of one of the files in
and turn off rules using entries in the XML file (all rules from a standard are enabled in the template). Here, polyspaceserverroot
\polyspace\examples\doc_cxx\coding_standards_XML
is
the root installation folder for the Polyspace Server products, for instance, polyspaceserverroot
C:\Program Files\Polyspace Server\R2024a
.
For instance, to turn off MISRA C:2012 rule 8.1, use this entry in a copy of the file
misra_c_2012_rules.xml
:
<standard name="MISRA C:2012"> ... <section name="8 Declarations and definitions"> ... <check id="8.1" state="off"> </check> ... </section> ... </standard>
When using the Guideline checkers, specify their threshold between the
threshold
tags. For instance, to activates the
checker Cyclomatic complexity exceeds threshold
and set the
threshold for the checker to five, use this entry in a copy of the
guidelines.xml
:
<check id="SC18" state="on"> <threshold>5</threshold> </check>
To use the XML file for a MISRA C:2012 analysis in Bug Finder, enter:
polyspace-bug-finder -sources filename -misra3 from-file -checkers-selection-file misra_c_2012_rules.xml
For full list of rule id-s and section names, see:
Note
The XML format of the checker configuration file can change in future releases.
Check for Coding Standards Only
A Bug Finder analysis checks C/C++ code for:
A default set of defects (bugs)
Adherence to any coding standard that you specify.
To check for coding standards only, disable defect checking
entirely. Specify none
for the option Find defects
(-checkers)
.
Review Coding Rule Violations
After analysis, you see the coding standard violations on the Results List pane. Select a violation to see further details on the Result Details pane and the source code on the Source pane.
Violations of coding standards are indicated in the source code with the icon.
For further steps, see Review Analysis Results or Review Polyspace Bug Finder Results in Web Browser.
Generate Reports
You can generate reports using templates that are explicitly defined for coding
standards. Use the CodingStandards
template. This template:
Reports only coding standard violations in your analysis results, and omits other types of results such as defects, run-time errors or code metrics.
Creates a separate chapter in the report for each coding standard. the chapter provides an overview of all violations of the standard and then lists each violation.
To specify a report template, use the option Bug Finder and Code
Prover report (-report-template)
.
Related Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Filter and Group Results in Polyspace Desktop User Interface
- Generate Reports from Polyspace Results