Main Content

Check for Violations of User-Defined Coding Standard Using Polyspace Bug Finder

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. A user-defined coding standard is defined in a .pschk file. This example shows how you can use a .pschk file to check for violations of a user-defined coding standard and review the results in the Polyspace® Platform user interface. For details about how you can create a user-defined coding standard, see Create User-Defined Coding Standard by Using Polyspace Bug Finder Checkers.

The older 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 the user-defined coding standard are not uploaded.

Example Files

To follow this topic, copy the content of the folder polyspaceroot\polyspace\examples\doc_cxx\Custom_Coding_Standard. Here, polyspacroot is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2024b.

The copied folder contains:

  • The file UDC.pschk that contains User-Defined Coding Standard

  • Source files on which you can run a Polyspace Bug Finder™ analysis

  • A text file containing a list of necessary source files

Configure and Run Analysis

To check for violations of the user-defined coding standard, create a Polyspace project (.psprjx), add the source files to the project, and configure the checkers.

To create a project, open the Polyspace Platform user interface by invoking the executable polyspace-desktop-ui. In the toolstrip, click New > Project. Save the new project as UserDefinedCodingStandard in a writable location. To add the source files to the project, in the toolstrip, click Add Source Folder and navigate to the folder WorkingFolder and click Select Folder.

For more details about creating .psprjx projects, see Create Project and Add Source Files in Polyspace Platform User Interface.

Configure the project to run a Bug Finder analysis using the user-defined coding standard. In the Polyspace Platform user interface:

  1. In the Configuration pane, select Build > Target & Compiler. From the Source code language list, select CPP.

  2. Select Static Analysis > Defects and Coding Standards, and then select Use custom checkers file. Click next to the Checkers activation file box to open the Checkers Selection window opens.

  3. In the Checkers Selection window, click Browse and navigate to the file UDC.pschk and click open. If prompted to save your changes, click No.

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

  4. Select coding rules from the user-defined standard. Optionally, select Bug Finder defects or coding rules from other existing coding standards.

  5. Save your selection by clicking Save changes.

  6. Click Finish to close the window.

The file UDC.pschk now contains the activated checkers.

Run Polyspace Bug Finder analysis by clicking Find Defects in the Project toolstrip.

To run the preceding analysis from the command line, specify the UDC.pschk file as an input to the option -checkers-activation-file. For example, at the command line, enter:

polyspace-bug-finder -sources-list-file src_list.txt -checkers-activation-file UDC.pschk -lang cpp
For details about the option -checkers-activation-file, see Checkers activation file (-checkers-activation-file).

Review and Annotate Result

After the analysis completes, right-click the latest entry in the Results pane and from the context menu, select Open Review to open the results list. The results list show 27 violations of the standard User-Defined Coding Standard.

Review the results in the list and, if necessary, add review information in the Result Details pane. You can specify Status and Severity and add comments to each result.

Result Details pane showing a violation of the coding rule C.4 from the User-Defined Coding Standard.

For more details about adding review information to results in Polyspace Platform user interface, see Address Results in Polyspace Platform User Interface Through Bug Fixes or Justifications.

You can also annotate results directly in your code by using code annotations. For example, use this comment to add review information for violations of the rule C.22:

// polyspace UDCS:C.22 [To fix:Medium] "Comment"
Here, UDCS is the id of the user-defined coding standard and C.22 is the id of the guideline. You can get this information by copying annotations from the Polyspace Platform user interface. To copy an annotation, right-click a result and from the context menu, select Add Pre-Justification to Clipboard > Copy annotation for finding StandardID ruleID, where StandardID is the ID of the user-defined standard and ruleID is the short name of the coding rule. Then, paste the annotation to appropriate location in the code.

For more information about code annotations, see Annotate Code and Hide Known or Acceptable Results.

See Also

Related Topics