Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/tools/qmlimportscanner/main.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||
2 | - | |||||||||||||
3 | - | |||||||||||||
4 | - | |||||||||||||
5 | - | |||||||||||||
6 | - | |||||||||||||
7 | - | |||||||||||||
8 | QStringList g_qmlImportPaths; | - | ||||||||||||
9 | - | |||||||||||||
10 | static inline QString typeLiteral() { return ([]() noexcept -> QString { enum { Size = sizeof(u"" "type")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "type" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); } never executed: end of block | 0 | ||||||||||||
11 | static inline QString versionLiteral() { return ([]() noexcept -> QString { enum { Size = sizeof(u"" "version")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "version" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); } never executed: end of block | 0 | ||||||||||||
12 | static inline QString nameLiteral() { return ([]() noexcept -> QString { enum { Size = sizeof(u"" "name")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "name" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); } never executed: end of block | 0 | ||||||||||||
13 | static inline QString relativePathLiteral() { return ([]() noexcept -> QString { enum { Size = sizeof(u"" "relativePath")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "relativePath" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); } never executed: end of block | 0 | ||||||||||||
14 | static inline QString pluginsLiteral() { return ([]() noexcept -> QString { enum { Size = sizeof(u"" "plugins")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "plugins" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); } never executed: end of block | 0 | ||||||||||||
15 | static inline QString pathLiteral() { return ([]() noexcept -> QString { enum { Size = sizeof(u"" "path")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "path" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); } never executed: end of block | 0 | ||||||||||||
16 | static inline QString classnamesLiteral() { return ([]() noexcept -> QString { enum { Size = sizeof(u"" "classnames")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "classnames" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); } never executed: end of block | 0 | ||||||||||||
17 | static inline QString dependenciesLiteral() { return ([]() noexcept -> QString { enum { Size = sizeof(u"" "dependencies")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "dependencies" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); } never executed: end of block | 0 | ||||||||||||
18 | - | |||||||||||||
19 | static void printUsage(const QString &appNameIn) | - | ||||||||||||
20 | { | - | ||||||||||||
21 | const std::wstring appName = appNameIn.toStdWString(); | - | ||||||||||||
22 | - | |||||||||||||
23 | const QString qmlPath = QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath); | - | ||||||||||||
24 | - | |||||||||||||
25 | - | |||||||||||||
26 | - | |||||||||||||
27 | std::wcerr | - | ||||||||||||
28 | << "Usage: " << appName << " -rootPath path/to/app/qml/directory -importPath path/to/qt/qml/directory\n" | - | ||||||||||||
29 | " " << appName << " -qmlFiles file1 file2 -importPath path/to/qt/qml/directory\n\n" | - | ||||||||||||
30 | "Example: " << appName << " -rootPath . -importPath " | - | ||||||||||||
31 | << QDir::toNativeSeparators(qmlPath).toStdWString() | - | ||||||||||||
32 | << '\n'; | - | ||||||||||||
33 | } never executed: end of block | 0 | ||||||||||||
34 | - | |||||||||||||
35 | QVariantList findImportsInAst(QQmlJS::AST::UiHeaderItemList *headerItemList, const QString &code, const QString &path) | - | ||||||||||||
36 | { | - | ||||||||||||
37 | QVariantList imports; | - | ||||||||||||
38 | - | |||||||||||||
39 | - | |||||||||||||
40 | for (QQmlJS::AST::UiHeaderItemList *headerItemIt = headerItemList; headerItemIt
| 0 | ||||||||||||
41 | QVariantMap import; | - | ||||||||||||
42 | QQmlJS::AST::UiImport *importNode = QQmlJS::AST::cast<QQmlJS::AST::UiImport *>(headerItemIt->headerItem); | - | ||||||||||||
43 | if (!importNode
| 0 | ||||||||||||
44 | continue; never executed: continue; | 0 | ||||||||||||
45 | - | |||||||||||||
46 | if (!importNode->fileName.isEmpty()
| 0 | ||||||||||||
47 | QString name = importNode->fileName.toString(); | - | ||||||||||||
48 | import[nameLiteral()] = name; | - | ||||||||||||
49 | if (name.endsWith(QLatin1String(".js"))
| 0 | ||||||||||||
50 | import[typeLiteral()] = ([]() noexcept -> QString { enum { Size = sizeof(u"" "javascript")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "javascript" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); | - | ||||||||||||
51 | } never executed: else {end of block | 0 | ||||||||||||
52 | import[typeLiteral()] = ([]() noexcept -> QString { enum { Size = sizeof(u"" "directory")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "directory" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); | - | ||||||||||||
53 | } never executed: end of block | 0 | ||||||||||||
54 | - | |||||||||||||
55 | import[pathLiteral()] = QDir::cleanPath(path + QLatin1Char('/') + name); | - | ||||||||||||
56 | } never executed: else {end of block | 0 | ||||||||||||
57 | - | |||||||||||||
58 | QString name; | - | ||||||||||||
59 | QQmlJS::AST::UiQualifiedId *uri = importNode->importUri; | - | ||||||||||||
60 | while (uri
| 0 | ||||||||||||
61 | name.append(uri->name); | - | ||||||||||||
62 | name.append(QLatin1Char('.')); | - | ||||||||||||
63 | uri = uri->next; | - | ||||||||||||
64 | } never executed: end of block | 0 | ||||||||||||
65 | name.chop(1); | - | ||||||||||||
66 | if (!name.isEmpty()
| 0 | ||||||||||||
67 | import[nameLiteral()] = name; never executed: import[nameLiteral()] = name; | 0 | ||||||||||||
68 | import[typeLiteral()] = ([]() noexcept -> QString { enum { Size = sizeof(u"" "module")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "module" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); | - | ||||||||||||
69 | import[versionLiteral()] = code.mid(importNode->versionToken.offset, importNode->versionToken.length); | - | ||||||||||||
70 | } never executed: end of block | 0 | ||||||||||||
71 | - | |||||||||||||
72 | imports.append(import); | - | ||||||||||||
73 | } never executed: end of block | 0 | ||||||||||||
74 | - | |||||||||||||
75 | return never executed: imports;return imports; never executed: return imports; | 0 | ||||||||||||
76 | } | - | ||||||||||||
77 | - | |||||||||||||
78 | - | |||||||||||||
79 | - | |||||||||||||
80 | QVariantMap pluginsForModulePath(const QString &modulePath) { | - | ||||||||||||
81 | QFile qmldirFile(modulePath + QLatin1String("/qmldir")); | - | ||||||||||||
82 | if (!qmldirFile.exists()
| 0 | ||||||||||||
83 | return never executed: QVariantMap();return QVariantMap(); never executed: return QVariantMap(); | 0 | ||||||||||||
84 | - | |||||||||||||
85 | qmldirFile.open(QIODevice::ReadOnly | QIODevice::Text); | - | ||||||||||||
86 | - | |||||||||||||
87 | - | |||||||||||||
88 | QString plugins; | - | ||||||||||||
89 | QString classnames; | - | ||||||||||||
90 | QStringList dependencies; | - | ||||||||||||
91 | QByteArray line; | - | ||||||||||||
92 | do { | - | ||||||||||||
93 | line = qmldirFile.readLine(); | - | ||||||||||||
94 | if (line.startsWith("plugin")
| 0 | ||||||||||||
95 | plugins += QString::fromUtf8(line.split(' ').at(1)); | - | ||||||||||||
96 | plugins += QLatin1Char(' '); | - | ||||||||||||
97 | } never executed: else if (line.startsWith("classname")end of block
| 0 | ||||||||||||
98 | classnames += QString::fromUtf8(line.split(' ').at(1)); | - | ||||||||||||
99 | classnames += QLatin1Char(' '); | - | ||||||||||||
100 | } never executed: else if (line.startsWith("depends")end of block
| 0 | ||||||||||||
101 | const QList<QByteArray> dep = line.split(' '); | - | ||||||||||||
102 | if (dep.length() != 3
| 0 | ||||||||||||
103 | std::cerr << "depends: expected 2 arguments: module identifier and version" << std::endl; never executed: std::cerr << "depends: expected 2 arguments: module identifier and version" << std::endl; | 0 | ||||||||||||
104 | else | - | ||||||||||||
105 | dependencies << QString::fromUtf8(dep[1]) + QLatin1Char(' ') + QString::fromUtf8(dep[2]).simplified(); never executed: dependencies << QString::fromUtf8(dep[1]) + QLatin1Char(' ') + QString::fromUtf8(dep[2]).simplified(); | 0 | ||||||||||||
106 | } | - | ||||||||||||
107 | - | |||||||||||||
108 | } never executed: while (line.length() > 0end of block
| 0 | ||||||||||||
109 | - | |||||||||||||
110 | QVariantMap pluginInfo; | - | ||||||||||||
111 | pluginInfo[pluginsLiteral()] = plugins.simplified(); | - | ||||||||||||
112 | pluginInfo[classnamesLiteral()] = classnames.simplified(); | - | ||||||||||||
113 | if (dependencies.length()
| 0 | ||||||||||||
114 | pluginInfo[dependenciesLiteral()] = dependencies; never executed: pluginInfo[dependenciesLiteral()] = dependencies; | 0 | ||||||||||||
115 | return never executed: pluginInfo;return pluginInfo; never executed: return pluginInfo; | 0 | ||||||||||||
116 | } | - | ||||||||||||
117 | - | |||||||||||||
118 | - | |||||||||||||
119 | - | |||||||||||||
120 | QPair<QString, QString> resolveImportPath(const QString &uri, const QString &version) | - | ||||||||||||
121 | { | - | ||||||||||||
122 | const QLatin1Char dot('.'); | - | ||||||||||||
123 | const QLatin1Char slash('/'); | - | ||||||||||||
124 | const QStringList parts = uri.split(dot, QString::SkipEmptyParts); | - | ||||||||||||
125 | - | |||||||||||||
126 | QString ver = version; | - | ||||||||||||
127 | while (true) { | - | ||||||||||||
128 | for (const QString &qmlImportPath : qAsConst(g_qmlImportPaths)) { | - | ||||||||||||
129 | - | |||||||||||||
130 | - | |||||||||||||
131 | - | |||||||||||||
132 | - | |||||||||||||
133 | - | |||||||||||||
134 | - | |||||||||||||
135 | - | |||||||||||||
136 | if (ver.isEmpty()
| 0 | ||||||||||||
137 | QString relativePath = parts.join(slash); | - | ||||||||||||
138 | if (relativePath.endsWith(slash)
| 0 | ||||||||||||
139 | relativePath.chop(1); never executed: relativePath.chop(1); | 0 | ||||||||||||
140 | const QString candidatePath = QDir::cleanPath(qmlImportPath + slash + relativePath); | - | ||||||||||||
141 | if (QDir(candidatePath).exists()
| 0 | ||||||||||||
142 | return never executed: qMakePair(candidatePath, relativePath);return qMakePair(candidatePath, relativePath); never executed: return qMakePair(candidatePath, relativePath); | 0 | ||||||||||||
143 | } never executed: else {end of block | 0 | ||||||||||||
144 | for (int index = parts.count() - 1; index >= 0
| 0 | ||||||||||||
145 | QString relativePath = parts.mid(0, index + 1).join(slash) | - | ||||||||||||
146 | + dot + ver + slash + parts.mid(index + 1).join(slash); | - | ||||||||||||
147 | if (relativePath.endsWith(slash)
| 0 | ||||||||||||
148 | relativePath.chop(1); never executed: relativePath.chop(1); | 0 | ||||||||||||
149 | const QString candidatePath = QDir::cleanPath(qmlImportPath + slash + relativePath); | - | ||||||||||||
150 | if (QDir(candidatePath).exists()
| 0 | ||||||||||||
151 | return never executed: qMakePair(candidatePath, relativePath);return qMakePair(candidatePath, relativePath); never executed: return qMakePair(candidatePath, relativePath); | 0 | ||||||||||||
152 | } never executed: end of block | 0 | ||||||||||||
153 | } never executed: end of block | 0 | ||||||||||||
154 | } | - | ||||||||||||
155 | - | |||||||||||||
156 | - | |||||||||||||
157 | if (ver.isEmpty()
| 0 | ||||||||||||
158 | break; never executed: break; | 0 | ||||||||||||
159 | - | |||||||||||||
160 | int lastDot = ver.lastIndexOf(dot); | - | ||||||||||||
161 | if (lastDot == -1
| 0 | ||||||||||||
162 | ver.clear(); never executed: ver.clear(); | 0 | ||||||||||||
163 | else | - | ||||||||||||
164 | ver = ver.mid(0, lastDot); never executed: ver = ver.mid(0, lastDot); | 0 | ||||||||||||
165 | } | - | ||||||||||||
166 | - | |||||||||||||
167 | return never executed: QPair<QString, QString>();return QPair<QString, QString>(); never executed: return QPair<QString, QString>(); | 0 | ||||||||||||
168 | } | - | ||||||||||||
169 | - | |||||||||||||
170 | - | |||||||||||||
171 | QVariantList findPathsForModuleImports(const QVariantList &imports) | - | ||||||||||||
172 | { | - | ||||||||||||
173 | QVariantList done; | - | ||||||||||||
174 | QVariantList importsCopy(imports); | - | ||||||||||||
175 | - | |||||||||||||
176 | for (int i = 0; i < importsCopy.length()
| 0 | ||||||||||||
177 | QVariantMap import = qvariant_cast<QVariantMap>(importsCopy.at(i)); | - | ||||||||||||
178 | if (import.value(typeLiteral()) == QLatin1String("module")
| 0 | ||||||||||||
179 | const QPair<QString, QString> paths = | - | ||||||||||||
180 | resolveImportPath(import.value(nameLiteral()).toString(), import.value(versionLiteral()).toString()); | - | ||||||||||||
181 | if (!paths.first.isEmpty()
| 0 | ||||||||||||
182 | import.insert(pathLiteral(), paths.first); | - | ||||||||||||
183 | import.insert(relativePathLiteral(), paths.second); | - | ||||||||||||
184 | } never executed: end of block | 0 | ||||||||||||
185 | QVariantMap plugininfo = pluginsForModulePath(import.value(pathLiteral()).toString()); | - | ||||||||||||
186 | QString plugins = plugininfo.value(pluginsLiteral()).toString(); | - | ||||||||||||
187 | QString classnames = plugininfo.value(classnamesLiteral()).toString(); | - | ||||||||||||
188 | if (!plugins.isEmpty()
| 0 | ||||||||||||
189 | import.insert(([]() noexcept -> QString { enum { Size = sizeof(u"" "plugin")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "plugin" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return never executed: qstring_literal_temp;return qstring_literal_temp; never executed: }()), plugins);return qstring_literal_temp; never executed: import.insert(([]() noexcept -> QString { enum { Size = sizeof(u"" "plugin")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "plugin" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()), plugins); | 0 | ||||||||||||
190 | if (!classnames.isEmpty()
| 0 | ||||||||||||
191 | import.insert(([]() noexcept -> QString { enum { Size = sizeof(u"" "classname")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "classname" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return never executed: qstring_literal_temp;return qstring_literal_temp; never executed: }()), classnames);return qstring_literal_temp; never executed: import.insert(([]() noexcept -> QString { enum { Size = sizeof(u"" "classname")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "classname" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()), classnames); | 0 | ||||||||||||
192 | if (plugininfo.contains(dependenciesLiteral())
| 0 | ||||||||||||
193 | const QStringList dependencies = plugininfo.value(dependenciesLiteral()).toStringList(); | - | ||||||||||||
194 | for (const QString &line : dependencies) { | - | ||||||||||||
195 | const auto dep = line.splitRef(QLatin1Char(' ')); | - | ||||||||||||
196 | QVariantMap depImport; | - | ||||||||||||
197 | depImport[typeLiteral()] = ([]() noexcept -> QString { enum { Size = sizeof(u"" "module")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "module" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); | - | ||||||||||||
198 | depImport[nameLiteral()] = dep[0].toString(); | - | ||||||||||||
199 | depImport[versionLiteral()] = dep[1].toString(); | - | ||||||||||||
200 | importsCopy.append(depImport); | - | ||||||||||||
201 | } never executed: end of block | 0 | ||||||||||||
202 | } never executed: end of block | 0 | ||||||||||||
203 | } never executed: end of block | 0 | ||||||||||||
204 | done.append(import); | - | ||||||||||||
205 | } never executed: end of block | 0 | ||||||||||||
206 | return never executed: done;return done; never executed: return done; | 0 | ||||||||||||
207 | } | - | ||||||||||||
208 | - | |||||||||||||
209 | - | |||||||||||||
210 | static QVariantList findQmlImportsInQmlCode(const QString &filePath, const QString &code) | - | ||||||||||||
211 | { | - | ||||||||||||
212 | QQmlJS::Engine engine; | - | ||||||||||||
213 | QQmlJS::Lexer lexer(&engine); | - | ||||||||||||
214 | lexer.setCode(code, 1); | - | ||||||||||||
215 | QQmlJS::Parser parser(&engine); | - | ||||||||||||
216 | - | |||||||||||||
217 | if (!parser.parse()
| 0 | ||||||||||||
218 | - | |||||||||||||
219 | const auto diagnosticMessages = parser.diagnosticMessages(); | - | ||||||||||||
220 | for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) { | - | ||||||||||||
221 | std::cerr << QDir::toNativeSeparators(filePath).toStdString() << ':' | - | ||||||||||||
222 | << m.loc.startLine << ':' << m.message.toStdString() << std::endl; | - | ||||||||||||
223 | } never executed: end of block | 0 | ||||||||||||
224 | return never executed: QVariantList();return QVariantList(); never executed: return QVariantList(); | 0 | ||||||||||||
225 | } | - | ||||||||||||
226 | return never executed: findImportsInAst(parser.ast()->headers, code, filePath);return findImportsInAst(parser.ast()->headers, code, filePath); never executed: return findImportsInAst(parser.ast()->headers, code, filePath); | 0 | ||||||||||||
227 | } | - | ||||||||||||
228 | - | |||||||||||||
229 | - | |||||||||||||
230 | static QVariantList findQmlImportsInQmlFile(const QString &filePath) | - | ||||||||||||
231 | { | - | ||||||||||||
232 | QFile file(filePath); | - | ||||||||||||
233 | if (!file.open(QIODevice::ReadOnly)
| 0 | ||||||||||||
234 | std::cerr << "Cannot open input file " << QDir::toNativeSeparators(file.fileName()).toStdString() | - | ||||||||||||
235 | << ':' << file.errorString().toStdString() << std::endl; | - | ||||||||||||
236 | return never executed: QVariantList();return QVariantList(); never executed: return QVariantList(); | 0 | ||||||||||||
237 | } | - | ||||||||||||
238 | QString code = QString::fromUtf8(file.readAll()); | - | ||||||||||||
239 | return never executed: findQmlImportsInQmlCode(filePath, code);return findQmlImportsInQmlCode(filePath, code); never executed: return findQmlImportsInQmlCode(filePath, code); | 0 | ||||||||||||
240 | } | - | ||||||||||||
241 | - | |||||||||||||
242 | struct ImportCollector : public QQmlJS::Directives | - | ||||||||||||
243 | { | - | ||||||||||||
244 | QVariantList imports; | - | ||||||||||||
245 | - | |||||||||||||
246 | void importFile(const QString &jsfile, const QString &module, int line, int column) override | - | ||||||||||||
247 | { | - | ||||||||||||
248 | QVariantMap entry; | - | ||||||||||||
249 | entry[typeLiteral()] = ([]() noexcept -> QString { enum { Size = sizeof(u"" "javascript")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "javascript" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); | - | ||||||||||||
250 | entry[pathLiteral()] = jsfile; | - | ||||||||||||
251 | imports << entry; | - | ||||||||||||
252 | - | |||||||||||||
253 | (void)module;; | - | ||||||||||||
254 | (void)line;; | - | ||||||||||||
255 | (void)column;; | - | ||||||||||||
256 | } never executed: end of block | 0 | ||||||||||||
257 | - | |||||||||||||
258 | void importModule(const QString &uri, const QString &version, const QString &module, int line, int column) override | - | ||||||||||||
259 | { | - | ||||||||||||
260 | QVariantMap entry; | - | ||||||||||||
261 | if (uri.contains(QLatin1Char('/'))
| 0 | ||||||||||||
262 | entry[typeLiteral()] = ([]() noexcept -> QString { enum { Size = sizeof(u"" "directory")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "directory" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); | - | ||||||||||||
263 | entry[nameLiteral()] = uri; | - | ||||||||||||
264 | } never executed: else {end of block | 0 | ||||||||||||
265 | entry[typeLiteral()] = ([]() noexcept -> QString { enum { Size = sizeof(u"" "module")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "module" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); | - | ||||||||||||
266 | entry[nameLiteral()] = uri; | - | ||||||||||||
267 | entry[versionLiteral()] = version; | - | ||||||||||||
268 | } never executed: end of block | 0 | ||||||||||||
269 | imports << entry; | - | ||||||||||||
270 | - | |||||||||||||
271 | (void)module;; | - | ||||||||||||
272 | (void)line;; | - | ||||||||||||
273 | (void)column;; | - | ||||||||||||
274 | } never executed: end of block | 0 | ||||||||||||
275 | }; | - | ||||||||||||
276 | - | |||||||||||||
277 | - | |||||||||||||
278 | QVariantList findQmlImportsInJavascriptFile(const QString &filePath) | - | ||||||||||||
279 | { | - | ||||||||||||
280 | QFile file(filePath); | - | ||||||||||||
281 | if (!file.open(QIODevice::ReadOnly)
| 0 | ||||||||||||
282 | std::cerr << "Cannot open input file " << QDir::toNativeSeparators(file.fileName()).toStdString() | - | ||||||||||||
283 | << ':' << file.errorString().toStdString() << std::endl; | - | ||||||||||||
284 | return never executed: QVariantList();return QVariantList(); never executed: return QVariantList(); | 0 | ||||||||||||
285 | } | - | ||||||||||||
286 | - | |||||||||||||
287 | QString sourceCode = QString::fromUtf8(file.readAll()); | - | ||||||||||||
288 | file.close(); | - | ||||||||||||
289 | - | |||||||||||||
290 | QQmlJS::Engine ee; | - | ||||||||||||
291 | ImportCollector collector; | - | ||||||||||||
292 | ee.setDirectives(&collector); | - | ||||||||||||
293 | QQmlJS::Lexer lexer(&ee); | - | ||||||||||||
294 | lexer.setCode(sourceCode, 1, false); | - | ||||||||||||
295 | QQmlJS::Parser parser(&ee); | - | ||||||||||||
296 | parser.parseProgram(); | - | ||||||||||||
297 | - | |||||||||||||
298 | const auto diagnosticMessages = parser.diagnosticMessages(); | - | ||||||||||||
299 | for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) | - | ||||||||||||
300 | if (m.isError()
| 0 | ||||||||||||
301 | return never executed: QVariantList();return QVariantList(); never executed: return QVariantList(); | 0 | ||||||||||||
302 | - | |||||||||||||
303 | return never executed: collector.imports;return collector.imports; never executed: return collector.imports; | 0 | ||||||||||||
304 | } | - | ||||||||||||
305 | - | |||||||||||||
306 | - | |||||||||||||
307 | QVariantList findQmlImportsInFile(const QString &filePath) | - | ||||||||||||
308 | { | - | ||||||||||||
309 | QVariantList imports; | - | ||||||||||||
310 | if (filePath == QLatin1String("-")
| 0 | ||||||||||||
311 | QFile f; | - | ||||||||||||
312 | if (f.open(
| 0 | ||||||||||||
313 | stdin
| 0 | ||||||||||||
314 | , QIODevice::ReadOnly)
| 0 | ||||||||||||
315 | imports = findQmlImportsInQmlCode(QLatin1String("<stdin>"), QString::fromUtf8(f.readAll())); never executed: imports = findQmlImportsInQmlCode(QLatin1String("<stdin>"), QString::fromUtf8(f.readAll())); | 0 | ||||||||||||
316 | } never executed: else if (filePath.endsWith(QLatin1String(".qml"))end of block
| 0 | ||||||||||||
317 | imports = findQmlImportsInQmlFile(filePath); | - | ||||||||||||
318 | } never executed: else if (filePath.endsWith(QLatin1String(".js"))end of block
| 0 | ||||||||||||
319 | imports = findQmlImportsInJavascriptFile(filePath); | - | ||||||||||||
320 | } never executed: end of block | 0 | ||||||||||||
321 | - | |||||||||||||
322 | return never executed: findPathsForModuleImports(imports);return findPathsForModuleImports(imports); never executed: return findPathsForModuleImports(imports); | 0 | ||||||||||||
323 | } | - | ||||||||||||
324 | - | |||||||||||||
325 | - | |||||||||||||
326 | QVariantList mergeImports(const QVariantList &a, const QVariantList &b) | - | ||||||||||||
327 | { | - | ||||||||||||
328 | QVariantList merged = a; | - | ||||||||||||
329 | for (const QVariant &variant : b) { | - | ||||||||||||
330 | if (!merged.contains(variant)
| 0 | ||||||||||||
331 | merged.append(variant); never executed: merged.append(variant); | 0 | ||||||||||||
332 | } never executed: end of block | 0 | ||||||||||||
333 | return never executed: merged;return merged; never executed: return merged; | 0 | ||||||||||||
334 | } | - | ||||||||||||
335 | - | |||||||||||||
336 | - | |||||||||||||
337 | - | |||||||||||||
338 | struct isMetainfo { | - | ||||||||||||
339 | bool operator() (const QFileInfo &x) const { | - | ||||||||||||
340 | return never executed: x.suffix() == QLatin1String("metainfo");return x.suffix() == QLatin1String("metainfo"); never executed: return x.suffix() == QLatin1String("metainfo"); | 0 | ||||||||||||
341 | } | - | ||||||||||||
342 | }; | - | ||||||||||||
343 | - | |||||||||||||
344 | struct pathStartsWith { | - | ||||||||||||
345 | pathStartsWith(const QString &path) : _path(path) {} never executed: end of block | 0 | ||||||||||||
346 | bool operator() (const QString &x) const { | - | ||||||||||||
347 | return never executed: _path.startsWith(x);return _path.startsWith(x); never executed: return _path.startsWith(x); | 0 | ||||||||||||
348 | } | - | ||||||||||||
349 | const QString _path; | - | ||||||||||||
350 | }; | - | ||||||||||||
351 | - | |||||||||||||
352 | - | |||||||||||||
353 | - | |||||||||||||
354 | - | |||||||||||||
355 | QVariantList findQmlImportsInDirectory(const QString &qmlDir) | - | ||||||||||||
356 | { | - | ||||||||||||
357 | QVariantList ret; | - | ||||||||||||
358 | if (qmlDir.isEmpty()
| 0 | ||||||||||||
359 | return never executed: ret;return ret; never executed: return ret; | 0 | ||||||||||||
360 | - | |||||||||||||
361 | QDirIterator iterator(qmlDir, QDir::AllDirs | QDir::NoDotDot, QDirIterator::Subdirectories); | - | ||||||||||||
362 | QStringList blacklist; | - | ||||||||||||
363 | - | |||||||||||||
364 | while (iterator.hasNext()
| 0 | ||||||||||||
365 | iterator.next(); | - | ||||||||||||
366 | const QString path = iterator.filePath(); | - | ||||||||||||
367 | const QFileInfoList entries = QDir(path).entryInfoList(); | - | ||||||||||||
368 | - | |||||||||||||
369 | - | |||||||||||||
370 | if (std::find_if(entries.cbegin(), entries.cend(), isMetainfo()) != entries.cend()
| 0 | ||||||||||||
371 | blacklist << path; | - | ||||||||||||
372 | continue; never executed: continue; | 0 | ||||||||||||
373 | } | - | ||||||||||||
374 | - | |||||||||||||
375 | if (std::find_if(blacklist.cbegin(), blacklist.cend(), pathStartsWith(path)) != blacklist.cend()
| 0 | ||||||||||||
376 | continue; never executed: continue; | 0 | ||||||||||||
377 | - | |||||||||||||
378 | - | |||||||||||||
379 | if (path.contains(QLatin1String("Debug-iphoneos"))
| 0 | ||||||||||||
380 | path.contains(QLatin1String("Debug-iphonesimulator"))
| 0 | ||||||||||||
381 | - | |||||||||||||
382 | - | |||||||||||||
383 | - | |||||||||||||
384 | ){ | - | ||||||||||||
385 | continue; never executed: continue; | 0 | ||||||||||||
386 | } | - | ||||||||||||
387 | - | |||||||||||||
388 | for (const QFileInfo &x : entries) | - | ||||||||||||
389 | if (x.isFile()
| 0 | ||||||||||||
390 | ret = mergeImports(ret, findQmlImportsInFile(x.absoluteFilePath())); never executed: ret = mergeImports(ret, findQmlImportsInFile(x.absoluteFilePath())); | 0 | ||||||||||||
391 | } never executed: end of block | 0 | ||||||||||||
392 | return never executed: ret;return ret; never executed: return ret; | 0 | ||||||||||||
393 | } | - | ||||||||||||
394 | - | |||||||||||||
395 | QSet<QString> importModulePaths(const QVariantList &imports) { | - | ||||||||||||
396 | QSet<QString> ret; | - | ||||||||||||
397 | for (const QVariant &importVariant : imports) { | - | ||||||||||||
398 | QVariantMap import = qvariant_cast<QVariantMap>(importVariant); | - | ||||||||||||
399 | QString path = import.value(pathLiteral()).toString(); | - | ||||||||||||
400 | QString type = import.value(typeLiteral()).toString(); | - | ||||||||||||
401 | if (type == QLatin1String("module")
| 0 | ||||||||||||
402 | ret.insert(QDir(path).canonicalPath()); never executed: ret.insert(QDir(path).canonicalPath()); | 0 | ||||||||||||
403 | } never executed: end of block | 0 | ||||||||||||
404 | return never executed: ret;return ret; never executed: return ret; | 0 | ||||||||||||
405 | } | - | ||||||||||||
406 | - | |||||||||||||
407 | - | |||||||||||||
408 | - | |||||||||||||
409 | - | |||||||||||||
410 | QVariantList findQmlImportsRecursively(const QStringList &qmlDirs, const QStringList &scanFiles) | - | ||||||||||||
411 | { | - | ||||||||||||
412 | QVariantList ret; | - | ||||||||||||
413 | - | |||||||||||||
414 | - | |||||||||||||
415 | for (const QString &qmlDir : qmlDirs) { | - | ||||||||||||
416 | QVariantList imports = findQmlImportsInDirectory(qmlDir); | - | ||||||||||||
417 | ret = mergeImports(ret, imports); | - | ||||||||||||
418 | } never executed: end of block | 0 | ||||||||||||
419 | - | |||||||||||||
420 | - | |||||||||||||
421 | for (const QString &file : scanFiles) { | - | ||||||||||||
422 | QVariantList imports = findQmlImportsInFile(file); | - | ||||||||||||
423 | ret = mergeImports(ret, imports); | - | ||||||||||||
424 | } never executed: end of block | 0 | ||||||||||||
425 | - | |||||||||||||
426 | - | |||||||||||||
427 | - | |||||||||||||
428 | QSet<QString> toVisit = importModulePaths(ret); | - | ||||||||||||
429 | - | |||||||||||||
430 | - | |||||||||||||
431 | QSet<QString> visited; | - | ||||||||||||
432 | while (!toVisit.isEmpty()
| 0 | ||||||||||||
433 | QString qmlDir = *toVisit.begin(); | - | ||||||||||||
434 | toVisit.erase(toVisit.begin()); | - | ||||||||||||
435 | visited.insert(qmlDir); | - | ||||||||||||
436 | - | |||||||||||||
437 | QVariantList imports = findQmlImportsInDirectory(qmlDir); | - | ||||||||||||
438 | ret = mergeImports(ret, imports); | - | ||||||||||||
439 | - | |||||||||||||
440 | QSet<QString> candidatePaths = importModulePaths(ret); | - | ||||||||||||
441 | candidatePaths.subtract(visited); | - | ||||||||||||
442 | toVisit.unite(candidatePaths); | - | ||||||||||||
443 | } never executed: end of block | 0 | ||||||||||||
444 | return never executed: ret;return ret; never executed: return ret; | 0 | ||||||||||||
445 | } | - | ||||||||||||
446 | - | |||||||||||||
447 | int main(int argc, char *argv[]) | - | ||||||||||||
448 | { | - | ||||||||||||
449 | QCoreApplication app(argc, argv); | - | ||||||||||||
450 | QCoreApplication::setApplicationVersion(QLatin1String("5.12.0")); | - | ||||||||||||
451 | QStringList args = app.arguments(); | - | ||||||||||||
452 | const QString appName = QFileInfo(app.applicationFilePath()).baseName(); | - | ||||||||||||
453 | if (args.size() < 2
| 0 | ||||||||||||
454 | printUsage(appName); | - | ||||||||||||
455 | return never executed: 1;return 1; never executed: return 1; | 0 | ||||||||||||
456 | } | - | ||||||||||||
457 | - | |||||||||||||
458 | QStringList qmlRootPaths; | - | ||||||||||||
459 | QStringList scanFiles; | - | ||||||||||||
460 | QStringList qmlImportPaths; | - | ||||||||||||
461 | - | |||||||||||||
462 | int i = 1; | - | ||||||||||||
463 | while (i < args.count()
| 0 | ||||||||||||
464 | const QString &arg = args.at(i); | - | ||||||||||||
465 | ++i; | - | ||||||||||||
466 | QStringList *argReceiver = nullptr; | - | ||||||||||||
467 | if (!arg.startsWith(QLatin1Char('-'))
| 0 | ||||||||||||
468 | qmlRootPaths += arg; | - | ||||||||||||
469 | } never executed: else if (arg == QLatin1String("-rootPath")end of block
| 0 | ||||||||||||
470 | if (i >= args.count()
| 0 | ||||||||||||
471 | std::cerr << "-rootPath requires an argument\n"; never executed: std::cerr << "-rootPath requires an argument\n"; | 0 | ||||||||||||
472 | argReceiver = &qmlRootPaths; | - | ||||||||||||
473 | } never executed: else if (arg == QLatin1String("-qmlFiles")end of block
| 0 | ||||||||||||
474 | if (i >= args.count()
| 0 | ||||||||||||
475 | std::cerr << "-qmlFiles requires an argument\n"; never executed: std::cerr << "-qmlFiles requires an argument\n"; | 0 | ||||||||||||
476 | argReceiver = &scanFiles; | - | ||||||||||||
477 | } never executed: else if (arg == QLatin1String("-jsFiles")end of block
| 0 | ||||||||||||
478 | if (i >= args.count()
| 0 | ||||||||||||
479 | std::cerr << "-jsFiles requires an argument\n"; never executed: std::cerr << "-jsFiles requires an argument\n"; | 0 | ||||||||||||
480 | argReceiver = &scanFiles; | - | ||||||||||||
481 | } never executed: else if (arg == QLatin1String("-importPath")end of block
| 0 | ||||||||||||
482 | if (i >= args.count()
| 0 | ||||||||||||
483 | std::cerr << "-importPath requires an argument\n"; never executed: std::cerr << "-importPath requires an argument\n"; | 0 | ||||||||||||
484 | argReceiver = &qmlImportPaths; | - | ||||||||||||
485 | } never executed: else {end of block | 0 | ||||||||||||
486 | std::cerr << QtPrivate::asString(appName).toLocal8Bit().constData() << ": Invalid argument: \"" | - | ||||||||||||
487 | << QtPrivate::asString(arg).toLocal8Bit().constData() << "\"\n"; | - | ||||||||||||
488 | return never executed: 1;return 1; never executed: return 1; | 0 | ||||||||||||
489 | } | - | ||||||||||||
490 | - | |||||||||||||
491 | while (i < args.count()
| 0 | ||||||||||||
492 | const QString arg = args.at(i); | - | ||||||||||||
493 | if (arg.startsWith(QLatin1Char('-'))
| 0 | ||||||||||||
494 | break; never executed: break; | 0 | ||||||||||||
495 | ++i; | - | ||||||||||||
496 | if (arg != QLatin1String("-")
| 0 | ||||||||||||
497 | std::cerr << QtPrivate::asString(appName).toLocal8Bit().constData() << ": No such file or directory: \"" | - | ||||||||||||
498 | << QtPrivate::asString(arg).toLocal8Bit().constData() << "\"\n"; | - | ||||||||||||
499 | return never executed: 1;return 1; never executed: return 1; | 0 | ||||||||||||
500 | } else { | - | ||||||||||||
501 | *argReceiver += arg; | - | ||||||||||||
502 | } never executed: end of block | 0 | ||||||||||||
503 | } | - | ||||||||||||
504 | } never executed: end of block | 0 | ||||||||||||
505 | - | |||||||||||||
506 | g_qmlImportPaths = qmlImportPaths; | - | ||||||||||||
507 | - | |||||||||||||
508 | - | |||||||||||||
509 | QVariantList imports = findQmlImportsRecursively(qmlRootPaths, scanFiles); | - | ||||||||||||
510 | - | |||||||||||||
511 | - | |||||||||||||
512 | QByteArray json = QJsonDocument(QJsonArray::fromVariantList(imports)).toJson(); | - | ||||||||||||
513 | std::cout << json.constData() << std::endl; | - | ||||||||||||
514 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||
515 | } | - | ||||||||||||
Switch to Source code | Preprocessed file |