1 | /* Diagnostic routines shared by all languages that are variants of C. |
2 | Copyright (C) 1992-2023 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 | #define INCLUDE_STRING |
21 | #include "config.h" |
22 | #include "system.h" |
23 | #include "coretypes.h" |
24 | #include "target.h" |
25 | #include "function.h" |
26 | #include "tree.h" |
27 | #include "c-common.h" |
28 | #include "memmodel.h" |
29 | #include "tm_p.h" |
30 | #include "diagnostic.h" |
31 | #include "intl.h" |
32 | #include "stringpool.h" |
33 | #include "attribs.h" |
34 | #include "asan.h" |
35 | #include "gcc-rich-location.h" |
36 | #include "gimplify.h" |
37 | #include "c-family/c-indentation.h" |
38 | #include "c-family/c-spellcheck.h" |
39 | #include "calls.h" |
40 | #include "stor-layout.h" |
41 | #include "tree-pretty-print.h" |
42 | #include "langhooks.h" |
43 | |
44 | /* Print a warning if a constant expression had overflow in folding. |
45 | Invoke this function on every expression that the language |
46 | requires to be a constant expression. |
47 | Note the ANSI C standard says it is erroneous for a |
48 | constant expression to overflow. */ |
49 | |
50 | void |
51 | constant_expression_warning (tree value) |
52 | { |
53 | if (warn_overflow && pedantic |
54 | && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
55 | || TREE_CODE (value) == FIXED_CST |
56 | || TREE_CODE (value) == VECTOR_CST |
57 | || TREE_CODE (value) == COMPLEX_CST) |
58 | && TREE_OVERFLOW (value)) |
59 | pedwarn (input_location, OPT_Woverflow, "overflow in constant expression" ); |
60 | } |
61 | |
62 | /* The same as above but print an unconditional error. */ |
63 | |
64 | void |
65 | constant_expression_error (tree value) |
66 | { |
67 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
68 | || TREE_CODE (value) == FIXED_CST |
69 | || TREE_CODE (value) == VECTOR_CST |
70 | || TREE_CODE (value) == COMPLEX_CST) |
71 | && TREE_OVERFLOW (value)) |
72 | error ("overflow in constant expression" ); |
73 | } |
74 | |
75 | /* Print a warning if an expression result VALUE had an overflow |
76 | in folding and its operands hadn't. EXPR, which may be null, is |
77 | the operand of the expression. |
78 | |
79 | Invoke this function on every expression that |
80 | (1) appears in the source code, and |
81 | (2) is a constant expression that overflowed, and |
82 | (3) is not already checked by convert_and_check; |
83 | however, do not invoke this function on operands of explicit casts |
84 | or when the expression is the result of an operator and any operand |
85 | already overflowed. */ |
86 | |
87 | void |
88 | overflow_warning (location_t loc, tree value, tree expr) |
89 | { |
90 | if (c_inhibit_evaluation_warnings != 0) |
91 | return; |
92 | |
93 | const char *warnfmt = NULL; |
94 | |
95 | switch (TREE_CODE (value)) |
96 | { |
97 | case INTEGER_CST: |
98 | warnfmt = (expr |
99 | ? G_("integer overflow in expression %qE of type %qT " |
100 | "results in %qE" ) |
101 | : G_("integer overflow in expression of type %qT " |
102 | "results in %qE" )); |
103 | break; |
104 | |
105 | case REAL_CST: |
106 | warnfmt = (expr |
107 | ? G_("floating point overflow in expression %qE " |
108 | "of type %qT results in %qE" ) |
109 | : G_("floating point overflow in expression of type %qT " |
110 | "results in %qE" )); |
111 | break; |
112 | |
113 | case FIXED_CST: |
114 | warnfmt = (expr |
115 | ? G_("fixed-point overflow in expression %qE of type %qT " |
116 | "results in %qE" ) |
117 | : G_("fixed-point overflow in expression of type %qT " |
118 | "results in %qE" )); |
119 | break; |
120 | |
121 | case VECTOR_CST: |
122 | warnfmt = (expr |
123 | ? G_("vector overflow in expression %qE of type %qT " |
124 | "results in %qE" ) |
125 | : G_("vector overflow in expression of type %qT " |
126 | "results in %qE" )); |
127 | break; |
128 | |
129 | case COMPLEX_CST: |
130 | if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) |
131 | warnfmt = (expr |
132 | ? G_("complex integer overflow in expression %qE " |
133 | "of type %qT results in %qE" ) |
134 | : G_("complex integer overflow in expression of type %qT " |
135 | "results in %qE" )); |
136 | else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) |
137 | warnfmt = (expr |
138 | ? G_("complex floating point overflow in expression %qE " |
139 | "of type %qT results in %qE" ) |
140 | : G_("complex floating point overflow in expression " |
141 | "of type %qT results in %qE" )); |
142 | else |
143 | return; |
144 | break; |
145 | |
146 | default: |
147 | return; |
148 | } |
149 | |
150 | bool warned; |
151 | if (expr) |
152 | warned = warning_at (loc, OPT_Woverflow, warnfmt, expr, TREE_TYPE (expr), |
153 | value); |
154 | else |
155 | warned = warning_at (loc, OPT_Woverflow, warnfmt, TREE_TYPE (value), |
156 | value); |
157 | |
158 | if (warned) |
159 | suppress_warning (value, OPT_Woverflow); |
160 | } |
161 | |
162 | /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression |
163 | pointed to by TP. */ |
164 | |
165 | static tree |
166 | unwrap_c_maybe_const (tree *tp, int *walk_subtrees, void *) |
167 | { |
168 | if (TREE_CODE (*tp) == C_MAYBE_CONST_EXPR) |
169 | { |
170 | *tp = C_MAYBE_CONST_EXPR_EXPR (*tp); |
171 | /* C_MAYBE_CONST_EXPRs don't nest. */ |
172 | *walk_subtrees = false; |
173 | } |
174 | return NULL_TREE; |
175 | } |
176 | |
177 | /* Warn about uses of logical || / && operator in a context where it |
178 | is likely that the bitwise equivalent was intended by the |
179 | programmer. We have seen an expression in which CODE is a binary |
180 | operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding |
181 | had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ |
182 | |
183 | void |
184 | warn_logical_operator (location_t location, enum tree_code code, tree type, |
185 | enum tree_code code_left, tree op_left, |
186 | enum tree_code ARG_UNUSED (code_right), tree op_right) |
187 | { |
188 | int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); |
189 | int in0_p, in1_p, in_p; |
190 | tree low0, low1, low, high0, high1, high, lhs, rhs, tem; |
191 | bool strict_overflow_p = false; |
192 | |
193 | if (!warn_logical_op) |
194 | return; |
195 | |
196 | if (code != TRUTH_ANDIF_EXPR |
197 | && code != TRUTH_AND_EXPR |
198 | && code != TRUTH_ORIF_EXPR |
199 | && code != TRUTH_OR_EXPR) |
200 | return; |
201 | |
202 | /* We don't want to warn if either operand comes from a macro |
203 | expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet; |
204 | see PR61534. */ |
205 | if (from_macro_expansion_at (EXPR_LOCATION (op_left)) |
206 | || from_macro_expansion_at (EXPR_LOCATION (op_right))) |
207 | return; |
208 | |
209 | /* Warn if &&/|| are being used in a context where it is |
210 | likely that the bitwise equivalent was intended by the |
211 | programmer. That is, an expression such as op && MASK |
212 | where op should not be any boolean expression, nor a |
213 | constant, and mask seems to be a non-boolean integer constant. */ |
214 | STRIP_ANY_LOCATION_WRAPPER (op_right); |
215 | if (TREE_CODE (op_right) == CONST_DECL) |
216 | /* An enumerator counts as a constant. */ |
217 | op_right = DECL_INITIAL (op_right); |
218 | tree stripped_op_left = tree_strip_any_location_wrapper (exp: op_left); |
219 | if (!truth_value_p (code: code_left) |
220 | && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) |
221 | && !CONSTANT_CLASS_P (stripped_op_left) |
222 | && TREE_CODE (stripped_op_left) != CONST_DECL |
223 | && !warning_suppressed_p (op_left, OPT_Wlogical_op) |
224 | && TREE_CODE (op_right) == INTEGER_CST |
225 | && !integer_zerop (op_right) |
226 | && !integer_onep (op_right)) |
227 | { |
228 | bool warned; |
229 | if (or_op) |
230 | warned |
231 | = warning_at (location, OPT_Wlogical_op, |
232 | "logical %<or%> applied to non-boolean constant" ); |
233 | else |
234 | warned |
235 | = warning_at (location, OPT_Wlogical_op, |
236 | "logical %<and%> applied to non-boolean constant" ); |
237 | if (warned) |
238 | suppress_warning (op_left, OPT_Wlogical_op); |
239 | return; |
240 | } |
241 | |
242 | /* We do not warn for constants because they are typical of macro |
243 | expansions that test for features. */ |
244 | if (CONSTANT_CLASS_P (fold_for_warn (op_left)) |
245 | || CONSTANT_CLASS_P (fold_for_warn (op_right))) |
246 | return; |
247 | |
248 | /* This warning only makes sense with logical operands. */ |
249 | if (!(truth_value_p (TREE_CODE (op_left)) |
250 | || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) |
251 | || !(truth_value_p (TREE_CODE (op_right)) |
252 | || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) |
253 | return; |
254 | |
255 | /* The range computations only work with scalars. */ |
256 | if (VECTOR_TYPE_P (TREE_TYPE (op_left)) |
257 | || VECTOR_TYPE_P (TREE_TYPE (op_right))) |
258 | return; |
259 | |
260 | /* We first test whether either side separately is trivially true |
261 | (with OR) or trivially false (with AND). If so, do not warn. |
262 | This is a common idiom for testing ranges of data types in |
263 | portable code. */ |
264 | op_left = unshare_expr (op_left); |
265 | walk_tree_without_duplicates (&op_left, unwrap_c_maybe_const, NULL); |
266 | lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); |
267 | if (!lhs) |
268 | return; |
269 | |
270 | /* If this is an OR operation, invert both sides; now, the result |
271 | should be always false to get a warning. */ |
272 | if (or_op) |
273 | in0_p = !in0_p; |
274 | |
275 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); |
276 | if (tem && integer_zerop (tem)) |
277 | return; |
278 | |
279 | op_right = unshare_expr (op_right); |
280 | walk_tree_without_duplicates (&op_right, unwrap_c_maybe_const, NULL); |
281 | rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); |
282 | if (!rhs) |
283 | return; |
284 | |
285 | /* If this is an OR operation, invert both sides; now, the result |
286 | should be always false to get a warning. */ |
287 | if (or_op) |
288 | in1_p = !in1_p; |
289 | |
290 | tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); |
291 | if (tem && integer_zerop (tem)) |
292 | return; |
293 | |
294 | /* If both expressions have the same operand, if we can merge the |
295 | ranges, ... */ |
296 | if (operand_equal_p (lhs, rhs, flags: 0) |
297 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, |
298 | in1_p, low1, high1)) |
299 | { |
300 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high); |
301 | /* ... and if the range test is always false, then warn. */ |
302 | if (tem && integer_zerop (tem)) |
303 | { |
304 | if (or_op) |
305 | warning_at (location, OPT_Wlogical_op, |
306 | "logical %<or%> of collectively exhaustive tests is " |
307 | "always true" ); |
308 | else |
309 | warning_at (location, OPT_Wlogical_op, |
310 | "logical %<and%> of mutually exclusive tests is " |
311 | "always false" ); |
312 | } |
313 | /* Or warn if the operands have exactly the same range, e.g. |
314 | A > 0 && A > 0. */ |
315 | else if (tree_int_cst_equal (low0, low1) |
316 | && tree_int_cst_equal (high0, high1)) |
317 | { |
318 | if (or_op) |
319 | warning_at (location, OPT_Wlogical_op, |
320 | "logical %<or%> of equal expressions" ); |
321 | else |
322 | warning_at (location, OPT_Wlogical_op, |
323 | "logical %<and%> of equal expressions" ); |
324 | } |
325 | } |
326 | } |
327 | |
328 | /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs |
329 | with constant indices. */ |
330 | |
331 | static tree |
332 | find_array_ref_with_const_idx_r (tree *expr_p, int *, void *) |
333 | { |
334 | tree expr = *expr_p; |
335 | |
336 | if ((TREE_CODE (expr) == ARRAY_REF |
337 | || TREE_CODE (expr) == ARRAY_RANGE_REF) |
338 | && (TREE_CODE (fold_for_warn (TREE_OPERAND (expr, 1))) |
339 | == INTEGER_CST)) |
340 | return integer_type_node; |
341 | |
342 | return NULL_TREE; |
343 | } |
344 | |
345 | /* Subroutine of warn_tautological_cmp. Warn about bitwise comparison |
346 | that always evaluate to true or false. LOC is the location of the |
347 | ==/!= comparison specified by CODE; LHS and RHS are the usual operands |
348 | of this comparison. */ |
349 | |
350 | static void |
351 | warn_tautological_bitwise_comparison (const op_location_t &loc, tree_code code, |
352 | tree lhs, tree rhs) |
353 | { |
354 | if (code != EQ_EXPR && code != NE_EXPR) |
355 | return; |
356 | |
357 | /* Extract the operands from e.g. (x & 8) == 4. */ |
358 | tree bitop; |
359 | tree cst; |
360 | tree stripped_lhs = tree_strip_any_location_wrapper (exp: lhs); |
361 | tree stripped_rhs = tree_strip_any_location_wrapper (exp: rhs); |
362 | if ((TREE_CODE (lhs) == BIT_AND_EXPR |
363 | || TREE_CODE (lhs) == BIT_IOR_EXPR) |
364 | && TREE_CODE (stripped_rhs) == INTEGER_CST) |
365 | bitop = lhs, cst = stripped_rhs; |
366 | else if ((TREE_CODE (rhs) == BIT_AND_EXPR |
367 | || TREE_CODE (rhs) == BIT_IOR_EXPR) |
368 | && TREE_CODE (stripped_lhs) == INTEGER_CST) |
369 | bitop = rhs, cst = stripped_lhs; |
370 | else |
371 | return; |
372 | |
373 | tree bitopcst; |
374 | tree bitop_op0 = fold_for_warn (TREE_OPERAND (bitop, 0)); |
375 | if (TREE_CODE (bitop_op0) == INTEGER_CST) |
376 | bitopcst = bitop_op0; |
377 | else { |
378 | tree bitop_op1 = fold_for_warn (TREE_OPERAND (bitop, 1)); |
379 | if (TREE_CODE (bitop_op1) == INTEGER_CST) |
380 | bitopcst = bitop_op1; |
381 | else |
382 | return; |
383 | } |
384 | |
385 | /* Note that the two operands are from before the usual integer |
386 | conversions, so their types might not be the same. |
387 | Use the larger of the two precisions and ignore bits outside |
388 | of that. */ |
389 | int prec = MAX (TYPE_PRECISION (TREE_TYPE (cst)), |
390 | TYPE_PRECISION (TREE_TYPE (bitopcst))); |
391 | |
392 | wide_int bitopcstw = wi::to_wide (t: bitopcst, prec); |
393 | wide_int cstw = wi::to_wide (t: cst, prec); |
394 | |
395 | wide_int res; |
396 | if (TREE_CODE (bitop) == BIT_AND_EXPR) |
397 | res = bitopcstw & cstw; |
398 | else |
399 | res = bitopcstw | cstw; |
400 | |
401 | /* For BIT_AND only warn if (CST2 & CST1) != CST1, and |
402 | for BIT_OR only if (CST2 | CST1) != CST1. */ |
403 | if (res == cstw) |
404 | return; |
405 | |
406 | binary_op_rich_location richloc (loc, lhs, rhs, false); |
407 | if (code == EQ_EXPR) |
408 | warning_at (&richloc, OPT_Wtautological_compare, |
409 | "bitwise comparison always evaluates to false" ); |
410 | else |
411 | warning_at (&richloc, OPT_Wtautological_compare, |
412 | "bitwise comparison always evaluates to true" ); |
413 | } |
414 | |
415 | /* Given LOC from a macro expansion, return the map for the outermost |
416 | macro in the nest of expansions. */ |
417 | |
418 | static const line_map_macro * |
419 | get_outermost_macro_expansion (location_t loc) |
420 | { |
421 | gcc_assert (from_macro_expansion_at (loc)); |
422 | |
423 | const line_map *map = linemap_lookup (line_table, loc); |
424 | const line_map_macro *macro_map; |
425 | do |
426 | { |
427 | macro_map = linemap_check_macro (map); |
428 | loc = linemap_unwind_toward_expansion (line_table, loc, loc_map: &map); |
429 | } while (linemap_macro_expansion_map_p (map)); |
430 | |
431 | return macro_map; |
432 | } |
433 | |
434 | /* Given LOC_A and LOC_B from macro expansions, return true if |
435 | they are "spelled the same" i.e. if they are both directly from |
436 | expansion of the same non-function-like macro. */ |
437 | |
438 | static bool |
439 | spelled_the_same_p (location_t loc_a, location_t loc_b) |
440 | { |
441 | gcc_assert (from_macro_expansion_at (loc_a)); |
442 | gcc_assert (from_macro_expansion_at (loc_b)); |
443 | |
444 | const line_map_macro *map_a = get_outermost_macro_expansion (loc: loc_a); |
445 | const line_map_macro *map_b = get_outermost_macro_expansion (loc: loc_b); |
446 | |
447 | if (map_a->macro == map_b->macro) |
448 | if (!cpp_fun_like_macro_p (node: map_a->macro)) |
449 | return true; |
450 | |
451 | return false; |
452 | } |
453 | |
454 | /* Warn if a self-comparison always evaluates to true or false. LOC |
455 | is the location of the comparison with code CODE, LHS and RHS are |
456 | operands of the comparison. */ |
457 | |
458 | void |
459 | warn_tautological_cmp (const op_location_t &loc, enum tree_code code, |
460 | tree lhs, tree rhs) |
461 | { |
462 | if (TREE_CODE_CLASS (code) != tcc_comparison) |
463 | return; |
464 | |
465 | /* Don't warn for various macro expansions. */ |
466 | if (from_macro_expansion_at (loc)) |
467 | return; |
468 | bool lhs_in_macro = from_macro_expansion_at (EXPR_LOCATION (lhs)); |
469 | bool rhs_in_macro = from_macro_expansion_at (EXPR_LOCATION (rhs)); |
470 | if (lhs_in_macro || rhs_in_macro) |
471 | { |
472 | /* Don't warn if exactly one is from a macro. */ |
473 | if (!(lhs_in_macro && rhs_in_macro)) |
474 | return; |
475 | |
476 | /* If both are in a macro, only warn if they're spelled the same. */ |
477 | if (!spelled_the_same_p (EXPR_LOCATION (lhs), EXPR_LOCATION (rhs))) |
478 | return; |
479 | } |
480 | |
481 | warn_tautological_bitwise_comparison (loc, code, lhs, rhs); |
482 | |
483 | /* We do not warn for constants because they are typical of macro |
484 | expansions that test for features, sizeof, and similar. */ |
485 | if (CONSTANT_CLASS_P (fold_for_warn (lhs)) |
486 | || CONSTANT_CLASS_P (fold_for_warn (rhs))) |
487 | return; |
488 | |
489 | /* Don't warn for e.g. |
490 | HOST_WIDE_INT n; |
491 | ... |
492 | if (n == (long) n) ... |
493 | */ |
494 | if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR) |
495 | || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR)) |
496 | return; |
497 | |
498 | /* Don't warn if either LHS or RHS has an IEEE floating-point type. |
499 | It could be a NaN, and NaN never compares equal to anything, even |
500 | itself. */ |
501 | if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs))) |
502 | return; |
503 | |
504 | if (operand_equal_p (lhs, rhs, flags: 0)) |
505 | { |
506 | /* Don't warn about array references with constant indices; |
507 | these are likely to come from a macro. */ |
508 | if (walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r, |
509 | NULL)) |
510 | return; |
511 | const bool always_true = (code == EQ_EXPR || code == LE_EXPR |
512 | || code == GE_EXPR || code == UNLE_EXPR |
513 | || code == UNGE_EXPR || code == UNEQ_EXPR); |
514 | binary_op_rich_location richloc (loc, lhs, rhs, false); |
515 | if (always_true) |
516 | warning_at (&richloc, OPT_Wtautological_compare, |
517 | "self-comparison always evaluates to true" ); |
518 | else |
519 | warning_at (&richloc, OPT_Wtautological_compare, |
520 | "self-comparison always evaluates to false" ); |
521 | } |
522 | } |
523 | |
524 | /* Return true iff EXPR only contains boolean operands, or comparisons. */ |
525 | |
526 | static bool |
527 | expr_has_boolean_operands_p (tree expr) |
528 | { |
529 | STRIP_NOPS (expr); |
530 | |
531 | if (CONVERT_EXPR_P (expr)) |
532 | return bool_promoted_to_int_p (expr); |
533 | else if (UNARY_CLASS_P (expr)) |
534 | return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0)); |
535 | else if (BINARY_CLASS_P (expr)) |
536 | return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0)) |
537 | && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1))); |
538 | else if (COMPARISON_CLASS_P (expr)) |
539 | return true; |
540 | else |
541 | return false; |
542 | } |
543 | |
544 | /* Warn about logical not used on the left hand side operand of a comparison. |
545 | This function assumes that the LHS is inside of TRUTH_NOT_EXPR. |
546 | Do not warn if RHS is of a boolean type, a logical operator, or |
547 | a comparison. */ |
548 | |
549 | void |
550 | warn_logical_not_parentheses (location_t location, enum tree_code code, |
551 | tree lhs, tree rhs) |
552 | { |
553 | if (TREE_CODE_CLASS (code) != tcc_comparison |
554 | || TREE_TYPE (rhs) == NULL_TREE |
555 | || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE |
556 | || truth_value_p (TREE_CODE (rhs))) |
557 | return; |
558 | |
559 | /* Don't warn for expression like !x == ~(bool1 | bool2). */ |
560 | if (expr_has_boolean_operands_p (expr: rhs)) |
561 | return; |
562 | |
563 | /* Don't warn for !x == 0 or !y != 0, those are equivalent to |
564 | !(x == 0) or !(y != 0). */ |
565 | if ((code == EQ_EXPR || code == NE_EXPR) |
566 | && integer_zerop (rhs)) |
567 | return; |
568 | |
569 | auto_diagnostic_group d; |
570 | if (warning_at (location, OPT_Wlogical_not_parentheses, |
571 | "logical not is only applied to the left hand side of " |
572 | "comparison" ) |
573 | && EXPR_HAS_LOCATION (lhs)) |
574 | { |
575 | location_t lhs_loc = EXPR_LOCATION (lhs); |
576 | rich_location richloc (line_table, lhs_loc); |
577 | richloc.add_fixit_insert_before (where: lhs_loc, new_content: "(" ); |
578 | richloc.add_fixit_insert_after (where: lhs_loc, new_content: ")" ); |
579 | inform (&richloc, "add parentheses around left hand side " |
580 | "expression to silence this warning" ); |
581 | } |
582 | } |
583 | |
584 | /* Warn if EXP contains any computations whose results are not used. |
585 | Return true if a warning is printed; false otherwise. LOCUS is the |
586 | (potential) location of the expression. */ |
587 | |
588 | bool |
589 | warn_if_unused_value (const_tree exp, location_t locus, bool quiet) |
590 | { |
591 | restart: |
592 | if (TREE_USED (exp) || warning_suppressed_p (exp, OPT_Wunused_value)) |
593 | return false; |
594 | |
595 | /* Don't warn about void constructs. This includes casting to void, |
596 | void function calls, and statement expressions with a final cast |
597 | to void. */ |
598 | if (VOID_TYPE_P (TREE_TYPE (exp))) |
599 | return false; |
600 | |
601 | if (EXPR_HAS_LOCATION (exp)) |
602 | locus = EXPR_LOCATION (exp); |
603 | |
604 | switch (TREE_CODE (exp)) |
605 | { |
606 | case PREINCREMENT_EXPR: |
607 | case POSTINCREMENT_EXPR: |
608 | case PREDECREMENT_EXPR: |
609 | case POSTDECREMENT_EXPR: |
610 | case MODIFY_EXPR: |
611 | case INIT_EXPR: |
612 | case TARGET_EXPR: |
613 | case CALL_EXPR: |
614 | case TRY_CATCH_EXPR: |
615 | case EXIT_EXPR: |
616 | case VA_ARG_EXPR: |
617 | return false; |
618 | |
619 | case BIND_EXPR: |
620 | /* For a binding, warn if no side effect within it. */ |
621 | exp = BIND_EXPR_BODY (exp); |
622 | goto restart; |
623 | |
624 | case SAVE_EXPR: |
625 | case NON_LVALUE_EXPR: |
626 | case NOP_EXPR: |
627 | exp = TREE_OPERAND (exp, 0); |
628 | goto restart; |
629 | |
630 | case TRUTH_ORIF_EXPR: |
631 | case TRUTH_ANDIF_EXPR: |
632 | /* In && or ||, warn if 2nd operand has no side effect. */ |
633 | exp = TREE_OPERAND (exp, 1); |
634 | goto restart; |
635 | |
636 | case COMPOUND_EXPR: |
637 | if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, quiet)) |
638 | return true; |
639 | /* Let people do `(foo (), 0)' without a warning. */ |
640 | if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) |
641 | return false; |
642 | exp = TREE_OPERAND (exp, 1); |
643 | goto restart; |
644 | |
645 | case COND_EXPR: |
646 | /* If this is an expression with side effects, don't warn; this |
647 | case commonly appears in macro expansions. */ |
648 | if (TREE_SIDE_EFFECTS (exp)) |
649 | return false; |
650 | goto warn; |
651 | |
652 | case COMPLEX_EXPR: |
653 | /* Warn only if both operands are unused. */ |
654 | if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, quiet: true) |
655 | && warn_if_unused_value (TREE_OPERAND (exp, 1), locus, quiet: true)) |
656 | goto warn; |
657 | return false; |
658 | |
659 | case INDIRECT_REF: |
660 | /* Don't warn about automatic dereferencing of references, since |
661 | the user cannot control it. */ |
662 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) |
663 | { |
664 | exp = TREE_OPERAND (exp, 0); |
665 | goto restart; |
666 | } |
667 | /* Fall through. */ |
668 | |
669 | default: |
670 | /* Referencing a volatile value is a side effect, so don't warn. */ |
671 | if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) |
672 | && TREE_THIS_VOLATILE (exp)) |
673 | return false; |
674 | |
675 | /* If this is an expression which has no operands, there is no value |
676 | to be unused. There are no such language-independent codes, |
677 | but front ends may define such. */ |
678 | if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) |
679 | return false; |
680 | |
681 | warn: |
682 | if (quiet) |
683 | return true; |
684 | return warning_at (locus, OPT_Wunused_value, "value computed is not used" ); |
685 | } |
686 | } |
687 | |
688 | /* Print a warning about casts that might indicate violation of strict |
689 | aliasing rules if -Wstrict-aliasing is used and strict aliasing |
690 | mode is in effect. LOC is the location of the expression being |
691 | cast, EXPR might be from inside it. TYPE is the type we're casting |
692 | to. */ |
693 | |
694 | bool |
695 | strict_aliasing_warning (location_t loc, tree type, tree expr) |
696 | { |
697 | if (loc == UNKNOWN_LOCATION) |
698 | loc = input_location; |
699 | |
700 | /* Strip pointer conversion chains and get to the correct original type. */ |
701 | STRIP_NOPS (expr); |
702 | tree otype = TREE_TYPE (expr); |
703 | |
704 | if (!(flag_strict_aliasing |
705 | && POINTER_TYPE_P (type) |
706 | && POINTER_TYPE_P (otype) |
707 | && !VOID_TYPE_P (TREE_TYPE (type))) |
708 | /* If the type we are casting to is a ref-all pointer |
709 | dereferencing it is always valid. */ |
710 | || TYPE_REF_CAN_ALIAS_ALL (type)) |
711 | return false; |
712 | |
713 | if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR |
714 | && (DECL_P (TREE_OPERAND (expr, 0)) |
715 | || handled_component_p (TREE_OPERAND (expr, 0)))) |
716 | { |
717 | /* Casting the address of an object to non void pointer. Warn |
718 | if the cast breaks type based aliasing. */ |
719 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) |
720 | { |
721 | warning_at (loc, OPT_Wstrict_aliasing, |
722 | "type-punning to incomplete type " |
723 | "might break strict-aliasing rules" ); |
724 | return true; |
725 | } |
726 | else |
727 | { |
728 | /* warn_strict_aliasing >= 3. This includes the default (3). |
729 | Only warn if the cast is dereferenced immediately. */ |
730 | alias_set_type set1 |
731 | = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); |
732 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); |
733 | |
734 | if (set2 != 0 |
735 | && set1 != set2 |
736 | && !alias_set_subset_of (set2, set1) |
737 | && !alias_sets_conflict_p (set1, set2)) |
738 | { |
739 | warning_at (loc, OPT_Wstrict_aliasing, |
740 | "dereferencing type-punned " |
741 | "pointer will break strict-aliasing rules" ); |
742 | return true; |
743 | } |
744 | else if (warn_strict_aliasing == 2 |
745 | && !alias_sets_must_conflict_p (set1, set2)) |
746 | { |
747 | warning_at (loc, OPT_Wstrict_aliasing, |
748 | "dereferencing type-punned " |
749 | "pointer might break strict-aliasing rules" ); |
750 | return true; |
751 | } |
752 | } |
753 | } |
754 | else if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) |
755 | { |
756 | /* At this level, warn for any conversions, even if an address is |
757 | not taken in the same statement. This will likely produce many |
758 | false positives, but could be useful to pinpoint problems that |
759 | are not revealed at higher levels. */ |
760 | alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); |
761 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); |
762 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) |
763 | || !alias_sets_must_conflict_p (set1, set2)) |
764 | { |
765 | warning_at (loc, OPT_Wstrict_aliasing, |
766 | "dereferencing type-punned " |
767 | "pointer might break strict-aliasing rules" ); |
768 | return true; |
769 | } |
770 | } |
771 | |
772 | return false; |
773 | } |
774 | |
775 | /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with |
776 | sizeof as last operand of certain builtins. */ |
777 | |
778 | void |
779 | sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee, |
780 | vec<tree, va_gc> *params, tree *sizeof_arg, |
781 | bool (*comp_types) (tree, tree)) |
782 | { |
783 | tree type, dest = NULL_TREE, src = NULL_TREE, tem; |
784 | bool strop = false, cmp = false; |
785 | unsigned int idx = ~0; |
786 | location_t loc; |
787 | |
788 | if (TREE_CODE (callee) != FUNCTION_DECL |
789 | || !fndecl_built_in_p (node: callee, klass: BUILT_IN_NORMAL) |
790 | || vec_safe_length (v: params) <= 1) |
791 | return; |
792 | |
793 | enum built_in_function fncode = DECL_FUNCTION_CODE (decl: callee); |
794 | switch (fncode) |
795 | { |
796 | case BUILT_IN_STRNCMP: |
797 | case BUILT_IN_STRNCASECMP: |
798 | cmp = true; |
799 | /* FALLTHRU */ |
800 | case BUILT_IN_STRNCPY: |
801 | case BUILT_IN_STRNCPY_CHK: |
802 | case BUILT_IN_STRNCAT: |
803 | case BUILT_IN_STRNCAT_CHK: |
804 | case BUILT_IN_STPNCPY: |
805 | case BUILT_IN_STPNCPY_CHK: |
806 | strop = true; |
807 | /* FALLTHRU */ |
808 | case BUILT_IN_MEMCPY: |
809 | case BUILT_IN_MEMCPY_CHK: |
810 | case BUILT_IN_MEMMOVE: |
811 | case BUILT_IN_MEMMOVE_CHK: |
812 | if (params->length () < 3) |
813 | return; |
814 | src = (*params)[1]; |
815 | dest = (*params)[0]; |
816 | idx = 2; |
817 | break; |
818 | case BUILT_IN_BCOPY: |
819 | if (params->length () < 3) |
820 | return; |
821 | src = (*params)[0]; |
822 | dest = (*params)[1]; |
823 | idx = 2; |
824 | break; |
825 | case BUILT_IN_MEMCMP: |
826 | case BUILT_IN_BCMP: |
827 | if (params->length () < 3) |
828 | return; |
829 | src = (*params)[1]; |
830 | dest = (*params)[0]; |
831 | idx = 2; |
832 | cmp = true; |
833 | break; |
834 | case BUILT_IN_MEMSET: |
835 | case BUILT_IN_MEMSET_CHK: |
836 | if (params->length () < 3) |
837 | return; |
838 | dest = (*params)[0]; |
839 | idx = 2; |
840 | break; |
841 | case BUILT_IN_BZERO: |
842 | dest = (*params)[0]; |
843 | idx = 1; |
844 | break; |
845 | case BUILT_IN_STRNDUP: |
846 | src = (*params)[0]; |
847 | strop = true; |
848 | idx = 1; |
849 | break; |
850 | case BUILT_IN_MEMCHR: |
851 | if (params->length () < 3) |
852 | return; |
853 | src = (*params)[0]; |
854 | idx = 2; |
855 | break; |
856 | case BUILT_IN_SNPRINTF: |
857 | case BUILT_IN_SNPRINTF_CHK: |
858 | case BUILT_IN_VSNPRINTF: |
859 | case BUILT_IN_VSNPRINTF_CHK: |
860 | dest = (*params)[0]; |
861 | idx = 1; |
862 | strop = true; |
863 | break; |
864 | default: |
865 | break; |
866 | } |
867 | |
868 | if (idx >= 3) |
869 | return; |
870 | |
871 | /* Use error_operand_p to detect non-error arguments with an error |
872 | type that the C++ front-end constructs. */ |
873 | if (error_operand_p (t: src) |
874 | || error_operand_p (t: dest) |
875 | || !sizeof_arg[idx] |
876 | || error_operand_p (t: sizeof_arg[idx])) |
877 | return; |
878 | |
879 | type = TYPE_P (sizeof_arg[idx]) |
880 | ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]); |
881 | |
882 | if (!POINTER_TYPE_P (type)) |
883 | { |
884 | /* The argument type may be an array. Diagnose bounded string |
885 | copy functions that specify the bound in terms of the source |
886 | argument rather than the destination unless they are equal |
887 | to one another. Handle constant sizes and also try to handle |
888 | sizeof expressions involving VLAs. */ |
889 | if (strop && !cmp && fncode != BUILT_IN_STRNDUP && src) |
890 | { |
891 | tem = tree_strip_nop_conversions (src); |
892 | if (TREE_CODE (tem) == ADDR_EXPR) |
893 | tem = TREE_OPERAND (tem, 0); |
894 | |
895 | /* Avoid diagnosing sizeof SRC when SRC is declared with |
896 | attribute nonstring. */ |
897 | tree dummy; |
898 | if (get_attr_nonstring_decl (tem, &dummy)) |
899 | return; |
900 | |
901 | tree d = tree_strip_nop_conversions (dest); |
902 | if (TREE_CODE (d) == ADDR_EXPR) |
903 | d = TREE_OPERAND (d, 0); |
904 | |
905 | tree dstsz = TYPE_SIZE_UNIT (TREE_TYPE (d)); |
906 | tree srcsz = TYPE_SIZE_UNIT (TREE_TYPE (tem)); |
907 | |
908 | if ((!dstsz |
909 | || !srcsz |
910 | || !operand_equal_p (dstsz, srcsz, flags: OEP_LEXICOGRAPHIC)) |
911 | && operand_equal_p (tem, sizeof_arg[idx], flags: OEP_ADDRESS_OF)) |
912 | warning_at (sizeof_arg_loc[idx], OPT_Wsizeof_pointer_memaccess, |
913 | "argument to %<sizeof%> in %qD call is the same " |
914 | "expression as the source; did you mean to use " |
915 | "the size of the destination?" , |
916 | callee); |
917 | } |
918 | |
919 | return; |
920 | } |
921 | |
922 | if (dest |
923 | && (tem = tree_strip_nop_conversions (dest)) |
924 | && POINTER_TYPE_P (TREE_TYPE (tem)) |
925 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) |
926 | return; |
927 | |
928 | if (src |
929 | && (tem = tree_strip_nop_conversions (src)) |
930 | && POINTER_TYPE_P (TREE_TYPE (tem)) |
931 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) |
932 | return; |
933 | |
934 | loc = sizeof_arg_loc[idx]; |
935 | |
936 | if (dest && !cmp) |
937 | { |
938 | if (!TYPE_P (sizeof_arg[idx]) |
939 | && operand_equal_p (dest, sizeof_arg[idx], flags: 0) |
940 | && comp_types (TREE_TYPE (dest), type)) |
941 | { |
942 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
943 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
944 | "argument to %<sizeof%> in %qD call is the same " |
945 | "expression as the destination; did you mean to " |
946 | "remove the addressof?" , callee); |
947 | else if ((TYPE_PRECISION (TREE_TYPE (type)) |
948 | == TYPE_PRECISION (char_type_node)) |
949 | || strop) |
950 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
951 | "argument to %<sizeof%> in %qD call is the same " |
952 | "expression as the destination; did you mean to " |
953 | "provide an explicit length?" , callee); |
954 | else |
955 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
956 | "argument to %<sizeof%> in %qD call is the same " |
957 | "expression as the destination; did you mean to " |
958 | "dereference it?" , callee); |
959 | return; |
960 | } |
961 | |
962 | if (POINTER_TYPE_P (TREE_TYPE (dest)) |
963 | && !strop |
964 | && comp_types (TREE_TYPE (dest), type) |
965 | && !VOID_TYPE_P (TREE_TYPE (type))) |
966 | { |
967 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
968 | "argument to %<sizeof%> in %qD call is the same " |
969 | "pointer type %qT as the destination; expected %qT " |
970 | "or an explicit length" , callee, TREE_TYPE (dest), |
971 | TREE_TYPE (TREE_TYPE (dest))); |
972 | return; |
973 | } |
974 | } |
975 | |
976 | if (src && !cmp) |
977 | { |
978 | if (!TYPE_P (sizeof_arg[idx]) |
979 | && operand_equal_p (src, sizeof_arg[idx], flags: 0) |
980 | && comp_types (TREE_TYPE (src), type)) |
981 | { |
982 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
983 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
984 | "argument to %<sizeof%> in %qD call is the same " |
985 | "expression as the source; did you mean to " |
986 | "remove the addressof?" , callee); |
987 | else if ((TYPE_PRECISION (TREE_TYPE (type)) |
988 | == TYPE_PRECISION (char_type_node)) |
989 | || strop) |
990 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
991 | "argument to %<sizeof%> in %qD call is the same " |
992 | "expression as the source; did you mean to " |
993 | "provide an explicit length?" , callee); |
994 | else |
995 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
996 | "argument to %<sizeof%> in %qD call is the same " |
997 | "expression as the source; did you mean to " |
998 | "dereference it?" , callee); |
999 | return; |
1000 | } |
1001 | |
1002 | if (POINTER_TYPE_P (TREE_TYPE (src)) |
1003 | && !strop |
1004 | && comp_types (TREE_TYPE (src), type) |
1005 | && !VOID_TYPE_P (TREE_TYPE (type))) |
1006 | { |
1007 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1008 | "argument to %<sizeof%> in %qD call is the same " |
1009 | "pointer type %qT as the source; expected %qT " |
1010 | "or an explicit length" , callee, TREE_TYPE (src), |
1011 | TREE_TYPE (TREE_TYPE (src))); |
1012 | return; |
1013 | } |
1014 | } |
1015 | |
1016 | if (dest) |
1017 | { |
1018 | if (!TYPE_P (sizeof_arg[idx]) |
1019 | && operand_equal_p (dest, sizeof_arg[idx], flags: 0) |
1020 | && comp_types (TREE_TYPE (dest), type)) |
1021 | { |
1022 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
1023 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1024 | "argument to %<sizeof%> in %qD call is the same " |
1025 | "expression as the first source; did you mean to " |
1026 | "remove the addressof?" , callee); |
1027 | else if ((TYPE_PRECISION (TREE_TYPE (type)) |
1028 | == TYPE_PRECISION (char_type_node)) |
1029 | || strop) |
1030 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1031 | "argument to %<sizeof%> in %qD call is the same " |
1032 | "expression as the first source; did you mean to " |
1033 | "provide an explicit length?" , callee); |
1034 | else |
1035 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1036 | "argument to %<sizeof%> in %qD call is the same " |
1037 | "expression as the first source; did you mean to " |
1038 | "dereference it?" , callee); |
1039 | return; |
1040 | } |
1041 | |
1042 | if (POINTER_TYPE_P (TREE_TYPE (dest)) |
1043 | && !strop |
1044 | && comp_types (TREE_TYPE (dest), type) |
1045 | && !VOID_TYPE_P (TREE_TYPE (type))) |
1046 | { |
1047 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1048 | "argument to %<sizeof%> in %qD call is the same " |
1049 | "pointer type %qT as the first source; expected %qT " |
1050 | "or an explicit length" , callee, TREE_TYPE (dest), |
1051 | TREE_TYPE (TREE_TYPE (dest))); |
1052 | return; |
1053 | } |
1054 | } |
1055 | |
1056 | if (src) |
1057 | { |
1058 | if (!TYPE_P (sizeof_arg[idx]) |
1059 | && operand_equal_p (src, sizeof_arg[idx], flags: 0) |
1060 | && comp_types (TREE_TYPE (src), type)) |
1061 | { |
1062 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
1063 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1064 | "argument to %<sizeof%> in %qD call is the same " |
1065 | "expression as the second source; did you mean to " |
1066 | "remove the addressof?" , callee); |
1067 | else if ((TYPE_PRECISION (TREE_TYPE (type)) |
1068 | == TYPE_PRECISION (char_type_node)) |
1069 | || strop) |
1070 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1071 | "argument to %<sizeof%> in %qD call is the same " |
1072 | "expression as the second source; did you mean to " |
1073 | "provide an explicit length?" , callee); |
1074 | else |
1075 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1076 | "argument to %<sizeof%> in %qD call is the same " |
1077 | "expression as the second source; did you mean to " |
1078 | "dereference it?" , callee); |
1079 | return; |
1080 | } |
1081 | |
1082 | if (POINTER_TYPE_P (TREE_TYPE (src)) |
1083 | && !strop |
1084 | && comp_types (TREE_TYPE (src), type) |
1085 | && !VOID_TYPE_P (TREE_TYPE (type))) |
1086 | { |
1087 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1088 | "argument to %<sizeof%> in %qD call is the same " |
1089 | "pointer type %qT as the second source; expected %qT " |
1090 | "or an explicit length" , callee, TREE_TYPE (src), |
1091 | TREE_TYPE (TREE_TYPE (src))); |
1092 | return; |
1093 | } |
1094 | } |
1095 | |
1096 | } |
1097 | |
1098 | /* Warn for unlikely, improbable, or stupid DECL declarations |
1099 | of `main'. */ |
1100 | |
1101 | void |
1102 | check_main_parameter_types (tree decl) |
1103 | { |
1104 | function_args_iterator iter; |
1105 | tree type; |
1106 | int argct = 0; |
1107 | |
1108 | FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) |
1109 | { |
1110 | /* XXX void_type_node belies the abstraction. */ |
1111 | if (type == void_type_node || type == error_mark_node) |
1112 | break; |
1113 | |
1114 | tree t = type; |
1115 | if (TYPE_ATOMIC (t)) |
1116 | pedwarn (input_location, OPT_Wmain, |
1117 | "%<_Atomic%>-qualified parameter type %qT of %q+D" , |
1118 | type, decl); |
1119 | while (POINTER_TYPE_P (t)) |
1120 | { |
1121 | t = TREE_TYPE (t); |
1122 | if (TYPE_ATOMIC (t)) |
1123 | pedwarn (input_location, OPT_Wmain, |
1124 | "%<_Atomic%>-qualified parameter type %qT of %q+D" , |
1125 | type, decl); |
1126 | } |
1127 | |
1128 | ++argct; |
1129 | switch (argct) |
1130 | { |
1131 | case 1: |
1132 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) |
1133 | pedwarn (input_location, OPT_Wmain, |
1134 | "first argument of %q+D should be %<int%>" , decl); |
1135 | break; |
1136 | |
1137 | case 2: |
1138 | if (TREE_CODE (type) != POINTER_TYPE |
1139 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE |
1140 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) |
1141 | != char_type_node)) |
1142 | pedwarn (input_location, OPT_Wmain, |
1143 | "second argument of %q+D should be %<char **%>" , decl); |
1144 | break; |
1145 | |
1146 | case 3: |
1147 | if (TREE_CODE (type) != POINTER_TYPE |
1148 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE |
1149 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) |
1150 | != char_type_node)) |
1151 | pedwarn (input_location, OPT_Wmain, |
1152 | "third argument of %q+D should probably be " |
1153 | "%<char **%>" , decl); |
1154 | break; |
1155 | } |
1156 | } |
1157 | |
1158 | /* It is intentional that this message does not mention the third |
1159 | argument because it's only mentioned in an appendix of the |
1160 | standard. */ |
1161 | if (argct > 0 && (argct < 2 || argct > 3)) |
1162 | pedwarn (input_location, OPT_Wmain, |
1163 | "%q+D takes only zero or two arguments" , decl); |
1164 | |
1165 | if (stdarg_p (TREE_TYPE (decl))) |
1166 | pedwarn (input_location, OPT_Wmain, |
1167 | "%q+D declared as variadic function" , decl); |
1168 | } |
1169 | |
1170 | /* Warns and returns true if the conversion of EXPR to TYPE may alter a value. |
1171 | This is a helper function for warnings_for_convert_and_check. */ |
1172 | |
1173 | static bool |
1174 | conversion_warning (location_t loc, tree type, tree expr, tree result) |
1175 | { |
1176 | tree expr_type = TREE_TYPE (expr); |
1177 | enum conversion_safety conversion_kind; |
1178 | int arith_ops = 0; |
1179 | |
1180 | if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) |
1181 | return false; |
1182 | |
1183 | /* This may happen, because for LHS op= RHS we preevaluate |
1184 | RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which |
1185 | means we could no longer see the code of the EXPR. */ |
1186 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) |
1187 | expr = C_MAYBE_CONST_EXPR_EXPR (expr); |
1188 | if (TREE_CODE (expr) == SAVE_EXPR) |
1189 | expr = TREE_OPERAND (expr, 0); |
1190 | |
1191 | switch (TREE_CODE (expr)) |
1192 | { |
1193 | case EQ_EXPR: |
1194 | case NE_EXPR: |
1195 | case LE_EXPR: |
1196 | case GE_EXPR: |
1197 | case LT_EXPR: |
1198 | case GT_EXPR: |
1199 | case TRUTH_ANDIF_EXPR: |
1200 | case TRUTH_ORIF_EXPR: |
1201 | case TRUTH_AND_EXPR: |
1202 | case TRUTH_OR_EXPR: |
1203 | case TRUTH_XOR_EXPR: |
1204 | case TRUTH_NOT_EXPR: |
1205 | /* Conversion from boolean to a signed:1 bit-field (which only |
1206 | can hold the values 0 and -1) doesn't lose information - but |
1207 | it does change the value. */ |
1208 | if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) |
1209 | warning_at (loc, OPT_Wconversion, |
1210 | "conversion to %qT from boolean expression" , type); |
1211 | return true; |
1212 | |
1213 | case REAL_CST: |
1214 | case INTEGER_CST: |
1215 | case COMPLEX_CST: |
1216 | { |
1217 | conversion_kind = unsafe_conversion_p (type, expr, result, true); |
1218 | int warnopt; |
1219 | if (conversion_kind == UNSAFE_REAL) |
1220 | warnopt = OPT_Wfloat_conversion; |
1221 | else if (conversion_kind) |
1222 | warnopt = OPT_Wconversion; |
1223 | else |
1224 | break; |
1225 | |
1226 | if (conversion_kind == UNSAFE_SIGN) |
1227 | { |
1228 | bool cstresult |
1229 | = (result |
1230 | && CONSTANT_CLASS_P (result)); |
1231 | if (TYPE_UNSIGNED (type)) |
1232 | { |
1233 | if (cstresult) |
1234 | warning_at (loc, OPT_Wsign_conversion, |
1235 | "unsigned conversion from %qT to %qT " |
1236 | "changes value from %qE to %qE" , |
1237 | expr_type, type, expr, result); |
1238 | else |
1239 | warning_at (loc, OPT_Wsign_conversion, |
1240 | "unsigned conversion from %qT to %qT " |
1241 | "changes the value of %qE" , |
1242 | expr_type, type, expr); |
1243 | } |
1244 | else |
1245 | { |
1246 | if (cstresult) |
1247 | warning_at (loc, OPT_Wsign_conversion, |
1248 | "signed conversion from %qT to %qT changes " |
1249 | "value from %qE to %qE" , |
1250 | expr_type, type, expr, result); |
1251 | else |
1252 | warning_at (loc, OPT_Wsign_conversion, |
1253 | "signed conversion from %qT to %qT changes " |
1254 | "the value of %qE" , |
1255 | expr_type, type, expr); |
1256 | } |
1257 | } |
1258 | else if (CONSTANT_CLASS_P (result)) |
1259 | warning_at (loc, warnopt, |
1260 | "conversion from %qT to %qT changes value from %qE to %qE" , |
1261 | expr_type, type, expr, result); |
1262 | else |
1263 | warning_at (loc, warnopt, |
1264 | "conversion from %qT to %qT changes the value of %qE" , |
1265 | expr_type, type, expr); |
1266 | return true; |
1267 | } |
1268 | |
1269 | case PLUS_EXPR: |
1270 | case MINUS_EXPR: |
1271 | case MULT_EXPR: |
1272 | case MAX_EXPR: |
1273 | case MIN_EXPR: |
1274 | case TRUNC_MOD_EXPR: |
1275 | case FLOOR_MOD_EXPR: |
1276 | case TRUNC_DIV_EXPR: |
1277 | case FLOOR_DIV_EXPR: |
1278 | case CEIL_DIV_EXPR: |
1279 | case EXACT_DIV_EXPR: |
1280 | case RDIV_EXPR: |
1281 | arith_ops = 2; |
1282 | goto default_; |
1283 | |
1284 | case PREDECREMENT_EXPR: |
1285 | case PREINCREMENT_EXPR: |
1286 | case POSTDECREMENT_EXPR: |
1287 | case POSTINCREMENT_EXPR: |
1288 | case LSHIFT_EXPR: |
1289 | case RSHIFT_EXPR: |
1290 | case FIX_TRUNC_EXPR: |
1291 | case NON_LVALUE_EXPR: |
1292 | case NEGATE_EXPR: |
1293 | case BIT_NOT_EXPR: |
1294 | arith_ops = 1; |
1295 | goto default_; |
1296 | |
1297 | case COND_EXPR: |
1298 | { |
1299 | /* In case of COND_EXPR, we do not care about the type of |
1300 | COND_EXPR, only about the conversion of each operand. */ |
1301 | tree op1 = TREE_OPERAND (expr, 1); |
1302 | tree op2 = TREE_OPERAND (expr, 2); |
1303 | |
1304 | return ((op1 && conversion_warning (loc, type, expr: op1, result)) |
1305 | || conversion_warning (loc, type, expr: op2, result)); |
1306 | } |
1307 | |
1308 | case BIT_AND_EXPR: |
1309 | if ((TREE_CODE (expr_type) == INTEGER_TYPE |
1310 | || TREE_CODE (expr_type) == BITINT_TYPE) |
1311 | && (TREE_CODE (type) == INTEGER_TYPE |
1312 | || TREE_CODE (type) == BITINT_TYPE)) |
1313 | for (int i = 0; i < 2; ++i) |
1314 | { |
1315 | tree op = TREE_OPERAND (expr, i); |
1316 | if (TREE_CODE (op) != INTEGER_CST) |
1317 | continue; |
1318 | |
1319 | /* If one of the operands is a non-negative constant |
1320 | that fits in the target type, then the type of the |
1321 | other operand does not matter. */ |
1322 | if (int_fits_type_p (op, c_common_signed_type (type)) |
1323 | && int_fits_type_p (op, c_common_unsigned_type (type))) |
1324 | return false; |
1325 | |
1326 | /* If constant is unsigned and fits in the target |
1327 | type, then the result will also fit. */ |
1328 | if (TYPE_UNSIGNED (TREE_TYPE (op)) && int_fits_type_p (op, type)) |
1329 | return false; |
1330 | } |
1331 | /* FALLTHRU */ |
1332 | case BIT_IOR_EXPR: |
1333 | case BIT_XOR_EXPR: |
1334 | return (conversion_warning (loc, type, TREE_OPERAND (expr, 0), result) |
1335 | || conversion_warning (loc, type, TREE_OPERAND (expr, 1), |
1336 | result)); |
1337 | |
1338 | default_: |
1339 | default: |
1340 | conversion_kind = unsafe_conversion_p (type, expr, result, true); |
1341 | { |
1342 | int warnopt; |
1343 | if (conversion_kind == UNSAFE_REAL) |
1344 | warnopt = OPT_Wfloat_conversion; |
1345 | else if (conversion_kind == UNSAFE_SIGN) |
1346 | warnopt = OPT_Wsign_conversion; |
1347 | else if (conversion_kind) |
1348 | warnopt = OPT_Wconversion; |
1349 | else |
1350 | break; |
1351 | |
1352 | if (arith_ops |
1353 | && global_dc->m_option_enabled (warnopt, |
1354 | global_dc->m_lang_mask, |
1355 | global_dc->m_option_state)) |
1356 | { |
1357 | for (int i = 0; i < arith_ops; ++i) |
1358 | { |
1359 | tree op = TREE_OPERAND (expr, i); |
1360 | /* Avoid -Wsign-conversion for (unsigned)(x + (-1)). */ |
1361 | if (TREE_CODE (expr) == PLUS_EXPR && i == 1 |
1362 | && INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) |
1363 | && TREE_CODE (op) == INTEGER_CST |
1364 | && tree_int_cst_sgn (op) < 0) |
1365 | op = fold_build1 (NEGATE_EXPR, TREE_TYPE (op), op); |
1366 | tree opr = convert (type, op); |
1367 | if (unsafe_conversion_p (type, op, opr, true)) |
1368 | goto op_unsafe; |
1369 | } |
1370 | /* The operands seem safe, we might still want to warn if |
1371 | -Warith-conversion. */ |
1372 | warnopt = OPT_Warith_conversion; |
1373 | op_unsafe:; |
1374 | } |
1375 | |
1376 | if (conversion_kind == UNSAFE_SIGN) |
1377 | warning_at (loc, warnopt, "conversion to %qT from %qT " |
1378 | "may change the sign of the result" , |
1379 | type, expr_type); |
1380 | else if (conversion_kind == UNSAFE_IMAGINARY) |
1381 | warning_at (loc, warnopt, |
1382 | "conversion from %qT to %qT discards imaginary component" , |
1383 | expr_type, type); |
1384 | else |
1385 | warning_at (loc, warnopt, |
1386 | "conversion from %qT to %qT may change value" , |
1387 | expr_type, type); |
1388 | return true; |
1389 | } |
1390 | } |
1391 | return false; |
1392 | } |
1393 | |
1394 | /* Produce warnings after a conversion. RESULT is the result of |
1395 | converting EXPR to TYPE. This is a helper function for |
1396 | convert_and_check and cp_convert_and_check. */ |
1397 | |
1398 | void |
1399 | warnings_for_convert_and_check (location_t loc, tree type, tree expr, |
1400 | tree result) |
1401 | { |
1402 | loc = expansion_point_location_if_in_system_header (loc); |
1403 | |
1404 | while (TREE_CODE (expr) == COMPOUND_EXPR) |
1405 | expr = TREE_OPERAND (expr, 1); |
1406 | while (TREE_CODE (result) == COMPOUND_EXPR) |
1407 | result = TREE_OPERAND (result, 1); |
1408 | |
1409 | bool cst = CONSTANT_CLASS_P (result); |
1410 | tree exprtype = TREE_TYPE (expr); |
1411 | tree result_diag; |
1412 | /* We're interested in the actual numerical value here, not its ASCII |
1413 | representation. */ |
1414 | if (cst && TYPE_MAIN_VARIANT (TREE_TYPE (result)) == char_type_node) |
1415 | result_diag = fold_convert (integer_type_node, result); |
1416 | else |
1417 | result_diag = result; |
1418 | |
1419 | if (TREE_CODE (expr) == INTEGER_CST |
1420 | && (TREE_CODE (type) == INTEGER_TYPE |
1421 | || TREE_CODE (type) == BITINT_TYPE |
1422 | || (TREE_CODE (type) == ENUMERAL_TYPE |
1423 | && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) != BOOLEAN_TYPE)) |
1424 | && !int_fits_type_p (expr, type)) |
1425 | { |
1426 | /* Do not diagnose overflow in a constant expression merely |
1427 | because a conversion overflowed. */ |
1428 | if (TREE_OVERFLOW (result)) |
1429 | TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); |
1430 | |
1431 | if (TYPE_UNSIGNED (type)) |
1432 | { |
1433 | /* This detects cases like converting -129 or 256 to |
1434 | unsigned char. */ |
1435 | if (!int_fits_type_p (expr, c_common_signed_type (type))) |
1436 | { |
1437 | if (cst) |
1438 | warning_at (loc, OPT_Woverflow, |
1439 | (TYPE_UNSIGNED (exprtype) |
1440 | ? G_("conversion from %qT to %qT " |
1441 | "changes value from %qE to %qE" ) |
1442 | : G_("unsigned conversion from %qT to %qT " |
1443 | "changes value from %qE to %qE" )), |
1444 | exprtype, type, expr, result_diag); |
1445 | else |
1446 | warning_at (loc, OPT_Woverflow, |
1447 | (TYPE_UNSIGNED (exprtype) |
1448 | ? G_("conversion from %qT to %qT " |
1449 | "changes the value of %qE" ) |
1450 | : G_("unsigned conversion from %qT to %qT " |
1451 | "changes the value of %qE" )), |
1452 | exprtype, type, expr); |
1453 | } |
1454 | else |
1455 | conversion_warning (loc, type, expr, result); |
1456 | } |
1457 | else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) |
1458 | { |
1459 | if (cst) |
1460 | warning_at (loc, OPT_Woverflow, |
1461 | "overflow in conversion from %qT to %qT " |
1462 | "changes value from %qE to %qE" , |
1463 | exprtype, type, expr, result_diag); |
1464 | else |
1465 | warning_at (loc, OPT_Woverflow, |
1466 | "overflow in conversion from %qT to %qT " |
1467 | "changes the value of %qE" , |
1468 | exprtype, type, expr); |
1469 | } |
1470 | /* No warning for converting 0x80000000 to int. */ |
1471 | else if (pedantic |
1472 | && ((TREE_CODE (exprtype) != INTEGER_TYPE |
1473 | && TREE_CODE (exprtype) != BITINT_TYPE) |
1474 | || (TYPE_PRECISION (exprtype) |
1475 | != TYPE_PRECISION (type)))) |
1476 | { |
1477 | if (cst) |
1478 | warning_at (loc, OPT_Woverflow, |
1479 | "overflow in conversion from %qT to %qT " |
1480 | "changes value from %qE to %qE" , |
1481 | exprtype, type, expr, result_diag); |
1482 | else |
1483 | warning_at (loc, OPT_Woverflow, |
1484 | "overflow in conversion from %qT to %qT " |
1485 | "changes the value of %qE" , |
1486 | exprtype, type, expr); |
1487 | } |
1488 | else |
1489 | conversion_warning (loc, type, expr, result); |
1490 | } |
1491 | else if ((TREE_CODE (result) == INTEGER_CST |
1492 | || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) |
1493 | { |
1494 | if (cst) |
1495 | warning_at (loc, OPT_Woverflow, |
1496 | "overflow in conversion from %qT to %qT " |
1497 | "changes value from %qE to %qE" , |
1498 | exprtype, type, expr, result_diag); |
1499 | else |
1500 | warning_at (loc, OPT_Woverflow, |
1501 | "overflow in conversion from %qT to %qT " |
1502 | "changes the value of %qE" , |
1503 | exprtype, type, expr); |
1504 | } |
1505 | else |
1506 | conversion_warning (loc, type, expr, result); |
1507 | } |
1508 | |
1509 | /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. |
1510 | Used to verify that case values match up with enumerator values. */ |
1511 | |
1512 | static void |
1513 | match_case_to_enum_1 (tree key, tree type, tree label) |
1514 | { |
1515 | /* Avoid warning about enums that have no enumerators. */ |
1516 | if (TYPE_VALUES (type) == NULL_TREE) |
1517 | return; |
1518 | |
1519 | char buf[WIDE_INT_PRINT_BUFFER_SIZE]; |
1520 | wide_int w = wi::to_wide (t: key); |
1521 | |
1522 | gcc_assert (w.get_precision () <= WIDE_INT_MAX_INL_PRECISION); |
1523 | if (tree_fits_uhwi_p (key)) |
1524 | print_dec (wi: w, buf, sgn: UNSIGNED); |
1525 | else if (tree_fits_shwi_p (key)) |
1526 | print_dec (wi: w, buf, sgn: SIGNED); |
1527 | else |
1528 | print_hex (wi: w, buf); |
1529 | |
1530 | if (TYPE_NAME (type) == NULL_TREE) |
1531 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
1532 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, |
1533 | "case value %qs not in enumerated type" , |
1534 | buf); |
1535 | else |
1536 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
1537 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, |
1538 | "case value %qs not in enumerated type %qT" , |
1539 | buf, type); |
1540 | } |
1541 | |
1542 | /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. |
1543 | Used to verify that case values match up with enumerator values. */ |
1544 | |
1545 | static int |
1546 | match_case_to_enum (splay_tree_node node, void *data) |
1547 | { |
1548 | tree label = (tree) node->value; |
1549 | tree type = (tree) data; |
1550 | |
1551 | /* Skip default case. */ |
1552 | if (!CASE_LOW (label)) |
1553 | return 0; |
1554 | |
1555 | /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear |
1556 | when we did our enum->case scan. Reset our scratch bit after. */ |
1557 | if (!CASE_LOW_SEEN (label)) |
1558 | match_case_to_enum_1 (CASE_LOW (label), type, label); |
1559 | else |
1560 | CASE_LOW_SEEN (label) = 0; |
1561 | |
1562 | /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is |
1563 | not set, that means that CASE_HIGH did not appear when we did our |
1564 | enum->case scan. Reset our scratch bit after. */ |
1565 | if (CASE_HIGH (label)) |
1566 | { |
1567 | if (!CASE_HIGH_SEEN (label)) |
1568 | match_case_to_enum_1 (CASE_HIGH (label), type, label); |
1569 | else |
1570 | CASE_HIGH_SEEN (label) = 0; |
1571 | } |
1572 | |
1573 | return 0; |
1574 | } |
1575 | |
1576 | /* Handle -Wswitch*. Called from the front end after parsing the |
1577 | switch construct. */ |
1578 | /* ??? Should probably be somewhere generic, since other languages |
1579 | besides C and C++ would want this. At the moment, however, C/C++ |
1580 | are the only tree-ssa languages that support enumerations at all, |
1581 | so the point is moot. */ |
1582 | |
1583 | void |
1584 | c_do_switch_warnings (splay_tree cases, location_t switch_location, |
1585 | tree type, tree cond, bool bool_cond_p) |
1586 | { |
1587 | splay_tree_node default_node; |
1588 | splay_tree_node node; |
1589 | tree chain; |
1590 | bool outside_range_p = false; |
1591 | |
1592 | if (type != error_mark_node |
1593 | && type != TREE_TYPE (cond) |
1594 | && INTEGRAL_TYPE_P (type) |
1595 | && INTEGRAL_TYPE_P (TREE_TYPE (cond)) |
1596 | && (!tree_int_cst_equal (TYPE_MIN_VALUE (type), |
1597 | TYPE_MIN_VALUE (TREE_TYPE (cond))) |
1598 | || !tree_int_cst_equal (TYPE_MAX_VALUE (type), |
1599 | TYPE_MAX_VALUE (TREE_TYPE (cond))))) |
1600 | { |
1601 | tree min_value = TYPE_MIN_VALUE (type); |
1602 | tree max_value = TYPE_MAX_VALUE (type); |
1603 | |
1604 | node = splay_tree_predecessor (cases, (splay_tree_key) min_value); |
1605 | if (node && node->key) |
1606 | { |
1607 | outside_range_p = true; |
1608 | /* There is at least one case smaller than TYPE's minimum value. |
1609 | NODE itself could be still a range overlapping the valid values, |
1610 | but any predecessors thereof except the default case will be |
1611 | completely outside of range. */ |
1612 | if (CASE_HIGH ((tree) node->value) |
1613 | && tree_int_cst_compare (CASE_HIGH ((tree) node->value), |
1614 | t2: min_value) >= 0) |
1615 | { |
1616 | location_t loc = EXPR_LOCATION ((tree) node->value); |
1617 | warning_at (loc, OPT_Wswitch_outside_range, |
1618 | "lower value in case label range less than minimum" |
1619 | " value for type" ); |
1620 | CASE_LOW ((tree) node->value) = convert (TREE_TYPE (cond), |
1621 | min_value); |
1622 | node->key = (splay_tree_key) CASE_LOW ((tree) node->value); |
1623 | } |
1624 | /* All the following ones are completely outside of range. */ |
1625 | do |
1626 | { |
1627 | node = splay_tree_predecessor (cases, |
1628 | (splay_tree_key) min_value); |
1629 | if (node == NULL || !node->key) |
1630 | break; |
1631 | location_t loc = EXPR_LOCATION ((tree) node->value); |
1632 | warning_at (loc, OPT_Wswitch_outside_range, "case label value is" |
1633 | " less than minimum value for type" ); |
1634 | splay_tree_remove (cases, node->key); |
1635 | } |
1636 | while (1); |
1637 | } |
1638 | node = splay_tree_lookup (cases, (splay_tree_key) max_value); |
1639 | if (node == NULL) |
1640 | node = splay_tree_predecessor (cases, (splay_tree_key) max_value); |
1641 | /* Handle a single node that might partially overlap the range. */ |
1642 | if (node |
1643 | && node->key |
1644 | && CASE_HIGH ((tree) node->value) |
1645 | && tree_int_cst_compare (CASE_HIGH ((tree) node->value), |
1646 | t2: max_value) > 0) |
1647 | { |
1648 | location_t loc = EXPR_LOCATION ((tree) node->value); |
1649 | warning_at (loc, OPT_Wswitch_outside_range, "upper value in case" |
1650 | " label range exceeds maximum value for type" ); |
1651 | CASE_HIGH ((tree) node->value) |
1652 | = convert (TREE_TYPE (cond), max_value); |
1653 | outside_range_p = true; |
1654 | } |
1655 | /* And any nodes that are completely outside of the range. */ |
1656 | while ((node = splay_tree_successor (cases, |
1657 | (splay_tree_key) max_value)) |
1658 | != NULL) |
1659 | { |
1660 | location_t loc = EXPR_LOCATION ((tree) node->value); |
1661 | warning_at (loc, OPT_Wswitch_outside_range, |
1662 | "case label value exceeds maximum value for type" ); |
1663 | splay_tree_remove (cases, node->key); |
1664 | outside_range_p = true; |
1665 | } |
1666 | } |
1667 | |
1668 | if (!warn_switch && !warn_switch_enum && !warn_switch_default |
1669 | && !warn_switch_bool) |
1670 | return; |
1671 | |
1672 | default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); |
1673 | if (!default_node) |
1674 | warning_at (switch_location, OPT_Wswitch_default, |
1675 | "switch missing default case" ); |
1676 | |
1677 | /* There are certain cases where -Wswitch-bool warnings aren't |
1678 | desirable, such as |
1679 | switch (boolean) |
1680 | { |
1681 | case true: ... |
1682 | case false: ... |
1683 | } |
1684 | so be careful here. */ |
1685 | if (warn_switch_bool && bool_cond_p) |
1686 | { |
1687 | splay_tree_node min_node; |
1688 | /* If there's a default node, it's also the value with the minimal |
1689 | key. So look at the penultimate key (if any). */ |
1690 | if (default_node) |
1691 | min_node = splay_tree_successor (cases, (splay_tree_key) NULL); |
1692 | else |
1693 | min_node = splay_tree_min (cases); |
1694 | tree min = min_node ? (tree) min_node->key : NULL_TREE; |
1695 | |
1696 | splay_tree_node max_node = splay_tree_max (cases); |
1697 | /* This might be a case range, so look at the value with the |
1698 | maximal key and then check CASE_HIGH. */ |
1699 | tree max = max_node ? (tree) max_node->value : NULL_TREE; |
1700 | if (max) |
1701 | max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max); |
1702 | |
1703 | /* If there's a case value > 1 or < 0, that is outside bool |
1704 | range, warn. */ |
1705 | if (outside_range_p |
1706 | || (max && wi::gts_p (x: wi::to_wide (t: max), y: 1)) |
1707 | || (min && wi::lts_p (x: wi::to_wide (t: min), y: 0)) |
1708 | /* And handle the |
1709 | switch (boolean) |
1710 | { |
1711 | case true: ... |
1712 | case false: ... |
1713 | default: ... |
1714 | } |
1715 | case, where we want to warn. */ |
1716 | || (default_node |
1717 | && max && wi::to_wide (t: max) == 1 |
1718 | && min && wi::to_wide (t: min) == 0)) |
1719 | warning_at (switch_location, OPT_Wswitch_bool, |
1720 | "switch condition has boolean value" ); |
1721 | } |
1722 | |
1723 | /* From here on, we only care about enumerated types. */ |
1724 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) |
1725 | return; |
1726 | |
1727 | /* From here on, we only care about -Wswitch and -Wswitch-enum. */ |
1728 | if (!warn_switch_enum && !warn_switch) |
1729 | return; |
1730 | |
1731 | /* Check the cases. Warn about case values which are not members of |
1732 | the enumerated type. For -Wswitch-enum, or for -Wswitch when |
1733 | there is no default case, check that exactly all enumeration |
1734 | literals are covered by the cases. */ |
1735 | |
1736 | /* Clearing COND if it is not an integer constant simplifies |
1737 | the tests inside the loop below. */ |
1738 | if (TREE_CODE (cond) != INTEGER_CST) |
1739 | cond = NULL_TREE; |
1740 | |
1741 | /* The time complexity here is O(N*lg(N)) worst case, but for the |
1742 | common case of monotonically increasing enumerators, it is |
1743 | O(N), since the nature of the splay tree will keep the next |
1744 | element adjacent to the root at all times. */ |
1745 | |
1746 | for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) |
1747 | { |
1748 | tree value = TREE_VALUE (chain); |
1749 | tree attrs = DECL_ATTRIBUTES (value); |
1750 | value = DECL_INITIAL (value); |
1751 | node = splay_tree_lookup (cases, (splay_tree_key) value); |
1752 | if (node) |
1753 | { |
1754 | /* Mark the CASE_LOW part of the case entry as seen. */ |
1755 | tree label = (tree) node->value; |
1756 | CASE_LOW_SEEN (label) = 1; |
1757 | continue; |
1758 | } |
1759 | |
1760 | /* Even though there wasn't an exact match, there might be a |
1761 | case range which includes the enumerator's value. */ |
1762 | node = splay_tree_predecessor (cases, (splay_tree_key) value); |
1763 | if (node && CASE_HIGH ((tree) node->value)) |
1764 | { |
1765 | tree label = (tree) node->value; |
1766 | int cmp = tree_int_cst_compare (CASE_HIGH (label), t2: value); |
1767 | if (cmp >= 0) |
1768 | { |
1769 | /* If we match the upper bound exactly, mark the CASE_HIGH |
1770 | part of the case entry as seen. */ |
1771 | if (cmp == 0) |
1772 | CASE_HIGH_SEEN (label) = 1; |
1773 | continue; |
1774 | } |
1775 | } |
1776 | |
1777 | /* We've now determined that this enumerated literal isn't |
1778 | handled by the case labels of the switch statement. */ |
1779 | |
1780 | /* Don't warn if the enumerator was marked as unused. We can't use |
1781 | TREE_USED here: it could have been set on the enumerator if the |
1782 | enumerator was used earlier. */ |
1783 | if (lookup_attribute (attr_name: "unused" , list: attrs) |
1784 | || lookup_attribute (attr_name: "maybe_unused" , list: attrs)) |
1785 | continue; |
1786 | |
1787 | /* If the switch expression is a constant, we only really care |
1788 | about whether that constant is handled by the switch. */ |
1789 | if (cond && tree_int_cst_compare (t1: cond, t2: value)) |
1790 | continue; |
1791 | |
1792 | /* If the enumerator is defined in a system header and uses a reserved |
1793 | name, then we continue to avoid throwing a warning. */ |
1794 | location_t loc = DECL_SOURCE_LOCATION |
1795 | (TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type))); |
1796 | if (in_system_header_at (loc) |
1797 | && name_reserved_for_implementation_p |
1798 | (IDENTIFIER_POINTER (TREE_PURPOSE (chain)))) |
1799 | continue; |
1800 | |
1801 | /* If there is a default_node, the only relevant option is |
1802 | Wswitch-enum. Otherwise, if both are enabled then we prefer |
1803 | to warn using -Wswitch because -Wswitch is enabled by -Wall |
1804 | while -Wswitch-enum is explicit. */ |
1805 | warning_at (switch_location, |
1806 | (default_node || !warn_switch |
1807 | ? OPT_Wswitch_enum |
1808 | : OPT_Wswitch), |
1809 | "enumeration value %qE not handled in switch" , |
1810 | TREE_PURPOSE (chain)); |
1811 | } |
1812 | |
1813 | /* Warn if there are case expressions that don't correspond to |
1814 | enumerators. This can occur since C and C++ don't enforce |
1815 | type-checking of assignments to enumeration variables. |
1816 | |
1817 | The time complexity here is now always O(N) worst case, since |
1818 | we should have marked both the lower bound and upper bound of |
1819 | every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN |
1820 | above. This scan also resets those fields. */ |
1821 | |
1822 | splay_tree_foreach (cases, match_case_to_enum, type); |
1823 | } |
1824 | |
1825 | /* Warn for A ?: C expressions (with B omitted) where A is a boolean |
1826 | expression, because B will always be true. */ |
1827 | |
1828 | void |
1829 | warn_for_omitted_condop (location_t location, tree cond) |
1830 | { |
1831 | /* In C++ template declarations it can happen that the type is dependent |
1832 | and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */ |
1833 | if (truth_value_p (TREE_CODE (cond)) |
1834 | || (TREE_TYPE (cond) != NULL_TREE |
1835 | && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE)) |
1836 | warning_at (location, OPT_Wparentheses, |
1837 | "the omitted middle operand in %<?:%> will always be %<true%>, " |
1838 | "suggest explicit middle operand" ); |
1839 | } |
1840 | |
1841 | /* Give an error for storing into ARG, which is 'const'. USE indicates |
1842 | how ARG was being used. */ |
1843 | |
1844 | void |
1845 | readonly_error (location_t loc, tree arg, enum lvalue_use use) |
1846 | { |
1847 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement |
1848 | || use == lv_asm); |
1849 | STRIP_ANY_LOCATION_WRAPPER (arg); |
1850 | /* Using this macro rather than (for example) arrays of messages |
1851 | ensures that all the format strings are checked at compile |
1852 | time. */ |
1853 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ |
1854 | : (use == lv_increment ? (I) \ |
1855 | : (use == lv_decrement ? (D) : (AS)))) |
1856 | if (TREE_CODE (arg) == COMPONENT_REF) |
1857 | { |
1858 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) |
1859 | error_at (loc, READONLY_MSG (G_("assignment of member " |
1860 | "%qD in read-only object" ), |
1861 | G_("increment of member " |
1862 | "%qD in read-only object" ), |
1863 | G_("decrement of member " |
1864 | "%qD in read-only object" ), |
1865 | G_("member %qD in read-only object " |
1866 | "used as %<asm%> output" )), |
1867 | TREE_OPERAND (arg, 1)); |
1868 | else |
1869 | error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD" ), |
1870 | G_("increment of read-only member %qD" ), |
1871 | G_("decrement of read-only member %qD" ), |
1872 | G_("read-only member %qD used as %<asm%> output" )), |
1873 | TREE_OPERAND (arg, 1)); |
1874 | } |
1875 | else if (VAR_P (arg)) |
1876 | error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD" ), |
1877 | G_("increment of read-only variable %qD" ), |
1878 | G_("decrement of read-only variable %qD" ), |
1879 | G_("read-only variable %qD used as %<asm%> output" )), |
1880 | arg); |
1881 | else if (TREE_CODE (arg) == PARM_DECL) |
1882 | error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD" ), |
1883 | G_("increment of read-only parameter %qD" ), |
1884 | G_("decrement of read-only parameter %qD" ), |
1885 | G_("read-only parameter %qD use as %<asm%> output" )), |
1886 | arg); |
1887 | else if (TREE_CODE (arg) == RESULT_DECL) |
1888 | { |
1889 | gcc_assert (c_dialect_cxx ()); |
1890 | error_at (loc, READONLY_MSG (G_("assignment of " |
1891 | "read-only named return value %qD" ), |
1892 | G_("increment of " |
1893 | "read-only named return value %qD" ), |
1894 | G_("decrement of " |
1895 | "read-only named return value %qD" ), |
1896 | G_("read-only named return value %qD " |
1897 | "used as %<asm%>output" )), |
1898 | arg); |
1899 | } |
1900 | else if (TREE_CODE (arg) == FUNCTION_DECL) |
1901 | error_at (loc, READONLY_MSG (G_("assignment of function %qD" ), |
1902 | G_("increment of function %qD" ), |
1903 | G_("decrement of function %qD" ), |
1904 | G_("function %qD used as %<asm%> output" )), |
1905 | arg); |
1906 | else |
1907 | error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE" ), |
1908 | G_("increment of read-only location %qE" ), |
1909 | G_("decrement of read-only location %qE" ), |
1910 | G_("read-only location %qE used as %<asm%> output" )), |
1911 | arg); |
1912 | } |
1913 | |
1914 | /* Print an error message for an invalid lvalue. USE says |
1915 | how the lvalue is being used and so selects the error message. LOC |
1916 | is the location for the error. */ |
1917 | |
1918 | void |
1919 | lvalue_error (location_t loc, enum lvalue_use use) |
1920 | { |
1921 | switch (use) |
1922 | { |
1923 | case lv_assign: |
1924 | error_at (loc, "lvalue required as left operand of assignment" ); |
1925 | break; |
1926 | case lv_increment: |
1927 | error_at (loc, "lvalue required as increment operand" ); |
1928 | break; |
1929 | case lv_decrement: |
1930 | error_at (loc, "lvalue required as decrement operand" ); |
1931 | break; |
1932 | case lv_addressof: |
1933 | error_at (loc, "lvalue required as unary %<&%> operand" ); |
1934 | break; |
1935 | case lv_asm: |
1936 | error_at (loc, "lvalue required in %<asm%> statement" ); |
1937 | break; |
1938 | default: |
1939 | gcc_unreachable (); |
1940 | } |
1941 | } |
1942 | |
1943 | /* Print an error message for an invalid indirection of type TYPE. |
1944 | ERRSTRING is the name of the operator for the indirection. */ |
1945 | |
1946 | void |
1947 | invalid_indirection_error (location_t loc, tree type, ref_operator errstring) |
1948 | { |
1949 | switch (errstring) |
1950 | { |
1951 | case RO_NULL: |
1952 | gcc_assert (c_dialect_cxx ()); |
1953 | error_at (loc, "invalid type argument (have %qT)" , type); |
1954 | break; |
1955 | case RO_ARRAY_INDEXING: |
1956 | error_at (loc, |
1957 | "invalid type argument of array indexing (have %qT)" , |
1958 | type); |
1959 | break; |
1960 | case RO_UNARY_STAR: |
1961 | error_at (loc, |
1962 | "invalid type argument of unary %<*%> (have %qT)" , |
1963 | type); |
1964 | break; |
1965 | case RO_ARROW: |
1966 | error_at (loc, |
1967 | "invalid type argument of %<->%> (have %qT)" , |
1968 | type); |
1969 | break; |
1970 | case RO_ARROW_STAR: |
1971 | error_at (loc, |
1972 | "invalid type argument of %<->*%> (have %qT)" , |
1973 | type); |
1974 | break; |
1975 | case RO_IMPLICIT_CONVERSION: |
1976 | error_at (loc, |
1977 | "invalid type argument of implicit conversion (have %qT)" , |
1978 | type); |
1979 | break; |
1980 | default: |
1981 | gcc_unreachable (); |
1982 | } |
1983 | } |
1984 | |
1985 | /* Subscripting with type char is likely to lose on a machine where |
1986 | chars are signed. So warn on any machine, but optionally. Don't |
1987 | warn for unsigned char since that type is safe. Don't warn for |
1988 | signed char because anyone who uses that must have done so |
1989 | deliberately. Furthermore, we reduce the false positive load by |
1990 | warning only for non-constant value of type char. |
1991 | LOC is the location of the subscripting expression. */ |
1992 | |
1993 | void |
1994 | warn_array_subscript_with_type_char (location_t loc, tree index) |
1995 | { |
1996 | if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) |
1997 | { |
1998 | /* If INDEX has a location, use it; otherwise use LOC (the location |
1999 | of the subscripting expression as a whole). */ |
2000 | loc = EXPR_LOC_OR_LOC (index, loc); |
2001 | STRIP_ANY_LOCATION_WRAPPER (index); |
2002 | if (TREE_CODE (index) != INTEGER_CST) |
2003 | warning_at (loc, OPT_Wchar_subscripts, |
2004 | "array subscript has type %<char%>" ); |
2005 | } |
2006 | } |
2007 | |
2008 | /* Implement -Wparentheses for the unexpected C precedence rules, to |
2009 | cover cases like x + y << z which readers are likely to |
2010 | misinterpret. We have seen an expression in which CODE is a binary |
2011 | operator used to combine expressions ARG_LEFT and ARG_RIGHT, which |
2012 | before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and |
2013 | CODE_RIGHT may be ERROR_MARK, which means that that side of the |
2014 | expression was not formed using a binary or unary operator, or it |
2015 | was enclosed in parentheses. */ |
2016 | |
2017 | void |
2018 | warn_about_parentheses (location_t loc, enum tree_code code, |
2019 | enum tree_code code_left, tree arg_left, |
2020 | enum tree_code code_right, tree arg_right) |
2021 | { |
2022 | if (!warn_parentheses) |
2023 | return; |
2024 | |
2025 | /* This macro tests that the expression ARG with original tree code |
2026 | CODE appears to be a boolean expression. or the result of folding a |
2027 | boolean expression. */ |
2028 | #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ |
2029 | (truth_value_p (TREE_CODE (ARG)) \ |
2030 | || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ |
2031 | /* Folding may create 0 or 1 integers from other expressions. */ \ |
2032 | || ((CODE) != INTEGER_CST \ |
2033 | && (integer_onep (ARG) || integer_zerop (ARG)))) |
2034 | |
2035 | switch (code) |
2036 | { |
2037 | case LSHIFT_EXPR: |
2038 | if (code_left == PLUS_EXPR) |
2039 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2040 | "suggest parentheses around %<+%> inside %<<<%>" ); |
2041 | else if (code_right == PLUS_EXPR) |
2042 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2043 | "suggest parentheses around %<+%> inside %<<<%>" ); |
2044 | else if (code_left == MINUS_EXPR) |
2045 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2046 | "suggest parentheses around %<-%> inside %<<<%>" ); |
2047 | else if (code_right == MINUS_EXPR) |
2048 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2049 | "suggest parentheses around %<-%> inside %<<<%>" ); |
2050 | return; |
2051 | |
2052 | case RSHIFT_EXPR: |
2053 | if (code_left == PLUS_EXPR) |
2054 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2055 | "suggest parentheses around %<+%> inside %<>>%>" ); |
2056 | else if (code_right == PLUS_EXPR) |
2057 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2058 | "suggest parentheses around %<+%> inside %<>>%>" ); |
2059 | else if (code_left == MINUS_EXPR) |
2060 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2061 | "suggest parentheses around %<-%> inside %<>>%>" ); |
2062 | else if (code_right == MINUS_EXPR) |
2063 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2064 | "suggest parentheses around %<-%> inside %<>>%>" ); |
2065 | return; |
2066 | |
2067 | case TRUTH_ORIF_EXPR: |
2068 | if (code_left == TRUTH_ANDIF_EXPR) |
2069 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2070 | "suggest parentheses around %<&&%> within %<||%>" ); |
2071 | else if (code_right == TRUTH_ANDIF_EXPR) |
2072 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2073 | "suggest parentheses around %<&&%> within %<||%>" ); |
2074 | return; |
2075 | |
2076 | case BIT_IOR_EXPR: |
2077 | if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR |
2078 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
2079 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2080 | "suggest parentheses around arithmetic in operand of %<|%>" ); |
2081 | else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR |
2082 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) |
2083 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2084 | "suggest parentheses around arithmetic in operand of %<|%>" ); |
2085 | /* Check cases like x|y==z */ |
2086 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
2087 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2088 | "suggest parentheses around comparison in operand of %<|%>" ); |
2089 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) |
2090 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2091 | "suggest parentheses around comparison in operand of %<|%>" ); |
2092 | /* Check cases like !x | y */ |
2093 | else if (code_left == TRUTH_NOT_EXPR |
2094 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) |
2095 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2096 | "suggest parentheses around operand of " |
2097 | "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>" ); |
2098 | return; |
2099 | |
2100 | case BIT_XOR_EXPR: |
2101 | if (code_left == BIT_AND_EXPR |
2102 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
2103 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2104 | "suggest parentheses around arithmetic in operand of %<^%>" ); |
2105 | else if (code_right == BIT_AND_EXPR |
2106 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) |
2107 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2108 | "suggest parentheses around arithmetic in operand of %<^%>" ); |
2109 | /* Check cases like x^y==z */ |
2110 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
2111 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2112 | "suggest parentheses around comparison in operand of %<^%>" ); |
2113 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) |
2114 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2115 | "suggest parentheses around comparison in operand of %<^%>" ); |
2116 | return; |
2117 | |
2118 | case BIT_AND_EXPR: |
2119 | if (code_left == PLUS_EXPR) |
2120 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2121 | "suggest parentheses around %<+%> in operand of %<&%>" ); |
2122 | else if (code_right == PLUS_EXPR) |
2123 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2124 | "suggest parentheses around %<+%> in operand of %<&%>" ); |
2125 | else if (code_left == MINUS_EXPR) |
2126 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2127 | "suggest parentheses around %<-%> in operand of %<&%>" ); |
2128 | else if (code_right == MINUS_EXPR) |
2129 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2130 | "suggest parentheses around %<-%> in operand of %<&%>" ); |
2131 | /* Check cases like x&y==z */ |
2132 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
2133 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2134 | "suggest parentheses around comparison in operand of %<&%>" ); |
2135 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) |
2136 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2137 | "suggest parentheses around comparison in operand of %<&%>" ); |
2138 | /* Check cases like !x & y */ |
2139 | else if (code_left == TRUTH_NOT_EXPR |
2140 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) |
2141 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2142 | "suggest parentheses around operand of " |
2143 | "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>" ); |
2144 | return; |
2145 | |
2146 | case EQ_EXPR: |
2147 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
2148 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2149 | "suggest parentheses around comparison in operand of %<==%>" ); |
2150 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) |
2151 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2152 | "suggest parentheses around comparison in operand of %<==%>" ); |
2153 | return; |
2154 | case NE_EXPR: |
2155 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
2156 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2157 | "suggest parentheses around comparison in operand of %<!=%>" ); |
2158 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) |
2159 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2160 | "suggest parentheses around comparison in operand of %<!=%>" ); |
2161 | return; |
2162 | |
2163 | default: |
2164 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
2165 | { |
2166 | if (TREE_CODE_CLASS (code_left) == tcc_comparison |
2167 | && code_left != NE_EXPR && code_left != EQ_EXPR |
2168 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) |
2169 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
2170 | "comparisons like %<X<=Y<=Z%> do not " |
2171 | "have their mathematical meaning" ); |
2172 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison |
2173 | && code_right != NE_EXPR && code_right != EQ_EXPR |
2174 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) |
2175 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, |
2176 | "comparisons like %<X<=Y<=Z%> do not " |
2177 | "have their mathematical meaning" ); |
2178 | } |
2179 | return; |
2180 | } |
2181 | #undef NOT_A_BOOLEAN_EXPR_P |
2182 | } |
2183 | |
2184 | /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ |
2185 | |
2186 | void |
2187 | warn_for_unused_label (tree label) |
2188 | { |
2189 | if (!TREE_USED (label)) |
2190 | { |
2191 | if (DECL_INITIAL (label)) |
2192 | warning (OPT_Wunused_label, "label %q+D defined but not used" , label); |
2193 | else |
2194 | warning (OPT_Wunused_label, "label %q+D declared but not defined" , label); |
2195 | } |
2196 | else if (asan_sanitize_use_after_scope ()) |
2197 | { |
2198 | if (asan_used_labels == NULL) |
2199 | asan_used_labels = new hash_set<tree> (16); |
2200 | |
2201 | asan_used_labels->add (k: label); |
2202 | } |
2203 | } |
2204 | |
2205 | /* Warn for division by zero according to the value of DIVISOR. LOC |
2206 | is the location of the division operator. */ |
2207 | |
2208 | void |
2209 | warn_for_div_by_zero (location_t loc, tree divisor) |
2210 | { |
2211 | /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning |
2212 | about division by zero. Do not issue a warning if DIVISOR has a |
2213 | floating-point type, since we consider 0.0/0.0 a valid way of |
2214 | generating a NaN. */ |
2215 | if (c_inhibit_evaluation_warnings == 0 |
2216 | && (integer_zerop (divisor) || fixed_zerop (divisor))) |
2217 | warning_at (loc, OPT_Wdiv_by_zero, "division by zero" ); |
2218 | } |
2219 | |
2220 | /* Warn for patterns where memset appears to be used incorrectly. The |
2221 | warning location should be LOC. ARG0, and ARG2 are the first and |
2222 | last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for |
2223 | each argument that was a literal zero. */ |
2224 | |
2225 | void |
2226 | warn_for_memset (location_t loc, tree arg0, tree arg2, |
2227 | int literal_zero_mask) |
2228 | { |
2229 | arg0 = fold_for_warn (arg0); |
2230 | arg2 = fold_for_warn (arg2); |
2231 | |
2232 | if (warn_memset_transposed_args |
2233 | && integer_zerop (arg2) |
2234 | && (literal_zero_mask & (1 << 2)) != 0 |
2235 | && (literal_zero_mask & (1 << 1)) == 0) |
2236 | warning_at (loc, OPT_Wmemset_transposed_args, |
2237 | "%<memset%> used with constant zero length " |
2238 | "parameter; this could be due to transposed " |
2239 | "parameters" ); |
2240 | |
2241 | if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST) |
2242 | { |
2243 | STRIP_NOPS (arg0); |
2244 | if (TREE_CODE (arg0) == ADDR_EXPR) |
2245 | arg0 = TREE_OPERAND (arg0, 0); |
2246 | tree type = TREE_TYPE (arg0); |
2247 | if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE) |
2248 | { |
2249 | tree elt_type = TREE_TYPE (type); |
2250 | tree domain = TYPE_DOMAIN (type); |
2251 | if (COMPLETE_TYPE_P (elt_type) |
2252 | && !integer_onep (TYPE_SIZE_UNIT (elt_type)) |
2253 | && domain != NULL_TREE |
2254 | && TYPE_MAX_VALUE (domain) |
2255 | && TYPE_MIN_VALUE (domain) |
2256 | && integer_zerop (TYPE_MIN_VALUE (domain)) |
2257 | && integer_onep (fold_build2 (MINUS_EXPR, domain, |
2258 | arg2, |
2259 | TYPE_MAX_VALUE (domain)))) |
2260 | warning_at (loc, OPT_Wmemset_elt_size, |
2261 | "%<memset%> used with length equal to " |
2262 | "number of elements without multiplication " |
2263 | "by element size" ); |
2264 | } |
2265 | } |
2266 | } |
2267 | |
2268 | /* Subroutine of build_binary_op. Give warnings for comparisons |
2269 | between signed and unsigned quantities that may fail. Do the |
2270 | checking based on the original operand trees ORIG_OP0 and ORIG_OP1, |
2271 | so that casts will be considered, but default promotions won't |
2272 | be. |
2273 | |
2274 | LOCATION is the location of the comparison operator. |
2275 | |
2276 | The arguments of this function map directly to local variables |
2277 | of build_binary_op. */ |
2278 | |
2279 | void |
2280 | warn_for_sign_compare (location_t location, |
2281 | tree orig_op0, tree orig_op1, |
2282 | tree op0, tree op1, |
2283 | tree result_type, enum tree_code resultcode) |
2284 | { |
2285 | if (error_operand_p (t: orig_op0) || error_operand_p (t: orig_op1)) |
2286 | return; |
2287 | |
2288 | int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); |
2289 | int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
2290 | int unsignedp0, unsignedp1; |
2291 | |
2292 | /* Do not warn if the comparison is being done in a signed type, |
2293 | since the signed type will only be chosen if it can represent |
2294 | all the values of the unsigned type. */ |
2295 | if (!TYPE_UNSIGNED (result_type)) |
2296 | /* OK */; |
2297 | /* Do not warn if both operands are unsigned. */ |
2298 | else if (op0_signed == op1_signed) |
2299 | /* OK */; |
2300 | else |
2301 | { |
2302 | tree sop, uop, base_type; |
2303 | bool ovf; |
2304 | |
2305 | if (op0_signed) |
2306 | sop = orig_op0, uop = orig_op1; |
2307 | else |
2308 | sop = orig_op1, uop = orig_op0; |
2309 | |
2310 | sop = fold_for_warn (sop); |
2311 | uop = fold_for_warn (uop); |
2312 | |
2313 | STRIP_TYPE_NOPS (sop); |
2314 | STRIP_TYPE_NOPS (uop); |
2315 | base_type = (TREE_CODE (result_type) == COMPLEX_TYPE |
2316 | ? TREE_TYPE (result_type) : result_type); |
2317 | |
2318 | /* Do not warn if the signed quantity is an unsuffixed integer |
2319 | literal (or some static constant expression involving such |
2320 | literals or a conditional expression involving such literals) |
2321 | and it is non-negative. */ |
2322 | if (tree_expr_nonnegative_warnv_p (sop, &ovf)) |
2323 | /* OK */; |
2324 | /* Do not warn if the comparison is an equality operation, the |
2325 | unsigned quantity is an integral constant, and it would fit |
2326 | in the result if the result were signed. */ |
2327 | else if (TREE_CODE (uop) == INTEGER_CST |
2328 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) |
2329 | && int_fits_type_p (uop, c_common_signed_type (base_type))) |
2330 | /* OK */; |
2331 | /* In C, do not warn if the unsigned quantity is an enumeration |
2332 | constant and its maximum value would fit in the result if the |
2333 | result were signed. */ |
2334 | else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST |
2335 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE |
2336 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), |
2337 | c_common_signed_type (base_type))) |
2338 | /* OK */; |
2339 | else |
2340 | warning_at (location, OPT_Wsign_compare, |
2341 | "comparison of integer expressions of different " |
2342 | "signedness: %qT and %qT" , TREE_TYPE (orig_op0), |
2343 | TREE_TYPE (orig_op1)); |
2344 | } |
2345 | |
2346 | /* Warn if two unsigned values are being compared in a size larger |
2347 | than their original size, and one (and only one) is the result of |
2348 | a `~' operator. This comparison will always fail. |
2349 | |
2350 | Also warn if one operand is a constant, and the constant does not |
2351 | have all bits set that are set in the ~ operand when it is |
2352 | extended. */ |
2353 | |
2354 | /* bits0 is the bit index of op0 extended to result_type, which will |
2355 | be always 0 and so all bits above it. If there is a BIT_NOT_EXPR |
2356 | in that operand possibly sign or zero extended to op0 and then |
2357 | possibly further sign or zero extended to result_type, bits0 will |
2358 | be the precision of result type if all the extensions involved |
2359 | if any are sign extensions, and will be the place of the innermost |
2360 | zero extension otherwise. We warn only if BIT_NOT_EXPR's operand is |
2361 | zero extended from some even smaller precision, in that case after |
2362 | BIT_NOT_EXPR some bits below bits0 will be guaranteed to be set. |
2363 | Similarly for bits1. */ |
2364 | int bits0 = TYPE_PRECISION (result_type); |
2365 | if (TYPE_UNSIGNED (TREE_TYPE (op0))) |
2366 | bits0 = TYPE_PRECISION (TREE_TYPE (op0)); |
2367 | tree arg0 = c_common_get_narrower (op0, &unsignedp0); |
2368 | if (TYPE_PRECISION (TREE_TYPE (arg0)) == TYPE_PRECISION (TREE_TYPE (op0))) |
2369 | unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
2370 | else if (unsignedp0) |
2371 | bits0 = TYPE_PRECISION (TREE_TYPE (arg0)); |
2372 | op0 = arg0; |
2373 | int bits1 = TYPE_PRECISION (result_type); |
2374 | if (TYPE_UNSIGNED (TREE_TYPE (op1))) |
2375 | bits1 = TYPE_PRECISION (TREE_TYPE (op1)); |
2376 | tree arg1 = c_common_get_narrower (op1, &unsignedp1); |
2377 | if (TYPE_PRECISION (TREE_TYPE (arg1)) == TYPE_PRECISION (TREE_TYPE (op1))) |
2378 | unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); |
2379 | else if (unsignedp1) |
2380 | bits1 = TYPE_PRECISION (TREE_TYPE (arg1)); |
2381 | op1 = arg1; |
2382 | |
2383 | if ((TREE_CODE (op0) == BIT_NOT_EXPR) |
2384 | ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) |
2385 | { |
2386 | if (TREE_CODE (op1) == BIT_NOT_EXPR) |
2387 | { |
2388 | std::swap (a&: op0, b&: op1); |
2389 | std::swap (a&: unsignedp0, b&: unsignedp1); |
2390 | std::swap (a&: bits0, b&: bits1); |
2391 | } |
2392 | |
2393 | int unsignedp; |
2394 | arg0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp); |
2395 | |
2396 | /* For these warnings, we need BIT_NOT_EXPR operand to be |
2397 | zero extended from narrower type to BIT_NOT_EXPR's type. |
2398 | In that case, all those bits above the narrower's type |
2399 | are after BIT_NOT_EXPR set to 1. */ |
2400 | if (tree_fits_shwi_p (op1)) |
2401 | { |
2402 | HOST_WIDE_INT constant = tree_to_shwi (op1); |
2403 | unsigned int bits = TYPE_PRECISION (TREE_TYPE (arg0)); |
2404 | if (unsignedp |
2405 | && bits < TYPE_PRECISION (TREE_TYPE (op0)) |
2406 | && bits < HOST_BITS_PER_WIDE_INT) |
2407 | { |
2408 | HOST_WIDE_INT mask = HOST_WIDE_INT_M1U << bits; |
2409 | if (bits0 < HOST_BITS_PER_WIDE_INT) |
2410 | mask &= ~(HOST_WIDE_INT_M1U << bits0); |
2411 | if ((mask & constant) != mask) |
2412 | { |
2413 | if (constant == 0) |
2414 | warning_at (location, OPT_Wsign_compare, |
2415 | "promoted bitwise complement of an unsigned " |
2416 | "value is always nonzero" ); |
2417 | else |
2418 | warning_at (location, OPT_Wsign_compare, |
2419 | "comparison of promoted bitwise complement " |
2420 | "of an unsigned value with constant" ); |
2421 | } |
2422 | } |
2423 | } |
2424 | else if ((TYPE_PRECISION (TREE_TYPE (arg0)) |
2425 | < TYPE_PRECISION (TREE_TYPE (op0))) |
2426 | && unsignedp |
2427 | && unsignedp1 |
2428 | && TYPE_PRECISION (TREE_TYPE (op1)) < bits0) |
2429 | warning_at (location, OPT_Wsign_compare, |
2430 | "comparison of promoted bitwise complement " |
2431 | "of an unsigned value with unsigned" ); |
2432 | } |
2433 | } |
2434 | |
2435 | /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common |
2436 | type via c_common_type. If -Wdouble-promotion is in use, and the |
2437 | conditions for warning have been met, issue a warning. GMSGID is |
2438 | the warning message. It must have two %T specifiers for the type |
2439 | that was converted (generally "float") and the type to which it was |
2440 | converted (generally "double), respectively. LOC is the location |
2441 | to which the warning should refer. */ |
2442 | |
2443 | void |
2444 | do_warn_double_promotion (tree result_type, tree type1, tree type2, |
2445 | const char *gmsgid, location_t loc) |
2446 | { |
2447 | tree source_type; |
2448 | |
2449 | if (!warn_double_promotion) |
2450 | return; |
2451 | /* If the conversion will not occur at run-time, there is no need to |
2452 | warn about it. */ |
2453 | if (c_inhibit_evaluation_warnings) |
2454 | return; |
2455 | /* If an invalid conversion has occurred, don't warn. */ |
2456 | if (result_type == error_mark_node) |
2457 | return; |
2458 | if (TYPE_MAIN_VARIANT (result_type) != double_type_node |
2459 | && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) |
2460 | return; |
2461 | if (TYPE_MAIN_VARIANT (type1) == float_type_node |
2462 | || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) |
2463 | source_type = type1; |
2464 | else if (TYPE_MAIN_VARIANT (type2) == float_type_node |
2465 | || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) |
2466 | source_type = type2; |
2467 | else |
2468 | return; |
2469 | warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); |
2470 | } |
2471 | |
2472 | /* Possibly warn about unused parameters. */ |
2473 | |
2474 | void |
2475 | do_warn_unused_parameter (tree fn) |
2476 | { |
2477 | tree decl; |
2478 | |
2479 | for (decl = DECL_ARGUMENTS (fn); |
2480 | decl; decl = DECL_CHAIN (decl)) |
2481 | if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL |
2482 | && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl) |
2483 | && !warning_suppressed_p (decl, OPT_Wunused_parameter)) |
2484 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter, |
2485 | "unused parameter %qD" , decl); |
2486 | } |
2487 | |
2488 | /* If DECL is a typedef that is declared in the current function, |
2489 | record it for the purpose of -Wunused-local-typedefs. */ |
2490 | |
2491 | void |
2492 | record_locally_defined_typedef (tree decl) |
2493 | { |
2494 | struct c_language_function *l; |
2495 | |
2496 | if (!warn_unused_local_typedefs |
2497 | || cfun == NULL |
2498 | /* if this is not a locally defined typedef then we are not |
2499 | interested. */ |
2500 | || !is_typedef_decl (x: decl) |
2501 | || !decl_function_context (decl)) |
2502 | return; |
2503 | |
2504 | l = (struct c_language_function *) cfun->language; |
2505 | vec_safe_push (v&: l->local_typedefs, obj: decl); |
2506 | } |
2507 | |
2508 | /* If T is a TYPE_DECL declared locally, mark it as used. */ |
2509 | |
2510 | void |
2511 | maybe_record_typedef_use (tree t) |
2512 | { |
2513 | if (!is_typedef_decl (x: t)) |
2514 | return; |
2515 | |
2516 | TREE_USED (t) = true; |
2517 | } |
2518 | |
2519 | /* Warn if there are some unused locally defined typedefs in the |
2520 | current function. */ |
2521 | |
2522 | void |
2523 | maybe_warn_unused_local_typedefs (void) |
2524 | { |
2525 | int i; |
2526 | tree decl; |
2527 | /* The number of times we have emitted -Wunused-local-typedefs |
2528 | warnings. If this is different from errorcount, that means some |
2529 | unrelated errors have been issued. In which case, we'll avoid |
2530 | emitting "unused-local-typedefs" warnings. */ |
2531 | static int unused_local_typedefs_warn_count; |
2532 | struct c_language_function *l; |
2533 | |
2534 | if (cfun == NULL) |
2535 | return; |
2536 | |
2537 | if ((l = (struct c_language_function *) cfun->language) == NULL) |
2538 | return; |
2539 | |
2540 | if (warn_unused_local_typedefs |
2541 | && errorcount == unused_local_typedefs_warn_count) |
2542 | { |
2543 | FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl) |
2544 | if (!TREE_USED (decl)) |
2545 | warning_at (DECL_SOURCE_LOCATION (decl), |
2546 | OPT_Wunused_local_typedefs, |
2547 | "typedef %qD locally defined but not used" , decl); |
2548 | unused_local_typedefs_warn_count = errorcount; |
2549 | } |
2550 | |
2551 | vec_free (v&: l->local_typedefs); |
2552 | } |
2553 | |
2554 | /* If we're creating an if-else-if condition chain, first see if we |
2555 | already have this COND in the CHAIN. If so, warn and don't add COND |
2556 | into the vector, otherwise add the COND there. LOC is the location |
2557 | of COND. */ |
2558 | |
2559 | void |
2560 | warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain) |
2561 | { |
2562 | /* No chain has been created yet. Do nothing. */ |
2563 | if (*chain == NULL) |
2564 | return; |
2565 | |
2566 | if (TREE_SIDE_EFFECTS (cond) || instantiation_dependent_expression_p (cond)) |
2567 | { |
2568 | /* Uh-oh! This condition has a side-effect, thus invalidates |
2569 | the whole chain. */ |
2570 | delete *chain; |
2571 | *chain = NULL; |
2572 | return; |
2573 | } |
2574 | |
2575 | unsigned int ix; |
2576 | tree t; |
2577 | bool found = false; |
2578 | FOR_EACH_VEC_ELT (**chain, ix, t) |
2579 | if (operand_equal_p (cond, t, flags: 0)) |
2580 | { |
2581 | auto_diagnostic_group d; |
2582 | if (warning_at (loc, OPT_Wduplicated_cond, |
2583 | "duplicated %<if%> condition" )) |
2584 | inform (EXPR_LOCATION (t), "previously used here" ); |
2585 | found = true; |
2586 | break; |
2587 | } |
2588 | |
2589 | if (!found |
2590 | && !CONSTANT_CLASS_P (cond) |
2591 | /* Don't infinitely grow the chain. */ |
2592 | && (*chain)->length () < 512) |
2593 | (*chain)->safe_push (obj: cond); |
2594 | } |
2595 | |
2596 | /* Check and possibly warn if two declarations have contradictory |
2597 | attributes, such as always_inline vs. noinline. */ |
2598 | |
2599 | bool |
2600 | diagnose_mismatched_attributes (tree olddecl, tree newdecl) |
2601 | { |
2602 | bool warned = false; |
2603 | |
2604 | tree a1 = lookup_attribute (attr_name: "optimize" , DECL_ATTRIBUTES (olddecl)); |
2605 | tree a2 = lookup_attribute (attr_name: "optimize" , DECL_ATTRIBUTES (newdecl)); |
2606 | /* An optimization attribute applied on a declaration after the |
2607 | definition is likely not what the user wanted. */ |
2608 | if (a2 != NULL_TREE |
2609 | && DECL_SAVED_TREE (olddecl) != NULL_TREE |
2610 | && (a1 == NULL_TREE || !attribute_list_equal (a1, a2))) |
2611 | warned |= warning (OPT_Wattributes, |
2612 | "optimization attribute on %qD follows " |
2613 | "definition but the attribute doesn%'t match" , |
2614 | newdecl); |
2615 | |
2616 | /* Diagnose inline __attribute__ ((noinline)) which is silly. */ |
2617 | if (DECL_DECLARED_INLINE_P (newdecl) |
2618 | && DECL_UNINLINABLE (olddecl) |
2619 | && lookup_attribute (attr_name: "noinline" , DECL_ATTRIBUTES (olddecl))) |
2620 | warned |= warning (OPT_Wattributes, "inline declaration of %qD follows " |
2621 | "declaration with attribute %<noinline%>" , newdecl); |
2622 | else if (DECL_DECLARED_INLINE_P (olddecl) |
2623 | && DECL_UNINLINABLE (newdecl) |
2624 | && lookup_attribute (attr_name: "noinline" , DECL_ATTRIBUTES (newdecl))) |
2625 | warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " |
2626 | "%<noinline%> follows inline declaration" , newdecl); |
2627 | |
2628 | return warned; |
2629 | } |
2630 | |
2631 | /* Warn if signed left shift overflows. We don't warn |
2632 | about left-shifting 1 into the sign bit in C++14; cf. |
2633 | <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457> |
2634 | and don't warn for C++20 at all, as signed left shifts never |
2635 | overflow. |
2636 | LOC is a location of the shift; OP0 and OP1 are the operands. |
2637 | Return true if an overflow is detected, false otherwise. */ |
2638 | |
2639 | bool |
2640 | maybe_warn_shift_overflow (location_t loc, tree op0, tree op1) |
2641 | { |
2642 | if (TREE_CODE (op0) != INTEGER_CST |
2643 | || TREE_CODE (op1) != INTEGER_CST) |
2644 | return false; |
2645 | |
2646 | /* match.pd could have narrowed the left shift already, |
2647 | take type promotion into account. */ |
2648 | tree type0 = lang_hooks.types.type_promotes_to (TREE_TYPE (op0)); |
2649 | unsigned int prec0 = TYPE_PRECISION (type0); |
2650 | |
2651 | /* Left-hand operand must be signed. */ |
2652 | if (TYPE_OVERFLOW_WRAPS (type0) || cxx_dialect >= cxx20) |
2653 | return false; |
2654 | |
2655 | signop sign = SIGNED; |
2656 | if (TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (type0)) |
2657 | sign = TYPE_SIGN (TREE_TYPE (op0)); |
2658 | unsigned int min_prec = (wi::min_precision (x: wi::to_wide (t: op0), sgn: sign) |
2659 | + TREE_INT_CST_LOW (op1)); |
2660 | /* Handle the case of left-shifting 1 into the sign bit. |
2661 | * However, shifting 1 _out_ of the sign bit, as in |
2662 | * INT_MIN << 1, is considered an overflow. |
2663 | */ |
2664 | if (!tree_int_cst_sign_bit (op0) && min_prec == prec0 + 1) |
2665 | { |
2666 | /* Never warn for C++14 onwards. */ |
2667 | if (cxx_dialect >= cxx14) |
2668 | return false; |
2669 | /* Otherwise only if -Wshift-overflow=2. But return |
2670 | true to signal an overflow for the sake of integer |
2671 | constant expressions. */ |
2672 | if (warn_shift_overflow < 2) |
2673 | return true; |
2674 | } |
2675 | |
2676 | bool overflowed = min_prec > prec0; |
2677 | if (overflowed && c_inhibit_evaluation_warnings == 0) |
2678 | warning_at (loc, OPT_Wshift_overflow_, |
2679 | "result of %qE requires %u bits to represent, " |
2680 | "but %qT only has %u bits" , |
2681 | build2_loc (loc, code: LSHIFT_EXPR, type: type0, |
2682 | fold_convert (type0, op0), arg1: op1), |
2683 | min_prec, type0, prec0); |
2684 | |
2685 | return overflowed; |
2686 | } |
2687 | |
2688 | /* Warn about boolean expression compared with an integer value different |
2689 | from true/false. Warns also e.g. about "(i1 == i2) == 2". |
2690 | LOC is the location of the comparison, CODE is its code, OP0 and OP1 |
2691 | are the operands of the comparison. The caller must ensure that |
2692 | either operand is a boolean expression. */ |
2693 | |
2694 | void |
2695 | maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0, |
2696 | tree op1) |
2697 | { |
2698 | if (TREE_CODE_CLASS (code) != tcc_comparison) |
2699 | return; |
2700 | |
2701 | tree f, cst; |
2702 | if (f = fold_for_warn (op0), |
2703 | TREE_CODE (f) == INTEGER_CST) |
2704 | cst = op0 = f; |
2705 | else if (f = fold_for_warn (op1), |
2706 | TREE_CODE (f) == INTEGER_CST) |
2707 | cst = op1 = f; |
2708 | else |
2709 | return; |
2710 | |
2711 | if (!integer_zerop (cst) && !integer_onep (cst)) |
2712 | { |
2713 | int sign = (TREE_CODE (op0) == INTEGER_CST |
2714 | ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst)); |
2715 | if (code == EQ_EXPR |
2716 | || ((code == GT_EXPR || code == GE_EXPR) && sign < 0) |
2717 | || ((code == LT_EXPR || code == LE_EXPR) && sign > 0)) |
2718 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " |
2719 | "with boolean expression is always false" , cst); |
2720 | else |
2721 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " |
2722 | "with boolean expression is always true" , cst); |
2723 | } |
2724 | else if (integer_zerop (cst) || integer_onep (cst)) |
2725 | { |
2726 | /* If the non-constant operand isn't of a boolean type, we |
2727 | don't want to warn here. */ |
2728 | tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0; |
2729 | /* Handle booleans promoted to integers. */ |
2730 | if (bool_promoted_to_int_p (noncst)) |
2731 | /* Warn. */; |
2732 | else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE |
2733 | && !truth_value_p (TREE_CODE (noncst))) |
2734 | return; |
2735 | /* Do some magic to get the right diagnostics. */ |
2736 | bool flag = TREE_CODE (op0) == INTEGER_CST; |
2737 | flag = integer_zerop (cst) ? flag : !flag; |
2738 | if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag)) |
2739 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " |
2740 | "with boolean expression is always true" , cst); |
2741 | else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag)) |
2742 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " |
2743 | "with boolean expression is always false" , cst); |
2744 | } |
2745 | } |
2746 | |
2747 | /* Warn if an argument at position param_pos is passed to a |
2748 | restrict-qualified param, and it aliases with another argument. |
2749 | Return true if a warning has been issued. */ |
2750 | |
2751 | bool |
2752 | warn_for_restrict (unsigned param_pos, tree *argarray, unsigned nargs) |
2753 | { |
2754 | tree arg = argarray[param_pos]; |
2755 | if (TREE_VISITED (arg) || integer_zerop (arg)) |
2756 | return false; |
2757 | |
2758 | location_t loc = EXPR_LOC_OR_LOC (arg, input_location); |
2759 | gcc_rich_location richloc (loc); |
2760 | |
2761 | unsigned i; |
2762 | auto_vec<int, 16> arg_positions; |
2763 | |
2764 | for (i = 0; i < nargs; i++) |
2765 | { |
2766 | if (i == param_pos) |
2767 | continue; |
2768 | |
2769 | tree current_arg = argarray[i]; |
2770 | if (operand_equal_p (arg, current_arg, flags: 0)) |
2771 | { |
2772 | TREE_VISITED (current_arg) = 1; |
2773 | arg_positions.safe_push (obj: i + 1); |
2774 | } |
2775 | } |
2776 | |
2777 | if (arg_positions.is_empty ()) |
2778 | return false; |
2779 | |
2780 | int pos; |
2781 | FOR_EACH_VEC_ELT (arg_positions, i, pos) |
2782 | { |
2783 | arg = argarray[pos - 1]; |
2784 | if (EXPR_HAS_LOCATION (arg)) |
2785 | richloc.add_range (EXPR_LOCATION (arg)); |
2786 | } |
2787 | |
2788 | return warning_n (&richloc, OPT_Wrestrict, arg_positions.length (), |
2789 | "passing argument %i to %qs-qualified parameter" |
2790 | " aliases with argument %Z" , |
2791 | "passing argument %i to %qs-qualified parameter" |
2792 | " aliases with arguments %Z" , |
2793 | param_pos + 1, "restrict" , arg_positions.address (), |
2794 | arg_positions.length ()); |
2795 | } |
2796 | |
2797 | /* Callback function to determine whether an expression TP or one of its |
2798 | subexpressions comes from macro expansion. Used to suppress bogus |
2799 | warnings. */ |
2800 | |
2801 | static tree |
2802 | expr_from_macro_expansion_r (tree *tp, int *, void *) |
2803 | { |
2804 | if (CAN_HAVE_LOCATION_P (*tp) |
2805 | && from_macro_expansion_at (EXPR_LOCATION (*tp))) |
2806 | return integer_zero_node; |
2807 | |
2808 | return NULL_TREE; |
2809 | } |
2810 | |
2811 | /* Possibly warn when an if-else has identical branches. */ |
2812 | |
2813 | static void |
2814 | do_warn_duplicated_branches (tree expr) |
2815 | { |
2816 | tree thenb = COND_EXPR_THEN (expr); |
2817 | tree elseb = COND_EXPR_ELSE (expr); |
2818 | |
2819 | /* Don't bother if any of the branches is missing. */ |
2820 | if (thenb == NULL_TREE || elseb == NULL_TREE) |
2821 | return; |
2822 | |
2823 | /* And don't warn for empty statements. */ |
2824 | if (TREE_CODE (thenb) == NOP_EXPR |
2825 | && TREE_TYPE (thenb) == void_type_node |
2826 | && TREE_OPERAND (thenb, 0) == size_zero_node) |
2827 | return; |
2828 | |
2829 | /* ... or empty branches. */ |
2830 | if (TREE_CODE (thenb) == STATEMENT_LIST |
2831 | && STATEMENT_LIST_HEAD (thenb) == NULL) |
2832 | return; |
2833 | |
2834 | /* Compute the hash of the then branch. */ |
2835 | inchash::hash hstate0 (0); |
2836 | inchash::add_expr (thenb, hstate0); |
2837 | hashval_t h0 = hstate0.end (); |
2838 | |
2839 | /* Compute the hash of the else branch. */ |
2840 | inchash::hash hstate1 (0); |
2841 | inchash::add_expr (elseb, hstate1); |
2842 | hashval_t h1 = hstate1.end (); |
2843 | |
2844 | /* Compare the hashes. */ |
2845 | if (h0 == h1 |
2846 | && operand_equal_p (thenb, elseb, flags: OEP_LEXICOGRAPHIC |
2847 | | OEP_ADDRESS_OF_SAME_FIELD) |
2848 | /* Don't warn if any of the branches or their subexpressions comes |
2849 | from a macro. */ |
2850 | && !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r, |
2851 | NULL) |
2852 | && !walk_tree_without_duplicates (&elseb, expr_from_macro_expansion_r, |
2853 | NULL)) |
2854 | warning_at (EXPR_LOCATION (expr), OPT_Wduplicated_branches, |
2855 | "this condition has identical branches" ); |
2856 | } |
2857 | |
2858 | /* Callback for c_genericize to implement -Wduplicated-branches. */ |
2859 | |
2860 | tree |
2861 | do_warn_duplicated_branches_r (tree *tp, int *, void *) |
2862 | { |
2863 | if (TREE_CODE (*tp) == COND_EXPR) |
2864 | do_warn_duplicated_branches (expr: *tp); |
2865 | return NULL_TREE; |
2866 | } |
2867 | |
2868 | /* Implementation of -Wmultistatement-macros. This warning warns about |
2869 | cases when a macro expands to multiple statements not wrapped in |
2870 | do {} while (0) or ({ }) and is used as a body of if/else/for/while |
2871 | conditionals. For example, |
2872 | |
2873 | #define DOIT x++; y++ |
2874 | |
2875 | if (c) |
2876 | DOIT; |
2877 | |
2878 | will increment y unconditionally. |
2879 | |
2880 | BODY_LOC is the location of the first token in the body after labels |
2881 | have been parsed, NEXT_LOC is the location of the next token after the |
2882 | body of the conditional has been parsed, and GUARD_LOC is the location |
2883 | of the conditional. */ |
2884 | |
2885 | void |
2886 | warn_for_multistatement_macros (location_t body_loc, location_t next_loc, |
2887 | location_t guard_loc, enum rid keyword) |
2888 | { |
2889 | if (!warn_multistatement_macros) |
2890 | return; |
2891 | |
2892 | /* Ain't got time to waste. We only care about macros here. */ |
2893 | if (!from_macro_expansion_at (loc: body_loc) |
2894 | || !from_macro_expansion_at (loc: next_loc)) |
2895 | return; |
2896 | |
2897 | /* Let's skip macros defined in system headers. */ |
2898 | if (in_system_header_at (loc: body_loc) |
2899 | || in_system_header_at (loc: next_loc)) |
2900 | return; |
2901 | |
2902 | /* Find the actual tokens in the macro definition. BODY_LOC and |
2903 | NEXT_LOC have to come from the same spelling location, but they |
2904 | will resolve to different locations in the context of the macro |
2905 | definition. */ |
2906 | location_t body_loc_exp |
2907 | = linemap_resolve_location (line_table, loc: body_loc, |
2908 | lrk: LRK_MACRO_DEFINITION_LOCATION, NULL); |
2909 | location_t next_loc_exp |
2910 | = linemap_resolve_location (line_table, loc: next_loc, |
2911 | lrk: LRK_MACRO_DEFINITION_LOCATION, NULL); |
2912 | location_t guard_loc_exp |
2913 | = linemap_resolve_location (line_table, loc: guard_loc, |
2914 | lrk: LRK_MACRO_DEFINITION_LOCATION, NULL); |
2915 | |
2916 | /* These are some funky cases we don't want to warn about. */ |
2917 | if (body_loc_exp == guard_loc_exp |
2918 | || next_loc_exp == guard_loc_exp |
2919 | || body_loc_exp == next_loc_exp) |
2920 | return; |
2921 | |
2922 | /* Find the macro maps for the macro expansions. */ |
2923 | const line_map *body_map = linemap_lookup (line_table, body_loc); |
2924 | const line_map *next_map = linemap_lookup (line_table, next_loc); |
2925 | const line_map *guard_map = linemap_lookup (line_table, guard_loc); |
2926 | |
2927 | /* Now see if the following token (after the body) is coming from the |
2928 | same macro expansion. If it is, it might be a problem. */ |
2929 | if (body_map != next_map) |
2930 | return; |
2931 | |
2932 | /* The conditional itself must not come from the same expansion, because |
2933 | we don't want to warn about |
2934 | #define IF if (x) x++; y++ |
2935 | and similar. */ |
2936 | if (guard_map == body_map) |
2937 | return; |
2938 | |
2939 | /* Handle the case where NEXT and BODY come from the same expansion while |
2940 | GUARD doesn't, yet we shouldn't warn. E.g. |
2941 | |
2942 | #define GUARD if (...) |
2943 | #define GUARD2 GUARD |
2944 | |
2945 | and in the definition of another macro: |
2946 | |
2947 | GUARD2 |
2948 | foo (); |
2949 | return 1; |
2950 | */ |
2951 | while (linemap_macro_expansion_map_p (guard_map)) |
2952 | { |
2953 | const line_map_macro *mm = linemap_check_macro (map: guard_map); |
2954 | guard_loc_exp = mm->get_expansion_point_location (); |
2955 | guard_map = linemap_lookup (line_table, guard_loc_exp); |
2956 | if (guard_map == body_map) |
2957 | return; |
2958 | } |
2959 | |
2960 | auto_diagnostic_group d; |
2961 | if (warning_at (body_loc, OPT_Wmultistatement_macros, |
2962 | "macro expands to multiple statements" )) |
2963 | inform (guard_loc, "some parts of macro expansion are not guarded by " |
2964 | "this %qs clause" , guard_tinfo_to_string (keyword)); |
2965 | } |
2966 | |
2967 | /* Return struct or union type if the alignment of data member, FIELD, |
2968 | is less than the alignment of TYPE. Otherwise, return NULL_TREE. |
2969 | If RVALUE is true, only arrays evaluate to pointers. */ |
2970 | |
2971 | static tree |
2972 | check_alignment_of_packed_member (tree type, tree field, bool rvalue) |
2973 | { |
2974 | /* Check alignment of the data member. */ |
2975 | if (TREE_CODE (field) == FIELD_DECL |
2976 | && (DECL_PACKED (field) || TYPE_PACKED (TREE_TYPE (field))) |
2977 | /* Ignore FIELDs not laid out yet. */ |
2978 | && DECL_FIELD_OFFSET (field) |
2979 | && (!rvalue || TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)) |
2980 | { |
2981 | /* Check the expected alignment against the field alignment. */ |
2982 | unsigned int type_align = min_align_of_type (type); |
2983 | tree context = DECL_CONTEXT (field); |
2984 | unsigned int record_align = min_align_of_type (context); |
2985 | if (record_align < type_align) |
2986 | return context; |
2987 | tree field_off = byte_position (field); |
2988 | if (!multiple_of_p (TREE_TYPE (field_off), field_off, |
2989 | size_int (type_align))) |
2990 | return context; |
2991 | } |
2992 | |
2993 | return NULL_TREE; |
2994 | } |
2995 | |
2996 | /* Return struct or union type if the right hand value, RHS: |
2997 | 1. Is a pointer value which isn't aligned to a pointer type TYPE. |
2998 | 2. Is an address which takes the unaligned address of packed member |
2999 | of struct or union when assigning to TYPE. |
3000 | Otherwise, return NULL_TREE. */ |
3001 | |
3002 | static tree |
3003 | check_address_or_pointer_of_packed_member (tree type, tree rhs) |
3004 | { |
3005 | bool rvalue = true; |
3006 | bool indirect = false; |
3007 | |
3008 | if (INDIRECT_REF_P (rhs)) |
3009 | { |
3010 | rhs = TREE_OPERAND (rhs, 0); |
3011 | STRIP_NOPS (rhs); |
3012 | indirect = true; |
3013 | } |
3014 | |
3015 | if (TREE_CODE (rhs) == ADDR_EXPR) |
3016 | { |
3017 | rhs = TREE_OPERAND (rhs, 0); |
3018 | rvalue = indirect; |
3019 | } |
3020 | |
3021 | if (!POINTER_TYPE_P (type)) |
3022 | return NULL_TREE; |
3023 | |
3024 | type = TREE_TYPE (type); |
3025 | |
3026 | if (TREE_CODE (rhs) == PARM_DECL |
3027 | || VAR_P (rhs) |
3028 | || TREE_CODE (rhs) == CALL_EXPR) |
3029 | { |
3030 | tree rhstype = TREE_TYPE (rhs); |
3031 | if (TREE_CODE (rhs) == CALL_EXPR) |
3032 | { |
3033 | rhs = CALL_EXPR_FN (rhs); /* Pointer expression. */ |
3034 | if (rhs == NULL_TREE) |
3035 | return NULL_TREE; |
3036 | rhs = TREE_TYPE (rhs); /* Pointer type. */ |
3037 | /* We could be called while processing a template and RHS could be |
3038 | a functor. In that case it's a class, not a pointer. */ |
3039 | if (!rhs || !POINTER_TYPE_P (rhs)) |
3040 | return NULL_TREE; |
3041 | rhs = TREE_TYPE (rhs); /* Function type. */ |
3042 | rhstype = TREE_TYPE (rhs); |
3043 | if (!rhstype || !POINTER_TYPE_P (rhstype)) |
3044 | return NULL_TREE; |
3045 | rvalue = true; |
3046 | } |
3047 | if (rvalue && POINTER_TYPE_P (rhstype)) |
3048 | rhstype = TREE_TYPE (rhstype); |
3049 | while (TREE_CODE (rhstype) == ARRAY_TYPE) |
3050 | rhstype = TREE_TYPE (rhstype); |
3051 | if (TYPE_PACKED (rhstype)) |
3052 | { |
3053 | unsigned int type_align = min_align_of_type (type); |
3054 | unsigned int rhs_align = min_align_of_type (rhstype); |
3055 | if (rhs_align < type_align) |
3056 | { |
3057 | auto_diagnostic_group d; |
3058 | location_t location = EXPR_LOC_OR_LOC (rhs, input_location); |
3059 | if (warning_at (location, OPT_Waddress_of_packed_member, |
3060 | "converting a packed %qT pointer (alignment %d) " |
3061 | "to a %qT pointer (alignment %d) may result in " |
3062 | "an unaligned pointer value" , |
3063 | rhstype, rhs_align, type, type_align)) |
3064 | { |
3065 | tree decl = TYPE_STUB_DECL (rhstype); |
3066 | if (decl) |
3067 | inform (DECL_SOURCE_LOCATION (decl), "defined here" ); |
3068 | decl = TYPE_STUB_DECL (type); |
3069 | if (decl) |
3070 | inform (DECL_SOURCE_LOCATION (decl), "defined here" ); |
3071 | } |
3072 | } |
3073 | } |
3074 | return NULL_TREE; |
3075 | } |
3076 | |
3077 | tree context = NULL_TREE; |
3078 | |
3079 | /* Check alignment of the object. */ |
3080 | while (handled_component_p (t: rhs)) |
3081 | { |
3082 | if (TREE_CODE (rhs) == COMPONENT_REF) |
3083 | { |
3084 | tree field = TREE_OPERAND (rhs, 1); |
3085 | context = check_alignment_of_packed_member (type, field, rvalue); |
3086 | if (context) |
3087 | break; |
3088 | } |
3089 | if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE) |
3090 | rvalue = false; |
3091 | if (rvalue) |
3092 | return NULL_TREE; |
3093 | rhs = TREE_OPERAND (rhs, 0); |
3094 | } |
3095 | |
3096 | return context; |
3097 | } |
3098 | |
3099 | /* Check and warn if the right hand value, RHS: |
3100 | 1. Is a pointer value which isn't aligned to a pointer type TYPE. |
3101 | 2. Is an address which takes the unaligned address of packed member |
3102 | of struct or union when assigning to TYPE. |
3103 | */ |
3104 | |
3105 | static void |
3106 | check_and_warn_address_or_pointer_of_packed_member (tree type, tree rhs) |
3107 | { |
3108 | bool nop_p = false; |
3109 | tree orig_rhs; |
3110 | |
3111 | do |
3112 | { |
3113 | while (TREE_CODE (rhs) == COMPOUND_EXPR) |
3114 | rhs = TREE_OPERAND (rhs, 1); |
3115 | orig_rhs = rhs; |
3116 | STRIP_NOPS (rhs); |
3117 | nop_p |= orig_rhs != rhs; |
3118 | } |
3119 | while (orig_rhs != rhs); |
3120 | |
3121 | if (TREE_CODE (rhs) == COND_EXPR) |
3122 | { |
3123 | /* Check the THEN path. */ |
3124 | check_and_warn_address_or_pointer_of_packed_member |
3125 | (type, TREE_OPERAND (rhs, 1)); |
3126 | |
3127 | /* Check the ELSE path. */ |
3128 | check_and_warn_address_or_pointer_of_packed_member |
3129 | (type, TREE_OPERAND (rhs, 2)); |
3130 | } |
3131 | else |
3132 | { |
3133 | if (nop_p) |
3134 | { |
3135 | switch (TREE_CODE (rhs)) |
3136 | { |
3137 | case ADDR_EXPR: |
3138 | /* Address is taken. */ |
3139 | case PARM_DECL: |
3140 | case VAR_DECL: |
3141 | /* Pointer conversion. */ |
3142 | break; |
3143 | case CALL_EXPR: |
3144 | /* Function call. */ |
3145 | break; |
3146 | default: |
3147 | return; |
3148 | } |
3149 | } |
3150 | |
3151 | tree context |
3152 | = check_address_or_pointer_of_packed_member (type, rhs); |
3153 | if (context) |
3154 | { |
3155 | location_t loc = EXPR_LOC_OR_LOC (rhs, input_location); |
3156 | warning_at (loc, OPT_Waddress_of_packed_member, |
3157 | "taking address of packed member of %qT may result " |
3158 | "in an unaligned pointer value" , |
3159 | context); |
3160 | } |
3161 | } |
3162 | } |
3163 | |
3164 | /* Warn if the right hand value, RHS: |
3165 | 1. Is a pointer value which isn't aligned to a pointer type TYPE. |
3166 | 2. Is an address which takes the unaligned address of packed member |
3167 | of struct or union when assigning to TYPE. |
3168 | */ |
3169 | |
3170 | void |
3171 | warn_for_address_or_pointer_of_packed_member (tree type, tree rhs) |
3172 | { |
3173 | if (!warn_address_of_packed_member) |
3174 | return; |
3175 | |
3176 | /* Don't warn if we don't assign RHS to a pointer. */ |
3177 | if (!POINTER_TYPE_P (type)) |
3178 | return; |
3179 | |
3180 | check_and_warn_address_or_pointer_of_packed_member (type, rhs); |
3181 | } |
3182 | |
3183 | /* Return EXPR + 1. Convenience helper used below. */ |
3184 | |
3185 | static inline tree |
3186 | plus_one (tree expr) |
3187 | { |
3188 | tree type = TREE_TYPE (expr); |
3189 | return fold_build2 (PLUS_EXPR, type, expr, build_int_cst (type, 1)); |
3190 | } |
3191 | |
3192 | /* Try to strip the expressions from around a VLA bound added internally |
3193 | to make it fit the domain mold, including any casts, and return |
3194 | the result. The goal is to obtain the PARM_DECL the VLA bound may |
3195 | refer to. */ |
3196 | |
3197 | static tree |
3198 | vla_bound_parm_decl (tree expr) |
3199 | { |
3200 | if (!expr) |
3201 | return NULL_TREE; |
3202 | |
3203 | if (TREE_CODE (expr) == NOP_EXPR) |
3204 | expr = TREE_OPERAND (expr, 0); |
3205 | if (TREE_CODE (expr) == PLUS_EXPR |
3206 | && integer_all_onesp (TREE_OPERAND (expr, 1))) |
3207 | { |
3208 | expr = TREE_OPERAND (expr, 0); |
3209 | if (TREE_CODE (expr) == NOP_EXPR) |
3210 | expr = TREE_OPERAND (expr, 0); |
3211 | } |
3212 | if (TREE_CODE (expr) == SAVE_EXPR) |
3213 | { |
3214 | expr = TREE_OPERAND (expr, 0); |
3215 | if (TREE_CODE (expr) == NOP_EXPR) |
3216 | expr = TREE_OPERAND (expr, 0); |
3217 | } |
3218 | return expr; |
3219 | } |
3220 | |
3221 | /* Diagnose mismatches in VLA bounds between function parameters NEWPARMS |
3222 | of pointer types on a redeclaration of a function previously declared |
3223 | with CURPARMS at ORIGLOC. */ |
3224 | |
3225 | static void |
3226 | warn_parm_ptrarray_mismatch (location_t origloc, tree curparms, tree newparms) |
3227 | { |
3228 | /* Maps each named integral parameter seen so far to its position |
3229 | in the argument list; used to associate VLA sizes with arguments. */ |
3230 | hash_map<tree, unsigned> curparm2pos; |
3231 | hash_map<tree, unsigned> newparm2pos; |
3232 | |
3233 | unsigned parmpos = 1; |
3234 | for (tree curp = curparms, newp = newparms; curp && newp; |
3235 | curp = TREE_CHAIN (curp), newp = TREE_CHAIN (newp), ++parmpos) |
3236 | { |
3237 | tree curtyp = TREE_TYPE (curp), newtyp = TREE_TYPE (newp); |
3238 | if (INTEGRAL_TYPE_P (curtyp)) |
3239 | { |
3240 | /* Only add named parameters; unnamed ones cannot be referred |
3241 | to in VLA bounds. */ |
3242 | if (DECL_NAME (curp)) |
3243 | curparm2pos.put (k: curp, v: parmpos); |
3244 | if (DECL_NAME (newp)) |
3245 | newparm2pos.put (k: newp, v: parmpos); |
3246 | |
3247 | continue; |
3248 | } |
3249 | |
3250 | /* The parameter types should match at this point so only test one. */ |
3251 | if (TREE_CODE (curtyp) != POINTER_TYPE) |
3252 | continue; |
3253 | |
3254 | do |
3255 | { |
3256 | curtyp = TREE_TYPE (curtyp); |
3257 | newtyp = TREE_TYPE (newtyp); |
3258 | |
3259 | if (!newtyp) |
3260 | /* Bail on error. */ |
3261 | return; |
3262 | } |
3263 | while (TREE_CODE (curtyp) == POINTER_TYPE |
3264 | && TREE_CODE (newtyp) == POINTER_TYPE); |
3265 | |
3266 | if (TREE_CODE (curtyp) != ARRAY_TYPE |
3267 | || TREE_CODE (newtyp) != ARRAY_TYPE) |
3268 | { |
3269 | if (curtyp == error_mark_node |
3270 | || newtyp == error_mark_node) |
3271 | /* Bail on error. */ |
3272 | return; |
3273 | |
3274 | continue; |
3275 | } |
3276 | |
3277 | tree curdom = TYPE_DOMAIN (curtyp), newdom = TYPE_DOMAIN (newtyp); |
3278 | tree curbnd = curdom ? TYPE_MAX_VALUE (curdom) : NULL_TREE; |
3279 | tree newbnd = newdom ? TYPE_MAX_VALUE (newdom) : NULL_TREE; |
3280 | |
3281 | if (DECL_P (curp)) |
3282 | origloc = DECL_SOURCE_LOCATION (curp); |
3283 | else if (EXPR_P (curp) && EXPR_HAS_LOCATION (curp)) |
3284 | origloc = EXPR_LOCATION (curp); |
3285 | |
3286 | /* The location of the parameter in the current redeclaration. */ |
3287 | location_t newloc = DECL_SOURCE_LOCATION (newp); |
3288 | if (origloc == UNKNOWN_LOCATION) |
3289 | origloc = newloc; |
3290 | |
3291 | /* Issue -Warray-parameter unless one or more mismatches involves |
3292 | a VLA bound; then issue -Wvla-parameter. */ |
3293 | int opt = OPT_Warray_parameter_; |
3294 | /* Traverse the two array types looking for variable bounds and |
3295 | comparing the two in each pair for mismatches either in their |
3296 | positions in the function parameter list or lexicographically |
3297 | for others. Record the 1-based parameter position of each |
3298 | mismatch in BNDVEC, and the location of each parameter in |
3299 | the mismatch in WARNLOC (for the new parameter list) and |
3300 | NOTELOC (for the current parameter list). */ |
3301 | unsigned bndpos = 1; |
3302 | auto_vec<int> bndvec; |
3303 | gcc_rich_location warnloc (newloc); |
3304 | gcc_rich_location noteloc (origloc); |
3305 | for ( ; curtyp || newtyp; |
3306 | ++bndpos, |
3307 | curbnd = curdom ? TYPE_MAX_VALUE (curdom) : NULL_TREE, |
3308 | newbnd = newdom ? TYPE_MAX_VALUE (newdom) : NULL_TREE) |
3309 | { |
3310 | /* Try to strip each bound down to the PARM_DECL if it does |
3311 | correspond to one. Either bound can be null if it's |
3312 | unspecified (i.e., has the [*] form). */ |
3313 | curbnd = vla_bound_parm_decl (expr: curbnd); |
3314 | newbnd = vla_bound_parm_decl (expr: newbnd); |
3315 | |
3316 | /* Peel the current bound off CURTYP and NEWTYP, skipping |
3317 | over any subsequent pointer types. */ |
3318 | if (curtyp && TREE_CODE (curtyp) == ARRAY_TYPE) |
3319 | { |
3320 | do |
3321 | curtyp = TREE_TYPE (curtyp); |
3322 | while (TREE_CODE (curtyp) == POINTER_TYPE); |
3323 | if (TREE_CODE (curtyp) == ARRAY_TYPE) |
3324 | curdom = TYPE_DOMAIN (curtyp); |
3325 | else |
3326 | curdom = NULL_TREE; |
3327 | } |
3328 | else |
3329 | curtyp = NULL_TREE; |
3330 | |
3331 | if (newtyp && TREE_CODE (newtyp) == ARRAY_TYPE) |
3332 | { |
3333 | do |
3334 | newtyp = TREE_TYPE (newtyp); |
3335 | while (TREE_CODE (newtyp) == POINTER_TYPE); |
3336 | if (TREE_CODE (newtyp) == ARRAY_TYPE) |
3337 | newdom = TYPE_DOMAIN (newtyp); |
3338 | else |
3339 | newdom = NULL_TREE; |
3340 | } |
3341 | else |
3342 | newtyp = NULL_TREE; |
3343 | |
3344 | /* Move on to the next bound if this one is unspecified. */ |
3345 | if (!curbnd && !newbnd) |
3346 | continue; |
3347 | |
3348 | /* Try to find each bound in the parameter list. */ |
3349 | const unsigned* const pcurbndpos = curparm2pos.get (k: curbnd); |
3350 | const unsigned* const pnewbndpos = newparm2pos.get (k: newbnd); |
3351 | /* Move on if both bounds refer to the same parameter. */ |
3352 | if (pcurbndpos && pnewbndpos && *pcurbndpos == *pnewbndpos) |
3353 | continue; |
3354 | |
3355 | /* Move on if the bounds look the same. */ |
3356 | if (!pcurbndpos && !pnewbndpos |
3357 | && curbnd && newbnd |
3358 | && operand_equal_p (curbnd, newbnd, |
3359 | flags: OEP_DECL_NAME | OEP_LEXICOGRAPHIC)) |
3360 | continue; |
3361 | |
3362 | if ((curbnd && TREE_CODE (curbnd) != INTEGER_CST) |
3363 | || (newbnd && TREE_CODE (newbnd) != INTEGER_CST)) |
3364 | opt = OPT_Wvla_parameter; |
3365 | |
3366 | /* Record the mismatch. */ |
3367 | bndvec.safe_push (obj: bndpos); |
3368 | /* Underline the bounding parameter in the declaration. */ |
3369 | if (curbnd && TREE_CODE (curbnd) == PARM_DECL) |
3370 | noteloc.add_range (DECL_SOURCE_LOCATION (curbnd)); |
3371 | if (newbnd && TREE_CODE (newbnd) == PARM_DECL) |
3372 | warnloc.add_range (DECL_SOURCE_LOCATION (newbnd)); |
3373 | } |
3374 | |
3375 | const unsigned nbnds = bndvec.length (); |
3376 | if (!nbnds) |
3377 | continue; |
3378 | |
3379 | /* Use attr_access to format the parameter types. */ |
3380 | attr_access spec = { }; |
3381 | const std::string newparmstr = spec.array_as_string (TREE_TYPE (newp)); |
3382 | const std::string curparmstr = spec.array_as_string (TREE_TYPE (curp)); |
3383 | |
3384 | if (warning_n (&warnloc, opt, nbnds, |
3385 | "mismatch in bound %Z of argument %u declared as %s" , |
3386 | "mismatch in bounds %Z of argument %u declared as %s" , |
3387 | bndvec.address (), nbnds, parmpos, newparmstr.c_str ())) |
3388 | inform (¬eloc, "previously declared as %s" , curparmstr.c_str ()); |
3389 | } |
3390 | } |
3391 | |
3392 | /* Format EXPR if nonnull and return the formatted string. If EXPR is |
3393 | null return DFLT. */ |
3394 | |
3395 | static inline const char* |
3396 | expr_to_str (pretty_printer &pp, tree expr, const char *dflt) |
3397 | { |
3398 | if (!expr) |
3399 | return dflt; |
3400 | |
3401 | dump_generic_node (&pp, expr, 0, TDF_VOPS | TDF_MEMSYMS, false); |
3402 | return pp_formatted_text (&pp); |
3403 | } |
3404 | |
3405 | /* Detect and diagnose a mismatch between an attribute access specification |
3406 | on the original declaration of FNDECL and that on the parameters NEWPARMS |
3407 | from its redeclaration. ORIGLOC is the location of the first declaration |
3408 | (FNDECL's is set to the location of the redeclaration). */ |
3409 | |
3410 | void |
3411 | warn_parm_array_mismatch (location_t origloc, tree fndecl, tree newparms) |
3412 | { |
3413 | /* The original parameter list (copied from the original declaration |
3414 | into the current [re]declaration, FNDECL)). The two are equal if |
3415 | and only if FNDECL is the first declaration. */ |
3416 | tree curparms = DECL_ARGUMENTS (fndecl); |
3417 | if (!curparms || !newparms || curparms == newparms) |
3418 | return; |
3419 | |
3420 | if (TREE_CODE (curparms) != PARM_DECL |
3421 | || TREE_CODE (newparms) != PARM_DECL) |
3422 | return; |
3423 | /* Extract the (possibly empty) attribute access specification from |
3424 | the declaration and its type (it doesn't yet reflect those created |
3425 | in response to NEWPARMS). */ |
3426 | rdwr_map cur_idx; |
3427 | tree fntype = TREE_TYPE (fndecl); |
3428 | init_attr_rdwr_indices (&cur_idx, TYPE_ATTRIBUTES (fntype)); |
3429 | |
3430 | /* Build a (possibly null) chain of access attributes corresponding |
3431 | to NEWPARMS. */ |
3432 | const bool builtin = fndecl_built_in_p (node: fndecl); |
3433 | tree newattrs = build_attr_access_from_parms (newparms, builtin); |
3434 | |
3435 | /* Extract the (possibly empty) attribute access specification from |
3436 | NEWATTRS. */ |
3437 | rdwr_map new_idx; |
3438 | init_attr_rdwr_indices (&new_idx, newattrs); |
3439 | |
3440 | if (cur_idx.is_empty () && new_idx.is_empty ()) |
3441 | { |
3442 | /* If both specs are empty check pointers to VLAs for mismatches. */ |
3443 | warn_parm_ptrarray_mismatch (origloc, curparms, newparms); |
3444 | return; |
3445 | } |
3446 | /* ...otherwise, if at least one spec isn't empty there may be mismatches, |
3447 | such as between f(T*) and f(T[1]), where the former mapping would be |
3448 | empty. */ |
3449 | |
3450 | /* Create an empty access specification and use it for pointers with |
3451 | no spec of their own. */ |
3452 | attr_access ptr_spec = { }; |
3453 | |
3454 | /* Iterate over the two lists of function parameters, comparing their |
3455 | respective mappings and diagnosing mismatches. */ |
3456 | unsigned parmpos = 0; |
3457 | for (tree curp = curparms, newp = newparms; curp; |
3458 | curp = TREE_CHAIN (curp), newp = TREE_CHAIN (newp), ++parmpos) |
3459 | { |
3460 | if (!newp) |
3461 | /* Bail on invalid redeclarations with fewer arguments. */ |
3462 | return; |
3463 | |
3464 | /* Only check pointers and C++ references. */ |
3465 | tree curptype = TREE_TYPE (curp); |
3466 | tree newptype = TREE_TYPE (newp); |
3467 | if (!POINTER_TYPE_P (curptype) || !POINTER_TYPE_P (newptype)) |
3468 | continue; |
3469 | |
3470 | /* Skip mismatches in __builtin_va_list that is commonly |
3471 | an array but that in declarations of built-ins decays |
3472 | to a pointer. */ |
3473 | if (builtin && TREE_TYPE (newptype) == TREE_TYPE (va_list_type_node)) |
3474 | continue; |
3475 | |
3476 | /* Access specs for the argument on the current (previous) and |
3477 | new (to replace the current) declarations. Either may be null, |
3478 | indicating the parameter is an ordinary pointer with no size |
3479 | associated with it. */ |
3480 | attr_access *cura = cur_idx.get (k: parmpos); |
3481 | attr_access *newa = new_idx.get (k: parmpos); |
3482 | |
3483 | if (!newa) |
3484 | { |
3485 | /* Continue of both parameters are pointers with no size |
3486 | associated with it. */ |
3487 | if (!cura) |
3488 | continue; |
3489 | |
3490 | /* Otherwise point at PTR_SPEC and set its parameter pointer |
3491 | and number. */ |
3492 | newa = &ptr_spec; |
3493 | newa->ptr = newp; |
3494 | newa->ptrarg = parmpos; |
3495 | } |
3496 | else if (!cura) |
3497 | { |
3498 | cura = &ptr_spec; |
3499 | cura->ptr = curp; |
3500 | cura->ptrarg = parmpos; |
3501 | } |
3502 | |
3503 | /* Set if the parameter is [re]declared as a VLA. */ |
3504 | const bool cur_vla_p = cura->size || cura->minsize == HOST_WIDE_INT_M1U; |
3505 | const bool new_vla_p = newa->size || newa->minsize == HOST_WIDE_INT_M1U; |
3506 | |
3507 | if (DECL_P (curp)) |
3508 | origloc = DECL_SOURCE_LOCATION (curp); |
3509 | else if (EXPR_P (curp) && EXPR_HAS_LOCATION (curp)) |
3510 | origloc = EXPR_LOCATION (curp); |
3511 | |
3512 | /* The location of the parameter in the current redeclaration. */ |
3513 | location_t newloc = DECL_SOURCE_LOCATION (newp); |
3514 | if (origloc == UNKNOWN_LOCATION) |
3515 | origloc = newloc; |
3516 | |
3517 | const std::string newparmstr = newa->array_as_string (newptype); |
3518 | const std::string curparmstr = cura->array_as_string (curptype); |
3519 | if (new_vla_p && !cur_vla_p) |
3520 | { |
3521 | if (warning_at (newloc, OPT_Wvla_parameter, |
3522 | "argument %u of type %s declared as " |
3523 | "a variable length array" , |
3524 | parmpos + 1, newparmstr.c_str ())) |
3525 | inform (origloc, |
3526 | (cura == &ptr_spec |
3527 | ? G_("previously declared as a pointer %s" ) |
3528 | : G_("previously declared as an ordinary array %s" )), |
3529 | curparmstr.c_str ()); |
3530 | continue; |
3531 | } |
3532 | |
3533 | if (newa == &ptr_spec) |
3534 | { |
3535 | /* The new declaration uses the pointer form. Detect mismatches |
3536 | between the pointer and a previous array or VLA forms. */ |
3537 | if (cura->minsize == HOST_WIDE_INT_M1U) |
3538 | { |
3539 | /* Diagnose a pointer/VLA mismatch. */ |
3540 | if (warning_at (newloc, OPT_Wvla_parameter, |
3541 | "argument %u of type %s declared " |
3542 | "as a pointer" , |
3543 | parmpos + 1, newparmstr.c_str ())) |
3544 | inform (origloc, |
3545 | "previously declared as a variable length array %s" , |
3546 | curparmstr.c_str ()); |
3547 | continue; |
3548 | } |
3549 | |
3550 | if (cura->minsize && cura->minsize != HOST_WIDE_INT_M1U) |
3551 | { |
3552 | /* Diagnose mismatches between arrays with a constant |
3553 | bound and pointers. */ |
3554 | if (warning_at (newloc, OPT_Warray_parameter_, |
3555 | "argument %u of type %s declared " |
3556 | "as a pointer" , |
3557 | parmpos + 1, newparmstr.c_str ())) |
3558 | inform (origloc, "previously declared as an array %s" , |
3559 | curparmstr.c_str ()); |
3560 | continue; |
3561 | } |
3562 | } |
3563 | |
3564 | if (!new_vla_p && cur_vla_p) |
3565 | { |
3566 | if (warning_at (newloc, OPT_Wvla_parameter, |
3567 | "argument %u of type %s declared " |
3568 | "as an ordinary array" , |
3569 | parmpos + 1, newparmstr.c_str ())) |
3570 | inform (origloc, |
3571 | "previously declared as a variable length array %s" , |
3572 | curparmstr.c_str ()); |
3573 | continue; |
3574 | } |
3575 | |
3576 | /* Move on to the next pair of parameters if both of the current |
3577 | pair are VLAs with a single variable bound that refers to |
3578 | a parameter at the same position. */ |
3579 | if (newa->size && cura->size |
3580 | && newa->sizarg != UINT_MAX |
3581 | && newa->sizarg == cura->sizarg |
3582 | && newa->minsize == cura->minsize |
3583 | && !TREE_PURPOSE (newa->size) && !TREE_PURPOSE (cura->size)) |
3584 | continue; |
3585 | |
3586 | if (newa->size || cura->size) |
3587 | { |
3588 | unsigned newunspec, curunspec; |
3589 | unsigned newbnds = newa->vla_bounds (&newunspec) + newunspec; |
3590 | unsigned curbnds = cura->vla_bounds (&curunspec) + curunspec; |
3591 | |
3592 | if (newbnds != curbnds) |
3593 | { |
3594 | if (warning_n (newloc, OPT_Wvla_parameter, newbnds, |
3595 | "argument %u of type %s declared with " |
3596 | "%u variable bound" , |
3597 | "argument %u of type %s declared with " |
3598 | "%u variable bounds" , |
3599 | parmpos + 1, newparmstr.c_str (), |
3600 | newbnds)) |
3601 | inform_n (origloc, curbnds, |
3602 | "previously declared as %s with %u variable bound" , |
3603 | "previously declared as %s with %u variable bounds" , |
3604 | curparmstr.c_str (), curbnds); |
3605 | continue; |
3606 | } |
3607 | |
3608 | if (newunspec > curunspec) |
3609 | { |
3610 | location_t warnloc = newloc, noteloc = origloc; |
3611 | const char *warnparmstr = newparmstr.c_str (); |
3612 | const char *noteparmstr = curparmstr.c_str (); |
3613 | unsigned warnunspec = newunspec, noteunspec = curunspec; |
3614 | |
3615 | if (warning_n (warnloc, OPT_Wvla_parameter, warnunspec, |
3616 | "argument %u of type %s declared with " |
3617 | "%u unspecified variable bound" , |
3618 | "argument %u of type %s declared with " |
3619 | "%u unspecified variable bounds" , |
3620 | parmpos + 1, warnparmstr, warnunspec)) |
3621 | { |
3622 | if (warnloc == newloc) |
3623 | inform_n (noteloc, noteunspec, |
3624 | "previously declared as %s with %u unspecified " |
3625 | "variable bound" , |
3626 | "previously declared as %s with %u unspecified " |
3627 | "variable bounds" , |
3628 | noteparmstr, noteunspec); |
3629 | else |
3630 | inform_n (noteloc, noteunspec, |
3631 | "subsequently declared as %s with %u unspecified " |
3632 | "variable bound" , |
3633 | "subsequently declared as %s with %u unspecified " |
3634 | "variable bounds" , |
3635 | noteparmstr, noteunspec); |
3636 | } |
3637 | continue; |
3638 | } |
3639 | } |
3640 | |
3641 | /* Iterate over the lists of VLA variable bounds, comparing each |
3642 | pair for equality, and diagnosing mismatches. */ |
3643 | for (tree newvbl = newa->size, curvbl = cura->size; newvbl && curvbl; |
3644 | newvbl = TREE_CHAIN (newvbl), curvbl = TREE_CHAIN (curvbl)) |
3645 | { |
3646 | tree newpos = TREE_PURPOSE (newvbl); |
3647 | tree curpos = TREE_PURPOSE (curvbl); |
3648 | |
3649 | tree newbnd = vla_bound_parm_decl (TREE_VALUE (newvbl)); |
3650 | tree curbnd = vla_bound_parm_decl (TREE_VALUE (curvbl)); |
3651 | |
3652 | if (newpos == curpos && newbnd == curbnd) |
3653 | /* In the expected case when both bounds either refer to |
3654 | the same positional parameter or when neither does, |
3655 | and both are the same expression they are necessarily |
3656 | the same. */ |
3657 | continue; |
3658 | |
3659 | pretty_printer pp1, pp2; |
3660 | const char* const newbndstr = expr_to_str (pp&: pp1, expr: newbnd, dflt: "*" ); |
3661 | const char* const curbndstr = expr_to_str (pp&: pp2, expr: curbnd, dflt: "*" ); |
3662 | |
3663 | if (!newpos != !curpos |
3664 | || (newpos && !tree_int_cst_equal (newpos, curpos))) |
3665 | { |
3666 | /* Diagnose a mismatch between a specified VLA bound and |
3667 | an unspecified one. This can only happen in the most |
3668 | significant bound. |
3669 | |
3670 | Distinguish between the common case of bounds that are |
3671 | other function parameters such as in |
3672 | f (int n, int[n]); |
3673 | and others. */ |
3674 | |
3675 | gcc_rich_location richloc (newloc); |
3676 | bool warned; |
3677 | if (newpos) |
3678 | { |
3679 | /* Also underline the VLA bound argument. */ |
3680 | richloc.add_range (DECL_SOURCE_LOCATION (newbnd)); |
3681 | warned = warning_at (&richloc, OPT_Wvla_parameter, |
3682 | "argument %u of type %s declared " |
3683 | "with mismatched bound argument %E" , |
3684 | parmpos + 1, newparmstr.c_str (), |
3685 | plus_one (expr: newpos)); |
3686 | } |
3687 | else |
3688 | warned = warning_at (&richloc, OPT_Wvla_parameter, |
3689 | "argument %u of type %s declared " |
3690 | "with mismatched bound %<%s%>" , |
3691 | parmpos + 1, newparmstr.c_str (), |
3692 | newbndstr); |
3693 | |
3694 | if (warned) |
3695 | { |
3696 | gcc_rich_location richloc (origloc); |
3697 | if (curpos) |
3698 | { |
3699 | /* Also underline the VLA bound argument. */ |
3700 | richloc.add_range (DECL_SOURCE_LOCATION (curbnd)); |
3701 | inform (&richloc, "previously declared as %s with " |
3702 | "bound argument %E" , |
3703 | curparmstr.c_str (), plus_one (expr: curpos)); |
3704 | } |
3705 | else |
3706 | inform (&richloc, "previously declared as %s with bound " |
3707 | "%<%s%>" , curparmstr.c_str (), curbndstr); |
3708 | |
3709 | continue; |
3710 | } |
3711 | } |
3712 | |
3713 | if (!newpos && newbnd && curbnd) |
3714 | { |
3715 | /* The VLA bounds don't refer to other function parameters. |
3716 | Compare them lexicographically to detect gross mismatches |
3717 | such as between T[foo()] and T[bar()]. */ |
3718 | if (operand_equal_p (newbnd, curbnd, |
3719 | flags: OEP_DECL_NAME | OEP_LEXICOGRAPHIC)) |
3720 | continue; |
3721 | |
3722 | if (warning_at (newloc, OPT_Wvla_parameter, |
3723 | "argument %u of type %s declared with " |
3724 | "mismatched bound %<%s%>" , |
3725 | parmpos + 1, newparmstr.c_str (), newbndstr)) |
3726 | inform (origloc, "previously declared as %s with bound %qs" , |
3727 | curparmstr.c_str (), curbndstr); |
3728 | continue; |
3729 | } |
3730 | } |
3731 | |
3732 | if (newa->minsize == cura->minsize |
3733 | || (((newa->minsize == 0 && newa->mode != access_deferred) |
3734 | || (cura->minsize == 0 && cura->mode != access_deferred)) |
3735 | && newa != &ptr_spec |
3736 | && cura != &ptr_spec)) |
3737 | continue; |
3738 | |
3739 | if (!newa->static_p && !cura->static_p && warn_array_parameter < 2) |
3740 | /* Avoid warning about mismatches in ordinary (non-static) arrays |
3741 | at levels below 2. */ |
3742 | continue; |
3743 | |
3744 | if (warning_at (newloc, OPT_Warray_parameter_, |
3745 | "argument %u of type %s with mismatched bound" , |
3746 | parmpos + 1, newparmstr.c_str ())) |
3747 | inform (origloc, "previously declared as %s" , curparmstr.c_str ()); |
3748 | } |
3749 | } |
3750 | |
3751 | /* Warn about divisions of two sizeof operators when the first one is applied |
3752 | to an array and the divisor does not equal the size of the array element. |
3753 | For instance: |
3754 | |
3755 | sizeof (ARR) / sizeof (OP) |
3756 | |
3757 | ARR is the array argument of the first sizeof, ARR_TYPE is its ARRAY_TYPE. |
3758 | OP1 is the whole second SIZEOF_EXPR, or its argument; TYPE1 is the type |
3759 | of the second argument. */ |
3760 | |
3761 | void |
3762 | maybe_warn_sizeof_array_div (location_t loc, tree arr, tree arr_type, |
3763 | tree op1, tree type1) |
3764 | { |
3765 | tree elt_type = TREE_TYPE (arr_type); |
3766 | |
3767 | if (!warn_sizeof_array_div |
3768 | /* Don't warn on multidimensional arrays. */ |
3769 | || TREE_CODE (elt_type) == ARRAY_TYPE) |
3770 | return; |
3771 | |
3772 | if (!tree_int_cst_equal (TYPE_SIZE (elt_type), TYPE_SIZE (type1))) |
3773 | { |
3774 | auto_diagnostic_group d; |
3775 | if (warning_at (loc, OPT_Wsizeof_array_div, |
3776 | "expression does not compute the number of " |
3777 | "elements in this array; element type is " |
3778 | "%qT, not %qT" , elt_type, type1)) |
3779 | { |
3780 | if (EXPR_HAS_LOCATION (op1)) |
3781 | { |
3782 | location_t op1_loc = EXPR_LOCATION (op1); |
3783 | gcc_rich_location richloc (op1_loc); |
3784 | richloc.add_fixit_insert_before (where: op1_loc, new_content: "(" ); |
3785 | richloc.add_fixit_insert_after (where: op1_loc, new_content: ")" ); |
3786 | inform (&richloc, "add parentheses around %qE to " |
3787 | "silence this warning" , op1); |
3788 | } |
3789 | else |
3790 | inform (loc, "add parentheses around the second %<sizeof%> " |
3791 | "to silence this warning" ); |
3792 | if (DECL_P (arr)) |
3793 | inform (DECL_SOURCE_LOCATION (arr), "array %qD declared here" , arr); |
3794 | } |
3795 | } |
3796 | } |
3797 | |
3798 | /* Warn about C++20 [depr.array.comp] array comparisons: "Equality |
3799 | and relational comparisons between two operands of array type are |
3800 | deprecated." We also warn in C and earlier C++ standards. CODE is |
3801 | the code for this comparison, OP0 and OP1 are the operands. */ |
3802 | |
3803 | void |
3804 | do_warn_array_compare (location_t location, tree_code code, tree op0, tree op1) |
3805 | { |
3806 | STRIP_NOPS (op0); |
3807 | STRIP_NOPS (op1); |
3808 | if (TREE_CODE (op0) == ADDR_EXPR) |
3809 | op0 = TREE_OPERAND (op0, 0); |
3810 | if (TREE_CODE (op1) == ADDR_EXPR) |
3811 | op1 = TREE_OPERAND (op1, 0); |
3812 | |
3813 | auto_diagnostic_group d; |
3814 | if (warning_at (location, OPT_Warray_compare, |
3815 | (c_dialect_cxx () && cxx_dialect >= cxx20) |
3816 | ? G_("comparison between two arrays is deprecated in C++20" ) |
3817 | : G_("comparison between two arrays" ))) |
3818 | { |
3819 | /* C doesn't allow +arr. */ |
3820 | if (c_dialect_cxx ()) |
3821 | inform (location, "use unary %<+%> which decays operands to pointers " |
3822 | "or %<&%D[0] %s &%D[0]%> to compare the addresses" , |
3823 | op0, op_symbol_code (code), op1); |
3824 | else |
3825 | inform (location, "use %<&%D[0] %s &%D[0]%> to compare the addresses" , |
3826 | op0, op_symbol_code (code), op1); |
3827 | } |
3828 | } |
3829 | |
3830 | /* Given LHS_VAL ^ RHS_VAL, where LHS_LOC is the location of the LHS, |
3831 | OPERATOR_LOC is the location of the ^, and RHS_LOC the location of the |
3832 | RHS, complain with -Wxor-used-as-pow if it looks like the user meant |
3833 | exponentiation rather than xor. */ |
3834 | |
3835 | void |
3836 | check_for_xor_used_as_pow (location_t lhs_loc, tree lhs_val, |
3837 | location_t operator_loc, |
3838 | location_t rhs_loc, tree rhs_val) |
3839 | { |
3840 | /* Only complain if both args are non-negative integer constants that fit |
3841 | in uhwi. */ |
3842 | if (!tree_fits_uhwi_p (lhs_val) || !tree_fits_uhwi_p (rhs_val)) |
3843 | return; |
3844 | |
3845 | /* Only complain if the LHS is 2 or 10. */ |
3846 | unsigned HOST_WIDE_INT lhs_uhwi = tree_to_uhwi (lhs_val); |
3847 | if (lhs_uhwi != 2 && lhs_uhwi != 10) |
3848 | return; |
3849 | |
3850 | unsigned HOST_WIDE_INT rhs_uhwi = tree_to_uhwi (rhs_val); |
3851 | unsigned HOST_WIDE_INT xor_result = lhs_uhwi ^ rhs_uhwi; |
3852 | binary_op_rich_location loc (operator_loc, |
3853 | lhs_val, rhs_val, false); |
3854 | |
3855 | /* Reject cases where we don't have 3 distinct locations. |
3856 | This can happen e.g. due to macro expansion with |
3857 | -ftrack-macro-expansion=0 */ |
3858 | if (!(lhs_loc != operator_loc |
3859 | && lhs_loc != rhs_loc |
3860 | && operator_loc != rhs_loc)) |
3861 | return; |
3862 | |
3863 | /* Reject cases in which any of the locations came from a macro. */ |
3864 | if (from_macro_expansion_at (loc: lhs_loc) |
3865 | || from_macro_expansion_at (loc: operator_loc) |
3866 | || from_macro_expansion_at (loc: rhs_loc)) |
3867 | return; |
3868 | |
3869 | /* If we issue fix-it hints with the warning then we will also issue a |
3870 | note suggesting how to suppress the warning with a different change. |
3871 | These proposed changes are incompatible. */ |
3872 | loc.fixits_cannot_be_auto_applied (); |
3873 | |
3874 | auto_diagnostic_group d; |
3875 | bool warned = false; |
3876 | if (lhs_uhwi == 2) |
3877 | { |
3878 | /* Would exponentiation fit in int, in long long, or not at all? */ |
3879 | if (rhs_uhwi < (INT_TYPE_SIZE - 1)) |
3880 | { |
3881 | unsigned HOST_WIDE_INT suggested_result = 1 << rhs_uhwi; |
3882 | loc.add_fixit_replace (where: lhs_loc, new_content: "1" ); |
3883 | loc.add_fixit_replace (where: operator_loc, new_content: "<<" ); |
3884 | warned = warning_at (&loc, OPT_Wxor_used_as_pow, |
3885 | "result of %<%wu^%wu%> is %wu;" |
3886 | " did you mean %<1 << %wu%> (%wu)?" , |
3887 | lhs_uhwi, rhs_uhwi, xor_result, |
3888 | rhs_uhwi, suggested_result); |
3889 | } |
3890 | else if (rhs_uhwi < (LONG_LONG_TYPE_SIZE - 1)) |
3891 | { |
3892 | loc.add_fixit_replace (where: lhs_loc, new_content: "1LL" ); |
3893 | loc.add_fixit_replace (where: operator_loc, new_content: "<<" ); |
3894 | warned = warning_at (&loc, OPT_Wxor_used_as_pow, |
3895 | "result of %<%wu^%wu%> is %wu;" |
3896 | " did you mean %<1LL << %wu%>?" , |
3897 | lhs_uhwi, rhs_uhwi, xor_result, |
3898 | rhs_uhwi); |
3899 | } |
3900 | else if (rhs_uhwi <= LONG_LONG_TYPE_SIZE) |
3901 | warned = warning_at (&loc, OPT_Wxor_used_as_pow, |
3902 | "result of %<%wu^%wu%> is %wu;" |
3903 | " did you mean exponentiation?" , |
3904 | lhs_uhwi, rhs_uhwi, xor_result); |
3905 | /* Otherwise assume it's an xor. */ |
3906 | } |
3907 | else |
3908 | { |
3909 | gcc_assert (lhs_uhwi == 10); |
3910 | loc.add_fixit_replace (where: lhs_loc, new_content: "1" ); |
3911 | loc.add_fixit_replace (where: operator_loc, new_content: "e" ); |
3912 | warned = warning_at (&loc, OPT_Wxor_used_as_pow, |
3913 | "result of %<%wu^%wu%> is %wu;" |
3914 | " did you mean %<1e%wu%>?" , |
3915 | lhs_uhwi, rhs_uhwi, xor_result, |
3916 | rhs_uhwi); |
3917 | } |
3918 | if (warned) |
3919 | { |
3920 | gcc_rich_location note_loc (lhs_loc); |
3921 | if (lhs_uhwi == 2) |
3922 | note_loc.add_fixit_replace (where: lhs_loc, new_content: "0x2" ); |
3923 | else |
3924 | { |
3925 | gcc_assert (lhs_uhwi == 10); |
3926 | note_loc.add_fixit_replace (where: lhs_loc, new_content: "0xa" ); |
3927 | } |
3928 | note_loc.fixits_cannot_be_auto_applied (); |
3929 | inform (¬e_loc, |
3930 | "you can silence this warning by using a hexadecimal constant" |
3931 | " (%wx rather than %wd)" , |
3932 | lhs_uhwi, lhs_uhwi); |
3933 | } |
3934 | } |
3935 | |