aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/valgrind/xmlprotocol/errorlistmodel.cpp
blob: 96bf9023eec9240f598d7c5b89b213b4fc49ef65 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "errorlistmodel.h"
#include "error.h"
#include "frame.h"
#include "stack.h"
#include "../valgrindtr.h"

#include <debugger/analyzer/diagnosticlocation.h>

#include <utils/qtcassert.h>

#include <QDir>
#include <QList>

namespace Valgrind::XmlProtocol {

class ErrorItem : public Utils::TreeItem
{
public:
    ErrorItem(const ErrorListModel *model, const Error &error);

    const ErrorListModel *modelPrivate() const { return m_model; }
    Error error() const { return m_error; }

private:
    QVariant data(int column, int role) const override;

    const ErrorListModel * const m_model;
    const Error m_error;
};

class StackItem : public Utils::TreeItem
{
public:
    StackItem(const Stack &stack);

private:
    QVariant data(int column, int role) const override;

    const ErrorItem *getErrorItem() const;

    const Stack m_stack;
};

class FrameItem : public Utils::TreeItem
{
public:
    FrameItem(const Frame &frame);

private:
    QVariant data(int column, int role) const override;

    const ErrorItem *getErrorItem() const;

    const Frame m_frame;
};


ErrorListModel::ErrorListModel(QObject *parent)
    : Utils::TreeModel<>(parent)
{
    setHeader({Tr::tr("Issue"), Tr::tr("Location")});
}

Frame ErrorListModel::findRelevantFrame(const Error &error) const
{
    if (m_relevantFrameFinder)
        return m_relevantFrameFinder(error);
    const QList<Stack> stacks = error.stacks();
    if (stacks.isEmpty())
        return Frame();
    const Stack &stack = stacks[0];
    const QList<Frame> frames = stack.frames();
    if (!frames.isEmpty())
        return frames.first();
    return Frame();
}

static QString makeFrameName(const Frame &frame, bool withLocation)
{
    const QString d = frame.directory();
    const QString f = frame.fileName();
    const QString fn = frame.functionName();
    const QString fullPath = frame.filePath();

    QString path;
    if (!d.isEmpty() && !f.isEmpty())
        path = fullPath;
    else
        path = frame.object();

    if (QFileInfo::exists(path))
        path = QFileInfo(path).canonicalFilePath();

    if (frame.line() != -1)
        path += ':' + QString::number(frame.line());

    if (!fn.isEmpty()) {
        const QString location = withLocation || path == frame.object()
                ? QString::fromLatin1(" in %2").arg(path) : QString();
        return Tr::tr("%1%2").arg(fn, location);
    }
    if (!path.isEmpty())
        return path;
    return QString::fromLatin1("0x%1").arg(frame.instructionPointer(), 0, 16);
}

QString ErrorListModel::errorLocation(const Error &error) const
{
    return Tr::tr("in %1").arg(makeFrameName(findRelevantFrame(error), true));
}

void ErrorListModel::addError(const Error &error)
{
    rootItem()->appendChild(new ErrorItem(this, error));
}

ErrorListModel::RelevantFrameFinder ErrorListModel::relevantFrameFinder() const
{
    return m_relevantFrameFinder;
}

void ErrorListModel::setRelevantFrameFinder(const RelevantFrameFinder &relevantFrameFinder)
{
    m_relevantFrameFinder = relevantFrameFinder;
}


ErrorItem::ErrorItem(const ErrorListModel *model, const Error &error)
    : m_model(model), m_error(error)
{
    QTC_ASSERT(!m_error.stacks().isEmpty(), return);

    // If there's more than one stack, we simply map the real tree structure.
    // Otherwise, we skip the stack level, which has no useful information and would
    // just annoy the user.
    // The same goes for the frame level.
    if (m_error.stacks().count() > 1) {
        const QList<Stack> stacks = m_error.stacks();
        for (const Stack &s : stacks)
            appendChild(new StackItem(s));
    } else if (m_error.stacks().constFirst().frames().count() > 1) {
        const QList<Frame> frames = m_error.stacks().constFirst().frames();
        for (const Frame &f : frames)
            appendChild(new FrameItem(f));
    }
}

static QVariant locationData(int role, const Frame &frame)
{
    const Debugger::DiagnosticLocation location(Utils::FilePath::fromString(frame.filePath()),
                                                frame.line(),
                                                0);
    return Debugger::DetailedErrorView::locationData(role, location);
}

QVariant ErrorItem::data(int column, int role) const
{
    if (column == Debugger::DetailedErrorView::LocationColumn)
        return locationData(role, m_model->findRelevantFrame(m_error));

    // DiagnosticColumn
    switch (role) {
    case Debugger::DetailedErrorView::FullTextRole: {
        QString content;
        QTextStream stream(&content);

        stream << m_error.what() << "\n";
        stream << "  "
               << m_model->errorLocation(m_error)
               << "\n";

        const QList<Stack> stacks = m_error.stacks();
        for (const Stack &stack : stacks) {
            if (!stack.auxWhat().isEmpty())
                stream << stack.auxWhat();
            int i = 1;
            const QList<Frame> frames = stack.frames();
            for (const Frame &frame : frames)
                stream << "  " << i++ << ": " << makeFrameName(frame, true) << "\n";
        }

        stream.flush();
        return content;
    }
    case ErrorListModel::ErrorRole:
        return QVariant::fromValue<Error>(m_error);
    case Qt::DisplayRole:
        // If and only if there is exactly one frame, we have omitted creating a child item for it
        // (see the constructor) and display the function name in the error item instead.
        if (m_error.stacks().count() != 1 || m_error.stacks().constFirst().frames().count() != 1
                || m_error.stacks().constFirst().frames().constFirst().functionName().isEmpty()) {
            return m_error.what();
        }
        return Tr::tr("%1 in function %2")
                .arg(m_error.what(), m_error.stacks().constFirst().frames().constFirst().functionName());
    case Qt::ToolTipRole:
        return m_model->findRelevantFrame(m_error).toolTip();
    default:
        return QVariant();
    }
}


StackItem::StackItem(const Stack &stack) : m_stack(stack)
{
    const QList<Frame> frames = m_stack.frames();
    for (const Frame &f : frames)
        appendChild(new FrameItem(f));
}

QVariant StackItem::data(int column, int role) const
{
    const ErrorItem * const errorItem = getErrorItem();
    if (column == Debugger::DetailedErrorView::LocationColumn)
        return locationData(role, errorItem->modelPrivate()->findRelevantFrame(errorItem->error()));

    // DiagnosticColumn
    switch (role) {
    case ErrorListModel::ErrorRole:
        return QVariant::fromValue(errorItem->error());
    case Qt::DisplayRole:
        return m_stack.auxWhat().isEmpty() ? errorItem->error().what() : m_stack.auxWhat();
    case Qt::ToolTipRole:
        return errorItem->modelPrivate()->findRelevantFrame(errorItem->error()).toolTip();
    default:
        return QVariant();
    }
}

const ErrorItem *StackItem::getErrorItem() const
{
    return static_cast<ErrorItem *>(parent());
}


FrameItem::FrameItem(const Frame &frame) : m_frame(frame)
{
}

QVariant FrameItem::data(int column, int role) const
{
    if (column == Debugger::DetailedErrorView::LocationColumn)
        return locationData(role, m_frame);

    // DiagnosticColumn
    switch (role) {
    case ErrorListModel::ErrorRole:
        return QVariant::fromValue(getErrorItem()->error());
    case Qt::DisplayRole: {
        const int row = indexInParent() + 1;
        const int padding = static_cast<int>(std::log10(parent()->childCount()))
                - static_cast<int>(std::log10(row));
        return QString::fromLatin1("%1%2: %3")
                .arg(QString(padding, ' '))
                .arg(row)
                .arg(makeFrameName(m_frame, false));
    }
    case Qt::ToolTipRole:
        return m_frame.toolTip();
    default:
        return QVariant();
    }
}

const ErrorItem *FrameItem::getErrorItem() const
{
    for (const TreeItem *parentItem = parent(); parentItem; parentItem = parentItem->parent()) {
        auto const errorItem = dynamic_cast<const ErrorItem *>(parentItem);
        if (errorItem)
            return errorItem;
    }
    QTC_CHECK(false);
    return nullptr;
}

} // namespace Valgrind::XmlProtocol