You can create rules that check for formatting, spacing, and comments within source code. These rules are created with the JavaText dictionary instead of the default Java dictionary. This lesson demonstrates how to create a text formatting rule that checks whether there are comments in the form of ‘//’ after each semicolon.
Designing the Rule Pattern
Designing the rule pattern consists of creating a parent rule node and then adding qualifications to that node so that it fully expresses the code pattern that you wish to search for and remove during static analysis.
Creating the Parent Node
- Choose File> New> Rule from the menu bar.
- Choose JavaText from the Dictionary menu.
- Enable the By Node option in the Rule Creation box.
- Choose the Various> ; node in the Node Selections box and click OK.
Adding Additional Qualifications to the Parent Rule Node
- Right-click the ; rule node and choose Context from the shortcut menu.
- Choose Brackets> { and click OK.
- Right-click the Context rule node and choose Direct Check from the shortcut menu. When a direct check is performed, the product searches for the specified condition in only the first node associated with the given node. When this rule is complete, the direct check will prompt the product to ignore the semicolons inside for loops.
- Right-click the ; rule node and choose NextIgnoringWhitespace from the shortcut menu.
- In the NextIgnoring Whitespace dialog, choose Comments> // node and click OK.
The current rule structure represents // comments following a semicolon, which is in the context of an opening bracket. - Right-click the NextIgnoringWhitespace rule node and choose Direct Check from the shortcut menu. When the rule is complete, the direct check will ensure that there are no characters (except white space) between the semicolon and the
//
. If you do add direct check, a//
following a statement would also count for the previous statement. - Right-click the NextIgnoringWhitespace rule node and choose Create Collector from the shortcut menu. A collector pentagon will be added to this node.
- Right-click the collector and choose Count # from the shortcut menu.
- Verify that the dialog that opens contains the expression
$$==0
and click OK. The rule now represents code which contains zero matches for the specified pattern (a semicolon [in the context of a “{“], which is followed by a “//”).
Specifying an Error Message
- Right click the ; rule node and choose Create Output> Display from the shortcut menu.
- Add an error message in this dialog box (e.g.,
Comments in the form of “//” do not follow a semicolon
).
Your rule now tells the product to report the specified error message for every semicolon that is not followed with comments in the form of "//". Your rule is now functionally complete, but you will need to specify a rule ID and header, as well as specify other rule properties. See Customizing Rule Properties for details.
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.