aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmldom/qqmldomerrormessage.cpp
blob: e0e9448cc4ad8156dfe32764b8631949526652c3 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**/
#include "qqmldomerrormessage_p.h"
#include "qqmldomitem_p.h"
#include "qqmldomstringdumper_p.h"

#include <QtCore/QCborMap>
#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>

QT_BEGIN_NAMESPACE

namespace QQmlJS {
namespace Dom {

enum {
    FatalMsgMaxLen=511
};

/*!
\internal
\macro NewErrorGroup

\param groupId a double qouted string giving the groupId for this group

\brief convenience macro creating a new ErrorGroup and registering its groupId as translatable string
*/

/*!
\internal
\class QQmlJS::Dom::ErrorGroup
\brief Represents a tag grouping a set of related error messages, it can be used to disable them

Every group has a unique string identifying it (the \l{groupId}), and it should be a string that can
be translated to get the local name. The best way to acheive this is to create new groups using
the NewErrorGroup macro.
 */
void ErrorGroup::dump(Sink sink) const
{
    sink(u"[");
    sink(groupName());
    sink(u"]");
}

void ErrorGroup::dumpId(Sink sink) const
{
    sink(u"[");
    sink(QString(groupId()));
    sink(u"]");
}

QLatin1String ErrorGroup::groupId() const
{
    return QLatin1String(m_groupId);
}

QString ErrorGroup::groupName() const
{
    return tr(m_groupId);
}

/*!
\internal
\class QQmlJS::Dom::ErrorGroups
\brief Represents a set of tags grouping a set of related error messages

The simplest way to create new ErrorMessages is to have an ErrorGroups instance,
and use it to create new ErrorMessages using its debug, warning, error,... methods
 */

void ErrorGroups::dump(Sink sink) const
{
    for (int i = 0; i < groups.length(); ++i)
        groups.at(i).dump(sink);
}

void ErrorGroups::dumpId(Sink sink) const
{
    for (int i = 0; i < groups.length(); ++i)
        groups.at(i).dumpId(sink);
}

QCborArray ErrorGroups::toCbor() const
{
    QCborArray res;
    for (int i = 0; i < groups.length(); ++i)
        res.append(QCborValue(groups.at(i).groupId()));
    return res;
}

/*!
\internal
\class QQmlJS::Dom::ErrorMessage
\brief Represents an error message connected to the dom

The error messages *should* be translated, but they do not need to be pre registered.
To give a meaningful handling of error messages ErrorMessages have "tags" (ErrorGroup) that are
grouped toghether in ErrorGroups.

To create an ErrorMessage from scratch the best way is to use one of the methods provided by
an ErrorGroups object.
For example create an ErrorGroups called myErrors and use it to create all your errors.
\code
static ErrorGroups myErrors(){
    static ErrorGroups res({NewErrorGroup("StaticAnalysis"), NewErrorGroup("FancyDetector")});
    return res;
}
\endcode

You can  preregister the errors giving them a unique name (reverse dns notation is encouraged) with
the msg function.
This unique name (errorId) is a const char* (QLatin1String) to integrate better with the tr function.
Ideally you create variables to store the errorId either by creating variables with plain strings
that you use to initialize the error messages
\code
// in .h file
constexpr const char *myError0 = "my.company.error0";
// in some initialization function
ErrorMessage::msg(myError0, myErrors().warning(tr("Error number 0")));
\endcode
or using the result of the msg function
\code
// in cpp file
static auto myError1 = ErrorMessage::msg("my.company.error1", myErrors().warning(tr("Error number 1")));
static auto myError2 = ErrorMessage::msg("my.company.error2", myErrors().error(tr("Error number 2 on %1")));
\endcode
and then use them like this
\code
ErrorMessage::load(myError2, QLatin1String("extra info")).handle(errorHandler);
\endcode
or using directly the string (more error prone)
\code
errorHandler(ErrorMessage::load(QLatin1String("my.company.error1")));
\endcode

The \l{withItem} method can be used to set the path file and location if not aready set.
 */

ErrorMessage ErrorGroups::errorMessage(Dumper msg, ErrorLevel level, Path element, QString canonicalFilePath, SourceLocation location) const
{
    if (level == ErrorLevel::Fatal)
        fatal(msg, element, canonicalFilePath, location);
    return ErrorMessage(dumperToString(msg), *this, level, element, canonicalFilePath, location);
}

ErrorMessage ErrorGroups::errorMessage(const DiagnosticMessage &msg, Path element, QString canonicalFilePath) const
{
    ErrorMessage res(*this, msg, element, canonicalFilePath);
    if (!res.location.isValid() && (res.location.startLine != 0 || res.location.startColumn != 0)) {
        res.location.offset = -1;
        res.location.length = 1;
    }
    return res;
}

void ErrorGroups::fatal(Dumper msg, Path element, QStringView canonicalFilePath, SourceLocation location) const
{
    enum { FatalMsgMaxLen = 1023 };
    char buf[FatalMsgMaxLen+1];
    int ibuf = 0;
    auto sink = [&ibuf, &buf](QStringView s) {
        int is = 0;
        while (ibuf < FatalMsgMaxLen && is < s.length()) {
            QChar c = s.at(is);
            if (c == QChar::fromLatin1('\n') || c == QChar::fromLatin1('\r') || (c >= QChar::fromLatin1(' ') && c <= QChar::fromLatin1('~')))
                buf[ibuf++] = c.toLatin1();
            else
                buf[ibuf++] = '~';
            ++is;
        }
    };
    if (!canonicalFilePath.isEmpty()) {
        sink(canonicalFilePath);
        sink(u":");
    }
    if (location.length) {
        sinkInt(sink, location.startLine);
        sink(u":");
        sinkInt(sink, location.startColumn);
        sink(u":");
    }
    dump(sink);
    msg(sink);
    if (element.length()>0) {
        sink(u" for ");
        element.dump(sink);
    }
    buf[ibuf] = 0;
    qFatal("%s", buf);
}

ErrorMessage ErrorGroups::debug(QString message) const
{
    return ErrorMessage(message, *this, ErrorLevel::Debug);
}

ErrorMessage ErrorGroups::debug(Dumper message) const
{
    return ErrorMessage(dumperToString(message), *this, ErrorLevel::Debug);
}

ErrorMessage ErrorGroups::info(QString message) const
{
    return ErrorMessage(message, *this, ErrorLevel::Info);
}

ErrorMessage ErrorGroups::info(Dumper message) const
{
    return ErrorMessage(dumperToString(message), *this, ErrorLevel::Info);
}

ErrorMessage ErrorGroups::hint(QString message) const
{
    return ErrorMessage(message, *this, ErrorLevel::Hint);
}

ErrorMessage ErrorGroups::hint(Dumper message) const
{
    return ErrorMessage(dumperToString(message), *this, ErrorLevel::Hint);
}

ErrorMessage ErrorGroups::maybeWarning(QString message) const
{
    return ErrorMessage(message, *this, ErrorLevel::MaybeWarning);
}

ErrorMessage ErrorGroups::maybeWarning(Dumper message) const
{
    return ErrorMessage(dumperToString(message), *this, ErrorLevel::MaybeWarning);
}

ErrorMessage ErrorGroups::warning(QString message) const
{
    return ErrorMessage(message, *this, ErrorLevel::Warning);
}

ErrorMessage ErrorGroups::warning(Dumper message) const
{
    return ErrorMessage(dumperToString(message), *this, ErrorLevel::Warning);
}

ErrorMessage ErrorGroups::maybeError(QString message) const
{
    return ErrorMessage(message, *this, ErrorLevel::MaybeError);
}

ErrorMessage ErrorGroups::maybeError(Dumper message) const
{
    return ErrorMessage(dumperToString(message), *this, ErrorLevel::MaybeError);
}

ErrorMessage ErrorGroups::error(QString message) const
{
    return ErrorMessage(message, *this, ErrorLevel::Error);
}

ErrorMessage ErrorGroups::error(Dumper message) const
{
    return ErrorMessage(dumperToString(message), *this, ErrorLevel::Error);
}

int ErrorGroups::cmp(const ErrorGroups &o1, const ErrorGroups &o2)
{
    auto &g1 = o1.groups;
    auto &g2 = o2.groups;
    if (g1.length() < g2.length())
        return -1;
    if (g1.length() < g2.length())
        return 1;
    for (int i = 0; i < g1.length(); ++i) {
        int c = std::strcmp(g1.at(i).groupId().data(), g2.at(i).groupId().data());
        if (c != 0)
            return c;
    }
    return 0;
}

ErrorMessage::ErrorMessage(QString msg, ErrorGroups errorGroups, Level level, Path element, QString canonicalFilePath, SourceLocation location, QLatin1String errorId):
    errorId(errorId), message(msg), errorGroups(errorGroups), level(level), path(element), file(canonicalFilePath), location(location)
{
    if (level == Level::Fatal) // we should not end up here, it should have been handled at a higher level already
        errorGroups.fatal(msg, element, canonicalFilePath, location);
}

ErrorMessage::ErrorMessage(ErrorGroups errorGroups, const DiagnosticMessage &msg, Path element,
                           QString canonicalFilePath, QLatin1String errorId):
    errorId(errorId), message(msg.message), errorGroups(errorGroups),
    level(errorLevelFromQtMsgType(msg.type)), path(element), file(canonicalFilePath), location(msg.loc)
{
    if (level == Level::Fatal) // we should not end up here, it should have been handled at a higher level already
        errorGroups.fatal(msg.message, element, canonicalFilePath, location);
}


static QBasicMutex *registryMutex()
{
    static QBasicMutex rMutex{};
    return &rMutex;
}


static ErrorGroups myErrors()
{
    static ErrorGroups g = {{NewErrorGroup("ErrorMessage")}};
    return g;
}

struct StorableMsg {
    StorableMsg():
        msg(QStringLiteral(u"dummy"), myErrors(), ErrorLevel::Error)
    {}

    StorableMsg(const ErrorMessage &e):
        msg(e)
    {}

    ErrorMessage msg;
};

static QHash<QLatin1String, StorableMsg> &registry()
{
    static QHash<QLatin1String, StorableMsg> r;
    return r;
}

QLatin1String ErrorMessage::msg(const char *errorId, ErrorMessage err)
{
    return msg(QLatin1String(errorId), err);
}

QLatin1String ErrorMessage::msg(QLatin1String errorId, ErrorMessage err)
{
    bool doubleRegister = false;
    ErrorMessage old = myErrors().debug(u"dummy");
    {
        QMutexLocker l(registryMutex());
        auto &r = registry();
        if (r.contains(err.errorId)) {
            old = r[err.errorId].msg;
            doubleRegister = true;
        }
        r[errorId] = StorableMsg{err.withErrorId(errorId)};
    }
    if (doubleRegister)
        defaultErrorHandler(myErrors().warning(tr("Double registration of error %1: (%2) vs (%3)").arg(errorId, err.withErrorId(errorId).toString(), old.toString())));
    return errorId;
}

void ErrorMessage::visitRegisteredMessages(std::function<bool (ErrorMessage)> visitor)
{
    QHash<QLatin1String, StorableMsg> r;
    {
        QMutexLocker l(registryMutex());
        r = registry();
    }
    auto it = r.cbegin();
    auto end = r.cend();
    while (it != end) {
        visitor(it->msg);
        ++it;
    }
}

ErrorMessage ErrorMessage::load(QLatin1String errorId)
{
    ErrorMessage res = myErrors().error([errorId](Sink s){
            s(u"Unregistered error ");
            s(QString(errorId)); });
    {
        QMutexLocker l(registryMutex());
        res = registry().value(errorId,res).msg;
    }
    return res;
}

ErrorMessage ErrorMessage::load(const char *errorId)
{
    return load(QLatin1String(errorId));
}

ErrorMessage &ErrorMessage::withErrorId(QLatin1String errorId)
{
    this->errorId = errorId;
    return *this;
}

ErrorMessage &ErrorMessage::withPath(const Path &path)
{
    this->path = path;
    return *this;
}

ErrorMessage &ErrorMessage::withFile(QString f)
{
    file=f;
    return *this;
}

ErrorMessage &ErrorMessage::withFile(QStringView f)
{
    file = f.toString();
    return *this;
}

ErrorMessage &ErrorMessage::withLocation(SourceLocation loc)
{
    location = loc;
    return *this;
}

ErrorMessage &ErrorMessage::withItem(DomItem el)
{
    if (path.length() == 0)
        path = el.canonicalPath();
    if (file.isEmpty())
        file = el.canonicalFilePath();
    if (!location.isValid())
        location = el.location();
    return *this;
}

ErrorMessage ErrorMessage::handle(const ErrorHandler &errorHandler)
{
    if (errorHandler)
        errorHandler(*this);
    else
        defaultErrorHandler(*this);
    return *this;
}

void ErrorMessage::dump(Sink sink) const
{
    if (!file.isEmpty()) {
        sink(file);
        sink(u":");
    }
    if (location.length) {
        sinkInt(sink, location.startLine);
        sink(u":");
        sinkInt(sink, location.startColumn);
        sink(u": ");
    }
    errorGroups.dump(sink);
    sink(u" ");
    dumpErrorLevel(sink, level);
    if (! errorId.isEmpty()) {
        sink(u" ");
        sink(QString(errorId));
    }
    sink(u": ");
    sink(message);
    if (path.length()>0) {
        sink(u" for ");
        path.dump(sink);
    }
}

QString ErrorMessage::toString() const
{
    return dumperToString([this](Sink sink){ this->dump(sink); });
}

QCborMap ErrorMessage::toCbor() const
{
    return QCborMap({
                        {QStringLiteral(u"errorId"),errorId},
                        {QStringLiteral(u"message"), message},
                        {QStringLiteral(u"errorGroups"), errorGroups.toCbor()},
                        {QStringLiteral(u"level"), int(level)},
                        {QStringLiteral(u"path"), path.toString()},
                        {QStringLiteral(u"file"), file},
                        {QStringLiteral(u"location"), QCborMap({
                             {QStringLiteral(u"offset"),location.offset},
                             {QStringLiteral(u"length"),location.length},
                             {QStringLiteral(u"startLine"),location.startLine},
                             {QStringLiteral(u"startColumn"),location.startColumn}})}
                    });
}

/*!
 * \internal
 * \brief writes an ErrorMessage to QDebug
 * \param error the error to write
 */
void errorToQDebug(const ErrorMessage &error)
{
    dumperToQDebug([&error](Sink s){ error.dump(s); }, error.level);
}

/*!
 * \internal
 * \brief Error handler that ignores all errors (excluding fatal ones)
 */
void silentError(const ErrorMessage &)
{
}

void errorHandlerHandler(const ErrorMessage &msg, ErrorHandler *h = nullptr)
{
    static ErrorHandler handler = &errorToQDebug;
    if (h) {
        handler = *h;
    } else {
        handler(msg);
    }
}

/*!
 * \internal
 * \brief Calls the default error handler (by default errorToQDebug)
 */
void defaultErrorHandler(const ErrorMessage &error)
{
    errorHandlerHandler(error);
}

/*!
 * \internal
 * \brief Sets the default error handler
 */
void setDefaultErrorHandler(ErrorHandler h)
{
    errorHandlerHandler(ErrorMessage(QString(), ErrorGroups({})), &h);
}

ErrorLevel errorLevelFromQtMsgType(QtMsgType msgType)
{
    switch (msgType) {
    case QtFatalMsg:
        return ErrorLevel::Fatal;
    case QtCriticalMsg:
        return ErrorLevel::Error;
    case QtWarningMsg:
        return ErrorLevel::Warning;
    case QtInfoMsg:
        return ErrorLevel::Info;
    case QtDebugMsg:
        return ErrorLevel::Debug;
    default:
        return ErrorLevel::Error;
    }
}

} // end namespace Dom
} // end namespace QQmlJS

QT_END_NAMESPACE