Main Content

Create User-Defined Coding Standard by Using Polyspace Bug Finder Checkers

A user-defined coding standard is a collection of coding rules that you curate to check for bugs and defects that are relevant to your project. To create a user-defined coding standard, map your coding rules to existing Polyspace® Bug Finder™ checkers in a Static Analysis Results Interchange Format (SARIF) file. The SARIF file defines the coding rules of your user-defined coding standard and sorts them into subgroups and categories. After creating the SARIF file, you can convert it into a Polyspace coding standard (.pschk) file, which you can use in a Bug Finder analysis to enforce the user-defined standard.

This topic shows how to create a user-defined coding standard once you have the mapping between the coding rules in the user-defined coding standard and existing Bug Finder checkers. To learn how to create the mapping, see Find Polyspace Bug Finder Checkers That Map to Coding Rules in User-Defined Coding Standard.

You can use user-defined coding standards and review the resulting violations in the Polyspace Platform user interface and Polyspace as You Code IDE extensions. The Polyspace desktop user interface and Polyspace Access™ do not support user-defined coding standards. If you upload analysis results that contain violations of a user-defined coding standard to Polyspace Access, the violations of user-defined coding standard are not uploaded and are ignored when you review the results in a web browser.

Overview of SARIF File Properties

This section describes the properties of the SARIF file that are relevant to creating a user-defined coding standard. For details about the SARIF file format and its properties, see SARIF documentation.

A valid SARIF file that encodes the mapping between coding rules and Bug Finder checkers must have "https://docs.oasis-open.org/sarif/sarif/v2.1.0/cos02/schemas/sarif-schema-2.1.0.json" as the value of the property $schema. The $version property must be 2.1.0.

The body of the SARIF file contains a runs node, which includes a description of the user-defined coding standard. The runs node has a child node named supportedTaxonomies, which in turn has a child node named taxonomies. The taxonomies node has these child nodes:

  • properties — This node introduces categories and subsets for the coding rules in your user-defined coding standard.

  • taxa — This node consists of one or more comma-separated elements. Each element corresponds to a coding rule in the user-defined coding standard. To add a new rule, add a new element.

Each elements of the taxa node define the properties of one coding rule in the user-defined standard:

  • id — This property of the taxa node specifies the identifier of the coding rule. This identifier is used when reviewing violations of the user-defined coding standard.

  • text — This property specifies the rule description.

  • sectionName — A user-defined coding standard can consist of several sections. This property specifies which sections the coding rule belongs to. Group related coding rules into sections to make the standard easier to use. You can put new coding rules into existing sections or new sections.

  • category and subsets — These properties specify the category and subsets that the coding rule belong to. The category property accepts a single value, while the subset property accepts an array of values. Categories and subsets divide the coding rules in the standard following two separate axes. For example, you can categorize the coding rules in your user-defined standards into categories based on the importance of fixing their violations, such as Required and Optional. Then you can categorize the coding rules into subsets based on the cost of running the analysis, such as Quick Analysis and Overnight Analysis. When you define each coding rule, you can choose to categorize the rule into one of the categories and one or more of the subsets. When running an analysis, you can quickly select a set of rules using these categories and subsets based on the nature of the analysis.

  • language — This property indicates whether a rule is applicable to C, or C++, or both. When analyzing C code, the rules that apply to C++ are deactivated, and vice versa. Use the values C and C++ for rules that apply to C and C++, respectively. If a rule applies to both C and C++, use the value C|C++.

  • semantic — This value specifies how a coding rule of a user-defined standard is implemented in Bug Finder. Currently, Bug Finder supports implementing coding rule for user-defined coding standard by mapping coding rules to existing checkers. Use the value mapping for this field.

  • mapping — Each element of the taxa node contains one or more mapping nodes in a comma-separated list. Use this property to indicate which Bug Finder checkers map to your coding rule.

Each entry in the mapping list has these child nodes:

Create Mapping in SARIF

By using the properties described in the preceding section, you can summarize the required information for the user-defined coding standard into a SARIF file. Consider these coding rules in a user-defined coding standard:

SectionIDDescriptionBug Finder CheckerCategorySubset
ClassC.4Make a function a member only if it needs direct access to the representation of a classAUTOSAR C++14 Rule M9-3-3Cat-1Subset A
ClassC.22Make default operations consistentMove operation uses copy, AUTOSAR C++14 Rule A12-8-1, AUTOSAR C++14 Rule A12-1-1Cat-1Subset A
Move SemanticsMove_1A move operation may throwA move operation may throwCat-2Subset A
Move SemanticsMove_2Move operation uses copyMove operation uses copyCat-2Subset A, Subset B
Move SemanticsMove_3Const std::move input may cause a more expensive object copyConst std::move input may cause a more expensive object copyCat-2Subset A, Subset B
ContainerContainer_1Expensive use of map instead of setExpensive use of map instead of setCat-3Subset A, Subset B
ContainerContainer_2Expensive use of map's bracket operator to insert or assign a valueExpensive use of map's bracket operator to insert or assign a valueCat-3Subset A, Subset B

To create the mapping for this user-defined coding standard:

  1. Create a SARIF file that follows the schema 2.1.0. Set "https://docs.oasis-open.org/sarif/sarif/v2.1.0/cos02/schemas/sarif-schema-2.1.0.json" as the value of the property $schema and 2.1.0 as the value of $version.

  2. Specify the name of your coding standard in the property runs/taxonomies/name.

  3. Specify the categories and subsets of your standard in the properties runs/taxonomies/properties/categories and runs/taxonomies/properties/subsets. For example:

    "categories": ["Cat-1", "Cat-2", "Cat-3"],
    "subsets": ["Subset A", "Subset B"],

  4. Specify an ID for the standard by using the property runs/taxonomies/properties/id. This ID is used when annotating results of the user-defined coding standard.

  5. For each rule in your coding standard, add an element in the comma-separated list runs/taxonomies/taxa.

  6. Specify coding rule properties for each element in the properties node. For example, use this entry for the rule C.22:

    {
       "id": "C.22",
       "shortDescription": {
         "text": "Make default operations consistent."
       },
       "properties": {
         "sectionName": "Class",
         "category": "Cat-1",
         "language": ["C++"],
         "subsets": ["Subset A"],
         "semantic": {
           "type": "mapping",
           "mapping": [
             {
               "standard": "DEFECT",
               "id": "MOVE_OPERATION_USES_COPY"
             },
             {
               "standard": "AUTOSAR-CPP14",
               "id": "A12-8-1"
             },
             {
               "standard": "AUTOSAR-CPP14",
               "id": "A12-1-1"
             }
    
           ]
         }
       }
     }

 Example SARIF

Create User-Defined Coding Standard

Once you have a SARIF file that implements the mapping between the coding rules in your user-defined coding standard and existing Bug Finder checkers, you can then create the user-defined coding standard. To create the user-defined coding standard, use the command polyspace-catalog-bundler.

Save the SARIF file as myMapping.SARIF. Then create a Polyspace coding standard (.pschk) file myCodingStandard by using this command:

polyspace-catalog-bundler import -catalog myMapping.SARIF -bundle myCodingStandard.pschk -identifier VersionA
Here, myMapping.SARIF is the SARIF file that contains the mapping between the guidelines in your user-defined coding standard. The file myCodingStandard.pschk is the resulting user-defined coding standard file. The identifier VersionA is associated with the user-defined coding standard file. When you run a Bug Finder analysis using user-defined standard, Polyspace prints the identifier in the analysis log. The analysis log also shows the checksum of myMapping.SARIF.

Use the identifier and the checksum to verify that you use the correct user-defined coding standard in your analysis. This information allow you to keep track of different versions of a user-defined standard that you use in your project.

You can open file myCodingStandard.pschk file in the Checkers Selection window:

The Checkers Selection window showing the user-defined coding standard.

  • The coding rule defined in the taxa node are selectable in the Checkers Selection window.

  • The categories and subsets you listed in the taxonomies/properties node are selectable check boxes that allow you to select a group of coding rules quickly.

  • Each new entry in the sectionName nodes corresponds to an expandable section in the Checkers Selection window.

After creating the .pschk file, you can share the file with other Polyspace users in your organization.

To check for violations of the user-defined coding standard, use one of these methods:

  • Command line — Use the .pschk file as an input to the option Checkers activation file (-checkers-activation-file).

  • Polyspace Platform user interface — In the Configuration pane, select Static Analysis > Defects and Coding Standards > . Enter the .pschk file in the Checkers activation file box.

  • Polyspace as You Code — Specify the .pschk file in the Checkers Selection window. You can open the Checkers Selection window from your Polyspace as You Code plugin. See Setting Checkers in Polyspace as You Code (Polyspace Access).

For more details about finding violations of the user-defined coding standard, see Check for Violations of User-Defined Coding Standard Using Polyspace Bug Finder.

See Also

Related Topics