1 // RUN: %clang_cc1 -fsyntax-only %s 2>&1 | FileCheck %s -strict-whitespace
2 
3 #define M1(x) x
4 #define M2 1;
foo()5 void foo() {
6   M1(
7     M2);
8   // CHECK: {{.*}}:7:{{[0-9]+}}: warning: expression result unused
9   // CHECK: {{.*}}:4:{{[0-9]+}}: note: expanded from macro 'M2'
10   // CHECK: {{.*}}:3:{{[0-9]+}}: note: expanded from macro 'M1'
11 }
12 
13 #define A(x) x
14 #define B(x) A(x)
15 #define C(x) B(x)
bar()16 void bar() {
17   C(1);
18   // CHECK: {{.*}}:17:5: warning: expression result unused
19 }
20 
21 // rdar://7597492
22 #define sprintf(str, A, B) \
23 __builtin___sprintf_chk (str, 0, 42, A, B)
24 
baz(char * Msg)25 void baz(char *Msg) {
26   sprintf(Msg,  "  sizeof FoooLib            : =%3u\n",   12LL);
27 }
28 
29 
30 // PR9279: comprehensive tests for multi-level macro back traces
31 #define macro_args1(x) x
32 #define macro_args2(x) macro_args1(x)
33 #define macro_args3(x) macro_args2(x)
34 
35 #define macro_many_args1(x, y, z) y
36 #define macro_many_args2(x, y, z) macro_many_args1(x, y, z)
37 #define macro_many_args3(x, y, z) macro_many_args2(x, y, z)
38 
test()39 void test() {
40   macro_args3(11);
41   // CHECK: {{.*}}:40:15: warning: expression result unused
42   // Also check that the 'caret' printing agrees with the location here where
43   // its easy to FileCheck.
44   // CHECK-NEXT:      macro_args3(11);
45   // CHECK-NEXT: {{^              \^~}}
46 
47   macro_many_args3(
48     1,
49     2,
50     3);
51   // CHECK: {{.*}}:49:5: warning: expression result unused
52   // CHECK: {{.*}}:37:55: note: expanded from macro 'macro_many_args3'
53   // CHECK: {{.*}}:36:55: note: expanded from macro 'macro_many_args2'
54   // CHECK: {{.*}}:35:35: note: expanded from macro 'macro_many_args1'
55 
56   macro_many_args3(
57     1,
58     M2,
59     3);
60   // CHECK: {{.*}}:58:5: warning: expression result unused
61   // CHECK: {{.*}}:4:12: note: expanded from macro 'M2'
62   // CHECK: {{.*}}:37:55: note: expanded from macro 'macro_many_args3'
63   // CHECK: {{.*}}:36:55: note: expanded from macro 'macro_many_args2'
64   // CHECK: {{.*}}:35:35: note: expanded from macro 'macro_many_args1'
65 
66   macro_many_args3(
67     1,
68     macro_args2(22),
69     3);
70   // CHECK: {{.*}}:68:17: warning: expression result unused
71   // This caret location needs to be printed *inside* a different macro's
72   // arguments.
73   // CHECK-NEXT:        macro_args2(22),
74   // CHECK-NEXT: {{^                \^~}}
75   // CHECK: {{.*}}:32:36: note: expanded from macro 'macro_args2'
76   // CHECK: {{.*}}:31:24: note: expanded from macro 'macro_args1'
77   // CHECK: {{.*}}:37:55: note: expanded from macro 'macro_many_args3'
78   // CHECK: {{.*}}:36:55: note: expanded from macro 'macro_many_args2'
79   // CHECK: {{.*}}:35:35: note: expanded from macro 'macro_many_args1'
80 }
81 
82 #define variadic_args1(x, y, ...) y
83 #define variadic_args2(x, ...) variadic_args1(x, __VA_ARGS__)
84 #define variadic_args3(x, y, ...) variadic_args2(x, y, __VA_ARGS__)
85 
test2()86 void test2() {
87   variadic_args3(1, 22, 3, 4);
88   // CHECK: {{.*}}:87:21: warning: expression result unused
89   // CHECK-NEXT:      variadic_args3(1, 22, 3, 4);
90   // CHECK-NEXT: {{^                    \^~}}
91   // CHECK: {{.*}}:84:53: note: expanded from macro 'variadic_args3'
92   // CHECK: {{.*}}:83:50: note: expanded from macro 'variadic_args2'
93   // CHECK: {{.*}}:82:35: note: expanded from macro 'variadic_args1'
94 }
95 
96 #define variadic_pasting_args1(x, y, z) y
97 #define variadic_pasting_args2(x, ...) variadic_pasting_args1(x ## __VA_ARGS__)
98 #define variadic_pasting_args2a(x, y, ...) variadic_pasting_args1(x, y ## __VA_ARGS__)
99 #define variadic_pasting_args3(x, y, ...) variadic_pasting_args2(x, y, __VA_ARGS__)
100 #define variadic_pasting_args3a(x, y, ...) variadic_pasting_args2a(x, y, __VA_ARGS__)
101 
test3()102 void test3() {
103   variadic_pasting_args3(1, 2, 3, 4);
104   // CHECK: {{.*}}:103:32: warning: expression result unused
105   // CHECK: {{.*}}:99:72: note: expanded from macro 'variadic_pasting_args3'
106   // CHECK: {{.*}}:97:68: note: expanded from macro 'variadic_pasting_args2'
107   // CHECK: {{.*}}:96:41: note: expanded from macro 'variadic_pasting_args1'
108 
109   variadic_pasting_args3a(1, 2, 3, 4);
110   // CHECK:        {{.*}}:109:3: warning: expression result unused
111   // CHECK-NEXT:     variadic_pasting_args3a(1, 2, 3, 4);
112   // CHECK-NEXT: {{  \^~~~~~~~~~~~~~~~~~~~~~~}}
113   // CHECK:        {{.*}}:100:44: note: expanded from macro 'variadic_pasting_args3a'
114   // CHECK-NEXT:   #define variadic_pasting_args3a(x, y, ...) variadic_pasting_args2a(x, y, __VA_ARGS__)
115   // CHECK-NEXT: {{                                           \^~~~~~~~~~~~~~~~~~~~~~~}}
116   // CHECK:        {{.*}}:98:70: note: expanded from macro 'variadic_pasting_args2a'
117   // CHECK-NEXT:   #define variadic_pasting_args2a(x, y, ...) variadic_pasting_args1(x, y ## __VA_ARGS__)
118   // CHECK-NEXT: {{                                                                     \^~~~~~~~~~~~~~~~}}
119   // CHECK:        {{.*}}:96:41: note: expanded from macro 'variadic_pasting_args1'
120   // CHECK-NEXT:   #define variadic_pasting_args1(x, y, z) y
121   // CHECK-NEXT: {{                                        \^}}
122 }
123 
124 #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
125 int test4 = BAD_CONDITIONAL_OPERATOR+BAD_CONDITIONAL_OPERATOR;
126 // CHECK:         {{.*}}:124:39: note: expanded from macro 'BAD_CONDITIONAL_OPERATOR'
127 // CHECK-NEXT:    #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
128 // CHECK-NEXT: {{^                                      \^}}
129 // CHECK:         {{.*}}:124:39: note: expanded from macro 'BAD_CONDITIONAL_OPERATOR'
130 // CHECK-NEXT:    #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
131 // CHECK-NEXT: {{^                                      \^}}
132 // CHECK:         {{.*}}:124:39: note: expanded from macro 'BAD_CONDITIONAL_OPERATOR'
133 // CHECK-NEXT:    #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
134 // CHECK-NEXT: {{^                                 ~~~~~\^~~~}}
135 
136 #define QMARK ?
137 #define TWOL (2<
138 #define X 1+TWOL 3) QMARK 4:5
139 int x = X;
140 // CHECK:         {{.*}}:139:9: note: place parentheses around the '+' expression to silence this warning
141 // CHECK-NEXT:    int x = X;
142 // CHECK-NEXT: {{^        \^}}
143 // CHECK-NEXT:    {{.*}}:138:21: note: expanded from macro 'X'
144 // CHECK-NEXT:    #define X 1+TWOL 3) QMARK 4:5
145 // CHECK-NEXT: {{^          ~~~~~~~~~ \^}}
146 // CHECK-NEXT:    {{.*}}:136:15: note: expanded from macro 'QMARK'
147 // CHECK-NEXT:    #define QMARK ?
148 // CHECK-NEXT: {{^              \^}}
149 // CHECK-NEXT:    {{.*}}:139:9: note: place parentheses around the '?:' expression to evaluate it first
150 // CHECK-NEXT:    int x = X;
151 // CHECK-NEXT: {{^        \^}}
152 // CHECK-NEXT:    {{.*}}:138:21: note: expanded from macro 'X'
153 // CHECK-NEXT:    #define X 1+TWOL 3) QMARK 4:5
154 // CHECK-NEXT: {{^            ~~~~~~~~\^~~~~~~~~}}
155 
156 #define ONEPLUS 1+
157 #define Y ONEPLUS (2<3) QMARK 4:5
158 int y = Y;
159 // CHECK:         {{.*}}:158:9: warning: operator '?:' has lower precedence than '+'; '+' will be evaluated first
160 // CHECK-NEXT:    int y = Y;
161 // CHECK-NEXT: {{^        \^}}
162 // CHECK-NEXT:    {{.*}}:157:25: note: expanded from macro 'Y'
163 // CHECK-NEXT:    #define Y ONEPLUS (2<3) QMARK 4:5
164 // CHECK-NEXT: {{^          ~~~~~~~~~~~~~ \^}}
165 // CHECK-NEXT:    {{.*}}:136:15: note: expanded from macro 'QMARK'
166 // CHECK-NEXT:    #define QMARK ?
167 // CHECK-NEXT: {{^              \^}}
168 
169 // PR14399
170 void iequals(int,int,int);
foo_aa(char * s)171 void foo_aa(char* s)
172 {
173 #define /* */ BARC(c, /* */b, a) (a + b ? c : c)
174   iequals(__LINE__, BARC(123, (456 < 345), 789), 8);
175 }
176 // CHECK:         {{.*}}:174:21: warning: operator '?:' has lower precedence than '+'
177 // CHECK-NEXT:      iequals(__LINE__, BARC(123, (456 < 345), 789), 8);
178 // CHECK-NEXT: {{^                    \^~~~~~~~~~~~~~~~~~~~~~~~~~~}}
179 // CHECK-NEXT:    {{.*}}:173:41: note: expanded from macro 'BARC'
180 // CHECK-NEXT:    #define /* */ BARC(c, /* */b, a) (a + b ? c : c)
181 // CHECK-NEXT: {{^                                  ~~~~~ \^}}
182 
183 #define APPEND2(NUM, SUFF) -1 != NUM ## SUFF
184 #define APPEND(NUM, SUFF) APPEND2(NUM, SUFF)
185 #define UTARG_MAX_U APPEND (MAX_UINT, UL)
186 #define MAX_UINT 18446744073709551615
187 #if UTARG_MAX_U
188 #endif
189 
190 // CHECK:         {{.*}}:187:5: warning: left side of operator converted from negative value to unsigned: -1 to 18446744073709551615
191 // CHECK-NEXT:    #if UTARG_MAX_U
192 // CHECK-NEXT: {{^    \^~~~~~~~~~~}}
193 // CHECK-NEXT:    {{.*}}:185:21: note: expanded from macro 'UTARG_MAX_U'
194 // CHECK-NEXT:    #define UTARG_MAX_U APPEND (MAX_UINT, UL)
195 // CHECK-NEXT: {{^                    \^~~~~~~~~~~~~~~~~~~~~}}
196 // CHECK-NEXT:    {{.*}}:184:27: note: expanded from macro 'APPEND'
197 // CHECK-NEXT:    #define APPEND(NUM, SUFF) APPEND2(NUM, SUFF)
198 // CHECK-NEXT: {{^                          \^~~~~~~~~~~~~~~~~~}}
199 // CHECK-NEXT:    {{.*}}:183:31: note: expanded from macro 'APPEND2'
200 // CHECK-NEXT:    #define APPEND2(NUM, SUFF) -1 != NUM ## SUFF
201 // CHECK-NEXT: {{^                           ~~ \^  ~~~~~~~~~~~}}
202 
203 unsigned long strlen_test(const char *s);
204 #define __darwin_obsz(object) __builtin_object_size (object, 1)
205 #define sprintf2(str, ...) \
206   __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
207 #define Cstrlen(a)  strlen_test(a)
208 #define Csprintf    sprintf2
f(char * pMsgBuf,char * pKeepBuf)209 void f(char* pMsgBuf, char* pKeepBuf) {
210 Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ", strlen_test(pKeepBuf));
211 // FIXME: Change test to use 'Cstrlen' instead of 'strlen_test' when macro printing is fixed.
212 }
213 // CHECK:         {{.*}}:210:62: warning: format specifies type 'int' but the argument has type 'unsigned long'
214 // CHECK-NEXT:    Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ", strlen_test(pKeepBuf));
215 // CHECK-NEXT: {{^                                                    ~~~      \^~~~~~~~~~~~~~~~~~~~~}}
216 // CHECK-NEXT: {{^                                                    %1lu}}
217 // CHECK-NEXT:    {{.*}}:208:21: note: expanded from macro 'Csprintf'
218 // CHECK-NEXT:    #define Csprintf    sprintf2
219 // CHECK-NEXT: {{^                    \^}}
220 // CHECK-NEXT:    {{.*}}:206:56: note: expanded from macro 'sprintf2'
221 // CHECK-NEXT:      __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
222 // CHECK-NEXT: {{^                                                       \^~~~~~~~~~~}}
223 
224 #define SWAP_AND_APPLY(arg, macro) macro arg
225 #define APPLY(macro, arg) macro arg
226 #define DECLARE_HELPER() __builtin_printf("%d\n", mylong);
use_evil_macros(long mylong)227 void use_evil_macros(long mylong) {
228   SWAP_AND_APPLY((), DECLARE_HELPER)
229   APPLY(DECLARE_HELPER, ())
230 }
231 // CHECK:      {{.*}}:228:22: warning: format specifies type 'int' but the argument has type 'long'
232 // CHECK-NEXT:   SWAP_AND_APPLY((), DECLARE_HELPER)
233 // CHECK-NEXT:   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
234 // CHECK-NEXT: {{.*}}:224:36: note: expanded from macro 'SWAP_AND_APPLY'
235 // CHECK-NEXT: #define SWAP_AND_APPLY(arg, macro) macro arg
236 // CHECK-NEXT:                                    ^~~~~~~~~
237 // CHECK-NEXT: {{.*}}:226:51: note: expanded from macro 'DECLARE_HELPER'
238 // CHECK-NEXT: #define DECLARE_HELPER() __builtin_printf("%d\n", mylong);
239 // CHECK-NEXT:                                            ~~     ^~~~~~
240 // CHECK-NEXT: {{.*}}:229:9: warning: format specifies type 'int' but the argument has type 'long'
241 // CHECK-NEXT:   APPLY(DECLARE_HELPER, ())
242 // CHECK-NEXT:   ~~~~~~^~~~~~~~~~~~~~~~~~~
243 // CHECK-NEXT: {{.*}}:225:27: note: expanded from macro 'APPLY'
244 // CHECK-NEXT: #define APPLY(macro, arg) macro arg
245 // CHECK-NEXT:                           ^~~~~~~~~
246 // CHECK-NEXT: {{.*}}:226:51: note: expanded from macro 'DECLARE_HELPER'
247 // CHECK-NEXT: #define DECLARE_HELPER() __builtin_printf("%d\n", mylong);
248 // CHECK-NEXT:                                            ~~     ^~~~~~
249