summaryrefslogtreecommitdiff
path: root/solenv/gdb/libreoffice/sw.py
blob: 7a5ce193684b2de81e67cd45e9d8356383bfe902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import six
import gdb
from libreoffice.util import printing

class SwPositionPrinter(object):
    '''Prints SwPosition.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        node = self.value['nNode']['m_pNode'].dereference();
        block = node['m_pBlock'].dereference();
        nodeindex = block['nStart'] + node['m_nOffset']
        offset = self.value['nContent']['m_nIndex']
        return "%s (node %d, offset %d)" % (self.typename, nodeindex, offset)

class SwNodeIndexPrinter(object):
    '''Prints SwNodeIndex.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        node = self.value['m_pNode'].dereference();
        block = node['m_pBlock'].dereference();
        nodeindex = block['nStart'] + node['m_nOffset']
        return "%s (node %d)" % (self.typename, nodeindex)

class SwIndexPrinter(object):
    '''Prints SwIndex.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        offset = self.value['m_nIndex']
        return "%s (offset %d)" % (self.typename, offset)

class SwPaMPrinter(object):
    '''Prints SwPaM.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        return "%s" % (self.typename)

    def children(self):
        next_ = self.value['m_pNext']
        prev  = self.value['m_pPrev']
        point = self.value['m_pPoint'].dereference()
        mark = self.value['m_pMark'].dereference()
        children = [ ( 'point', point), ( 'mark', mark ) ]
        if next_ != self.value.address:
            children.append(("next", next_))
        if prev != self.value.address:
            children.append(("prev", prev))
        return children.__iter__()

# apparently the purpose of this is to suppress printing all the extra members
# that SwCursor and SwUnoCursor add
class SwUnoCursorPrinter(SwPaMPrinter):
    '''Prints SwUnoCursor.'''

class SwRectPrinter(object):
    '''Prints SwRect.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        return "%s" % (self.typename)

    def children(self):
        point = self.value['m_Point']
        size = self.value['m_Size']
        children = [ ( 'point', point), ( 'size', size ) ]
        return children.__iter__()

class MarkBasePrinter(object):
    '''Prints sw::mark::MarkBase.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        return "%s" % (self.typename)

    def children(self):
        m = self.value.cast(self.value.dynamic_type)
        return [ ( v, m[ v ] )
            for v in ( 'm_aName', 'm_pPos1', 'm_pPos2' ) ].__iter__()

class SwXTextRangeImplPrinter(object):
    '''Prints SwXTextRange::Impl.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        return "%s" % (self.typename)

    def children(self):
        mark = self.value['m_pMark'].dereference()
        children = [('mark', mark)]
        return children.__iter__()

class SwXTextCursorImplPrinter(object):
    '''Prints SwXTextCursor::Impl.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        return "%s" % (self.typename)

    def children(self):
        cursor = self.value['m_pUnoCursor']["m_pCursor"]["_M_ptr"]
        registeredIn = cursor.dereference()
        children = [('m_pUnoCursor', registeredIn)]
        return children.__iter__()

class SwUnoImplPtrPrinter(object):
    """Prints sw::UnoImplPtr"""

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        if self.value['m_p']:
            return "%s %s" % (self.typename, self.value['m_p'].dereference())
        else:
            return "empty %s" % (self.typename,)

class SwXTextRangePrinter(object):
    '''Prints SwXTextRange.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        return "%s %s" % (self.typename, self.value['m_pImpl'])

class SwXTextCursorPrinter(object):
    '''Prints SwXTextCursor.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        return "%s %s" % (self.typename, self.value['m_pImpl'])

class BigPtrArrayPrinter(object):
    '''Prints BigPtrArray.'''

    def __init__(self, typename, value):
        self.typename = typename
        self.value = value

    def to_string(self):
        length = self.value['m_nSize']
        if length > 0:
            return "%s of length %d" % (self.typename, length)
        else:
            return "empty %s" % self.typename

    def children(self):
        return self._iterator(self.value)

    def display_hint(self):
        return 'array'


    class _iterator(six.Iterator):

        def __init__(self, array):
            # libstdc++ unique_ptr is a std::tuple which contains multiple
            # _M_head_impl members and gdb may pick the wrong one by default
            # so have to manually cast it to the one that contains the array
            self.blocks = array['m_ppInf']['_M_t']['_M_t'].cast(gdb.lookup_type("std::_Head_base<0, BlockInfo**, false>"))['_M_head_impl']
            self.count = array['m_nSize']
            self.pos = 0
            self.block_count = array['m_nBlock']
            self.block_pos = 0
            self.block = None
            self.indent = ""
            self.max_indent = "        "
            self._next_block(False)
            self._check_invariant()

        def __iter__(self):
            return self

        def _node_value(self, node):
            cur_indent = self.indent
            if str(node.dynamic_type.target()) == "SwTextNode":
                # accessing this is completely non-obvious...
                # also, node.dynamic_cast(node.dynamic_type) is null?
                value = "    TextNode " + \
                  six.text_type(node.cast(node.dynamic_type).dereference()['m_Text'])
            elif str(node.dynamic_type.target()) == "SwOLENode":
                value = "     OLENode "
            elif str(node.dynamic_type.target()) == "SwGrfNode":
                value = "     GrfNode "
            elif str(node.dynamic_type.target()) == "SwSectionNode":
                value = " SectionNode "
                self.indent += " "
            elif str(node.dynamic_type.target()) == "SwTableNode":
                value = "   TableNode "
                self.indent += " "
            elif str(node.dynamic_type.target()) == "SwStartNode":
                value = "   StartNode "
                self.indent += " "
            elif str(node.dynamic_type.target()) == "SwEndNode":
                value = "     EndNode "
                self.indent = self.indent[:-1]
                cur_indent = self.indent
            elif str(node.dynamic_type.target()) == "SwDummySectionNode":
                value = "DummySctNode "
            else: # must be currently being deleted, so has some abstract type
                value = "~DeletedNode "
#            return "\n[%s%4d%s] %s %s" % (cur_indent, self.pos, \
#                                self.max_indent[len(cur_indent):], node, value)
            return "\n[%4d] %s%s%s %s" % (self.pos, cur_indent, \
                                node, self.max_indent[len(cur_indent):], value)

        def __next__(self):
            if self.pos == self.count:
                raise StopIteration()

            name = str(self.pos)
            node = self.block['mvData']['_M_elems'][self.pos - self.block['nStart']]
            value =  self._node_value(node)
            if self.pos == self.block['nEnd']:
                self._next_block()
            self.pos += 1

            self._check_invariant()
            return (name, value)

        def _next_block(self, advance = True):
            if advance:
                self.block_pos += 1

            if self.block_pos == self.block_count:
                return

            pblock = self.blocks[self.block_pos]
            assert pblock
            block = pblock.dereference()
            start = block['nStart']
            end = block['nEnd']
            assert end - start + 1 == block['nElem']
            if self.block:
                assert start == self.block['nEnd'] + 1
                assert end <= self.count
            else:
                assert start == 0
            self.block = block

        def _check_invariant(self):
            assert self.pos <= self.count
            assert self.block_pos <= self.block_count
            if self.pos == 0 and self.pos < self.count:
                assert self.block != None

printer = None

def build_pretty_printers():
    global printer

    printer = printing.Printer("libreoffice/sw")
    printer.add('BigPtrArray', BigPtrArrayPrinter)
    printer.add('SwPosition', SwPositionPrinter)
    printer.add('SwNodeIndex', SwNodeIndexPrinter)
    printer.add('SwIndex', SwIndexPrinter)
    printer.add('SwPaM', SwPaMPrinter)
    printer.add('SwUnoCursor', SwUnoCursorPrinter)
    printer.add('SwRect', SwRectPrinter)
    printer.add('sw::mark::Bookmark', MarkBasePrinter)
    printer.add('sw::mark::MarkBase', MarkBasePrinter)
    printer.add('sw::mark::UnoMark', MarkBasePrinter)
    printer.add('sw::mark::IMark', MarkBasePrinter)
    printer.add('SwXTextRange::Impl', SwXTextRangeImplPrinter)
    printer.add('sw::UnoImplPtr', SwUnoImplPtrPrinter)
    printer.add('SwXTextRange', SwXTextRangePrinter)
    printer.add('SwXTextCursor::Impl', SwXTextCursorImplPrinter)
    printer.add('SwXTextCursor', SwXTextCursorPrinter)

def register_pretty_printers(obj):
    printing.register_pretty_printer(printer, obj)

build_pretty_printers()

# vim:set shiftwidth=4 softtabstop=4 expandtab: