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:

  • MISRA C: 2004

  • MISRA C: 2012

  • MISRA® C++

  • 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)

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)

  1. Select the coding standard. From the drop-down list for the subset of rules, select from-file. Click Edit.

  2. 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 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.

With the Polyspace server products, you have to create a coding standard XML from scratch. Use the file StandardsConfiguration.xml in polyspaceserverroot\polyspace\examples\cxx\Bug_Finder_Example\sources as a template and turn on rules using entries in the XML file. Here, polyspaceserverroot is the root installation folder for the Polyspace Server products, for instance, C:\Program Files\Polyspace Server\R2019a.

For instance, to turn on MISRA C: 2012 rule 8.1, use this entry:

<standard name="MISRA C:2012">
  ...
  <section name="8 Declarations and definitions">
      ...
      <check id="8.1" state="on">
      </check>
      ...
  </section>
  ...
</standard>

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

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).

    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.

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