Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmljslintercodegen.cpp
Go to the documentation of this file.
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// Qt-Security score:significant
4
7
8#include <private/qqmljsbasicblocks_p.h>
9#include <private/qqmljsfunctioninitializer_p.h>
10#include <private/qqmljsimportvisitor_p.h>
11#include <private/qqmljsshadowcheck_p.h>
12#include <private/qqmljsstoragegeneralizer_p.h>
13#include <private/qqmljsstorageinitializer_p.h>
14
15#include <QFileInfo>
16
18
19using namespace Qt::StringLiterals;
20
21bool operator==(const IdMemberShadow &lhs, const IdMemberShadow &rhs)
22{
23 return lhs.name == rhs.name && lhs.idScope == rhs.idScope
24 && lhs.memberOwnerScope == rhs.memberOwnerScope;
25}
26bool operator!=(const IdMemberShadow &lhs, const IdMemberShadow &rhs)
27{
28 return !(lhs == rhs);
29}
30size_t qHash(const IdMemberShadow &idShadowsMember, size_t seed)
31{
32 return qHashMulti(seed, idShadowsMember.name, idShadowsMember.idScope,
33 idShadowsMember.memberOwnerScope);
34}
35QQmlJSLinterCodegen::QQmlJSLinterCodegen(QQmlJSImporter *importer, const QString &fileName,
36 const QStringList &qmldirFiles, QQmlJSLogger *logger,
37 const QQmlJS::LinterContext &context)
39{
40 m_flags |= QQmlJSAotCompiler::IsLintCompiler;
41}
42
43void QQmlJSLinterCodegen::setDocument(const QmlIR::JSCodeGen *codegen,
44 const QmlIR::Document *document)
45{
46 Q_UNUSED(codegen);
47 m_document = document;
48 m_unitGenerator = &document->jsGenerator;
49}
50
51std::variant<QQmlJSAotFunction, QList<QQmlJS::DiagnosticMessage>>
52QQmlJSLinterCodegen::compileBinding(const QV4::Compiler::Context *context,
53 const QmlIR::Binding &irBinding, QQmlJS::AST::Node *astNode)
54{
55 const QString name = m_document->stringAt(irBinding.propertyNameIndex);
56 m_logger->setCompileErrorPrefix(
57 u"Could not determine signature of binding for %1: "_s.arg(name));
58
59 QQmlJSFunctionInitializer initializer(
60 &m_typeResolver, m_currentObject->location, m_currentScope->location, m_logger);
61 QQmlJSCompilePass::Function function = initializer.run(context, name, astNode, irBinding);
62
63 m_logger->iterateCurrentFunctionMessages(
64 [this](const Message &error) { diagnose(error.message, error.msgType(), error.loc); });
65
66 m_logger->setCompileErrorPrefix(u"Could not compile binding for %1: "_s.arg(name));
67 m_logger->setCompileSkipPrefix(u"Compilation of binding for %1 was skipped: "_s.arg(name));
68
69 analyzeFunction(context, &function);
70 if (const auto errors = finalizeBindingOrFunction())
71 return *errors;
72
73 return QQmlJSAotFunction {};
74}
75
76std::variant<QQmlJSAotFunction, QList<QQmlJS::DiagnosticMessage>>
77QQmlJSLinterCodegen::compileFunction(const QV4::Compiler::Context *context,
78 const QString &name, QQmlJS::AST::Node *astNode)
79{
80 m_logger->setCompileErrorPrefix(u"Could not determine signature of function %1: "_s.arg(name));
81
82 QQmlJSFunctionInitializer initializer(
83 &m_typeResolver, m_currentObject->location, m_currentScope->location, m_logger);
84 QQmlJSCompilePass::Function function = initializer.run(context, name, astNode);
85
86 m_logger->iterateCurrentFunctionMessages(
87 [this](const Message &error) { diagnose(error.message, error.msgType(), error.loc); });
88
89 m_logger->setCompileErrorPrefix(u"Could not compile function %1: "_s.arg(name));
90 m_logger->setCompileSkipPrefix(u"Compilation of function %1 was skipped: "_s.arg(name));
91 analyzeFunction(context, &function);
92
93 if (const auto errors = finalizeBindingOrFunction())
94 return *errors;
95
96 return QQmlJSAotFunction {};
97}
98
99void QQmlJSLinterCodegen::setPassManager(QQmlSA::PassManager *passManager)
100{
101 m_passManager = passManager;
102 auto managerPriv = QQmlSA::PassManagerPrivate::get(passManager);
103 managerPriv->m_typeResolver = typeResolver();
104}
105
106void QQmlJSLinterCodegen::analyzeFunction(const QV4::Compiler::Context *context,
107 QQmlJSCompilePass::Function *function)
108{
109 bool dummy = false;
110 QQmlJSCompilePass::BlocksAndAnnotations blocksAndAnnotations =
111 QQmlJSBasicBlocks(context, m_unitGenerator, &m_typeResolver, m_logger)
112 .run(function, ValidateBasicBlocks, dummy);
113
114 QQmlJSLinterTypePropagator lintTypePropgator(m_unitGenerator, &m_typeResolver, m_logger,
115 m_context, blocksAndAnnotations.basicBlocks,
116 blocksAndAnnotations.annotations, m_passManager);
117 lintTypePropgator.setIdMemberShadows(&m_idMemberShadows);
118 blocksAndAnnotations = lintTypePropgator.run(function);
119
120 if (m_logger->categorySeverity(qmlCompiler) == QQmlJS::WarningSeverity::Disable)
121 return;
122
123 if (!m_logger->currentFunctionHasCompileError()) {
124 blocksAndAnnotations = QQmlJSShadowCheck(m_unitGenerator, &m_typeResolver, m_logger,
125 blocksAndAnnotations.basicBlocks,
126 blocksAndAnnotations.annotations)
127 .run(function);
128 }
129
130 if (!m_logger->currentFunctionHasCompileError()) {
131 blocksAndAnnotations = QQmlJSStorageInitializer(m_unitGenerator, &m_typeResolver, m_logger,
132 blocksAndAnnotations.basicBlocks,
133 blocksAndAnnotations.annotations)
134 .run(function);
135 }
136
137 if (!m_logger->currentFunctionHasCompileError()) {
138 blocksAndAnnotations = QQmlJSStorageGeneralizer(m_unitGenerator, &m_typeResolver, m_logger,
139 blocksAndAnnotations.basicBlocks,
140 blocksAndAnnotations.annotations)
141 .run(function);
142 }
143}
144
145QT_END_NAMESPACE
QQmlJSLinterCodegen(QQmlJSImporter *importer, const QString &fileName, const QStringList &qmldirFiles, QQmlJSLogger *logger, const QQmlJS::LinterContext &context)
void setDocument(const QmlIR::JSCodeGen *codegen, const QmlIR::Document *document) override
void setPassManager(QQmlSA::PassManager *passManager)
\inmodule QtQmlCompiler
Combined button and popup list for selecting options.
bool operator==(const IdMemberShadow &lhs, const IdMemberShadow &rhs)
bool operator!=(const IdMemberShadow &lhs, const IdMemberShadow &rhs)
size_t qHash(const IdMemberShadow &idShadowsMember, size_t seed)