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

* Re: [PATCH] Add macro DISABLE_COPY_AND_ASSIGN
  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
  0 siblings, 1 reply; 7+ messages in thread
From: Yao Qi @ 2017-07-26  8:55 UTC (permalink / raw)
  To: gcc-patches

On 17-07-19 10:30:45, Yao Qi wrote:
> 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.

Ping.
https://gcc.gnu.org/ml/gcc-patches/2017-07/msg01134.html

-- 
Yao (齐尧)

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

* Re: [PATCH] Add macro DISABLE_COPY_AND_ASSIGN
  2017-07-26  8:55 ` Yao Qi
@ 2017-08-02 11:19   ` Yao Qi
  2017-08-11 15:38     ` Pedro Alves
  0 siblings, 1 reply; 7+ messages in thread
From: Yao Qi @ 2017-08-02 11:19 UTC (permalink / raw)
  To: gcc-patches

On Wed, Jul 26, 2017 at 9:55 AM, Yao Qi <qiyaoltc@gmail.com> wrote:
> On 17-07-19 10:30:45, Yao Qi wrote:
>> 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.
>
> Ping.
> https://gcc.gnu.org/ml/gcc-patches/2017-07/msg01134.html
>

Ping.  It is a quite straightforward patch, can any one
take a look?

-- 
Yao (齐尧)

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

* Re: [PATCH] Add macro DISABLE_COPY_AND_ASSIGN
  2017-08-02 11:19   ` Yao Qi
@ 2017-08-11 15:38     ` Pedro Alves
  2017-09-09  9:45       ` Yao Qi
  0 siblings, 1 reply; 7+ messages in thread
From: Pedro Alves @ 2017-08-11 15:38 UTC (permalink / raw)
  To: Yao Qi, gcc-patches, Ian Lance Taylor

On 08/02/2017 12:19 PM, Yao Qi wrote:
> On Wed, Jul 26, 2017 at 9:55 AM, Yao Qi <qiyaoltc@gmail.com> wrote:
>> On 17-07-19 10:30:45, Yao Qi wrote:
>>> 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.
>>
>> Ping.
>> https://gcc.gnu.org/ml/gcc-patches/2017-07/msg01134.html
>>
> 
> Ping.  It is a quite straightforward patch, can any one
> take a look?
> 

Yeah, this is a macro that lots of projects out there reinvent,
can't imagine it being very controversial.

I could have used this today in another spot in gdb.

The patch as is touches areas with different maintainers, it
may have fallen victim of diffusion of responsibility.

Could we get at least the ansidecl.h change in, so we can
start using it in gdb?  CCing Ian as a libiberty maintainer.

Thanks,
Pedro Alves

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

* Re: [PATCH] Add macro DISABLE_COPY_AND_ASSIGN
  2017-08-11 15:38     ` Pedro Alves
@ 2017-09-09  9:45       ` Yao Qi
  2017-09-09 12:27         ` Ian Lance Taylor
  0 siblings, 1 reply; 7+ messages in thread
From: Yao Qi @ 2017-09-09  9:45 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gcc-patches, Ian Lance Taylor

On Fri, Aug 11, 2017 at 3:14 PM, Pedro Alves
> Yeah, this is a macro that lots of projects out there reinvent,
> can't imagine it being very controversial.
>
> I could have used this today in another spot in gdb.
>
> The patch as is touches areas with different maintainers, it
> may have fallen victim of diffusion of responsibility.
>
> Could we get at least the ansidecl.h change in, so we can
> start using it in gdb?  CCing Ian as a libiberty maintainer.

Hi Ian,
I just talked with you about this patch.  You are cc'ed.  Could you
take a look at the change in include/ansidecl.h?  Then, we can use it
in different
projects, gcc, gdb and gold.

-- 
Yao (齐尧)

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

* Re: [PATCH] Add macro DISABLE_COPY_AND_ASSIGN
  2017-09-09  9:45       ` Yao Qi
@ 2017-09-09 12:27         ` Ian Lance Taylor
  2017-09-15 15:44           ` Yao Qi
  0 siblings, 1 reply; 7+ messages in thread
From: Ian Lance Taylor @ 2017-09-09 12:27 UTC (permalink / raw)
  To: Yao Qi; +Cc: Pedro Alves, gcc-patches

On Sat, Sep 9, 2017 at 2:45 AM, Yao Qi <qiyaoltc@gmail.com> wrote:
> On Fri, Aug 11, 2017 at 3:14 PM, Pedro Alves
>> Yeah, this is a macro that lots of projects out there reinvent,
>> can't imagine it being very controversial.
>>
>> I could have used this today in another spot in gdb.
>>
>> The patch as is touches areas with different maintainers, it
>> may have fallen victim of diffusion of responsibility.
>>
>> Could we get at least the ansidecl.h change in, so we can
>> start using it in gdb?  CCing Ian as a libiberty maintainer.
>
> Hi Ian,
> I just talked with you about this patch.  You are cc'ed.  Could you
> take a look at the change in include/ansidecl.h?  Then, we can use it
> in different
> projects, gcc, gdb and gold.

The patch to include/ansidecl.h is basically OK.  Please add an
example in the comment showing how to use it: after `private:`, and
with a trailing semicolon.  Thanks.

The patches to the other files will have to be approved by the
relevant maintainers.

Ian

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

* Re: [PATCH] Add macro DISABLE_COPY_AND_ASSIGN
  2017-09-09 12:27         ` Ian Lance Taylor
@ 2017-09-15 15:44           ` Yao Qi
  0 siblings, 0 replies; 7+ messages in thread
From: Yao Qi @ 2017-09-15 15:44 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Pedro Alves, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 466 bytes --]

On Sat, Sep 9, 2017 at 1:27 PM, Ian Lance Taylor <iant@golang.org> wrote:
>
> The patch to include/ansidecl.h is basically OK.  Please add an
> example in the comment showing how to use it: after `private:`, and
> with a trailing semicolon.  Thanks.

Patch below is committed.  Thanks for the review.

>
> The patches to the other files will have to be approved by the
> relevant maintainers.
>

I'll split it and post them later.

-- 
Yao (齐尧)

[-- Attachment #2: 0001-include-Add-macro-DISABLE_COPY_AND_ASSIGN.patch --]
[-- Type: text/x-patch, Size: 2628 bytes --]

From 753d12319d85876c2513029037af539c43717251 Mon Sep 17 00:00:00 2001
From: qiyao <qiyao@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Fri, 15 Sep 2017 15:40:50 +0000
Subject: [PATCH] [include] Add macro DISABLE_COPY_AND_ASSIGN

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.

include:

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

	* ansidecl.h (DISABLE_COPY_AND_ASSIGN): New macro.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@252823 138bc75d-0d04-0410-961f-82ee72b054a4
---
 include/ChangeLog  |  5 +++++
 include/ansidecl.h | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/include/ChangeLog b/include/ChangeLog
index 4703588..0221586 100644
--- a/include/ChangeLog
+++ b/include/ChangeLog
@@ -1,3 +1,8 @@
+2017-09-15  Yao Qi  <yao.qi@linaro.org>
+	    Pedro Alves  <palves@redhat.com>
+
+	* ansidecl.h (DISABLE_COPY_AND_ASSIGN): New macro.
+
 2017-09-12  Jiong Wang  <jiong.wang@arm.com>
 
 	* dwarf2.def (DW_CFA_AARCH64_negate_ra_state): New DW_CFA_DUP.
diff --git a/include/ansidecl.h b/include/ansidecl.h
index ab3b895..450ce35 100644
--- a/include/ansidecl.h
+++ b/include/ansidecl.h
@@ -360,6 +360,32 @@ 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, like this,
+
+   class name_lookup
+   {
+     private:
+       DISABLE_COPY_AND_ASSIGN (name_lookup);
+   };
+
+   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).