In this lesson, we'll build a rule that will report a violation when the ?: operator is used. The following code snapshot has the violation we are interested in finding:

int max(int a,int b) { 
	return (a<=b) ? b:a; // Violation 
}

In this lesson:

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 a Parent Rule Node

Whenever you create a rule, the first thing that you need to do is activate the desired node dictionary and then select the node that you want to be the subject of your rule. To begin creating this rule:

  1. Choose New> Rule from the menu bar.
  2. Choose C,C++ from the Dictionary drop-down menu and enable the By Node option.
  3. Choose Expressions> Miscellaneous> a?:b:c and click OK.

Adding Additional Qualifications

The rule already expresses the pattern that we want the product to search for (the presence of the ?: operator in a file). Thus, no further qualifications are required.

Specifying an Error Mesage

We need to specify what text the product should print when this rule is violated.

  1. Right-click the a?b:c parent rule node and choose Create Output> Display from the shortcut menu.
  2. Enter the error message in the Customize Output dialog, e.g., Do not use the ?: operator, and click OK.

Your rule now tells the product to report the specified error message when the ?: operator is used. The status bar in the lower-right corner still has a yellow icon, however, indicating that the rule is not yet complete. The rule still needs a header and rule ID.

Customizing Rule Properties

You can assign a rule ID, header (title for the rule), severity level, and other other properties from the rule properties pane. 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.

Saving and 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.

  • No labels