This topic explains how to check custom requirements or tailor existing rules to your unique needs by either modifying existing rules or by creating custom rules.
Sections include:
Introduction
A cornerstone of static analysis with C/C++test is the ability to customize both static analysis test configurations (the set of rules to check) and specific rules—including creating new custom rules.
The rules provided with C/C++test are a comprehensive set to select from, yet specific coding guidelines may differ slightly, or require well-defined exceptions to the rules, depending on the style of coding, or the nature of the application. For this reason, it is crucial to understand how C/C++test rules can be customized, deployed, and shared across the team.
C/C++test has the following types of rules:
- Pattern-based rules check code for specific patterns that are encoded in the rule, and trigger when the code matches the pattern. This type of analysis is usually local to a compilation unit (a single source file with all included headers). Many pattern-based rules can be parameterized using RuleWizard (see Customizing Rules with RuleWizard).
- Flow-based (Flow Analysis) rules check coding patterns and flow of data across multiple functions and source files. Many flow-based rules can be parameterized by changing their configuration parameters directly in the C++test GUI (see Customizing Parameterized Rules). RuleWizard is not used to configure flow-based rules.
Customizing Parameterized Rules
A number of rules can be customized by modifying the available rule parameters. Parameterized rules are marked with a special icon (a wizard hat with a radio button) in the Test Configurations dialog Static> Rules Tree tab:
Available rule parameters are described in the rule’s documentation. To view a rule’s description, right-click the node that represents that rule, then choose View Rule Documentation from the shortcut menu.
To modify rule parameters:
- Open the Test Configurations dialog by choosing Parasoft> Test Configurations or by choosing Test Configurations in the drop-down menu on the Run Tests toolbar button.
- Open the Static> Rules Tree tab for any Test Configuration. The modified rule parameters will be applied to all Test Configurations, so it does not matter which Test Configuration you select in this step.
- Expand the rule’s category branch.
- Right-click the parameterized rule that you want to modify, then choose View/Change Rule Parameters from the shortcut menu.
- Modify the rule parameters in the dialog that opens.
- Click OK to save your changes.
Customizing and Creating Rules with RuleWizard
Customizing Rules with RuleWizard
Many of C/C++test’s pattern-based rules are edited with RuleWizard. C/C++test rules are physical files that can be loaded in RuleWizard and changed as needed. The built-in C/C++test rules are included in the installation directories.
Customizable rules are marked with the following wizard hat + wizard wand icon in the Test Configuration panel’s rules tree:
We strongly recommend that you leave the C/C++test built-in rules intact. To customize the rule, duplicate a built-in rule, and then modify the duplicate.
The rules are customized viat the GUI and do not require modifying the actual rule rules.
To customize a rule with RuleWizard:
- Open the Test Configurations dialog by choosing Parasoft> Test Configurations or by choosing Test Configurations in the drop-down menu on the Run Tests toolbar button.
- Open the Static> Rules Tree tab for any Test Configuration.
- Right-click the rule you want to modify, then choose Duplicate from the shortcut menu.
- A duplicate rule node—with a file icon—will be added to the rules tree.
- A copy of the rule file(s) will be added to the user-specific disk location. The rule ID changes with this operation so that the duplicated rule does not mask the built-in rule (see Note on Duplicated Rule IDs for details)
- Right-click the duplicate rule, then choose Edit Rule in RuleWizard from the shortcut menu to open the rule in RuleWizard.
- Edit the rule in RuleWizard, and save it. See the RuleWizard User Guide (accessible by choosing Help> Documentation in the RuleWizard GUI) for information on how to modify and save custom rules.
- Upload the rule to DTP or Team Server to make it accessible to other team members:
- Uploading to DTP: See the Managing Custom Rules section of the DTP User Guide.
- Uploading to Team Server: Right-click the edited rule and choose Upload to Team Server. - Enable the edited rule in your test configuration(s).
Creating New Rules with RuleWizard
You can easily create your own rules using the RuleWizard module–a graphical rule creation and customization tool available in C/C++test. With RuleWizard, rules can be created graphically (by creating a flow-chart-like representation of the rule) or automatically (by providing code that demonstrates a sample rule violation).
To open RuleWizard, choose Parasoft> Launch RuleWizard from your IDE menu.
See the RuleWizard User Guide (Help> Documentation in the RuleWizard GUI) for information on how to modify and save custom rules.
Deploying Customized Rules or Fully-Custom Rules
You can deploy custom rules on DTP, on Team Server, or by manually importing the rule file.
Notes on Rule IDs
Each rule that you import into the tool must have a unique rule ID. See Note on Duplicated Rule IDs for details. To specify rule categories using the rule ID, use the format <rule_category>-<rule_uid>.
For example, "myrule-123" will be automatically assigned to "myrule" category.
Deploying Custom Rules with Team Server
See Deploying Custom Rules Across the Team.
Deploying Custom Rules on DTP
See the Managing Custom Rules section in the DTP User Guide for information how to deploy custom rules on DTP.
Manually Importing Custom Rules
- Open the Test Configurations dialog by choosing Parasoft> Test Configurations or by choosing Parasoft> Test Configurations in the drop-down menu on the Run Tests toolbar button.
- Select any Test Configurations category. The new rule(s) will be available in all available Test Configurations.
- Open the Static> Rules Tree tab.
- If any new rules should belong to a new category, create a new category as follows:
- Click the Edit Rulemap. button.
- Open the Categories tab.
- Click New. A new entry will be added to the category table.
- Enter a category ID and category description in the new entry. For instance, an organization might choose to use ACME as the category ID and ACME INTERNAL RULES as the description.
- Click OK to save the new category
- Click the Import button to the right of the rules tree. The Import RuleWizard rule dialog will open.
- Use the Import RuleWizard rule dialog to specify which rule(s) you want to import, and whether you want to overwrite existing rule files (if an imported rule file has the same name as an existing rule file).
- Click OK. The rule will be displayed under the assigned category and will be disabled by default.
- Enable the new rule(s) you want checked.
- Click either Apply or Close to commit the modified settings.
Note on Duplicated Rule IDs
Rules are recognized by rule ID within C/C++test If you have a rule with the same ID as a rule already in the built-in, user, or team categories, then the priority of usage is team/user/built-in. As a result, if you import a rule that matches the ID of a built-in rule, then the imported rule will be physically located in the default rule directory set in the Preferences panel, and the rule configuration will be use the imported rule instead of the built-in rule with the same name. If a rule is uploaded to Team Server, then the rule on Team Server will mask any user rules with the same ID, as well as the built-in rule with the same ID.