Check custom rules (-custom-rules
)
Follow naming conventions for identifiers
Description
Define naming conventions for identifiers and check your code against them.
Set Option
User interface (desktop products only): In your project configuration, the option is on the Coding Standards & Code Metrics node.
User interface
(Polyspace Platform, desktop products only):
This option does not exist in the project configuration. Activate checkers by using
the options Use custom
checkers file
and Checkers
activation file (-checkers-activation-file)
.
Command line and options file: Use the option
-custom-rules
. See Command-Line Information.
Why Use This Option
Use this option to impose naming conventions on identifiers. Using a naming convention allows you to easily determine the nature of an identifier from its name. For instance, if you define a naming convention for structures, you can easily tell whether an identifier represents a structured variable or not.
After analysis, the Results List pane lists violations of the naming conventions. On the Source pane, for every violation, Polyspace® assigns a symbol to the keyword or identifier relevant to the violation.
For the full list of types on which you can apply naming conventions, see Custom Coding Rules.
Settings
- On
Polyspace matches identifiers in your code against text patterns you define. Define the text patterns in a custom coding rules file. To create a coding rules file,
Use the custom rules wizard:
Click . A Checkers selection window opens.
The Custom node in the left pane is highlighted. Expand the nodes in the right pane to select custom rule you want to check.
For every custom rule you want to check:
Select On .
In the Convention column, enter the error message you want to display if the rule is violated.
For example, for rule 4.3, All struct fields must follow the specified pattern, you can enter
All struct fields must begin with s_
. This message appears on the Result Details pane if:You specify the Pattern as
s_[A-Za-z0-9_]+
.A structure field in your code does not begin with
s_
.
In the Pattern column, enter the text pattern.
For example, for rule 4.3, All struct fields must follow the specified pattern, you can enter
s_[A-Za-z0-9_]+
. Polyspace reports violation of rule 4.3 if a structure field does not begin withs_
.You can use Perl regular expressions to define patterns. For instance, you can use the following expressions.
Expression Meaning . Matches any single character except newline [a-z0-9]
Matches any single letter in the set a-z
, or digit in the set0-9
[^a-e]
Matches any single letter not in the set a-e
\d
Matches any single digit \w
Matches any single alphanumeric character or _
x?
Matches 0 or 1 occurrence of x
x*
Matches 0 or more occurrences of x
x+
Matches 1 or more occurrences of x
For frequent patterns, you can use the following regular expressions:
(?!__)[a-z0-9_]+(?!__)
, matches a text pattern that does not start and end with two underscores.int __text; //Does not match int _text_; //Matches
[a-z0-9_]+_(u8|u16|u32|s8|s16|s32)
, matches a text pattern that ends with a specific suffix.int _text_; //Does not match int _text_s16; //Matches int _text_s33; // Does not match
[a-z0-9_]+_(u8|u16|u32|s8|s16|s32)(_b3|_b8)?
, matches a text pattern that ends with a specific suffix and an optional second suffix.int _text_s16; //Matches int _text_s16_b8; //Matches
If you do not want Polyspace to report a trivial case when you check for a specific pattern, add the pattern for that trivial case to your regular expression.
For example, for rule 8.5, All floating constants must follow the specified pattern, the specified pattern might be that all floating point constants must be suffixed with "f" or "F". To ignore the trivial case 0.0, use this regular expression for instance:
Polyspace reports violations of this rule for the specified pattern except for 0.0.(.*(f|F))|(^0\.0$)
float arr[] = { 0.0, // No violation reported 10.05, // Violation reported 4.00f }; // No violation reported
For a complete list of regular expressions, see Perl documentation.
To use or update an existing coding rules file, click to open the Checkers selection window then do one of the following:
Enter the full path to the file in the field provided
Click Browse and navigate to the file location.
- Off (default)
Polyspace does not check your code against custom naming conventions.
Command-Line Information
Use the command -custom-rules
in the command line to check for
violations of predefined naming conventions.
Parameter:
-custom-rules |
Value:
from-file , specify the file using Set checkers by file
(-checkers-selection-file) |
Default: Off |
Example (Bug Finder):
polyspace-bug-finder -sources |
Example (Bug Finder Server):
polyspace-bug-finder-server -sources |
Alternatively, activate the rules in an activation XML file. See Checkers activation file
(-checkers-activation-file)
.