blob: 46b6705f15810a9448f9f2d0cdc969b8b39e36d5 [file] [log] [blame]
Jordan Rose670941c2012-09-13 02:11:15 +00001// RUN: %clang_cc1 -triple i386-apple-darwin9 -fsyntax-only -fblocks -Wformat-non-iso -verify %s
2// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -fsyntax-only -fblocks -Wformat-non-iso -verify %s
Jordan Roseec087352012-09-05 22:56:26 +00003
Jordan Rose670941c2012-09-13 02:11:15 +00004// RUN: %clang_cc1 -triple i386-apple-darwin9 -fdiagnostics-parseable-fixits -fblocks -Wformat-non-iso %s 2>&1 | FileCheck %s
5// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -fdiagnostics-parseable-fixits -fblocks -Wformat-non-iso %s 2>&1 | FileCheck %s
Jordan Roseec087352012-09-05 22:56:26 +00006
Jordan Rose670941c2012-09-13 02:11:15 +00007// RUN: %clang_cc1 -triple i386-apple-darwin9 -fdiagnostics-parseable-fixits -fblocks -Wformat-non-iso %s 2>&1 | FileCheck -check-prefix=CHECK-32 %s
8// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -fdiagnostics-parseable-fixits -fblocks -Wformat-non-iso %s 2>&1 | FileCheck -check-prefix=CHECK-64 %s
Jordan Roseec087352012-09-05 22:56:26 +00009
10int printf(const char * restrict, ...);
11
12#if __LP64__
13typedef long NSInteger;
14typedef unsigned long NSUInteger;
15typedef int SInt32;
16typedef unsigned int UInt32;
17
18#else
19
20typedef int NSInteger;
21typedef unsigned int NSUInteger;
22typedef long SInt32;
23typedef unsigned long UInt32;
24#endif
25
26NSInteger getNSInteger();
27NSUInteger getNSUInteger();
28SInt32 getSInt32();
29UInt32 getUInt32();
30
31void testCorrectionInAllCases() {
32 printf("%s", getNSInteger()); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
33 printf("%s", getNSUInteger()); // expected-warning{{values of type 'NSUInteger' should not be used as format arguments; add an explicit cast to 'unsigned long' instead}}
34 printf("%s", getSInt32()); // expected-warning{{values of type 'SInt32' should not be used as format arguments; add an explicit cast to 'int' instead}}
35 printf("%s", getUInt32()); // expected-warning{{values of type 'UInt32' should not be used as format arguments; add an explicit cast to 'unsigned int' instead}}
36
Jordan Rosec8145bb2013-01-17 18:47:12 +000037 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:11-[[@LINE-5]]:13}:"%ld"
38 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:"(long)"
Jordan Roseec087352012-09-05 22:56:26 +000039
Jordan Rosec8145bb2013-01-17 18:47:12 +000040 // CHECK: fix-it:"{{.*}}":{[[@LINE-7]]:11-[[@LINE-7]]:13}:"%lu"
41 // CHECK: fix-it:"{{.*}}":{[[@LINE-8]]:16-[[@LINE-8]]:16}:"(unsigned long)"
Jordan Roseec087352012-09-05 22:56:26 +000042
Jordan Rosec8145bb2013-01-17 18:47:12 +000043 // CHECK: fix-it:"{{.*}}":{[[@LINE-9]]:11-[[@LINE-9]]:13}:"%d"
44 // CHECK: fix-it:"{{.*}}":{[[@LINE-10]]:16-[[@LINE-10]]:16}:"(int)"
Jordan Roseec087352012-09-05 22:56:26 +000045
Jordan Rosec8145bb2013-01-17 18:47:12 +000046 // CHECK: fix-it:"{{.*}}":{[[@LINE-11]]:11-[[@LINE-11]]:13}:"%u"
47 // CHECK: fix-it:"{{.*}}":{[[@LINE-12]]:16-[[@LINE-12]]:16}:"(unsigned int)"
Jordan Roseec087352012-09-05 22:56:26 +000048}
49
50@interface Foo {
51@public
52 NSInteger _value;
53}
54- (NSInteger)getInteger;
55
56@property NSInteger value;
57@end
58
59struct Bar {
60 NSInteger value;
61};
62
63
64void testParens(Foo *obj, struct Bar *record) {
65 NSInteger arr[4] = {0};
66 NSInteger i = 0;
67
Jordan Rose17ddc542012-12-05 18:44:44 +000068 // These cases match the relevant cases in CheckPrintfHandler::checkFormatExpr.
Jordan Roseec087352012-09-05 22:56:26 +000069 printf("%s", arr[0]); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
70 printf("%s", getNSInteger()); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
71 printf("%s", i); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
72 printf("%s", obj->_value); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
73 printf("%s", [obj getInteger]); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
74 printf("%s", obj.value); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
75 printf("%s", record->value); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
76 printf("%s", (i ? i : i)); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
77 printf("%s", *arr); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
78
79 // CHECK-NOT: fix-it:{{.*}}:")"
80
81 printf("%s", i ? i : i); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
82
Jordan Rosec8145bb2013-01-17 18:47:12 +000083 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:11-[[@LINE-2]]:13}:"%ld"
84 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:16-[[@LINE-3]]:16}:"(long)("
85 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:25-[[@LINE-4]]:25}:")"
Jordan Roseec087352012-09-05 22:56:26 +000086}
87
88
89#if __LP64__
90
91void testWarn() {
92 printf("%d", getNSInteger()); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
93 printf("%u", getNSUInteger()); // expected-warning{{values of type 'NSUInteger' should not be used as format arguments; add an explicit cast to 'unsigned long' instead}}
94 printf("%ld", getSInt32()); // expected-warning{{values of type 'SInt32' should not be used as format arguments; add an explicit cast to 'int' instead}}
95 printf("%lu", getUInt32()); // expected-warning{{values of type 'UInt32' should not be used as format arguments; add an explicit cast to 'unsigned int' instead}}
96
Jordan Rosec8145bb2013-01-17 18:47:12 +000097 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-5]]:11-[[@LINE-5]]:13}:"%ld"
98 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:"(long)"
Jordan Roseec087352012-09-05 22:56:26 +000099
Jordan Rosec8145bb2013-01-17 18:47:12 +0000100 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-7]]:11-[[@LINE-7]]:13}:"%lu"
101 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-8]]:16-[[@LINE-8]]:16}:"(unsigned long)"
Jordan Roseec087352012-09-05 22:56:26 +0000102
Jordan Rosec8145bb2013-01-17 18:47:12 +0000103 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-9]]:11-[[@LINE-9]]:14}:"%d"
104 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-10]]:17-[[@LINE-10]]:17}:"(int)"
Jordan Roseec087352012-09-05 22:56:26 +0000105
Jordan Rosec8145bb2013-01-17 18:47:12 +0000106 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-11]]:11-[[@LINE-11]]:14}:"%u"
107 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-12]]:17-[[@LINE-12]]:17}:"(unsigned int)"
Jordan Roseec087352012-09-05 22:56:26 +0000108}
109
110void testPreserveHex() {
111 printf("%x", getNSInteger()); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
112 printf("%x", getNSUInteger()); // expected-warning{{values of type 'NSUInteger' should not be used as format arguments; add an explicit cast to 'unsigned long' instead}}
113
Jordan Rosec8145bb2013-01-17 18:47:12 +0000114 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-3]]:11-[[@LINE-3]]:13}:"%lx"
115 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:"(long)"
Jordan Roseec087352012-09-05 22:56:26 +0000116
Jordan Rosec8145bb2013-01-17 18:47:12 +0000117 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-5]]:11-[[@LINE-5]]:13}:"%lx"
118 // CHECK-64: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:"(unsigned long)"
Jordan Roseec087352012-09-05 22:56:26 +0000119}
120
121void testNoWarn() {
122 printf("%ld", getNSInteger()); // no-warning
123 printf("%lu", getNSUInteger()); // no-warning
124 printf("%d", getSInt32()); // no-warning
125 printf("%u", getUInt32()); // no-warning
126}
127
128#else
129
130void testWarn() {
131 printf("%ld", getNSInteger()); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
132 printf("%lu", getNSUInteger()); // expected-warning{{values of type 'NSUInteger' should not be used as format arguments; add an explicit cast to 'unsigned long' instead}}
133 printf("%d", getSInt32()); // expected-warning{{values of type 'SInt32' should not be used as format arguments; add an explicit cast to 'int' instead}}
134 printf("%u", getUInt32()); // expected-warning{{values of type 'UInt32' should not be used as format arguments; add an explicit cast to 'unsigned int' instead}}
135
Jordan Rosec8145bb2013-01-17 18:47:12 +0000136 // CHECK-32: fix-it:"{{.*}}":{[[@LINE-5]]:17-[[@LINE-5]]:17}:"(long)"
137 // CHECK-32: fix-it:"{{.*}}":{[[@LINE-5]]:17-[[@LINE-5]]:17}:"(unsigned long)"
138 // CHECK-32: fix-it:"{{.*}}":{[[@LINE-5]]:16-[[@LINE-5]]:16}:"(int)"
139 // CHECK-32: fix-it:"{{.*}}":{[[@LINE-5]]:16-[[@LINE-5]]:16}:"(unsigned int)"
Jordan Roseec087352012-09-05 22:56:26 +0000140}
141
142void testPreserveHex() {
143 printf("%lx", getNSInteger()); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
144 printf("%lx", getNSUInteger()); // expected-warning{{values of type 'NSUInteger' should not be used as format arguments; add an explicit cast to 'unsigned long' instead}}
145
Jordan Rosec8145bb2013-01-17 18:47:12 +0000146 // CHECK-32: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:"(long)"
147 // CHECK-32: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:"(unsigned long)"
Jordan Roseec087352012-09-05 22:56:26 +0000148}
149
150void testNoWarn() {
151 printf("%d", getNSInteger()); // no-warning
152 printf("%u", getNSUInteger()); // no-warning
153 printf("%ld", getSInt32()); // no-warning
154 printf("%lu", getUInt32()); // no-warning
155}
156
157#endif
158
159
160void testCasts() {
161 printf("%s", (NSInteger)0); // expected-warning{{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
162 printf("%s", (NSUInteger)0); // expected-warning{{values of type 'NSUInteger' should not be used as format arguments; add an explicit cast to 'unsigned long' instead}}
163 printf("%s", (SInt32)0); // expected-warning{{values of type 'SInt32' should not be used as format arguments; add an explicit cast to 'int' instead}}
164 printf("%s", (UInt32)0); // expected-warning{{values of type 'UInt32' should not be used as format arguments; add an explicit cast to 'unsigned int' instead}}
165
Jordan Rosec8145bb2013-01-17 18:47:12 +0000166 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:11-[[@LINE-5]]:13}:"%ld"
167 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:27}:"(long)"
Jordan Roseec087352012-09-05 22:56:26 +0000168
Jordan Rosec8145bb2013-01-17 18:47:12 +0000169 // CHECK: fix-it:"{{.*}}":{[[@LINE-7]]:11-[[@LINE-7]]:13}:"%lu"
170 // CHECK: fix-it:"{{.*}}":{[[@LINE-8]]:16-[[@LINE-8]]:28}:"(unsigned long)"
Jordan Roseec087352012-09-05 22:56:26 +0000171
Jordan Rosec8145bb2013-01-17 18:47:12 +0000172 // CHECK: fix-it:"{{.*}}":{[[@LINE-9]]:11-[[@LINE-9]]:13}:"%d"
173 // CHECK: fix-it:"{{.*}}":{[[@LINE-10]]:16-[[@LINE-10]]:24}:"(int)"
Jordan Roseec087352012-09-05 22:56:26 +0000174
Jordan Rosec8145bb2013-01-17 18:47:12 +0000175 // CHECK: fix-it:"{{.*}}":{[[@LINE-11]]:11-[[@LINE-11]]:13}:"%u"
176 // CHECK: fix-it:"{{.*}}":{[[@LINE-12]]:16-[[@LINE-12]]:24}:"(unsigned int)"
Jordan Roseec087352012-09-05 22:56:26 +0000177}
Jordan Rose275b6f52012-09-13 02:11:03 +0000178
179void testCapitals() {
Jordan Rose670941c2012-09-13 02:11:15 +0000180 printf("%D", 1); // expected-warning{{conversion specifier is not supported by ISO C}} expected-note {{did you mean to use 'd'?}}
181 printf("%U", 1); // expected-warning{{conversion specifier is not supported by ISO C}} expected-note {{did you mean to use 'u'?}}
182 printf("%O", 1); // expected-warning{{conversion specifier is not supported by ISO C}} expected-note {{did you mean to use 'o'?}}
Jordan Rose275b6f52012-09-13 02:11:03 +0000183
Jordan Rosec8145bb2013-01-17 18:47:12 +0000184 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:12-[[@LINE-4]]:13}:"d"
185 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:12-[[@LINE-4]]:13}:"u"
186 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:12-[[@LINE-4]]:13}:"o"
Jordan Rose275b6f52012-09-13 02:11:03 +0000187
Jordan Rose670941c2012-09-13 02:11:15 +0000188
189 printf("%lD", 1); // expected-warning{{conversion specifier is not supported by ISO C}} expected-note {{did you mean to use 'd'?}} expected-warning{{format specifies type 'long' but the argument has type 'int'}}
190
191 // FIXME: offering two somewhat-conflicting fixits is less than ideal.
Jordan Rosec8145bb2013-01-17 18:47:12 +0000192 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:13-[[@LINE-3]]:14}:"d"
193 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:11-[[@LINE-4]]:14}:"%D"
Jordan Rose275b6f52012-09-13 02:11:03 +0000194}