Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 1 | # |
Johnny Chen | c622005 | 2011-04-28 23:53:16 +0000 | [diff] [blame] | 2 | # modify-lldb-python.py |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 3 | # |
| 4 | # This script modifies the lldb module (which was automatically generated via |
Johnny Chen | 22e418a | 2011-04-29 19:22:24 +0000 | [diff] [blame] | 5 | # running swig) to support iteration and/or equality operations for certain lldb |
Johnny Chen | e5637d2 | 2011-05-24 21:05:16 +0000 | [diff] [blame] | 6 | # objects, implements truth value testing for certain lldb objects, and adds a |
| 7 | # global variable 'debugger_unique_id' which is initialized to 0. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 8 | # |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 9 | # As a cleanup step, it also removes the 'residues' from the autodoc features of |
| 10 | # swig. For an example, take a look at SBTarget.h header file, where we take |
Johnny Chen | 2c77fa4 | 2011-07-02 20:01:09 +0000 | [diff] [blame] | 11 | # advantage of the already existing doxygen C++-docblock and make it the Python |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 12 | # docstring for the same method. The 'residues' in this context include the |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 13 | # '#endif', the '#ifdef SWIG', the c comment marker, the trailing blank (SPC's) |
| 14 | # line, and the doxygen comment start marker. |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 15 | # |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 16 | # In addition to the 'residues' removal during the cleanup step, it also |
| 17 | # transforms the 'char' data type (which was actually 'char *' but the 'autodoc' |
| 18 | # feature of swig removes ' *' from it into 'str' (as a Python str type). |
| 19 | # |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 20 | # It also calls SBDebugger.Initialize() to initialize the lldb debugger |
| 21 | # subsystem. |
| 22 | # |
| 23 | |
| 24 | import sys, re, StringIO |
| 25 | |
| 26 | if len (sys.argv) != 2: |
| 27 | output_name = "./lldb.py" |
| 28 | else: |
| 29 | output_name = sys.argv[1] + "/lldb.py" |
| 30 | |
| 31 | # print "output_name is '" + output_name + "'" |
| 32 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 33 | # |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 34 | # Residues to be removed. |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 35 | # |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 36 | c_endif_swig = "#endif" |
| 37 | c_ifdef_swig = "#ifdef SWIG" |
Johnny Chen | 2c77fa4 | 2011-07-02 20:01:09 +0000 | [diff] [blame] | 38 | c_comment_marker = "//------------" |
| 39 | # The pattern for recognizing the doxygen comment block line. |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 40 | doxygen_comment_start = re.compile("^\s*(/// ?)") |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 41 | # The demarcation point for turning on/off residue removal state. |
| 42 | # When bracketed by the lines, the CLEANUP_DOCSTRING state (see below) is ON. |
| 43 | toggle_docstring_cleanup_line = ' """' |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 44 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 45 | def char_to_str_xform(line): |
| 46 | """This transforms the 'char', i.e, 'char *' to 'str', Python string.""" |
| 47 | line = line.replace(' char', ' str') |
| 48 | line = line.replace('char ', 'str ') |
Johnny Chen | 2de7ce6 | 2011-07-14 00:17:49 +0000 | [diff] [blame] | 49 | # Special case handling of 'char **argv' and 'char **envp'. |
| 50 | line = line.replace('str argv', 'list argv') |
| 51 | line = line.replace('str envp', 'list envp') |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 52 | return line |
| 53 | |
| 54 | # |
| 55 | # The one-liner docstring also needs char_to_str transformation, btw. |
| 56 | # |
Johnny Chen | 21c0fd1 | 2011-07-08 23:57:20 +0000 | [diff] [blame] | 57 | TWO_SPACES = ' ' * 2 |
| 58 | EIGHT_SPACES = ' ' * 8 |
| 59 | one_liner_docstring_pattern = re.compile('^(%s|%s)""".*"""$' % (TWO_SPACES, EIGHT_SPACES)) |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 60 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 61 | # |
Johnny Chen | ec5e0a2 | 2011-06-01 18:40:11 +0000 | [diff] [blame] | 62 | # lldb_iter() should appear before our first SB* class definition. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 63 | # |
| 64 | lldb_iter_def = ''' |
| 65 | # =================================== |
| 66 | # Iterator for lldb container objects |
| 67 | # =================================== |
| 68 | def lldb_iter(obj, getsize, getelem): |
| 69 | """A generator adaptor to support iteration for lldb container objects.""" |
| 70 | size = getattr(obj, getsize) |
| 71 | elem = getattr(obj, getelem) |
| 72 | for i in range(size()): |
| 73 | yield elem(i) |
| 74 | |
Johnny Chen | 8142220 | 2011-06-01 19:21:08 +0000 | [diff] [blame] | 75 | # ============================================================================== |
| 76 | # The modify-python-lldb.py script is responsible for post-processing this SWIG- |
| 77 | # generated lldb.py module. It is responsible for adding the above lldb_iter() |
| 78 | # function definition as well as the supports, in the following, for iteration |
| 79 | # protocol: __iter__, rich comparison methods: __eq__ and __ne__, truth value |
| 80 | # testing (and built-in operation bool()): __nonzero__, and built-in function |
| 81 | # len(): __len__. |
| 82 | # ============================================================================== |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 83 | ''' |
| 84 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 85 | # |
Johnny Chen | de856cc | 2011-07-25 23:41:08 +0000 | [diff] [blame] | 86 | # linked_list_iter() is a special purpose iterator to treat the SBValue as the |
| 87 | # head of a list data structure, where you specify the child member name which |
| 88 | # points to the next item on the list and you specify the end-of-list function |
| 89 | # which takes an SBValue and returns True if EOL is reached and False if not. |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 90 | # |
| 91 | linked_list_iter_def = ''' |
Johnny Chen | a4673e1 | 2011-07-26 20:57:10 +0000 | [diff] [blame] | 92 | def __eol_test__(val): |
| 93 | """Default function for end of list test takes an SBValue object. |
| 94 | |
| 95 | Return True if val is invalid or it corresponds to a null pointer. |
| 96 | Otherwise, return False. |
| 97 | """ |
| 98 | if not val or int(val.GetValue(), 0) == 0: |
| 99 | return True |
| 100 | else: |
| 101 | return False |
| 102 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 103 | # ================================================== |
| 104 | # Iterator for lldb.SBValue treated as a linked list |
| 105 | # ================================================== |
Johnny Chen | a4673e1 | 2011-07-26 20:57:10 +0000 | [diff] [blame] | 106 | def linked_list_iter(self, next_item_name, end_of_list_test=__eol_test__): |
Johnny Chen | de856cc | 2011-07-25 23:41:08 +0000 | [diff] [blame] | 107 | """Generator adaptor to support iteration for SBValue as a linked list. |
| 108 | |
| 109 | linked_list_iter() is a special purpose iterator to treat the SBValue as |
| 110 | the head of a list data structure, where you specify the child member |
| 111 | name which points to the next item on the list and you specify the |
| 112 | end-of-list test function which takes an SBValue for an item and returns |
| 113 | True if EOL is reached and False if not. |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 114 | |
Johnny Chen | a4673e1 | 2011-07-26 20:57:10 +0000 | [diff] [blame] | 115 | The end_of_list_test arg, if omitted, defaults to the __eol_test__ |
| 116 | function above. |
| 117 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 118 | For example, |
| 119 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 120 | # Get Frame #0. |
| 121 | ... |
| 122 | |
| 123 | # Get variable 'task_head'. |
| 124 | task_head = frame0.FindVariable('task_head') |
| 125 | ... |
| 126 | |
Johnny Chen | a4673e1 | 2011-07-26 20:57:10 +0000 | [diff] [blame] | 127 | for t in task_head.linked_list_iter('next'): |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 128 | print t |
| 129 | """ |
| 130 | try: |
| 131 | item = self.GetChildMemberWithName(next_item_name) |
Johnny Chen | 38581d2 | 2011-07-27 21:14:01 +0000 | [diff] [blame] | 132 | while not end_of_list_test(item): |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 133 | yield item |
| 134 | # Prepare for the next iteration. |
| 135 | item = item.GetChildMemberWithName(next_item_name) |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 136 | except: |
| 137 | # Exception occurred. Stop the generator. |
| 138 | pass |
| 139 | |
| 140 | return |
| 141 | ''' |
| 142 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 143 | # This supports the iteration protocol. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 144 | iter_def = " def __iter__(self): return lldb_iter(self, '%s', '%s')" |
| 145 | module_iter = " def module_iter(self): return lldb_iter(self, '%s', '%s')" |
| 146 | breakpoint_iter = " def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')" |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 147 | |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 148 | # Called to implement the built-in function len(). |
| 149 | # Eligible objects are those containers with unambiguous iteration support. |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 150 | len_def = " def __len__(self): return self.%s()" |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 151 | |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 152 | # This supports the rich comparison methods of __eq__ and __ne__. |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 153 | eq_def = " def __eq__(self, other): return isinstance(other, %s) and %s" |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 154 | ne_def = " def __ne__(self, other): return not self.__eq__(other)" |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 155 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 156 | # Called to implement truth value testing and the built-in operation bool(); |
| 157 | # should return False or True, or their integer equivalents 0 or 1. |
| 158 | # Delegate to self.IsValid() if it is defined for the current lldb object. |
| 159 | nonzero_def = " def __nonzero__(self): return self.IsValid()" |
| 160 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 161 | # |
Johnny Chen | a6303ef | 2011-05-24 22:53:03 +0000 | [diff] [blame] | 162 | # This dictionary defines a mapping from classname to (getsize, getelem) tuple. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 163 | # |
| 164 | d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'), |
| 165 | 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'), |
| 166 | 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'), |
| 167 | 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'), |
| 168 | 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'), |
| 169 | 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'), |
| 170 | |
| 171 | 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'), |
| 172 | 'SBStringList': ('GetSize', 'GetStringAtIndex',), |
| 173 | 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'), |
Johnny Chen | 3ee8520 | 2011-08-05 01:35:49 +0000 | [diff] [blame^] | 174 | 'SBTypeList': ('GetSize', 'GetTypeAtIndex'), |
| 175 | 'SBValueList': ('GetSize', 'GetValueAtIndex'), |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 176 | |
| 177 | 'SBType': ('GetNumberChildren', 'GetChildAtIndex'), |
| 178 | 'SBValue': ('GetNumChildren', 'GetChildAtIndex'), |
| 179 | |
Johnny Chen | 08477f5 | 2011-05-24 22:57:42 +0000 | [diff] [blame] | 180 | # SBTarget needs special processing, see below. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 181 | 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'), |
| 182 | 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex') |
| 183 | } |
| 184 | } |
| 185 | |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 186 | # |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 187 | # This dictionary defines a mapping from classname to equality method name(s). |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 188 | # |
Johnny Chen | 694cfd0 | 2011-06-09 22:04:56 +0000 | [diff] [blame] | 189 | e = { 'SBAddress': ['GetFileAddress', 'GetModule'], |
| 190 | 'SBBreakpoint': ['GetID'], |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 191 | 'SBFileSpec': ['GetFilename', 'GetDirectory'], |
| 192 | 'SBModule': ['GetFileSpec', 'GetUUIDString'] |
| 193 | } |
| 194 | |
| 195 | def list_to_frag(list): |
| 196 | """Transform a list to equality program fragment. |
| 197 | |
| 198 | For example, ['GetID'] is transformed to 'self.GetID() == other.GetID()', |
| 199 | and ['GetFilename', 'GetDirectory'] to 'self.GetFilename() == other.GetFilename() |
| 200 | and self.GetDirectory() == other.GetDirectory()'. |
| 201 | """ |
| 202 | if not list: |
| 203 | raise Exception("list should be non-empty") |
| 204 | frag = StringIO.StringIO() |
| 205 | for i in range(len(list)): |
| 206 | if i > 0: |
| 207 | frag.write(" and ") |
| 208 | frag.write("self.{0}() == other.{0}()".format(list[i])) |
| 209 | return frag.getvalue() |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 210 | |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 211 | class NewContent(StringIO.StringIO): |
| 212 | """Simple facade to keep track of the previous line to be committed.""" |
| 213 | def __init__(self): |
| 214 | StringIO.StringIO.__init__(self) |
| 215 | self.prev_line = None |
| 216 | def add_line(self, a_line): |
| 217 | """Add a line to the content, if there is a previous line, commit it.""" |
| 218 | if self.prev_line != None: |
| 219 | print >> self, self.prev_line |
| 220 | self.prev_line = a_line |
| 221 | def del_line(self): |
| 222 | """Forget about the previous line, do not commit it.""" |
| 223 | self.prev_line = None |
| 224 | def del_blank_line(self): |
| 225 | """Forget about the previous line if it is a blank line.""" |
| 226 | if self.prev_line != None and not self.prev_line.strip(): |
| 227 | self.prev_line = None |
| 228 | def finish(self): |
| 229 | """Call this when you're finished with populating content.""" |
| 230 | if self.prev_line != None: |
| 231 | print >> self, self.prev_line |
| 232 | self.prev_line = None |
| 233 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 234 | # The new content will have the iteration protocol defined for our lldb objects. |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 235 | new_content = NewContent() |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 236 | |
| 237 | with open(output_name, 'r') as f_in: |
| 238 | content = f_in.read() |
| 239 | |
| 240 | # The pattern for recognizing the beginning of an SB class definition. |
| 241 | class_pattern = re.compile("^class (SB.*)\(_object\):$") |
| 242 | |
| 243 | # The pattern for recognizing the beginning of the __init__ method definition. |
Johnny Chen | 3ee8520 | 2011-08-05 01:35:49 +0000 | [diff] [blame^] | 244 | init_pattern = re.compile("^ def __init__\(self.*\):") |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 245 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 246 | # The pattern for recognizing the beginning of the IsValid method definition. |
Peter Collingbourne | f208453 | 2011-06-14 03:55:41 +0000 | [diff] [blame] | 247 | isvalid_pattern = re.compile("^ def IsValid\(") |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 248 | |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 249 | # These define the states of our finite state machine. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 250 | NORMAL = 0 |
| 251 | DEFINING_ITERATOR = 1 |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 252 | DEFINING_EQUALITY = 2 |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 253 | CLEANUP_DOCSTRING = 4 |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 254 | |
| 255 | # The lldb_iter_def only needs to be inserted once. |
| 256 | lldb_iter_defined = False; |
| 257 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 258 | # Our FSM begins its life in the NORMAL state, and transitions to the |
| 259 | # DEFINING_ITERATOR and/or DEFINING_EQUALITY state whenever it encounters the |
| 260 | # beginning of certain class definitions, see dictionaries 'd' and 'e' above. |
| 261 | # |
| 262 | # Note that the two states DEFINING_ITERATOR and DEFINING_EQUALITY are |
| 263 | # orthogonal in that our FSM can be in one, the other, or both states at the |
| 264 | # same time. During such time, the FSM is eagerly searching for the __init__ |
| 265 | # method definition in order to insert the appropriate method(s) into the lldb |
| 266 | # module. |
| 267 | # |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 268 | # The state CLEANUP_DOCSTRING can be entered from either the NORMAL or the |
| 269 | # DEFINING_ITERATOR/EQUALITY states. While in this state, the FSM is fixing/ |
| 270 | # cleaning the Python docstrings generated by the swig docstring features. |
| 271 | # |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 272 | # The FSM, in all possible states, also checks the current input for IsValid() |
| 273 | # definition, and inserts a __nonzero__() method definition to implement truth |
| 274 | # value testing and the built-in operation bool(). |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 275 | state = NORMAL |
| 276 | for line in content.splitlines(): |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 277 | # Handle the state transition into CLEANUP_DOCSTRING state as it is possible |
| 278 | # to enter this state from either NORMAL or DEFINING_ITERATOR/EQUALITY. |
| 279 | # |
| 280 | # If ' """' is the sole line, prepare to transition to the |
| 281 | # CLEANUP_DOCSTRING state or out of it. |
| 282 | if line == toggle_docstring_cleanup_line: |
| 283 | if state & CLEANUP_DOCSTRING: |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 284 | # Special handling of the trailing blank line right before the '"""' |
| 285 | # end docstring marker. |
| 286 | new_content.del_blank_line() |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 287 | state ^= CLEANUP_DOCSTRING |
| 288 | else: |
| 289 | state |= CLEANUP_DOCSTRING |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 290 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 291 | if state == NORMAL: |
| 292 | match = class_pattern.search(line) |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 293 | # Inserts the lldb_iter() definition before the first class definition. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 294 | if not lldb_iter_defined and match: |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 295 | new_content.add_line(lldb_iter_def) |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 296 | lldb_iter_defined = True |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 297 | |
| 298 | # If we are at the beginning of the class definitions, prepare to |
| 299 | # transition to the DEFINING_ITERATOR/DEFINING_EQUALITY state for the |
| 300 | # right class names. |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 301 | if match: |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 302 | cls = match.group(1) |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 303 | if cls in d: |
| 304 | # Adding support for iteration for the matched SB class. |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 305 | state |= DEFINING_ITERATOR |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 306 | if cls in e: |
| 307 | # Adding support for eq and ne for the matched SB class. |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 308 | state |= DEFINING_EQUALITY |
| 309 | |
Johnny Chen | 533ed2f | 2011-07-15 20:46:19 +0000 | [diff] [blame] | 310 | if (state & DEFINING_ITERATOR) or (state & DEFINING_EQUALITY): |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 311 | match = init_pattern.search(line) |
| 312 | if match: |
| 313 | # We found the beginning of the __init__ method definition. |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 314 | # This is a good spot to insert the iter and/or eq-ne support. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 315 | # |
| 316 | # But note that SBTarget has two types of iterations. |
| 317 | if cls == "SBTarget": |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 318 | new_content.add_line(module_iter % (d[cls]['module'])) |
| 319 | new_content.add_line(breakpoint_iter % (d[cls]['breakpoint'])) |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 320 | else: |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 321 | if (state & DEFINING_ITERATOR): |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 322 | new_content.add_line(iter_def % d[cls]) |
| 323 | new_content.add_line(len_def % d[cls][0]) |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 324 | if (state & DEFINING_EQUALITY): |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 325 | new_content.add_line(eq_def % (cls, list_to_frag(e[cls]))) |
| 326 | new_content.add_line(ne_def) |
Johnny Chen | a2f86e8 | 2011-04-29 19:19:13 +0000 | [diff] [blame] | 327 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 328 | # This special purpose iterator is for SBValue only!!! |
| 329 | if cls == "SBValue": |
| 330 | new_content.add_line(linked_list_iter_def) |
| 331 | |
Johnny Chen | a2f86e8 | 2011-04-29 19:19:13 +0000 | [diff] [blame] | 332 | # Next state will be NORMAL. |
| 333 | state = NORMAL |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 334 | |
Johnny Chen | 533ed2f | 2011-07-15 20:46:19 +0000 | [diff] [blame] | 335 | if (state & CLEANUP_DOCSTRING): |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 336 | # Cleanse the lldb.py of the autodoc'ed residues. |
| 337 | if c_ifdef_swig in line or c_endif_swig in line: |
| 338 | continue |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 339 | # As well as the comment marker line. |
| 340 | if c_comment_marker in line: |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 341 | continue |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 342 | |
Johnny Chen | 533ed2f | 2011-07-15 20:46:19 +0000 | [diff] [blame] | 343 | # Also remove the '\a ' and '\b 'substrings. |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 344 | line = line.replace('\a ', '') |
Johnny Chen | 533ed2f | 2011-07-15 20:46:19 +0000 | [diff] [blame] | 345 | line = line.replace('\b ', '') |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 346 | # And the leading '///' substring. |
| 347 | doxygen_comment_match = doxygen_comment_start.match(line) |
| 348 | if doxygen_comment_match: |
| 349 | line = line.replace(doxygen_comment_match.group(1), '', 1) |
| 350 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 351 | line = char_to_str_xform(line) |
| 352 | |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 353 | # Note that the transition out of CLEANUP_DOCSTRING is handled at the |
| 354 | # beginning of this function already. |
| 355 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 356 | # This deals with one-liner docstring, for example, SBThread.GetName: |
| 357 | # """GetName(self) -> char""". |
| 358 | if one_liner_docstring_pattern.match(line): |
| 359 | line = char_to_str_xform(line) |
| 360 | |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 361 | # Look for 'def IsValid(*args):', and once located, add implementation |
| 362 | # of truth value testing for this object by delegation. |
| 363 | if isvalid_pattern.search(line): |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 364 | new_content.add_line(nonzero_def) |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 365 | |
Johnny Chen | 6ea16c7 | 2011-05-02 17:53:04 +0000 | [diff] [blame] | 366 | # Pass the original line of content to new_content. |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 367 | new_content.add_line(line) |
| 368 | |
| 369 | # We are finished with recording new content. |
| 370 | new_content.finish() |
| 371 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 372 | with open(output_name, 'w') as f_out: |
| 373 | f_out.write(new_content.getvalue()) |
| 374 | f_out.write("debugger_unique_id = 0\n") |
| 375 | f_out.write("SBDebugger.Initialize()\n") |