aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2023-03-04 10:14:33 +0100
committerJakub Jelinek <jakub@redhat.com>2023-03-04 10:14:33 +0100
commitdb1405ddf566fe6129328229579b3f98a574b34c (patch)
tree76e6e6c95b14c13949c0a7324331bb319b2414b8 /gcc/c-family
parent9d5730dee4f42e94004b38f8f4862c0b1f4d964c (diff)
c-family: Account for integral promotions of left shifts for -Wshift-overflow warning [PR107846]
The r13-1100-gacb1e6f43dc2bbedd124 change added match.pd narrowing of left shifts, and while I believe the C++ FE calls the warning on unfolded trees, the C FE folds them and so left shifts where integral promotion happened and so were done in int type will be usually narrowed back to char/signed char/unsigned char/short/unsigned short left shifts if the shift count is constant and fits into the precision of the var being shifted. One possibility would be to restrict the match.pd optimization to GIMPLE only, another don't fold in C FE before this warning (well, we need to fold the shift count operand to constant if possible), the following patch just takes integral promotion into account in the warning code. 2023-03-04 Jakub Jelinek <jakub@redhat.com> PR c/107846 * c-warn.cc: Include langhooks.h. (maybe_warn_shift_overflow): Set type0 to what TREE_TYPE (op0) promotes to rather than TREE_TYPE (op0) itself, if TREE_TYPE (op0) is narrower than type0 and unsigned, use wi::min_precision with UNSIGNED and fold_convert op0 to type0 before emitting the warning. * gcc.dg/pr107846.c: New test.
Diffstat (limited to 'gcc/c-family')
-rw-r--r--gcc/c-family/c-warn.cc13
1 files changed, 10 insertions, 3 deletions
diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc
index 29ae1ea1dc8..2941cf4c2f9 100644
--- a/gcc/c-family/c-warn.cc
+++ b/gcc/c-family/c-warn.cc
@@ -39,6 +39,7 @@ along with GCC; see the file COPYING3. If not see
#include "calls.h"
#include "stor-layout.h"
#include "tree-pretty-print.h"
+#include "langhooks.h"
/* Print a warning if a constant expression had overflow in folding.
Invoke this function on every expression that the language
@@ -2615,14 +2616,19 @@ maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
|| TREE_CODE (op1) != INTEGER_CST)
return false;
- tree type0 = TREE_TYPE (op0);
+ /* match.pd could have narrowed the left shift already,
+ take type promotion into account. */
+ tree type0 = lang_hooks.types.type_promotes_to (TREE_TYPE (op0));
unsigned int prec0 = TYPE_PRECISION (type0);
/* Left-hand operand must be signed. */
if (TYPE_OVERFLOW_WRAPS (type0) || cxx_dialect >= cxx20)
return false;
- unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), SIGNED)
+ signop sign = SIGNED;
+ if (TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (type0))
+ sign = TYPE_SIGN (TREE_TYPE (op0));
+ unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), sign)
+ TREE_INT_CST_LOW (op1));
/* Handle the case of left-shifting 1 into the sign bit.
* However, shifting 1 _out_ of the sign bit, as in
@@ -2645,7 +2651,8 @@ maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
warning_at (loc, OPT_Wshift_overflow_,
"result of %qE requires %u bits to represent, "
"but %qT only has %u bits",
- build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
+ build2_loc (loc, LSHIFT_EXPR, type0,
+ fold_convert (type0, op0), op1),
min_prec, type0, prec0);
return overflowed;