modify-python-lldb.py: clean up __iter__ and __len__ support

Summary:
Instead of modifying the swig-generated code, just add the appropriate
methods to the interface files in order to get the swig to do the
generation for us.

This is a straight-forward move from the python script to the interface
files. The single class which has nontrivial handling in the script
(SBModule) has been left for a separate patch.

For the cases where I did not find any tests exercising the
iteration/length methods (i.e., no tests failed after I stopped emitting
them), I tried to add basic tests for that functionality.

Reviewers: zturner, jingham, amccarth

Subscribers: jdoerfert, lldb-commits

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

llvm-svn: 357572
diff --git a/lldb/scripts/interface/SBBreakpoint.i b/lldb/scripts/interface/SBBreakpoint.i
index ee9cdc1..de80de3 100644
--- a/lldb/scripts/interface/SBBreakpoint.i
+++ b/lldb/scripts/interface/SBBreakpoint.i
@@ -287,7 +287,17 @@
             for idx in range(len(accessor)):
                 locations.append(accessor[idx])
             return locations
-        
+
+        def __iter__(self):
+            '''Iterate over all breakpoint locations in a lldb.SBBreakpoint
+            object.'''
+            return lldb_iter(self, 'GetNumLocations', 'GetLocationAtIndex')
+
+        def __len__(self):
+            '''Return the number of breakpoint locations in a lldb.SBBreakpoint
+            object.'''
+            return self.GetNumLocations()
+
         __swig_getmethods__["locations"] = get_breakpoint_location_list
         if _newclass: locations = property(get_breakpoint_location_list, None, doc='''A read only property that returns a list() of lldb.SBBreakpointLocation objects for this breakpoint.''')
         
diff --git a/lldb/scripts/interface/SBCompileUnit.i b/lldb/scripts/interface/SBCompileUnit.i
index 0ddc576..1421813 100644
--- a/lldb/scripts/interface/SBCompileUnit.i
+++ b/lldb/scripts/interface/SBCompileUnit.i
@@ -120,6 +120,15 @@
     operator != (const lldb::SBCompileUnit &rhs) const;
     
     %pythoncode %{
+        def __iter__(self):
+            '''Iterate over all line entries in a lldb.SBCompileUnit object.'''
+            return lldb_iter(self, 'GetNumLineEntries', 'GetLineEntryAtIndex')
+
+        def __len__(self):
+            '''Return the number of line entries in a lldb.SBCompileUnit
+            object.'''
+            return self.GetNumLineEntries()
+
         __swig_getmethods__["file"] = GetFileSpec
         if _newclass: file = property(GetFileSpec, None, doc='''A read only property that returns the same result an lldb object that represents the source file (lldb.SBFileSpec) for the compile unit.''')
         
diff --git a/lldb/scripts/interface/SBDebugger.i b/lldb/scripts/interface/SBDebugger.i
index da9c9b7..9397d73 100644
--- a/lldb/scripts/interface/SBDebugger.i
+++ b/lldb/scripts/interface/SBDebugger.i
@@ -435,6 +435,17 @@
 
     lldb::SBError
     RunREPL (lldb::LanguageType language, const char *repl_options);
+
+    %pythoncode%{
+    def __iter__(self):
+        '''Iterate over all targets in a lldb.SBDebugger object.'''
+        return lldb_iter(self, 'GetNumTargets', 'GetTargetAtIndex')
+
+    def __len__(self):
+        '''Return the number of targets in a lldb.SBDebugger object.'''
+        return self.GetNumTargets()
+    %}
+
 }; // class SBDebugger
 
 } // namespace lldb
diff --git a/lldb/scripts/interface/SBInstructionList.i b/lldb/scripts/interface/SBInstructionList.i
index cbc4351..d877e7b 100644
--- a/lldb/scripts/interface/SBInstructionList.i
+++ b/lldb/scripts/interface/SBInstructionList.i
@@ -64,6 +64,11 @@
     DumpEmulationForAllInstructions (const char *triple);
 
     %pythoncode %{
+        def __iter__(self):
+            '''Iterate over all instructions in a lldb.SBInstructionList
+            object.'''
+            return lldb_iter(self, 'GetSize', 'GetInstructionAtIndex')
+
         def __len__(self):
             '''Access len of the instruction list.'''
             return int(self.GetSize())
diff --git a/lldb/scripts/interface/SBProcess.i b/lldb/scripts/interface/SBProcess.i
index 9cb4741..7e00ed3 100644
--- a/lldb/scripts/interface/SBProcess.i
+++ b/lldb/scripts/interface/SBProcess.i
@@ -497,6 +497,15 @@
             for idx in range(len(accessor)):
                 threads.append(accessor[idx])
             return threads
+
+        def __iter__(self):
+            '''Iterate over all threads in a lldb.SBProcess object.'''
+            return lldb_iter(self, 'GetNumThreads', 'GetThreadAtIndex')
+ 
+        def __len__(self):
+            '''Return the number of threads in a lldb.SBProcess object.'''
+            return self.GetNumThreads()
+
         
         __swig_getmethods__["threads"] = get_process_thread_list
         if _newclass: threads = property(get_process_thread_list, None, doc='''A read only property that returns a list() of lldb.SBThread objects for this process.''')
diff --git a/lldb/scripts/interface/SBSection.i b/lldb/scripts/interface/SBSection.i
index 9cf7052..8dc3d15 100644
--- a/lldb/scripts/interface/SBSection.i
+++ b/lldb/scripts/interface/SBSection.i
@@ -118,6 +118,14 @@
     operator != (const lldb::SBSection &rhs);
      
     %pythoncode %{
+        def __iter__(self):
+            '''Iterate over all subsections in a lldb.SBSection object.'''
+            return lldb_iter(self, 'GetNumSubSections', 'GetSubSectionAtIndex')
+
+        def __len__(self):
+            '''Return the number of subsections in a lldb.SBSection object.'''
+            return self.GetNumSubSections()
+
         def get_addr(self):
             return SBAddress(self, 0)
 
diff --git a/lldb/scripts/interface/SBStringList.i b/lldb/scripts/interface/SBStringList.i
index 48869a1..a866cab 100644
--- a/lldb/scripts/interface/SBStringList.i
+++ b/lldb/scripts/interface/SBStringList.i
@@ -40,6 +40,16 @@
 
     void
     Clear ();
+
+    %pythoncode%{
+    def __iter__(self):
+        '''Iterate over all strings in a lldb.SBStringList object.'''
+        return lldb_iter(self, 'GetSize', 'GetStringAtIndex')
+
+    def __len__(self):
+        '''Return the number of strings in a lldb.SBStringList object.'''
+        return self.GetSize()
+    %}
 };
 
 } // namespace lldb
diff --git a/lldb/scripts/interface/SBSymbolContextList.i b/lldb/scripts/interface/SBSymbolContextList.i
index ee3df2f..f35caa6 100644
--- a/lldb/scripts/interface/SBSymbolContextList.i
+++ b/lldb/scripts/interface/SBSymbolContextList.i
@@ -62,6 +62,11 @@
     Clear();
     
     %pythoncode %{
+        def __iter__(self):
+            '''Iterate over all symbol contexts in a lldb.SBSymbolContextList
+            object.'''
+            return lldb_iter(self, 'GetSize', 'GetContextAtIndex')
+
         def __len__(self):
             return int(self.GetSize())
 
diff --git a/lldb/scripts/interface/SBTarget.i b/lldb/scripts/interface/SBTarget.i
index 7ae234f..4d4f2c6 100644
--- a/lldb/scripts/interface/SBTarget.i
+++ b/lldb/scripts/interface/SBTarget.i
@@ -1103,6 +1103,21 @@
                 modules.append(self.GetModuleAtIndex(idx))
             return modules
 
+        def module_iter(self):
+            '''Returns an iterator over all modules in a lldb.SBTarget
+            object.'''
+            return lldb_iter(self, 'GetNumModules', 'GetModuleAtIndex')
+
+        def breakpoint_iter(self):
+            '''Returns an iterator over all breakpoints in a lldb.SBTarget
+            object.'''
+            return lldb_iter(self, 'GetNumBreakpoints', 'GetBreakpointAtIndex')
+
+        def watchpoint_iter(self):
+            '''Returns an iterator over all watchpoints in a lldb.SBTarget
+            object.'''
+            return lldb_iter(self, 'GetNumWatchpoints', 'GetWatchpointAtIndex')
+
         __swig_getmethods__["modules"] = get_modules_array
         if _newclass: modules = property(get_modules_array, None, doc='''A read only property that returns a list() of lldb.SBModule objects contained in this target. This list is a list all modules that the target currently is tracking (the main executable and all dependent shared libraries).''')
 
diff --git a/lldb/scripts/interface/SBThread.i b/lldb/scripts/interface/SBThread.i
index d237225..86ec280 100644
--- a/lldb/scripts/interface/SBThread.i
+++ b/lldb/scripts/interface/SBThread.i
@@ -428,6 +428,14 @@
     SafeToCallFunctions ();
 
     %pythoncode %{
+        def __iter__(self):
+            '''Iterate over all frames in a lldb.SBThread object.'''
+            return lldb_iter(self, 'GetNumFrames', 'GetFrameAtIndex')
+
+        def __len__(self):
+            '''Return the number of frames in a lldb.SBThread object.'''
+            return self.GetNumFrames()
+
         class frames_access(object):
             '''A helper object that will lazily hand out frames for a thread when supplied an index.'''
             def __init__(self, sbthread):
diff --git a/lldb/scripts/interface/SBType.i b/lldb/scripts/interface/SBType.i
index 983465e..addfe6c 100644
--- a/lldb/scripts/interface/SBType.i
+++ b/lldb/scripts/interface/SBType.i
@@ -509,6 +509,16 @@
     GetSize();
 
     ~SBTypeList();
+
+    %pythoncode%{
+    def __iter__(self):
+        '''Iterate over all types in a lldb.SBTypeList object.'''
+        return lldb_iter(self, 'GetSize', 'GetTypeAtIndex')
+
+    def __len__(self):
+        '''Return the number of types in a lldb.SBTypeList object.'''
+        return self.GetSize()
+    %}
 };
 
 } // namespace lldb
diff --git a/lldb/scripts/interface/SBValue.i b/lldb/scripts/interface/SBValue.i
index 6894354..eca83d5 100644
--- a/lldb/scripts/interface/SBValue.i
+++ b/lldb/scripts/interface/SBValue.i
@@ -495,6 +495,14 @@
             for idx in range(len(accessor)):
                 children.append(accessor[idx])
             return children
+
+        def __iter__(self):
+            '''Iterate over all child values of a lldb.SBValue object.'''
+            return lldb_iter(self, 'GetNumChildren', 'GetChildAtIndex')
+
+        def __len__(self):
+            '''Return the number of child values of a lldb.SBValue object.'''
+            return self.GetNumChildren()
         
         __swig_getmethods__["children"] = get_value_child_list
         if _newclass: children = property(get_value_child_list, None, doc='''A read only property that returns a list() of lldb.SBValue objects for the children of the value.''')
diff --git a/lldb/scripts/interface/SBValueList.i b/lldb/scripts/interface/SBValueList.i
index c2cdda2..7960e09 100644
--- a/lldb/scripts/interface/SBValueList.i
+++ b/lldb/scripts/interface/SBValueList.i
@@ -102,6 +102,10 @@
     GetFirstValueByName (const char* name) const;
     
     %pythoncode %{
+        def __iter__(self):
+            '''Iterate over all values in a lldb.SBValueList object.'''
+            return lldb_iter(self, 'GetSize', 'GetValueAtIndex')
+
         def __len__(self):
             return int(self.GetSize())