c++: Disable -Wint-in-bool-context in instantiations

Message ID 20210512162142.438022-1-polacek@redhat.com
State New
Headers show
Series
  • c++: Disable -Wint-in-bool-context in instantiations
Related show

Commit Message

H.J. Lu via Gcc-patches May 12, 2021, 4:21 p.m.
This warning is of questionable value when it's emitted when
instantiating a template, as in the following testcase.  It could be
silenced by writing hb(i) << 1 instead of 2 * hb(i) but that's
unnecessary obfuscation.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

gcc/cp/ChangeLog:

	* pt.c (tsubst_copy_and_build): Add warn_int_in_bool_context
	sentinel.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wint-in-bool-context-2.C: New test.
---
 gcc/cp/pt.c                                      |  1 +
 .../g++.dg/warn/Wint-in-bool-context-2.C         | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wint-in-bool-context-2.C


base-commit: 097fde5e7514e909f2e8472be2e008d0cab2260d
-- 
2.31.1

Comments

H.J. Lu via Gcc-patches May 12, 2021, 4:41 p.m. | #1
On 5/12/21 12:21 PM, Marek Polacek wrote:
> This warning is of questionable value when it's emitted when

> instantiating a template, as in the following testcase.  It could be

> silenced by writing hb(i) << 1 instead of 2 * hb(i) but that's

> unnecessary obfuscation.

> 

> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?


OK.

> gcc/cp/ChangeLog:

> 

> 	* pt.c (tsubst_copy_and_build): Add warn_int_in_bool_context

> 	sentinel.

> 

> gcc/testsuite/ChangeLog:

> 

> 	* g++.dg/warn/Wint-in-bool-context-2.C: New test.

> ---

>   gcc/cp/pt.c                                      |  1 +

>   .../g++.dg/warn/Wint-in-bool-context-2.C         | 16 ++++++++++++++++

>   2 files changed, 17 insertions(+)

>   create mode 100644 gcc/testsuite/g++.dg/warn/Wint-in-bool-context-2.C

> 

> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c

> index 85c05af42ed..d7d6a3f2c14 100644

> --- a/gcc/cp/pt.c

> +++ b/gcc/cp/pt.c

> @@ -19801,6 +19801,7 @@ tsubst_copy_and_build (tree t,

>   

>   	warning_sentinel s(warn_useless_cast);

>   	warning_sentinel s2(warn_ignored_qualifiers);

> +	warning_sentinel s3(warn_int_in_bool_context);

>   	switch (TREE_CODE (t))

>   	  {

>   	  case CAST_EXPR:

> diff --git a/gcc/testsuite/g++.dg/warn/Wint-in-bool-context-2.C b/gcc/testsuite/g++.dg/warn/Wint-in-bool-context-2.C

> new file mode 100644

> index 00000000000..6cb482d4dc9

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/warn/Wint-in-bool-context-2.C

> @@ -0,0 +1,16 @@

> +// { dg-do compile { target c++11 } }

> +// { dg-options "-Wint-in-bool-context" }

> +

> +unsigned hb(unsigned i) { return ~i; }

> +

> +template<typename T>

> +void f(int i)

> +{

> +  auto l = [i]() { return T(2 * hb(i)); }; // { dg-bogus "in boolean context" }

> +  (void) l;

> +}

> +

> +int main()

> +{

> +  f<bool>(0);

> +}

> 

> base-commit: 097fde5e7514e909f2e8472be2e008d0cab2260d

>

Patch

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 85c05af42ed..d7d6a3f2c14 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -19801,6 +19801,7 @@  tsubst_copy_and_build (tree t,
 
 	warning_sentinel s(warn_useless_cast);
 	warning_sentinel s2(warn_ignored_qualifiers);
+	warning_sentinel s3(warn_int_in_bool_context);
 	switch (TREE_CODE (t))
 	  {
 	  case CAST_EXPR:
diff --git a/gcc/testsuite/g++.dg/warn/Wint-in-bool-context-2.C b/gcc/testsuite/g++.dg/warn/Wint-in-bool-context-2.C
new file mode 100644
index 00000000000..6cb482d4dc9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wint-in-bool-context-2.C
@@ -0,0 +1,16 @@ 
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wint-in-bool-context" }
+
+unsigned hb(unsigned i) { return ~i; }
+
+template<typename T>
+void f(int i)
+{
+  auto l = [i]() { return T(2 * hb(i)); }; // { dg-bogus "in boolean context" }
+  (void) l;
+}
+
+int main()
+{
+  f<bool>(0);
+}