aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlcompiler/qqmlsasourcelocation.cpp
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.cpp
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.cpp')
-rw-r--r--src/qmlcompiler/qqmlsasourcelocation.cpp125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/qmlcompiler/qqmlsasourcelocation.cpp b/src/qmlcompiler/qqmlsasourcelocation.cpp
new file mode 100644
index 0000000000..63b61cce5b
--- /dev/null
+++ b/src/qmlcompiler/qqmlsasourcelocation.cpp
@@ -0,0 +1,125 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+#include "qqmlsasourcelocation.h"
+#include "qqmlsasourcelocation_p.h"
+
+QT_BEGIN_NAMESPACE
+
+using namespace Qt::StringLiterals;
+
+namespace QQmlSA {
+
+static_assert(SourceLocationPrivate::sizeOfSourceLocation() == sizeof(SourceLocation));
+
+/*!
+ \class QQmlSA::SourceLocation
+ \inmodule QtQmlCompiler
+
+ \brief Represents a location or region in the source code.
+ */
+QQmlSA::SourceLocation::SourceLocation(quint32 offset, quint32 length, quint32 line, quint32 column)
+{
+ new (m_data) QQmlJS::SourceLocation{ offset, length, line, column };
+}
+
+// explicitly defaulted out-of-line for PIMPL
+QQmlSA::SourceLocation::SourceLocation(const SourceLocation &other) = default;
+QQmlSA::SourceLocation & QQmlSA::SourceLocation::operator=(const QQmlSA::SourceLocation &other) = default;
+SourceLocation::~SourceLocation() = default;
+
+bool QQmlSA::SourceLocation::isValid() const
+{
+ return QQmlSA::SourceLocationPrivate::sourceLocation(*this).isValid();
+}
+
+/*!
+ Returns the offset of the beginning of this source location.
+ */
+quint32 QQmlSA::SourceLocation::begin() const
+{
+ return QQmlSA::SourceLocationPrivate::sourceLocation(*this).begin();
+}
+
+/*!
+ Returns the offset of the end of this source location.
+ */
+quint32 QQmlSA::SourceLocation::end() const
+{
+ return QQmlSA::SourceLocationPrivate::sourceLocation(*this).end();
+}
+
+/*!
+ Returns the offset of the beginning of this source location.
+ */
+quint32 QQmlSA::SourceLocation::offset() const
+{
+ return QQmlSA::SourceLocationPrivate::sourceLocation(*this).offset;
+}
+
+/*!
+ Returns the length of this source location.
+ */
+quint32 QQmlSA::SourceLocation::length() const
+{
+ return QQmlSA::SourceLocationPrivate::sourceLocation(*this).length;
+}
+
+/*!
+ Returns the line number containing the beginning of this source location.
+ */
+quint32 QQmlSA::SourceLocation::startLine() const
+{
+ return QQmlSA::SourceLocationPrivate::sourceLocation(*this).startLine;
+}
+
+/*!
+ Returns the column number containing the beginning of this source location.
+ */
+quint32 QQmlSA::SourceLocation::startColumn() const
+{
+ return QQmlSA::SourceLocationPrivate::sourceLocation(*this).startColumn;
+}
+
+/*!
+ Returns a source location of lenth zero pointing to the beginning of this
+ source location.
+ */
+QQmlSA::SourceLocation QQmlSA::SourceLocation::startZeroLengthLocation() const
+{
+ QQmlSA::SourceLocation saLocation;
+ auto &wrappedLocation = reinterpret_cast<QQmlJS::SourceLocation &>(saLocation.m_data);
+ wrappedLocation =
+ QQmlSA::SourceLocationPrivate::sourceLocation(*this).startZeroLengthLocation();
+
+ return saLocation;
+}
+
+/*!
+ Returns a source location of lenth zero pointing to the end of this source
+ location pointing to \a text.
+ */
+QQmlSA::SourceLocation QQmlSA::SourceLocation::endZeroLengthLocation(QStringView text) const
+{
+ QQmlSA::SourceLocation saLocation;
+ auto &wrappedLocation = reinterpret_cast<QQmlJS::SourceLocation &>(saLocation.m_data);
+ wrappedLocation = wrappedLocation.endZeroLengthLocation(text);
+
+ return saLocation;
+}
+
+qsizetype QQmlSA::SourceLocation::qHashImpl(const SourceLocation &location, qsizetype seed)
+{
+ return qHash(QQmlSA::SourceLocationPrivate::sourceLocation(location), seed);
+}
+
+bool QQmlSA::SourceLocation::operatorEqualsImpl(const SourceLocation &lhs,
+ const SourceLocation &rhs)
+{
+ return QQmlSA::SourceLocationPrivate::sourceLocation(lhs)
+ == QQmlSA::SourceLocationPrivate::sourceLocation(rhs);
+}
+
+} // namespace QQmlSA
+
+QT_END_NAMESPACE