public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Add macro DISABLE_COPY_AND_ASSIGN
@ 2017-07-19  9:31 Yao Qi
  2017-07-26  8:55 ` Yao Qi
  0 siblings, 1 reply; 7+ messages in thread
From: Yao Qi @ 2017-07-19  9:31 UTC (permalink / raw)
  To: gcc-patches; +Cc: palves

We have many classes that copy cotr and assignment operator are deleted
in different projects, gcc, gdb and gold.  So this patch adds a macro
to do this, and replace these existing mechanical code with macro
DISABLE_COPY_AND_ASSIGN.

The patch was posted in gdb-patches,
https://sourceware.org/ml/gdb-patches/2017-07/msg00254.html but we
think it is better to put this macro in include/ansidecl.h so that
other projects can use it too.

Boostrapped on x86_64-linux-gnu.  Is it OK?

include:

2017-07-19  Yao Qi  <yao.qi@linaro.org>
	    Pedro Alves  <palves@redhat.com>

	* ansidecl.h (DISABLE_COPY_AND_ASSIGN): New macro.

gcc/cp:

2017-07-19  Yao Qi  <yao.qi@linaro.org>

	* cp-tree.h (class ovl_iterator): Use DISABLE_COPY_AND_ASSIGN.
	* name-lookup.c (struct name_loopup): Likewise.

gcc:

2017-07-19  Yao Qi  <yao.qi@linaro.org>

	* tree-ssa-scopedtables.h (class avail_exprs_stack): Use
	DISABLE_COPY_AND_ASSIGN.
	(class const_and_copies): Likewise.
---
 gcc/cp/cp-tree.h            |  4 +---
 gcc/cp/name-lookup.c        |  3 +--
 gcc/tree-ssa-scopedtables.h |  6 ++----
 include/ansidecl.h          | 19 +++++++++++++++++++
 4 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index abc9b65..9a45680 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -718,9 +718,7 @@ class ovl_iterator
   }
 
  private:
-  /* Do not duplicate.  */
-  ovl_iterator &operator= (const ovl_iterator &);
-  ovl_iterator (const ovl_iterator &);
+  DISABLE_COPY_AND_ASSIGN (ovl_iterator);
 
  public:
   operator bool () const
diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index cd7428a..f80958d 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -194,8 +194,7 @@ public:
   }
 
 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
-  name_lookup (const name_lookup &);
-  name_lookup &operator= (const name_lookup &);
+  DISABLE_COPY_AND_ASSIGN (name_lookup);
 
 protected:
   static bool seen_p (tree scope)
diff --git a/gcc/tree-ssa-scopedtables.h b/gcc/tree-ssa-scopedtables.h
index df304ae..692da8a 100644
--- a/gcc/tree-ssa-scopedtables.h
+++ b/gcc/tree-ssa-scopedtables.h
@@ -158,8 +158,7 @@ class avail_exprs_stack
 
   /* We do not allow copying this object or initializing one
      from another.  */
-  avail_exprs_stack& operator= (const avail_exprs_stack&);
-  avail_exprs_stack (class avail_exprs_stack &);
+  DISABLE_COPY_AND_ASSIGN (avail_exprs_stack);
 };
 
 /* This class defines an unwindable const/copy equivalence table
@@ -197,8 +196,7 @@ class const_and_copies
 
  private:
   vec<tree> m_stack;
-  const_and_copies& operator= (const const_and_copies&);
-  const_and_copies (class const_and_copies &);
+  DISABLE_COPY_AND_ASSIGN (const_and_copies);
 };
 
 void initialize_expr_from_cond (tree cond, struct hashable_expr *expr);
diff --git a/include/ansidecl.h b/include/ansidecl.h
index f6e1761..796f744 100644
--- a/include/ansidecl.h
+++ b/include/ansidecl.h
@@ -354,6 +354,25 @@ So instead we use the macro below and test it against specific values.  */
 # define FINAL
 #endif
 
+/* A macro to disable the copy constructor and assignment operator.
+   When building with C++11 and above, the methods are explicitly
+   deleted, causing a compile-time error if something tries to copy.
+   For C++03, this just declares the methods, causing a link-time
+   error if the methods end up called (assuming you don't
+   define them).  For C++03, for best results, place the macro
+   under the private: access specifier, so that most attempts at
+   copy are caught at compile-time.  */
+
+#if __cplusplus >= 201103
+#define DISABLE_COPY_AND_ASSIGN(TYPE)		\
+  TYPE (const TYPE&) = delete;			\
+  void operator= (const TYPE &) = delete
+  #else
+#define DISABLE_COPY_AND_ASSIGN(TYPE)		\
+  TYPE (const TYPE&);				\
+  void operator= (const TYPE &)
+#endif /* __cplusplus >= 201103 */
+
 #ifdef __cplusplus
 }
 #endif
-- 
1.9.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-09-15 15:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-19  9:31 [PATCH] Add macro DISABLE_COPY_AND_ASSIGN Yao Qi
2017-07-26  8:55 ` Yao Qi
2017-08-02 11:19   ` Yao Qi
2017-08-11 15:38     ` Pedro Alves
2017-09-09  9:45       ` Yao Qi
2017-09-09 12:27         ` Ian Lance Taylor
2017-09-15 15:44           ` Yao Qi

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).