diff options
author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2007-01-02 17:49:07 +0000 |
---|---|---|
committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2007-01-02 17:49:07 +0000 |
commit | 60a0513e1edb1f19485faf0a68810f49bf7f3d2b (patch) | |
tree | 8c417c46fb300ca0e55218e9d49e86f6e77ea715 | |
parent | 02619052e76db7ede9d3151c6e3cbf47d6e389e3 (diff) | |
download | gcc-60a0513e1edb1f19485faf0a68810f49bf7f3d2b.tar.gz |
./:
* c-common.c (c_common_truthvalue_conversion): When warning about
using an assignment as a truth value, set TREE_NO_WARNING.
cp/:
* 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.
testsuite/:
* g++.dg/warn/Wparentheses-22.C: New test.
* g++.dg/warn/Wparentheses-23.C: New test.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@120348 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r-- | gcc/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/c-common.c | 12 | ||||
-rw-r--r-- | gcc/cp/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/cp/semantics.c | 12 | ||||
-rw-r--r-- | gcc/cp/typeck.c | 14 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wparentheses-22.C | 111 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wparentheses-23.C | 121 |
8 files changed, 281 insertions, 6 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 78b8ce8314c..30e9a20759a 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2006-01-02 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. + 2007-01-02 Manuel Lopez-Ibanez <manu@gcc.gnu.org> PR middle-end/7651 diff --git a/gcc/c-common.c b/gcc/c-common.c index 0658141dadc..cca1490a9d5 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -1,6 +1,6 @@ /* Subroutines shared by all languages that are variants of C. Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, - 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. This file is part of GCC. @@ -2726,9 +2726,13 @@ c_common_truthvalue_conversion (tree expr) 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: diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 16da3ee0aa1..abc7a71d7fc 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2006-01-02 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. + 2007-01-02 Douglas Gregor <doug.gregor@gmail.com> * pt.c (canonical_template_parms): Correct typo in comment. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 41d2be9817c..5b8906eb020 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -3,7 +3,7 @@ building RTL. These routines are used both during actual parsing and during the instantiation of template functions. - Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 + Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. Written by Mark Mitchell (mmitchell@usa.net) based on code found formerly in parse.y and pt.c. @@ -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); } diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 7e8b785b3eb..6c05b9fe1f1 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -1,6 +1,7 @@ /* Build expressions with type checking for C++ compiler. Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 + Free Software Foundation, Inc. Hacked by Michael Tiemann (tiemann@cygnus.com) This file is part of GCC. @@ -6365,6 +6366,17 @@ convert_for_assignment (tree type, tree rhs, 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); } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e2e3d3dbb05..5f17a946fe6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2006-01-02 Ian Lance Taylor <iant@google.com> + + * g++.dg/warn/Wparentheses-22.C: New test. + * g++.dg/warn/Wparentheses-23.C: New test. + 2007-01-02 Manuel Lopez-Ibanez <manu@gcc.gnu.org> PR middle-end/7651 diff --git a/gcc/testsuite/g++.dg/warn/Wparentheses-22.C b/gcc/testsuite/g++.dg/warn/Wparentheses-22.C new file mode 100644 index 00000000000..395953dd446 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wparentheses-22.C @@ -0,0 +1,111 @@ +// { 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); + if (C(a)) + foo (28); +} + +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); +} diff --git a/gcc/testsuite/g++.dg/warn/Wparentheses-23.C b/gcc/testsuite/g++.dg/warn/Wparentheses-23.C new file mode 100644 index 00000000000..755c574d0f2 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wparentheses-23.C @@ -0,0 +1,121 @@ +// { 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); + if (C(a)) + foo (28); +} + +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); |