forked from csutils/csdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-writer.cc
534 lines (435 loc) · 16.1 KB
/
html-writer.cc
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* Copyright (C) 2012 Red Hat, Inc.
*
* This file is part of csdiff.
*
* csdiff is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* csdiff is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
*/
#include "html-writer.hh"
#include "deflookup.hh"
#include <boost/algorithm/string/replace.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
static int parsingRatioThr = 95;
static int parsingOldToNewRatioThr = 75;
namespace HtmlLib {
void escapeText(std::string &text) {
using namespace boost::algorithm;
replace_all(text, "&", "&" );
replace_all(text, "\"", """);
replace_all(text, "\'", "'");
replace_all(text, "<", "<" );
replace_all(text, ">", ">" );
}
std::string escapeTextInline(std::string text) {
escapeText(text);
return text;
}
void initHtml(std::ostream &str, std::string title) {
escapeText(title);
str << "<?xml version='1.0' encoding='utf-8'?>\n\
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' \
'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>\n\
<html xmlns='http://www.w3.org/1999/xhtml'>\n\
<head><title>" << title << "</title></head>\n\
<body style='background: white;'>\n<h1>" << title << "</h1>\n";
}
void writeLink(
std::ostream &str,
const std::string &url,
const std::string &name)
{
str << "<a href='" << url << "'>" << name << "</a>\n";
}
void finalizeHtml(std::ostream &str) {
str << "</body>\n</html>\n";
}
void initSection(std::ostream &str, std::string name) {
escapeText(name);
str << "<h2>" << name << "</h2>\n";
}
void initPre(std::ostream &str) {
str << "<pre style='white-space: pre-wrap;'>\n";
}
void finalizePre(std::ostream &str) {
str << "</pre>\n";
}
} // namespace HtmlLib
namespace CsLib {
std::string digTitle(const TScanProps &props) {
const TScanProps::const_iterator NA = props.end();
TScanProps::const_iterator it = props.find("title");
if (NA != it)
return it->second;
std::string title;
it = props.find("project-name");
if (NA == it) {
it = props.find("tool-args");
if (props.end() == it)
return "";
const std::string &args = it->second;
const boost::regex reSrpm("^.*[ /']([^ /']*)\\.src\\.rpm.*$");
boost::smatch sm;
if (!boost::regex_match(args, sm, reSrpm))
return "";
title = sm[/* NVR */ 1];
}
else
title = it->second;
it = props.find("diffbase-project-name");
if (NA != it) {
title += " - defects not occurring in ";
title += it->second;
}
return title;
}
void writeParseWarnings(std::ostream &str, const TScanProps &props) {
TScanProps::const_iterator itCount, itRatio;
itCount = props.find("cov-compilation-unit-count");
itRatio = props.find("cov-compilation-unit-ratio");
if (props.end() == itCount || props.end() == itRatio) {
// fallback to deprecated format produced by cov-mockbuild
itCount = props.find("compilation-unit-count");
itRatio = props.find("compilation-unit-ratio");
}
if (props.end() == itCount || props.end() == itRatio)
return;
try {
const int count = boost::lexical_cast<int>(itCount->second);
const int ratio = boost::lexical_cast<float>(itRatio->second);
if (ratio < parsingRatioThr)
str << "<p><b style='color: #FF0000;'>warning:</b> "
"low parsing ratio: " << ratio << "%</p>\n";
itCount = props.find("diffbase-cov-compilation-unit-count");
itRatio = props.find("diffbase-cov-compilation-unit-ratio");
if (props.end() == itCount || props.end() == itRatio) {
// fallback to deprecated format produced by cov-mockbuild
itCount = props.find("diffbase-compilation-unit-count");
itRatio = props.find("diffbase-compilation-unit-ratio");
}
if (props.end() == itCount || props.end() == itRatio)
return;
const int baseCount = boost::lexical_cast<int>(itCount->second);
const int baseRatio = boost::lexical_cast<float>(itRatio->second);
if (baseRatio < parsingRatioThr && baseRatio < ratio)
str << "<p><b style='color: #FF0000;'>warning:</b> "
"low parsing ratio in diff base: "
<< baseRatio << "%</p>\n";
if (!count || 100 * baseCount / count < parsingOldToNewRatioThr)
str << "<p><b style='color: #FF0000;'>warning:</b> "
"low count of parsed units in diff base: "
<< baseCount << "</p>\n";
}
catch (boost::bad_lexical_cast &) {
// failed to parse count/ratio
}
}
void writeScanProps(std::ostream &str, const TScanProps &props) {
if (props.empty())
return;
HtmlLib::initSection(str, "Scan Properties");
str << "<table style='font-family: monospace;'>\n";
int i = 0;
BOOST_FOREACH(TScanProps::const_reference item, props) {
const char *trStyle = "";
if (++i & 1)
trStyle = " style='background-color: #EEE;'";
str << "<tr" << trStyle << "><td style='padding-right: 8px;'>"
<< item.first << "</td><td>" << item.second << "</td></tr>\n";
}
str << "</table>\n";
}
} // namespace CsLib
class HtmlWriterCore {
public:
HtmlWriterCore(
std::ostream &str,
const std::string &titleFallback,
const std::string &spPlacement);
bool headerWritten() const {
return headerWritten_;
}
// FIXME: the API needs to be improved
void writeHeaderOnce(
const TScanProps &props,
const std::string &plainTextUrl);
void closeDocument(const TScanProps &props);
private:
std::ostream &str_;
std::string titleFallback_;
bool spOnTop_;
bool spBottom_;
bool headerWritten_;
bool documentClosed_;
};
HtmlWriterCore::HtmlWriterCore(
std::ostream &str,
const std::string &titleFb,
const std::string &spPlacement):
str_(str),
titleFallback_(titleFb),
spOnTop_(!spPlacement.compare("top")),
spBottom_(!spPlacement.compare("bottom")),
headerWritten_(false),
documentClosed_(false)
{
if (titleFallback_.empty())
titleFallback_ = "Scan Results";
if (!spOnTop_ && !spBottom_ && !!spPlacement.compare("none"))
std::cerr << "warning: unknown placement of scan properties table: "
<< spPlacement << "\n";
}
void HtmlWriterCore::writeHeaderOnce(
const TScanProps &props,
const std::string &plainTextUrl)
{
assert(!documentClosed_);
if (headerWritten_)
// header already out
return;
// resolve title of the document
std::string title = CsLib::digTitle(props);
if (title.empty())
title = titleFallback_;
// initialize a HTML document
HtmlLib::initHtml(str_, title);
if (!plainTextUrl.empty())
HtmlLib::writeLink(str_, plainTextUrl, "[Show plain-text results]");
// write scan properties
CsLib::writeParseWarnings(str_, props);
if (spOnTop_)
CsLib::writeScanProps(str_, props);
// initialize the section for defects
HtmlLib::initSection(str_, "List of Defects");
HtmlLib::initPre(str_);
headerWritten_ = true;
}
void HtmlWriterCore::closeDocument(const TScanProps &props) {
assert(headerWritten_);
assert(!documentClosed_);
// close the HTML document
HtmlLib::finalizePre(str_);
if (spBottom_)
CsLib::writeScanProps(str_, props);
HtmlLib::finalizeHtml(str_);
documentClosed_ = true;
}
struct HtmlWriter::Private {
std::ostream &str;
HtmlWriterCore core;
TScanProps scanProps;
const std::string defUrlTemplate;
const boost::regex reEvent;
unsigned defCnt;
DefLookup *baseLookup;
boost::regex checkerIgnRegex;
std::string newDefMsg;
std::string plainTextUrl;
Private(
std::ostream &str_,
const std::string &titleFallback_,
const std::string &defUrlTemplate_,
const std::string &spPlacement_):
str(str_),
core(str_, titleFallback_, spPlacement_),
defUrlTemplate(defUrlTemplate_),
reEvent("^([^\\[]*\\[)?([^\\]]+)(])?$"),
defCnt(0),
baseLookup(0)
{
if (!defUrlTemplate.empty())
// just make sure that the format string is correct
boost::format(defUrlTemplate) % 1 % 2;
}
void writeLinkToDetails(const Defect &);
void writeNewDefWarning(const Defect &);
};
HtmlWriter::HtmlWriter(
std::ostream &outputStream,
const std::string &titleFallback,
const std::string &defUrlTemplate,
const std::string &spPlacement):
d(new Private(outputStream, titleFallback, defUrlTemplate, spPlacement))
{
}
HtmlWriter::~HtmlWriter() {
delete d;
}
const TScanProps& HtmlWriter::getScanProps() const {
return d->scanProps;
}
void HtmlWriter::setScanProps(const TScanProps &scanProps) {
assert(!d->core.headerWritten());
d->scanProps = scanProps;
}
void HtmlWriter::setDiffBase(
DefLookup *baseLookup,
const std::string &checkerIgnRegex,
const TScanProps &baseProps,
const std::string &baseTitleFallback)
{
assert(baseLookup);
d->baseLookup = baseLookup;
d->checkerIgnRegex = checkerIgnRegex;
// TODO: merge with already existing metadata stomping on the same keys
TScanProps::const_iterator it = baseProps.find("cov-compilation-unit-count");
if (baseProps.end() != it)
d->scanProps["diffbase-cov-compilation-unit-count"] = it->second;
it = baseProps.find("cov-compilation-unit-ratio");
if (baseProps.end() != it)
d->scanProps["diffbase-cov-compilation-unit-ratio"] = it->second;
it = baseProps.find("project-name");
const std::string projName = (baseProps.end() == it)
? baseTitleFallback
: it->second;
if (projName.empty()) {
d->newDefMsg = "newly introduced defect";
return;
}
d->newDefMsg += "defect not occurring in <b>";
d->newDefMsg += projName;
d->newDefMsg += "</b>";
}
void HtmlWriter::setPlainTextUrl(const std::string &url) {
d->plainTextUrl = url;
}
void HtmlWriter::Private::writeLinkToDetails(const Defect &def) {
const int defId = def.defectId;
if (!defId)
// no defect ID
return;
if (this->defUrlTemplate.empty())
// no defect URL template
return;
const TScanProps::const_iterator it = this->scanProps.find("project-id");
if (this->scanProps.end() == it)
// no project ID
return;
try {
const int projId = boost::lexical_cast<int>(it->second);
// write the link
this->str << " <a href ='"
<< boost::format(this->defUrlTemplate) % projId % defId
<< "'>[Show Details]</a>";
}
catch (boost::bad_lexical_cast &) {
// failed to parse project ID
}
}
void HtmlWriter::Private::writeNewDefWarning(const Defect &def) {
if (!this->baseLookup)
// not lookup set
return;
if (boost::regex_match(def.checker, this->checkerIgnRegex))
// user requested to ignore this checker for lookup
return;
if (this->baseLookup->lookup(def))
// defect found in the lookup
return;
// a newly introduced defect
this->str << " <span style='color: #00FF00;'>[<b>warning:</b> "
<< this->newDefMsg << "]</span>";
}
void linkifyShellCheckMsg(std::string *pMgs) {
static boost::regex reShellCheckMsg(" \\[SC([0-9]+)\\]$");
*pMgs = boost::regex_replace(*pMgs, reShellCheckMsg,
" <a href=\"https://github.com/koalaman/shellcheck/wiki/SC\\1\""
" title=\"description of ShellCheck's checker SC\\1\">"
"[SC\\1]</a>");
}
void HtmlWriter::handleDef(const Defect &def) {
d->core.writeHeaderOnce(d->scanProps, d->plainTextUrl);
// HTML anchor
d->str << "<a name='def" << ++(d->defCnt) << "'/>";
d->str << "<b>Error: <span style='background: #C0FF00;'>"
<< HtmlLib::escapeTextInline(def.checker) << "</span>";
if (def.cwe)
d->str << " (CWE-" << def.cwe << ")";
else
d->str << HtmlLib::escapeTextInline(def.annotation);
d->str << ":</b>";
d->writeLinkToDetails(def);
// link to self
d->str << " <a href ='#def"
<< d->defCnt << "'>[#def"
<< d->defCnt << "]</a>";
d->writeNewDefWarning(def);
d->str << "\n";
const unsigned cntEvents = def.events.size();
for (unsigned idx = 0; idx < cntEvents; ++idx) {
const DefEvent &evt = def.events[idx];
const bool isComment = (evt.event == "#");
switch (evt.verbosityLevel) {
case 1:
if (isComment)
d->str << "<span style='color: #00C0C0;'>";
else
d->str << "<span style='color: #808080;'>";
break;
case 2:
d->str << "<span style='color: #C0C0C0;'>";
break;
}
if (!evt.fileName.empty())
d->str << HtmlLib::escapeTextInline(evt.fileName) << ":";
if (0 < evt.line)
d->str << evt.line << ":";
if (0 < evt.column)
d->str << evt.column << ":";
if (isComment) {
d->str << "#";
}
else {
d->str << " ";
boost::smatch sm;
const std::string &evtName = evt.event;
if (boost::regex_match(evtName, sm, d->reEvent))
d->str
<< HtmlLib::escapeTextInline(sm[1]) << "<b>"
<< HtmlLib::escapeTextInline(sm[2]) << "</b>"
<< HtmlLib::escapeTextInline(sm[3]);
else
d->str << "<b>" << HtmlLib::escapeTextInline(evtName) << "</b>";
d->str << ": ";
}
static CtxEventDetector detector;
const bool isCtxLine = detector.isAnyCtxLine(evt);
if (isCtxLine) {
const char *color = (detector.isKeyCtxLine(evt))
? "000000"
: "C0C0C0";
d->str << "<span style='color: #" << color << ";'>";
}
// translate message text
std::string msgText = HtmlLib::escapeTextInline(evt.msg);
if (def.checker == "SHELLCHECK_WARNING")
linkifyShellCheckMsg(&msgText);
d->str << msgText;
if (isCtxLine)
d->str << "</span>";
switch (evt.verbosityLevel) {
case 1:
case 2:
d->str << "</span>";
}
d->str << "\n";
}
d->str << "\n";
}
void HtmlWriter::flush() {
d->core.writeHeaderOnce(d->scanProps, d->plainTextUrl);
d->core.closeDocument(d->scanProps);
}