Qt-Autosar-A2.13.2¶
String literals with different encoding prefixes shall not be concatenated
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Concatenation of wide and narrow string literals leads to undefined behavior.
“In translation phase 6 (2.2), adjacent string-literals are concatenated. If both string- literals have the same encoding-prefix, the resulting concatenated string literal has that encoding-prefix. If one string-literal has no encoding-prefix, it is treated as a string-literal of the same encoding-prefix as the other operand. If a UTF-8 string literal token is adjacent to a wide string literal token, the program is ill-formed. Any other concatenations are conditionally-supported with implementation-defined behavior. [ Note: This concatenation is an interpretation, not a conversion. Because the interpretation happens in translation phase 6 (after each character from a literal has been translated into a value from the appropriate character set), a string-literal’s initial rawness has no effect on the interpretation or well-formedness of the concatenation. -end note ]” [C++14 Language Standard] [3]
Example
//% $Id: A2-13-2.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
char16_t nArray[] =
u"Hello"
u"World"; // Compliant, "u" stands for char16_t type
char32_t nArray2[] =
U"Hello"
U"World"; // Compliant, "U" stands for char32_t type
wchar_t wArray[] =
L"Hello"
L"World"; // Compliant, "L" stands for wchar_t type - violates A2-13-3
// rule.
wchar_t mixed1[] =
"Hello"
L"World"; // Compliant
char32_t mixed2[] =
"Hello"
U"World"; // Compliant
char16_t mixed3[] =
"Hello"
u"World"; // Compliant
// wchar_t mixed1[] = u"Hello" L"World"; // Non-compliant - compilation error
// char32_t mixed2[] = u"Hello" U"World"; // Non-compliant - compilation error
See Also
- MISRA C++ 2008 [7]: required 2-13-5 Narrow and wide string literals shall not be concatenated.
- HIC++ v4.0 [9]: 2.5.1 Do not concatenate strings with different encoding prefixes
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
mixed_string_concatenation |
Concatenation of mixed string encodings |
None |
False |
narrow_wide_concat |
Concatenation of narrow and wide string literal |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
cpp11_mode¶
cpp11_mode : bool = True
L and unprefixed literals are reported.
If true, prefixes of length two and unprefixed literals are conforming;
but additionally, mixed_string_concatenation compiler messages are reported.
report_all_prefix_differences¶
report_all_prefix_differences : bool = False