* C++ PATCH to abate shift warnings (PR c++/68979)
@ 2016-01-12 12:52 Marek Polacek
2016-01-12 13:02 ` Jakub Jelinek
2016-01-12 13:27 ` Jason Merrill
0 siblings, 2 replies; 8+ messages in thread
From: Marek Polacek @ 2016-01-12 12:52 UTC (permalink / raw)
To: GCC Patches, Jason Merrill
Seems that people find compile-time error on the following testcase overly
pedantic. I.e. that "enum A { X = -1 << 1 };" should compile, at least with
-fpermissive. So I've changed the error_at into permerror and the return value
of cxx_eval_check_shift_p now depends on flag_permissive. Luckily, I didn't
have to modify any of the existing tests.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2016-01-12 Marek Polacek <polacek@redhat.com>
PR c++/68979
* constexpr.c (cxx_eval_check_shift_p): Use permerror rather than
error_at and return negated flag_permissive.
* g++.dg/warn/permissive-1.C: New test.
diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c
index e60180e..dbcc242 100644
--- gcc/cp/constexpr.c
+++ gcc/cp/constexpr.c
@@ -1512,17 +1512,17 @@ cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
if (tree_int_cst_sgn (rhs) == -1)
{
if (!ctx->quiet)
- error_at (loc, "right operand of shift expression %q+E is negative",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc, "right operand of shift expression %q+E is negative",
+ build2_loc (loc, code, type, lhs, rhs));
+ return !flag_permissive;
}
if (compare_tree_int (rhs, uprec) >= 0)
{
if (!ctx->quiet)
- error_at (loc, "right operand of shift expression %q+E is >= than "
- "the precision of the left operand",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc, "right operand of shift expression %q+E is >= than "
+ "the precision of the left operand",
+ build2_loc (loc, code, type, lhs, rhs));
+ return !flag_permissive;
}
/* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
@@ -1536,9 +1536,10 @@ cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
if (tree_int_cst_sgn (lhs) == -1)
{
if (!ctx->quiet)
- error_at (loc, "left operand of shift expression %q+E is negative",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc,
+ "left operand of shift expression %q+E is negative",
+ build2_loc (loc, code, type, lhs, rhs));
+ return !flag_permissive;
}
/* For signed x << y the following:
(unsigned) x >> ((prec (lhs) - 1) - y)
@@ -1555,9 +1556,9 @@ cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
if (tree_int_cst_lt (integer_one_node, t))
{
if (!ctx->quiet)
- error_at (loc, "shift expression %q+E overflows",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc, "shift expression %q+E overflows",
+ build2_loc (loc, code, type, lhs, rhs));
+ return !flag_permissive;
}
}
return false;
diff --git gcc/testsuite/g++.dg/warn/permissive-1.C gcc/testsuite/g++.dg/warn/permissive-1.C
index e69de29..7223e68 100644
--- gcc/testsuite/g++.dg/warn/permissive-1.C
+++ gcc/testsuite/g++.dg/warn/permissive-1.C
@@ -0,0 +1,8 @@
+// PR c++/68979
+// { dg-do compile }
+// { dg-options "-fpermissive -Wno-shift-overflow -Wno-shift-count-overflow -Wno-shift-count-negative" }
+
+enum A { AA = -1 << 4 }; // { dg-warning "operand of shift expression" "" { target c++11 } }
+enum B { BB = 1 << -4 }; // { dg-warning "operand of shift expression" }
+enum C { CC = 1 << 100 }; // { dg-warning "operand of shift expression" }
+enum D { DD = 31 << 30 }; // { dg-warning "shift expression" "" { target c++11 } }
Marek
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: C++ PATCH to abate shift warnings (PR c++/68979)
2016-01-12 12:52 C++ PATCH to abate shift warnings (PR c++/68979) Marek Polacek
@ 2016-01-12 13:02 ` Jakub Jelinek
2016-01-12 13:12 ` Marek Polacek
2016-01-12 13:27 ` Jason Merrill
1 sibling, 1 reply; 8+ messages in thread
From: Jakub Jelinek @ 2016-01-12 13:02 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches, Jason Merrill
On Tue, Jan 12, 2016 at 01:52:01PM +0100, Marek Polacek wrote:
> --- gcc/testsuite/g++.dg/warn/permissive-1.C
> +++ gcc/testsuite/g++.dg/warn/permissive-1.C
> @@ -0,0 +1,8 @@
> +// PR c++/68979
> +// { dg-do compile }
> +// { dg-options "-fpermissive -Wno-shift-overflow -Wno-shift-count-overflow -Wno-shift-count-negative" }
> +
> +enum A { AA = -1 << 4 }; // { dg-warning "operand of shift expression" "" { target c++11 } }
> +enum B { BB = 1 << -4 }; // { dg-warning "operand of shift expression" }
> +enum C { CC = 1 << 100 }; // { dg-warning "operand of shift expression" }
> +enum D { DD = 31 << 30 }; // { dg-warning "shift expression" "" { target c++11 } }
Shouldn't this test be limited to
// { dg-do compile { target int32 } }
or better yet replace the 100 and 30 above with
say __SIZEOF_INT__ * 4 * __CHAR_BIT__ - 4 and __SIZEOF_INT__ * __CHAR_BIT__ - 2
?
I'd guess that on say int16 targets, or int64 targets (if we have any at
some point) or int128 targets this wouldn't do what you are expecting.
{ target int32 } is not exactly right, because it still assumes __CHAR_BIT__ == 8
and for other char sizes it could fail.
Jakub
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: C++ PATCH to abate shift warnings (PR c++/68979)
2016-01-12 13:02 ` Jakub Jelinek
@ 2016-01-12 13:12 ` Marek Polacek
0 siblings, 0 replies; 8+ messages in thread
From: Marek Polacek @ 2016-01-12 13:12 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: GCC Patches, Jason Merrill
On Tue, Jan 12, 2016 at 02:02:16PM +0100, Jakub Jelinek wrote:
> On Tue, Jan 12, 2016 at 01:52:01PM +0100, Marek Polacek wrote:
> > --- gcc/testsuite/g++.dg/warn/permissive-1.C
> > +++ gcc/testsuite/g++.dg/warn/permissive-1.C
> > @@ -0,0 +1,8 @@
> > +// PR c++/68979
> > +// { dg-do compile }
> > +// { dg-options "-fpermissive -Wno-shift-overflow -Wno-shift-count-overflow -Wno-shift-count-negative" }
> > +
> > +enum A { AA = -1 << 4 }; // { dg-warning "operand of shift expression" "" { target c++11 } }
> > +enum B { BB = 1 << -4 }; // { dg-warning "operand of shift expression" }
> > +enum C { CC = 1 << 100 }; // { dg-warning "operand of shift expression" }
> > +enum D { DD = 31 << 30 }; // { dg-warning "shift expression" "" { target c++11 } }
>
> Shouldn't this test be limited to
> // { dg-do compile { target int32 } }
> or better yet replace the 100 and 30 above with
> say __SIZEOF_INT__ * 4 * __CHAR_BIT__ - 4 and __SIZEOF_INT__ * __CHAR_BIT__ - 2
> ?
> I'd guess that on say int16 targets, or int64 targets (if we have any at
> some point) or int128 targets this wouldn't do what you are expecting.
> { target int32 } is not exactly right, because it still assumes __CHAR_BIT__ == 8
> and for other char sizes it could fail.
Oh yeah, forgot about those... The following should be better.
Thanks,
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2016-01-12 Marek Polacek <polacek@redhat.com>
PR c++/68979
* constexpr.c (cxx_eval_check_shift_p): Use permerror rather than
error_at and return negated flag_permissive.
* g++.dg/warn/permissive-1.C: New test.
diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c
index e60180e..dbcc242 100644
--- gcc/cp/constexpr.c
+++ gcc/cp/constexpr.c
@@ -1512,17 +1512,17 @@ cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
if (tree_int_cst_sgn (rhs) == -1)
{
if (!ctx->quiet)
- error_at (loc, "right operand of shift expression %q+E is negative",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc, "right operand of shift expression %q+E is negative",
+ build2_loc (loc, code, type, lhs, rhs));
+ return !flag_permissive;
}
if (compare_tree_int (rhs, uprec) >= 0)
{
if (!ctx->quiet)
- error_at (loc, "right operand of shift expression %q+E is >= than "
- "the precision of the left operand",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc, "right operand of shift expression %q+E is >= than "
+ "the precision of the left operand",
+ build2_loc (loc, code, type, lhs, rhs));
+ return !flag_permissive;
}
/* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
@@ -1536,9 +1536,10 @@ cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
if (tree_int_cst_sgn (lhs) == -1)
{
if (!ctx->quiet)
- error_at (loc, "left operand of shift expression %q+E is negative",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc,
+ "left operand of shift expression %q+E is negative",
+ build2_loc (loc, code, type, lhs, rhs));
+ return !flag_permissive;
}
/* For signed x << y the following:
(unsigned) x >> ((prec (lhs) - 1) - y)
@@ -1555,9 +1556,9 @@ cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
if (tree_int_cst_lt (integer_one_node, t))
{
if (!ctx->quiet)
- error_at (loc, "shift expression %q+E overflows",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc, "shift expression %q+E overflows",
+ build2_loc (loc, code, type, lhs, rhs));
+ return !flag_permissive;
}
}
return false;
diff --git gcc/testsuite/g++.dg/warn/permissive-1.C gcc/testsuite/g++.dg/warn/permissive-1.C
index e69de29..bfaca76 100644
--- gcc/testsuite/g++.dg/warn/permissive-1.C
+++ gcc/testsuite/g++.dg/warn/permissive-1.C
@@ -0,0 +1,8 @@
+// PR c++/68979
+// { dg-do compile { target int32 } }
+// { dg-options "-fpermissive -Wno-shift-overflow -Wno-shift-count-overflow -Wno-shift-count-negative" }
+
+enum A { AA = -1 << 4 }; // { dg-warning "operand of shift expression" "" { target c++11 } }
+enum B { BB = 1 << -4 }; // { dg-warning "operand of shift expression" }
+enum C { CC = 1 << __SIZEOF_INT__ * 4 * __CHAR_BIT__ - 4 }; // { dg-warning "operand of shift expression" }
+enum D { DD = 10 << __SIZEOF_INT__ * __CHAR_BIT__ - 2 }; // { dg-warning "shift expression" "" { target c++11 } }
Marek
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: C++ PATCH to abate shift warnings (PR c++/68979)
2016-01-12 12:52 C++ PATCH to abate shift warnings (PR c++/68979) Marek Polacek
2016-01-12 13:02 ` Jakub Jelinek
@ 2016-01-12 13:27 ` Jason Merrill
2016-01-12 14:05 ` Marek Polacek
1 sibling, 1 reply; 8+ messages in thread
From: Jason Merrill @ 2016-01-12 13:27 UTC (permalink / raw)
To: Marek Polacek, GCC Patches
Changing the diagnostic is OK, but cxx_eval_check_shift_p should return
true regardless of flag_permissive, so that SFINAE results follow the
standard.
Jason
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: C++ PATCH to abate shift warnings (PR c++/68979)
2016-01-12 13:27 ` Jason Merrill
@ 2016-01-12 14:05 ` Marek Polacek
2016-01-12 14:09 ` Jason Merrill
0 siblings, 1 reply; 8+ messages in thread
From: Marek Polacek @ 2016-01-12 14:05 UTC (permalink / raw)
To: Jason Merrill; +Cc: GCC Patches
On Tue, Jan 12, 2016 at 08:27:47AM -0500, Jason Merrill wrote:
> Changing the diagnostic is OK, but cxx_eval_check_shift_p should return true
> regardless of flag_permissive, so that SFINAE results follow the standard.
There's a complication, because if I keep returning true, we'll give a
compile-time error like this:
permissive-1.C:5:18: warning: left operand of shift expression â(-1 << 4)â is
negative [-fpermissive]
enum A { AA = -1 << 4 }; // { dg-warning "operand of shift expression" "" {
target c++11 } }
permissive-1.C:5:21: error: enumerator value for âAAâ is not an integer
constant
enum A { AA = -1 << 4 }; // { dg-warning "operand of shift expression" "" {
target c++11 } }
So I suppose that wouldn't really help. :(
Marek
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: C++ PATCH to abate shift warnings (PR c++/68979)
2016-01-12 14:05 ` Marek Polacek
@ 2016-01-12 14:09 ` Jason Merrill
2016-01-12 14:16 ` Marek Polacek
0 siblings, 1 reply; 8+ messages in thread
From: Jason Merrill @ 2016-01-12 14:09 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches
On 01/12/2016 09:05 AM, Marek Polacek wrote:
> On Tue, Jan 12, 2016 at 08:27:47AM -0500, Jason Merrill wrote:
>> Changing the diagnostic is OK, but cxx_eval_check_shift_p should return true
>> regardless of flag_permissive, so that SFINAE results follow the standard.
>
> There's a complication, because if I keep returning true, we'll give a
> compile-time error like this:
>
> permissive-1.C:5:18: warning: left operand of shift expression â(-1 << 4)â is
> negative [-fpermissive]
> enum A { AA = -1 << 4 }; // { dg-warning "operand of shift expression" "" {
> target c++11 } }
>
> permissive-1.C:5:21: error: enumerator value for âAAâ is not an integer
> constant
> enum A { AA = -1 << 4 }; // { dg-warning "operand of shift expression" "" {
> target c++11 } }
>
> So I suppose that wouldn't really help. :(
In that case, we need to return (!flag_permissive || ctx->quiet).
Jason
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: C++ PATCH to abate shift warnings (PR c++/68979)
2016-01-12 14:09 ` Jason Merrill
@ 2016-01-12 14:16 ` Marek Polacek
2016-01-12 16:38 ` Jason Merrill
0 siblings, 1 reply; 8+ messages in thread
From: Marek Polacek @ 2016-01-12 14:16 UTC (permalink / raw)
To: Jason Merrill; +Cc: GCC Patches
On Tue, Jan 12, 2016 at 09:09:38AM -0500, Jason Merrill wrote:
> In that case, we need to return (!flag_permissive || ctx->quiet).
Thanks. So is this one ok once it passes testing?
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2016-01-12 Marek Polacek <polacek@redhat.com>
PR c++/68979
* constexpr.c (cxx_eval_check_shift_p): Use permerror rather than
error_at and adjust the return value.
* g++.dg/warn/permissive-1.C: New test.
diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c
index e60180e..36a1e42 100644
--- gcc/cp/constexpr.c
+++ gcc/cp/constexpr.c
@@ -1512,17 +1512,17 @@ cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
if (tree_int_cst_sgn (rhs) == -1)
{
if (!ctx->quiet)
- error_at (loc, "right operand of shift expression %q+E is negative",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc, "right operand of shift expression %q+E is negative",
+ build2_loc (loc, code, type, lhs, rhs));
+ return (!flag_permissive || ctx->quiet);
}
if (compare_tree_int (rhs, uprec) >= 0)
{
if (!ctx->quiet)
- error_at (loc, "right operand of shift expression %q+E is >= than "
- "the precision of the left operand",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc, "right operand of shift expression %q+E is >= than "
+ "the precision of the left operand",
+ build2_loc (loc, code, type, lhs, rhs));
+ return (!flag_permissive || ctx->quiet);
}
/* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
@@ -1536,9 +1536,10 @@ cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
if (tree_int_cst_sgn (lhs) == -1)
{
if (!ctx->quiet)
- error_at (loc, "left operand of shift expression %q+E is negative",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc,
+ "left operand of shift expression %q+E is negative",
+ build2_loc (loc, code, type, lhs, rhs));
+ return (!flag_permissive || ctx->quiet);
}
/* For signed x << y the following:
(unsigned) x >> ((prec (lhs) - 1) - y)
@@ -1555,9 +1556,9 @@ cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
if (tree_int_cst_lt (integer_one_node, t))
{
if (!ctx->quiet)
- error_at (loc, "shift expression %q+E overflows",
- build2_loc (loc, code, type, lhs, rhs));
- return true;
+ permerror (loc, "shift expression %q+E overflows",
+ build2_loc (loc, code, type, lhs, rhs));
+ return (!flag_permissive || ctx->quiet);
}
}
return false;
diff --git gcc/testsuite/g++.dg/warn/permissive-1.C gcc/testsuite/g++.dg/warn/permissive-1.C
index e69de29..bfaca76 100644
--- gcc/testsuite/g++.dg/warn/permissive-1.C
+++ gcc/testsuite/g++.dg/warn/permissive-1.C
@@ -0,0 +1,8 @@
+// PR c++/68979
+// { dg-do compile { target int32 } }
+// { dg-options "-fpermissive -Wno-shift-overflow -Wno-shift-count-overflow -Wno-shift-count-negative" }
+
+enum A { AA = -1 << 4 }; // { dg-warning "operand of shift expression" "" { target c++11 } }
+enum B { BB = 1 << -4 }; // { dg-warning "operand of shift expression" }
+enum C { CC = 1 << __SIZEOF_INT__ * 4 * __CHAR_BIT__ - 4 }; // { dg-warning "operand of shift expression" }
+enum D { DD = 10 << __SIZEOF_INT__ * __CHAR_BIT__ - 2 }; // { dg-warning "shift expression" "" { target c++11 } }
Marek
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: C++ PATCH to abate shift warnings (PR c++/68979)
2016-01-12 14:16 ` Marek Polacek
@ 2016-01-12 16:38 ` Jason Merrill
0 siblings, 0 replies; 8+ messages in thread
From: Jason Merrill @ 2016-01-12 16:38 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches
OK.
Jason
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2016-01-12 16:38 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-12 12:52 C++ PATCH to abate shift warnings (PR c++/68979) Marek Polacek
2016-01-12 13:02 ` Jakub Jelinek
2016-01-12 13:12 ` Marek Polacek
2016-01-12 13:27 ` Jason Merrill
2016-01-12 14:05 ` Marek Polacek
2016-01-12 14:09 ` Jason Merrill
2016-01-12 14:16 ` Marek Polacek
2016-01-12 16:38 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).