Main Content

Enforce Naming Conventions for C/C++ Variables and Functions

This example shows how to check for violations of naming conventions on functions and objects in your C/C++ code. For each naming convention, you specify a pattern in the form of a regular expression. The software compares the pattern to identifiers in the source code and determines whether the identifiers follow those naming conventions.

The tutorial uses this code stored in a file printInitialValue.c:

#include <stdio.h>

typedef struct {
    int a;
    int b;
} collection;

void main()
{
    collection myCollection= {0,0};
    printf("Initial values in the collection are %d and %d.",myCollection.a,myCollection.b);
}

Specify Naming Convention

Custom coding rule checkers compare the identifiers in your code to a naming convention that you specify. Polyspace® raises a violation if the identifiers do not match the convention. Before you use the custom coding rules to enforce the naming convention, specify the naming convention by using a regular expression.

  1. Open the Checkers Selection window. Depending on your workflow, you might open the window by using one of the Polyspace as You Code IDE plugins, the desktop user interface, or the command polyspace-checkers-selection.

  2. In the Checkers Selection window, select the rule 4.3.

  3. In the Convention field, enter the message that you want to display when the rule is violated. This message describes the naming convention that you want to specify. For instance, in the Convention field, enter All struct fields must begin with s_ and have capital letters or digits.

  4. In the Pattern field, enter a regular expression corresponding to the naming convention that you want to specify. For instance, to represent struct field names that begin with s_ and have capital letters or digits, specify s_[A-Z0-9_]+. Polyspace supports Perl regular expressions when defining patterns. See Check custom rules (-custom-rules).

    Example Checkers selection window showing custom rules selected. The pattern field is not empty.

    A custom rule is not activated if the Pattern field is empty.

  5. The Comment field is optional. A comment does not appear in the Polyspace results list. Leave the Comment field blank.

  6. Save your changes in an XML file and close the window. This XML file can be used to check the specified custom rule.

Alternatively, edit a preexisting checkers XML file to specify naming conventions. The Polyspace installation folder contains a template that you can copy and edit.

  1. Locate the template custom_rules.xml in polyspaceroot\polyspace\examples\doc_cxx\coding_standards_XML. Here, polyspaceroot is the root installation folder for the Polyspace products, for instance, C:\Program Files\Polyspace Server\R2024b. Make an editable copy of the file custom_rules.xml.

  2. In the editable XML file, locate the node corresponding to rule 4.3. Set the state attribute to on. Add a subnode Convention and specify it as All struct fields must begin with s_ and have capital letters or digits. Then, add a subnode Pattern and specify it as s_[A-Z0-9_]+. For instance:

          <check id="4.3" state="on">
    	  <convention>All struct fields must begin with s_ and have capital letters or digits</convention>
    	  <pattern>s_[A-Z0-9_]+</pattern>
          </check>

  3. Save the XML file. You can use this XML file check the specified custom rule.

Check for Violations of Defined Custom Coding Rule

After specifying the naming convention, run a Polyspace analysis.

  • If you are using the Polyspace desktop UI or one of the Polyspace as You Code plugins in an IDE, run a Polyspace analysis after saving your changes in the Checkers Selection window.

  • If you are using the command line interface, provide the modified custom_rules.xml file as the argument for the option Set checkers by file (-checkers-selection-file) during analysis, along with the option Check custom rules (-custom-rules). For instance, for custom rules checking by using Polyspace Bug Finder Server, enter:

    polyspace-bug-finder-server -sources printInitialValue.c -custom-rules from-file -checkers-selection-file custom_rules.xml

The Polyspace analysis reports two violations of custom rule 4.3 on the two fields collection.a and collection.b.

To resolve the defects, change the name of the fields so that they comply with the naming convention. For instance, rename the fields as s_A and s_B respectively. After renaming the fields, run another Polyspace analysis to verify that the naming convention is no longer violated.

See Also

|

Related Topics