This topic explains how to prevent C/C++test from reporting selected static analysis violations.
Sections include:
About Suppressions
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.
Defining Suppressions in the GUI
To suppress a static analysis task that is shown in the Quality Tasks view:
- Right-click a Quality Tasks view item that represents the task you want to suppress, then choose Suppress Task... from the menu.
To suppress more than one task, right-click the node that represents a group of tasks (a rule category, a specific rule, a file), then choose Suppress All Tasks - Choose where the suppressions will be stored. You can select one of the following options:
- Suppress in suppression file - The suppression will be stored in a parasoft.suppress file located in the same directory as the corresponding source file. See Defining Suppressions in Suppression Files for details.
- Suppress in source code - The selected task will be suppressed in code and shared across the team when checked in your source control system.
- Suppress locally (deprecated) - The suppression will be stored locally in the deprecated XML-style format. See Using the Deprecated Suppression Format for details. - Enter the reason for suppression.
- Click OK to complete suppression. The suppressed task(s) will be removed from the Quality Tasks view.
Defining Suppressions in Suppression Files
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 manually creating parasoft.suppress files where you can add suppression entries. If you manually create a suppression file, ensure it is located in the same directory as the source file that contains the task you want to suppress.
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
Example:
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
Notes on Attributes
- It is a good practice to specify the reason for suppression.
- 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. - In the
message
attribute, the following wildcards are supported:- ? - Any character
- * - Any sequence of characters
Defining Suppressions in Source Code
When suppressions are defined in source code:
- You ensure that the same suppressions are applied whenever you or a team member tests that code.
- You can add code comments explaining each suppressions, so the reason for each suppression is always clear when you or team members are reviewing the code.
- You gain fine-grained control over which rules are enforced at the file, class, or line level.
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
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>"
Examples:
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
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>"
Examples:
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"
Defining Line Suppressions Based on Regex Patterns
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.
- Specify the path to the advanced settings. See Configuring an Advanced Settings File.
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_.*
Handling Deprecated Suppressions
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.