Qt-Autosar-A2.13.2

String literals with different encoding prefixes shall not be concatenated

Required inputs: IR

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

cpp11_mode

cpp11_mode : bool = True

Use rules as defined in the C++11 standard. If false, only 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

Report all prefix difference (i.e. do not exclude those with and without cpp11_mode).