2
// Description: Widget that local variables of the gdb inferior
4
// Copyright (c) 2010 Kåre Särs <kare.sars@iki.fi>
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.
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.
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.
20
#include "localsview.h"
23
#include <klocalizedstring.h>
26
LocalsView::LocalsView(QWidget *parent)
27
: QTreeWidget(parent), m_allAdded(true)
30
headers << i18n("Symbol");
31
headers << i18n("Value");
32
setHeaderLabels(headers);
36
LocalsView::~LocalsView()
40
void LocalsView::showEvent(QShowEvent *)
42
emit localsVisible(true);
45
void LocalsView::hideEvent(QHideEvent *)
47
emit localsVisible(false);
50
void LocalsView::createWrappedItem(QTreeWidgetItem *parent, const QString &name, const QString &value)
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);
59
void LocalsView::createWrappedItem(QTreeWidget *parent, const QString &name, const QString &value)
61
QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList(name));
62
QLabel *label = new QLabel(value);
63
label->setWordWrap(true);
64
setItemWidget(item, 1, label);
67
void LocalsView::addLocal(const QString &vString)
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+"));
81
if (vString.isEmpty()) {
85
if (isStartPartial.exactMatch(vString)) {
89
if (isPrettyQList.exactMatch(vString)) {
90
m_local += vString.trimmed();
91
if (m_local.endsWith(QLatin1Char(','))) m_local += QLatin1Char(' ');
94
if (vString == QLatin1String("}")) {
98
QStringList symbolAndValue;
101
if (m_local.isEmpty()) {
102
if (vString == QLatin1String("No symbol table info available.")) {
103
return; /* this is not an error */
105
if (!isValue.exactMatch(vString)) {
106
qDebug() << "Could not parse:" << vString;
109
symbolAndValue << isValue.cap(1);
110
// check out for "print *this"
111
if (isThisValue.exactMatch(symbolAndValue[0])) {
112
symbolAndValue[0] = QLatin1String("*this");
114
value = isValue.cap(2);
117
if (!isPrettyValue.exactMatch(m_local)) {
118
qDebug() << "Could not parse:" << m_local;
122
symbolAndValue << isPrettyValue.cap(1) << isPrettyValue.cap(2);
123
value = isPrettyValue.cap(3);
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));
133
if (isStruct.exactMatch(value)) {
134
item = new QTreeWidgetItem(this, symbolAndValue);
135
addStruct(item, value.mid(1, value.size()-2));
138
createWrappedItem(this, symbolAndValue[0], value);
143
createWrappedItem(this, symbolAndValue[0], value);
150
void LocalsView::addStruct(QTreeWidgetItem *parent, const QString &vString)
152
static QRegExp isArray(QStringLiteral("\\{\\.*\\s=\\s.*"));
153
static QRegExp isStruct(QStringLiteral("\\.*\\s=\\s.*"));
154
QTreeWidgetItem *item;
155
QStringList symbolAndValue;
159
while (start < vString.size()) {
161
symbolAndValue.clear();
162
end = vString.indexOf(QStringLiteral(" = "), start);
164
// error situation -> bail out
165
createWrappedItem(parent, QString(), vString.right(start));
168
symbolAndValue << vString.mid(start, end-start);
169
//qDebug() << symbolAndValue;
173
if (start < 0 || start >= vString.size()) {
174
qDebug() << vString << start;
177
if (vString[start] == QLatin1Char('{')) {
181
bool inComment = false;
182
// search for the matching }
183
while(end < vString.size()) {
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;
191
if ((vString[end] == QLatin1Char('"')) && (vString[end-1] != QLatin1Char('\\'))) {
197
subValue = vString.mid(start, end-start);
198
if (isArray.exactMatch(subValue)) {
199
item = new QTreeWidgetItem(parent, symbolAndValue);
200
addArray(item, subValue);
202
else if (isStruct.exactMatch(subValue)) {
203
item = new QTreeWidgetItem(parent, symbolAndValue);
204
addStruct(item, subValue);
207
createWrappedItem(parent, symbolAndValue[0], vString.mid(start, end-start));
209
start = end + 3; // },_
212
// look for the end of the value in the vString
213
bool inComment = false;
214
while(end < vString.size()) {
216
if (vString[end] == QLatin1Char('"')) inComment = true;
217
else if (vString[end] == QLatin1Char(',')) break;
220
if ((vString[end] == QLatin1Char('"')) && (vString[end-1] != QLatin1Char('\\'))) {
226
createWrappedItem(parent, symbolAndValue[0], vString.mid(start, end-start));
227
start = end + 2; // ,_
232
void LocalsView::addArray(QTreeWidgetItem *parent, const QString &vString)
234
// getting here we have this kind of string:
235
// "{...}" or "{...}, {...}" or ...
236
QTreeWidgetItem *item;
238
bool inComment = false;
243
while (end < vString.size()) {
245
if (vString[end] == QLatin1Char('"')) inComment = true;
246
else if (vString[end] == QLatin1Char('}')) count--;
247
else if (vString[end] == QLatin1Char('{')) count++;
250
name << QStringLiteral("[%1]").arg(index);
252
item = new QTreeWidgetItem(parent, name);
253
addStruct(item, vString.mid(start, end-start));
260
if ((vString[end] == QLatin1Char('"')) && (vString[end-1] != QLatin1Char('\\'))) {