Main Content

AUTOSAR C++14 Rule A2-3-1

Only those characters specified in the C++ Language Standard basic source character set shall be used in the source code

Since R2020a

Description

Rule Definition

Only those characters specified in the C++ Language Standard basic source character set shall be used in the source code.

Rationale

In the C++ standard, the basic source character set consists of 96 characters. They are:

  • The space character.

  • The control characters such as horizontal tab, vertical tab, form feed, and new line.

  • Upper and lowercase letters, and numbers.

  • Special characters, such as _ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " '.

Using characters outside this set can cause confusion and unexpected bugs. For example, the Greek letter "Τ" is visually similar to the English letter "T", but they are separate characters with different Unicode code-point values. To avoid unexpected behavior, use only the above specified characters in your source code, including comments and string literals. You can use characters outside this set in only two cases. You can use:

  • Other characters inside the text of a wide string or a UTF-8 encoded string.

  • The character @ inside comments, the text of a wide string, or a UTF-8 encoded string.

Polyspace Implementation

Polyspace® flags the characters in your source code that are not in the set of 96 characters specified in C++ standard, with two exceptions that come from the AUTOSAR C++14 Standard. Polyspace does not flag:

  • Other characters inside the text of a wide string or a UTF-8 encoded string.

  • The character @ inside comments, the text of a wide string, or a UTF-8 encoded string.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

The following example demonstrates the Polyspace implementation of AUTOSAR rule A2-3-1.

#include <cstdint>

// @ brief foo function    //Compliant by exception
/* @ brief foo function */ //Compliant by exception

#if 0
@ This one is not in a comment  //Noncompliant
#endif
/*Define £ and € as currency */   // Noncompliant 
#define CUR1 "£"   //Noncompliant                         
#define CUR2 "€"   //Noncompliant 
void myfunction(char *str );                        
int  Total = 0;    //Complaint
int  Τotal = 0;    //Noncompliant
void foo()
{
	char *s1 = "Greek Τ  - normal string"; //Noncompliant
	wchar_t *s2 { L"Greek Τ @ wide string"};  //Compliant
	char *s3 = u8"Greek Τ @ UTF-8";          // Compliant 
	char16_t *s4 = u"Greek Τ UTF-16";  //Noncompliant
	char32_t *s5 = U"Greek Τ UTF-32";  //Noncompliant
	char *s6 = "mail@company.com"; //Noncompliant
	myfunction("Greek Τ");//Noncompliant 
	myfunction(s3);//Complaint
}

main(){
	// ..
}

If your code has characters that are not in the specified character set, Polyspace flags them. Note the global variables Total and Τotal. Even though it looks as if they are the same variable, they are two different variables because the latter starts with the Greek letter "Τ". Confusion between these two characters can lead to unexpected behavior. Because the Greek letter "Τ" is outside the standard set of characters, Polyspace flags every use of the character, even those in comments and string literals.

Polyspace flags every use of characters outside the specified set, with the following exceptions. You can use:

  • Other characters inside a wide string such as s2 or UTF-8 encoded string such as s3.

  • The character @ inside a wide string such as s2, a UTF-8 encoded string such as s3, or a comment.

Check Information

Group: Lexical conventions
Category: Required, Automated

Version History

Introduced in R2020a