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