forked from csutils/csdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter.cc
More file actions
145 lines (117 loc) · 3.8 KB
/
writer.cc
File metadata and controls
145 lines (117 loc) · 3.8 KB
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
/*
* Copyright (C) 2011-2022 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 "writer.hh"
#include "instream.hh"
#include "regex.hh"
#include "writer-cov.hh"
#include "writer-html.hh"
#include "writer-json.hh"
// /////////////////////////////////////////////////////////////////////////////
// implementation of AbstractWriter
bool AbstractWriter::handleFile(Parser &parser)
{
this->notifyFile(parser.input().fileName());
// detect the input format and create the parser
if (inputFormat_ == FF_INVALID)
inputFormat_ = parser.inputFormat();
// read scan properties if still empty
if (this->getScanProps().empty())
this->setScanProps(parser.getScanProps());
Defect def;
while (parser.getNext(&def))
this->handleDef(def);
return ignoreParserWarnings_ || !parser.hasError();
}
bool AbstractWriter::handleFile(InStream &input)
{
Parser parser(input);
return this->handleFile(parser);
}
bool AbstractWriter::handleFile(const std::string &fileName, bool silent)
{
try {
InStream str(fileName, silent, recordInputLocations_);
return this->handleFile(str);
}
catch (const InFileException &e) {
std::cerr << e.fileName << ": failed to open input file\n";
return false;
}
}
void AbstractWriter::setScanProps(const TScanProps &scanProps)
{
if (scanProps.empty())
return;
std::cerr << "warning: scan properties not supported by output format\n";
}
TWriterPtr createWriter(
std::ostream &strDst,
const EFileFormat format,
const EColorMode cm,
const TScanProps &scanProps)
{
TWriterPtr writer;
switch (format) {
case FF_GCC:
// we have no writer for GCC format, fallback to Coverity
// fall through!
case FF_INVALID:
case FF_COVERITY:
writer.reset(new CovWriter(strDst, cm));
break;
case FF_AUTO:
// TODO
case FF_JSON:
writer.reset(new JsonWriter(strDst));
break;
case FF_HTML: {
const std::string emp;
const std::string spPlacement = "bottom";
writer.reset(new HtmlWriter(strDst, emp, emp, spPlacement));
break;
}
case FF_SARIF:
writer.reset(new JsonWriter(strDst, FF_SARIF));
break;
}
if (!scanProps.empty())
writer->setScanProps(scanProps);
return writer;
}
// /////////////////////////////////////////////////////////////////////////////
// implementation of CtxEventDetector
struct CtxEventDetector::Private {
const RE reAnyCtxLine = RE("^ *[0-9]+\\|(?:->)? .*$");
const RE reKeyCtxLine = RE("^ *[0-9]+\\|-> .*$");
};
CtxEventDetector::CtxEventDetector():
d(new Private)
{
}
CtxEventDetector::~CtxEventDetector() = default;
bool CtxEventDetector::isAnyCtxLine(const DefEvent &evt) const
{
return (evt.event == "#")
&& boost::regex_match(evt.msg, d->reAnyCtxLine);
}
bool CtxEventDetector::isKeyCtxLine(const DefEvent &evt) const
{
return (evt.event == "#")
&& boost::regex_match(evt.msg, d->reKeyCtxLine);
}