RuleWizard can automatically create rules that flag a given code pattern (see Creating a Rule Automatically). In this lesson, you will learn how to use the Auto-Create feature to create two rules:
Example 1 - Begin Class Names with an Upper Case Letter
Creating the Basic Rule Pattern
- Choose New> Rule from the menu bar.
- Choose C,C++ from the Dictionary drop-down menu and enable the Auto-Create option.
- Enter the code pattern that you want the rule to identify and click OK.
A graphical representation, as well as the source pattern, will be added to panel on the right-hand side. - We want to make sure that the rule checks as many class names as possible. Right-click the Name node and choose Modify.
- Enter a pattern in the Regexp field to scan for all lowercase characters, i.e.,
^[a-z]
. - Click OK. Your rule will now report a violation if a class name begins with a lowercase letter.
Customizing the Output Message
We need to specify what to print when this rule is violated.
- Right-click the output arrow and choose Modify from the shortcut menu.
- Enter the message that you want the product to deliver when this rule is violated, e.g.,
A class name does not begin with a capital letter
. - Click OK to save the output message.
The rule is now functionally complete, but additional steps must be taken before it can be used.
Customizing Rule Properties
At a minimum, you must specify a rule ID and a rule header (name).
- Right-click an empty area in the GUI panel and choose Properties from the shortcut menu.
- Click the Properties tab and specify your rule properties (see Customizing Rule Properties for details)
- Click the Description tab and enter information about the rule. The description is part of the rule documentation.
- Click OK.
Saving and Enabling Your Rule
Before you can check your rule, you need to save and enable it as described in Saving and Enabling Rules.
Viewing Rule Documentation
Information about the rule will be included in RuleWizard’s Rules Documentation files. To view this documentation, click the View RuleDocs button in the toolbar. To refresh this documentation, click the Update RuleDocs button in the toolbar. These buttons are described in the Tool Bar chapter.
Example 2 - Begin Private Field Names With an Underscore
Creating the Basic Rule Pattern
- Choose New> Rule from the menu bar.
- Choose C,C++ from the Dictionary drop-down menu and enable the Auto-Create option.
Enter the code pattern that you want the rule to identify and click OK.
In our example, there is a space between
int
and_i;
. It should be typed exactly like this:int _i;
A graphical representation, as well as the source pattern, will be added to panel on the right-hand side.
- We want to manually add permissions and restrict body variables. Right-click the Body node and choose Modify.
- Choose Declarations> Variables> Member Variable and click OK.
- Right-click the Body node again and choose Permission (M).
- Enable the private option when prompted and click OK.
The basic functionality of your rule is configured.
Modifying the Rule Pattern
- Because class name is irrelevant in the rule that you want to create, remove the rule node that looks for a class with a specific name. Right-click the top Name node containing the regular expression ^A$ and choose choose Edit> Delete.
- We also want the rule to find instances in which a private field does not begin with an underscore character. Right-click the Name node containing the regular expression ^_i$ and choose choose Modify from the shortcut menu.
- Enter
_
(underscore character) in the Regexp field when prompted and enable the Negate option. - Click OK.
- We want the rule to be independent of the field type. Double-click the Type node and choose the Type folder.
- Click OK. At this point, the rule is triggered when the static analysis tool finds classes that have at least one private member variable that do not begin with an underscore. But we need to move the output from the Class node to the Body> Member Variable node to enable the rule to trigger on member variables that do not begin with an underscore.
- Right-click the output arrow class beneath the Class node and choose Edit> Cut.
- Right-click the Body node and click Edit> Paste.
Customizing the Output Message
We need to specify what to print when this rule is violated.
- Right-click the output arrow and choose Modify from the shortcut menu.
- Enter the message that you want the product to deliver when this rule is violated, e.g.,
A private field’s name does not begin with an underscore character
. - Click OK to save the output message.
The rule is now functionally complete, but the rule properties must be set before it can be saved and enabled.
Customizing Rule Properties
At a minimum, you must specify a rule ID and a rule header (name).
- Right-click an empty area in the GUI panel and choose Properties from the shortcut menu.
- Click the Properties tab and specify your rule properties (see Customizing Rule Properties for details)
- Click the Description tab and enter information about the rule. The description is part of the rule documentation.
- Click OK.
Saving and Enabling Your Rule
Before you can check your rule, you need to save and enable it as described in Saving and Enabling Rules.
Viewing Rule Documentation
Information about the rule will be included in RuleWizard’s Rules Documentation files. To view this documentation, click the View RuleDocs button in the toolbar. To refresh this documentation, click the Update RuleDocs button in the toolbar. These buttons are described in the Tool Bar chapter.