1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "qqmljscontextproperties_p.h"
#include <private/qtqml-config_p.h>
#include <QtCore/qtconfigmacros.h>
#include <QtCore/qregularexpression.h>
#include <QtCore/qdirlisting.h>
#include <QtCore/qfile.h>
#if QT_CONFIG(qmlcontextpropertydump)
# include <QtCore/qsettings.h>
#endif
#if QT_CONFIG(process)
# include <QtCore/qprocess.h>
#endif
QT_BEGIN_NAMESPACE
namespace QQmlJS {
using namespace Qt::StringLiterals;
// There are many ways to set context properties without triggering the regexp in s_pattern,
// but its supposed to catch most context properties set via "setContextProperty".
static constexpr QLatin1StringView s_pattern =
R"x((\.|->)setContextProperty\s*\(\s*(QStringLiteral\s*\(|QString\s*\(|QLatin1String(View)?\s*\(|u)?\s*"([^"]*)")x"_L1;
static constexpr int s_contextPropertyNameIdxInPattern = 4;
// TODO: use a central list of file extensions that can also be used by qmetatypesjsonprocessor.cpp
// (that needs header file extensions) and Qt6QmlMacros.cmake.
static constexpr std::array s_fileFilters = {
"*.cpp"_L1, "*.cxx"_L1, "*.cc"_L1, "*.c"_L1, "*.c++"_L1,
"*.hpp"_L1, "*.hxx"_L1, "*.hh"_L1, "*.h"_L1, "*.h++"_L1,
};
static const QRegularExpression s_matchSetContextProperty{ s_pattern,
QRegularExpression::MultilineOption };
QList<HeuristicContextProperty>
HeuristicContextProperties::definitionsForName(const QString &name) const
{
const auto it = m_properties.find(name);
if (it != m_properties.end())
return it.value();
return {};
}
void HeuristicContextProperties::add(const QString &name, const HeuristicContextProperty &property)
{
if (const auto it = m_properties.find(name); it != m_properties.end()) {
it.value().append(property);
return;
}
m_properties.insert(name, { property });
}
void HeuristicContextProperties::collectFromFile(const QString &filePath)
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
const QString fileContent = QString::fromUtf8(file.readAll());
for (const auto &match : s_matchSetContextProperty.globalMatch(fileContent)) {
const quint32 offset = match.capturedStart(s_contextPropertyNameIdxInPattern);
const quint32 length = match.capturedLength(s_contextPropertyNameIdxInPattern);
const auto [row, column] = QQmlJS::SourceLocation::rowAndColumnFrom(fileContent, offset);
const QQmlJS::SourceLocation sourceLocation{ offset, length, row, column };
add(match.captured(s_contextPropertyNameIdxInPattern),
HeuristicContextProperty{ filePath, sourceLocation });
}
}
void HeuristicContextProperties::grepFallback(const QList<QString> &rootUrls)
{
const QStringList fileFilters{ s_fileFilters.begin(), s_fileFilters.end() };
for (const QString &url : rootUrls) {
for (const auto &dirEntry : QDirListing{ url, fileFilters,
QDirListing::IteratorFlag::Recursive
| QDirListing::IteratorFlag::FilesOnly }) {
const QString filePath = dirEntry.filePath();
collectFromFile(filePath);
}
}
}
#if QT_CONFIG(process) && !defined(Q_OS_WINDOWS)
void HeuristicContextProperties::parseGrepOutput(const QString &output)
{
for (const auto line : QStringTokenizer{ output, "\n"_L1, Qt::SkipEmptyParts })
collectFromFile(line.toString());
}
#endif
HeuristicContextProperties
HeuristicContextProperties::collectFromCppSourceDirs(const QList<QString> &cppSourceDirs)
{
HeuristicContextProperties result;
result.collectFromDirs(cppSourceDirs);
return result;
}
/*!
\internal
Uses grep to find files that have setContextProperty()-calls, and then search matching files
with QRegularExpression to extract the location and name of the found context properties.
*/
void HeuristicContextProperties::collectFromDirs(const QList<QString> &dirs)
{
if (dirs.isEmpty())
return;
#if QT_CONFIG(process) && !defined(Q_OS_WINDOWS)
if (qEnvironmentVariableIsSet("QT_QML_NO_GREP")) {
grepFallback(dirs);
return;
}
QProcess grep;
QStringList arguments{ "--recursive"_L1,
"--null-data"_L1, // match multiline patterns
"--files-with-matches"_L1,
"--extended-regexp"_L1, // the pattern is "extended"
"-e"_L1,
s_pattern };
// don't search non-cpp files
for (const auto fileFilter : s_fileFilters)
arguments << "--include"_L1 << fileFilter;
arguments.append(dirs);
grep.start("grep"_L1, arguments);
grep.waitForFinished();
if (grep.exitStatus() == QProcess::NormalExit) {
switch (grep.exitCode()) {
case 0: { // success
const QString output = QString::fromUtf8(grep.readAllStandardOutput());
parseGrepOutput(output);
return;
}
case 1: // success but no context properties found
return;
default: // grep error
break;
}
}
#endif
grepFallback(dirs);
}
#if QT_CONFIG(qmlcontextpropertydump)
static SourceLocation deserializeSourceLocation(const QString &string)
{
constexpr int size = 4;
const QStringList bits = string.split(u',', Qt::SkipEmptyParts);
if (bits.length() != size)
return SourceLocation{};
SourceLocation result;
quint32 *destination[size] = { &result.offset, &result.length, &result.startLine,
&result.startColumn };
bool everythingOk = true;
for (int i = 0; i < size; ++i) {
bool ok = false;
*destination[i] = bits[i].toInt(&ok);
everythingOk &= ok;
}
if (everythingOk)
return result;
return SourceLocation{};
}
static QString serializeSourceLocation(const SourceLocation &location)
{
QString result;
result.append(QString::number(location.offset)).append(u',');
result.append(QString::number(location.length)).append(u',');
result.append(QString::number(location.startLine)).append(u',');
result.append(QString::number(location.startColumn));
return result;
}
#endif
static constexpr auto cachedHeuristicListKey = "cachedHeuristicList"_L1;
HeuristicContextProperties HeuristicContextProperties::collectFrom(QSettings *settings)
{
#if QT_CONFIG(qmlcontextpropertydump)
HeuristicContextProperties result;
std::vector<QString> names;
const int size = settings->beginReadArray(cachedHeuristicListKey);
for (int i = 0; i < size; ++i) {
settings->setArrayIndex(i);
names.push_back(settings->value("name").toString());
}
settings->endArray();
for (const auto &name : names) {
const int size = settings->beginReadArray(u"property_"_s.append(name));
for (int i = 0; i < size; ++i) {
settings->setArrayIndex(i);
result.add(
name,
HeuristicContextProperty{
settings->value("fileName").toString(),
deserializeSourceLocation(settings->value("sourceLocation").toString()),
});
}
settings->endArray();
}
return result;
#else
Q_UNUSED(settings);
return HeuristicContextProperties{};
#endif
}
void HeuristicContextProperties::writeCache(const QString &folder) const
{
#if QT_CONFIG(qmlcontextpropertydump)
QSettings settings(folder + "/.qt/contextPropertyDump.ini"_L1, QSettings::IniFormat);
settings.beginWriteArray(cachedHeuristicListKey);
int index = 0;
for (const auto &[name, _] : m_properties) {
settings.setArrayIndex(index++);
settings.setValue("name", name);
}
settings.endArray();
for (const auto &[name, definitions] : m_properties) {
settings.beginWriteArray(u"property_"_s.append(name));
for (int i = 0; i < definitions.size(); ++i) {
settings.setArrayIndex(i);
settings.setValue("fileName", definitions[i].filename);
settings.setValue("sourceLocation", serializeSourceLocation(definitions[i].location));
}
settings.endArray();
}
#else
Q_UNUSED(folder);
#endif
}
} // namespace QQmlJS
QT_END_NAMESPACE
|