xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/string-plus-int.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wno-array-bounds %s -fpascal-strings
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fdiagnostics-parseable-fixits -x c++ %s 2>&1 -Wno-array-bounds -fpascal-strings | FileCheck %s
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc void consume(const char* c) {}
5*f4a2713aSLionel Sambuc void consume(const unsigned char* c) {}
6*f4a2713aSLionel Sambuc void consume(const wchar_t* c) {}
7*f4a2713aSLionel Sambuc void consumeChar(char c) {}
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc enum MyEnum {
10*f4a2713aSLionel Sambuc   kMySmallEnum = 1,
11*f4a2713aSLionel Sambuc   kMyEnum = 5
12*f4a2713aSLionel Sambuc };
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc enum OperatorOverloadEnum {
15*f4a2713aSLionel Sambuc   kMyOperatorOverloadedEnum = 5
16*f4a2713aSLionel Sambuc };
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc const char* operator+(const char* c, OperatorOverloadEnum e) {
19*f4a2713aSLionel Sambuc   return "yo";
20*f4a2713aSLionel Sambuc }
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc const char* operator+(OperatorOverloadEnum e, const char* c) {
23*f4a2713aSLionel Sambuc   return "yo";
24*f4a2713aSLionel Sambuc }
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc void f(int index) {
27*f4a2713aSLionel Sambuc   // Should warn.
28*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{31:11-31:11}:"&"
29*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{31:17-31:18}:"["
30*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{31:20-31:20}:"]"
31*f4a2713aSLionel Sambuc   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*f4a2713aSLionel Sambuc   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*f4a2713aSLionel Sambuc   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*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc   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}}
36*f4a2713aSLionel Sambuc   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}}
37*f4a2713aSLionel Sambuc   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}}
38*f4a2713aSLionel Sambuc 
39*f4a2713aSLionel Sambuc   // FIXME: suggest replacing with "foo"[5]
40*f4a2713aSLionel Sambuc   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}}
41*f4a2713aSLionel Sambuc   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}}
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc   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}}
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc   // Should not warn.
46*f4a2713aSLionel Sambuc   consume(&("foo"[3]));
47*f4a2713aSLionel Sambuc   consume(&("foo"[index]));
48*f4a2713aSLionel Sambuc   consume(&("foo"[kMyEnum]));
49*f4a2713aSLionel Sambuc   consume("foo" + kMySmallEnum);
50*f4a2713aSLionel Sambuc   consume(kMySmallEnum + "foo");
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc   consume(L"foo" + 2);
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc   consume("foo" + 3);  // Points at the \0
55*f4a2713aSLionel Sambuc   consume("foo" + 4);  // Points 1 past the \0, which is legal too.
56*f4a2713aSLionel Sambuc   consume("\pfoo" + 4);  // Pascal strings don't have a trailing \0, but they
57*f4a2713aSLionel Sambuc                          // have a leading length byte, so this is fine too.
58*f4a2713aSLionel Sambuc 
59*f4a2713aSLionel Sambuc   consume("foo" + kMyOperatorOverloadedEnum);
60*f4a2713aSLionel Sambuc   consume(kMyOperatorOverloadedEnum + "foo");
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc   #define A "foo"
63*f4a2713aSLionel Sambuc   #define B "bar"
64*f4a2713aSLionel Sambuc   consume(A B + sizeof(A) - 1);
65*f4a2713aSLionel Sambuc }
66*f4a2713aSLionel Sambuc 
67