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 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | #ifdef USE_LEXEM_STORE |
12 | Symbol::LexemStore Symbol::lexemStore; |
13 | #endif |
14 | |
15 | static const char *error_msg = nullptr; |
16 | |
17 | /*! \internal |
18 | Base implementation for printing diagnostic messages. |
19 | |
20 | For example: |
21 | "/path/to/file:line:column: error: %s\n" |
22 | '%s' is replaced by \a msg. (Currently "column" is always 1). |
23 | |
24 | If sym.lineNum is -1, the line and column parts aren't printed: |
25 | "/path/to/file: error: %s\n" |
26 | |
27 | \a formatStringSuffix specifies the type of the message e.g.: |
28 | "error: %s\n" |
29 | "warning: %s\n" |
30 | "note: %s\n" |
31 | "Parse error at %s\n" (from defaultErrorMsg()) |
32 | */ |
33 | void Parser::printMsg(QByteArrayView formatStringSuffix, QByteArrayView msg, const Symbol &sym) |
34 | { |
35 | if (sym.lineNum != -1) { |
36 | #ifdef Q_CC_MSVC |
37 | QByteArray formatString = "%s(%d:%d): " + formatStringSuffix; |
38 | #else |
39 | QByteArray formatString = "%s:%d:%d: " + formatStringSuffix; |
40 | #endif |
41 | fprintf(stderr, format: formatString.constData(), |
42 | currentFilenames.top().constData(), sym.lineNum, 1, msg.data()); |
43 | } else { |
44 | QByteArray formatString = "%s: " + formatStringSuffix; |
45 | fprintf(stderr, format: formatString.constData(), |
46 | currentFilenames.top().constData(), msg.data()); |
47 | } |
48 | } |
49 | |
50 | void Parser::defaultErrorMsg(const Symbol &sym) |
51 | { |
52 | if (sym.lineNum != -1) |
53 | printMsg(formatStringSuffix: "error: Parse error at \"%s\"\n" , msg: sym.lexem().data(), sym); |
54 | else |
55 | printMsg(formatStringSuffix: "error: could not parse file\n" , msg: "" , sym); |
56 | } |
57 | |
58 | void Parser::error(const Symbol &sym) |
59 | { |
60 | defaultErrorMsg(sym); |
61 | exit(EXIT_FAILURE); |
62 | } |
63 | |
64 | void Parser::error(const char *msg) |
65 | { |
66 | if (msg || error_msg) |
67 | printMsg(formatStringSuffix: "error: %s\n" , |
68 | msg: msg ? msg : error_msg, |
69 | sym: index > 0 ? symbol() : Symbol{}); |
70 | else |
71 | defaultErrorMsg(sym: symbol()); |
72 | |
73 | exit(EXIT_FAILURE); |
74 | } |
75 | |
76 | void Parser::warning(const char *msg) { |
77 | if (displayWarnings && msg) |
78 | printMsg(formatStringSuffix: "warning: %s\n" , msg, sym: index > 0 ? symbol() : Symbol{}); |
79 | } |
80 | |
81 | void Parser::note(const char *msg) { |
82 | if (displayNotes && msg) |
83 | printMsg(formatStringSuffix: "note: %s\n" , msg, sym: index > 0 ? symbol() : Symbol{}); |
84 | } |
85 | |
86 | QT_END_NAMESPACE |
87 | |