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