From 03dd2ec39d144530ef9dd3b3a0719f24435c50f3 Mon Sep 17 00:00:00 2001 From: Seth R Johnson Date: Thu, 14 Feb 2019 07:18:33 -0500 Subject: [PATCH] Auto-detect non-assignable classes Classes with references or const data are now marked as 'noassign'. This renders many explicit `private: operator=` declarations redundant. --- Examples/test-suite/apply_signed_char.i | 2 -- Examples/test-suite/bools.i | 2 -- Examples/test-suite/constant_pointers.i | 6 ------ Examples/test-suite/cpp_basic.i | 2 ++ Examples/test-suite/enum_thorough.i | 2 -- Examples/test-suite/java_typemaps_proxy.i | 2 -- Source/Modules/allocate.cxx | 9 +++++++++ 7 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Examples/test-suite/apply_signed_char.i b/Examples/test-suite/apply_signed_char.i index d3116f024..a33b285a3 100644 --- a/Examples/test-suite/apply_signed_char.i +++ b/Examples/test-suite/apply_signed_char.i @@ -35,7 +35,5 @@ const char memberconstchar; virtual ~DirectorTest() {} - private: - DirectorTest& operator=(const DirectorTest &); }; %} diff --git a/Examples/test-suite/bools.i b/Examples/test-suite/bools.i index 2ef3d93a6..0c4f9d661 100644 --- a/Examples/test-suite/bools.i +++ b/Examples/test-suite/bools.i @@ -62,8 +62,6 @@ struct BoolStructure { m_rbool(m_bool2), m_const_pbool(m_pbool), m_const_rbool(m_rbool) {} -private: - BoolStructure& operator=(const BoolStructure &); }; %} diff --git a/Examples/test-suite/constant_pointers.i b/Examples/test-suite/constant_pointers.i index 9094e9dea..1e03b29ca 100644 --- a/Examples/test-suite/constant_pointers.i +++ b/Examples/test-suite/constant_pointers.i @@ -53,8 +53,6 @@ public: int* array_member1[ARRAY_SIZE]; ParametersTest* array_member2[ARRAY_SIZE]; MemberVariablesTest() : member3(NULL), member4(NULL) {} -private: - MemberVariablesTest& operator=(const MemberVariablesTest&); }; void foofunction(const int *const i) {} @@ -80,8 +78,6 @@ public: void ret8(int*const& a) {} int*const& ret9() {return GlobalIntPtr;} ReturnValuesTest() : int3(NULL) {} -private: - ReturnValuesTest& operator=(const ReturnValuesTest&); }; const int* globalRet1() {return &GlobalInt;} @@ -113,8 +109,6 @@ int* const globalRet2() {return &GlobalInt;} A* ap; const A* cap; Acptr acptr; - private: - B& operator=(const B&); }; const B* bar(const B* b) { diff --git a/Examples/test-suite/cpp_basic.i b/Examples/test-suite/cpp_basic.i index f2537e109..365873eda 100644 --- a/Examples/test-suite/cpp_basic.i +++ b/Examples/test-suite/cpp_basic.i @@ -74,8 +74,10 @@ class Bar { Foo *testFoo(int a, Foo *f) { return new Foo(2 * a + (f ? f->num : 0) + fval.num); } +/* Const member data means this class can't be assigned. private: Bar& operator=(const Bar&); +*/ }; %} diff --git a/Examples/test-suite/enum_thorough.i b/Examples/test-suite/enum_thorough.i index 3beefccc0..b8eb3df0d 100644 --- a/Examples/test-suite/enum_thorough.i +++ b/Examples/test-suite/enum_thorough.i @@ -89,8 +89,6 @@ struct SpeedClass { const colour myColour2; speedtd1 mySpeedtd1; SpeedClass() : myColour2(red), mySpeedtd1(slow) { } -private: - SpeedClass& operator=(const SpeedClass&); }; int speedTest0(int s) { return s; } diff --git a/Examples/test-suite/java_typemaps_proxy.i b/Examples/test-suite/java_typemaps_proxy.i index 3e9b18335..77d706835 100644 --- a/Examples/test-suite/java_typemaps_proxy.i +++ b/Examples/test-suite/java_typemaps_proxy.i @@ -119,8 +119,6 @@ public: void const_member_method(const ConstWithout *p) const {} const ConstWithout * const_var; const ConstWithout * const var_const; -private: - ConstWithout& operator=(const ConstWithout &); }; const ConstWithout * global_constwithout = 0; void global_method_constwithout(const ConstWithout *p) {} diff --git a/Source/Modules/allocate.cxx b/Source/Modules/allocate.cxx index 23683c385..56b6f0b08 100644 --- a/Source/Modules/allocate.cxx +++ b/Source/Modules/allocate.cxx @@ -779,6 +779,15 @@ Allocate(): if (Swig_storage_isstatic(n)) { Setattr(n, "cplus:staticbase", inclass); + } else if (Cmp(Getattr(n, "kind"), "variable") == 0) { + /* Check member variable to determine whether assignment is valid */ + if (GetFlag(n, "feature:immutable")) { + /* Can't assign a class with an immutable member variable */ + Setattr(inclass, "allocate:noassign", "1"); + } else if (SwigType_isreference(Getattr(n, "type"))) { + /* Can't assign a class with reference member data */ + Setattr(inclass, "allocate:noassign", "1"); + } } String *name = Getattr(n, "name");