aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlcompiler/qqmlsasourcelocation.h
diff options
context:
space:
mode:
authorOlivier De Cannière <olivier.decanniere@qt.io>2023-05-05 09:30:27 +0200
committerOlivier De Cannière <olivier.decanniere@qt.io>2023-05-30 13:42:35 +0200
commitcdd7fe05f676ed1664a156beaf63093237a3beac (patch)
tree8f7adccde1adc0e8404a96a895c5170b84f3f0cc /src/qmlcompiler/qqmlsasourcelocation.h
parent65cb77165ba18442a524faf44f712ae26661965c (diff)
QQmlSA: Create an abstraction layer for static analysis
This patch adds abstractions for QML Elements, Bindings, Methods and Properties. This abstraction layer avoids exposing internal details and should be more suited for static analysis tasks. It is now possible to write qmllint plugins without including private headers. As a drive-by, change tst_qmllint:verifyJsRoot to open files in text mode instead of binary. This fixes an issue where line endings cause issues on Windows. Fixes: QTBUG-102276 Change-Id: I6b6e53f1e0078734a18f3aa51807fbe875b375f0 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qmlcompiler/qqmlsasourcelocation.h')
-rw-r--r--src/qmlcompiler/qqmlsasourcelocation.h83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/qmlcompiler/qqmlsasourcelocation.h b/src/qmlcompiler/qqmlsasourcelocation.h
new file mode 100644
index 0000000000..38f21a2167
--- /dev/null
+++ b/src/qmlcompiler/qqmlsasourcelocation.h
@@ -0,0 +1,83 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+#ifndef QQMLSASOURCELOCATION_H
+#define QQMLSASOURCELOCATION_H
+
+#include <QtQmlCompiler/qtqmlcompilerexports.h>
+
+#include <QtCore/qstringview.h>
+
+QT_BEGIN_NAMESPACE
+
+namespace QQmlJS {
+class SourceLocation;
+} // namespace QQmlJS
+
+namespace QQmlSA {
+
+class SourceLocationPrivate;
+
+class Q_QMLCOMPILER_EXPORT SourceLocation
+{
+ friend class QT_PREPEND_NAMESPACE(QQmlSA::SourceLocationPrivate);
+
+public:
+ explicit SourceLocation(quint32 offset = 0, quint32 length = 0, quint32 line = 0,
+ quint32 column = 0);
+ SourceLocation(const SourceLocation &);
+ SourceLocation(SourceLocation &&other)
+ {
+ memcpy(m_data, other.m_data, sizeofSourceLocation);
+ memset(other.m_data, 0, sizeofSourceLocation);
+ }
+ SourceLocation &operator=(const SourceLocation &);
+ SourceLocation &operator=(SourceLocation &&other)
+ {
+ memcpy(m_data, other.m_data, sizeofSourceLocation);
+ memset(other.m_data, 0, sizeofSourceLocation);
+ return *this;
+ }
+ ~SourceLocation();
+
+ bool isValid() const;
+
+ quint32 begin() const;
+ quint32 end() const;
+
+ quint32 offset() const;
+ quint32 length() const;
+ quint32 startLine() const;
+ quint32 startColumn() const;
+
+ SourceLocation startZeroLengthLocation() const;
+ SourceLocation endZeroLengthLocation(QStringView text) const;
+
+ friend qsizetype qHash(const SourceLocation &location, qsizetype seed = 0)
+ {
+ return qHashImpl(location, seed);
+ }
+
+ friend bool operator==(const SourceLocation &lhs, const SourceLocation &rhs)
+ {
+ return operatorEqualsImpl(lhs, rhs);
+ }
+
+ friend bool operator!=(const SourceLocation &lhs, const SourceLocation &rhs)
+ {
+ return !(lhs == rhs);
+ }
+
+private:
+ static qsizetype qHashImpl(const SourceLocation &location, qsizetype seed);
+ static bool operatorEqualsImpl(const SourceLocation &, const SourceLocation &);
+
+ static constexpr qsizetype sizeofSourceLocation = 4 * sizeof(quint32);
+ char m_data[sizeofSourceLocation] = {};
+};
+
+} // namespace QQmlSA
+
+QT_END_NAMESPACE
+
+#endif // QQMLSASOURCELOCATION_H