Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/tools/qmlmin/main.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||||||||
2 | ** | - | ||||||||||||||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||||||||
5 | ** | - | ||||||||||||||||||||||||
6 | ** This file is part of the QtQml module of the Qt Toolkit. | - | ||||||||||||||||||||||||
7 | ** | - | ||||||||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | - | ||||||||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||||||||
16 | ** | - | ||||||||||||||||||||||||
17 | ** GNU General Public License Usage | - | ||||||||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||||||||
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | - | ||||||||||||||||||||||||
21 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||||||||
22 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||||||||
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||||||||
24 | ** | - | ||||||||||||||||||||||||
25 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||||||||
26 | ** | - | ||||||||||||||||||||||||
27 | ****************************************************************************/ | - | ||||||||||||||||||||||||
28 | - | |||||||||||||||||||||||||
29 | #include <private/qqmljsengine_p.h> | - | ||||||||||||||||||||||||
30 | #include <private/qqmljslexer_p.h> | - | ||||||||||||||||||||||||
31 | #include <private/qqmljsparser_p.h> | - | ||||||||||||||||||||||||
32 | #include <QtCore/QCoreApplication> | - | ||||||||||||||||||||||||
33 | #include <QtCore/QStringList> | - | ||||||||||||||||||||||||
34 | #include <QtCore/QFile> | - | ||||||||||||||||||||||||
35 | #include <QtCore/QFileInfo> | - | ||||||||||||||||||||||||
36 | #include <QtCore/QDir> | - | ||||||||||||||||||||||||
37 | #include <iostream> | - | ||||||||||||||||||||||||
38 | #include <cstdlib> | - | ||||||||||||||||||||||||
39 | - | |||||||||||||||||||||||||
40 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||||||||
41 | - | |||||||||||||||||||||||||
42 | // | - | ||||||||||||||||||||||||
43 | // QML/JS minifier | - | ||||||||||||||||||||||||
44 | // | - | ||||||||||||||||||||||||
45 | namespace QQmlJS { | - | ||||||||||||||||||||||||
46 | - | |||||||||||||||||||||||||
47 | enum RegExpFlag { | - | ||||||||||||||||||||||||
48 | Global = 0x01, | - | ||||||||||||||||||||||||
49 | IgnoreCase = 0x02, | - | ||||||||||||||||||||||||
50 | Multiline = 0x04 | - | ||||||||||||||||||||||||
51 | }; | - | ||||||||||||||||||||||||
52 | - | |||||||||||||||||||||||||
53 | - | |||||||||||||||||||||||||
54 | class QmlminLexer: protected Lexer, public Directives | - | ||||||||||||||||||||||||
55 | { | - | ||||||||||||||||||||||||
56 | QQmlJS::Engine _engine; | - | ||||||||||||||||||||||||
57 | QString _fileName; | - | ||||||||||||||||||||||||
58 | QString _directives; | - | ||||||||||||||||||||||||
59 | - | |||||||||||||||||||||||||
60 | protected: | - | ||||||||||||||||||||||||
61 | QVector<int> _stateStack; | - | ||||||||||||||||||||||||
62 | QList<int> _tokens; | - | ||||||||||||||||||||||||
63 | QList<QString> _tokenStrings; | - | ||||||||||||||||||||||||
64 | int yytoken = -1; | - | ||||||||||||||||||||||||
65 | QString yytokentext; | - | ||||||||||||||||||||||||
66 | - | |||||||||||||||||||||||||
67 | void lex() { | - | ||||||||||||||||||||||||
68 | if (_tokens.isEmpty()) {
| 339844-8894968 | ||||||||||||||||||||||||
69 | _tokens.append(Lexer::lex()); | - | ||||||||||||||||||||||||
70 | _tokenStrings.append(tokenText()); | - | ||||||||||||||||||||||||
71 | } executed 8894968 times by 1 test: end of block Executed by:
| 8894968 | ||||||||||||||||||||||||
72 | - | |||||||||||||||||||||||||
73 | yytoken = _tokens.takeFirst(); | - | ||||||||||||||||||||||||
74 | yytokentext = _tokenStrings.takeFirst(); | - | ||||||||||||||||||||||||
75 | } executed 9234812 times by 1 test: end of block Executed by:
| 9234812 | ||||||||||||||||||||||||
76 | - | |||||||||||||||||||||||||
77 | int lookaheadToken() | - | ||||||||||||||||||||||||
78 | { | - | ||||||||||||||||||||||||
79 | if (yytoken < 0)
| 398546-851740 | ||||||||||||||||||||||||
80 | lex(); executed 851740 times by 1 test: lex(); Executed by:
| 851740 | ||||||||||||||||||||||||
81 | return yytoken; executed 1250286 times by 1 test: return yytoken; Executed by:
| 1250286 | ||||||||||||||||||||||||
82 | } | - | ||||||||||||||||||||||||
83 | - | |||||||||||||||||||||||||
84 | void pushToken(int token) | - | ||||||||||||||||||||||||
85 | { | - | ||||||||||||||||||||||||
86 | _tokens.prepend(yytoken); | - | ||||||||||||||||||||||||
87 | _tokenStrings.prepend(yytokentext); | - | ||||||||||||||||||||||||
88 | yytoken = token; | - | ||||||||||||||||||||||||
89 | yytokentext = QString(); | - | ||||||||||||||||||||||||
90 | } executed 48632 times by 1 test: end of block Executed by:
| 48632 | ||||||||||||||||||||||||
91 | - | |||||||||||||||||||||||||
92 | public: | - | ||||||||||||||||||||||||
93 | QmlminLexer() | - | ||||||||||||||||||||||||
94 | : Lexer(&_engine), _stateStack(128) {} executed 39940 times by 1 test: end of block Executed by:
| 39940 | ||||||||||||||||||||||||
95 | virtual ~QmlminLexer() {} | - | ||||||||||||||||||||||||
96 | - | |||||||||||||||||||||||||
97 | QString fileName() const { return _fileName; } executed 76 times by 1 test: return _fileName; Executed by:
| 76 | ||||||||||||||||||||||||
98 | - | |||||||||||||||||||||||||
99 | bool operator()(const QString &fileName, const QString &code) | - | ||||||||||||||||||||||||
100 | { | - | ||||||||||||||||||||||||
101 | int startToken = T_FEED_JS_SCRIPT; | - | ||||||||||||||||||||||||
102 | const QFileInfo fileInfo(fileName); | - | ||||||||||||||||||||||||
103 | if (fileInfo.suffix().toLower() == QLatin1String("qml"))
| 10608-29332 | ||||||||||||||||||||||||
104 | startToken = T_FEED_UI_PROGRAM; executed 29332 times by 1 test: startToken = T_FEED_UI_PROGRAM; Executed by:
| 29332 | ||||||||||||||||||||||||
105 | setCode(code, /*line = */ 1, /*qmlMode = */ startToken == T_FEED_UI_PROGRAM); | - | ||||||||||||||||||||||||
106 | _fileName = fileName; | - | ||||||||||||||||||||||||
107 | _directives.clear(); | - | ||||||||||||||||||||||||
108 | return parse(startToken); executed 39940 times by 1 test: return parse(startToken); Executed by:
| 39940 | ||||||||||||||||||||||||
109 | } | - | ||||||||||||||||||||||||
110 | - | |||||||||||||||||||||||||
111 | QString directives() | - | ||||||||||||||||||||||||
112 | { | - | ||||||||||||||||||||||||
113 | return _directives; executed 10586 times by 1 test: return _directives; Executed by:
| 10586 | ||||||||||||||||||||||||
114 | } | - | ||||||||||||||||||||||||
115 | - | |||||||||||||||||||||||||
116 | // | - | ||||||||||||||||||||||||
117 | // Handle the .pragma/.import directives | - | ||||||||||||||||||||||||
118 | // | - | ||||||||||||||||||||||||
119 | void pragmaLibrary() override | - | ||||||||||||||||||||||||
120 | { | - | ||||||||||||||||||||||||
121 | _directives += QLatin1String(".pragma library\n"); | - | ||||||||||||||||||||||||
122 | } executed 178 times by 1 test: end of block Executed by:
| 178 | ||||||||||||||||||||||||
123 | - | |||||||||||||||||||||||||
124 | void importFile(const QString &jsfile, const QString &module, int line, int column) override | - | ||||||||||||||||||||||||
125 | { | - | ||||||||||||||||||||||||
126 | _directives += QLatin1String(".import"); | - | ||||||||||||||||||||||||
127 | _directives += QLatin1Char('"'); | - | ||||||||||||||||||||||||
128 | _directives += quote(jsfile); | - | ||||||||||||||||||||||||
129 | _directives += QLatin1Char('"'); | - | ||||||||||||||||||||||||
130 | _directives += QLatin1String("as "); | - | ||||||||||||||||||||||||
131 | _directives += module; | - | ||||||||||||||||||||||||
132 | _directives += QLatin1Char('\n'); | - | ||||||||||||||||||||||||
133 | Q_UNUSED(line); | - | ||||||||||||||||||||||||
134 | Q_UNUSED(column); | - | ||||||||||||||||||||||||
135 | } executed 872 times by 1 test: end of block Executed by:
| 872 | ||||||||||||||||||||||||
136 | - | |||||||||||||||||||||||||
137 | void importModule(const QString &uri, const QString &version, const QString &module, int line, int column) override | - | ||||||||||||||||||||||||
138 | { | - | ||||||||||||||||||||||||
139 | _directives += QLatin1String(".import "); | - | ||||||||||||||||||||||||
140 | _directives += uri; | - | ||||||||||||||||||||||||
141 | _directives += QLatin1Char(' '); | - | ||||||||||||||||||||||||
142 | _directives += version; | - | ||||||||||||||||||||||||
143 | _directives += QLatin1String(" as "); | - | ||||||||||||||||||||||||
144 | _directives += module; | - | ||||||||||||||||||||||||
145 | _directives += QLatin1Char('\n'); | - | ||||||||||||||||||||||||
146 | Q_UNUSED(line); | - | ||||||||||||||||||||||||
147 | Q_UNUSED(column); | - | ||||||||||||||||||||||||
148 | } executed 312 times by 1 test: end of block Executed by:
| 312 | ||||||||||||||||||||||||
149 | - | |||||||||||||||||||||||||
150 | protected: | - | ||||||||||||||||||||||||
151 | virtual bool parse(int startToken) = 0; | - | ||||||||||||||||||||||||
152 | - | |||||||||||||||||||||||||
153 | static QString quote(const QString &string) | - | ||||||||||||||||||||||||
154 | { | - | ||||||||||||||||||||||||
155 | QString quotedString; | - | ||||||||||||||||||||||||
156 | for (const QChar &ch : string) { | - | ||||||||||||||||||||||||
157 | if (ch == QLatin1Char('"'))
| 19620-3624962 | ||||||||||||||||||||||||
158 | quotedString += QLatin1String("\\\""); executed 19620 times by 1 test: quotedString += QLatin1String("\\\""); Executed by:
| 19620 | ||||||||||||||||||||||||
159 | else { | - | ||||||||||||||||||||||||
160 | if (ch == QLatin1Char('\\')) quotedString += QLatin1String("\\\\"); executed 2024 times by 1 test: quotedString += QLatin1String("\\\\"); Executed by:
| 2024-3622938 | ||||||||||||||||||||||||
161 | else if (ch == QLatin1Char('\"')) quotedString += QLatin1String("\\\""); never executed: quotedString += QLatin1String("\\\"");
| 0-3622938 | ||||||||||||||||||||||||
162 | else if (ch == QLatin1Char('\b')) quotedString += QLatin1String("\\b"); executed 28 times by 1 test: quotedString += QLatin1String("\\b"); Executed by:
| 28-3622910 | ||||||||||||||||||||||||
163 | else if (ch == QLatin1Char('\f')) quotedString += QLatin1String("\\f"); executed 200 times by 1 test: quotedString += QLatin1String("\\f"); Executed by:
| 200-3622710 | ||||||||||||||||||||||||
164 | else if (ch == QLatin1Char('\n')) quotedString += QLatin1String("\\n"); executed 7328 times by 1 test: quotedString += QLatin1String("\\n"); Executed by:
| 7328-3615382 | ||||||||||||||||||||||||
165 | else if (ch == QLatin1Char('\r')) quotedString += QLatin1String("\\r"); executed 336 times by 1 test: quotedString += QLatin1String("\\r"); Executed by:
| 336-3615046 | ||||||||||||||||||||||||
166 | else if (ch == QLatin1Char('\t')) quotedString += QLatin1String("\\t"); executed 5864 times by 1 test: quotedString += QLatin1String("\\t"); Executed by:
| 5864-3609182 | ||||||||||||||||||||||||
167 | else if (ch == QLatin1Char('\v')) quotedString += QLatin1String("\\v"); executed 108 times by 1 test: quotedString += QLatin1String("\\v"); Executed by:
| 108-3609074 | ||||||||||||||||||||||||
168 | else if (ch == QLatin1Char('\0')) quotedString += QLatin1String("\\0"); executed 24 times by 1 test: quotedString += QLatin1String("\\0"); Executed by:
| 24-3609050 | ||||||||||||||||||||||||
169 | else quotedString += ch; executed 3609050 times by 1 test: quotedString += ch; Executed by:
| 3609050 | ||||||||||||||||||||||||
170 | } | - | ||||||||||||||||||||||||
171 | } | - | ||||||||||||||||||||||||
172 | return quotedString; executed 205310 times by 1 test: return quotedString; Executed by:
| 205310 | ||||||||||||||||||||||||
173 | } | - | ||||||||||||||||||||||||
174 | - | |||||||||||||||||||||||||
175 | bool isIdentChar(const QChar &ch) const | - | ||||||||||||||||||||||||
176 | { | - | ||||||||||||||||||||||||
177 | if (ch.isLetterOrNumber())
| 1609510-3510010 | ||||||||||||||||||||||||
178 | return true; executed 1609510 times by 1 test: return true; Executed by:
| 1609510 | ||||||||||||||||||||||||
179 | else if (ch == QLatin1Char('_') || ch == QLatin1Char('$'))
| 0-3509546 | ||||||||||||||||||||||||
180 | return true; executed 464 times by 1 test: return true; Executed by:
| 464 | ||||||||||||||||||||||||
181 | return false; executed 3509546 times by 1 test: return false; Executed by:
| 3509546 | ||||||||||||||||||||||||
182 | } | - | ||||||||||||||||||||||||
183 | - | |||||||||||||||||||||||||
184 | bool isRegExpRule(int ruleno) const | - | ||||||||||||||||||||||||
185 | { | - | ||||||||||||||||||||||||
186 | return ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 || executed 60844938 times by 1 test: return ruleno == 128 || ruleno == 129; Executed by:
| 60844938 | ||||||||||||||||||||||||
187 | ruleno == J_SCRIPT_REGEXPLITERAL_RULE2; executed 60844938 times by 1 test: return ruleno == 128 || ruleno == 129; Executed by:
| 60844938 | ||||||||||||||||||||||||
188 | } | - | ||||||||||||||||||||||||
189 | - | |||||||||||||||||||||||||
190 | void handleLookaheads(int ruleno) { | - | ||||||||||||||||||||||||
191 | if (ruleno == J_SCRIPT_EXPRESSIONSTATEMENTLOOKAHEAD_RULE) {
| 1250286-59594652 | ||||||||||||||||||||||||
192 | int token = lookaheadToken(); | - | ||||||||||||||||||||||||
193 | if (token == T_LBRACE)
| 33624-1216662 | ||||||||||||||||||||||||
194 | pushToken(T_FORCE_BLOCK); executed 33624 times by 1 test: pushToken(T_FORCE_BLOCK); Executed by:
| 33624 | ||||||||||||||||||||||||
195 | else if (token == T_FUNCTION || token == T_CLASS || token == T_LET || token == T_CONST)
| 0-1201654 | ||||||||||||||||||||||||
196 | pushToken(T_FORCE_DECLARATION); executed 15008 times by 1 test: pushToken(T_FORCE_DECLARATION); Executed by:
| 15008 | ||||||||||||||||||||||||
197 | } else if (ruleno == J_SCRIPT_CONCISEBODYLOOKAHEAD_RULE) { executed 1250286 times by 1 test: end of block Executed by:
| 0-59594652 | ||||||||||||||||||||||||
198 | int token = lookaheadToken(); | - | ||||||||||||||||||||||||
199 | if (token == T_LBRACE)
| 0 | ||||||||||||||||||||||||
200 | pushToken(T_FORCE_BLOCK); never executed: pushToken(T_FORCE_BLOCK); | 0 | ||||||||||||||||||||||||
201 | } else if (ruleno == J_SCRIPT_EXPORTDECLARATIONLOOKAHEAD_RULE) { never executed: end of block
| 0-59594652 | ||||||||||||||||||||||||
202 | int token = lookaheadToken(); | - | ||||||||||||||||||||||||
203 | if (token == T_FUNCTION || token == T_CLASS)
| 0 | ||||||||||||||||||||||||
204 | pushToken(T_FORCE_DECLARATION); never executed: pushToken(T_FORCE_DECLARATION); | 0 | ||||||||||||||||||||||||
205 | } never executed: end of block | 0 | ||||||||||||||||||||||||
206 | } executed 60844938 times by 1 test: end of block Executed by:
| 60844938 | ||||||||||||||||||||||||
207 | - | |||||||||||||||||||||||||
208 | bool scanRestOfRegExp(int ruleno, QString *restOfRegExp) | - | ||||||||||||||||||||||||
209 | { | - | ||||||||||||||||||||||||
210 | if (! scanRegExp(ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ? Lexer::NoPrefix : Lexer::EqualPrefix))
| 0-7464 | ||||||||||||||||||||||||
211 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
212 | - | |||||||||||||||||||||||||
213 | *restOfRegExp = regExpPattern(); | - | ||||||||||||||||||||||||
214 | if (ruleno == J_SCRIPT_REGEXPLITERAL_RULE2) {
| 0-7464 | ||||||||||||||||||||||||
215 | Q_ASSERT(! restOfRegExp->isEmpty()); | - | ||||||||||||||||||||||||
216 | Q_ASSERT(restOfRegExp->at(0) == QLatin1Char('=')); | - | ||||||||||||||||||||||||
217 | *restOfRegExp = restOfRegExp->mid(1); // strip the prefix | - | ||||||||||||||||||||||||
218 | } never executed: end of block | 0 | ||||||||||||||||||||||||
219 | *restOfRegExp += QLatin1Char('/'); | - | ||||||||||||||||||||||||
220 | const RegExpFlag flags = (RegExpFlag) regExpFlags(); | - | ||||||||||||||||||||||||
221 | if (flags & Global)
| 992-6472 | ||||||||||||||||||||||||
222 | *restOfRegExp += QLatin1Char('g'); executed 992 times by 1 test: *restOfRegExp += QLatin1Char('g'); Executed by:
| 992 | ||||||||||||||||||||||||
223 | if (flags & IgnoreCase)
| 1256-6208 | ||||||||||||||||||||||||
224 | *restOfRegExp += QLatin1Char('i'); executed 1256 times by 1 test: *restOfRegExp += QLatin1Char('i'); Executed by:
| 1256 | ||||||||||||||||||||||||
225 | if (flags & Multiline)
| 888-6576 | ||||||||||||||||||||||||
226 | *restOfRegExp += QLatin1Char('m'); executed 888 times by 1 test: *restOfRegExp += QLatin1Char('m'); Executed by:
| 888 | ||||||||||||||||||||||||
227 | - | |||||||||||||||||||||||||
228 | if (regExpFlags() == 0) {
| 2784-4680 | ||||||||||||||||||||||||
229 | // Add an extra space after the regexp literal delimiter (aka '/'). | - | ||||||||||||||||||||||||
230 | // This will avoid possible problems when pasting tokens like `instanceof' | - | ||||||||||||||||||||||||
231 | // after the regexp literal. | - | ||||||||||||||||||||||||
232 | *restOfRegExp += QLatin1Char(' '); | - | ||||||||||||||||||||||||
233 | } executed 4680 times by 1 test: end of block Executed by:
| 4680 | ||||||||||||||||||||||||
234 | return true; executed 7464 times by 1 test: return true; Executed by:
| 7464 | ||||||||||||||||||||||||
235 | } | - | ||||||||||||||||||||||||
236 | }; | - | ||||||||||||||||||||||||
237 | - | |||||||||||||||||||||||||
238 | - | |||||||||||||||||||||||||
239 | class Minify: public QmlminLexer | - | ||||||||||||||||||||||||
240 | { | - | ||||||||||||||||||||||||
241 | QString _minifiedCode; | - | ||||||||||||||||||||||||
242 | int _maxWidth; | - | ||||||||||||||||||||||||
243 | int _width; | - | ||||||||||||||||||||||||
244 | - | |||||||||||||||||||||||||
245 | public: | - | ||||||||||||||||||||||||
246 | Minify(int maxWidth); | - | ||||||||||||||||||||||||
247 | - | |||||||||||||||||||||||||
248 | QString minifiedCode() const; | - | ||||||||||||||||||||||||
249 | - | |||||||||||||||||||||||||
250 | protected: | - | ||||||||||||||||||||||||
251 | void append(const QString &s); | - | ||||||||||||||||||||||||
252 | bool parse(int startToken) override; | - | ||||||||||||||||||||||||
253 | void escape(const QChar &ch, QString *out); | - | ||||||||||||||||||||||||
254 | }; | - | ||||||||||||||||||||||||
255 | - | |||||||||||||||||||||||||
256 | Minify::Minify(int maxWidth) | - | ||||||||||||||||||||||||
257 | : _maxWidth(maxWidth), _width(0) | - | ||||||||||||||||||||||||
258 | { | - | ||||||||||||||||||||||||
259 | } executed 20008 times by 1 test: end of block Executed by:
| 20008 | ||||||||||||||||||||||||
260 | - | |||||||||||||||||||||||||
261 | QString Minify::minifiedCode() const | - | ||||||||||||||||||||||||
262 | { | - | ||||||||||||||||||||||||
263 | return _minifiedCode; executed 39864 times by 1 test: return _minifiedCode; Executed by:
| 39864 | ||||||||||||||||||||||||
264 | } | - | ||||||||||||||||||||||||
265 | - | |||||||||||||||||||||||||
266 | void Minify::append(const QString &s) | - | ||||||||||||||||||||||||
267 | { | - | ||||||||||||||||||||||||
268 | if (!s.isEmpty()) {
| 4892-548296 | ||||||||||||||||||||||||
269 | if (_maxWidth) {
| 0-548296 | ||||||||||||||||||||||||
270 | // Prefer not to exceed the maximum chars per line (but don't break up segments) | - | ||||||||||||||||||||||||
271 | int segmentLength = s.count(); | - | ||||||||||||||||||||||||
272 | if (_width && ((_width + segmentLength) > _maxWidth)) {
| 19770-528526 | ||||||||||||||||||||||||
273 | _minifiedCode.append(QLatin1Char('\n')); | - | ||||||||||||||||||||||||
274 | _width = 0; | - | ||||||||||||||||||||||||
275 | } executed 177076 times by 1 test: end of block Executed by:
| 177076 | ||||||||||||||||||||||||
276 | - | |||||||||||||||||||||||||
277 | _width += segmentLength; | - | ||||||||||||||||||||||||
278 | } executed 548296 times by 1 test: end of block Executed by:
| 548296 | ||||||||||||||||||||||||
279 | - | |||||||||||||||||||||||||
280 | _minifiedCode.append(s); | - | ||||||||||||||||||||||||
281 | } executed 548296 times by 1 test: end of block Executed by:
| 548296 | ||||||||||||||||||||||||
282 | } executed 553188 times by 1 test: end of block Executed by:
| 553188 | ||||||||||||||||||||||||
283 | - | |||||||||||||||||||||||||
284 | void Minify::escape(const QChar &ch, QString *out) | - | ||||||||||||||||||||||||
285 | { | - | ||||||||||||||||||||||||
286 | out->append(QLatin1String("\\u")); | - | ||||||||||||||||||||||||
287 | const QString hx = QString::number(ch.unicode(), 16); | - | ||||||||||||||||||||||||
288 | switch (hx.length()) { | - | ||||||||||||||||||||||||
289 | case 1: out->append(QLatin1String("000")); break; never executed: break; never executed: case 1: | 0 | ||||||||||||||||||||||||
290 | case 2: out->append(QLatin1String("00")); break; executed 4 times by 1 test: break; Executed by:
executed 4 times by 1 test: case 2: Executed by:
| 4 | ||||||||||||||||||||||||
291 | case 3: out->append(QLatin1Char('0')); break; never executed: break; never executed: case 3: | 0 | ||||||||||||||||||||||||
292 | case 4: break; never executed: break; never executed: case 4: | 0 | ||||||||||||||||||||||||
293 | default: Q_ASSERT(!"unreachable"); never executed: default: | 0 | ||||||||||||||||||||||||
294 | } never executed: end of block | 0 | ||||||||||||||||||||||||
295 | out->append(hx); | - | ||||||||||||||||||||||||
296 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||||||||
297 | - | |||||||||||||||||||||||||
298 | bool Minify::parse(int startToken) | - | ||||||||||||||||||||||||
299 | { | - | ||||||||||||||||||||||||
300 | int yyaction = 0; | - | ||||||||||||||||||||||||
301 | int yytos = -1; | - | ||||||||||||||||||||||||
302 | QString assembled; | - | ||||||||||||||||||||||||
303 | - | |||||||||||||||||||||||||
304 | _minifiedCode.clear(); | - | ||||||||||||||||||||||||
305 | _tokens.append(startToken); | - | ||||||||||||||||||||||||
306 | _tokenStrings.append(QString()); | - | ||||||||||||||||||||||||
307 | - | |||||||||||||||||||||||||
308 | if (startToken == T_FEED_JS_SCRIPT) {
| 5320-14688 | ||||||||||||||||||||||||
309 | // parse optional pragma directive | - | ||||||||||||||||||||||||
310 | DiagnosticMessage error; | - | ||||||||||||||||||||||||
311 | if (scanDirectives(this, &error)) {
| 22-5298 | ||||||||||||||||||||||||
312 | // append the scanned directives to the minifier code. | - | ||||||||||||||||||||||||
313 | append(directives()); | - | ||||||||||||||||||||||||
314 | - | |||||||||||||||||||||||||
315 | _tokens.append(tokenKind()); | - | ||||||||||||||||||||||||
316 | _tokenStrings.append(tokenText()); | - | ||||||||||||||||||||||||
317 | } else { executed 5298 times by 1 test: end of block Executed by:
| 5298 | ||||||||||||||||||||||||
318 | std::cerr << qPrintable(fileName()) << ':' << tokenStartLine() << ':' | - | ||||||||||||||||||||||||
319 | << tokenStartColumn() << ": syntax error" << std::endl; | - | ||||||||||||||||||||||||
320 | return false; executed 22 times by 1 test: return false; Executed by:
| 22 | ||||||||||||||||||||||||
321 | } | - | ||||||||||||||||||||||||
322 | } | - | ||||||||||||||||||||||||
323 | - | |||||||||||||||||||||||||
324 | do { | - | ||||||||||||||||||||||||
325 | if (++yytos == _stateStack.size())
| 8-35040890 | ||||||||||||||||||||||||
326 | _stateStack.resize(_stateStack.size() * 2); executed 8 times by 1 test: _stateStack.resize(_stateStack.size() * 2); Executed by:
| 8 | ||||||||||||||||||||||||
327 | - | |||||||||||||||||||||||||
328 | _stateStack[yytos] = yyaction; | - | ||||||||||||||||||||||||
329 | - | |||||||||||||||||||||||||
330 | again: code before this statement executed 35040898 times by 1 test: again: Executed by:
| 35040898 | ||||||||||||||||||||||||
331 | if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT)
| 4191852-21942928 | ||||||||||||||||||||||||
332 | lex(); executed 4191852 times by 1 test: lex(); Executed by:
| 4191852 | ||||||||||||||||||||||||
333 | - | |||||||||||||||||||||||||
334 | yyaction = t_action(yyaction, yytoken); | - | ||||||||||||||||||||||||
335 | if (yyaction > 0) {
| 4617682-30543590 | ||||||||||||||||||||||||
336 | if (yyaction == ACCEPT_STATE) {
| 19932-4597750 | ||||||||||||||||||||||||
337 | --yytos; | - | ||||||||||||||||||||||||
338 | if (!assembled.isEmpty())
| 5276-14656 | ||||||||||||||||||||||||
339 | append(assembled); executed 14656 times by 1 test: append(assembled); Executed by:
| 14656 | ||||||||||||||||||||||||
340 | return true; executed 19932 times by 1 test: return true; Executed by:
| 19932 | ||||||||||||||||||||||||
341 | } | - | ||||||||||||||||||||||||
342 | - | |||||||||||||||||||||||||
343 | const QChar lastChar = assembled.isEmpty() ? (_minifiedCode.isEmpty() ? QChar()
| 39192-4022462 | ||||||||||||||||||||||||
344 | : _minifiedCode.at(_minifiedCode.length() - 1)) | - | ||||||||||||||||||||||||
345 | : assembled.at(assembled.length() - 1); | - | ||||||||||||||||||||||||
346 | - | |||||||||||||||||||||||||
347 | if (yytoken == T_SEMICOLON) {
| 533234-4064516 | ||||||||||||||||||||||||
348 | assembled += QLatin1Char(';'); | - | ||||||||||||||||||||||||
349 | - | |||||||||||||||||||||||||
350 | append(assembled); | - | ||||||||||||||||||||||||
351 | assembled.clear(); | - | ||||||||||||||||||||||||
352 | - | |||||||||||||||||||||||||
353 | } else if (yytoken == T_PLUS || yytoken == T_MINUS || yytoken == T_PLUS_PLUS || yytoken == T_MINUS_MINUS) { executed 533234 times by 1 test: end of block Executed by:
| 592-4018228 | ||||||||||||||||||||||||
354 | if (lastChar == QLatin1Char(spell[yytoken][0])) {
| 40-85226 | ||||||||||||||||||||||||
355 | // don't merge unary signs, additive expressions and postfix/prefix increments. | - | ||||||||||||||||||||||||
356 | assembled += QLatin1Char(' '); | - | ||||||||||||||||||||||||
357 | } executed 40 times by 1 test: end of block Executed by:
| 40 | ||||||||||||||||||||||||
358 | - | |||||||||||||||||||||||||
359 | assembled += QLatin1String(spell[yytoken]); | - | ||||||||||||||||||||||||
360 | - | |||||||||||||||||||||||||
361 | } else if (yytoken == T_NUMERIC_LITERAL) { executed 85266 times by 1 test: end of block Executed by:
| 85266-3584802 | ||||||||||||||||||||||||
362 | if (isIdentChar(lastChar))
| 92852-301596 | ||||||||||||||||||||||||
363 | assembled += QLatin1Char(' '); executed 92852 times by 1 test: assembled += QLatin1Char(' '); Executed by:
| 92852 | ||||||||||||||||||||||||
364 | - | |||||||||||||||||||||||||
365 | if (yytokentext.startsWith('.'))
| 348-394100 | ||||||||||||||||||||||||
366 | assembled += QLatin1Char('0'); executed 348 times by 1 test: assembled += QLatin1Char('0'); Executed by:
| 348 | ||||||||||||||||||||||||
367 | - | |||||||||||||||||||||||||
368 | assembled += yytokentext; | - | ||||||||||||||||||||||||
369 | - | |||||||||||||||||||||||||
370 | if (assembled.endsWith(QLatin1Char('.')))
| 22-394426 | ||||||||||||||||||||||||
371 | assembled += QLatin1Char('0'); executed 22 times by 1 test: assembled += QLatin1Char('0'); Executed by:
| 22 | ||||||||||||||||||||||||
372 | - | |||||||||||||||||||||||||
373 | } else if (yytoken == T_IDENTIFIER) { executed 394448 times by 1 test: end of block Executed by:
| 394448-2352780 | ||||||||||||||||||||||||
374 | QString identifier = yytokentext; | - | ||||||||||||||||||||||||
375 | - | |||||||||||||||||||||||||
376 | if (classify(identifier.constData(), identifier.size(), qmlMode()) != T_IDENTIFIER) {
| 4-1232018 | ||||||||||||||||||||||||
377 | // the unescaped identifier is a keyword. In this case just replace | - | ||||||||||||||||||||||||
378 | // the last character of the identifier with it escape sequence. | - | ||||||||||||||||||||||||
379 | const QChar ch = identifier.at(identifier.length() - 1); | - | ||||||||||||||||||||||||
380 | identifier.chop(1); | - | ||||||||||||||||||||||||
381 | escape(ch, &identifier); | - | ||||||||||||||||||||||||
382 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||||||||
383 | - | |||||||||||||||||||||||||
384 | if (isIdentChar(lastChar))
| 158358-1073664 | ||||||||||||||||||||||||
385 | assembled += QLatin1Char(' '); executed 158358 times by 1 test: assembled += QLatin1Char(' '); Executed by:
| 158358 | ||||||||||||||||||||||||
386 | - | |||||||||||||||||||||||||
387 | assembled += identifier; | - | ||||||||||||||||||||||||
388 | - | |||||||||||||||||||||||||
389 | } else if (yytoken == T_STRING_LITERAL || yytoken == T_MULTILINE_STRING_LITERAL) { executed 1232022 times by 1 test: end of block Executed by:
| 176-2148518 | ||||||||||||||||||||||||
390 | assembled += QLatin1Char('"'); | - | ||||||||||||||||||||||||
391 | assembled += quote(yytokentext); | - | ||||||||||||||||||||||||
392 | assembled += QLatin1Char('"'); | - | ||||||||||||||||||||||||
393 | } else { executed 204438 times by 1 test: end of block Executed by:
| 204438 | ||||||||||||||||||||||||
394 | if (isIdentChar(lastChar)) {
| 802234-1346108 | ||||||||||||||||||||||||
395 | if (! yytokentext.isEmpty()) {
| 1400-1344708 | ||||||||||||||||||||||||
396 | const QChar ch = yytokentext.at(0); | - | ||||||||||||||||||||||||
397 | if (isIdentChar(ch))
| 12656-1332052 | ||||||||||||||||||||||||
398 | assembled += QLatin1Char(' '); executed 12656 times by 1 test: assembled += QLatin1Char(' '); Executed by:
| 12656 | ||||||||||||||||||||||||
399 | } executed 1344708 times by 1 test: end of block Executed by:
| 1344708 | ||||||||||||||||||||||||
400 | } executed 1346108 times by 1 test: end of block Executed by:
| 1346108 | ||||||||||||||||||||||||
401 | assembled += yytokentext; | - | ||||||||||||||||||||||||
402 | } executed 2148342 times by 1 test: end of block Executed by:
| 2148342 | ||||||||||||||||||||||||
403 | yytoken = -1; | - | ||||||||||||||||||||||||
404 | } else if (yyaction < 0) { executed 4597750 times by 1 test: end of block Executed by:
| 120428-30423162 | ||||||||||||||||||||||||
405 | const int ruleno = -yyaction - 1; | - | ||||||||||||||||||||||||
406 | yytos -= rhs[ruleno]; | - | ||||||||||||||||||||||||
407 | - | |||||||||||||||||||||||||
408 | handleLookaheads(ruleno); | - | ||||||||||||||||||||||||
409 | - | |||||||||||||||||||||||||
410 | if (isRegExpRule(ruleno)) {
| 3732-30419430 | ||||||||||||||||||||||||
411 | QString restOfRegExp; | - | ||||||||||||||||||||||||
412 | - | |||||||||||||||||||||||||
413 | if (! scanRestOfRegExp(ruleno, &restOfRegExp))
| 0-3732 | ||||||||||||||||||||||||
414 | break; // break the loop, it wil report a syntax error never executed: break; | 0 | ||||||||||||||||||||||||
415 | - | |||||||||||||||||||||||||
416 | assembled += restOfRegExp; | - | ||||||||||||||||||||||||
417 | } executed 3732 times by 1 test: end of block Executed by:
| 3732 | ||||||||||||||||||||||||
418 | yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT); | - | ||||||||||||||||||||||||
419 | } executed 30423162 times by 1 test: end of block Executed by:
| 30423162 | ||||||||||||||||||||||||
420 | } while (yyaction); executed 35141340 times by 1 test: end of block Executed by:
| 120428-35141340 | ||||||||||||||||||||||||
421 | - | |||||||||||||||||||||||||
422 | const int yyerrorstate = _stateStack[yytos]; | - | ||||||||||||||||||||||||
423 | - | |||||||||||||||||||||||||
424 | // automatic insertion of `;' | - | ||||||||||||||||||||||||
425 | if (yytoken != -1 && ((t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && canInsertAutomaticSemicolon(yytoken))
| 0-120428 | ||||||||||||||||||||||||
426 | || t_action(yyerrorstate, T_COMPATIBILITY_SEMICOLON))) {
| 2-54 | ||||||||||||||||||||||||
427 | _tokens.prepend(yytoken); | - | ||||||||||||||||||||||||
428 | _tokenStrings.prepend(yytokentext); | - | ||||||||||||||||||||||||
429 | yyaction = yyerrorstate; | - | ||||||||||||||||||||||||
430 | yytoken = T_SEMICOLON; | - | ||||||||||||||||||||||||
431 | goto again; executed 120374 times by 1 test: goto again; Executed by:
| 120374 | ||||||||||||||||||||||||
432 | } | - | ||||||||||||||||||||||||
433 | - | |||||||||||||||||||||||||
434 | std::cerr << qPrintable(fileName()) << ':' << tokenStartLine() << ':' << tokenStartColumn() | - | ||||||||||||||||||||||||
435 | << ": syntax error" << std::endl; | - | ||||||||||||||||||||||||
436 | return false; executed 54 times by 1 test: return false; Executed by:
| 54 | ||||||||||||||||||||||||
437 | } | - | ||||||||||||||||||||||||
438 | - | |||||||||||||||||||||||||
439 | - | |||||||||||||||||||||||||
440 | class Tokenize: public QmlminLexer | - | ||||||||||||||||||||||||
441 | { | - | ||||||||||||||||||||||||
442 | QStringList _minifiedCode; | - | ||||||||||||||||||||||||
443 | - | |||||||||||||||||||||||||
444 | public: | - | ||||||||||||||||||||||||
445 | Tokenize() {} | - | ||||||||||||||||||||||||
446 | - | |||||||||||||||||||||||||
447 | QStringList tokenStream() const; | - | ||||||||||||||||||||||||
448 | - | |||||||||||||||||||||||||
449 | protected: | - | ||||||||||||||||||||||||
450 | bool parse(int startToken) override; | - | ||||||||||||||||||||||||
451 | }; | - | ||||||||||||||||||||||||
452 | - | |||||||||||||||||||||||||
453 | QStringList Tokenize::tokenStream() const | - | ||||||||||||||||||||||||
454 | { | - | ||||||||||||||||||||||||
455 | return _minifiedCode; executed 19932 times by 1 test: return _minifiedCode; Executed by:
| 19932 | ||||||||||||||||||||||||
456 | } | - | ||||||||||||||||||||||||
457 | - | |||||||||||||||||||||||||
458 | bool Tokenize::parse(int startToken) | - | ||||||||||||||||||||||||
459 | { | - | ||||||||||||||||||||||||
460 | int yyaction = 0; | - | ||||||||||||||||||||||||
461 | int yytos = -1; | - | ||||||||||||||||||||||||
462 | - | |||||||||||||||||||||||||
463 | _minifiedCode.clear(); | - | ||||||||||||||||||||||||
464 | _tokens.append(startToken); | - | ||||||||||||||||||||||||
465 | _tokenStrings.append(QString()); | - | ||||||||||||||||||||||||
466 | - | |||||||||||||||||||||||||
467 | if (startToken == T_FEED_JS_SCRIPT) {
| 5288-14644 | ||||||||||||||||||||||||
468 | // parse optional pragma directive | - | ||||||||||||||||||||||||
469 | DiagnosticMessage error; | - | ||||||||||||||||||||||||
470 | if (scanDirectives(this, &error)) {
| 0-5288 | ||||||||||||||||||||||||
471 | // append the scanned directives as one token to | - | ||||||||||||||||||||||||
472 | // the token stream. | - | ||||||||||||||||||||||||
473 | _minifiedCode.append(directives()); | - | ||||||||||||||||||||||||
474 | - | |||||||||||||||||||||||||
475 | _tokens.append(tokenKind()); | - | ||||||||||||||||||||||||
476 | _tokenStrings.append(tokenText()); | - | ||||||||||||||||||||||||
477 | } else { executed 5288 times by 1 test: end of block Executed by:
| 5288 | ||||||||||||||||||||||||
478 | std::cerr << qPrintable(fileName()) << ':' << tokenStartLine() << ':' | - | ||||||||||||||||||||||||
479 | << tokenStartColumn() << ": syntax error" << std::endl; | - | ||||||||||||||||||||||||
480 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
481 | } | - | ||||||||||||||||||||||||
482 | } | - | ||||||||||||||||||||||||
483 | - | |||||||||||||||||||||||||
484 | do { | - | ||||||||||||||||||||||||
485 | if (++yytos == _stateStack.size())
| 8-35038844 | ||||||||||||||||||||||||
486 | _stateStack.resize(_stateStack.size() * 2); executed 8 times by 1 test: _stateStack.resize(_stateStack.size() * 2); Executed by:
| 8 | ||||||||||||||||||||||||
487 | - | |||||||||||||||||||||||||
488 | _stateStack[yytos] = yyaction; | - | ||||||||||||||||||||||||
489 | - | |||||||||||||||||||||||||
490 | again: code before this statement executed 35038852 times by 1 test: again: Executed by:
| 35038852 | ||||||||||||||||||||||||
491 | if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT)
| 4191220-21942220 | ||||||||||||||||||||||||
492 | lex(); executed 4191220 times by 1 test: lex(); Executed by:
| 4191220 | ||||||||||||||||||||||||
493 | - | |||||||||||||||||||||||||
494 | yyaction = t_action(yyaction, yytoken); | - | ||||||||||||||||||||||||
495 | if (yyaction > 0) {
| 4617076-30542112 | ||||||||||||||||||||||||
496 | if (yyaction == ACCEPT_STATE) {
| 19932-4597144 | ||||||||||||||||||||||||
497 | --yytos; | - | ||||||||||||||||||||||||
498 | return true; executed 19932 times by 1 test: return true; Executed by:
| 19932 | ||||||||||||||||||||||||
499 | } | - | ||||||||||||||||||||||||
500 | - | |||||||||||||||||||||||||
501 | if (yytoken == T_SEMICOLON)
| 533188-4063956 | ||||||||||||||||||||||||
502 | _minifiedCode += QLatin1String(";"); executed 533188 times by 1 test: _minifiedCode += QLatin1String(";"); Executed by:
| 533188 | ||||||||||||||||||||||||
503 | else | - | ||||||||||||||||||||||||
504 | _minifiedCode += yytokentext; executed 4063956 times by 1 test: _minifiedCode += yytokentext; Executed by:
| 4063956 | ||||||||||||||||||||||||
505 | - | |||||||||||||||||||||||||
506 | yytoken = -1; | - | ||||||||||||||||||||||||
507 | } else if (yyaction < 0) { executed 4597144 times by 1 test: end of block Executed by:
| 120336-30421776 | ||||||||||||||||||||||||
508 | const int ruleno = -yyaction - 1; | - | ||||||||||||||||||||||||
509 | yytos -= rhs[ruleno]; | - | ||||||||||||||||||||||||
510 | - | |||||||||||||||||||||||||
511 | handleLookaheads(ruleno); | - | ||||||||||||||||||||||||
512 | - | |||||||||||||||||||||||||
513 | if (isRegExpRule(ruleno)) {
| 3732-30418044 | ||||||||||||||||||||||||
514 | QString restOfRegExp; | - | ||||||||||||||||||||||||
515 | - | |||||||||||||||||||||||||
516 | if (! scanRestOfRegExp(ruleno, &restOfRegExp))
| 0-3732 | ||||||||||||||||||||||||
517 | break; // break the loop, it wil report a syntax error never executed: break; | 0 | ||||||||||||||||||||||||
518 | - | |||||||||||||||||||||||||
519 | _minifiedCode.last().append(restOfRegExp); | - | ||||||||||||||||||||||||
520 | } executed 3732 times by 1 test: end of block Executed by:
| 3732 | ||||||||||||||||||||||||
521 | - | |||||||||||||||||||||||||
522 | yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT); | - | ||||||||||||||||||||||||
523 | } executed 30421776 times by 1 test: end of block Executed by:
| 30421776 | ||||||||||||||||||||||||
524 | } while (yyaction); executed 35139256 times by 1 test: end of block Executed by:
| 120336-35139256 | ||||||||||||||||||||||||
525 | - | |||||||||||||||||||||||||
526 | const int yyerrorstate = _stateStack[yytos]; | - | ||||||||||||||||||||||||
527 | - | |||||||||||||||||||||||||
528 | // automatic insertion of `;' | - | ||||||||||||||||||||||||
529 | if (yytoken != -1 && ((t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && canInsertAutomaticSemicolon(yytoken))
| 0-120336 | ||||||||||||||||||||||||
530 | || t_action(yyerrorstate, T_COMPATIBILITY_SEMICOLON))) {
| 0-2 | ||||||||||||||||||||||||
531 | _tokens.prepend(yytoken); | - | ||||||||||||||||||||||||
532 | _tokenStrings.prepend(yytokentext); | - | ||||||||||||||||||||||||
533 | yyaction = yyerrorstate; | - | ||||||||||||||||||||||||
534 | yytoken = T_SEMICOLON; | - | ||||||||||||||||||||||||
535 | goto again; executed 120336 times by 1 test: goto again; Executed by:
| 120336 | ||||||||||||||||||||||||
536 | } | - | ||||||||||||||||||||||||
537 | - | |||||||||||||||||||||||||
538 | std::cerr << qPrintable(fileName()) << ':' << tokenStartLine() << ':' | - | ||||||||||||||||||||||||
539 | << tokenStartColumn() << ": syntax error" << std::endl; | - | ||||||||||||||||||||||||
540 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
541 | } | - | ||||||||||||||||||||||||
542 | - | |||||||||||||||||||||||||
543 | } // end of QQmlJS namespace | - | ||||||||||||||||||||||||
544 | - | |||||||||||||||||||||||||
545 | static void usage(bool showHelp = false) | - | ||||||||||||||||||||||||
546 | { | - | ||||||||||||||||||||||||
547 | std::cerr << "Usage: qmlmin [options] file" << std::endl; | - | ||||||||||||||||||||||||
548 | - | |||||||||||||||||||||||||
549 | if (showHelp) {
| 0 | ||||||||||||||||||||||||
550 | std::cerr << " Removes comments and layout characters" << std::endl | - | ||||||||||||||||||||||||
551 | << " The options are:" << std::endl | - | ||||||||||||||||||||||||
552 | << " -o<file> write output to file rather than stdout" << std::endl | - | ||||||||||||||||||||||||
553 | << " -v --verify-only just run the verifier, no output" << std::endl | - | ||||||||||||||||||||||||
554 | << " -w<width> restrict line characters to width" << std::endl | - | ||||||||||||||||||||||||
555 | << " -h display this output" << std::endl; | - | ||||||||||||||||||||||||
556 | } never executed: end of block | 0 | ||||||||||||||||||||||||
557 | } never executed: end of block | 0 | ||||||||||||||||||||||||
558 | - | |||||||||||||||||||||||||
559 | int runQmlmin(int argc, char *argv[]) | - | ||||||||||||||||||||||||
560 | { | - | ||||||||||||||||||||||||
561 | QCoreApplication app(argc, argv); | - | ||||||||||||||||||||||||
562 | QCoreApplication::setApplicationVersion(QLatin1String(QT_VERSION_STR)); | - | ||||||||||||||||||||||||
563 | - | |||||||||||||||||||||||||
564 | const QStringList args = app.arguments(); | - | ||||||||||||||||||||||||
565 | - | |||||||||||||||||||||||||
566 | QString fileName; | - | ||||||||||||||||||||||||
567 | QString outputFile; | - | ||||||||||||||||||||||||
568 | bool verifyOnly = false; | - | ||||||||||||||||||||||||
569 | - | |||||||||||||||||||||||||
570 | // By default ensure the output character width is less than 16-bits (pass 0 to disable) | - | ||||||||||||||||||||||||
571 | int width = USHRT_MAX; | - | ||||||||||||||||||||||||
572 | - | |||||||||||||||||||||||||
573 | int index = 1; | - | ||||||||||||||||||||||||
574 | while (index < args.size()) {
| 10042-30126 | ||||||||||||||||||||||||
575 | const QString arg = args.at(index++); | - | ||||||||||||||||||||||||
576 | const QString next = index < args.size() ? args.at(index) : QString();
| 10042-20084 | ||||||||||||||||||||||||
577 | - | |||||||||||||||||||||||||
578 | if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
| 0-30126 | ||||||||||||||||||||||||
579 | usage(/*showHelp*/ true); | - | ||||||||||||||||||||||||
580 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
581 | } else if (arg == QLatin1String("-v") || arg == QLatin1String("--verify-only")) {
| 0-30126 | ||||||||||||||||||||||||
582 | verifyOnly = true; | - | ||||||||||||||||||||||||
583 | } else if (arg == QLatin1String("-o")) { executed 10042 times by 1 test: end of block Executed by:
| 0-20084 | ||||||||||||||||||||||||
584 | if (next.isEmpty()) {
| 0 | ||||||||||||||||||||||||
585 | std::cerr << "qmlmin: argument to '-o' is missing" << std::endl; | - | ||||||||||||||||||||||||
586 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
587 | } else { | - | ||||||||||||||||||||||||
588 | outputFile = next; | - | ||||||||||||||||||||||||
589 | ++index; // consume the next argument | - | ||||||||||||||||||||||||
590 | } never executed: end of block | 0 | ||||||||||||||||||||||||
591 | } else if (arg.startsWith(QLatin1String("-o"))) {
| 0-20084 | ||||||||||||||||||||||||
592 | outputFile = arg.mid(2); | - | ||||||||||||||||||||||||
593 | - | |||||||||||||||||||||||||
594 | if (outputFile.isEmpty()) {
| 0 | ||||||||||||||||||||||||
595 | std::cerr << "qmlmin: argument to '-o' is missing" << std::endl; | - | ||||||||||||||||||||||||
596 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
597 | } | - | ||||||||||||||||||||||||
598 | } else if (arg == QLatin1String("-w")) { never executed: end of block
| 0-20084 | ||||||||||||||||||||||||
599 | if (next.isEmpty()) {
| 0 | ||||||||||||||||||||||||
600 | std::cerr << "qmlmin: argument to '-w' is missing" << std::endl; | - | ||||||||||||||||||||||||
601 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
602 | } else { | - | ||||||||||||||||||||||||
603 | bool ok; | - | ||||||||||||||||||||||||
604 | width = next.toInt(&ok); | - | ||||||||||||||||||||||||
605 | - | |||||||||||||||||||||||||
606 | if (!ok) {
| 0 | ||||||||||||||||||||||||
607 | std::cerr << "qmlmin: argument to '-w' is invalid" << std::endl; | - | ||||||||||||||||||||||||
608 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
609 | } | - | ||||||||||||||||||||||||
610 | - | |||||||||||||||||||||||||
611 | ++index; // consume the next argument | - | ||||||||||||||||||||||||
612 | } never executed: end of block | 0 | ||||||||||||||||||||||||
613 | } else if (arg.startsWith(QLatin1String("-w"))) {
| 10042 | ||||||||||||||||||||||||
614 | bool ok; | - | ||||||||||||||||||||||||
615 | width = arg.midRef(2).toInt(&ok); | - | ||||||||||||||||||||||||
616 | - | |||||||||||||||||||||||||
617 | if (!ok) {
| 0-10042 | ||||||||||||||||||||||||
618 | std::cerr << "qmlmin: argument to '-w' is invalid" << std::endl; | - | ||||||||||||||||||||||||
619 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
620 | } | - | ||||||||||||||||||||||||
621 | } else { executed 10042 times by 1 test: end of block Executed by:
| 10042 | ||||||||||||||||||||||||
622 | const bool isInvalidOpt = arg.startsWith(QLatin1Char('-')); | - | ||||||||||||||||||||||||
623 | if (! isInvalidOpt && fileName.isEmpty())
| 0-10042 | ||||||||||||||||||||||||
624 | fileName = arg; executed 10042 times by 1 test: fileName = arg; Executed by:
| 10042 | ||||||||||||||||||||||||
625 | else { | - | ||||||||||||||||||||||||
626 | usage(/*show help*/ isInvalidOpt); | - | ||||||||||||||||||||||||
627 | if (isInvalidOpt)
| 0 | ||||||||||||||||||||||||
628 | std::cerr << "qmlmin: invalid option '" << qPrintable(arg) << '\'' << std::endl; never executed: std::cerr << "qmlmin: invalid option '" << QtPrivate::asString(arg).toLocal8Bit().constData() << '\'' << std::endl; | 0 | ||||||||||||||||||||||||
629 | else | - | ||||||||||||||||||||||||
630 | std::cerr << "qmlmin: too many input files specified" << std::endl; never executed: std::cerr << "qmlmin: too many input files specified" << std::endl; | 0 | ||||||||||||||||||||||||
631 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
632 | } | - | ||||||||||||||||||||||||
633 | } | - | ||||||||||||||||||||||||
634 | } | - | ||||||||||||||||||||||||
635 | - | |||||||||||||||||||||||||
636 | if (fileName.isEmpty()) {
| 0-10042 | ||||||||||||||||||||||||
637 | usage(); | - | ||||||||||||||||||||||||
638 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
639 | } | - | ||||||||||||||||||||||||
640 | - | |||||||||||||||||||||||||
641 | QFile file(fileName); | - | ||||||||||||||||||||||||
642 | if (! file.open(QFile::ReadOnly)) {
| 0-10042 | ||||||||||||||||||||||||
643 | std::cerr << "qmlmin: '" << qPrintable(fileName) << "' no such file or directory" << std::endl; | - | ||||||||||||||||||||||||
644 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
645 | } | - | ||||||||||||||||||||||||
646 | - | |||||||||||||||||||||||||
647 | const QString code = QString::fromUtf8(file.readAll()); // QML files are UTF-8 encoded. | - | ||||||||||||||||||||||||
648 | file.close(); | - | ||||||||||||||||||||||||
649 | - | |||||||||||||||||||||||||
650 | QQmlJS::Minify minify(width); | - | ||||||||||||||||||||||||
651 | if (! minify(fileName, code)) {
| 76-9966 | ||||||||||||||||||||||||
652 | std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (not a valid QML/JS file)" << std::endl; | - | ||||||||||||||||||||||||
653 | return EXIT_FAILURE; executed 76 times by 1 test: return 1 ; Executed by:
| 76 | ||||||||||||||||||||||||
654 | } | - | ||||||||||||||||||||||||
655 | - | |||||||||||||||||||||||||
656 | // | - | ||||||||||||||||||||||||
657 | // verify the output | - | ||||||||||||||||||||||||
658 | // | - | ||||||||||||||||||||||||
659 | QQmlJS::Minify secondMinify(width); | - | ||||||||||||||||||||||||
660 | if (! secondMinify(fileName, minify.minifiedCode()) || secondMinify.minifiedCode() != minify.minifiedCode()) {
| 0-9966 | ||||||||||||||||||||||||
661 | std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << '\'' << std::endl; | - | ||||||||||||||||||||||||
662 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
663 | } | - | ||||||||||||||||||||||||
664 | - | |||||||||||||||||||||||||
665 | QQmlJS::Tokenize originalTokens, minimizedTokens; | - | ||||||||||||||||||||||||
666 | originalTokens(fileName, code); | - | ||||||||||||||||||||||||
667 | minimizedTokens(fileName, minify.minifiedCode()); | - | ||||||||||||||||||||||||
668 | - | |||||||||||||||||||||||||
669 | if (originalTokens.tokenStream().size() != minimizedTokens.tokenStream().size()) {
| 0-9966 | ||||||||||||||||||||||||
670 | std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << '\'' << std::endl; | - | ||||||||||||||||||||||||
671 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
672 | } | - | ||||||||||||||||||||||||
673 | - | |||||||||||||||||||||||||
674 | if (! verifyOnly) {
| 0-9966 | ||||||||||||||||||||||||
675 | if (outputFile.isEmpty()) {
| 0 | ||||||||||||||||||||||||
676 | const QByteArray chars = minify.minifiedCode().toUtf8(); | - | ||||||||||||||||||||||||
677 | std::cout << chars.constData(); | - | ||||||||||||||||||||||||
678 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
679 | QFile file(outputFile); | - | ||||||||||||||||||||||||
680 | if (! file.open(QFile::WriteOnly)) {
| 0 | ||||||||||||||||||||||||
681 | std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (permission denied)" << std::endl; | - | ||||||||||||||||||||||||
682 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||||||||||||||
683 | } | - | ||||||||||||||||||||||||
684 | - | |||||||||||||||||||||||||
685 | file.write(minify.minifiedCode().toUtf8()); | - | ||||||||||||||||||||||||
686 | file.close(); | - | ||||||||||||||||||||||||
687 | } never executed: end of block | 0 | ||||||||||||||||||||||||
688 | } | - | ||||||||||||||||||||||||
689 | - | |||||||||||||||||||||||||
690 | return 0; executed 9966 times by 1 test: return 0; Executed by:
| 9966 | ||||||||||||||||||||||||
691 | } | - | ||||||||||||||||||||||||
692 | - | |||||||||||||||||||||||||
693 | QT_END_NAMESPACE | - | ||||||||||||||||||||||||
694 | - | |||||||||||||||||||||||||
695 | int main(int argc, char **argv) | - | ||||||||||||||||||||||||
696 | { | - | ||||||||||||||||||||||||
697 | return QT_PREPEND_NAMESPACE(runQmlmin(argc, argv)); executed 10042 times by 1 test: return ::runQmlmin(argc, argv); Executed by:
| 10042 | ||||||||||||||||||||||||
698 | } | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |