~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to addons/gdbplugin/localsview.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Description: Widget that local variables of the gdb inferior
 
3
//
 
4
// Copyright (c) 2010 Kåre Särs <kare.sars@iki.fi>
 
5
//
 
6
//  This library is free software; you can redistribute it and/or
 
7
//  modify it under the terms of the GNU Library General Public
 
8
//  License version 2 as published by the Free Software Foundation.
 
9
//
 
10
//  This library is distributed in the hope that it will be useful,
 
11
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
//  Library General Public License for more details.
 
14
//
 
15
//  You should have received a copy of the GNU Library General Public License
 
16
//  along with this library; see the file COPYING.LIB.  If not, write to
 
17
//  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
//  Boston, MA 02110-1301, USA.
 
19
 
 
20
#include "localsview.h"
 
21
#include <QLabel>
 
22
#include <QDebug>
 
23
#include <klocalizedstring.h>
 
24
 
 
25
 
 
26
LocalsView::LocalsView(QWidget *parent)
 
27
:   QTreeWidget(parent), m_allAdded(true)
 
28
{
 
29
    QStringList headers;
 
30
    headers << i18n("Symbol");
 
31
    headers << i18n("Value");
 
32
    setHeaderLabels(headers);
 
33
    setAutoScroll(false);
 
34
}
 
35
 
 
36
LocalsView::~LocalsView()
 
37
{
 
38
}
 
39
 
 
40
void LocalsView::showEvent(QShowEvent *)
 
41
{
 
42
    emit localsVisible(true);
 
43
}
 
44
 
 
45
void LocalsView::hideEvent(QHideEvent *)
 
46
{
 
47
    emit localsVisible(false);
 
48
}
 
49
 
 
50
void LocalsView::createWrappedItem(QTreeWidgetItem *parent, const QString &name, const QString &value)
 
51
{
 
52
    QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList(name));
 
53
    QLabel *label = new QLabel(value);
 
54
    label->setWordWrap(true);
 
55
    setItemWidget(item, 1, label);
 
56
    item->setData(1, Qt::UserRole, value);
 
57
}
 
58
 
 
59
void LocalsView::createWrappedItem(QTreeWidget *parent, const QString &name, const QString &value)
 
60
{
 
61
    QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList(name));
 
62
    QLabel *label = new QLabel(value);
 
63
    label->setWordWrap(true);
 
64
    setItemWidget(item, 1, label);
 
65
}
 
66
 
 
67
void LocalsView::addLocal(const QString &vString)
 
68
{
 
69
    static QRegExp isValue(QStringLiteral("(\\S*)\\s=\\s(.*)"));
 
70
    static QRegExp isStruct(QStringLiteral("\\{\\S*\\s=\\s.*"));
 
71
    static QRegExp isStartPartial(QStringLiteral("\\S*\\s=\\s\\S*\\s=\\s\\{"));
 
72
    static QRegExp isPrettyQList(QStringLiteral("\\s*\\[\\S*\\]\\s=\\s\\S*"));
 
73
    static QRegExp isPrettyValue(QStringLiteral("(\\S*)\\s=\\s(\\S*)\\s=\\s(.*)"));
 
74
    static QRegExp isThisValue(QStringLiteral("\\$\\d+"));
 
75
 
 
76
    if (m_allAdded) {
 
77
        clear();
 
78
        m_allAdded = false;
 
79
    }
 
80
    
 
81
    if (vString.isEmpty()) {
 
82
        m_allAdded = true;
 
83
        return;
 
84
    }
 
85
    if (isStartPartial.exactMatch(vString)) {
 
86
        m_local = vString;
 
87
        return;
 
88
    }
 
89
    if (isPrettyQList.exactMatch(vString)) {
 
90
        m_local += vString.trimmed();
 
91
        if (m_local.endsWith(QLatin1Char(','))) m_local += QLatin1Char(' ');
 
92
        return;
 
93
    }
 
94
    if (vString == QLatin1String("}")) {
 
95
        m_local += vString;
 
96
    }
 
97
 
 
98
    QStringList symbolAndValue;
 
99
    QString value;
 
100
    
 
101
    if (m_local.isEmpty()) {
 
102
        if (vString == QLatin1String("No symbol table info available.")) {
 
103
            return; /* this is not an error */
 
104
        }
 
105
        if (!isValue.exactMatch(vString)) {
 
106
            qDebug() << "Could not parse:" << vString;
 
107
            return;
 
108
        }
 
109
        symbolAndValue << isValue.cap(1);
 
110
        // check out for "print *this"
 
111
        if (isThisValue.exactMatch(symbolAndValue[0])) {
 
112
            symbolAndValue[0] = QLatin1String("*this");
 
113
        }
 
114
        value = isValue.cap(2);
 
115
    }
 
116
    else {
 
117
        if (!isPrettyValue.exactMatch(m_local)) {
 
118
            qDebug() << "Could not parse:" << m_local;
 
119
            m_local.clear();
 
120
            return;
 
121
        }
 
122
        symbolAndValue << isPrettyValue.cap(1) << isPrettyValue.cap(2);
 
123
        value = isPrettyValue.cap(3);
 
124
    }
 
125
 
 
126
    QTreeWidgetItem *item;
 
127
    if (value[0] == QLatin1Char('{')) {
 
128
        if (value[1] == QLatin1Char('{')) {
 
129
            item = new QTreeWidgetItem(this, symbolAndValue);
 
130
            addArray(item, value.mid(1, value.size()-2));
 
131
        }
 
132
        else {
 
133
            if (isStruct.exactMatch(value)) {
 
134
                item = new QTreeWidgetItem(this, symbolAndValue);
 
135
                addStruct(item, value.mid(1, value.size()-2));
 
136
            }
 
137
            else {
 
138
                createWrappedItem(this, symbolAndValue[0], value);
 
139
            }
 
140
        }
 
141
    }
 
142
    else {
 
143
        createWrappedItem(this, symbolAndValue[0], value);
 
144
    }
 
145
 
 
146
    m_local.clear();
 
147
}
 
148
 
 
149
 
 
150
void LocalsView::addStruct(QTreeWidgetItem *parent, const QString &vString)
 
151
{
 
152
    static QRegExp isArray(QStringLiteral("\\{\\.*\\s=\\s.*"));
 
153
    static QRegExp isStruct(QStringLiteral("\\.*\\s=\\s.*"));
 
154
    QTreeWidgetItem *item;
 
155
    QStringList symbolAndValue;
 
156
    QString subValue;
 
157
    int start = 0;
 
158
    int end;
 
159
    while (start < vString.size()) {
 
160
        // Symbol
 
161
        symbolAndValue.clear();
 
162
        end = vString.indexOf(QStringLiteral(" = "), start);
 
163
        if (end < 0) {
 
164
            // error situation -> bail out
 
165
            createWrappedItem(parent, QString(), vString.right(start));
 
166
            break;
 
167
        }
 
168
        symbolAndValue << vString.mid(start, end-start);
 
169
        //qDebug() << symbolAndValue;
 
170
        // Value
 
171
        start = end + 3;
 
172
        end = start;
 
173
        if (start < 0 || start >= vString.size()) {
 
174
            qDebug() << vString << start;
 
175
            break;
 
176
        }
 
177
        if (vString[start] == QLatin1Char('{')) {
 
178
            start++;
 
179
            end++;
 
180
            int count = 1;
 
181
            bool inComment = false;
 
182
            // search for the matching }
 
183
            while(end < vString.size()) {
 
184
                if (!inComment) {
 
185
                    if (vString[end] == QLatin1Char('"')) inComment = true;
 
186
                    else if (vString[end] == QLatin1Char('}')) count--;
 
187
                    else if (vString[end] == QLatin1Char('{')) count++;
 
188
                    if (count == 0) break;
 
189
                }
 
190
                else {
 
191
                    if ((vString[end] == QLatin1Char('"')) && (vString[end-1] != QLatin1Char('\\'))) {
 
192
                        inComment = false;
 
193
                    }
 
194
                }
 
195
                end++;
 
196
            }
 
197
            subValue = vString.mid(start, end-start);
 
198
            if (isArray.exactMatch(subValue)) {
 
199
                item = new QTreeWidgetItem(parent, symbolAndValue);
 
200
                addArray(item, subValue);
 
201
            }
 
202
            else if (isStruct.exactMatch(subValue)) {
 
203
                item = new QTreeWidgetItem(parent, symbolAndValue);
 
204
                addStruct(item, subValue);
 
205
            }
 
206
            else {
 
207
                createWrappedItem(parent, symbolAndValue[0], vString.mid(start, end-start));
 
208
            }
 
209
            start = end + 3; // },_
 
210
        }
 
211
        else {
 
212
            // look for the end of the value in the vString
 
213
            bool inComment = false;
 
214
            while(end < vString.size()) {
 
215
                if (!inComment) {
 
216
                    if (vString[end] == QLatin1Char('"')) inComment = true;
 
217
                    else if (vString[end] == QLatin1Char(',')) break;
 
218
                }
 
219
                else {
 
220
                    if ((vString[end] == QLatin1Char('"')) && (vString[end-1] != QLatin1Char('\\'))) {
 
221
                        inComment = false;
 
222
                    }
 
223
                }
 
224
                end++;
 
225
            }
 
226
            createWrappedItem(parent, symbolAndValue[0], vString.mid(start, end-start));
 
227
            start = end + 2; // ,_
 
228
        }
 
229
    }
 
230
}
 
231
 
 
232
void LocalsView::addArray(QTreeWidgetItem *parent, const QString &vString)
 
233
{
 
234
    // getting here we have this kind of string:
 
235
    // "{...}" or "{...}, {...}" or ...
 
236
    QTreeWidgetItem *item;
 
237
    int count = 1;
 
238
    bool inComment = false;
 
239
    int index = 0;
 
240
    int start = 1;
 
241
    int end = 1;
 
242
 
 
243
    while (end < vString.size()) {
 
244
        if (!inComment) {
 
245
            if (vString[end] == QLatin1Char('"')) inComment = true;
 
246
            else if (vString[end] == QLatin1Char('}')) count--;
 
247
            else if (vString[end] == QLatin1Char('{')) count++;
 
248
            if (count == 0) {
 
249
                QStringList name;
 
250
                name << QStringLiteral("[%1]").arg(index);
 
251
                index++;
 
252
                item = new QTreeWidgetItem(parent, name);
 
253
                addStruct(item, vString.mid(start, end-start));
 
254
                end += 4; // "}, {"
 
255
                start = end;
 
256
                count = 1;
 
257
            }
 
258
        }
 
259
        else {
 
260
            if ((vString[end] == QLatin1Char('"')) && (vString[end-1] != QLatin1Char('\\'))) {
 
261
                inComment = false;
 
262
            }
 
263
        }
 
264
        end++;
 
265
    }
 
266
}
 
267