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 that 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. In addition, suppressions can be defined programmatically through a configuration file.
If you implement a workflow for achieving compliance with the MISRA standard, you can exclude false positive violations from MISRA Compliance reports created on DTP. When you suppress a violation and specify a reason for suppression that begins with "false positive", the violation will not be included in MISRA Compliance reports. See the DTP User Guide for details about the Parasoft compliance solution. |
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 automatically included in your project and displayed in the file tree in the IDE along with other project files.
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 reason: false positive suppression-end |
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.message
attribute, the following wildcards are supported: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.
Line suppression allows for suppressing violations in a single line. The suppression comment must be specified at the end of the line of code where the violation occurs, using the following syntax:
// parasoft-suppress <rule-id>|<rule-category>|ALL "<suppression comment>" |
int proc1(int i) // parasoft-suppress AUTOSAR-A7_1_1-a "suppress AUTOSAR-A7_1_1-a violation" { char * m = "msg"; // parasoft-suppress AUTOSAR-A27_0_4 "suppress all AUTOSAR-A27_0_4 violations (AUTOSAR-A27_0_4-a, -b, ...)" int j = 0; // parasoft-suppress AUTOSAR "suppress all AUTOSAR violations" return i + (++j); // parasoft-suppress ALL "suppress all violations" } |
Block suppression allows for suppressing violations in a block of code. The suppression begin/end comments must be specified before/after the block of code where the violations occur, using the following syntax:
// parasoft-begin-suppress <rule-id>|<rule-category>|ALL "<suppression comment>" ... code block ... // parasoft-end-suppress <rule-id>|<rule-category>|ALL "<suppression comment>" |
int proc2(int i) { // parasoft-begin-suppress ALL "suppress all violations" char * m = "msg"; int j = 0; return i + (++j); // parasoft-end-suppress ALL "suppress all violations" } int proc3(int i) { // parasoft-begin-suppress AUTOSAR "suppress all AUTOSAR violations" char * m = "msg"; int j = 0; // parasoft-end-suppress AUTOSAR "suppress all AUTOSAR violations" return i + (++j); } // parasoft-begin-suppress AUTOSAR-A8_5_2-a AUTOSAR-A0_1_1-a "suppress all AUTOSAR-A8_5_2-a and AUTOSAR-A0_1_1-a violations" int proc4(int i) { char * m = "msg"; int j = 0; return i + (++j); } // parasoft-end-suppress AUTOSAR-A8_5_2-a AUTOSAR-A0_1_1-a "suppress all AUTOSAR-A8_5_2-a and AUTOSAR-A0_1_1-a violations" |
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<rule-id|rule-category|ALL>;regex<rule-id|rule-category|ALL>] // Specifies a semicolon-separated list of regex patterns. |
Note: The <> brackets are only required if you want to specify rules. The rule specifiers can also be comma-separated list.
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_.*<OPT> |
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 in-file suppression format (see Defining Suppressions in Suppression Files). If there are suppressions in the deprecated format available for your project that you want to migrate to the new format, please contact Parasoft Support.