diff mbox

C/C++ PATCH to improve location for -Wparentheses (PR c/71926)

Message ID 20160729143512.GR7007@redhat.com
State New
Headers show

Commit Message

Marek Polacek July 29, 2016, 2:35 p.m. UTC
These two parentheses warnings were missing proper location arguments, so the
location info was less than satisfying.  Fixed thus.

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

2016-07-29  Marek Polacek  <polacek@redhat.com>

	PR c/71926
	* c-common.c (c_common_truthvalue_conversion): Use LOCATION for the
	parentheses warning.

	* semantics.c (maybe_convert_cond): Use the location of COND for the
	parentheses warning.

	* g++.dg/warn/Wparentheses-30.C: New test.
	* gcc.dg/Wparentheses-14.c: New test.


	Marek

Comments

Jeff Law July 29, 2016, 3:31 p.m. UTC | #1
On 07/29/2016 08:35 AM, Marek Polacek wrote:
> These two parentheses warnings were missing proper location arguments, so the
> location info was less than satisfying.  Fixed thus.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2016-07-29  Marek Polacek  <polacek@redhat.com>
>
> 	PR c/71926
> 	* c-common.c (c_common_truthvalue_conversion): Use LOCATION for the
> 	parentheses warning.
>
> 	* semantics.c (maybe_convert_cond): Use the location of COND for the
> 	parentheses warning.
>
> 	* g++.dg/warn/Wparentheses-30.C: New test.
> 	* gcc.dg/Wparentheses-14.c: New test.
OK.
jeff
diff mbox

Patch

diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
index efd9815..27031b5 100644
--- gcc/c-family/c-common.c
+++ gcc/c-family/c-common.c
@@ -4591,8 +4591,9 @@  c_common_truthvalue_conversion (location_t location, tree expr)
       if (!TREE_NO_WARNING (expr)
 	  && warn_parentheses)
 	{
-	  warning (OPT_Wparentheses,
-		   "suggest parentheses around assignment used as truth value");
+	  warning_at (location, OPT_Wparentheses,
+		      "suggest parentheses around assignment used as "
+		      "truth value");
 	  TREE_NO_WARNING (expr) = 1;
 	}
       break;
diff --git gcc/cp/semantics.c gcc/cp/semantics.c
index 63063b8..2fe2d09 100644
--- gcc/cp/semantics.c
+++ gcc/cp/semantics.c
@@ -656,8 +656,8 @@  maybe_convert_cond (tree cond)
       && !TREE_NO_WARNING (cond)
       && warn_parentheses)
     {
-      warning (OPT_Wparentheses,
-	       "suggest parentheses around assignment used as truth value");
+      warning_at (EXPR_LOC_OR_LOC (cond, input_location), OPT_Wparentheses,
+		  "suggest parentheses around assignment used as truth value");
       TREE_NO_WARNING (cond) = 1;
     }
 
diff --git gcc/testsuite/g++.dg/warn/Wparentheses-30.C gcc/testsuite/g++.dg/warn/Wparentheses-30.C
index e69de29..ea7c741 100644
--- gcc/testsuite/g++.dg/warn/Wparentheses-30.C
+++ gcc/testsuite/g++.dg/warn/Wparentheses-30.C
@@ -0,0 +1,11 @@ 
+/* PR c/71926 */
+/* { dg-options "-Wparentheses" }  */
+
+int
+f (void)
+{
+  int a = 1, b = 2, c = 3, d = 4;
+  if (a = 2 || (b != 3 && c != 4 && d != 5)) /* { dg-warning "9:suggest parentheses" } */
+    return 1;
+  return 0;
+}
diff --git gcc/testsuite/gcc.dg/Wparentheses-14.c gcc/testsuite/gcc.dg/Wparentheses-14.c
index e69de29..36dedf7 100644
--- gcc/testsuite/gcc.dg/Wparentheses-14.c
+++ gcc/testsuite/gcc.dg/Wparentheses-14.c
@@ -0,0 +1,11 @@ 
+/* PR c/71926 */
+/* { dg-options "-Wparentheses" }  */
+
+int
+f (void)
+{
+  int a = 1, b = 2, c = 3, d = 4;
+  if (a = 2 || (b != 3 && c != 4 && d != 5)) /* { dg-warning "7:suggest parentheses" } */
+    return 1;
+  return 0;
+}