Check for 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 the violations of these rules:
JSF AV C++
AUTOSAR C++14 (Bug Finder only)
CERT® C (Bug Finder only)
CERT C++ (Bug Finder only)
ISO®/IEC TS 17961 (Bug Finder only)
Guidelines (Bug Finder only)
Configure Coding Rules Checking
Specify Standard and Predefined Checker Subsets
Specify the coding rules through Polyspace analysis options. When you run Bug Finder or Code Prover, 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 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 Create Custom Coding Rules.
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 Findings 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
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
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,
the root installation folder for the Polyspace Server products, for instance,
For instance, to turn off MISRA C:2012 rule 8.1, use this entry in a copy of the file
<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
Cyclomatic complexity exceeds threshold and set the
threshold for the checker to five, use this entry in a copy of the
<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:
The XML format of the checker configuration file can change in future releases.
Check for Coding Standards Only
To check for coding standards only:
In Bug Finder, disable checking of defects. Use the option
Find defects (-checkers).
In Code Prover, check for source compliance only. Use the option
Verification level (-to)(Polyspace Code Prover).
These rules are checked in the later stages of a Code Prover analysis: MISRA C:2004 rules 9.1, 13.7, and 21.1, and MISRA C:2012 rules 2.2, 9.1, 14.3, and 18.1. If you stop Code Prover at source compliance checking, the analysis might not find all violations of these rules. You can also see a difference in results based on your choice for the option
Verification level (-to). For example, it is possible that Code Prover suspects in the first pass that a variable may be uninitialized but proves in the second pass that the variable is initialized. In that case, you see a violation of MISRA C:2012 Rule 9.1 in the first pass but not in the second pass.
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.
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).