Enforce Naming Conventions for C/C++ Variables and Functions
Note
Starting in a future release, Code prover will not support checking naming conventions. Migrate to Bug Finder for enforcing naming convention. See 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.
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
.In the Checkers Selection window, select the rule
4.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
.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 withs_
and have capital letters or digits, specifys_[A-Z0-9_]+
. Polyspace supports Perl regular expressions when defining patterns. SeeCheck custom rules (-custom-rules)
.A custom rule is not activated if the Pattern field is empty.
The Comment field is optional. A comment does not appear in the Polyspace results list. Leave the Comment field blank.
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.
Locate the template
custom_rules.xml
in
. Here,polyspaceroot
\polyspace\examples\doc_cxx\coding_standards_XML
is the root installation folder for the Polyspace products, for instance,polyspaceroot
C:\Program Files\Polyspace Server\R2024b
. Make an editable copy of the filecustom_rules.xml
.In the editable XML file, locate the node corresponding to rule 4.3. Set the
state
attribute toon
. Add a subnodeConvention
and specify it asAll struct fields must begin with s_ and have capital letters or digits
. Then, add a subnodePattern
and specify it ass_[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>
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 during analysis, along with the optionCheck 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.