[cfe-dev] C++ String literals used as initializers are re-assigned incorrect array element type

Tom Honermann thonermann at coverity.com
Tue Mar 25 08:30:37 PDT 2014


Using clang trunk:

$ cat t.cpp
char ca[] = "text";

$ clang --version
clang version 3.5.0 (trunk 204467)
...

$ clang -c -Xclang -ast-dump t.cpp
...
`-VarDecl 0x7b0cb90 <t.cpp:1:1, col:13> ca 'char [5]'
   `-StringLiteral 0x7b0cc68 <col:13> 'char [5]' lvalue "text"

Note that the type of the string literal is 'char [5]'.  I would expect 
'const char[5]'.

I debugged a bit and the StringLiteral instance is initially created 
with a const qualified element type, but the const qualification is 
later removed by CheckStringInit() in lib/Sema/SemaInit.cpp.  I suspect 
CheckStringInit() intends only to adjust the size of the string literal 
and not to change qualifiers on the element type.

Actually, while debugging this, I realized the issue goes beyond 
qualifiers.  The element type of the string literal itself is changed:

$ cat t.cpp
typedef char my_char;
my_char ca[] = "text";

$ clang -c -Xclang -ast-dump t.cpp
...
`-VarDecl 0x4e61730 <line:2:1, col:16> ca 'my_char [5]'
   `-StringLiteral 0x4e61808 <col:16> 'my_char [5]' lvalue "text"

Note that the StringLiteral now has an array element type of 'my_char'.

Tom.



More information about the cfe-dev mailing list