Add truth value testing to those lldb Python objects with the IsValid() method definitions.
object.__nonzero__(self) is called to implement truth value testing and the built-in operation bool(),
via a simple delegation to self.IsValid().

Change tests under python_api/lldbutil to utilize this mechanism.


git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@131494 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/scripts/Python/modify-python-lldb.py b/scripts/Python/modify-python-lldb.py
index da395c5..5b75789 100644
--- a/scripts/Python/modify-python-lldb.py
+++ b/scripts/Python/modify-python-lldb.py
@@ -34,22 +34,24 @@
 
 '''
 
-#
 # This supports the iteration protocol.
-#
 iter_def = "    def __iter__(self): return lldb_iter(self, '%s', '%s')"
 module_iter = "    def module_iter(self): return lldb_iter(self, '%s', '%s')"
 breakpoint_iter = "    def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')"
-#
+
 # Called to implement the built-in function len().
 # 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)"
 
+# Called to implement truth value testing and the built-in operation bool();
+# should return False or True, or their integer equivalents 0 or 1.
+# Delegate to self.IsValid() if it is defined for the current lldb object.
+nonzero_def = "    def __nonzero__(self): return self.IsValid()"
+
 #
 # The dictionary defines a mapping from classname to (getsize, getelem) tuple.
 #
@@ -109,6 +111,9 @@
 # The pattern for recognizing the beginning of the __init__ method definition.
 init_pattern = re.compile("^    def __init__\(self, \*args\):")
 
+# The pattern for recognizing the beginning of the IsValid method definition.
+isvalid_pattern = re.compile("^    def IsValid\(\*args\):")
+
 # These define the states of our finite state machine.
 NORMAL = 0
 DEFINING_ITERATOR = 1
@@ -117,13 +122,32 @@
 # The lldb_iter_def only needs to be inserted once.
 lldb_iter_defined = False;
 
+# 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.
+#
+# 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__
+# method definition in order to insert the appropriate method(s) into the lldb
+# module.
+#
+# Assuming that SWIG puts out the __init__ method definition before other method
+# definitions, the FSM, while in NORMAL state, 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().
 state = NORMAL
 for line in content.splitlines():
     if state == NORMAL:
         match = class_pattern.search(line)
+        # Inserts the lldb_iter() definition before the first class definition.
         if not lldb_iter_defined and match:
             print >> new_content, lldb_iter_def
             lldb_iter_defined = True
+
+        # 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.
         if match:
             cls = match.group(1)
             if cls in d:
@@ -132,6 +156,10 @@
             if cls in e:
                 # Adding support for eq and ne for the matched SB class.
                 state = (state | DEFINING_EQUALITY)
+        # Look for 'def IsValid(*args):', and once located, add implementation
+        # of truth value testing for objects by delegation.
+        elif isvalid_pattern.search(line):
+            print >> new_content, nonzero_def
     elif state > NORMAL:
         match = init_pattern.search(line)
         if match: