]>
Commit | Line | Data |
---|---|---|
0f4cea33 | 1 | /* Diagnostic routines shared by all languages that are variants of C. |
aad93da1 | 2 | Copyright (C) 1992-2017 Free Software Foundation, Inc. |
0f4cea33 | 3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING3. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
23 | #include "target.h" | |
24 | #include "function.h" | |
25 | #include "tree.h" | |
26 | #include "c-common.h" | |
ad7b10a2 | 27 | #include "memmodel.h" |
0f4cea33 | 28 | #include "tm_p.h" |
29 | #include "diagnostic.h" | |
30 | #include "intl.h" | |
30a86690 | 31 | #include "stringpool.h" |
32 | #include "attribs.h" | |
629b6abc | 33 | #include "asan.h" |
9bf6a8e0 | 34 | #include "gcc-rich-location.h" |
1a2d3e8e | 35 | #include "gimplify.h" |
2bfb0686 | 36 | #include "c-family/c-indentation.h" |
0f4cea33 | 37 | |
38 | /* Print a warning if a constant expression had overflow in folding. | |
39 | Invoke this function on every expression that the language | |
40 | requires to be a constant expression. | |
41 | Note the ANSI C standard says it is erroneous for a | |
42 | constant expression to overflow. */ | |
43 | ||
44 | void | |
45 | constant_expression_warning (tree value) | |
46 | { | |
47 | if (warn_overflow && pedantic | |
48 | && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST | |
49 | || TREE_CODE (value) == FIXED_CST | |
50 | || TREE_CODE (value) == VECTOR_CST | |
51 | || TREE_CODE (value) == COMPLEX_CST) | |
52 | && TREE_OVERFLOW (value)) | |
53 | pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); | |
54 | } | |
55 | ||
56 | /* The same as above but print an unconditional error. */ | |
57 | ||
58 | void | |
59 | constant_expression_error (tree value) | |
60 | { | |
61 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST | |
62 | || TREE_CODE (value) == FIXED_CST | |
63 | || TREE_CODE (value) == VECTOR_CST | |
64 | || TREE_CODE (value) == COMPLEX_CST) | |
65 | && TREE_OVERFLOW (value)) | |
66 | error ("overflow in constant expression"); | |
67 | } | |
68 | ||
645b0f8d | 69 | /* Print a warning if an expression result VALUE had an overflow |
70 | in folding and its operands hadn't. EXPR, which may be null, is | |
71 | the operand of the expression. | |
0f4cea33 | 72 | |
73 | Invoke this function on every expression that | |
74 | (1) appears in the source code, and | |
75 | (2) is a constant expression that overflowed, and | |
76 | (3) is not already checked by convert_and_check; | |
77 | however, do not invoke this function on operands of explicit casts | |
78 | or when the expression is the result of an operator and any operand | |
79 | already overflowed. */ | |
80 | ||
81 | void | |
645b0f8d | 82 | overflow_warning (location_t loc, tree value, tree expr) |
0f4cea33 | 83 | { |
84 | if (c_inhibit_evaluation_warnings != 0) | |
85 | return; | |
86 | ||
645b0f8d | 87 | const char *warnfmt = NULL; |
88 | ||
0f4cea33 | 89 | switch (TREE_CODE (value)) |
90 | { | |
91 | case INTEGER_CST: | |
645b0f8d | 92 | warnfmt = (expr |
93 | ? G_("integer overflow in expression %qE of type %qT " | |
94 | "results in %qE") | |
95 | : G_("integer overflow in expression of type %qT " | |
96 | "results in %qE")); | |
0f4cea33 | 97 | break; |
98 | ||
99 | case REAL_CST: | |
645b0f8d | 100 | warnfmt = (expr |
101 | ? G_ ("floating point overflow in expression %qE " | |
102 | "of type %qT results in %qE") | |
103 | : G_ ("floating point overflow in expression of type %qT " | |
104 | "results in %qE")); | |
0f4cea33 | 105 | break; |
106 | ||
107 | case FIXED_CST: | |
645b0f8d | 108 | warnfmt = (expr |
109 | ? G_("fixed-point overflow in expression %qE of type %qT " | |
110 | "results in %qE") | |
111 | : G_("fixed-point overflow in expression of type %qT " | |
112 | "results in %qE")); | |
0f4cea33 | 113 | break; |
114 | ||
115 | case VECTOR_CST: | |
645b0f8d | 116 | warnfmt = (expr |
117 | ? G_("vector overflow in expression %qE of type %qT " | |
118 | "results in %qE") | |
119 | : G_("vector overflow in expression of type %qT " | |
120 | "results in %qE")); | |
0f4cea33 | 121 | break; |
122 | ||
123 | case COMPLEX_CST: | |
124 | if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) | |
645b0f8d | 125 | warnfmt = (expr |
126 | ? G_("complex integer overflow in expression %qE " | |
127 | "of type %qT results in %qE") | |
128 | : G_("complex integer overflow in expression of type %qT " | |
129 | "results in %qE")); | |
0f4cea33 | 130 | else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) |
645b0f8d | 131 | warnfmt = (expr |
132 | ? G_("complex floating point overflow in expression %qE " | |
133 | "of type %qT results in %qE") | |
134 | : G_("complex floating point overflow in expression " | |
135 | "of type %qT results in %qE")); | |
136 | else | |
137 | return; | |
0f4cea33 | 138 | break; |
139 | ||
140 | default: | |
645b0f8d | 141 | return; |
0f4cea33 | 142 | } |
645b0f8d | 143 | |
144 | if (expr) | |
145 | warning_at (loc, OPT_Woverflow, warnfmt, expr, TREE_TYPE (expr), value); | |
146 | else | |
147 | warning_at (loc, OPT_Woverflow, warnfmt, TREE_TYPE (value), value); | |
148 | ||
149 | TREE_NO_WARNING (value) = 1; | |
0f4cea33 | 150 | } |
151 | ||
1a2d3e8e | 152 | /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression |
153 | pointed to by TP. */ | |
154 | ||
155 | static tree | |
156 | unwrap_c_maybe_const (tree *tp, int *walk_subtrees, void *) | |
157 | { | |
158 | if (TREE_CODE (*tp) == C_MAYBE_CONST_EXPR) | |
159 | { | |
160 | *tp = C_MAYBE_CONST_EXPR_EXPR (*tp); | |
161 | /* C_MAYBE_CONST_EXPRs don't nest. */ | |
162 | *walk_subtrees = false; | |
163 | } | |
164 | return NULL_TREE; | |
165 | } | |
166 | ||
0f4cea33 | 167 | /* Warn about uses of logical || / && operator in a context where it |
168 | is likely that the bitwise equivalent was intended by the | |
169 | programmer. We have seen an expression in which CODE is a binary | |
170 | operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding | |
171 | had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ | |
172 | ||
173 | void | |
174 | warn_logical_operator (location_t location, enum tree_code code, tree type, | |
175 | enum tree_code code_left, tree op_left, | |
176 | enum tree_code ARG_UNUSED (code_right), tree op_right) | |
177 | { | |
178 | int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); | |
179 | int in0_p, in1_p, in_p; | |
180 | tree low0, low1, low, high0, high1, high, lhs, rhs, tem; | |
181 | bool strict_overflow_p = false; | |
182 | ||
183 | if (code != TRUTH_ANDIF_EXPR | |
184 | && code != TRUTH_AND_EXPR | |
185 | && code != TRUTH_ORIF_EXPR | |
186 | && code != TRUTH_OR_EXPR) | |
187 | return; | |
188 | ||
189 | /* We don't want to warn if either operand comes from a macro | |
190 | expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet; | |
191 | see PR61534. */ | |
192 | if (from_macro_expansion_at (EXPR_LOCATION (op_left)) | |
193 | || from_macro_expansion_at (EXPR_LOCATION (op_right))) | |
194 | return; | |
195 | ||
196 | /* Warn if &&/|| are being used in a context where it is | |
197 | likely that the bitwise equivalent was intended by the | |
198 | programmer. That is, an expression such as op && MASK | |
199 | where op should not be any boolean expression, nor a | |
200 | constant, and mask seems to be a non-boolean integer constant. */ | |
201 | if (TREE_CODE (op_right) == CONST_DECL) | |
202 | /* An enumerator counts as a constant. */ | |
203 | op_right = DECL_INITIAL (op_right); | |
204 | if (!truth_value_p (code_left) | |
205 | && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) | |
206 | && !CONSTANT_CLASS_P (op_left) | |
207 | && !TREE_NO_WARNING (op_left) | |
208 | && TREE_CODE (op_right) == INTEGER_CST | |
209 | && !integer_zerop (op_right) | |
210 | && !integer_onep (op_right)) | |
211 | { | |
212 | if (or_op) | |
213 | warning_at (location, OPT_Wlogical_op, "logical %<or%>" | |
214 | " applied to non-boolean constant"); | |
215 | else | |
216 | warning_at (location, OPT_Wlogical_op, "logical %<and%>" | |
217 | " applied to non-boolean constant"); | |
218 | TREE_NO_WARNING (op_left) = true; | |
219 | return; | |
220 | } | |
221 | ||
222 | /* We do not warn for constants because they are typical of macro | |
223 | expansions that test for features. */ | |
224 | if (CONSTANT_CLASS_P (fold_for_warn (op_left)) | |
225 | || CONSTANT_CLASS_P (fold_for_warn (op_right))) | |
226 | return; | |
227 | ||
228 | /* This warning only makes sense with logical operands. */ | |
229 | if (!(truth_value_p (TREE_CODE (op_left)) | |
230 | || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) | |
231 | || !(truth_value_p (TREE_CODE (op_right)) | |
232 | || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) | |
233 | return; | |
234 | ||
235 | /* The range computations only work with scalars. */ | |
236 | if (VECTOR_TYPE_P (TREE_TYPE (op_left)) | |
237 | || VECTOR_TYPE_P (TREE_TYPE (op_right))) | |
238 | return; | |
239 | ||
240 | /* We first test whether either side separately is trivially true | |
241 | (with OR) or trivially false (with AND). If so, do not warn. | |
242 | This is a common idiom for testing ranges of data types in | |
243 | portable code. */ | |
1a2d3e8e | 244 | op_left = unshare_expr (op_left); |
245 | walk_tree_without_duplicates (&op_left, unwrap_c_maybe_const, NULL); | |
0f4cea33 | 246 | lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); |
247 | if (!lhs) | |
248 | return; | |
0f4cea33 | 249 | |
250 | /* If this is an OR operation, invert both sides; now, the result | |
251 | should be always false to get a warning. */ | |
252 | if (or_op) | |
253 | in0_p = !in0_p; | |
254 | ||
255 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); | |
256 | if (tem && integer_zerop (tem)) | |
257 | return; | |
258 | ||
1a2d3e8e | 259 | op_right = unshare_expr (op_right); |
260 | walk_tree_without_duplicates (&op_right, unwrap_c_maybe_const, NULL); | |
0f4cea33 | 261 | rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); |
262 | if (!rhs) | |
263 | return; | |
0f4cea33 | 264 | |
265 | /* If this is an OR operation, invert both sides; now, the result | |
266 | should be always false to get a warning. */ | |
267 | if (or_op) | |
268 | in1_p = !in1_p; | |
269 | ||
270 | tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); | |
271 | if (tem && integer_zerop (tem)) | |
272 | return; | |
273 | ||
274 | /* If both expressions have the same operand, if we can merge the | |
275 | ranges, ... */ | |
276 | if (operand_equal_p (lhs, rhs, 0) | |
277 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, | |
278 | in1_p, low1, high1)) | |
279 | { | |
280 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high); | |
281 | /* ... and if the range test is always false, then warn. */ | |
282 | if (tem && integer_zerop (tem)) | |
283 | { | |
284 | if (or_op) | |
285 | warning_at (location, OPT_Wlogical_op, | |
286 | "logical %<or%> of collectively exhaustive tests is " | |
287 | "always true"); | |
288 | else | |
289 | warning_at (location, OPT_Wlogical_op, | |
290 | "logical %<and%> of mutually exclusive tests is " | |
291 | "always false"); | |
292 | } | |
293 | /* Or warn if the operands have exactly the same range, e.g. | |
294 | A > 0 && A > 0. */ | |
295 | else if (tree_int_cst_equal (low0, low1) | |
296 | && tree_int_cst_equal (high0, high1)) | |
297 | { | |
298 | if (or_op) | |
299 | warning_at (location, OPT_Wlogical_op, | |
300 | "logical %<or%> of equal expressions"); | |
301 | else | |
302 | warning_at (location, OPT_Wlogical_op, | |
303 | "logical %<and%> of equal expressions"); | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
308 | /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs | |
309 | with constant indices. */ | |
310 | ||
311 | static tree | |
876d4bc9 | 312 | find_array_ref_with_const_idx_r (tree *expr_p, int *, void *) |
0f4cea33 | 313 | { |
314 | tree expr = *expr_p; | |
315 | ||
316 | if ((TREE_CODE (expr) == ARRAY_REF | |
317 | || TREE_CODE (expr) == ARRAY_RANGE_REF) | |
318 | && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST) | |
876d4bc9 | 319 | return integer_type_node; |
0f4cea33 | 320 | |
321 | return NULL_TREE; | |
322 | } | |
323 | ||
324 | /* Warn if a self-comparison always evaluates to true or false. LOC | |
325 | is the location of the comparison with code CODE, LHS and RHS are | |
326 | operands of the comparison. */ | |
327 | ||
328 | void | |
329 | warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs) | |
330 | { | |
331 | if (TREE_CODE_CLASS (code) != tcc_comparison) | |
332 | return; | |
333 | ||
334 | /* Don't warn for various macro expansions. */ | |
335 | if (from_macro_expansion_at (loc) | |
336 | || from_macro_expansion_at (EXPR_LOCATION (lhs)) | |
337 | || from_macro_expansion_at (EXPR_LOCATION (rhs))) | |
338 | return; | |
339 | ||
340 | /* We do not warn for constants because they are typical of macro | |
341 | expansions that test for features, sizeof, and similar. */ | |
342 | if (CONSTANT_CLASS_P (fold_for_warn (lhs)) | |
343 | || CONSTANT_CLASS_P (fold_for_warn (rhs))) | |
344 | return; | |
345 | ||
346 | /* Don't warn for e.g. | |
347 | HOST_WIDE_INT n; | |
348 | ... | |
349 | if (n == (long) n) ... | |
350 | */ | |
351 | if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR) | |
352 | || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR)) | |
353 | return; | |
354 | ||
355 | /* Don't warn if either LHS or RHS has an IEEE floating-point type. | |
356 | It could be a NaN, and NaN never compares equal to anything, even | |
357 | itself. */ | |
358 | if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs))) | |
359 | return; | |
360 | ||
361 | if (operand_equal_p (lhs, rhs, 0)) | |
362 | { | |
363 | /* Don't warn about array references with constant indices; | |
364 | these are likely to come from a macro. */ | |
876d4bc9 | 365 | if (walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r, |
366 | NULL)) | |
0f4cea33 | 367 | return; |
368 | const bool always_true = (code == EQ_EXPR || code == LE_EXPR | |
369 | || code == GE_EXPR || code == UNLE_EXPR | |
370 | || code == UNGE_EXPR || code == UNEQ_EXPR); | |
371 | if (always_true) | |
372 | warning_at (loc, OPT_Wtautological_compare, | |
373 | "self-comparison always evaluates to true"); | |
374 | else | |
375 | warning_at (loc, OPT_Wtautological_compare, | |
376 | "self-comparison always evaluates to false"); | |
377 | } | |
378 | } | |
379 | ||
380 | /* Return true iff EXPR only contains boolean operands, or comparisons. */ | |
381 | ||
382 | static bool | |
383 | expr_has_boolean_operands_p (tree expr) | |
384 | { | |
385 | STRIP_NOPS (expr); | |
386 | ||
387 | if (CONVERT_EXPR_P (expr)) | |
388 | return bool_promoted_to_int_p (expr); | |
389 | else if (UNARY_CLASS_P (expr)) | |
390 | return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0)); | |
391 | else if (BINARY_CLASS_P (expr)) | |
392 | return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0)) | |
393 | && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1))); | |
394 | else if (COMPARISON_CLASS_P (expr)) | |
395 | return true; | |
396 | else | |
397 | return false; | |
398 | } | |
399 | ||
400 | /* Warn about logical not used on the left hand side operand of a comparison. | |
401 | This function assumes that the LHS is inside of TRUTH_NOT_EXPR. | |
402 | Do not warn if RHS is of a boolean type, a logical operator, or | |
403 | a comparison. */ | |
404 | ||
405 | void | |
406 | warn_logical_not_parentheses (location_t location, enum tree_code code, | |
407 | tree lhs, tree rhs) | |
408 | { | |
409 | if (TREE_CODE_CLASS (code) != tcc_comparison | |
410 | || TREE_TYPE (rhs) == NULL_TREE | |
411 | || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE | |
412 | || truth_value_p (TREE_CODE (rhs))) | |
413 | return; | |
414 | ||
415 | /* Don't warn for expression like !x == ~(bool1 | bool2). */ | |
416 | if (expr_has_boolean_operands_p (rhs)) | |
417 | return; | |
418 | ||
419 | /* Don't warn for !x == 0 or !y != 0, those are equivalent to | |
420 | !(x == 0) or !(y != 0). */ | |
421 | if ((code == EQ_EXPR || code == NE_EXPR) | |
422 | && integer_zerop (rhs)) | |
423 | return; | |
424 | ||
425 | if (warning_at (location, OPT_Wlogical_not_parentheses, | |
426 | "logical not is only applied to the left hand side of " | |
427 | "comparison") | |
428 | && EXPR_HAS_LOCATION (lhs)) | |
429 | { | |
430 | location_t lhs_loc = EXPR_LOCATION (lhs); | |
431 | rich_location richloc (line_table, lhs_loc); | |
432 | richloc.add_fixit_insert_before (lhs_loc, "("); | |
433 | richloc.add_fixit_insert_after (lhs_loc, ")"); | |
434 | inform_at_rich_loc (&richloc, "add parentheses around left hand side " | |
435 | "expression to silence this warning"); | |
436 | } | |
437 | } | |
438 | ||
439 | /* Warn if EXP contains any computations whose results are not used. | |
440 | Return true if a warning is printed; false otherwise. LOCUS is the | |
441 | (potential) location of the expression. */ | |
442 | ||
443 | bool | |
444 | warn_if_unused_value (const_tree exp, location_t locus) | |
445 | { | |
446 | restart: | |
447 | if (TREE_USED (exp) || TREE_NO_WARNING (exp)) | |
448 | return false; | |
449 | ||
450 | /* Don't warn about void constructs. This includes casting to void, | |
451 | void function calls, and statement expressions with a final cast | |
452 | to void. */ | |
453 | if (VOID_TYPE_P (TREE_TYPE (exp))) | |
454 | return false; | |
455 | ||
456 | if (EXPR_HAS_LOCATION (exp)) | |
457 | locus = EXPR_LOCATION (exp); | |
458 | ||
459 | switch (TREE_CODE (exp)) | |
460 | { | |
461 | case PREINCREMENT_EXPR: | |
462 | case POSTINCREMENT_EXPR: | |
463 | case PREDECREMENT_EXPR: | |
464 | case POSTDECREMENT_EXPR: | |
465 | case MODIFY_EXPR: | |
466 | case INIT_EXPR: | |
467 | case TARGET_EXPR: | |
468 | case CALL_EXPR: | |
469 | case TRY_CATCH_EXPR: | |
0f4cea33 | 470 | case EXIT_EXPR: |
471 | case VA_ARG_EXPR: | |
472 | return false; | |
473 | ||
474 | case BIND_EXPR: | |
475 | /* For a binding, warn if no side effect within it. */ | |
476 | exp = BIND_EXPR_BODY (exp); | |
477 | goto restart; | |
478 | ||
479 | case SAVE_EXPR: | |
480 | case NON_LVALUE_EXPR: | |
481 | case NOP_EXPR: | |
482 | exp = TREE_OPERAND (exp, 0); | |
483 | goto restart; | |
484 | ||
485 | case TRUTH_ORIF_EXPR: | |
486 | case TRUTH_ANDIF_EXPR: | |
487 | /* In && or ||, warn if 2nd operand has no side effect. */ | |
488 | exp = TREE_OPERAND (exp, 1); | |
489 | goto restart; | |
490 | ||
491 | case COMPOUND_EXPR: | |
492 | if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) | |
493 | return true; | |
494 | /* Let people do `(foo (), 0)' without a warning. */ | |
495 | if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) | |
496 | return false; | |
497 | exp = TREE_OPERAND (exp, 1); | |
498 | goto restart; | |
499 | ||
500 | case COND_EXPR: | |
501 | /* If this is an expression with side effects, don't warn; this | |
502 | case commonly appears in macro expansions. */ | |
503 | if (TREE_SIDE_EFFECTS (exp)) | |
504 | return false; | |
505 | goto warn; | |
506 | ||
507 | case INDIRECT_REF: | |
508 | /* Don't warn about automatic dereferencing of references, since | |
509 | the user cannot control it. */ | |
510 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) | |
511 | { | |
512 | exp = TREE_OPERAND (exp, 0); | |
513 | goto restart; | |
514 | } | |
515 | /* Fall through. */ | |
516 | ||
517 | default: | |
518 | /* Referencing a volatile value is a side effect, so don't warn. */ | |
519 | if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) | |
520 | && TREE_THIS_VOLATILE (exp)) | |
521 | return false; | |
522 | ||
523 | /* If this is an expression which has no operands, there is no value | |
524 | to be unused. There are no such language-independent codes, | |
525 | but front ends may define such. */ | |
526 | if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) | |
527 | return false; | |
528 | ||
529 | warn: | |
530 | return warning_at (locus, OPT_Wunused_value, "value computed is not used"); | |
531 | } | |
532 | } | |
533 | ||
534 | /* Print a warning about casts that might indicate violation | |
535 | of strict aliasing rules if -Wstrict-aliasing is used and | |
536 | strict aliasing mode is in effect. OTYPE is the original | |
537 | TREE_TYPE of EXPR, and TYPE the type we're casting to. */ | |
538 | ||
539 | bool | |
540 | strict_aliasing_warning (tree otype, tree type, tree expr) | |
541 | { | |
542 | /* Strip pointer conversion chains and get to the correct original type. */ | |
543 | STRIP_NOPS (expr); | |
544 | otype = TREE_TYPE (expr); | |
545 | ||
546 | if (!(flag_strict_aliasing | |
547 | && POINTER_TYPE_P (type) | |
548 | && POINTER_TYPE_P (otype) | |
549 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
550 | /* If the type we are casting to is a ref-all pointer | |
551 | dereferencing it is always valid. */ | |
552 | || TYPE_REF_CAN_ALIAS_ALL (type)) | |
553 | return false; | |
554 | ||
555 | if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR | |
556 | && (DECL_P (TREE_OPERAND (expr, 0)) | |
557 | || handled_component_p (TREE_OPERAND (expr, 0)))) | |
558 | { | |
559 | /* Casting the address of an object to non void pointer. Warn | |
560 | if the cast breaks type based aliasing. */ | |
561 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) | |
562 | { | |
563 | warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " | |
564 | "might break strict-aliasing rules"); | |
565 | return true; | |
566 | } | |
567 | else | |
568 | { | |
569 | /* warn_strict_aliasing >= 3. This includes the default (3). | |
570 | Only warn if the cast is dereferenced immediately. */ | |
571 | alias_set_type set1 | |
572 | = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); | |
573 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); | |
574 | ||
195b31a0 | 575 | if (set2 != 0 |
576 | && set1 != set2 | |
577 | && !alias_set_subset_of (set2, set1) | |
578 | && !alias_sets_conflict_p (set1, set2)) | |
0f4cea33 | 579 | { |
580 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
581 | "pointer will break strict-aliasing rules"); | |
582 | return true; | |
583 | } | |
584 | else if (warn_strict_aliasing == 2 | |
585 | && !alias_sets_must_conflict_p (set1, set2)) | |
586 | { | |
587 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
588 | "pointer might break strict-aliasing rules"); | |
589 | return true; | |
590 | } | |
591 | } | |
592 | } | |
593 | else if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) | |
594 | { | |
595 | /* At this level, warn for any conversions, even if an address is | |
596 | not taken in the same statement. This will likely produce many | |
597 | false positives, but could be useful to pinpoint problems that | |
598 | are not revealed at higher levels. */ | |
599 | alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); | |
600 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); | |
601 | if (!COMPLETE_TYPE_P (type) | |
602 | || !alias_sets_must_conflict_p (set1, set2)) | |
603 | { | |
604 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
605 | "pointer might break strict-aliasing rules"); | |
606 | return true; | |
607 | } | |
608 | } | |
609 | ||
610 | return false; | |
611 | } | |
612 | ||
613 | /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with | |
614 | sizeof as last operand of certain builtins. */ | |
615 | ||
616 | void | |
617 | sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee, | |
618 | vec<tree, va_gc> *params, tree *sizeof_arg, | |
619 | bool (*comp_types) (tree, tree)) | |
620 | { | |
621 | tree type, dest = NULL_TREE, src = NULL_TREE, tem; | |
622 | bool strop = false, cmp = false; | |
623 | unsigned int idx = ~0; | |
624 | location_t loc; | |
625 | ||
626 | if (TREE_CODE (callee) != FUNCTION_DECL | |
627 | || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL | |
628 | || vec_safe_length (params) <= 1) | |
629 | return; | |
630 | ||
631 | switch (DECL_FUNCTION_CODE (callee)) | |
632 | { | |
633 | case BUILT_IN_STRNCMP: | |
634 | case BUILT_IN_STRNCASECMP: | |
635 | cmp = true; | |
636 | /* FALLTHRU */ | |
637 | case BUILT_IN_STRNCPY: | |
638 | case BUILT_IN_STRNCPY_CHK: | |
639 | case BUILT_IN_STRNCAT: | |
640 | case BUILT_IN_STRNCAT_CHK: | |
641 | case BUILT_IN_STPNCPY: | |
642 | case BUILT_IN_STPNCPY_CHK: | |
643 | strop = true; | |
644 | /* FALLTHRU */ | |
645 | case BUILT_IN_MEMCPY: | |
646 | case BUILT_IN_MEMCPY_CHK: | |
647 | case BUILT_IN_MEMMOVE: | |
648 | case BUILT_IN_MEMMOVE_CHK: | |
649 | if (params->length () < 3) | |
650 | return; | |
651 | src = (*params)[1]; | |
652 | dest = (*params)[0]; | |
653 | idx = 2; | |
654 | break; | |
655 | case BUILT_IN_BCOPY: | |
656 | if (params->length () < 3) | |
657 | return; | |
658 | src = (*params)[0]; | |
659 | dest = (*params)[1]; | |
660 | idx = 2; | |
661 | break; | |
662 | case BUILT_IN_MEMCMP: | |
663 | case BUILT_IN_BCMP: | |
664 | if (params->length () < 3) | |
665 | return; | |
666 | src = (*params)[1]; | |
667 | dest = (*params)[0]; | |
668 | idx = 2; | |
669 | cmp = true; | |
670 | break; | |
671 | case BUILT_IN_MEMSET: | |
672 | case BUILT_IN_MEMSET_CHK: | |
673 | if (params->length () < 3) | |
674 | return; | |
675 | dest = (*params)[0]; | |
676 | idx = 2; | |
677 | break; | |
678 | case BUILT_IN_BZERO: | |
679 | dest = (*params)[0]; | |
680 | idx = 1; | |
681 | break; | |
682 | case BUILT_IN_STRNDUP: | |
683 | src = (*params)[0]; | |
684 | strop = true; | |
685 | idx = 1; | |
686 | break; | |
687 | case BUILT_IN_MEMCHR: | |
688 | if (params->length () < 3) | |
689 | return; | |
690 | src = (*params)[0]; | |
691 | idx = 2; | |
692 | break; | |
693 | case BUILT_IN_SNPRINTF: | |
694 | case BUILT_IN_SNPRINTF_CHK: | |
695 | case BUILT_IN_VSNPRINTF: | |
696 | case BUILT_IN_VSNPRINTF_CHK: | |
697 | dest = (*params)[0]; | |
698 | idx = 1; | |
699 | strop = true; | |
700 | break; | |
701 | default: | |
702 | break; | |
703 | } | |
704 | ||
705 | if (idx >= 3) | |
706 | return; | |
707 | ||
708 | if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node) | |
709 | return; | |
710 | ||
711 | type = TYPE_P (sizeof_arg[idx]) | |
712 | ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]); | |
713 | if (!POINTER_TYPE_P (type)) | |
714 | return; | |
715 | ||
716 | if (dest | |
717 | && (tem = tree_strip_nop_conversions (dest)) | |
718 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
719 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
720 | return; | |
721 | ||
722 | if (src | |
723 | && (tem = tree_strip_nop_conversions (src)) | |
724 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
725 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
726 | return; | |
727 | ||
728 | loc = sizeof_arg_loc[idx]; | |
729 | ||
730 | if (dest && !cmp) | |
731 | { | |
732 | if (!TYPE_P (sizeof_arg[idx]) | |
733 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
734 | && comp_types (TREE_TYPE (dest), type)) | |
735 | { | |
736 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
737 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
738 | "argument to %<sizeof%> in %qD call is the same " | |
739 | "expression as the destination; did you mean to " | |
740 | "remove the addressof?", callee); | |
741 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
742 | == TYPE_PRECISION (char_type_node)) | |
743 | || strop) | |
744 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
745 | "argument to %<sizeof%> in %qD call is the same " | |
746 | "expression as the destination; did you mean to " | |
747 | "provide an explicit length?", callee); | |
748 | else | |
749 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
750 | "argument to %<sizeof%> in %qD call is the same " | |
751 | "expression as the destination; did you mean to " | |
752 | "dereference it?", callee); | |
753 | return; | |
754 | } | |
755 | ||
756 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
757 | && !strop | |
758 | && comp_types (TREE_TYPE (dest), type) | |
759 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
760 | { | |
761 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
762 | "argument to %<sizeof%> in %qD call is the same " | |
763 | "pointer type %qT as the destination; expected %qT " | |
764 | "or an explicit length", callee, TREE_TYPE (dest), | |
765 | TREE_TYPE (TREE_TYPE (dest))); | |
766 | return; | |
767 | } | |
768 | } | |
769 | ||
770 | if (src && !cmp) | |
771 | { | |
772 | if (!TYPE_P (sizeof_arg[idx]) | |
773 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
774 | && comp_types (TREE_TYPE (src), type)) | |
775 | { | |
776 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
777 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
778 | "argument to %<sizeof%> in %qD call is the same " | |
779 | "expression as the source; did you mean to " | |
780 | "remove the addressof?", callee); | |
781 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
782 | == TYPE_PRECISION (char_type_node)) | |
783 | || strop) | |
784 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
785 | "argument to %<sizeof%> in %qD call is the same " | |
786 | "expression as the source; did you mean to " | |
787 | "provide an explicit length?", callee); | |
788 | else | |
789 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
790 | "argument to %<sizeof%> in %qD call is the same " | |
791 | "expression as the source; did you mean to " | |
792 | "dereference it?", callee); | |
793 | return; | |
794 | } | |
795 | ||
796 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
797 | && !strop | |
798 | && comp_types (TREE_TYPE (src), type) | |
799 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
800 | { | |
801 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
802 | "argument to %<sizeof%> in %qD call is the same " | |
803 | "pointer type %qT as the source; expected %qT " | |
804 | "or an explicit length", callee, TREE_TYPE (src), | |
805 | TREE_TYPE (TREE_TYPE (src))); | |
806 | return; | |
807 | } | |
808 | } | |
809 | ||
810 | if (dest) | |
811 | { | |
812 | if (!TYPE_P (sizeof_arg[idx]) | |
813 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
814 | && comp_types (TREE_TYPE (dest), type)) | |
815 | { | |
816 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
817 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
818 | "argument to %<sizeof%> in %qD call is the same " | |
819 | "expression as the first source; did you mean to " | |
820 | "remove the addressof?", callee); | |
821 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
822 | == TYPE_PRECISION (char_type_node)) | |
823 | || strop) | |
824 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
825 | "argument to %<sizeof%> in %qD call is the same " | |
826 | "expression as the first source; did you mean to " | |
827 | "provide an explicit length?", callee); | |
828 | else | |
829 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
830 | "argument to %<sizeof%> in %qD call is the same " | |
831 | "expression as the first source; did you mean to " | |
832 | "dereference it?", callee); | |
833 | return; | |
834 | } | |
835 | ||
836 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
837 | && !strop | |
838 | && comp_types (TREE_TYPE (dest), type) | |
839 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
840 | { | |
841 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
842 | "argument to %<sizeof%> in %qD call is the same " | |
843 | "pointer type %qT as the first source; expected %qT " | |
844 | "or an explicit length", callee, TREE_TYPE (dest), | |
845 | TREE_TYPE (TREE_TYPE (dest))); | |
846 | return; | |
847 | } | |
848 | } | |
849 | ||
850 | if (src) | |
851 | { | |
852 | if (!TYPE_P (sizeof_arg[idx]) | |
853 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
854 | && comp_types (TREE_TYPE (src), type)) | |
855 | { | |
856 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
857 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
858 | "argument to %<sizeof%> in %qD call is the same " | |
859 | "expression as the second source; did you mean to " | |
860 | "remove the addressof?", callee); | |
861 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
862 | == TYPE_PRECISION (char_type_node)) | |
863 | || strop) | |
864 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
865 | "argument to %<sizeof%> in %qD call is the same " | |
866 | "expression as the second source; did you mean to " | |
867 | "provide an explicit length?", callee); | |
868 | else | |
869 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
870 | "argument to %<sizeof%> in %qD call is the same " | |
871 | "expression as the second source; did you mean to " | |
872 | "dereference it?", callee); | |
873 | return; | |
874 | } | |
875 | ||
876 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
877 | && !strop | |
878 | && comp_types (TREE_TYPE (src), type) | |
879 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
880 | { | |
881 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
882 | "argument to %<sizeof%> in %qD call is the same " | |
883 | "pointer type %qT as the second source; expected %qT " | |
884 | "or an explicit length", callee, TREE_TYPE (src), | |
885 | TREE_TYPE (TREE_TYPE (src))); | |
886 | return; | |
887 | } | |
888 | } | |
889 | ||
890 | } | |
891 | ||
892 | /* Warn for unlikely, improbable, or stupid DECL declarations | |
893 | of `main'. */ | |
894 | ||
895 | void | |
896 | check_main_parameter_types (tree decl) | |
897 | { | |
898 | function_args_iterator iter; | |
899 | tree type; | |
900 | int argct = 0; | |
901 | ||
902 | FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) | |
903 | { | |
904 | /* XXX void_type_node belies the abstraction. */ | |
905 | if (type == void_type_node || type == error_mark_node) | |
906 | break; | |
907 | ||
908 | tree t = type; | |
909 | if (TYPE_ATOMIC (t)) | |
910 | pedwarn (input_location, OPT_Wmain, | |
911 | "%<_Atomic%>-qualified parameter type %qT of %q+D", | |
912 | type, decl); | |
913 | while (POINTER_TYPE_P (t)) | |
914 | { | |
915 | t = TREE_TYPE (t); | |
916 | if (TYPE_ATOMIC (t)) | |
917 | pedwarn (input_location, OPT_Wmain, | |
918 | "%<_Atomic%>-qualified parameter type %qT of %q+D", | |
919 | type, decl); | |
920 | } | |
921 | ||
922 | ++argct; | |
923 | switch (argct) | |
924 | { | |
925 | case 1: | |
926 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) | |
927 | pedwarn (input_location, OPT_Wmain, | |
928 | "first argument of %q+D should be %<int%>", decl); | |
929 | break; | |
930 | ||
931 | case 2: | |
932 | if (TREE_CODE (type) != POINTER_TYPE | |
933 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
934 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
935 | != char_type_node)) | |
936 | pedwarn (input_location, OPT_Wmain, | |
937 | "second argument of %q+D should be %<char **%>", decl); | |
938 | break; | |
939 | ||
940 | case 3: | |
941 | if (TREE_CODE (type) != POINTER_TYPE | |
942 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
943 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
944 | != char_type_node)) | |
945 | pedwarn (input_location, OPT_Wmain, | |
946 | "third argument of %q+D should probably be " | |
947 | "%<char **%>", decl); | |
948 | break; | |
949 | } | |
950 | } | |
951 | ||
952 | /* It is intentional that this message does not mention the third | |
953 | argument because it's only mentioned in an appendix of the | |
954 | standard. */ | |
955 | if (argct > 0 && (argct < 2 || argct > 3)) | |
956 | pedwarn (input_location, OPT_Wmain, | |
957 | "%q+D takes only zero or two arguments", decl); | |
958 | ||
959 | if (stdarg_p (TREE_TYPE (decl))) | |
960 | pedwarn (input_location, OPT_Wmain, | |
961 | "%q+D declared as variadic function", decl); | |
962 | } | |
963 | ||
964 | /* Warns if the conversion of EXPR to TYPE may alter a value. | |
965 | This is a helper function for warnings_for_convert_and_check. */ | |
966 | ||
967 | static void | |
645b0f8d | 968 | conversion_warning (location_t loc, tree type, tree expr, tree result) |
0f4cea33 | 969 | { |
970 | tree expr_type = TREE_TYPE (expr); | |
971 | enum conversion_safety conversion_kind; | |
972 | ||
973 | if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) | |
974 | return; | |
975 | ||
976 | /* This may happen, because for LHS op= RHS we preevaluate | |
977 | RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which | |
978 | means we could no longer see the code of the EXPR. */ | |
979 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
980 | expr = C_MAYBE_CONST_EXPR_EXPR (expr); | |
981 | if (TREE_CODE (expr) == SAVE_EXPR) | |
982 | expr = TREE_OPERAND (expr, 0); | |
983 | ||
984 | switch (TREE_CODE (expr)) | |
985 | { | |
986 | case EQ_EXPR: | |
987 | case NE_EXPR: | |
988 | case LE_EXPR: | |
989 | case GE_EXPR: | |
990 | case LT_EXPR: | |
991 | case GT_EXPR: | |
992 | case TRUTH_ANDIF_EXPR: | |
993 | case TRUTH_ORIF_EXPR: | |
994 | case TRUTH_AND_EXPR: | |
995 | case TRUTH_OR_EXPR: | |
996 | case TRUTH_XOR_EXPR: | |
997 | case TRUTH_NOT_EXPR: | |
998 | /* Conversion from boolean to a signed:1 bit-field (which only | |
999 | can hold the values 0 and -1) doesn't lose information - but | |
1000 | it does change the value. */ | |
1001 | if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) | |
1002 | warning_at (loc, OPT_Wconversion, | |
1003 | "conversion to %qT from boolean expression", type); | |
1004 | return; | |
1005 | ||
1006 | case REAL_CST: | |
1007 | case INTEGER_CST: | |
1008 | case COMPLEX_CST: | |
645b0f8d | 1009 | { |
1010 | conversion_kind = unsafe_conversion_p (loc, type, expr, result, true); | |
1011 | int warnopt; | |
1012 | if (conversion_kind == UNSAFE_REAL) | |
1013 | warnopt = OPT_Wfloat_conversion; | |
1014 | else if (conversion_kind) | |
1015 | warnopt = OPT_Wconversion; | |
1016 | else | |
1017 | break; | |
0f4cea33 | 1018 | |
645b0f8d | 1019 | if (TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant) |
1020 | warning_at (loc, warnopt, | |
1021 | "conversion from %qT to %qT changes value from %qE to %qE", | |
1022 | expr_type, type, expr, result); | |
1023 | else | |
1024 | warning_at (loc, warnopt, | |
1025 | "conversion from %qT to %qT changes the value of %qE", | |
1026 | expr_type, type, expr); | |
1027 | break; | |
1028 | } | |
0f4cea33 | 1029 | case COND_EXPR: |
645b0f8d | 1030 | { |
0f4cea33 | 1031 | /* In case of COND_EXPR, we do not care about the type of |
1032 | COND_EXPR, only about the conversion of each operand. */ | |
1033 | tree op1 = TREE_OPERAND (expr, 1); | |
1034 | tree op2 = TREE_OPERAND (expr, 2); | |
1035 | ||
645b0f8d | 1036 | conversion_warning (loc, type, op1, result); |
1037 | conversion_warning (loc, type, op2, result); | |
0f4cea33 | 1038 | return; |
1039 | } | |
1040 | ||
1041 | default: /* 'expr' is not a constant. */ | |
645b0f8d | 1042 | conversion_kind = unsafe_conversion_p (loc, type, expr, result, true); |
1043 | if (conversion_kind == UNSAFE_IMAGINARY) | |
0f4cea33 | 1044 | warning_at (loc, OPT_Wconversion, |
645b0f8d | 1045 | "conversion from %qT to to %qT discards imaginary " |
1046 | "component", | |
1047 | expr_type, type); | |
a977cac9 | 1048 | else |
1049 | { | |
1050 | int warnopt; | |
1051 | if (conversion_kind == UNSAFE_REAL) | |
1052 | warnopt = OPT_Wfloat_conversion; | |
1053 | else if (conversion_kind) | |
1054 | warnopt = OPT_Wconversion; | |
1055 | else | |
1056 | break; | |
1057 | warning_at (loc, warnopt, | |
1058 | "conversion from %qT to %qT may change value", | |
1059 | expr_type, type); | |
1060 | } | |
0f4cea33 | 1061 | } |
1062 | } | |
1063 | ||
1064 | /* Produce warnings after a conversion. RESULT is the result of | |
1065 | converting EXPR to TYPE. This is a helper function for | |
1066 | convert_and_check and cp_convert_and_check. */ | |
1067 | ||
1068 | void | |
1069 | warnings_for_convert_and_check (location_t loc, tree type, tree expr, | |
1070 | tree result) | |
1071 | { | |
1072 | loc = expansion_point_location_if_in_system_header (loc); | |
1073 | ||
645b0f8d | 1074 | bool cst = TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant; |
1075 | ||
1076 | tree exprtype = TREE_TYPE (expr); | |
1077 | ||
0f4cea33 | 1078 | if (TREE_CODE (expr) == INTEGER_CST |
1079 | && (TREE_CODE (type) == INTEGER_TYPE | |
1080 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
1081 | && !int_fits_type_p (expr, type)) | |
1082 | { | |
1083 | /* Do not diagnose overflow in a constant expression merely | |
1084 | because a conversion overflowed. */ | |
1085 | if (TREE_OVERFLOW (result)) | |
1086 | TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); | |
1087 | ||
1088 | if (TYPE_UNSIGNED (type)) | |
1089 | { | |
1090 | /* This detects cases like converting -129 or 256 to | |
1091 | unsigned char. */ | |
1092 | if (!int_fits_type_p (expr, c_common_signed_type (type))) | |
645b0f8d | 1093 | { |
1094 | if (cst) | |
1095 | warning_at (loc, OPT_Woverflow, | |
1096 | (TYPE_UNSIGNED (exprtype) | |
1097 | ? G_("conversion from %qT to %qT " | |
1098 | "changes value from %qE to %qE") | |
1099 | : G_("unsigned conversion from %qT to %qT " | |
1100 | "changes value from %qE to %qE")), | |
1101 | exprtype, type, expr, result); | |
1102 | else | |
1103 | warning_at (loc, OPT_Woverflow, | |
1104 | (TYPE_UNSIGNED (exprtype) | |
1105 | ? G_("conversion from %qT to %qT " | |
1106 | "changes the value of %qE") | |
1107 | : G_("unsigned conversion from %qT to %qT " | |
1108 | "changes the value of %qE")), | |
1109 | exprtype, type, expr); | |
1110 | } | |
0f4cea33 | 1111 | else |
645b0f8d | 1112 | conversion_warning (loc, type, expr, result); |
0f4cea33 | 1113 | } |
1114 | else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) | |
645b0f8d | 1115 | { |
1116 | if (cst) | |
1117 | warning_at (loc, OPT_Woverflow, | |
1118 | "overflow in conversion from %qT to %qT " | |
1119 | "changes value from %qE to %qE", | |
1120 | exprtype, type, expr, result); | |
1121 | else | |
1122 | warning_at (loc, OPT_Woverflow, | |
1123 | "overflow in conversion from %qT to %qT " | |
1124 | "changes the value of %qE", | |
1125 | exprtype, type, expr); | |
1126 | } | |
0f4cea33 | 1127 | /* No warning for converting 0x80000000 to int. */ |
1128 | else if (pedantic | |
645b0f8d | 1129 | && (TREE_CODE (exprtype) != INTEGER_TYPE |
1130 | || TYPE_PRECISION (exprtype) | |
0f4cea33 | 1131 | != TYPE_PRECISION (type))) |
645b0f8d | 1132 | { |
1133 | if (cst) | |
1134 | warning_at (loc, OPT_Woverflow, | |
1135 | "overflow in conversion from %qT to %qT " | |
1136 | "changes value from %qE to %qE", | |
1137 | exprtype, type, expr, result); | |
1138 | else | |
1139 | warning_at (loc, OPT_Woverflow, | |
1140 | "overflow in conversion from %qT to %qT " | |
1141 | "changes the value of %qE", | |
1142 | exprtype, type, expr); | |
1143 | } | |
0f4cea33 | 1144 | else |
645b0f8d | 1145 | conversion_warning (loc, type, expr, result); |
0f4cea33 | 1146 | } |
1147 | else if ((TREE_CODE (result) == INTEGER_CST | |
1148 | || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) | |
645b0f8d | 1149 | { |
1150 | if (cst) | |
1151 | warning_at (loc, OPT_Woverflow, | |
1152 | "overflow in conversion from %qT to %qT " | |
1153 | "chages value from %qE to %qE", | |
1154 | exprtype, type, expr, result); | |
1155 | else | |
1156 | warning_at (loc, OPT_Woverflow, | |
1157 | "overflow in conversion from %qT to %qT " | |
1158 | "chages the value of %qE", | |
1159 | exprtype, type, expr); | |
1160 | } | |
0f4cea33 | 1161 | else |
645b0f8d | 1162 | conversion_warning (loc, type, expr, result); |
0f4cea33 | 1163 | } |
1164 | ||
1165 | /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. | |
1166 | Used to verify that case values match up with enumerator values. */ | |
1167 | ||
1168 | static void | |
1169 | match_case_to_enum_1 (tree key, tree type, tree label) | |
1170 | { | |
65fb3b5b | 1171 | /* Avoid warning about enums that have no enumerators. */ |
1172 | if (TYPE_VALUES (type) == NULL_TREE) | |
1173 | return; | |
1174 | ||
0f4cea33 | 1175 | char buf[WIDE_INT_PRINT_BUFFER_SIZE]; |
1176 | ||
1177 | if (tree_fits_uhwi_p (key)) | |
1178 | print_dec (key, buf, UNSIGNED); | |
1179 | else if (tree_fits_shwi_p (key)) | |
1180 | print_dec (key, buf, SIGNED); | |
1181 | else | |
1182 | print_hex (key, buf); | |
1183 | ||
72749341 | 1184 | if (TYPE_NAME (type) == NULL_TREE) |
0f4cea33 | 1185 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
1186 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
1187 | "case value %qs not in enumerated type", | |
1188 | buf); | |
1189 | else | |
1190 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), | |
1191 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
1192 | "case value %qs not in enumerated type %qT", | |
1193 | buf, type); | |
1194 | } | |
1195 | ||
1196 | /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. | |
1197 | Used to verify that case values match up with enumerator values. */ | |
1198 | ||
1199 | static int | |
1200 | match_case_to_enum (splay_tree_node node, void *data) | |
1201 | { | |
1202 | tree label = (tree) node->value; | |
1203 | tree type = (tree) data; | |
1204 | ||
1205 | /* Skip default case. */ | |
1206 | if (!CASE_LOW (label)) | |
1207 | return 0; | |
1208 | ||
1209 | /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear | |
1210 | when we did our enum->case scan. Reset our scratch bit after. */ | |
1211 | if (!CASE_LOW_SEEN (label)) | |
1212 | match_case_to_enum_1 (CASE_LOW (label), type, label); | |
1213 | else | |
1214 | CASE_LOW_SEEN (label) = 0; | |
1215 | ||
1216 | /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is | |
1217 | not set, that means that CASE_HIGH did not appear when we did our | |
1218 | enum->case scan. Reset our scratch bit after. */ | |
1219 | if (CASE_HIGH (label)) | |
1220 | { | |
1221 | if (!CASE_HIGH_SEEN (label)) | |
1222 | match_case_to_enum_1 (CASE_HIGH (label), type, label); | |
1223 | else | |
1224 | CASE_HIGH_SEEN (label) = 0; | |
1225 | } | |
1226 | ||
1227 | return 0; | |
1228 | } | |
1229 | ||
1230 | /* Handle -Wswitch*. Called from the front end after parsing the | |
1231 | switch construct. */ | |
1232 | /* ??? Should probably be somewhere generic, since other languages | |
1233 | besides C and C++ would want this. At the moment, however, C/C++ | |
1234 | are the only tree-ssa languages that support enumerations at all, | |
1235 | so the point is moot. */ | |
1236 | ||
1237 | void | |
1238 | c_do_switch_warnings (splay_tree cases, location_t switch_location, | |
1239 | tree type, tree cond, bool bool_cond_p, | |
1240 | bool outside_range_p) | |
1241 | { | |
1242 | splay_tree_node default_node; | |
1243 | splay_tree_node node; | |
1244 | tree chain; | |
1245 | ||
1246 | if (!warn_switch && !warn_switch_enum && !warn_switch_default | |
1247 | && !warn_switch_bool) | |
1248 | return; | |
1249 | ||
1250 | default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); | |
1251 | if (!default_node) | |
1252 | warning_at (switch_location, OPT_Wswitch_default, | |
1253 | "switch missing default case"); | |
1254 | ||
1255 | /* There are certain cases where -Wswitch-bool warnings aren't | |
1256 | desirable, such as | |
1257 | switch (boolean) | |
1258 | { | |
1259 | case true: ... | |
1260 | case false: ... | |
1261 | } | |
1262 | so be careful here. */ | |
1263 | if (warn_switch_bool && bool_cond_p) | |
1264 | { | |
1265 | splay_tree_node min_node; | |
1266 | /* If there's a default node, it's also the value with the minimal | |
1267 | key. So look at the penultimate key (if any). */ | |
1268 | if (default_node) | |
1269 | min_node = splay_tree_successor (cases, (splay_tree_key) NULL); | |
1270 | else | |
1271 | min_node = splay_tree_min (cases); | |
1272 | tree min = min_node ? (tree) min_node->key : NULL_TREE; | |
1273 | ||
1274 | splay_tree_node max_node = splay_tree_max (cases); | |
1275 | /* This might be a case range, so look at the value with the | |
1276 | maximal key and then check CASE_HIGH. */ | |
1277 | tree max = max_node ? (tree) max_node->value : NULL_TREE; | |
1278 | if (max) | |
1279 | max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max); | |
1280 | ||
1281 | /* If there's a case value > 1 or < 0, that is outside bool | |
1282 | range, warn. */ | |
1283 | if (outside_range_p | |
1284 | || (max && wi::gts_p (max, 1)) | |
1285 | || (min && wi::lts_p (min, 0)) | |
1286 | /* And handle the | |
1287 | switch (boolean) | |
1288 | { | |
1289 | case true: ... | |
1290 | case false: ... | |
1291 | default: ... | |
1292 | } | |
1293 | case, where we want to warn. */ | |
1294 | || (default_node | |
1295 | && max && wi::eq_p (max, 1) | |
1296 | && min && wi::eq_p (min, 0))) | |
1297 | warning_at (switch_location, OPT_Wswitch_bool, | |
1298 | "switch condition has boolean value"); | |
1299 | } | |
1300 | ||
1301 | /* From here on, we only care about enumerated types. */ | |
1302 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
1303 | return; | |
1304 | ||
1305 | /* From here on, we only care about -Wswitch and -Wswitch-enum. */ | |
1306 | if (!warn_switch_enum && !warn_switch) | |
1307 | return; | |
1308 | ||
1309 | /* Check the cases. Warn about case values which are not members of | |
1310 | the enumerated type. For -Wswitch-enum, or for -Wswitch when | |
1311 | there is no default case, check that exactly all enumeration | |
1312 | literals are covered by the cases. */ | |
1313 | ||
1314 | /* Clearing COND if it is not an integer constant simplifies | |
1315 | the tests inside the loop below. */ | |
1316 | if (TREE_CODE (cond) != INTEGER_CST) | |
1317 | cond = NULL_TREE; | |
1318 | ||
1319 | /* The time complexity here is O(N*lg(N)) worst case, but for the | |
1320 | common case of monotonically increasing enumerators, it is | |
1321 | O(N), since the nature of the splay tree will keep the next | |
1322 | element adjacent to the root at all times. */ | |
1323 | ||
1324 | for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) | |
1325 | { | |
1326 | tree value = TREE_VALUE (chain); | |
1327 | if (TREE_CODE (value) == CONST_DECL) | |
1328 | value = DECL_INITIAL (value); | |
1329 | node = splay_tree_lookup (cases, (splay_tree_key) value); | |
1330 | if (node) | |
1331 | { | |
1332 | /* Mark the CASE_LOW part of the case entry as seen. */ | |
1333 | tree label = (tree) node->value; | |
1334 | CASE_LOW_SEEN (label) = 1; | |
1335 | continue; | |
1336 | } | |
1337 | ||
1338 | /* Even though there wasn't an exact match, there might be a | |
1339 | case range which includes the enumerator's value. */ | |
1340 | node = splay_tree_predecessor (cases, (splay_tree_key) value); | |
1341 | if (node && CASE_HIGH ((tree) node->value)) | |
1342 | { | |
1343 | tree label = (tree) node->value; | |
1344 | int cmp = tree_int_cst_compare (CASE_HIGH (label), value); | |
1345 | if (cmp >= 0) | |
1346 | { | |
1347 | /* If we match the upper bound exactly, mark the CASE_HIGH | |
1348 | part of the case entry as seen. */ | |
1349 | if (cmp == 0) | |
1350 | CASE_HIGH_SEEN (label) = 1; | |
1351 | continue; | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | /* We've now determined that this enumerated literal isn't | |
1356 | handled by the case labels of the switch statement. */ | |
1357 | ||
1358 | /* If the switch expression is a constant, we only really care | |
1359 | about whether that constant is handled by the switch. */ | |
1360 | if (cond && tree_int_cst_compare (cond, value)) | |
1361 | continue; | |
1362 | ||
1363 | /* If there is a default_node, the only relevant option is | |
1364 | Wswitch-enum. Otherwise, if both are enabled then we prefer | |
1365 | to warn using -Wswitch because -Wswitch is enabled by -Wall | |
1366 | while -Wswitch-enum is explicit. */ | |
1367 | warning_at (switch_location, | |
1368 | (default_node || !warn_switch | |
1369 | ? OPT_Wswitch_enum | |
1370 | : OPT_Wswitch), | |
1371 | "enumeration value %qE not handled in switch", | |
1372 | TREE_PURPOSE (chain)); | |
1373 | } | |
1374 | ||
1375 | /* Warn if there are case expressions that don't correspond to | |
1376 | enumerators. This can occur since C and C++ don't enforce | |
1377 | type-checking of assignments to enumeration variables. | |
1378 | ||
1379 | The time complexity here is now always O(N) worst case, since | |
1380 | we should have marked both the lower bound and upper bound of | |
1381 | every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN | |
1382 | above. This scan also resets those fields. */ | |
1383 | ||
1384 | splay_tree_foreach (cases, match_case_to_enum, type); | |
1385 | } | |
1386 | ||
1387 | /* Warn for A ?: C expressions (with B omitted) where A is a boolean | |
1388 | expression, because B will always be true. */ | |
1389 | ||
1390 | void | |
1391 | warn_for_omitted_condop (location_t location, tree cond) | |
1392 | { | |
1393 | /* In C++ template declarations it can happen that the type is dependent | |
1394 | and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */ | |
1395 | if (truth_value_p (TREE_CODE (cond)) | |
1396 | || (TREE_TYPE (cond) != NULL_TREE | |
1397 | && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE)) | |
1398 | warning_at (location, OPT_Wparentheses, | |
1399 | "the omitted middle operand in ?: will always be %<true%>, " | |
1400 | "suggest explicit middle operand"); | |
1401 | } | |
1402 | ||
1403 | /* Give an error for storing into ARG, which is 'const'. USE indicates | |
1404 | how ARG was being used. */ | |
1405 | ||
1406 | void | |
1407 | readonly_error (location_t loc, tree arg, enum lvalue_use use) | |
1408 | { | |
1409 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement | |
1410 | || use == lv_asm); | |
1411 | /* Using this macro rather than (for example) arrays of messages | |
1412 | ensures that all the format strings are checked at compile | |
1413 | time. */ | |
1414 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ | |
1415 | : (use == lv_increment ? (I) \ | |
1416 | : (use == lv_decrement ? (D) : (AS)))) | |
1417 | if (TREE_CODE (arg) == COMPONENT_REF) | |
1418 | { | |
1419 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
1420 | error_at (loc, READONLY_MSG (G_("assignment of member " | |
1421 | "%qD in read-only object"), | |
1422 | G_("increment of member " | |
1423 | "%qD in read-only object"), | |
1424 | G_("decrement of member " | |
1425 | "%qD in read-only object"), | |
1426 | G_("member %qD in read-only object " | |
1427 | "used as %<asm%> output")), | |
1428 | TREE_OPERAND (arg, 1)); | |
1429 | else | |
1430 | error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"), | |
1431 | G_("increment of read-only member %qD"), | |
1432 | G_("decrement of read-only member %qD"), | |
1433 | G_("read-only member %qD used as %<asm%> output")), | |
1434 | TREE_OPERAND (arg, 1)); | |
1435 | } | |
1436 | else if (VAR_P (arg)) | |
1437 | error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"), | |
1438 | G_("increment of read-only variable %qD"), | |
1439 | G_("decrement of read-only variable %qD"), | |
1440 | G_("read-only variable %qD used as %<asm%> output")), | |
1441 | arg); | |
1442 | else if (TREE_CODE (arg) == PARM_DECL) | |
1443 | error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"), | |
1444 | G_("increment of read-only parameter %qD"), | |
1445 | G_("decrement of read-only parameter %qD"), | |
1446 | G_("read-only parameter %qD use as %<asm%> output")), | |
1447 | arg); | |
1448 | else if (TREE_CODE (arg) == RESULT_DECL) | |
1449 | { | |
1450 | gcc_assert (c_dialect_cxx ()); | |
1451 | error_at (loc, READONLY_MSG (G_("assignment of " | |
1452 | "read-only named return value %qD"), | |
1453 | G_("increment of " | |
1454 | "read-only named return value %qD"), | |
1455 | G_("decrement of " | |
1456 | "read-only named return value %qD"), | |
1457 | G_("read-only named return value %qD " | |
1458 | "used as %<asm%>output")), | |
1459 | arg); | |
1460 | } | |
1461 | else if (TREE_CODE (arg) == FUNCTION_DECL) | |
1462 | error_at (loc, READONLY_MSG (G_("assignment of function %qD"), | |
1463 | G_("increment of function %qD"), | |
1464 | G_("decrement of function %qD"), | |
1465 | G_("function %qD used as %<asm%> output")), | |
1466 | arg); | |
1467 | else | |
1468 | error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"), | |
1469 | G_("increment of read-only location %qE"), | |
1470 | G_("decrement of read-only location %qE"), | |
1471 | G_("read-only location %qE used as %<asm%> output")), | |
1472 | arg); | |
1473 | } | |
1474 | ||
1475 | /* Print an error message for an invalid lvalue. USE says | |
1476 | how the lvalue is being used and so selects the error message. LOC | |
1477 | is the location for the error. */ | |
1478 | ||
1479 | void | |
1480 | lvalue_error (location_t loc, enum lvalue_use use) | |
1481 | { | |
1482 | switch (use) | |
1483 | { | |
1484 | case lv_assign: | |
1485 | error_at (loc, "lvalue required as left operand of assignment"); | |
1486 | break; | |
1487 | case lv_increment: | |
1488 | error_at (loc, "lvalue required as increment operand"); | |
1489 | break; | |
1490 | case lv_decrement: | |
1491 | error_at (loc, "lvalue required as decrement operand"); | |
1492 | break; | |
1493 | case lv_addressof: | |
1494 | error_at (loc, "lvalue required as unary %<&%> operand"); | |
1495 | break; | |
1496 | case lv_asm: | |
1497 | error_at (loc, "lvalue required in asm statement"); | |
1498 | break; | |
1499 | default: | |
1500 | gcc_unreachable (); | |
1501 | } | |
1502 | } | |
1503 | ||
1504 | /* Print an error message for an invalid indirection of type TYPE. | |
1505 | ERRSTRING is the name of the operator for the indirection. */ | |
1506 | ||
1507 | void | |
1508 | invalid_indirection_error (location_t loc, tree type, ref_operator errstring) | |
1509 | { | |
1510 | switch (errstring) | |
1511 | { | |
1512 | case RO_NULL: | |
1513 | gcc_assert (c_dialect_cxx ()); | |
1514 | error_at (loc, "invalid type argument (have %qT)", type); | |
1515 | break; | |
1516 | case RO_ARRAY_INDEXING: | |
1517 | error_at (loc, | |
1518 | "invalid type argument of array indexing (have %qT)", | |
1519 | type); | |
1520 | break; | |
1521 | case RO_UNARY_STAR: | |
1522 | error_at (loc, | |
1523 | "invalid type argument of unary %<*%> (have %qT)", | |
1524 | type); | |
1525 | break; | |
1526 | case RO_ARROW: | |
1527 | error_at (loc, | |
1528 | "invalid type argument of %<->%> (have %qT)", | |
1529 | type); | |
1530 | break; | |
1531 | case RO_ARROW_STAR: | |
1532 | error_at (loc, | |
1533 | "invalid type argument of %<->*%> (have %qT)", | |
1534 | type); | |
1535 | break; | |
1536 | case RO_IMPLICIT_CONVERSION: | |
1537 | error_at (loc, | |
1538 | "invalid type argument of implicit conversion (have %qT)", | |
1539 | type); | |
1540 | break; | |
1541 | default: | |
1542 | gcc_unreachable (); | |
1543 | } | |
1544 | } | |
1545 | ||
1546 | /* Subscripting with type char is likely to lose on a machine where | |
1547 | chars are signed. So warn on any machine, but optionally. Don't | |
1548 | warn for unsigned char since that type is safe. Don't warn for | |
1549 | signed char because anyone who uses that must have done so | |
1550 | deliberately. Furthermore, we reduce the false positive load by | |
1551 | warning only for non-constant value of type char. */ | |
1552 | ||
1553 | void | |
1554 | warn_array_subscript_with_type_char (location_t loc, tree index) | |
1555 | { | |
1556 | if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node | |
1557 | && TREE_CODE (index) != INTEGER_CST) | |
1558 | warning_at (loc, OPT_Wchar_subscripts, | |
1559 | "array subscript has type %<char%>"); | |
1560 | } | |
1561 | ||
1562 | /* Implement -Wparentheses for the unexpected C precedence rules, to | |
1563 | cover cases like x + y << z which readers are likely to | |
1564 | misinterpret. We have seen an expression in which CODE is a binary | |
1565 | operator used to combine expressions ARG_LEFT and ARG_RIGHT, which | |
1566 | before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and | |
1567 | CODE_RIGHT may be ERROR_MARK, which means that that side of the | |
1568 | expression was not formed using a binary or unary operator, or it | |
1569 | was enclosed in parentheses. */ | |
1570 | ||
1571 | void | |
1572 | warn_about_parentheses (location_t loc, enum tree_code code, | |
1573 | enum tree_code code_left, tree arg_left, | |
1574 | enum tree_code code_right, tree arg_right) | |
1575 | { | |
1576 | if (!warn_parentheses) | |
1577 | return; | |
1578 | ||
1579 | /* This macro tests that the expression ARG with original tree code | |
1580 | CODE appears to be a boolean expression. or the result of folding a | |
1581 | boolean expression. */ | |
1582 | #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ | |
1583 | (truth_value_p (TREE_CODE (ARG)) \ | |
1584 | || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ | |
1585 | /* Folding may create 0 or 1 integers from other expressions. */ \ | |
1586 | || ((CODE) != INTEGER_CST \ | |
1587 | && (integer_onep (ARG) || integer_zerop (ARG)))) | |
1588 | ||
1589 | switch (code) | |
1590 | { | |
1591 | case LSHIFT_EXPR: | |
1592 | if (code_left == PLUS_EXPR) | |
1593 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1594 | "suggest parentheses around %<+%> inside %<<<%>"); | |
1595 | else if (code_right == PLUS_EXPR) | |
1596 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1597 | "suggest parentheses around %<+%> inside %<<<%>"); | |
1598 | else if (code_left == MINUS_EXPR) | |
1599 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1600 | "suggest parentheses around %<-%> inside %<<<%>"); | |
1601 | else if (code_right == MINUS_EXPR) | |
1602 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1603 | "suggest parentheses around %<-%> inside %<<<%>"); | |
1604 | return; | |
1605 | ||
1606 | case RSHIFT_EXPR: | |
1607 | if (code_left == PLUS_EXPR) | |
1608 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1609 | "suggest parentheses around %<+%> inside %<>>%>"); | |
1610 | else if (code_right == PLUS_EXPR) | |
1611 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1612 | "suggest parentheses around %<+%> inside %<>>%>"); | |
1613 | else if (code_left == MINUS_EXPR) | |
1614 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1615 | "suggest parentheses around %<-%> inside %<>>%>"); | |
1616 | else if (code_right == MINUS_EXPR) | |
1617 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1618 | "suggest parentheses around %<-%> inside %<>>%>"); | |
1619 | return; | |
1620 | ||
1621 | case TRUTH_ORIF_EXPR: | |
1622 | if (code_left == TRUTH_ANDIF_EXPR) | |
1623 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1624 | "suggest parentheses around %<&&%> within %<||%>"); | |
1625 | else if (code_right == TRUTH_ANDIF_EXPR) | |
1626 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1627 | "suggest parentheses around %<&&%> within %<||%>"); | |
1628 | return; | |
1629 | ||
1630 | case BIT_IOR_EXPR: | |
1631 | if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR | |
1632 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) | |
1633 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1634 | "suggest parentheses around arithmetic in operand of %<|%>"); | |
1635 | else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR | |
1636 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
1637 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1638 | "suggest parentheses around arithmetic in operand of %<|%>"); | |
1639 | /* Check cases like x|y==z */ | |
1640 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) | |
1641 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1642 | "suggest parentheses around comparison in operand of %<|%>"); | |
1643 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
1644 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1645 | "suggest parentheses around comparison in operand of %<|%>"); | |
1646 | /* Check cases like !x | y */ | |
1647 | else if (code_left == TRUTH_NOT_EXPR | |
1648 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
1649 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1650 | "suggest parentheses around operand of " | |
1651 | "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); | |
1652 | return; | |
1653 | ||
1654 | case BIT_XOR_EXPR: | |
1655 | if (code_left == BIT_AND_EXPR | |
1656 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) | |
1657 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1658 | "suggest parentheses around arithmetic in operand of %<^%>"); | |
1659 | else if (code_right == BIT_AND_EXPR | |
1660 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
1661 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1662 | "suggest parentheses around arithmetic in operand of %<^%>"); | |
1663 | /* Check cases like x^y==z */ | |
1664 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) | |
1665 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1666 | "suggest parentheses around comparison in operand of %<^%>"); | |
1667 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
1668 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1669 | "suggest parentheses around comparison in operand of %<^%>"); | |
1670 | return; | |
1671 | ||
1672 | case BIT_AND_EXPR: | |
1673 | if (code_left == PLUS_EXPR) | |
1674 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1675 | "suggest parentheses around %<+%> in operand of %<&%>"); | |
1676 | else if (code_right == PLUS_EXPR) | |
1677 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1678 | "suggest parentheses around %<+%> in operand of %<&%>"); | |
1679 | else if (code_left == MINUS_EXPR) | |
1680 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1681 | "suggest parentheses around %<-%> in operand of %<&%>"); | |
1682 | else if (code_right == MINUS_EXPR) | |
1683 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1684 | "suggest parentheses around %<-%> in operand of %<&%>"); | |
1685 | /* Check cases like x&y==z */ | |
1686 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) | |
1687 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1688 | "suggest parentheses around comparison in operand of %<&%>"); | |
1689 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
1690 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1691 | "suggest parentheses around comparison in operand of %<&%>"); | |
1692 | /* Check cases like !x & y */ | |
1693 | else if (code_left == TRUTH_NOT_EXPR | |
1694 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
1695 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1696 | "suggest parentheses around operand of " | |
1697 | "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); | |
1698 | return; | |
1699 | ||
1700 | case EQ_EXPR: | |
1701 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) | |
1702 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1703 | "suggest parentheses around comparison in operand of %<==%>"); | |
1704 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
1705 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1706 | "suggest parentheses around comparison in operand of %<==%>"); | |
1707 | return; | |
1708 | case NE_EXPR: | |
1709 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) | |
1710 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1711 | "suggest parentheses around comparison in operand of %<!=%>"); | |
1712 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
1713 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1714 | "suggest parentheses around comparison in operand of %<!=%>"); | |
1715 | return; | |
1716 | ||
1717 | default: | |
1718 | if (TREE_CODE_CLASS (code) == tcc_comparison) | |
1719 | { | |
1720 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
1721 | && code_left != NE_EXPR && code_left != EQ_EXPR | |
1722 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) | |
1723 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
1724 | "comparisons like %<X<=Y<=Z%> do not " | |
1725 | "have their mathematical meaning"); | |
1726 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison | |
1727 | && code_right != NE_EXPR && code_right != EQ_EXPR | |
1728 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) | |
1729 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
1730 | "comparisons like %<X<=Y<=Z%> do not " | |
1731 | "have their mathematical meaning"); | |
1732 | } | |
1733 | return; | |
1734 | } | |
1735 | #undef NOT_A_BOOLEAN_EXPR_P | |
1736 | } | |
1737 | ||
1738 | /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ | |
1739 | ||
1740 | void | |
1741 | warn_for_unused_label (tree label) | |
1742 | { | |
1743 | if (!TREE_USED (label)) | |
1744 | { | |
1745 | if (DECL_INITIAL (label)) | |
1746 | warning (OPT_Wunused_label, "label %q+D defined but not used", label); | |
1747 | else | |
1748 | warning (OPT_Wunused_label, "label %q+D declared but not defined", label); | |
1749 | } | |
629b6abc | 1750 | else if (asan_sanitize_use_after_scope ()) |
1751 | { | |
1752 | if (asan_used_labels == NULL) | |
1753 | asan_used_labels = new hash_set<tree> (16); | |
1754 | ||
1755 | asan_used_labels->add (label); | |
1756 | } | |
0f4cea33 | 1757 | } |
1758 | ||
1759 | /* Warn for division by zero according to the value of DIVISOR. LOC | |
1760 | is the location of the division operator. */ | |
1761 | ||
1762 | void | |
1763 | warn_for_div_by_zero (location_t loc, tree divisor) | |
1764 | { | |
1765 | /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning | |
1766 | about division by zero. Do not issue a warning if DIVISOR has a | |
1767 | floating-point type, since we consider 0.0/0.0 a valid way of | |
1768 | generating a NaN. */ | |
1769 | if (c_inhibit_evaluation_warnings == 0 | |
1770 | && (integer_zerop (divisor) || fixed_zerop (divisor))) | |
1771 | warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); | |
1772 | } | |
1773 | ||
1774 | /* Warn for patterns where memset appears to be used incorrectly. The | |
1775 | warning location should be LOC. ARG0, and ARG2 are the first and | |
1776 | last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for | |
1777 | each argument that was a literal zero. */ | |
1778 | ||
1779 | void | |
1780 | warn_for_memset (location_t loc, tree arg0, tree arg2, | |
1781 | int literal_zero_mask) | |
1782 | { | |
1783 | if (warn_memset_transposed_args | |
1784 | && integer_zerop (arg2) | |
1785 | && (literal_zero_mask & (1 << 2)) != 0 | |
1786 | && (literal_zero_mask & (1 << 1)) == 0) | |
1787 | warning_at (loc, OPT_Wmemset_transposed_args, | |
1788 | "%<memset%> used with constant zero length " | |
1789 | "parameter; this could be due to transposed " | |
1790 | "parameters"); | |
1791 | ||
1792 | if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST) | |
1793 | { | |
1794 | STRIP_NOPS (arg0); | |
1795 | if (TREE_CODE (arg0) == ADDR_EXPR) | |
1796 | arg0 = TREE_OPERAND (arg0, 0); | |
1797 | tree type = TREE_TYPE (arg0); | |
1798 | if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE) | |
1799 | { | |
1800 | tree elt_type = TREE_TYPE (type); | |
1801 | tree domain = TYPE_DOMAIN (type); | |
1802 | if (!integer_onep (TYPE_SIZE_UNIT (elt_type)) | |
1803 | && domain != NULL_TREE | |
a8de6c92 | 1804 | && TYPE_MAX_VALUE (domain) |
1805 | && TYPE_MIN_VALUE (domain) | |
1806 | && integer_zerop (TYPE_MIN_VALUE (domain)) | |
0f4cea33 | 1807 | && integer_onep (fold_build2 (MINUS_EXPR, domain, |
1808 | arg2, | |
a8de6c92 | 1809 | TYPE_MAX_VALUE (domain)))) |
0f4cea33 | 1810 | warning_at (loc, OPT_Wmemset_elt_size, |
1811 | "%<memset%> used with length equal to " | |
1812 | "number of elements without multiplication " | |
1813 | "by element size"); | |
1814 | } | |
1815 | } | |
1816 | } | |
1817 | ||
1818 | /* Subroutine of build_binary_op. Give warnings for comparisons | |
1819 | between signed and unsigned quantities that may fail. Do the | |
1820 | checking based on the original operand trees ORIG_OP0 and ORIG_OP1, | |
1821 | so that casts will be considered, but default promotions won't | |
1822 | be. | |
1823 | ||
1824 | LOCATION is the location of the comparison operator. | |
1825 | ||
1826 | The arguments of this function map directly to local variables | |
1827 | of build_binary_op. */ | |
1828 | ||
1829 | void | |
1830 | warn_for_sign_compare (location_t location, | |
1831 | tree orig_op0, tree orig_op1, | |
1832 | tree op0, tree op1, | |
1833 | tree result_type, enum tree_code resultcode) | |
1834 | { | |
1835 | int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); | |
1836 | int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); | |
1837 | int unsignedp0, unsignedp1; | |
1838 | ||
1839 | /* In C++, check for comparison of different enum types. */ | |
1840 | if (c_dialect_cxx() | |
1841 | && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE | |
1842 | && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE | |
1843 | && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) | |
1844 | != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) | |
1845 | { | |
1846 | warning_at (location, | |
1847 | OPT_Wsign_compare, "comparison between types %qT and %qT", | |
1848 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); | |
1849 | } | |
1850 | ||
1851 | /* Do not warn if the comparison is being done in a signed type, | |
1852 | since the signed type will only be chosen if it can represent | |
1853 | all the values of the unsigned type. */ | |
1854 | if (!TYPE_UNSIGNED (result_type)) | |
1855 | /* OK */; | |
1856 | /* Do not warn if both operands are unsigned. */ | |
1857 | else if (op0_signed == op1_signed) | |
1858 | /* OK */; | |
1859 | else | |
1860 | { | |
1861 | tree sop, uop, base_type; | |
1862 | bool ovf; | |
1863 | ||
1864 | if (op0_signed) | |
1865 | sop = orig_op0, uop = orig_op1; | |
1866 | else | |
1867 | sop = orig_op1, uop = orig_op0; | |
1868 | ||
1869 | STRIP_TYPE_NOPS (sop); | |
1870 | STRIP_TYPE_NOPS (uop); | |
1871 | base_type = (TREE_CODE (result_type) == COMPLEX_TYPE | |
1872 | ? TREE_TYPE (result_type) : result_type); | |
1873 | ||
1874 | /* Do not warn if the signed quantity is an unsuffixed integer | |
1875 | literal (or some static constant expression involving such | |
1876 | literals or a conditional expression involving such literals) | |
1877 | and it is non-negative. */ | |
1878 | if (tree_expr_nonnegative_warnv_p (sop, &ovf)) | |
1879 | /* OK */; | |
1880 | /* Do not warn if the comparison is an equality operation, the | |
1881 | unsigned quantity is an integral constant, and it would fit | |
1882 | in the result if the result were signed. */ | |
1883 | else if (TREE_CODE (uop) == INTEGER_CST | |
1884 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) | |
1885 | && int_fits_type_p (uop, c_common_signed_type (base_type))) | |
1886 | /* OK */; | |
1887 | /* In C, do not warn if the unsigned quantity is an enumeration | |
1888 | constant and its maximum value would fit in the result if the | |
1889 | result were signed. */ | |
1890 | else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST | |
1891 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE | |
1892 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), | |
1893 | c_common_signed_type (base_type))) | |
1894 | /* OK */; | |
1895 | else | |
7d4d7ecb | 1896 | warning_at (location, OPT_Wsign_compare, |
1897 | "comparison of integer expressions of different " | |
1898 | "signedness: %qT and %qT", TREE_TYPE (orig_op0), | |
1899 | TREE_TYPE (orig_op1)); | |
0f4cea33 | 1900 | } |
1901 | ||
1902 | /* Warn if two unsigned values are being compared in a size larger | |
1903 | than their original size, and one (and only one) is the result of | |
1904 | a `~' operator. This comparison will always fail. | |
1905 | ||
1906 | Also warn if one operand is a constant, and the constant does not | |
1907 | have all bits set that are set in the ~ operand when it is | |
1908 | extended. */ | |
1909 | ||
1910 | op0 = c_common_get_narrower (op0, &unsignedp0); | |
1911 | op1 = c_common_get_narrower (op1, &unsignedp1); | |
1912 | ||
1913 | if ((TREE_CODE (op0) == BIT_NOT_EXPR) | |
1914 | ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) | |
1915 | { | |
1916 | if (TREE_CODE (op0) == BIT_NOT_EXPR) | |
1917 | op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); | |
1918 | if (TREE_CODE (op1) == BIT_NOT_EXPR) | |
1919 | op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); | |
1920 | ||
1921 | if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1)) | |
1922 | { | |
1923 | tree primop; | |
1924 | HOST_WIDE_INT constant, mask; | |
1925 | int unsignedp; | |
1926 | unsigned int bits; | |
1927 | ||
1928 | if (tree_fits_shwi_p (op0)) | |
1929 | { | |
1930 | primop = op1; | |
1931 | unsignedp = unsignedp1; | |
1932 | constant = tree_to_shwi (op0); | |
1933 | } | |
1934 | else | |
1935 | { | |
1936 | primop = op0; | |
1937 | unsignedp = unsignedp0; | |
1938 | constant = tree_to_shwi (op1); | |
1939 | } | |
1940 | ||
1941 | bits = TYPE_PRECISION (TREE_TYPE (primop)); | |
1942 | if (bits < TYPE_PRECISION (result_type) | |
1943 | && bits < HOST_BITS_PER_LONG && unsignedp) | |
1944 | { | |
1945 | mask = HOST_WIDE_INT_M1U << bits; | |
1946 | if ((mask & constant) != mask) | |
1947 | { | |
1948 | if (constant == 0) | |
1949 | warning_at (location, OPT_Wsign_compare, | |
1950 | "promoted ~unsigned is always non-zero"); | |
1951 | else | |
1952 | warning_at (location, OPT_Wsign_compare, | |
1953 | "comparison of promoted ~unsigned with constant"); | |
1954 | } | |
1955 | } | |
1956 | } | |
1957 | else if (unsignedp0 && unsignedp1 | |
1958 | && (TYPE_PRECISION (TREE_TYPE (op0)) | |
1959 | < TYPE_PRECISION (result_type)) | |
1960 | && (TYPE_PRECISION (TREE_TYPE (op1)) | |
1961 | < TYPE_PRECISION (result_type))) | |
1962 | warning_at (location, OPT_Wsign_compare, | |
1963 | "comparison of promoted ~unsigned with unsigned"); | |
1964 | } | |
1965 | } | |
1966 | ||
1967 | /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common | |
1968 | type via c_common_type. If -Wdouble-promotion is in use, and the | |
1969 | conditions for warning have been met, issue a warning. GMSGID is | |
1970 | the warning message. It must have two %T specifiers for the type | |
1971 | that was converted (generally "float") and the type to which it was | |
1972 | converted (generally "double), respectively. LOC is the location | |
2fbe7a32 | 1973 | to which the warning should refer. */ |
0f4cea33 | 1974 | |
1975 | void | |
1976 | do_warn_double_promotion (tree result_type, tree type1, tree type2, | |
1977 | const char *gmsgid, location_t loc) | |
1978 | { | |
1979 | tree source_type; | |
1980 | ||
1981 | if (!warn_double_promotion) | |
1982 | return; | |
1983 | /* If the conversion will not occur at run-time, there is no need to | |
1984 | warn about it. */ | |
1985 | if (c_inhibit_evaluation_warnings) | |
1986 | return; | |
0396f790 | 1987 | /* If an invalid conversion has occured, don't warn. */ |
1988 | if (result_type == error_mark_node) | |
1989 | return; | |
0f4cea33 | 1990 | if (TYPE_MAIN_VARIANT (result_type) != double_type_node |
1991 | && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) | |
1992 | return; | |
1993 | if (TYPE_MAIN_VARIANT (type1) == float_type_node | |
1994 | || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) | |
1995 | source_type = type1; | |
1996 | else if (TYPE_MAIN_VARIANT (type2) == float_type_node | |
1997 | || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) | |
1998 | source_type = type2; | |
1999 | else | |
2000 | return; | |
2001 | warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); | |
2002 | } | |
2003 | ||
2004 | /* Possibly warn about unused parameters. */ | |
2005 | ||
2006 | void | |
2007 | do_warn_unused_parameter (tree fn) | |
2008 | { | |
2009 | tree decl; | |
2010 | ||
2011 | for (decl = DECL_ARGUMENTS (fn); | |
2012 | decl; decl = DECL_CHAIN (decl)) | |
2013 | if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL | |
2014 | && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl) | |
2015 | && !TREE_NO_WARNING (decl)) | |
2016 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter, | |
2017 | "unused parameter %qD", decl); | |
2018 | } | |
2019 | ||
2020 | /* If DECL is a typedef that is declared in the current function, | |
2021 | record it for the purpose of -Wunused-local-typedefs. */ | |
2022 | ||
2023 | void | |
2024 | record_locally_defined_typedef (tree decl) | |
2025 | { | |
2026 | struct c_language_function *l; | |
2027 | ||
2028 | if (!warn_unused_local_typedefs | |
2029 | || cfun == NULL | |
2030 | /* if this is not a locally defined typedef then we are not | |
2031 | interested. */ | |
2032 | || !is_typedef_decl (decl) | |
2033 | || !decl_function_context (decl)) | |
2034 | return; | |
2035 | ||
2036 | l = (struct c_language_function *) cfun->language; | |
2037 | vec_safe_push (l->local_typedefs, decl); | |
2038 | } | |
2039 | ||
2040 | /* If T is a TYPE_DECL declared locally, mark it as used. */ | |
2041 | ||
2042 | void | |
2043 | maybe_record_typedef_use (tree t) | |
2044 | { | |
2045 | if (!is_typedef_decl (t)) | |
2046 | return; | |
2047 | ||
2048 | TREE_USED (t) = true; | |
2049 | } | |
2050 | ||
2051 | /* Warn if there are some unused locally defined typedefs in the | |
2052 | current function. */ | |
2053 | ||
2054 | void | |
2055 | maybe_warn_unused_local_typedefs (void) | |
2056 | { | |
2057 | int i; | |
2058 | tree decl; | |
2059 | /* The number of times we have emitted -Wunused-local-typedefs | |
2060 | warnings. If this is different from errorcount, that means some | |
2061 | unrelated errors have been issued. In which case, we'll avoid | |
2062 | emitting "unused-local-typedefs" warnings. */ | |
2063 | static int unused_local_typedefs_warn_count; | |
2064 | struct c_language_function *l; | |
2065 | ||
2066 | if (cfun == NULL) | |
2067 | return; | |
2068 | ||
2069 | if ((l = (struct c_language_function *) cfun->language) == NULL) | |
2070 | return; | |
2071 | ||
2072 | if (warn_unused_local_typedefs | |
2073 | && errorcount == unused_local_typedefs_warn_count) | |
2074 | { | |
2075 | FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl) | |
2076 | if (!TREE_USED (decl)) | |
2077 | warning_at (DECL_SOURCE_LOCATION (decl), | |
2078 | OPT_Wunused_local_typedefs, | |
2079 | "typedef %qD locally defined but not used", decl); | |
2080 | unused_local_typedefs_warn_count = errorcount; | |
2081 | } | |
2082 | ||
2083 | vec_free (l->local_typedefs); | |
2084 | } | |
2085 | ||
2086 | /* If we're creating an if-else-if condition chain, first see if we | |
2087 | already have this COND in the CHAIN. If so, warn and don't add COND | |
2088 | into the vector, otherwise add the COND there. LOC is the location | |
2089 | of COND. */ | |
2090 | ||
2091 | void | |
2092 | warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain) | |
2093 | { | |
2094 | /* No chain has been created yet. Do nothing. */ | |
2095 | if (*chain == NULL) | |
2096 | return; | |
2097 | ||
2098 | if (TREE_SIDE_EFFECTS (cond)) | |
2099 | { | |
2100 | /* Uh-oh! This condition has a side-effect, thus invalidates | |
2101 | the whole chain. */ | |
2102 | delete *chain; | |
2103 | *chain = NULL; | |
2104 | return; | |
2105 | } | |
2106 | ||
2107 | unsigned int ix; | |
2108 | tree t; | |
2109 | bool found = false; | |
2110 | FOR_EACH_VEC_ELT (**chain, ix, t) | |
2111 | if (operand_equal_p (cond, t, 0)) | |
2112 | { | |
2113 | if (warning_at (loc, OPT_Wduplicated_cond, | |
2114 | "duplicated %<if%> condition")) | |
2115 | inform (EXPR_LOCATION (t), "previously used here"); | |
2116 | found = true; | |
2117 | break; | |
2118 | } | |
2119 | ||
2120 | if (!found | |
2121 | && !CONSTANT_CLASS_P (cond) | |
2122 | /* Don't infinitely grow the chain. */ | |
2123 | && (*chain)->length () < 512) | |
2124 | (*chain)->safe_push (cond); | |
2125 | } | |
2126 | ||
2127 | /* Check and possibly warn if two declarations have contradictory | |
2128 | attributes, such as always_inline vs. noinline. */ | |
2129 | ||
2130 | bool | |
2131 | diagnose_mismatched_attributes (tree olddecl, tree newdecl) | |
2132 | { | |
2133 | bool warned = false; | |
2134 | ||
2135 | tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl)); | |
2136 | tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl)); | |
2137 | /* An optimization attribute applied on a declaration after the | |
2138 | definition is likely not what the user wanted. */ | |
2139 | if (a2 != NULL_TREE | |
2140 | && DECL_SAVED_TREE (olddecl) != NULL_TREE | |
2141 | && (a1 == NULL_TREE || !attribute_list_equal (a1, a2))) | |
2142 | warned |= warning (OPT_Wattributes, | |
2143 | "optimization attribute on %qD follows " | |
2144 | "definition but the attribute doesn%'t match", | |
2145 | newdecl); | |
2146 | ||
2147 | /* Diagnose inline __attribute__ ((noinline)) which is silly. */ | |
2148 | if (DECL_DECLARED_INLINE_P (newdecl) | |
2149 | && DECL_UNINLINABLE (olddecl) | |
2150 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
2151 | warned |= warning (OPT_Wattributes, "inline declaration of %qD follows " | |
2152 | "declaration with attribute noinline", newdecl); | |
2153 | else if (DECL_DECLARED_INLINE_P (olddecl) | |
2154 | && DECL_UNINLINABLE (newdecl) | |
2155 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
2156 | warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " | |
2157 | "noinline follows inline declaration ", newdecl); | |
2158 | else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)) | |
2159 | && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl))) | |
2160 | warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " | |
2161 | "%qs follows declaration with attribute %qs", | |
2162 | newdecl, "noinline", "always_inline"); | |
2163 | else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl)) | |
2164 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
2165 | warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " | |
2166 | "%qs follows declaration with attribute %qs", | |
2167 | newdecl, "always_inline", "noinline"); | |
2168 | else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl)) | |
2169 | && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl))) | |
2170 | warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " | |
2171 | "%qs follows declaration with attribute %qs", | |
2172 | newdecl, "cold", "hot"); | |
2173 | else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl)) | |
2174 | && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl))) | |
2175 | warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " | |
2176 | "%qs follows declaration with attribute %qs", | |
2177 | newdecl, "hot", "cold"); | |
2178 | return warned; | |
2179 | } | |
2180 | ||
2181 | /* Warn if signed left shift overflows. We don't warn | |
2182 | about left-shifting 1 into the sign bit in C++14; cf. | |
2183 | <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457> | |
2184 | LOC is a location of the shift; OP0 and OP1 are the operands. | |
2185 | Return true if an overflow is detected, false otherwise. */ | |
2186 | ||
2187 | bool | |
2188 | maybe_warn_shift_overflow (location_t loc, tree op0, tree op1) | |
2189 | { | |
2190 | if (TREE_CODE (op0) != INTEGER_CST | |
2191 | || TREE_CODE (op1) != INTEGER_CST) | |
2192 | return false; | |
2193 | ||
2194 | tree type0 = TREE_TYPE (op0); | |
2195 | unsigned int prec0 = TYPE_PRECISION (type0); | |
2196 | ||
2197 | /* Left-hand operand must be signed. */ | |
2198 | if (TYPE_UNSIGNED (type0)) | |
2199 | return false; | |
2200 | ||
2201 | unsigned int min_prec = (wi::min_precision (op0, SIGNED) | |
2202 | + TREE_INT_CST_LOW (op1)); | |
2203 | /* Handle the case of left-shifting 1 into the sign bit. | |
2204 | * However, shifting 1 _out_ of the sign bit, as in | |
2205 | * INT_MIN << 1, is considered an overflow. | |
2206 | */ | |
2207 | if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1) | |
2208 | { | |
2209 | /* Never warn for C++14 onwards. */ | |
2210 | if (cxx_dialect >= cxx14) | |
2211 | return false; | |
2212 | /* Otherwise only if -Wshift-overflow=2. But return | |
2213 | true to signal an overflow for the sake of integer | |
2214 | constant expressions. */ | |
2215 | if (warn_shift_overflow < 2) | |
2216 | return true; | |
2217 | } | |
2218 | ||
2219 | bool overflowed = min_prec > prec0; | |
2220 | if (overflowed && c_inhibit_evaluation_warnings == 0) | |
2221 | warning_at (loc, OPT_Wshift_overflow_, | |
2222 | "result of %qE requires %u bits to represent, " | |
2223 | "but %qT only has %u bits", | |
2224 | build2_loc (loc, LSHIFT_EXPR, type0, op0, op1), | |
2225 | min_prec, type0, prec0); | |
2226 | ||
2227 | return overflowed; | |
2228 | } | |
2229 | ||
2230 | /* Warn about boolean expression compared with an integer value different | |
2231 | from true/false. Warns also e.g. about "(i1 == i2) == 2". | |
2232 | LOC is the location of the comparison, CODE is its code, OP0 and OP1 | |
2233 | are the operands of the comparison. The caller must ensure that | |
2234 | either operand is a boolean expression. */ | |
2235 | ||
2236 | void | |
2237 | maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0, | |
2238 | tree op1) | |
2239 | { | |
2240 | if (TREE_CODE_CLASS (code) != tcc_comparison) | |
2241 | return; | |
2242 | ||
2243 | tree f, cst; | |
2244 | if (f = fold_for_warn (op0), | |
2245 | TREE_CODE (f) == INTEGER_CST) | |
2246 | cst = op0 = f; | |
2247 | else if (f = fold_for_warn (op1), | |
2248 | TREE_CODE (f) == INTEGER_CST) | |
2249 | cst = op1 = f; | |
2250 | else | |
2251 | return; | |
2252 | ||
2253 | if (!integer_zerop (cst) && !integer_onep (cst)) | |
2254 | { | |
2255 | int sign = (TREE_CODE (op0) == INTEGER_CST | |
2256 | ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst)); | |
2257 | if (code == EQ_EXPR | |
2258 | || ((code == GT_EXPR || code == GE_EXPR) && sign < 0) | |
2259 | || ((code == LT_EXPR || code == LE_EXPR) && sign > 0)) | |
2260 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
2261 | "with boolean expression is always false", cst); | |
2262 | else | |
2263 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
2264 | "with boolean expression is always true", cst); | |
2265 | } | |
2266 | else if (integer_zerop (cst) || integer_onep (cst)) | |
2267 | { | |
2268 | /* If the non-constant operand isn't of a boolean type, we | |
2269 | don't want to warn here. */ | |
2270 | tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0; | |
2271 | /* Handle booleans promoted to integers. */ | |
2272 | if (bool_promoted_to_int_p (noncst)) | |
2273 | /* Warn. */; | |
2274 | else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE | |
2275 | && !truth_value_p (TREE_CODE (noncst))) | |
2276 | return; | |
2277 | /* Do some magic to get the right diagnostics. */ | |
2278 | bool flag = TREE_CODE (op0) == INTEGER_CST; | |
2279 | flag = integer_zerop (cst) ? flag : !flag; | |
2280 | if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag)) | |
2281 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
2282 | "with boolean expression is always true", cst); | |
2283 | else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag)) | |
2284 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
2285 | "with boolean expression is always false", cst); | |
2286 | } | |
2287 | } | |
9bf6a8e0 | 2288 | |
2289 | /* Warn if an argument at position param_pos is passed to a | |
2290 | restrict-qualified param, and it aliases with another argument. */ | |
2291 | ||
2292 | void | |
bbd5521e | 2293 | warn_for_restrict (unsigned param_pos, tree *argarray, unsigned nargs) |
9bf6a8e0 | 2294 | { |
bbd5521e | 2295 | tree arg = argarray[param_pos]; |
2296 | if (TREE_VISITED (arg) || integer_zerop (arg)) | |
9bf6a8e0 | 2297 | return; |
2298 | ||
2299 | location_t loc = EXPR_LOC_OR_LOC (arg, input_location); | |
2300 | gcc_rich_location richloc (loc); | |
2301 | ||
2302 | unsigned i; | |
bbd5521e | 2303 | auto_vec<int, 16> arg_positions; |
9bf6a8e0 | 2304 | |
bbd5521e | 2305 | for (i = 0; i < nargs; i++) |
9bf6a8e0 | 2306 | { |
2307 | if (i == param_pos) | |
2308 | continue; | |
2309 | ||
bbd5521e | 2310 | tree current_arg = argarray[i]; |
9bf6a8e0 | 2311 | if (operand_equal_p (arg, current_arg, 0)) |
2312 | { | |
2313 | TREE_VISITED (current_arg) = 1; | |
bbd5521e | 2314 | arg_positions.safe_push (i + 1); |
9bf6a8e0 | 2315 | } |
2316 | } | |
2317 | ||
bbd5521e | 2318 | if (arg_positions.is_empty ()) |
2319 | return; | |
9bf6a8e0 | 2320 | |
bbd5521e | 2321 | int pos; |
2322 | FOR_EACH_VEC_ELT (arg_positions, i, pos) | |
9bf6a8e0 | 2323 | { |
bbd5521e | 2324 | arg = argarray[pos - 1]; |
9bf6a8e0 | 2325 | if (EXPR_HAS_LOCATION (arg)) |
2326 | richloc.add_range (EXPR_LOCATION (arg), false); | |
2327 | } | |
2328 | ||
bbd5521e | 2329 | warning_at_rich_loc_n (&richloc, OPT_Wrestrict, arg_positions.length (), |
9bf6a8e0 | 2330 | "passing argument %i to restrict-qualified parameter" |
2331 | " aliases with argument %Z", | |
2332 | "passing argument %i to restrict-qualified parameter" | |
2333 | " aliases with arguments %Z", | |
bbd5521e | 2334 | param_pos + 1, arg_positions.address (), |
2335 | arg_positions.length ()); | |
9bf6a8e0 | 2336 | } |
3ef7eab1 | 2337 | |
2338 | /* Callback function to determine whether an expression TP or one of its | |
2339 | subexpressions comes from macro expansion. Used to suppress bogus | |
2340 | warnings. */ | |
2341 | ||
2342 | static tree | |
2343 | expr_from_macro_expansion_r (tree *tp, int *, void *) | |
2344 | { | |
2345 | if (CAN_HAVE_LOCATION_P (*tp) | |
2346 | && from_macro_expansion_at (EXPR_LOCATION (*tp))) | |
2347 | return integer_zero_node; | |
2348 | ||
2349 | return NULL_TREE; | |
2350 | } | |
2351 | ||
2352 | /* Possibly warn when an if-else has identical branches. */ | |
2353 | ||
2354 | static void | |
2355 | do_warn_duplicated_branches (tree expr) | |
2356 | { | |
2357 | tree thenb = COND_EXPR_THEN (expr); | |
2358 | tree elseb = COND_EXPR_ELSE (expr); | |
2359 | ||
314b4196 | 2360 | /* Don't bother if any of the branches is missing. */ |
2361 | if (thenb == NULL_TREE || elseb == NULL_TREE) | |
3ef7eab1 | 2362 | return; |
2363 | ||
2364 | /* And don't warn for empty statements. */ | |
2365 | if (TREE_CODE (thenb) == NOP_EXPR | |
2366 | && TREE_TYPE (thenb) == void_type_node | |
2367 | && TREE_OPERAND (thenb, 0) == size_zero_node) | |
2368 | return; | |
2369 | ||
2370 | /* ... or empty branches. */ | |
2371 | if (TREE_CODE (thenb) == STATEMENT_LIST | |
2372 | && STATEMENT_LIST_HEAD (thenb) == NULL) | |
2373 | return; | |
2374 | ||
2375 | /* Compute the hash of the then branch. */ | |
2376 | inchash::hash hstate0 (0); | |
2377 | inchash::add_expr (thenb, hstate0); | |
2378 | hashval_t h0 = hstate0.end (); | |
2379 | ||
2380 | /* Compute the hash of the else branch. */ | |
2381 | inchash::hash hstate1 (0); | |
2382 | inchash::add_expr (elseb, hstate1); | |
2383 | hashval_t h1 = hstate1.end (); | |
2384 | ||
2385 | /* Compare the hashes. */ | |
2386 | if (h0 == h1 | |
2387 | && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC) | |
2388 | /* Don't warn if any of the branches or their subexpressions comes | |
2389 | from a macro. */ | |
2390 | && !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r, | |
2391 | NULL) | |
2392 | && !walk_tree_without_duplicates (&elseb, expr_from_macro_expansion_r, | |
2393 | NULL)) | |
2394 | warning_at (EXPR_LOCATION (expr), OPT_Wduplicated_branches, | |
2395 | "this condition has identical branches"); | |
2396 | } | |
2397 | ||
2398 | /* Callback for c_genericize to implement -Wduplicated-branches. */ | |
2399 | ||
2400 | tree | |
2401 | do_warn_duplicated_branches_r (tree *tp, int *, void *) | |
2402 | { | |
2403 | if (TREE_CODE (*tp) == COND_EXPR) | |
2404 | do_warn_duplicated_branches (*tp); | |
2405 | return NULL_TREE; | |
2406 | } | |
2bfb0686 | 2407 | |
2408 | /* Implementation of -Wmultistatement-macros. This warning warns about | |
2409 | cases when a macro expands to multiple statements not wrapped in | |
2410 | do {} while (0) or ({ }) and is used as a body of if/else/for/while | |
2411 | conditionals. For example, | |
2412 | ||
2413 | #define DOIT x++; y++ | |
2414 | ||
2415 | if (c) | |
2416 | DOIT; | |
2417 | ||
2418 | will increment y unconditionally. | |
2419 | ||
2420 | BODY_LOC is the location of the first token in the body after labels | |
2421 | have been parsed, NEXT_LOC is the location of the next token after the | |
2422 | body of the conditional has been parsed, and GUARD_LOC is the location | |
2423 | of the conditional. */ | |
2424 | ||
2425 | void | |
2426 | warn_for_multistatement_macros (location_t body_loc, location_t next_loc, | |
2427 | location_t guard_loc, enum rid keyword) | |
2428 | { | |
2429 | if (!warn_multistatement_macros) | |
2430 | return; | |
2431 | ||
2432 | /* Ain't got time to waste. We only care about macros here. */ | |
2433 | if (!from_macro_expansion_at (body_loc) | |
2434 | || !from_macro_expansion_at (next_loc)) | |
2435 | return; | |
2436 | ||
2437 | /* Let's skip macros defined in system headers. */ | |
2438 | if (in_system_header_at (body_loc) | |
2439 | || in_system_header_at (next_loc)) | |
2440 | return; | |
2441 | ||
2442 | /* Find the actual tokens in the macro definition. BODY_LOC and | |
2443 | NEXT_LOC have to come from the same spelling location, but they | |
2444 | will resolve to different locations in the context of the macro | |
2445 | definition. */ | |
2446 | location_t body_loc_exp | |
2447 | = linemap_resolve_location (line_table, body_loc, | |
2448 | LRK_MACRO_DEFINITION_LOCATION, NULL); | |
2449 | location_t next_loc_exp | |
2450 | = linemap_resolve_location (line_table, next_loc, | |
2451 | LRK_MACRO_DEFINITION_LOCATION, NULL); | |
2452 | location_t guard_loc_exp | |
2453 | = linemap_resolve_location (line_table, guard_loc, | |
2454 | LRK_MACRO_DEFINITION_LOCATION, NULL); | |
2455 | ||
2456 | /* These are some funky cases we don't want to warn about. */ | |
2457 | if (body_loc_exp == guard_loc_exp | |
2458 | || next_loc_exp == guard_loc_exp | |
2459 | || body_loc_exp == next_loc_exp) | |
2460 | return; | |
2461 | ||
ef85bba9 | 2462 | /* Find the macro maps for the macro expansions. */ |
2463 | const line_map *body_map = linemap_lookup (line_table, body_loc); | |
2464 | const line_map *next_map = linemap_lookup (line_table, next_loc); | |
2465 | const line_map *guard_map = linemap_lookup (line_table, guard_loc); | |
2466 | ||
2467 | /* Now see if the following token (after the body) is coming from the | |
2468 | same macro expansion. If it is, it might be a problem. */ | |
2469 | if (body_map != next_map) | |
2470 | return; | |
2bfb0686 | 2471 | |
2472 | /* The conditional itself must not come from the same expansion, because | |
2473 | we don't want to warn about | |
2474 | #define IF if (x) x++; y++ | |
2475 | and similar. */ | |
ef85bba9 | 2476 | if (guard_map == body_map) |
2bfb0686 | 2477 | return; |
2478 | ||
ef85bba9 | 2479 | /* Handle the case where NEXT and BODY come from the same expansion while |
2480 | GUARD doesn't, yet we shouldn't warn. E.g. | |
2481 | ||
2482 | #define GUARD if (...) | |
2483 | #define GUARD2 GUARD | |
2484 | ||
2485 | and in the definition of another macro: | |
2486 | ||
2487 | GUARD2 | |
2488 | foo (); | |
2489 | return 1; | |
2490 | */ | |
2491 | while (linemap_macro_expansion_map_p (guard_map)) | |
2492 | { | |
2493 | const line_map_macro *mm = linemap_check_macro (guard_map); | |
2494 | guard_loc_exp = MACRO_MAP_EXPANSION_POINT_LOCATION (mm); | |
2495 | guard_map = linemap_lookup (line_table, guard_loc_exp); | |
2496 | if (guard_map == body_map) | |
2497 | return; | |
2498 | } | |
2499 | ||
2bfb0686 | 2500 | if (warning_at (body_loc, OPT_Wmultistatement_macros, |
2501 | "macro expands to multiple statements")) | |
2502 | inform (guard_loc, "some parts of macro expansion are not guarded by " | |
2503 | "this %qs clause", guard_tinfo_to_string (keyword)); | |
2504 | } |