1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef ERRORMESSAGE_H |
5 | #define ERRORMESSAGE_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include "qqmldom_global.h" |
19 | #include "qqmldomstringdumper_p.h" |
20 | #include "qqmldompath_p.h" |
21 | |
22 | #include <QtQml/private/qqmljsast_p.h> |
23 | #include <QtCore/QCoreApplication> |
24 | #include <QtCore/QString> |
25 | #include <QtCore/QCborArray> |
26 | #include <QtCore/QCborMap> |
27 | #include <QtCore/QLoggingCategory> |
28 | #include <QtQml/private/qqmljsdiagnosticmessage_p.h> |
29 | |
30 | QT_BEGIN_NAMESPACE |
31 | |
32 | Q_DECLARE_LOGGING_CATEGORY(domLog); |
33 | |
34 | namespace QQmlJS { |
35 | namespace Dom { |
36 | |
37 | QMLDOM_EXPORT ErrorLevel errorLevelFromQtMsgType(QtMsgType msgType); |
38 | |
39 | class ErrorGroups; |
40 | class DomItem; |
41 | using std::function; |
42 | |
43 | #define NewErrorGroup(name) QQmlJS::Dom::ErrorGroup(QT_TRANSLATE_NOOP("ErrorGroup", name)) |
44 | |
45 | class QMLDOM_EXPORT ErrorGroup { |
46 | Q_GADGET |
47 | Q_DECLARE_TR_FUNCTIONS(ErrorGroup) |
48 | public: |
49 | constexpr ErrorGroup(const char *groupId): |
50 | m_groupId(groupId) |
51 | {} |
52 | |
53 | |
54 | void dump(const Sink &sink) const; |
55 | void dumpId(const Sink &sink) const; |
56 | |
57 | QLatin1String groupId() const; |
58 | QString groupName() const; |
59 | private: |
60 | const char *m_groupId; |
61 | }; |
62 | |
63 | class QMLDOM_EXPORT ErrorGroups{ |
64 | Q_GADGET |
65 | public: |
66 | void dump(const Sink &sink) const; |
67 | void dumpId(const Sink &sink) const; |
68 | QCborArray toCbor() const; |
69 | |
70 | [[nodiscard]] ErrorMessage errorMessage( |
71 | const Dumper &msg, ErrorLevel level, const Path &element = Path(), |
72 | const QString &canonicalFilePath = QString(), SourceLocation location = SourceLocation()) const; |
73 | [[nodiscard]] ErrorMessage errorMessage( |
74 | const DiagnosticMessage &msg, const Path &element = Path(), |
75 | const QString &canonicalFilePath = QString()) const; |
76 | |
77 | void fatal(const Dumper &msg, const Path &element = Path(), QStringView canonicalFilePath = u"" , |
78 | SourceLocation location = SourceLocation()) const; |
79 | |
80 | [[nodiscard]] ErrorMessage debug(const QString &message) const; |
81 | [[nodiscard]] ErrorMessage debug(const Dumper &message) const; |
82 | [[nodiscard]] ErrorMessage info(const QString &message) const; |
83 | [[nodiscard]] ErrorMessage info(const Dumper &message) const; |
84 | [[nodiscard]] ErrorMessage warning(const QString &message) const; |
85 | [[nodiscard]] ErrorMessage warning(const Dumper &message) const; |
86 | [[nodiscard]] ErrorMessage error(const QString &message) const; |
87 | [[nodiscard]] ErrorMessage error(const Dumper &message) const; |
88 | |
89 | static int cmp(const ErrorGroups &g1, const ErrorGroups &g2); |
90 | |
91 | QVector<ErrorGroup> groups; |
92 | }; |
93 | |
94 | inline bool operator==(const ErrorGroups& lhs, const ErrorGroups& rhs){ return ErrorGroups::cmp(g1: lhs,g2: rhs) == 0; } |
95 | inline bool operator!=(const ErrorGroups& lhs, const ErrorGroups& rhs){ return ErrorGroups::cmp(g1: lhs,g2: rhs) != 0; } |
96 | inline bool operator< (const ErrorGroups& lhs, const ErrorGroups& rhs){ return ErrorGroups::cmp(g1: lhs,g2: rhs) < 0; } |
97 | inline bool operator> (const ErrorGroups& lhs, const ErrorGroups& rhs){ return ErrorGroups::cmp(g1: lhs,g2: rhs) > 0; } |
98 | inline bool operator<=(const ErrorGroups& lhs, const ErrorGroups& rhs){ return ErrorGroups::cmp(g1: lhs,g2: rhs) <= 0; } |
99 | inline bool operator>=(const ErrorGroups& lhs, const ErrorGroups& rhs){ return ErrorGroups::cmp(g1: lhs,g2: rhs) >= 0; } |
100 | |
101 | class QMLDOM_EXPORT ErrorMessage { // reuse Some of the other DiagnosticMessages? |
102 | Q_GADGET |
103 | Q_DECLARE_TR_FUNCTIONS(ErrorMessage) |
104 | public: |
105 | using Level = ErrorLevel; |
106 | // error registry (usage is optional) |
107 | static QLatin1String msg(const char *errorId, ErrorMessage &&err); |
108 | static QLatin1String msg(QLatin1String errorId, ErrorMessage &&err); |
109 | static void visitRegisteredMessages(function_ref<bool (const ErrorMessage &)> visitor); |
110 | [[nodiscard]] static ErrorMessage load(QLatin1String errorId); |
111 | [[nodiscard]] static ErrorMessage load(const char *errorId); |
112 | template<typename... T> |
113 | [[nodiscard]] static ErrorMessage load(QLatin1String errorId, T... args){ |
114 | ErrorMessage res = load(errorId); |
115 | res.message = res.message.arg(args...); |
116 | return res; |
117 | } |
118 | |
119 | ErrorMessage( |
120 | const QString &message, const ErrorGroups &errorGroups, Level level = Level::Warning, |
121 | const Path &path = Path(), const QString &file = QString(), |
122 | SourceLocation location = SourceLocation(), QLatin1String errorId = QLatin1String("" )); |
123 | ErrorMessage( |
124 | const ErrorGroups &errorGroups, const DiagnosticMessage &msg, const Path &path = Path(), |
125 | const QString &file = QString(), QLatin1String errorId = QLatin1String("" )); |
126 | |
127 | [[nodiscard]] ErrorMessage &withErrorId(QLatin1String errorId); |
128 | [[nodiscard]] ErrorMessage &withPath(const Path &); |
129 | [[nodiscard]] ErrorMessage &withFile(const QString &); |
130 | [[nodiscard]] ErrorMessage &withFile(QStringView); |
131 | [[nodiscard]] ErrorMessage &withLocation(SourceLocation); |
132 | [[nodiscard]] ErrorMessage &withItem(const DomItem &); |
133 | |
134 | ErrorMessage handle(const ErrorHandler &errorHandler=nullptr); |
135 | |
136 | void dump(const Sink &s) const; |
137 | QString toString() const; |
138 | QCborMap toCbor() const; |
139 | friend int compare(const ErrorMessage &msg1, const ErrorMessage &msg2) |
140 | { |
141 | int c; |
142 | c = msg1.location.offset - msg2.location.offset; |
143 | if (c != 0) |
144 | return c; |
145 | c = msg1.location.startLine - msg2.location.startLine; |
146 | if (c != 0) |
147 | return c; |
148 | c = msg1.errorId.compare(other: msg2.errorId); |
149 | if (c != 0) |
150 | return c; |
151 | if (!msg1.errorId.isEmpty()) |
152 | return 0; |
153 | c = msg1.message.compare(s: msg2.message); |
154 | if (c != 0) |
155 | return c; |
156 | c = msg1.file.compare(s: msg2.file); |
157 | if (c != 0) |
158 | return c; |
159 | c = Path::cmp(p1: msg1.path, p2: msg2.path); |
160 | if (c != 0) |
161 | return c; |
162 | c = int(msg1.level) - int(msg2.level); |
163 | if (c != 0) |
164 | return c; |
165 | c = int(msg1.errorGroups.groups.size() - msg2.errorGroups.groups.size()); |
166 | if (c != 0) |
167 | return c; |
168 | for (qsizetype i = 0; i < msg1.errorGroups.groups.size(); ++i) { |
169 | c = msg1.errorGroups.groups[i].groupId().compare(other: msg2.errorGroups.groups[i].groupId()); |
170 | if (c != 0) |
171 | return c; |
172 | } |
173 | c = msg1.location.length - msg2.location.length; |
174 | if (c != 0) |
175 | return c; |
176 | c = msg1.location.startColumn - msg2.location.startColumn; |
177 | return c; |
178 | } |
179 | |
180 | QLatin1String errorId; |
181 | QString message; |
182 | ErrorGroups errorGroups; |
183 | Level level; |
184 | Path path; |
185 | QString file; |
186 | SourceLocation location; |
187 | }; |
188 | |
189 | inline bool operator !=(const ErrorMessage &e1, const ErrorMessage &e2) { |
190 | return compare(msg1: e1, msg2: e2) != 0; |
191 | } |
192 | inline bool operator ==(const ErrorMessage &e1, const ErrorMessage &e2) { |
193 | return compare(msg1: e1, msg2: e2) == 0; |
194 | } |
195 | inline bool operator<(const ErrorMessage &e1, const ErrorMessage &e2) |
196 | { |
197 | return compare(msg1: e1, msg2: e2) < 0; |
198 | } |
199 | inline bool operator<=(const ErrorMessage &e1, const ErrorMessage &e2) |
200 | { |
201 | return compare(msg1: e1, msg2: e2) <= 0; |
202 | } |
203 | inline bool operator>(const ErrorMessage &e1, const ErrorMessage &e2) |
204 | { |
205 | return compare(msg1: e1, msg2: e2) > 0; |
206 | } |
207 | inline bool operator>=(const ErrorMessage &e1, const ErrorMessage &e2) |
208 | { |
209 | return compare(msg1: e1, msg2: e2) >= 0; |
210 | } |
211 | |
212 | QMLDOM_EXPORT void silentError(const ErrorMessage &); |
213 | QMLDOM_EXPORT void errorToQDebug(const ErrorMessage &); |
214 | |
215 | QMLDOM_EXPORT void defaultErrorHandler(const ErrorMessage &); |
216 | QMLDOM_EXPORT void setDefaultErrorHandler(const ErrorHandler &h); |
217 | |
218 | } // end namespace Dom |
219 | } // end namespace QQmlJS |
220 | QT_END_NAMESPACE |
221 | #endif // ERRORMESSAGE_H |
222 | |