This lesson demonstrates how to build a rule that flags instances where function names do not begin with a capital letter. The following code snapshot has the violation we are interested in finding:

class Base {
	public:
	Base(); // ok
	~Base(); // ok
};

int test() { // violation
	return 0;
}

In this section:

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

  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 the Declarations> Functions directory and click OK. We selected the parent node because want this rule to apply to both member and global function.

Adding Additional Qualifications 

Right-click the Functions rule node and choose Name from the shortcut menu. 

Specify a value that the tool should look for in the Regexp field. If you want to report an error when a specific value is missing in the code, (as we do in our example), enter the value and enable the Negate option. This tells the product to report an error if the specified value is not present. Disable this option to report an error when a specific value is present in the code. 

In this lesson, we want the function name to begin with a capital letter, so we enter ^[A-Z] and enable the Negate option. The caret (^) indicates the beginning of an expression; [A-Z] indicates uppercase letters from A to Z. 4.

Click OK. Your rule should now look like this:

 

Optional Qualifications

The following qualifications are not required to complete this lesson, but they demonstrate additional Rule Wizard functionality.

If you want to exempt operator functions from this rule:

  1. Right-click the Function rule node and choose IsOperator from the shortcut menu.
  2. Right-click the IsOperator rule node and choose Toggle from the shortcut menu.

If you want to exempt constructor functions from this rule:

  1. Right-click the Function rule node and choose Member Function> IsConstructor from the shortcut menu.
  2. Right-click the IsConstructor rule node and choose Toggle from the shortcut menu.

If you want to exempt destructor functions from this rule:

  1. Right-click the Function rule node and choose Member Function> IsDestructor from the shortcut menu.
  2. Right-click the IsDestructor rule node and choose Toggle from the shortcut menu.

 Your rule should resemble the following image after adding these qualifications:

When you enter these conditions, the product will not check whether the names of operator functions, constructor functions, or destructor functions begin with a capital letter.

Specifying an Error Message

  1. Right-click the Functions rule node and choose Create Output> Display from the shortcut menu.
  2. Enter the message you want delivered when this rule is violated, e.g., A function name should begin with a capital letter.
  3. Click OK. Your rule should now look like this:

The yellow nodes indicate that the rule is properly constructed and now tells the product enforcing the rule to report the specified error message when a function's name does not begin with a capital letter. The rule is now functionally complete, but the rule properties must be set before it can be saved and enabled.

Customizing Rule Properties

The status bar in the lower-right corner indicates that the rule is not quite complete. After you customize this rule's properties and save it, the product will be able to enforce it. 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.

  • No labels