* C++ PATCH to prevent bogus -Wint-in-bool-context warning (PR c++/79184)
@ 2017-02-10 15:55 Marek Polacek
2017-02-10 16:07 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2017-02-10 15:55 UTC (permalink / raw)
To: GCC Patches, Jason Merrill
This PR complains about -Wint-in-bool-context warning where it shouldn't.
Here, in "f<1 * 1>()" the MULT_EXPR isn't really in a boolean context, but we
warn anyway during overload resolution, when adding various template
candidates. I believe the way to prevent this is to honor complain &
tf_warning which is what this patch does. Does that seem reasonable or
is there a better way?
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2017-02-10 Marek Polacek <polacek@redhat.com>
PR c++/79184
* cvt.c (ocp_convert): Add a sentinel against -Wint-in-bool-context
if warnings shouldn't be given.
* g++.dg/warn/Wint-in-bool-context-1.C: New.
diff --git gcc/cp/cvt.c gcc/cp/cvt.c
index ae9991a..5f4b5e3 100644
--- gcc/cp/cvt.c
+++ gcc/cp/cvt.c
@@ -798,7 +798,15 @@ ocp_convert (tree type, tree expr, int convtype, int flags,
to the underlying type first. */
if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
- return cp_truthvalue_conversion (e);
+ if (complain & tf_warning)
+ return cp_truthvalue_conversion (e);
+ else
+ {
+ /* Prevent bogus -Wint-in-bool-context warnings coming
+ from c_common_truthvalue_conversion down the line. */
+ warning_sentinel w (warn_int_in_bool_context);
+ return cp_truthvalue_conversion (e);
+ }
}
converted = convert_to_integer_maybe_fold (type, e, dofold);
diff --git gcc/testsuite/g++.dg/warn/Wint-in-bool-context-1.C gcc/testsuite/g++.dg/warn/Wint-in-bool-context-1.C
index e69de29..117eca4 100644
--- gcc/testsuite/g++.dg/warn/Wint-in-bool-context-1.C
+++ gcc/testsuite/g++.dg/warn/Wint-in-bool-context-1.C
@@ -0,0 +1,16 @@
+// PR c++/79184
+// { dg-do compile }
+// { dg-options "-Wint-in-bool-context" }
+
+enum { E = 2 };
+template <bool> void f(int) { }
+template <int> void f() {}
+
+int
+main ()
+{
+ f<1 * 1>(); // { dg-bogus "in boolean context" }
+ f<1 << 1>(); // { dg-bogus "in boolean context" }
+ f<1 ? 3 : 2>(); // { dg-bogus "in boolean context" }
+ f<E>(); // { dg-bogus "in boolean context" }
+}
Marek
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: C++ PATCH to prevent bogus -Wint-in-bool-context warning (PR c++/79184)
2017-02-10 15:55 C++ PATCH to prevent bogus -Wint-in-bool-context warning (PR c++/79184) Marek Polacek
@ 2017-02-10 16:07 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2017-02-10 16:07 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches
OK.
On Fri, Feb 10, 2017 at 10:55 AM, Marek Polacek <polacek@redhat.com> wrote:
> This PR complains about -Wint-in-bool-context warning where it shouldn't.
> Here, in "f<1 * 1>()" the MULT_EXPR isn't really in a boolean context, but we
> warn anyway during overload resolution, when adding various template
> candidates. I believe the way to prevent this is to honor complain &
> tf_warning which is what this patch does. Does that seem reasonable or
> is there a better way?
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2017-02-10 Marek Polacek <polacek@redhat.com>
>
> PR c++/79184
> * cvt.c (ocp_convert): Add a sentinel against -Wint-in-bool-context
> if warnings shouldn't be given.
>
> * g++.dg/warn/Wint-in-bool-context-1.C: New.
>
> diff --git gcc/cp/cvt.c gcc/cp/cvt.c
> index ae9991a..5f4b5e3 100644
> --- gcc/cp/cvt.c
> +++ gcc/cp/cvt.c
> @@ -798,7 +798,15 @@ ocp_convert (tree type, tree expr, int convtype, int flags,
> to the underlying type first. */
> if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
> e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
> - return cp_truthvalue_conversion (e);
> + if (complain & tf_warning)
> + return cp_truthvalue_conversion (e);
> + else
> + {
> + /* Prevent bogus -Wint-in-bool-context warnings coming
> + from c_common_truthvalue_conversion down the line. */
> + warning_sentinel w (warn_int_in_bool_context);
> + return cp_truthvalue_conversion (e);
> + }
> }
>
> converted = convert_to_integer_maybe_fold (type, e, dofold);
> diff --git gcc/testsuite/g++.dg/warn/Wint-in-bool-context-1.C gcc/testsuite/g++.dg/warn/Wint-in-bool-context-1.C
> index e69de29..117eca4 100644
> --- gcc/testsuite/g++.dg/warn/Wint-in-bool-context-1.C
> +++ gcc/testsuite/g++.dg/warn/Wint-in-bool-context-1.C
> @@ -0,0 +1,16 @@
> +// PR c++/79184
> +// { dg-do compile }
> +// { dg-options "-Wint-in-bool-context" }
> +
> +enum { E = 2 };
> +template <bool> void f(int) { }
> +template <int> void f() {}
> +
> +int
> +main ()
> +{
> + f<1 * 1>(); // { dg-bogus "in boolean context" }
> + f<1 << 1>(); // { dg-bogus "in boolean context" }
> + f<1 ? 3 : 2>(); // { dg-bogus "in boolean context" }
> + f<E>(); // { dg-bogus "in boolean context" }
> +}
>
> Marek
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-02-10 16:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-10 15:55 C++ PATCH to prevent bogus -Wint-in-bool-context warning (PR c++/79184) Marek Polacek
2017-02-10 16:07 ` 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).