This topic explains how to prevent C/C++test from reporting selected static analysis violations.
Sections include:
Suppressions are used to prevent C/C++test from reporting selected occurrences of a static analysis violations. You use suppressions for situations when you generally want to follow a rule, but decide to ignore specific occurrences of the reported task.
Suppressions can be stored in source code or in local suppression files, which can be checked in the source control system so hat they are shared across the team. You can use the Parasoft GUI to create suppressions or manually add information about suppressions in your source code or suppression files.
If you are using the MISRA Compliance Pack for DTP, see the DTP User Guide to learn more about how suppressions are handled by MISRA compliance reporting on DTP. |
To suppress a static analysis task that is shown in the Quality Tasks view:
If you choose to store a suppression in a file, it will be stored in a parasoft.suppress file located in the same directory as the source file that contains the rule violation. You can define in-file suppressions in one of the following ways:
By choosing the Suppress in suppression file option in the GUI (see Defining Suppressions in the GUI).
Newly created suppression files are not included in your project. To add a suppression file to your project:
We recommend that suppression files be checked in your source control system. This allows you to share information about suppressions with other team members and easily review the suppressions on a branch in your SCM repository before merging the code into the main stream of development, such as "master", "trunk', etc.
Use the following format to add suppression entries to parasoft.suppress files:
suppression-begin file: Account.cpp (required) line: 12 (optional) rule-id: CODSTA-123 (optional) message: Exact violation message (optional) reason: Approved (optional) author: devel (optional) date: 2020-09-21 (optional) suppression-end |
At a minimum, you must specify the source file where the problem was detected. This will suppress all findings reported for the specified file. In the following example, all findings detected in the Account file will be suppressed:
suppression-begin file: Account.cpp suppression-end |
Other attributes are optional and help you fine-tune the suppression. In the following example, all findings that the PB.TYPO.TLS rule detected in the Account file are suppressed, regardless on which code line they occur:
suppression-begin file: Account.cpp rule-id: PB.TYPO.TLS suppression-end |
The line
attribute should be used with caution as it may invalidate the suppression if the code is moved to another line when the source file is modified.
When suppressions are defined in source code:
You can define suppressions in source code either by choosing the appropriate option in the GUI (see Defining Suppressions in the GUI) or by manually them manually in source code – using the following suppression syntax.
<suppression keyword> [<rule category> | <rule category> . <rule id> | <rule category > - <rule severity> | ALL ] <suppression comment> |
//parasoft-suppress CODSTA "suppress all rules in category CODSTA" // parasoft-suppress CODSTA.NEA "suppress rule CODSTA.NEA" // parasoft-suppress CODSTA-1 "suppress all rules in category CODSTA with severity level 1" // parasoft-suppress ALL "suppress all rules" // parasoft-suppress CODSTA FORMAT.MCH JAVADOC-3 "suppress all rules in category CODSTA and rule FORMAT.MCH and all rules in category JAVADOC with severity level 3" |
<begin suppression keyword> [<rule category> | <rule category> . <rule id> | <rule category > - <rule severity> | ALL ] <suppression comment> ..... source code block ..... <end suppression keyword> [<rule category> | <rule category> . <rule id> | <rule category > - <rule severity> | ALL ] <suppression comment> |
// parasoft-begin-suppress CODSTA "begin suppress all rules in category CODSTA" ..... // parasoft-end-suppress CODSTA "end suppress all rules in category CODSTA" // parasoft-begin-suppress CODSTA.NEA "begin suppress rule CODSTA.NEA" ..... // parasoft-end-suppress CODSTA.NEA "end suppress rule CODSTA.NEA" // parasoft-begin-suppress CODSTA-1 "begin suppress all rules in category CODSTA with severity level 1" ...... // parasoft-end-suppress CODSTA-1 "end suppress all rules in category CODSTA with severity level 1" //parasoft-begin-suppress ALL "begin suppress all rules" ..... // parasoft-end-suppress ALL "end suppress all rules" // parasoft-begin-suppress CODSTA FORMAT.MCH "begin suppress all rules in category CODSTA and rule FORMAT.MCH" ..... // parasoft-end-suppress CODSTA FORMAT.MCH "end suppress all rules in category CODSTA and rule FORMAT.MCH" // parasoft-begin-suppress CODSTA "begin suppress all rules in category CODSTA" ..... // parasoft-end-suppress CODSTA-1 "end suppress all rules in category CODSTA with severity level 1; however rules with severity level 2-5 in category CODSTA are still suppressed." ..... // parasoft-end-suppress CODSTA "end suppress all rules in category CODSTA" // parasoft-begin-suppress ALL "begin suppress all rules" ..... // parasoft-end-suppress CODSTA FORMAT-1 "end suppress all rules in category CODSTA and all rules in category FORMAT with severity level 1; however, others rules in CODSTA and FORMAT-1 are still suppressed." ..... // parasoft-end-suppress ALL "end suppress all rules" //parasoft-begin-suppress ALL "begin suppress all rules, since no end suppression comment, all rules will be suppressed starting from this line" |
The following suppresion directives are deprecated:
|
You can configure C/C++test to automatically suppress static rule violations that are detected on lines that match a regular expression pattern. This may be useful when you want to suppress tasks that are difficult to suppress using the in-line or in-code suppressions, such as Qt macros.
To define regular expressions patterns to specify the code lines:
Create an advanced settings file that includes the following C/C++test's advanced settings:
cpptest.result.line.suppressions.enabled=true // Enables creating regex-based suppressions. cpptest.result.line.suppressions.pattern=[regex;regex] // Specifies a semicolon-separated list of regex patterns. |
Static rule violations that occur on lines that match the configured regex pattern(s) will be suppressed. For example, the following configuration will suppresses all findings detected on code lines that contain "Q_" anywhere on the line.
cpptest.result.line.suppressions.enabled=true cpptest.result.line.suppressions.pattern=.*Q_.* |
Storing suppressions in the XML-style format locally or on Team Server is deprecated. Starting with C/C++test 2020.2, suppressions are created either directly in code or in the new in-file format (see Defining Suppressions in Suppression Files). If there are suppressions in the deprecated format available for your project (typically, when you upgrade C/C++test to version 2020.2 or later and you have created suppressions with previous versions of C/C++test) they are still applied to your code and you can review them in the deprecated Suppressions view.
You can migrate local suppressions to a new format using C/C++test's capabilities. If you need to migrate suppressions stored on Team Server, please contact Parasoft Support.
To migrate deprecated local suppressions:
Migration has the following consequences:
If you should want to restore deprecated suppressions from the backup file:
Go to the location where the backup file is stored (the location is printed to the console). It contains a local_suppressions_backup.zip file.
Extract the contents of local_suppressions_backup.zip to the same location.
You can re-migrate restored suppressions on next IDE startup.
Enabling deprecated suppressions allows you to create suppressions in the deprecated format and store them locally or on Team Server.