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([this](const Message &error) {
64 diagnose(error.message, error.type, error.loc);
65 });
66
67 m_logger->setCompileErrorPrefix(u"Could not compile binding for %1: "_s.arg(name));
68 m_logger->setCompileSkipPrefix(u"Compilation of binding for %1 was skipped: "_s.arg(name));
69
70 analyzeFunction(context, &function);
71 if (const auto errors = finalizeBindingOrFunction())
72 return *errors;
73
74 return QQmlJSAotFunction {};
75}
76
77std::variant<QQmlJSAotFunction, QList<QQmlJS::DiagnosticMessage>>
78QQmlJSLinterCodegen::compileFunction(const QV4::Compiler::Context *context,
79 const QString &name, QQmlJS::AST::Node *astNode)
80{
81 m_logger->setCompileErrorPrefix(u"Could not determine signature of function %1: "_s.arg(name));
82
83 QQmlJSFunctionInitializer initializer(
84 &m_typeResolver, m_currentObject->location, m_currentScope->location, m_logger);
85 QQmlJSCompilePass::Function function = initializer.run(context, name, astNode);
86
87 m_logger->iterateCurrentFunctionMessages([this](const Message &error) {
88 diagnose(error.message, error.type, error.loc);
89 });
90
91 m_logger->setCompileErrorPrefix(u"Could not compile function %1: "_s.arg(name));
92 m_logger->setCompileSkipPrefix(u"Compilation of function %1 was skipped: "_s.arg(name));
93 analyzeFunction(context, &function);
94
95 if (const auto errors = finalizeBindingOrFunction())
96 return *errors;
97
98 return QQmlJSAotFunction {};
99}
100
101void QQmlJSLinterCodegen::setPassManager(QQmlSA::PassManager *passManager)
102{
103 m_passManager = passManager;
104 auto managerPriv = QQmlSA::PassManagerPrivate::get(passManager);
105 managerPriv->m_typeResolver = typeResolver();
106}
107
108void QQmlJSLinterCodegen::analyzeFunction(const QV4::Compiler::Context *context,
109 QQmlJSCompilePass::Function *function)
110{
111 bool dummy = false;
112 QQmlJSCompilePass::BlocksAndAnnotations blocksAndAnnotations =
113 QQmlJSBasicBlocks(context, m_unitGenerator, &m_typeResolver, m_logger)
114 .run(function, ValidateBasicBlocks, dummy);
115
116 QQmlJSLinterTypePropagator lintTypePropgator(m_unitGenerator, &m_typeResolver, m_logger,
117 m_context, blocksAndAnnotations.basicBlocks,
118 blocksAndAnnotations.annotations, m_passManager);
119 lintTypePropgator.setIdMemberShadows(&m_idMemberShadows);
120 blocksAndAnnotations = lintTypePropgator.run(function);
121
122 if (m_logger->categorySeverity(qmlCompiler) == QQmlJS::WarningSeverity::Disable)
123 return;
124
125 if (!m_logger->currentFunctionHasCompileError()) {
126 blocksAndAnnotations = QQmlJSShadowCheck(m_unitGenerator, &m_typeResolver, m_logger,
127 blocksAndAnnotations.basicBlocks,
128 blocksAndAnnotations.annotations)
129 .run(function);
130 }
131
132 if (!m_logger->currentFunctionHasCompileError()) {
133 blocksAndAnnotations = QQmlJSStorageInitializer(m_unitGenerator, &m_typeResolver, m_logger,
134 blocksAndAnnotations.basicBlocks,
135 blocksAndAnnotations.annotations)
136 .run(function);
137 }
138
139 if (!m_logger->currentFunctionHasCompileError()) {
140 blocksAndAnnotations = QQmlJSStorageGeneralizer(m_unitGenerator, &m_typeResolver, m_logger,
141 blocksAndAnnotations.basicBlocks,
142 blocksAndAnnotations.annotations)
143 .run(function);
144 }
145}
146
147QT_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)