Clang Project

clang_source_code/test/SemaCXX/string-plus-int.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -Wno-array-bounds %s -fpascal-strings
2// RUN: %clang_cc1 -fdiagnostics-parseable-fixits -x c++ %s 2>&1 -Wno-array-bounds -fpascal-strings | FileCheck %s
3
4void consume(const char* c) {}
5void consume(const unsigned char* c) {}
6void consume(const wchar_t* c) {}
7void consumeChar(char c) {}
8
9enum MyEnum {
10  kMySmallEnum = 1,
11  kMyEnum = 5
12};
13
14enum OperatorOverloadEnum {
15  kMyOperatorOverloadedEnum = 5
16};
17
18const char* operator+(const char* c, OperatorOverloadEnum e) {
19  return "yo";
20}
21
22const char* operator+(OperatorOverloadEnum e, const char* c) {
23  return "yo";
24}
25
26void f(int index) {
27  // Should warn.
28  // CHECK: fix-it:"{{.*}}":{31:11-31:11}:"&"
29  // CHECK: fix-it:"{{.*}}":{31:17-31:18}:"["
30  // CHECK: fix-it:"{{.*}}":{31:20-31:20}:"]"
31  consume("foo" + 5);  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
32  consume("foo" + index);  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
33  consume("foo" + kMyEnum);  // expected-warning {{adding 'MyEnum' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
34  consume("foo" + kMySmallEnum); // expected-warning {{adding 'MyEnum' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
35
36  consume(5 + "foo");  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
37  consume(index + "foo");  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
38  consume(kMyEnum + "foo");  // expected-warning {{adding 'MyEnum' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
39  consume(kMySmallEnum + "foo"); // expected-warning {{adding 'MyEnum' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
40
41  // FIXME: suggest replacing with "foo"[5]
42  consumeChar(*("foo" + 5));  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
43  consumeChar(*(5 + "foo"));  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
44
45  consume(L"foo" + 5);  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
46  consume(L"foo" + 2); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
47
48  consume("foo" + 3);  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
49  consume("foo" + 4);  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
50  consume("\pfoo" + 4);  // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
51
52  #define A "foo"
53  #define B "bar"
54  consume(A B + sizeof(A) - 1); // expected-warning {{to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
55
56  // Should not warn.
57  consume(&("foo"[3]));
58  consume(&("foo"[index]));
59  consume(&("foo"[kMyEnum]));
60
61
62  consume("foo" + kMyOperatorOverloadedEnum);
63  consume(kMyOperatorOverloadedEnum + "foo");
64}
65
66template <typename T>
67void PR21848() {
68  (void)(sizeof(T) + ""); // expected-warning {{to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
69}
70template void PR21848<int>(); // expected-note {{in instantiation of function template specialization 'PR21848<int>' requested here}}
71