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