Author: labath
Date: Tue Apr  2 03:18:46 2019
New Revision: 357463

URL: http://llvm.org/viewvc/llvm-project?rev=357463&view=rev
Log:
Make operator==s consistent between c++ and python APIs

Summary:
modify-python-lldb.py had code to insert python equality operators to
some classes. Some of those classes already had c++ equality operators,
and some didn't.

This makes the situation more consistent, by removing all equality
handilng from modify-python-lldb. Instead, I add c++ operators to
classes where they were missing, and expose them in the swig interface
files so that they are available to python too.

The only tricky case was the SBAddress class, which had an operator==
defined as a free function, which is not handled by swig. This function
cannot be removed without breaking ABI, and we cannot add an extra
operator== member, as that would make equality comparisons ambiguous.
For this class, I define a python __eq__ function by hand and have it
delegate to the operator!=, which I have defined as a member function.

This isn't fully NFC, as the semantics of some equality functions in
python changes slightly, but I believe it changes for the better (e.g.,
previously SBBreakpoint.__eq__ would consider two breakpoints with the
same ID as equal, even if they belonged to different targets; now they
are only equal if they belong to the same target).

Reviewers: jingham, clayborg, zturner

Subscribers: jdoerfert, JDevlieghere, lldb-commits

Differential Revision: https://reviews.llvm.org/D59819

Modified:
    lldb/trunk/include/lldb/API/SBAddress.h
    lldb/trunk/include/lldb/API/SBFileSpec.h
    lldb/trunk/include/lldb/API/SBWatchpoint.h
    lldb/trunk/scripts/Python/modify-python-lldb.py
    lldb/trunk/scripts/interface/SBAddress.i
    lldb/trunk/scripts/interface/SBBreakpoint.i
    lldb/trunk/scripts/interface/SBFileSpec.i
    lldb/trunk/scripts/interface/SBModule.i
    lldb/trunk/scripts/interface/SBWatchpoint.i
    lldb/trunk/scripts/lldb.swig
    lldb/trunk/source/API/SBAddress.cpp
    lldb/trunk/source/API/SBFileSpec.cpp
    lldb/trunk/source/API/SBWatchpoint.cpp

Modified: lldb/trunk/include/lldb/API/SBAddress.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBAddress.h?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBAddress.h (original)
+++ lldb/trunk/include/lldb/API/SBAddress.h Tue Apr  2 03:18:46 2019
@@ -31,6 +31,10 @@ public:
 
   explicit operator bool() const;
 
+  // operator== is a free function
+
+  bool operator!=(const SBAddress &rhs) const;
+
   bool IsValid() const;
 
   void Clear();

Modified: lldb/trunk/include/lldb/API/SBFileSpec.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBFileSpec.h?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBFileSpec.h (original)
+++ lldb/trunk/include/lldb/API/SBFileSpec.h Tue Apr  2 03:18:46 2019
@@ -30,6 +30,10 @@ public:
 
   explicit operator bool() const;
 
+  bool operator==(const SBFileSpec &rhs) const;
+
+  bool operator!=(const SBFileSpec &rhs) const;
+
   bool IsValid() const;
 
   bool Exists() const;

Modified: lldb/trunk/include/lldb/API/SBWatchpoint.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBWatchpoint.h?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBWatchpoint.h (original)
+++ lldb/trunk/include/lldb/API/SBWatchpoint.h Tue Apr  2 03:18:46 2019
@@ -27,6 +27,10 @@ public:
 
   explicit operator bool() const;
 
+  bool operator==(const SBWatchpoint &rhs) const;
+
+  bool operator!=(const SBWatchpoint &rhs) const;
+
   bool IsValid() const;
 
   SBError GetError();

Modified: lldb/trunk/scripts/Python/modify-python-lldb.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/modify-python-lldb.py?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/modify-python-lldb.py (original)
+++ lldb/trunk/scripts/Python/modify-python-lldb.py Tue Apr  2 03:18:46 2019
@@ -87,10 +87,6 @@ compile_unit_iter = "    def compile_uni
 # Eligible objects are those containers with unambiguous iteration support.
 len_def = "    def __len__(self): return self.%s()"
 
-# This supports the rich comparison methods of __eq__ and __ne__.
-eq_def = "    def __eq__(self, other): return isinstance(other, %s) and %s"
-ne_def = "    def __ne__(self, other): return not self.__eq__(other)"
-
 # A convenience iterator for SBSymbol!
 symbol_in_section_iter_def = '''
     def symbol_in_section_iter(self, section):
@@ -135,17 +131,6 @@ d = {'SBBreakpoint': ('GetNumLocations',
      'SBModule-symbol-in-section': symbol_in_section_iter_def
      }
 
-#
-# This dictionary defines a mapping from classname to equality method name(s).
-#
-e = {'SBAddress': ['GetFileAddress', 'GetModule'],
-     'SBBreakpoint': ['GetID'],
-     'SBWatchpoint': ['GetID'],
-     'SBFileSpec': ['GetFilename', 'GetDirectory'],
-     'SBModule': ['GetFileSpec', 'GetUUIDString'],
-     }
-
-
 def list_to_frag(list):
     """Transform a list to equality program fragment.
 
@@ -198,41 +183,28 @@ new_content = NewContent()
 with open(output_name, 'r') as f_in:
     content = f_in.read()
 
-# The pattern for recognizing the SWIG Version string
-version_pattern = re.compile("^# Version:? (.*)$")
-
 # The pattern for recognizing the beginning of an SB class definition.
 class_pattern = re.compile("^class (SB.*)\(_object\):$")
 
 # The pattern for recognizing the beginning of the __init__ method definition.
 init_pattern = re.compile("^    def __init__\(self.*\):")
 
-# The pattern for recognizing the beginning of the IsValid method definition.
-isvalid_pattern = re.compile("^    def IsValid\(")
-
 # These define the states of our finite state machine.
 NORMAL = 1
 DEFINING_ITERATOR = 2
-DEFINING_EQUALITY = 4
 CLEANUP_DOCSTRING = 8
 
 # Our FSM begins its life in the NORMAL state, and transitions to the
-# DEFINING_ITERATOR and/or DEFINING_EQUALITY state whenever it encounters the
-# beginning of certain class definitions, see dictionaries 'd' and 'e' above.
+# DEFINING_ITERATOR state whenever it encounters the beginning of certain class
+# definitions, see dictionary 'd' above.
 #
-# Note that the two states DEFINING_ITERATOR and DEFINING_EQUALITY are
-# orthogonal in that our FSM can be in one, the other, or both states at the
-# same time.  During such time, the FSM is eagerly searching for the __init__
+# In the DEFINING_ITERATOR state, the FSM is eagerly searching for the __init__
 # method definition in order to insert the appropriate method(s) into the lldb
 # module.
 #
 # The state CLEANUP_DOCSTRING can be entered from either the NORMAL or the
-# DEFINING_ITERATOR/EQUALITY states.  While in this state, the FSM is fixing/
-# cleaning the Python docstrings generated by the swig docstring features.
-#
-# The FSM, in all possible states, also checks the current input for IsValid()
-# definition, and inserts a __nonzero__() method definition to implement truth
-# value testing and the built-in operation bool().
+# DEFINING_ITERATOR state.  While in this state, the FSM is fixing/cleaning the
+# Python docstrings generated by the swig docstring features.
 state = NORMAL
 
 for line in content.splitlines():
@@ -254,22 +226,18 @@ for line in content.splitlines():
     if state == NORMAL:
         match = class_pattern.search(line)
         # If we are at the beginning of the class definitions, prepare to
-        # transition to the DEFINING_ITERATOR/DEFINING_EQUALITY state for the
-        # right class names.
+        # transition to the DEFINING_ITERATOR state for the right class names.
         if match:
             cls = match.group(1)
             if cls in d:
                 # Adding support for iteration for the matched SB class.
                 state |= DEFINING_ITERATOR
-            if cls in e:
-                # Adding support for eq and ne for the matched SB class.
-                state |= DEFINING_EQUALITY
 
-    if (state & DEFINING_ITERATOR) or (state & DEFINING_EQUALITY):
+    if state & DEFINING_ITERATOR:
         match = init_pattern.search(line)
         if match:
             # We found the beginning of the __init__ method definition.
-            # This is a good spot to insert the iter and/or eq-ne support.
+            # This is a good spot to insert the iter support.
             #
             # But note that SBTarget has three types of iterations.
             if cls == "SBTarget":
@@ -280,9 +248,6 @@ for line in content.splitlines():
                 if (state & DEFINING_ITERATOR):
                     new_content.add_line(iter_def % d[cls])
                     new_content.add_line(len_def % d[cls][0])
-                if (state & DEFINING_EQUALITY):
-                    new_content.add_line(eq_def % (cls, list_to_frag(e[cls])))
-                    new_content.add_line(ne_def)
 
             # SBModule has extra SBSection, SBCompileUnit iterators and
             # symbol_in_section_iter()!

Modified: lldb/trunk/scripts/interface/SBAddress.i
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/interface/SBAddress.i?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/scripts/interface/SBAddress.i (original)
+++ lldb/trunk/scripts/interface/SBAddress.i Tue Apr  2 03:18:46 2019
@@ -64,6 +64,15 @@ public:
 
     explicit operator bool() const;
 
+    // operator== is a free function, which swig does not handle, so we inject
+    // our own equality operator here
+    %pythoncode%{
+    def __eq__(self, other):
+      return not self.__ne__(other)
+    %}
+
+    bool operator!=(const SBAddress &rhs) const;
+
     void
     Clear ();
 

Modified: lldb/trunk/scripts/interface/SBBreakpoint.i
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/interface/SBBreakpoint.i?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/scripts/interface/SBBreakpoint.i (original)
+++ lldb/trunk/scripts/interface/SBBreakpoint.i Tue Apr  2 03:18:46 2019
@@ -85,6 +85,10 @@ public:
 
     ~SBBreakpoint();
 
+    bool operator==(const lldb::SBBreakpoint &rhs);
+
+    bool operator!=(const lldb::SBBreakpoint &rhs);
+
     break_id_t
     GetID () const;
 

Modified: lldb/trunk/scripts/interface/SBFileSpec.i
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/interface/SBFileSpec.i?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/scripts/interface/SBFileSpec.i (original)
+++ lldb/trunk/scripts/interface/SBFileSpec.i Tue Apr  2 03:18:46 2019
@@ -42,6 +42,10 @@ public:
 
     ~SBFileSpec ();
 
+    bool operator==(const SBFileSpec &rhs) const;
+
+    bool operator!=(const SBFileSpec &rhs) const;
+
     bool
     IsValid() const;
 

Modified: lldb/trunk/scripts/interface/SBModule.i
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/interface/SBModule.i?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/scripts/interface/SBModule.i (original)
+++ lldb/trunk/scripts/interface/SBModule.i Tue Apr  2 03:18:46 2019
@@ -184,6 +184,10 @@ public:
     const char *
     GetUUIDString () const;
 
+    bool operator==(const lldb::SBModule &rhs) const;
+
+    bool operator!=(const lldb::SBModule &rhs) const;
+
     lldb::SBSection
     FindSection (const char *sect_name);
 

Modified: lldb/trunk/scripts/interface/SBWatchpoint.i
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/interface/SBWatchpoint.i?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/scripts/interface/SBWatchpoint.i (original)
+++ lldb/trunk/scripts/interface/SBWatchpoint.i Tue Apr  2 03:18:46 2019
@@ -32,6 +32,10 @@ public:
 
     explicit operator bool() const;
 
+    bool operator==(const SBWatchpoint &rhs) const;
+
+    bool operator!=(const SBWatchpoint &rhs) const;
+
     SBError
     GetError();
 

Modified: lldb/trunk/scripts/lldb.swig
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/lldb.swig?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/scripts/lldb.swig (original)
+++ lldb/trunk/scripts/lldb.swig Tue Apr  2 03:18:46 2019
@@ -95,8 +95,7 @@ def lldb_iter(obj, getsize, getelem):
 # 
==============================================================================
 # The modify-python-lldb.py script is responsible for post-processing this 
SWIG-
 # generated lldb.py module.  It is responsible for adding support for: 
iteration
-# protocol: __iter__, rich comparison methods: __eq__ and __ne__, and built-in
-# function len(): __len__.
+# protocol: __iter__, and built-in function len(): __len__.
 # 
==============================================================================
 %}
 

Modified: lldb/trunk/source/API/SBAddress.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBAddress.cpp?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/source/API/SBAddress.cpp (original)
+++ lldb/trunk/source/API/SBAddress.cpp Tue Apr  2 03:18:46 2019
@@ -69,6 +69,13 @@ bool lldb::operator==(const SBAddress &l
   return false;
 }
 
+bool SBAddress::operator!=(const SBAddress &rhs) const {
+  LLDB_RECORD_METHOD_CONST(bool, SBAddress, operator!=,(const SBAddress &),
+                           &rhs);
+
+  return !(*this == rhs);
+}
+
 bool SBAddress::IsValid() const {
   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBAddress, IsValid);
   return this->operator bool();
@@ -294,6 +301,8 @@ void RegisterMethods<SBAddress>(Registry
   LLDB_REGISTER_CONSTRUCTOR(SBAddress, (lldb::addr_t, lldb::SBTarget &));
   LLDB_REGISTER_METHOD(const lldb::SBAddress &,
                        SBAddress, operator=,(const lldb::SBAddress &));
+  LLDB_REGISTER_METHOD_CONST(bool,
+                             SBAddress, operator!=,(const lldb::SBAddress &));
   LLDB_REGISTER_METHOD_CONST(bool, SBAddress, IsValid, ());
   LLDB_REGISTER_METHOD_CONST(bool, SBAddress, operator bool, ());
   LLDB_REGISTER_METHOD(void, SBAddress, Clear, ());

Modified: lldb/trunk/source/API/SBFileSpec.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBFileSpec.cpp?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/source/API/SBFileSpec.cpp (original)
+++ lldb/trunk/source/API/SBFileSpec.cpp Tue Apr  2 03:18:46 2019
@@ -62,6 +62,20 @@ const SBFileSpec &SBFileSpec::operator=(
   return *this;
 }
 
+bool SBFileSpec::operator==(const SBFileSpec &rhs) const {
+  LLDB_RECORD_METHOD_CONST(bool, SBFileSpec, operator==,(const SBFileSpec 
&rhs),
+                           rhs);
+
+  return ref() == rhs.ref();
+}
+
+bool SBFileSpec::operator!=(const SBFileSpec &rhs) const {
+  LLDB_RECORD_METHOD_CONST(bool, SBFileSpec, operator!=,(const SBFileSpec 
&rhs),
+                           rhs);
+
+  return !(*this == rhs);
+}
+
 bool SBFileSpec::IsValid() const {
   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, IsValid);
   return this->operator bool();
@@ -185,6 +199,10 @@ void RegisterMethods<SBFileSpec>(Registr
   LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, (const char *, bool));
   LLDB_REGISTER_METHOD(const lldb::SBFileSpec &,
                        SBFileSpec, operator=,(const lldb::SBFileSpec &));
+  LLDB_REGISTER_METHOD_CONST(bool,
+                             SBFileSpec, operator==,(const lldb::SBFileSpec 
&));
+  LLDB_REGISTER_METHOD_CONST(bool,
+                             SBFileSpec, operator!=,(const lldb::SBFileSpec 
&));
   LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, IsValid, ());
   LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, operator bool, ());
   LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, Exists, ());

Modified: lldb/trunk/source/API/SBWatchpoint.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBWatchpoint.cpp?rev=357463&r1=357462&r2=357463&view=diff
==============================================================================
--- lldb/trunk/source/API/SBWatchpoint.cpp (original)
+++ lldb/trunk/source/API/SBWatchpoint.cpp Tue Apr  2 03:18:46 2019
@@ -70,6 +70,20 @@ SBWatchpoint::operator bool() const {
   return bool(m_opaque_wp.lock());
 }
 
+bool SBWatchpoint::operator==(const SBWatchpoint &rhs) const {
+  LLDB_RECORD_METHOD_CONST(
+      bool, SBWatchpoint, operator==,(const SBWatchpoint &), rhs);
+
+  return GetSP() == rhs.GetSP();
+}
+
+bool SBWatchpoint::operator!=(const SBWatchpoint &rhs) const {
+  LLDB_RECORD_METHOD_CONST(
+      bool, SBWatchpoint, operator!=,(const SBWatchpoint &), rhs);
+
+  return !(*this == rhs);
+}
+
 SBError SBWatchpoint::GetError() {
   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBWatchpoint, GetError);
 
@@ -303,6 +317,10 @@ void RegisterMethods<SBWatchpoint>(Regis
   LLDB_REGISTER_METHOD(lldb::watch_id_t, SBWatchpoint, GetID, ());
   LLDB_REGISTER_METHOD_CONST(bool, SBWatchpoint, IsValid, ());
   LLDB_REGISTER_METHOD_CONST(bool, SBWatchpoint, operator bool, ());
+  LLDB_REGISTER_METHOD_CONST(
+      bool, SBWatchpoint, operator==,(const lldb::SBWatchpoint &));
+  LLDB_REGISTER_METHOD_CONST(
+      bool, SBWatchpoint, operator!=,(const lldb::SBWatchpoint &));
   LLDB_REGISTER_METHOD(lldb::SBError, SBWatchpoint, GetError, ());
   LLDB_REGISTER_METHOD(int32_t, SBWatchpoint, GetHardwareIndex, ());
   LLDB_REGISTER_METHOD(lldb::addr_t, SBWatchpoint, GetWatchAddress, ());


_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to