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