xref: /aosp_15_r20/external/clang/test/SemaCXX/string-plus-char.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li class A {
4*67e74705SXin Li public:
A()5*67e74705SXin Li   A(): str() { }
A(const char * p)6*67e74705SXin Li   A(const char *p) { }
A(char * p)7*67e74705SXin Li   A(char *p) : str(p + 'a') { } // expected-warning {{adding 'char' to a string pointer does not append to the string}} expected-note {{use array indexing to silence this warning}}
operator +(const char * p)8*67e74705SXin Li   A& operator+(const char *p) { return *this; }
operator +(char ch)9*67e74705SXin Li   A& operator+(char ch) { return *this; }
10*67e74705SXin Li   char * str;
11*67e74705SXin Li };
12*67e74705SXin Li 
f(const char * s)13*67e74705SXin Li void f(const char *s) {
14*67e74705SXin Li   A a = s + 'a'; // // expected-warning {{adding 'char' to a string pointer does not append to the string}} expected-note {{use array indexing to silence this warning}}
15*67e74705SXin Li   a = a + s + 'b'; // no-warning
16*67e74705SXin Li 
17*67e74705SXin Li   char *str = 0;
18*67e74705SXin Li   char *str2 = str + 'c'; // expected-warning {{adding 'char' to a string pointer does not append to the string}} expected-note {{use array indexing to silence this warning}}
19*67e74705SXin Li 
20*67e74705SXin Li   const char *constStr = s + 'c'; // expected-warning {{adding 'char' to a string pointer does not append to the string}} expected-note {{use array indexing to silence this warning}}
21*67e74705SXin Li 
22*67e74705SXin Li   str = 'c' + str;// expected-warning {{adding 'char' to a string pointer does not append to the string}} expected-note {{use array indexing to silence this warning}}
23*67e74705SXin Li 
24*67e74705SXin Li   wchar_t *wstr;
25*67e74705SXin Li   wstr = wstr + L'c'; // expected-warning {{adding 'wchar_t' to a string pointer does not append to the string}} expected-note {{use array indexing to silence this warning}}
26*67e74705SXin Li   str2 = str + u'a'; // expected-warning {{adding 'char16_t' to a string pointer does not append to the string}} expected-note {{use array indexing to silence this warning}}
27*67e74705SXin Li 
28*67e74705SXin Li   // no-warning
29*67e74705SXin Li   char c = 'c';
30*67e74705SXin Li   str = str + c;
31*67e74705SXin Li   str = c + str;
32*67e74705SXin Li }
33