This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH RFA: More -Wparentheses checks for C++


In my earlier patch adding support for -Wparentheses to the C++
frontend, I forgot about the case of a class with a conversion to
bool.  Assignments of instances of such a class do not follow the
c_common_truthvalue_conversion path used by assignments of builtin
types.

This patch adds support for this.  It adds -Wparentheses warnings for
    bool b = a = b;
    bool fn(C a, C b) { return a = b; }
where a and b have class type.  These warnings already exist when a
and b are builtin types.

Tested with a C/C++ bootstrap and gcc/libstdc++-v3 testsuite run on
i686-pc-linux-gnu.

OK for mainline?

Ian


gcc/ChangeLog:

2006-12-28  Ian Lance Taylor  <iant@google.com>

	* c-common.c (c_common_truthvalue_conversion): When warning about
	using an assignment as a truth value, set TREE_NO_WARNING.

gcc/cp/ChangeLog:

2006-12-28  Ian Lance Taylor  <iant@google.com>

	* semantics.c (maybe_convert_cond): Optionally warn when using an
	assignment as a condition.
	* typeck.c (convert_for_assignment): Optionally warn about
	assigning the result of an assignment to a bool.

gcc/testsuite/ChangeLog:

2006-12-28  Ian Lance Taylor  <iant@google.com>

	* g++.dg/warn/Wparentheses-22.C: New test.
	* g++.dg/warn/Wparentheses-23.C: New test.


Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 120236)
+++ gcc/cp/typeck.c	(working copy)
@@ -6306,6 +6306,17 @@ convert_for_assignment (tree type, tree 
 		 errtype);
     }
 
+  /* If -Wparentheses, warn about a = b = c when a has type bool.  */
+  if (warn_parentheses
+      && type == boolean_type_node
+      && TREE_CODE (rhs) == MODIFY_EXPR
+      && !TREE_NO_WARNING (rhs))
+    {
+      warning (OPT_Wparentheses,
+	       "suggest parentheses around assignment used as truth value");
+      TREE_NO_WARNING (rhs) = 1;
+    }
+
   return perform_implicit_conversion (strip_top_quals (type), rhs);
 }
 
Index: gcc/cp/semantics.c
===================================================================
--- gcc/cp/semantics.c	(revision 120236)
+++ gcc/cp/semantics.c	(working copy)
@@ -587,6 +587,16 @@ maybe_convert_cond (tree cond)
 
   /* Do the conversion.  */
   cond = convert_from_reference (cond);
+
+  if (TREE_CODE (cond) == MODIFY_EXPR
+      && !TREE_NO_WARNING (cond)
+      && warn_parentheses)
+    {
+      warning (OPT_Wparentheses,
+	       "suggest parentheses around assignment used as truth value");
+      TREE_NO_WARNING (cond) = 1;
+    }
+
   return condition_conversion (cond);
 }
 
Index: gcc/c-common.c
===================================================================
--- gcc/c-common.c	(revision 120236)
+++ gcc/c-common.c	(working copy)
@@ -2726,9 +2726,13 @@ c_common_truthvalue_conversion (tree exp
       break;
 
     case MODIFY_EXPR:
-      if (!TREE_NO_WARNING (expr))
-	warning (OPT_Wparentheses,
-		 "suggest parentheses around assignment used as truth value");
+      if (!TREE_NO_WARNING (expr)
+	  && warn_parentheses)
+	{
+	  warning (OPT_Wparentheses,
+		   "suggest parentheses around assignment used as truth value");
+	  TREE_NO_WARNING (expr) = 1;
+	}
       break;
 
     default:
Index: gcc/testsuite/g++.dg/warn/Wparentheses-22.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wparentheses-22.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/Wparentheses-22.C	(revision 0)
@@ -0,0 +1,109 @@
+// { dg-do compile }
+// { dg-options "-Wparentheses" }
+
+// Warnings for assignments used as truth-values when using classes.
+// Like Wparentheses-1.C, but with a class.
+
+int foo (int);
+
+class C
+{
+ public:
+  C()
+    : b(0)
+  { }
+
+  // Use default assignment constructor.
+
+  // Provide conversion to bool so that an instance of this class will
+  // work as a condition.
+  operator bool() const
+  { return b != 0; }
+
+ private:
+  int b;
+};
+
+C a, b, c;
+bool d;
+
+void
+bar (void)
+{
+  if (a = b) // { dg-warning "assignment" "correct warning" }
+    foo (0);
+  if ((a = b))
+    foo (1);
+  if (a = a) // { dg-warning "assignment" "correct warning" }
+    foo (2);
+  if ((a = a))
+    foo (3);
+  if (b = c) // { dg-warning "assignment" "correct warning" }
+    foo (4);
+  else
+    foo (5);
+  if ((b = c))
+    foo (6);
+  else
+    foo (7);
+  if (b = b) // { dg-warning "assignment" "correct warning" }
+    foo (8);
+  else
+    foo (9);
+  if ((b = b))
+    foo (10);
+  else
+    foo (11);
+  while (c = b) // { dg-warning "assignment" "correct warning" }
+    foo (12);
+  while ((c = b))
+    foo (13);
+  while (c = c) // { dg-warning "assignment" "correct warning" }
+    foo (14);
+  while ((c = c))
+    foo (15);
+  do foo (16); while (a = b); // { dg-warning "assignment" "correct warning" }
+  do foo (17); while ((a = b));
+  do foo (18); while (a = a); // { dg-warning "assignment" "correct warning" }
+  do foo (19); while ((a = a));
+  for (;c = b;) // { dg-warning "assignment" "correct warning" }
+    foo (20);
+  for (;(c = b);)
+    foo (21);
+  for (;c = c;) // { dg-warning "assignment" "correct warning" }
+    foo (22);
+  for (;(c = c);)
+    foo (23);
+  d = a = b; // { dg-warning "assignment" "correct warning" }
+  foo (24);
+  d = (a = b);
+  foo (25);
+  d = a = a; // { dg-warning "assignment" "correct warning" }
+  foo (26);
+  d = (a = a);
+  foo (27);
+}
+
+bool
+bar1 (void)
+{
+  return a = b; // { dg-warning "assignment" "correct warning" }
+}
+
+bool
+bar2 (void)
+{
+  return (a = b);
+}
+
+bool
+bar3 (void)
+{
+  return a = a; // { dg-warning "assignment" "correct warning" }
+}
+
+bool
+bar4 (void)
+{
+  return (a = a);
+}
Index: gcc/testsuite/g++.dg/warn/Wparentheses-23.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wparentheses-23.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/Wparentheses-23.C	(revision 0)
@@ -0,0 +1,117 @@
+// { dg-do compile }
+// { dg-options "-Wparentheses" }
+
+// Template version of Wparentheses-22.C.
+
+int foo (int);
+
+class C
+{
+ public:
+  C()
+    : b(0)
+  { }
+
+  // Use default assignment constructor.
+
+  // Provide conversion to bool so that an instance of this class will
+  // work as a condition.
+  operator bool() const
+  { return b != 0; }
+
+ private:
+  int b;
+};
+
+C a, b, c;
+bool d;
+
+template<class T>
+void
+bar (T)
+{
+  if (a = b) // { dg-warning "assignment" "correct warning" }
+    foo (0);
+  if ((a = b))
+    foo (1);
+  if (a = a) // { dg-warning "assignment" "correct warning" }
+    foo (2);
+  if ((a = a))
+    foo (3);
+  if (b = c) // { dg-warning "assignment" "correct warning" }
+    foo (4);
+  else
+    foo (5);
+  if ((b = c))
+    foo (6);
+  else
+    foo (7);
+  if (b = b) // { dg-warning "assignment" "correct warning" }
+    foo (8);
+  else
+    foo (9);
+  if ((b = b))
+    foo (10);
+  else
+    foo (11);
+  while (c = b) // { dg-warning "assignment" "correct warning" }
+    foo (12);
+  while ((c = b))
+    foo (13);
+  while (c = c) // { dg-warning "assignment" "correct warning" }
+    foo (14);
+  while ((c = c))
+    foo (15);
+  do foo (16); while (a = b); // { dg-warning "assignment" "correct warning" }
+  do foo (17); while ((a = b));
+  do foo (18); while (a = a); // { dg-warning "assignment" "correct warning" }
+  do foo (19); while ((a = a));
+  for (;c = b;) // { dg-warning "assignment" "correct warning" }
+    foo (20);
+  for (;(c = b);)
+    foo (21);
+  for (;c = c;) // { dg-warning "assignment" "correct warning" }
+    foo (22);
+  for (;(c = c);)
+    foo (23);
+  d = a = b; // { dg-warning "assignment" "correct warning" }
+  foo (24);
+  d = (a = b);
+  foo (25);
+  d = a = a; // { dg-warning "assignment" "correct warning" }
+  foo (26);
+  d = (a = a);
+  foo (27);
+}
+
+template<class T>
+bool
+bar1 (T)
+{
+  return a = b; // { dg-warning "assignment" "correct warning" }
+}
+
+template<class T>
+bool
+bar2 (T)
+{
+  return (a = b);
+}
+
+template<class T>
+bool
+bar3 (T)
+{
+  return a = a; // { dg-warning "assignment" "correct warning" }
+}
+
+template<class T>
+bool
+bar4 (T)
+{
+  return (a = a);
+}
+
+template void bar<int> (int); // { dg-warning "instantiated" }
+template bool bar1<int> (int); // { dg-warning "instantiated" }
+template bool bar2<int> (int);
+template bool bar3<int> (int); // { dg-warning "instantiated" }
+template bool bar4<int> (int);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]