Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
parser.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "parser.h"
5#include "utils.h"
6#include <stdio.h>
7#include <stdlib.h>
8
10
11static const char *error_msg = nullptr;
12
13/*! \internal
14 Base implementation for printing diagnostic messages.
15
16 For example:
17 "/path/to/file:line:column: error: %s\n"
18 '%s' is replaced by \a msg. (Currently "column" is always 1).
19
20 If sym.lineNum is -1, the line and column parts aren't printed:
21 "/path/to/file: error: %s\n"
22
23 \a formatStringSuffix specifies the type of the message e.g.:
24 "error: %s\n"
25 "warning: %s\n"
26 "note: %s\n"
27 "Parse error at %s\n" (from defaultErrorMsg())
28*/
29void Parser::printMsg(QByteArrayView formatStringSuffix, QByteArrayView msg, const Symbol &sym)
30{
31 if (sym.lineNum != -1) {
32#ifdef Q_CC_MSVC
33 QByteArray formatString = "%s(%d:%d): " + formatStringSuffix;
34#else
35 QByteArray formatString = "%s:%d:%d: " + formatStringSuffix;
36#endif
37 fprintf(stderr, formatString.constData(),
38 currentFilenames.top().constData(), sym.lineNum, 1,
39 int(msg.size()), msg.data());
40 } else {
41 QByteArray formatString = "%s: " + formatStringSuffix;
42 fprintf(stderr, formatString.constData(),
43 currentFilenames.top().constData(),
44 int(msg.size()), msg.data());
45 }
46}
47
48void Parser::defaultErrorMsg(const Symbol &sym)
49{
50 if (sym.lineNum != -1)
51 printMsg("error: Parse error at \"%.*s\"\n", sym.lexem(), sym);
52 else
53 printMsg("error: could not parse file\n", "", sym);
54}
55
56void Parser::error(const Symbol &sym)
57{
58 defaultErrorMsg(sym);
59 exit(EXIT_FAILURE);
60}
61
62void Parser::error(const char *msg)
63{
64 if (msg || error_msg)
65 printMsg("error: %.*s\n",
66 msg ? msg : error_msg,
67 index > 0 ? symbol() : Symbol{});
68 else
69 defaultErrorMsg(symbol());
70
71 exit(EXIT_FAILURE);
72}
73
74void Parser::error(const Symbol& symbol, const char *msg)
75{
76 printMsg("error: %.*s\n", msg, symbol);
77 exit(EXIT_FAILURE);
78}
79
80void Parser::warning(const Symbol &sym, QByteArrayView msg)
81{
82 if (displayWarnings)
83 printMsg("warning: %.*s\n", msg, sym);
84}
85
86void Parser::warning(const char *msg) {
87 warning(index > 0 ? symbol() : Symbol{}, msg);
88}
89
90void Parser::note(const char *msg) {
91 if (displayNotes && msg)
92 printMsg("note: %.*s\n", msg, index > 0 ? symbol() : Symbol{});
93}
94
95QT_END_NAMESPACE
static QT_BEGIN_NAMESPACE const char * error_msg
Definition parser.cpp:11
int lineNum
Definition symbols.h:57