xref: /aosp_15_r20/external/clang/test/Sema/string-plus-char.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li struct AB{const char *a; const char*b;};
4*67e74705SXin Li 
foo(const struct AB * ab)5*67e74705SXin Li const char *foo(const struct AB *ab) {
6*67e74705SXin Li   return ab->a + 'b'; // expected-warning {{adding 'char' to a string pointer does not append to the string}} expected-note {{use array indexing to silence this warning}}
7*67e74705SXin Li }
8*67e74705SXin Li 
f(const char * s)9*67e74705SXin Li void f(const char *s) {
10*67e74705SXin Li   char *str = 0;
11*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}}
12*67e74705SXin Li 
13*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}}
14*67e74705SXin Li 
15*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}}
16*67e74705SXin Li 
17*67e74705SXin Li   char strArr[] = "foo";
18*67e74705SXin Li   str = strArr + '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   char *strArr2[] = {"ac","dc"};
20*67e74705SXin Li   str = strArr2[0] + '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 
23*67e74705SXin Li   struct AB ab;
24*67e74705SXin Li   constStr = foo(&ab) + 'c'; // expected-warning {{adding 'char' to a string pointer does not append to the string}} expected-note {{use array indexing to silence this warning}}
25*67e74705SXin Li 
26*67e74705SXin Li   // no-warning
27*67e74705SXin Li   char c = 'c';
28*67e74705SXin Li   str = str + c;
29*67e74705SXin Li   str = c + str;
30*67e74705SXin Li }
31