This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch] Add a new warning flag -Wself-assign
- From: Le-Chun Wu <lcwu at google dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Cc: Diego Novillo <dnovillo at google dot com>
- Date: Fri, 28 May 2010 16:47:12 -0700
- Subject: [patch] Add a new warning flag -Wself-assign
Hi,
This patch adds a new warnings flag "-Wself-assign" that warns about
self-assignment (including self-initialization). This warning is
intended for detecting accidental self-assignment due to typos, and
therefore does not warn on a statement that is semantically a
self-assignment after constant folding. Here is an example of what
will trigger a self-assign warning and what will not:
void func()
{
int i = 2;
int x = x; /* warn */
float f = 5.0;
double a[3];
i = i + 0; /* not warn */
f = f / 1; /* not warn */
a[1] = a[1]; /* warn */
i += 0; /* not warn */
}
As mentioned above, this flag warns about self-initialization as well.
Unlike the existing -Winit-self flag, this new flag does not require
the use of -Wuninitialized. And also it warns about
self-initialization of global variables, function-scoped static
variables, and class members which -Winit-self doesn't. Here is
another example:
class Foo {
private:
int a_;
public:
Foo() : a_(a_) {} // warns with -Wself-assign, but not with
-Winit-self
};
int g = g; // warns with -Wself-assign, but not with -Winit-self
Foo foo = foo; // warns with -Wself-assign, but not with -Winit-self
int func()
{
int x = x; // warns with both -Wself-assign and -Winit-self
static int y = y; // warns with -Wself-assign, but not with
-Winit-self
}
In our testing of this new flag on our internal C/C++ code base, we
saw cases where self-assignment are intentional. For example, a C++
programmers might write code to test whether an overloaded `operator='
works when the same object is assigned to itself. One way to work
around the self-assign warning in such case is using the functional
form `object.operator=(object)' instead of the assignment form `object
= object', as shown in the following example.
void test_func()
{
MyType t;
t.operator=(t); // not warn
t = t; // warn
}
Another common case of intentional self-assignment is using `x = x' to
avoid `-Wunused-variable' warning if x is not used. To work around
the self-assign warning in such cases, people can use `(void) x' or
`static_cast<void>(x)' instead (or use the 'unused' attribute).
(Note that most of what I mentioned above is included in the
documentation for -Wself-assign in invoke.texi.)
This patch is bootstrapped and tested on x86_64-unknown-linux-gnu.
What do people think about this new warning flag? Is it OK for the trunk?
Thanks,
Le-chun
2010-05-28 Le-Chun Wu <lcwu@google.com>
* gcc/doc/invoke.texi: Documentation for -Wself-assign.
* gcc/tree.h(tree_base): Add a new tree_base flag indicating if an
expression is the result of constant-folding.
(operand_equal_flag): Add two new flags for operand_equal_p routine.
* gcc/fold-const.c (operand_equal_p): Support comparison of NULL
operands and operands without types.
(set_expr_folded_flag): New helper function.
(fold_unary_loc_1): Renamed from fold_unary_loc.
(fold_unary_loc): A wrapper around fold_unary_loc_1 function that sets
the EXPR_FOLDED flag of the folded expression if folding is
successful.
(fold_binary_loc_1): Renamed from fold_binary_loc.
(fold_binary_loc): A wrapper around fold_binary_loc_1 function that
sets the EXPR_FOLDED flag of the folded expression if folding is
successful.
(fold_ternary_loc_1): Renamed from fold_ternary_loc.
(fold_ternary_loc): A wrapper around fold_ternary_loc_1 function that
sets the EXPR_FOLDED flag of the folded expression if folding is
successful.
* gcc/testsuite/gcc.dg/plugin/selfassign.c (check_self_assign):
Renamed from warn_self_assign.
* gcc/testsuite/gcc.dg/wself-assign-1.c: New test.
* gcc/testsuite/gcc.dg/wself-assign-2.c: New test.
* gcc/testsuite/g++.dg/plugin/selfassign.c (check_self_assign):
Renamed from warn_self_assign.
* gcc/testsuite/g++.dg/warn/Wself-assign-1.C: New test.
* gcc/testsuite/g++.dg/warn/Wself-assign-2.C: New test.
* gcc/testsuite/g++.dg/warn/Wself-assign-3.C: New test.
* gcc/testsuite/g++.dg/warn/Wself-assign-4.C: New test.
* gcc/cp/init.c (perform_member_init): Check for self-assign after
parsing class member initialization.
* gcc/cp/parser.c (cp_parser_assignment_expression): Check for
self-assign after parsing an assignment.
(cp_parser_init_declarator): Check for self-assign after parsing
variable initialization.
* gcc/common.opt: Add -Wself-assign.
* gcc/c-common.c (check_for_self_assign): New function that checks for
self-assignment.
* gcc/c-common.h: Add prototype for check_for_self_assign.
* gcc/c-parser.c (c_parser_declaration_or_fndef): Check for
self-assign after parsing variable initialization.
(c_parser_expr_no_commas): Check for self-assign after parsing an
assignment.
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi (revision 159995)
+++ gcc/doc/invoke.texi (working copy)
@@ -253,7 +253,7 @@ Objective-C and Objective-C++ Dialects}.
-Wparentheses -Wpedantic-ms-format -Wno-pedantic-ms-format @gol
-Wpointer-arith -Wno-pointer-to-int-cast @gol
-Wredundant-decls @gol
--Wreturn-type -Wsequence-point -Wshadow @gol
+-Wreturn-type -Wself-assign -Wsequence-point -Wshadow @gol
-Wsign-compare -Wsign-conversion -Wstack-protector @gol
-Wstrict-aliasing -Wstrict-aliasing=n @gol
-Wstrict-overflow -Wstrict-overflow=@var{n} @gol
@@ -3306,6 +3306,51 @@ definitions, may be found on the GCC rea
This warning is enabled by @option{-Wall} for C and C++.
+@item -Wself-assign
+@opindex Wself-assign
+@opindex Wno-self-assign
+Warn about self-assignment and self-initialization. This warning is intended
+for detecting accidental self-assignment due to typos, and therefore does
+not warn on a statement that is semantically a self-assignment after
+constant folding. Here is an example of what will trigger a self-assign
+warning and what will not:
+
+@smallexample
+@group
+void func()
+@{
+ int i = 2;
+ int x = x; /* warn */
+ float f = 5.0;
+ double a[3];
+
+ i = i + 0; /* not warn */
+ f = f / 1; /* not warn */
+ a[1] = a[1]; /* warn */
+ i += 0; /* not warn */
+@}
+@end group
+@end smallexample
+
+There are cases where self-assignment might be intentional. For example,
+a C++ programmers might write code to test whether an overloaded
+@code{operator=} works when the same object is assigned to itself.
+One way to work around the self-assign warning in such case is using
+the functional form @code{object.operator=(object)} instead of the
+assignment form @code{object = object}, as shown in the following example.
+
+@smallexample
+@group
+void test_func()
+@{
+ MyType t;
+
+ t.operator=(t); // not warn
+ t = t; // warn
+@}
+@end group
+@end smallexample
+
@item -Wreturn-type
@opindex Wreturn-type
@opindex Wno-return-type
@@ -3429,6 +3474,10 @@ This warning is enabled by @option{-Wall
To suppress this warning use the @samp{unused} attribute
(@pxref{Variable Attributes}).
+Note that a classic way to avoid @option{-Wunused-variable} warning is
+using @code{x = x}, but that does not work with @option{-Wself-assign}.
+Use @code{(void) x} or @code{static_cast<void>(x)} instead.
+
@item -Wunused-value
@opindex Wunused-value
@opindex Wno-unused-value
Index: gcc/tree.h
===================================================================
--- gcc/tree.h (revision 159995)
+++ gcc/tree.h (working copy)
@@ -388,8 +388,9 @@ struct GTY(()) tree_base {
unsigned visited : 1;
unsigned packed_flag : 1;
unsigned user_align : 1;
+ unsigned expr_folded_flag : 1;
- unsigned spare : 13;
+ unsigned spare : 12;
/* This field is only used with type nodes; the only reason it is present
in tree_base instead of tree_type is to save space. The size of the
@@ -627,6 +628,13 @@ struct GTY(()) tree_common {
SSA_NAME_IS_DEFAULT_DEF in
SSA_NAME
+
+ expr_folded_flag:
+
+ EXPR_FOLDED in
+ all expressions
+ all decls
+ all constants
*/
#undef DEFTREESTRUCT
@@ -1362,6 +1370,10 @@ extern void omp_clause_range_check_faile
/* In fixed-point types, means a saturating type. */
#define TYPE_SATURATING(NODE) ((NODE)->base.saturating_flag)
+/* Nonzero in an expression, a decl, or a constant node if the node is
+ the result of a successful constant-folding. */
+#define EXPR_FOLDED(NODE) ((NODE)->base.expr_folded_flag)
+
/* These flags are available for each language front end to use internally. */
#define TREE_LANG_FLAG_0(NODE) ((NODE)->base.lang_flag_0)
#define TREE_LANG_FLAG_1(NODE) ((NODE)->base.lang_flag_1)
@@ -4920,7 +4932,9 @@ extern bool fold_deferring_overflow_warn
enum operand_equal_flag
{
OEP_ONLY_CONST = 1,
- OEP_PURE_SAME = 2
+ OEP_PURE_SAME = 2,
+ OEP_ALLOW_NULL = 4, /* Allow comparison of NULL operands. */
+ OEP_ALLOW_NULL_TYPE = 8 /* Allow comparison of operands without types. */
};
extern int operand_equal_p (const_tree, const_tree, unsigned int);
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c (revision 159995)
+++ gcc/fold-const.c (working copy)
@@ -2409,22 +2409,45 @@ combine_comparisons (location_t loc,
If OEP_PURE_SAME is set, then pure functions with identical arguments
are considered the same. It is used when the caller has other ways
- to ensure that global memory is unchanged in between. */
+ to ensure that global memory is unchanged in between.
+
+ If OEP_ALLOW_NULL is set, this routine will not crash on NULL operands,
+ and two NULL operands are considered equal. This flag is usually set
+ in the context of frontend when ARG0 and/or ARG1 may be NULL mostly due
+ to recursion on partially built expressions (e.g. a CAST_EXPR on a NULL
+ tree.) In this case, we certainly don't want the compiler to crash and
+ it's OK to consider two NULL operands equal. On the other hand, when
+ called in the context of code generation and optimization, if NULL
+ operands are not expected, silently ignoring them could be dangerous
+ and might cause problems downstream that are hard to find/debug. In that
+ case, the flag should probably not be set. */
int
operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
{
+ /* If either is NULL, they must be both NULL to be equal. We only do this
+ check when OEP_ALLOW_NULL is set. */
+ if ((flags & OEP_ALLOW_NULL) && (!arg0 || !arg1))
+ return arg0 == arg1;
+
+ /* Similar, if either does not have a type (like a released SSA name, or
+ an operand whose type depends on a template parameter), they aren't
+ equal, unless OEP_ALLOW_NULL_TYPE is set, in which case we will continue
+ to compare the operands only when both don't have a type. */
+ if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
+ {
+ if (((~flags) & OEP_ALLOW_NULL_TYPE)
+ || ((TREE_TYPE (arg0) && !TREE_TYPE (arg1))
+ || (!TREE_TYPE (arg0) && TREE_TYPE (arg1))))
+ return 0;
+ }
+
/* If either is ERROR_MARK, they aren't equal. */
if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
|| TREE_TYPE (arg0) == error_mark_node
|| TREE_TYPE (arg1) == error_mark_node)
return 0;
- /* Similar, if either does not have a type (like a released SSA name),
- they aren't equal. */
- if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
- return 0;
-
/* Check equality of integer constants before bailing out due to
precision differences. */
if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
@@ -2435,19 +2458,25 @@ operand_equal_p (const_tree arg0, const_
because they may change the signedness of the arguments. As pointers
strictly don't have a signedness, require either two pointers or
two non-pointers as well. */
- if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
- || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
+ if (TREE_TYPE (arg0)
+ && (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
+ || (POINTER_TYPE_P (TREE_TYPE (arg0))
+ != POINTER_TYPE_P (TREE_TYPE (arg1)))))
return 0;
/* We cannot consider pointers to different address space equal. */
- if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
- && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
- != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
+ if (TREE_TYPE (arg0)
+ && (POINTER_TYPE_P (TREE_TYPE (arg0))
+ && POINTER_TYPE_P (TREE_TYPE (arg1))
+ && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
+ != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1))))))
return 0;
/* If both types don't have the same precision, then it is not safe
to strip NOPs. */
- if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
+ if (TREE_TYPE (arg0)
+ && (TYPE_PRECISION (TREE_TYPE (arg0))
+ != TYPE_PRECISION (TREE_TYPE (arg1))))
return 0;
STRIP_NOPS (arg0);
@@ -2472,9 +2501,10 @@ operand_equal_p (const_tree arg0, const_
if (TREE_CODE (arg0) != TREE_CODE (arg1)
/* This is needed for conversions and for COMPONENT_REF.
Might as well play it safe and always test this. */
- || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
- || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
- || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
+ || (TREE_TYPE (arg0)
+ && (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
+ || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
+ || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))))
return 0;
/* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
@@ -2507,7 +2537,8 @@ operand_equal_p (const_tree arg0, const_
return 1;
- if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
+ if (TREE_TYPE (arg0)
+ && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
{
/* If we do not distinguish between signed and unsigned zero,
consider them equal. */
@@ -2575,8 +2606,9 @@ operand_equal_p (const_tree arg0, const_
{
CASE_CONVERT:
case FIX_TRUNC_EXPR:
- if (TYPE_UNSIGNED (TREE_TYPE (arg0))
- != TYPE_UNSIGNED (TREE_TYPE (arg1)))
+ if (TREE_TYPE (arg0)
+ && (TYPE_UNSIGNED (TREE_TYPE (arg0))
+ != TYPE_UNSIGNED (TREE_TYPE (arg1))))
return 0;
break;
default:
@@ -7653,8 +7685,8 @@ build_fold_addr_expr_loc (location_t loc
OP0. Return the folded expression if folding is successful.
Otherwise, return NULL_TREE. */
-tree
-fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
+static tree
+fold_unary_loc_1 (location_t loc, enum tree_code code, tree type, tree op0)
{
tree tem;
tree arg0;
@@ -8302,6 +8334,41 @@ fold_unary_loc (location_t loc, enum tre
} /* switch (code) */
}
+/* Given an expression tree EXP, set the EXPR_FOLDED flag, and if it is
+ a nop, recursively set the EXPR_FOLDED flag of its operand. */
+
+static void
+set_expr_folded_flag (tree exp)
+{
+ EXPR_FOLDED (exp) = 1;
+
+ /* If EXP is a nop (i.e. NON_LVALUE_EXPRs and NOP_EXPRs), we need to
+ recursively set the EXPR_FOLDED flag of its operand because the
+ expression will be stripped later. */
+ while ((CONVERT_EXPR_P (exp)
+ || TREE_CODE (exp) == NON_LVALUE_EXPR)
+ && TREE_OPERAND (exp, 0) != error_mark_node)
+ {
+ exp = TREE_OPERAND (exp, 0);
+ EXPR_FOLDED (exp) = 1;
+ }
+}
+
+/* Fold a unary expression of code CODE and type TYPE with operand
+ OP0. Return the folded expression if folding is successful.
+ Otherwise, return NULL_TREE.
+ This is a wrapper around fold_unary_loc_1 function (which does the
+ actual folding). Set the EXPR_FOLDED flag of the folded expression
+ if folding is successful. */
+
+tree
+fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
+{
+ tree tem = fold_unary_loc_1 (loc, code, type, op0);
+ if (tem)
+ set_expr_folded_flag (tem);
+ return tem;
+}
/* If the operation was a conversion do _not_ mark a resulting constant
with TREE_OVERFLOW if the original constant was not. These conversions
@@ -9394,8 +9461,8 @@ get_pointer_modulus_and_residue (tree ex
Return the folded expression if folding is successful. Otherwise,
return NULL_TREE. */
-tree
-fold_binary_loc (location_t loc,
+static tree
+fold_binary_loc_1 (location_t loc,
enum tree_code code, tree type, tree op0, tree op1)
{
enum tree_code_class kind = TREE_CODE_CLASS (code);
@@ -13066,6 +13133,24 @@ fold_binary_loc (location_t loc,
return tem;
}
+/* Fold a binary expression of code CODE and type TYPE with operands
+ OP0 and OP1. LOC is the location of the resulting expression.
+ Return the folded expression if folding is successful. Otherwise,
+ return NULL_TREE.
+ This is a wrapper around fold_binary_loc_1 function (which does the
+ actual folding). Set the EXPR_FOLDED flag of the folded expression
+ if folding is successful. */
+
+tree
+fold_binary_loc (location_t loc,
+ enum tree_code code, tree type, tree op0, tree op1)
+{
+ tree tem = fold_binary_loc_1 (loc, code, type, op0, op1);
+ if (tem)
+ set_expr_folded_flag (tem);
+ return tem;
+}
+
/* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
of GOTO_EXPR. */
@@ -13102,8 +13187,8 @@ contains_label_p (tree st)
OP0, OP1, and OP2. Return the folded expression if folding is
successful. Otherwise, return NULL_TREE. */
-tree
-fold_ternary_loc (location_t loc, enum tree_code code, tree type,
+static tree
+fold_ternary_loc_1 (location_t loc, enum tree_code code, tree type,
tree op0, tree op1, tree op2)
{
tree tem;
@@ -13438,6 +13523,23 @@ fold_ternary_loc (location_t loc, enum t
} /* switch (code) */
}
+/* Fold a ternary expression of code CODE and type TYPE with operands
+ OP0, OP1, and OP2. Return the folded expression if folding is
+ successful. Otherwise, return NULL_TREE.
+ This is a wrapper around fold_ternary_loc_1 function (which does the
+ actual folding). Set the EXPR_FOLDED flag of the folded expression
+ if folding is successful. */
+
+tree
+fold_ternary_loc (location_t loc, enum tree_code code, tree type,
+ tree op0, tree op1, tree op2)
+{
+ tree tem = fold_ternary_loc_1 (loc, code, type, op0, op1, op2);
+ if (tem)
+ set_expr_folded_flag (tem);
+ return tem;
+}
+
/* Perform constant folding and related simplification of EXPR.
The related simplifications include x*1 => x, x*0 => 0, etc.,
and application of the associative law.
Index: gcc/testsuite/gcc.dg/plugin/selfassign.c
===================================================================
--- gcc/testsuite/gcc.dg/plugin/selfassign.c (revision 159995)
+++ gcc/testsuite/gcc.dg/plugin/selfassign.c (working copy)
@@ -196,7 +196,7 @@ compare_and_warn (gimple stmt, tree lhs,
/* Check and warn if STMT is a self-assign statement. */
static void
-warn_self_assign (gimple stmt)
+check_self_assign (gimple stmt)
{
tree rhs, lhs;
@@ -251,7 +251,7 @@ execute_warn_self_assign (void)
FOR_EACH_BB (bb)
{
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
- warn_self_assign (gsi_stmt (gsi));
+ check_self_assign (gsi_stmt (gsi));
}
return 0;
Index: gcc/testsuite/gcc.dg/wself-assign-1.c
===================================================================
--- gcc/testsuite/gcc.dg/wself-assign-1.c (revision 0)
+++ gcc/testsuite/gcc.dg/wself-assign-1.c (revision 0)
@@ -0,0 +1,27 @@
+/* Test the self-assignemnt detection and warning. */
+/* { dg-do compile } */
+/* { dg-options "-Wself-assign" } */
+
+struct Bar {
+ int b_;
+ int c_;
+};
+
+int g;
+
+int main()
+{
+ struct Bar *bar;
+ int x = x; /* { dg-warning "assigned to itself" } */
+ static int y;
+ struct Bar b_array[5];
+
+ b_array[x+g].b_ = b_array[x+g].b_; /* { dg-warning "assigned to itself" } */
+ g = g; /* { dg-warning "assigned to itself" } */
+ y = y; /* { dg-warning "assigned to itself" } */
+ bar->b_ = bar->b_; /* { dg-warning "assigned to itself" } */
+ x += 0; /* should not warn */
+ y -= 0; /* should not warn */
+ x /= x; /* should not warn */
+ y *= y; /* should not warn */
+}
Index: gcc/testsuite/gcc.dg/wself-assign-2.c
===================================================================
--- gcc/testsuite/gcc.dg/wself-assign-2.c (revision 0)
+++ gcc/testsuite/gcc.dg/wself-assign-2.c (revision 0)
@@ -0,0 +1,24 @@
+/* Test how self-assignment detection handles constant-folding happening */
+/* when parsing the RHS or the initializer. */
+/* { dg-do compile } */
+/* { dg-options "-Wself-assign" } */
+
+struct Bar {
+ int b_;
+ float c_;
+};
+
+int g;
+
+int main()
+{
+ struct Bar *bar;
+ int x = x - 0; /* should not warn */
+ static int y;
+ struct Bar b_array[5];
+
+ b_array[x+g].b_ = b_array[x+g].b_ * 1; /* should no warn */
+ g = g + 0; /* should not warn */
+ y = y / 1; /* should not warn */
+ bar->b_ = bar->b_ - 0; /* should not warn */
+}
Index: gcc/testsuite/g++.dg/plugin/selfassign.c
===================================================================
--- gcc/testsuite/g++.dg/plugin/selfassign.c (revision 159995)
+++ gcc/testsuite/g++.dg/plugin/selfassign.c (working copy)
@@ -196,7 +196,7 @@ compare_and_warn (gimple stmt, tree lhs,
/* Check and warn if STMT is a self-assign statement. */
static void
-warn_self_assign (gimple stmt)
+check_self_assign (gimple stmt)
{
tree rhs, lhs;
@@ -251,7 +251,7 @@ execute_warn_self_assign (void)
FOR_EACH_BB (bb)
{
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
- warn_self_assign (gsi_stmt (gsi));
+ check_self_assign (gsi_stmt (gsi));
}
return 0;
Index: gcc/testsuite/g++.dg/warn/Wself-assign-1.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wself-assign-1.C (revision 0)
+++ gcc/testsuite/g++.dg/warn/Wself-assign-1.C (revision 0)
@@ -0,0 +1,54 @@
+// Test the self-assignemnt detection and warning.
+// { dg-do compile }
+// { dg-options "-Wself-assign" }
+
+class Foo {
+ private:
+ int a_;
+
+ public:
+ Foo() : a_(a_) {} // { dg-warning "assigned to itself" }
+
+ void setA(int a) {
+ a_ = a_; // { dg-warning "assigned to itself" }
+ }
+
+ void operator=(Foo& rhs) {
+ this->a_ = rhs.a_;
+ }
+};
+
+struct Bar {
+ int b_;
+ int c_;
+};
+
+int g = g; // { dg-warning "assigned to itself" }
+Foo foo = foo; // { dg-warning "assigned to itself" }
+
+int func()
+{
+ Bar *bar1, bar2;
+ Foo local_foo;
+ int x = x; // { dg-warning "assigned to itself" }
+ static int y = y; // { dg-warning "assigned to itself" }
+ float *f;
+ Bar bar_array[5];
+ char n;
+ int overflow;
+
+ *f = *f; // { dg-warning "assigned to itself" }
+ bar1->b_ = bar1->b_; // { dg-warning "assigned to itself" }
+ bar2.c_ = bar2.c_; // { dg-warning "assigned to itself" }
+ local_foo = local_foo; // { dg-warning "assigned to itself" }
+ foo = foo; // { dg-warning "assigned to itself" }
+ foo.setA(5);
+ bar_array[3].c_ = bar_array[3].c_; // { dg-warning "assigned to itself" }
+ bar_array[x+g].b_ = bar_array[x+g].b_; // { dg-warning "assigned to itself" }
+ y = x;
+ x = y;
+ x += 0; // should not warn
+ y -= 0; // should not warn
+ x /= x; // should not warn
+ y *= y; // should not warn
+}
Index: gcc/testsuite/g++.dg/warn/Wself-assign-2.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wself-assign-2.C (revision 0)
+++ gcc/testsuite/g++.dg/warn/Wself-assign-2.C (revision 0)
@@ -0,0 +1,31 @@
+// Test the handling of expressions that depend on template parameters in
+// self-assignemnt detection.
+// { dg-do compile }
+// { dg-options "-Wself-assign" }
+
+template<typename T>
+struct Bar {
+ T x;
+ Bar operator++(int) {
+ Bar tmp = *this;
+ ++x;
+ tmp = tmp; // { dg-warning "assigned to itself" }
+ return tmp;
+ }
+};
+
+template<typename T>
+T DoSomething(T y) {
+ T a[5], *p;
+ Bar<T> b;
+ b.x = b.x; // { dg-warning "assigned to itself" }
+ *p = *p; // { dg-warning "assigned to itself" }
+ a[2] = a[2]; // { dg-warning "assigned to itself" }
+ return *p;
+}
+
+main() {
+ Bar<int> bar;
+ bar++;
+ DoSomething(5);
+}
Index: gcc/testsuite/g++.dg/warn/Wself-assign-3.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wself-assign-3.C (revision 0)
+++ gcc/testsuite/g++.dg/warn/Wself-assign-3.C (revision 0)
@@ -0,0 +1,35 @@
+// Test how operands_equal_p handles a NULL operand.
+// { dg-do compile }
+// { dg-options "-Wself-assign" }
+
+#include <cstdio>
+
+namespace testing {
+
+class Foo {
+ int f;
+ public:
+ Foo() { printf("Construct Foo\n"); }
+};
+
+class Bar {
+ int b;
+ public:
+ Bar(int x) { printf("Construct Bar\n"); }
+
+ void operator=(const Foo& foo) {
+ printf("Assign Foo to Bar\n");
+ }
+};
+
+}
+
+template <class T>
+void func(T t) {
+ ::testing::Bar(1) = ::testing::Foo(); // used to trigger a segfault
+ ::testing::Foo() = ::testing::Foo(); // { dg-warning "assigned to itself" }
+}
+
+main() {
+ func(2);
+}
Index: gcc/testsuite/g++.dg/warn/Wself-assign-4.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wself-assign-4.C (revision 0)
+++ gcc/testsuite/g++.dg/warn/Wself-assign-4.C (revision 0)
@@ -0,0 +1,48 @@
+// Test how self-assignment detection handles constant-folding happening
+// when parsing the RHS or the initializer.
+// { dg-do compile }
+// { dg-options "-Wself-assign" }
+
+class Foo {
+ private:
+ int a_;
+
+ public:
+ Foo() : a_(a_+0) {} // should not warn
+
+ void setA(int a) {
+ a_ = a_ + 0; // should not warn
+ }
+
+ void operator=(Foo& rhs) {
+ this->a_ = rhs.a_;
+ }
+};
+
+struct Bar {
+ int b_;
+ float c_;
+};
+
+int g = g * 1; // should not warn
+
+int func()
+{
+ Bar *bar1, bar2;
+ Foo foo;
+ int x = x - 0; // should not warn
+ static int y = y / 1; // should not warn
+ float *f;
+ Bar bar_array[5];
+
+ *f = *f / 1; // should not warn
+ bar1->b_ = bar1->b_ * 1; // should not warn
+ bar2.c_ = bar2.c_ - 0; // should not warn
+ foo.setA(5);
+ bar_array[3].c_ = bar_array[3].c_ * 1; // should not warn
+ bar_array[x+g].b_ = bar_array[x+g].b_ / 1; // should not warn
+ x += 0;
+ y -= 0;
+ foo = foo; // { dg-warning "assigned to itself" }
+ foo.operator=(foo); // should not warn
+}
Index: gcc/cp/init.c
===================================================================
--- gcc/cp/init.c (revision 159995)
+++ gcc/cp/init.c (working copy)
@@ -529,8 +529,14 @@ perform_member_init (tree member, tree i
init = build_x_compound_expr_from_list (init, "member initializer");
if (init)
- finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
- tf_warning_or_error));
+ {
+ finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
+ tf_warning_or_error));
+ /* Check for and warn about self-initialization if -Wself-assign is
+ enabled. */
+ if (warn_self_assign)
+ check_for_self_assign (input_location, decl, init);
+ }
}
if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c (revision 159995)
+++ gcc/cp/parser.c (working copy)
@@ -6858,6 +6858,12 @@ cp_parser_assignment_expression (cp_pars
if (cp_parser_non_integral_constant_expression (parser,
NIC_ASSIGNMENT))
return error_mark_node;
+
+ /* Check for and warn about self-assignment if -Wself-assign is
+ enabled and the assignment operator is "=". */
+ if (warn_self_assign && assignment_operator == NOP_EXPR)
+ check_for_self_assign (input_location, expr, rhs);
+
/* Build the assignment expression. */
expr = build_x_modify_expr (expr,
assignment_operator,
@@ -14030,6 +14036,10 @@ cp_parser_init_declarator (cp_parser* pa
`explicit' constructor cannot be used. */
((is_direct_init || !is_initialized)
? 0 : LOOKUP_ONLYCONVERTING));
+ /* Check for and warn about self-initialization if -Wself-assign is
+ enabled. */
+ if (warn_self_assign && initializer)
+ check_for_self_assign (input_location, decl, initializer);
}
else if ((cxx_dialect != cxx98) && friend_p
&& decl && TREE_CODE (decl) == FUNCTION_DECL)
Index: gcc/common.opt
===================================================================
--- gcc/common.opt (revision 159995)
+++ gcc/common.opt (working copy)
@@ -160,6 +160,10 @@ Wpadded
Common Var(warn_padded) Warning
Warn when padding is required to align structure members
+Wself-assign
+Common Var(warn_self_assign) Init(0) Warning
+Warn when a variable is assigned to itself
+
Wshadow
Common Var(warn_shadow) Warning
Warn when one local variable shadows another
Index: gcc/c-common.c
===================================================================
--- gcc/c-common.c (revision 159995)
+++ gcc/c-common.c (working copy)
@@ -9486,4 +9486,21 @@ make_tree_vector_copy (const VEC(tree,gc
return ret;
}
+/* Check for and warn about self-assignment or self-initialization.
+ LHS and RHS are the tree nodes for the left-hand side and right-hand side
+ of the assignment or initialization we are checking.
+ LOCATION is the source location for RHS. */
+
+void
+check_for_self_assign (location_t location, tree lhs, tree rhs)
+{
+ /* Only emit a warning if RHS is not a folded expression so that we don't
+ warn on something like x = x / 1. */
+ if (!EXPR_FOLDED (rhs)
+ && operand_equal_p (lhs, rhs,
+ OEP_PURE_SAME | OEP_ALLOW_NULL | OEP_ALLOW_NULL_TYPE))
+ warning_at (location, OPT_Wself_assign, G_("%qE is assigned to itself"),
+ lhs);
+}
+
#include "gt-c-common.h"
Index: gcc/c-common.h
===================================================================
--- gcc/c-common.h (revision 159995)
+++ gcc/c-common.h (working copy)
@@ -1060,6 +1060,7 @@ extern VEC(tree,gc) *make_tree_vector (v
extern void release_tree_vector (VEC(tree,gc) *);
extern VEC(tree,gc) *make_tree_vector_single (tree);
extern VEC(tree,gc) *make_tree_vector_copy (const VEC(tree,gc) *);
+extern void check_for_self_assign (location_t, tree, tree);
/* In c-gimplify.c */
extern void c_genericize (tree);
Index: gcc/c-parser.c
===================================================================
--- gcc/c-parser.c (revision 159995)
+++ gcc/c-parser.c (working copy)
@@ -1297,6 +1297,11 @@ c_parser_declaration_or_fndef (c_parser
maybe_warn_string_init (TREE_TYPE (d), init);
finish_decl (d, init_loc, init.value,
init.original_type, asm_name);
+ /* Check for and warn about self-initialization if
+ -Wself-assign is enabled. Don't warn if there is
+ already an error for the initializer. */
+ if (warn_self_assign && DECL_INITIAL (d) != error_mark_node)
+ check_for_self_assign (here, d, init.value);
}
}
else
@@ -4767,7 +4772,13 @@ c_parser_expr_no_commas (c_parser *parse
code, exp_location, rhs.value,
rhs.original_type);
if (code == NOP_EXPR)
- ret.original_code = MODIFY_EXPR;
+ {
+ ret.original_code = MODIFY_EXPR;
+ /* Check for and warn about self-assignment if -Wself-assign is
+ enabled. */
+ if (warn_self_assign)
+ check_for_self_assign (op_location, lhs.value, rhs.value);
+ }
else
{
TREE_NO_WARNING (ret.value) = 1;