From: Marek Polacek <polacek@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH v3] c++: Implement -Wself-move warning [PR81159]
Date: Fri, 19 Aug 2022 18:34:26 -0400 [thread overview]
Message-ID: <YwAP8nAiN7lYsYoJ@redhat.com> (raw)
In-Reply-To: <f40d374a-cba0-c06b-5586-1f9f8785eabd@redhat.com>
On Thu, Aug 18, 2022 at 08:33:47PM -0400, Jason Merrill wrote:
> On 8/18/22 13:19, Marek Polacek wrote:
> > On Mon, Aug 15, 2022 at 03:54:05PM -0400, Jason Merrill wrote:
> > > On 8/9/22 09:37, Marek Polacek wrote:
> > > > + /* We're looking for *std::move<T&> ((T &) &arg), or
> > > > + *std::move<T&> ((T &) (T *) r) if the argument it a reference. */
> > > > + if (!REFERENCE_REF_P (rhs)
> > > > + || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
> > > > + return;
> > > > + tree fn = TREE_OPERAND (rhs, 0);
> > > > + if (!is_std_move_p (fn))
> > > > + return;
> > > > + tree arg = CALL_EXPR_ARG (fn, 0);
> > > > + if (TREE_CODE (arg) != NOP_EXPR)
> > > > + return;
> > > > + /* Strip the (T &). */
> > > > + arg = TREE_OPERAND (arg, 0);
> > > > + /* Strip the (T *) or &. */
> > > > + arg = TREE_OPERAND (arg, 0);
> > >
> > > Are you sure these are the only two expressions that can make it here? What
> > > if the argument to move is *Tptr?
> >
> > Not 100% sure but I couldn't find any other form. For *Tptr we get
> > *std::move<int*&> ((int * &) &Tptr)
>
> That likes like what you'd get when the argument is Tptr, not when it's
> *Tptr. And indeed that's what I see in the testcase:
>
> > + Tptr = std::move (Tptr); // { dg-warning "moving a variable to itself" }
>
> is missing the *
Duh, sorry. The previous patch didn't handle the *Tptr case. Further poking
revealed that we need special care to handle (*(Tptr)) and **Tptr etc. So in
this patch I'm stripping all *s and V_C_Es. Sigh.
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
-- >8 --
About 5 years ago we got a request to implement -Wself-move, which
warns about useless moves like this:
int x;
x = std::move (x);
This patch implements that warning.
PR c++/81159
gcc/c-family/ChangeLog:
* c.opt (Wself-move): New option.
gcc/cp/ChangeLog:
* typeck.cc (maybe_warn_self_move): New.
(cp_build_modify_expr): Call maybe_warn_self_move.
gcc/ChangeLog:
* doc/invoke.texi: Document -Wself-move.
gcc/testsuite/ChangeLog:
* g++.dg/warn/Wself-move1.C: New test.
---
gcc/c-family/c.opt | 4 +
gcc/cp/typeck.cc | 51 +++++++++-
gcc/doc/invoke.texi | 23 ++++-
gcc/testsuite/g++.dg/warn/Wself-move1.C | 122 ++++++++++++++++++++++++
4 files changed, 198 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/warn/Wself-move1.C
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index dfdebd596ef..f776efd39d8 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1229,6 +1229,10 @@ Wselector
ObjC ObjC++ Var(warn_selector) Warning
Warn if a selector has multiple methods.
+Wself-move
+C++ ObjC++ Var(warn_self_move) Warning LangEnabledBy(C++ ObjC++, Wall)
+Warn when a value is moved to itself with std::move.
+
Wsequence-point
C ObjC C++ ObjC++ Var(warn_sequence_point) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
Warn about possible violations of sequence point rules.
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 992ebfd99fb..b2bd13db1b6 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -8897,7 +8897,54 @@ cp_build_c_cast (location_t loc, tree type, tree expr,
return error_mark_node;
}
-\f
+
+/* Warn when a value is moved to itself with std::move. LHS is the target,
+ RHS may be the std::move call, and LOC is the location of the whole
+ assignment. */
+
+static void
+maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
+{
+ if (!warn_self_move)
+ return;
+
+ /* C++98 doesn't know move. */
+ if (cxx_dialect < cxx11)
+ return;
+
+ if (processing_template_decl)
+ return;
+
+ if (!REFERENCE_REF_P (rhs)
+ || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
+ return;
+ tree fn = TREE_OPERAND (rhs, 0);
+ if (!is_std_move_p (fn))
+ return;
+
+ /* Just a little helper to strip * and various NOPs. */
+ auto extract_op = [] (tree &op) {
+ STRIP_NOPS (op);
+ while (INDIRECT_REF_P (op))
+ op = TREE_OPERAND (op, 0);
+ op = maybe_undo_parenthesized_ref (op);
+ STRIP_ANY_LOCATION_WRAPPER (op);
+ };
+
+ tree arg = CALL_EXPR_ARG (fn, 0);
+ extract_op (arg);
+ if (TREE_CODE (arg) == ADDR_EXPR)
+ arg = TREE_OPERAND (arg, 0);
+ extract_op (lhs);
+
+ if (cp_tree_equal (lhs, arg))
+ {
+ auto_diagnostic_group d;
+ if (warning_at (loc, OPT_Wself_move, "moving a variable to itself"))
+ inform (loc, "remove %<std::move%> call");
+ }
+}
+
/* For use from the C common bits. */
tree
build_modify_expr (location_t location,
@@ -9101,6 +9148,8 @@ cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
if (modifycode == NOP_EXPR)
{
+ maybe_warn_self_move (loc, lhs, rhs);
+
if (c_dialect_objc ())
{
result = objc_maybe_build_modify_expr (lhs, rhs);
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index f65d351a5fc..5dea3fee124 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -264,7 +264,7 @@ in the following sections.
-Wreorder -Wregister @gol
-Wstrict-null-sentinel -Wno-subobject-linkage -Wtemplates @gol
-Wno-non-template-friend -Wold-style-cast @gol
--Woverloaded-virtual -Wno-pmf-conversions -Wsign-promo @gol
+-Woverloaded-virtual -Wno-pmf-conversions -Wself-move -Wsign-promo @gol
-Wsized-deallocation -Wsuggest-final-methods @gol
-Wsuggest-final-types -Wsuggest-override @gol
-Wno-terminate -Wuseless-cast -Wno-vexing-parse @gol
@@ -5843,6 +5843,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}.
-Wreorder @gol
-Wrestrict @gol
-Wreturn-type @gol
+-Wself-move @r{(only for C++)} @gol
-Wsequence-point @gol
-Wsign-compare @r{(only in C++)} @gol
-Wsizeof-array-div @gol
@@ -6828,6 +6829,26 @@ of a declaration:
This warning is enabled by @option{-Wall}.
+@item -Wno-self-move @r{(C++ and Objective-C++ only)}
+@opindex Wself-move
+@opindex Wno-self-move
+This warning warns when a value is moved to itself with @code{std::move}.
+Such a @code{std::move} typically has no effect.
+
+@smallexample
+struct T @{
+@dots{}
+@};
+void fn()
+@{
+ T t;
+ @dots{}
+ t = std::move (t);
+@}
+@end smallexample
+
+This warning is enabled by @option{-Wall}.
+
@item -Wsequence-point
@opindex Wsequence-point
@opindex Wno-sequence-point
diff --git a/gcc/testsuite/g++.dg/warn/Wself-move1.C b/gcc/testsuite/g++.dg/warn/Wself-move1.C
new file mode 100644
index 00000000000..601d8c1521a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wself-move1.C
@@ -0,0 +1,122 @@
+// PR c++/81159
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wself-move" }
+
+// Define std::move.
+namespace std {
+ template<typename _Tp>
+ struct remove_reference
+ { typedef _Tp type; };
+
+ template<typename _Tp>
+ struct remove_reference<_Tp&>
+ { typedef _Tp type; };
+
+ template<typename _Tp>
+ struct remove_reference<_Tp&&>
+ { typedef _Tp type; };
+
+ template<typename _Tp>
+ constexpr typename std::remove_reference<_Tp>::type&&
+ move(_Tp&& __t) noexcept
+ { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
+}
+
+int g;
+
+struct S {
+ int x;
+ S(S&& o) {
+ x = std::move (x); // { dg-warning "moving a variable to itself" }
+ x = std::move (o.x);
+ o.x = std::move (x);
+ o.x = std::move (o.x); // { dg-warning "moving a variable to itself" }
+ }
+ void foo (int x) {
+ x = std::move (x); // { dg-warning "moving a variable to itself" }
+ }
+};
+
+struct X {
+ int x;
+ X(int x) : x(std::move (x)) { }
+};
+
+struct A {};
+struct B { A a; };
+struct C { C(); ~C(); };
+struct D { D(); D(const D&); D(D&&); D& operator=(const D&); };
+
+void
+test ()
+{
+ int i = 42;
+ i = std::move (i); // { dg-warning "moving a variable to itself" }
+ (i) = std::move (i); // { dg-warning "moving a variable to itself" }
+
+ g = std::move (g); // { dg-warning "moving a variable to itself" }
+ (g) = std::move (g); // { dg-warning "moving a variable to itself" }
+
+ A a;
+ a = std::move (a); // { dg-warning "moving a variable to itself" }
+
+ B b;
+ b = std::move (b); // { dg-warning "moving a variable to itself" }
+ b.a = std::move (b.a); // { dg-warning "moving a variable to itself" }
+
+ C c;
+ c = std::move (c); // { dg-warning "moving a variable to itself" }
+ D d;
+ d = std::move (d); // { dg-warning "moving a variable to itself" }
+}
+
+template<typename T>
+void ttest ()
+{
+ T t;
+ t = std::move (t); // { dg-warning "moving a variable to itself" }
+}
+
+template void ttest<A>();
+
+void
+testref (int &r, int &&rr)
+{
+ r = std::move (r); // { dg-warning "moving a variable to itself" }
+ rr = std::move (rr); // { dg-warning "moving a variable to itself" }
+}
+
+// Test various other arguments to std::move.
+template<typename T>
+void
+testargs (T *Tptr, T **Tpptr, T& Tref, T&& Trref, const T *Tcptr)
+{
+ Tptr = std::move (Tptr); // { dg-warning "moving a variable to itself" }
+ *Tptr = std::move (*Tptr); // { dg-warning "moving a variable to itself" }
+ *Tptr = std::move (*(Tptr)); // { dg-warning "moving a variable to itself" }
+ *(Tptr) = std::move (*Tptr); // { dg-warning "moving a variable to itself" }
+ *(Tptr + 1) = std::move (*Tptr + 1);
+ *(Tptr + 1) = std::move (*Tptr + 2);
+ (*(Tptr)) = std::move (*Tptr); // { dg-warning "moving a variable to itself" }
+ *Tpptr = std::move (*Tpptr); // { dg-warning "moving a variable to itself" }
+ **Tpptr = std::move (**Tpptr); // { dg-warning "moving a variable to itself" }
+ Tref = std::move (Tref); // { dg-warning "moving a variable to itself" }
+ Trref = std::move (Trref); // { dg-warning "moving a variable to itself" }
+ Tcptr = std::move (Tcptr); // { dg-warning "moving a variable to itself" }
+ (Tptr) = std::move (Tptr); // { dg-warning "moving a variable to itself" }
+ (*Tptr) = std::move (*Tptr); // { dg-warning "moving a variable to itself" }
+ (*Tpptr) = std::move (*Tpptr); // { dg-warning "moving a variable to itself" }
+ (**Tpptr) = std::move (**Tpptr); // { dg-warning "moving a variable to itself" }
+ (*(*(Tpptr))) = std::move (**Tpptr); // { dg-warning "moving a variable to itself" }
+ (Tref) = std::move (Tref); // { dg-warning "moving a variable to itself" }
+ (Trref) = std::move (Trref); // { dg-warning "moving a variable to itself" }
+ (Tcptr) = std::move (Tcptr); // { dg-warning "moving a variable to itself" }
+}
+
+void
+call_testargs ()
+{
+ int i = 42;
+ int *p = &i;
+ testargs<int>(&i, &p, i, 42, &i);
+}
base-commit: 713ec97e593bd4d9915a13bc4047f064fec0e24a
--
2.37.2
next prev parent reply other threads:[~2022-08-19 22:34 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-09 16:37 [PATCH] " Marek Polacek
2022-08-15 19:54 ` Jason Merrill
2022-08-18 20:19 ` [PATCH v2] " Marek Polacek
2022-08-19 0:33 ` Jason Merrill
2022-08-19 22:34 ` Marek Polacek [this message]
2022-08-20 21:31 ` [PATCH v3] " Jason Merrill
2022-08-23 16:39 ` [PATCH v4] " Marek Polacek
2022-08-23 21:27 ` Jason Merrill
2022-08-24 21:30 ` Marek Polacek
2022-08-25 13:25 ` Jason Merrill
2022-08-25 21:49 ` Marek Polacek
2022-08-26 0:52 ` Jason Merrill
2022-08-26 17:04 ` [PATCH v5] " Marek Polacek
2022-08-26 17:59 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YwAP8nAiN7lYsYoJ@redhat.com \
--to=polacek@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).