1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #ifndef LOCATION_H |
5 | #define LOCATION_H |
6 | |
7 | #include <QtCore/qcoreapplication.h> |
8 | #include <QtCore/qstack.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | class QRegularExpression; |
13 | |
14 | class Location |
15 | { |
16 | public: |
17 | Location(); |
18 | explicit Location(const QString &filePath); |
19 | Location(const Location &other); |
20 | ~Location() { delete m_stk; } |
21 | |
22 | Location &operator=(const Location &other); |
23 | |
24 | void start(); |
25 | void advance(QChar ch); |
26 | void advanceLines(int n) |
27 | { |
28 | m_stkTop->m_lineNo += n; |
29 | m_stkTop->m_columnNo = 1; |
30 | } |
31 | |
32 | void push(const QString &filePath); |
33 | void pop(); |
34 | void setEtc(bool etc) { m_etc = etc; } |
35 | void setLineNo(int no) { m_stkTop->m_lineNo = no; } |
36 | void setColumnNo(int no) { m_stkTop->m_columnNo = no; } |
37 | |
38 | [[nodiscard]] bool isEmpty() const { return m_stkDepth == 0; } |
39 | [[nodiscard]] int depth() const { return m_stkDepth; } |
40 | [[nodiscard]] const QString &filePath() const { return m_stkTop->m_filePath; } |
41 | [[nodiscard]] QString fileName() const; |
42 | [[nodiscard]] QString fileSuffix() const; |
43 | [[nodiscard]] int lineNo() const { return m_stkTop->m_lineNo; } |
44 | [[nodiscard]] int columnNo() const { return m_stkTop->m_columnNo; } |
45 | [[nodiscard]] bool etc() const { return m_etc; } |
46 | [[nodiscard]] QString toString() const; |
47 | void warning(const QString &message, const QString &details = QString()) const; |
48 | void error(const QString &message, const QString &details = QString()) const; |
49 | void fatal(const QString &message, const QString &details = QString()) const; |
50 | void report(const QString &message, const QString &details = QString()) const; |
51 | |
52 | static void initialize(); |
53 | |
54 | static void terminate(); |
55 | static void information(const QString &message); |
56 | static void internalError(const QString &hint); |
57 | static int exitCode(); |
58 | |
59 | private: |
60 | enum MessageType { Warning, Error, Report }; |
61 | |
62 | struct StackEntry |
63 | { |
64 | QString m_filePath {}; |
65 | int m_lineNo {}; |
66 | int m_columnNo {}; |
67 | }; |
68 | friend class QTypeInfo<StackEntry>; |
69 | |
70 | void emitMessage(MessageType type, const QString &message, const QString &details) const; |
71 | [[nodiscard]] QString top() const; |
72 | |
73 | private: |
74 | StackEntry m_stkBottom {}; |
75 | QStack<StackEntry> *m_stk {}; |
76 | StackEntry *m_stkTop {}; |
77 | int m_stkDepth {}; |
78 | bool m_etc {}; |
79 | |
80 | static int s_tabSize; |
81 | static int s_warningCount; |
82 | static int s_warningLimit; |
83 | static QString s_programName; |
84 | static QString s_project; |
85 | static QRegularExpression *s_spuriousRegExp; |
86 | }; |
87 | Q_DECLARE_TYPEINFO(Location::StackEntry, Q_RELOCATABLE_TYPE); |
88 | Q_DECLARE_TYPEINFO(Location, Q_COMPLEX_TYPE); // stkTop = &stkBottom |
89 | |
90 | QT_END_NAMESPACE |
91 | |
92 | #endif |
93 | |