summaryrefslogtreecommitdiffstats
path: root/src/tools/moc/parser.cpp
blob: 1cfb8ce48688e873789ddc6a7937f6e6d83d20f2 (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
// 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 "parser.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>

QT_BEGIN_NAMESPACE

static const char *error_msg = nullptr;

/*! \internal
    Base implementation for printing diagnostic messages.

    For example:
        "/path/to/file:line:column: error: %s\n"
        '%s' is replaced by \a msg. (Currently "column" is always 1).

    If sym.lineNum is -1, the line and column parts aren't printed:
        "/path/to/file: error: %s\n"

    \a formatStringSuffix specifies the type of the message e.g.:
        "error: %s\n"
        "warning: %s\n"
        "note: %s\n"
        "Parse error at %s\n" (from defaultErrorMsg())
*/
void Parser::printMsg(QByteArrayView formatStringSuffix, QByteArrayView msg, const Symbol &sym)
{
    if (sym.lineNum != -1) {
#ifdef Q_CC_MSVC
        QByteArray formatString = "%s(%d:%d): " + formatStringSuffix;
#else
        QByteArray formatString = "%s:%d:%d: " + formatStringSuffix;
#endif
        fprintf(stderr, formatString.constData(),
                currentFilenames.top().constData(), sym.lineNum, 1, msg.data());
    } else {
        QByteArray formatString = "%s: " + formatStringSuffix;
        fprintf(stderr, formatString.constData(),
                currentFilenames.top().constData(), msg.data());
    }
}

void Parser::defaultErrorMsg(const Symbol &sym)
{
    if (sym.lineNum != -1)
        printMsg("error: Parse error at \"%s\"\n", sym.lexem().data(), sym);
    else
        printMsg("error: could not parse file\n", "", sym);
}

void Parser::error(const Symbol &sym)
{
    defaultErrorMsg(sym);
    exit(EXIT_FAILURE);
}

void Parser::error(const char *msg)
{
    if (msg || error_msg)
        printMsg("error: %s\n",
                 msg ? msg : error_msg,
                 index > 0 ? symbol() : Symbol{});
    else
        defaultErrorMsg(symbol());

    exit(EXIT_FAILURE);
}

void Parser::warning(const Symbol &sym, QByteArrayView msg)
{
    if (displayWarnings)
        printMsg("warning: %s\n", msg, sym);
}

void Parser::warning(const char *msg) {
    warning(index > 0 ? symbol() : Symbol{}, msg);
}

void Parser::note(const char *msg) {
    if (displayNotes && msg)
        printMsg("note: %s\n", msg, index > 0 ? symbol() : Symbol{});
}

QT_END_NAMESPACE