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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://www.qt.io/contact-us
**
** This file is part of the Qt Enterprise LicenseChecker Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/
#include "clangstaticanalyzerlogfilereader.h"
#include <QDebug>
#include <QObject>
#include <QFile>
#include <QFileInfo>
#include <QXmlStreamReader>
#include <utils/qtcassert.h>
namespace ClangStaticAnalyzer {
namespace Internal {
class ClangStaticAnalyzerLogFileReader
{
public:
ClangStaticAnalyzerLogFileReader(const QString &filePath);
QXmlStreamReader::Error read();
// Output
QString clangVersion() const;
QStringList files() const;
QList<Diagnostic> diagnostics() const;
private:
void readPlist();
void readTopLevelDict();
void readDiagnosticsArray();
void readDiagnosticsDict();
QList<ExplainingStep> readPathArray();
ExplainingStep readPathDict();
Location readLocationDict(bool elementIsRead = false);
QList<Location> readRangesArray();
QString readString();
QStringList readStringArray();
int readInteger(bool *convertedSuccessfully);
private:
QString m_filePath;
QXmlStreamReader m_xml;
QString m_clangVersion;
QStringList m_referencedFiles;
QList<Diagnostic> m_diagnostics;
};
QList<Diagnostic> LogFileReader::read(const QString &filePath, QString *errorMessage)
{
const QList<Diagnostic> emptyList;
// Check file path
QFileInfo fi(filePath);
if (!fi.exists() || !fi.isReadable()) {
if (errorMessage) {
*errorMessage = QObject::tr("File \"%1\" does not exist or is not readable.")
.arg(filePath);
}
return emptyList;
}
// Read
ClangStaticAnalyzerLogFileReader reader(filePath);
const QXmlStreamReader::Error error = reader.read();
// Return diagnostics
switch (error) {
case QXmlStreamReader::NoError:
return reader.diagnostics();
// Handle errors
case QXmlStreamReader::UnexpectedElementError:
if (errorMessage) {
*errorMessage = QObject::tr("Could not read file \"%1\": UnexpectedElementError.")
.arg(filePath);
} // fall-through
case QXmlStreamReader::CustomError:
if (errorMessage) {
*errorMessage = QObject::tr("Could not read file \"%1\": CustomError.")
.arg(filePath);
} // fall-through
case QXmlStreamReader::NotWellFormedError:
if (errorMessage) {
*errorMessage = QObject::tr("Could not read file \"%1\": NotWellFormedError.")
.arg(filePath);
} // fall-through
case QXmlStreamReader::PrematureEndOfDocumentError:
if (errorMessage) {
*errorMessage = QObject::tr("Could not read file \"%1\": PrematureEndOfDocumentError.")
.arg(filePath);
} // fall-through
default:
return emptyList;
}
}
ClangStaticAnalyzerLogFileReader::ClangStaticAnalyzerLogFileReader(const QString &filePath)
: m_filePath(filePath)
{
}
QXmlStreamReader::Error ClangStaticAnalyzerLogFileReader::read()
{
QTC_ASSERT(!m_filePath.isEmpty(), return QXmlStreamReader::CustomError);
QFile file(m_filePath);
QTC_ASSERT(file.open(QIODevice::ReadOnly | QIODevice::Text),
return QXmlStreamReader::CustomError);
m_xml.setDevice(&file);
readPlist();
// If file is empty, m_xml.error() == QXmlStreamReader::PrematureEndOfDocumentError
return m_xml.error();
}
QString ClangStaticAnalyzerLogFileReader::clangVersion() const
{
return m_clangVersion;
}
QStringList ClangStaticAnalyzerLogFileReader::files() const
{
return m_referencedFiles;
}
QList<Diagnostic> ClangStaticAnalyzerLogFileReader::diagnostics() const
{
return m_diagnostics;
}
void ClangStaticAnalyzerLogFileReader::readPlist()
{
if (m_xml.readNextStartElement()) {
if (m_xml.name() == QLatin1String("plist")) {
if (m_xml.attributes().value(QLatin1String("version")) == QLatin1String("1.0"))
readTopLevelDict();
} else {
m_xml.raiseError(QObject::tr("File is not a plist version 1.0 file."));
}
}
}
void ClangStaticAnalyzerLogFileReader::readTopLevelDict()
{
QTC_ASSERT(m_xml.isStartElement() && m_xml.name() == QLatin1String("plist"), return);
QTC_ASSERT(m_xml.readNextStartElement() && m_xml.name() == QLatin1String("dict"), return);
while (m_xml.readNextStartElement()) {
if (m_xml.name() == QLatin1String("key")) {
const QString key = m_xml.readElementText();
if (key == QLatin1String("clang_version"))
m_clangVersion = readString();
else if (key == QLatin1String("files"))
m_referencedFiles = readStringArray();
else if (key == QLatin1String("diagnostics"))
readDiagnosticsArray();
} else {
m_xml.skipCurrentElement();
}
}
}
void ClangStaticAnalyzerLogFileReader::readDiagnosticsArray()
{
if (m_xml.readNextStartElement() && m_xml.name() == QLatin1String("array")) {
while (m_xml.readNextStartElement() && m_xml.name() == QLatin1String("dict"))
readDiagnosticsDict();
}
}
void ClangStaticAnalyzerLogFileReader::readDiagnosticsDict()
{
QTC_ASSERT(m_xml.isStartElement() && m_xml.name() == QLatin1String("dict"), return);
Diagnostic diagnostic;
while (m_xml.readNextStartElement()) {
if (m_xml.name() == QLatin1String("key")) {
const QString key = m_xml.readElementText();
if (key == QLatin1String("path"))
diagnostic.explainingSteps = readPathArray();
else if (key == QLatin1String("description"))
diagnostic.description = readString();
else if (key == QLatin1String("category"))
diagnostic.category = readString();
else if (key == QLatin1String("type"))
diagnostic.type = readString();
else if (key == QLatin1String("issue_context_kind"))
diagnostic.issueContextKind = readString();
else if (key == QLatin1String("issue_context"))
diagnostic.issueContext = readString();
else if (key == QLatin1String("location"))
diagnostic.location = readLocationDict();
} else {
m_xml.skipCurrentElement();
}
}
m_diagnostics << diagnostic;
}
QList<ExplainingStep> ClangStaticAnalyzerLogFileReader::readPathArray()
{
QList<ExplainingStep> result;
if (m_xml.readNextStartElement() && m_xml.name() == QLatin1String("array")) {
while (m_xml.readNextStartElement() && m_xml.name() == QLatin1String("dict")) {
const ExplainingStep step = readPathDict();
if (step.isValid())
result << step;
}
}
return result;
}
ExplainingStep ClangStaticAnalyzerLogFileReader::readPathDict()
{
ExplainingStep explainingStep;
QTC_ASSERT(m_xml.isStartElement() && m_xml.name() == QLatin1String("dict"),
return explainingStep);
// We are interested only in dict entries an kind=event type
if (m_xml.readNextStartElement()) {
if (m_xml.name() == QLatin1String("key")) {
const QString key = m_xml.readElementText();
QTC_ASSERT(key == QLatin1String("kind"), return explainingStep);
const QString kind = readString();
if (kind != QLatin1String("event")) {
m_xml.skipCurrentElement();
return explainingStep;
}
}
}
bool depthOk = false;
while (m_xml.readNextStartElement()) {
if (m_xml.name() == QLatin1String("key")) {
const QString key = m_xml.readElementText();
if (key == QLatin1String("location"))
explainingStep.location = readLocationDict();
else if (key == QLatin1String("ranges"))
explainingStep.ranges = readRangesArray();
else if (key == QLatin1String("depth"))
explainingStep.depth = readInteger(&depthOk);
else if (key == QLatin1String("message"))
explainingStep.message = readString();
else if (key == QLatin1String("extended_message"))
explainingStep.extendedMessage = readString();
} else {
m_xml.skipCurrentElement();
}
}
QTC_CHECK(depthOk);
return explainingStep;
}
Location ClangStaticAnalyzerLogFileReader::readLocationDict(bool elementIsRead)
{
Location location;
if (elementIsRead) {
QTC_ASSERT(m_xml.isStartElement() && m_xml.name() == QLatin1String("dict"),
return location);
} else {
QTC_ASSERT(m_xml.readNextStartElement() && m_xml.name() == QLatin1String("dict"),
return location);
}
int line = 0;
int column = 0;
int fileIndex = 0;
bool lineOk = false, columnOk = false, fileIndexOk = false;
// Collect values
while (m_xml.readNextStartElement()) {
if (m_xml.name() == QLatin1String("key")) {
const QString keyName = m_xml.readElementText();
if (keyName == QLatin1String("line"))
line = readInteger(&lineOk);
else if (keyName == QLatin1String("col"))
column = readInteger(&columnOk);
else if (keyName == QLatin1String("file"))
fileIndex = readInteger(&fileIndexOk);
} else {
m_xml.skipCurrentElement();
}
}
if (lineOk && columnOk && fileIndexOk) {
QTC_ASSERT(fileIndex < m_referencedFiles.size(), return location);
location = Location(m_referencedFiles.at(fileIndex), line, column);
}
return location;
}
QList<Location> ClangStaticAnalyzerLogFileReader::readRangesArray()
{
QList<Location> result;
// It's an array of arrays...
QTC_ASSERT(m_xml.readNextStartElement() && m_xml.name() == QLatin1String("array"),
return result);
QTC_ASSERT(m_xml.readNextStartElement() && m_xml.name() == QLatin1String("array"),
return result);
while (m_xml.readNextStartElement() && m_xml.name() == QLatin1String("dict"))
result << readLocationDict(true);
m_xml.skipCurrentElement(); // Laeve outer array
return result;
}
QString ClangStaticAnalyzerLogFileReader::readString()
{
if (m_xml.readNextStartElement() && m_xml.name() == QLatin1String("string"))
return m_xml.readElementText();
m_xml.raiseError(QObject::tr("Expected a string element."));
return QString();
}
QStringList ClangStaticAnalyzerLogFileReader::readStringArray()
{
if (m_xml.readNextStartElement() && m_xml.name() == QLatin1String("array")) {
QStringList result;
while (m_xml.readNextStartElement() && m_xml.name() == QLatin1String("string")) {
const QString string = m_xml.readElementText();
if (!string.isEmpty())
result << string;
}
return result;
}
m_xml.raiseError(QObject::tr("Expected an array element."));
return QStringList();
}
int ClangStaticAnalyzerLogFileReader::readInteger(bool *convertedSuccessfully)
{
if (m_xml.readNextStartElement() && m_xml.name() == QLatin1String("integer")) {
const QString contents = m_xml.readElementText();
return contents.toInt(convertedSuccessfully);
}
m_xml.raiseError(QObject::tr("Expected an integer element."));
if (convertedSuccessfully)
*convertedSuccessfully = false;
return -1;
}
} // namespace Internal
} // namespace ClangStaticAnalyzer
|