Main Content

Find Polyspace Bug Finder Checkers That Map to Coding Rules in User-Defined Coding Standard

User-defined coding standards allow you to collect coding rules that are relevant specifically to your project into a single standard when an existing coding standard does not exactly meet your needs. This topic shows some possible strategies that you can use to find which existing coding rule or Polyspace® Bug Finder™ defect can check for a coding rule of interest. For coding rules with narrow scope, review the documentation of Polyspace Bug Finder checkers to find an appropriate mapping for your coding rule. For broader coding rules, specify your scope of your coding rule by gathering noncompliant code in a specification file and then find appropriate mappings to Bug Finder checkers by running Polyspace on the specification file.

Once you have identified a set of Bug Finder checkers that map to the coding rules in the user-defined coding standard, create and share the .pschk file that represent the user-defined coding standard. See Create User-Defined Coding Standard by Using Polyspace Bug Finder Checkers.

Review Documentation and Other Resources

To find Bug Finder checkers that you can map to the coding rules in your user-defined coding standard, review the documentation of existing coding rule and defect checkers. The documentation explains the issues that these checkers can identify. By using the examples in the documentation, determine if a checker is appropriate for the coding rule that you want to implement.

For example, the Rule of Five in C++ states, Any class for which move semantics are desirable, has to declare all five special member functions. If you search the documentation for the phrase "rule of five", you find that the checker AUTOSAR C++14 Rule A12-0-1 checks for compliance with the Rule of Five. You can make the Rule of Five a coding rule in your user-defined coding standard by using AUTOSAR C++14 Rule A12-0-1.

For a list of all supported checkers, see Complete List of Polyspace Bug Finder Results.

The supported checkers are also listed in the file polyspaceroot\polyspace\resources\Polyspace R2024b Results List.xlsx. Here, polyspaceroot is the Polyspace installation folder. The default installation folder is C:\Program Files\Polyspace\R2024b.

Find Mappings by Running Polyspace Bug Finder on Specification File

If you cannot find a good mapping for your coding rule using documentation and other resources, create a specification file that specifies when a coding rule is violated. The specification file contains C/C++ code that is noncompliant with your coding rule, as well as code that is compliant. Run Polyspace Bug Finder on the specification file and observe the results. If there is a checker that reports a violation for the noncompliant example and no violation for the compliant example, the checker could be an appropriate mapping for your coding rule.

Your coding rule could require mapping to one or multiple Bug Finder checkers. To find accurate mapping between your coding rule and existing Bug Finder checkers, make the scope of the specification file as narrow as possible and avoid code that is not directly related to the coding rule. Multiple specification files can be necessary to cover different aspects of a coding rule.

Map One Bug Finder Checker to Coding Rule

If your coding rule has a narrow scope, it is possible that you can map a single Bug Finder checker to the coding rule. Consider the C++ Core Guideline C.4[1]: Make a function a member only if it needs direct access to the representation of a class. This rule is specific and the C++ Core Guidelines clearly define when the rule is violated — this rule is violated when you have a class with a non-virtual member function that does not modify any of the class data members. Conversely, if a non-virtual class member function modifies a data member, the rule is not violated. To find a Bug Finder checker that you can map to the guideline, create a specification file. The specification file contains:

  • Non-virtual class member functions that do not modify any of the class data members.

  • Non-virtual class member functions that do modify at least one class data member.

This example specification file contains four noncompliant member functions, two examples of compliant member function, and an operator that is compliant by exception.

 Example Specification for C++ Core Guideline C.4

To find a mapping for C++ Core Guideline C.4, run a Polyspace Bug Finder analysis on the specification. Enable C++ coding rules such as the AUTOSAR C++14 and MISRA C++:2023 coding rules. After the analysis completes, review the results. To reduce the number of results you review, limit the review to coding rules related to class design that show four or fewer violations. The results show only one coding rule related to class design with four or fewer violations — AUTOSAR C++14 Rule M9-3-3. A review of the implementation of this rule checker reveals that AUTOSAR C++14 Rule M9-3-3 reports violations on non-const and non-static class member functions that do not access or modify any data members. You can map this checker to C++ Core Guideline C.4..

Map Multiple Bug Finder Checkers to Coding Rule

Some coding rules are broad and can require multiple checkers to cover the different kinds of violations. Consider the C++ Core Guideline C.22: Make default operations consistent[2]. The guideline covers many different inconsistencies in default operations. To make this guideline enforceable by Bug Finder, specify the inconsistencies in default operations that you want to detect. Consider these inconsistencies:

  • Move and copy constructors have side effects.

  • Move constructors fail to move.

  • Constructor does not correctly initialize data members and base classes.

Create specification files for these three issues and run Bug Finder on these specification files.

 Move and copy constructors have side effects

 Move constructors fail to move

 Constructor does not correctly initialize data members and base classes

A review of the results show that these Bug Finder checkers each report violations on one of the issues:

To detect violations of the C++ Core Guideline C.22, these checkers are required.

References

[1] “C++ Core Guidelines.” Accessed June 26, 2024. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.

[2] “C++ Core Guidelines.” Accessed June 26, 2024. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.

Related Topics