forked from v-yatsenko/binder-aio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporter.cpp
More file actions
291 lines (237 loc) · 8.96 KB
/
reporter.cpp
File metadata and controls
291 lines (237 loc) · 8.96 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
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
// Copyright (c) 2018 Vadym Yatsenko <[email protected]>
//
// All rights reserved. Use of this source code is governed by a
// MIT license that can be found in the LICENSE file.
/// @file binder/reporter.cpp
/// @brief Binding statistic reporting
/// @author Vadym Yatsenko
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <reporter.hpp>
using namespace binder;
using namespace llvm;
using namespace clang;
using boost::property_tree::ptree;
#define JACOCO_DTD "<!DOCTYPE report SYSTEM 'http://www.eclemma.org/jacoco/trunk/coverage/report.dtd' >"
class CoverageExporterJacoco {
ptree doc;
void emitMethod(ptree& tree, ptree& sourcefile, const PythonCoverage::Method& m, unsigned& covered, unsigned& missed)
{
ptree method;
method.put( "<xmlattr>.name", m.name );
method.put( "<xmlattr>.desc", m.desc );
method.put( "<xmlattr>.line", m.declaration_line );
ptree m_counter;
m_counter.put( "<xmlattr>.type", "METHOD" );
ptree l_counter;
l_counter.put( "<xmlattr>.type", "LINE" );
ptree i_counter;
i_counter.put( "<xmlattr>.type", "INSTRUCTION" );
ptree line;
line.put( "<xmlattr>.ln", m.declaration_line );
llvm::outs() << " " << m.name << " use " << m.usages << "\n";
if( m.usages > 0 ) {
m_counter.put( "<xmlattr>.covered", 1 );
m_counter.put( "<xmlattr>.missed", 0 );
l_counter.put( "<xmlattr>.covered", 1 );
l_counter.put( "<xmlattr>.missed", 0 );
i_counter.put( "<xmlattr>.covered", 1 );
i_counter.put( "<xmlattr>.missed", 0 );
line.put( "<xmlattr>.ci", 1 );
line.put( "<xmlattr>.mi", 0 );
covered++;
} else {
m_counter.put( "<xmlattr>.covered", 0 );
m_counter.put( "<xmlattr>.missed", 1 );
l_counter.put( "<xmlattr>.covered", 0 );
l_counter.put( "<xmlattr>.missed", 1 );
i_counter.put( "<xmlattr>.covered", 0 );
i_counter.put( "<xmlattr>.missed", 1 );
line.put( "<xmlattr>.ci", 0 );
line.put( "<xmlattr>.mi", 1 );
missed++;
}
method.add_child( "counter", m_counter );
method.add_child( "counter", l_counter );
//method.add_child( "counter", i_counter );
tree.add_child( "method", method );
sourcefile.add_child( "line", line );
}
void emitClass(ptree& tree, ptree& sourcefile, const PythonCoverage::Class& c, unsigned& covered, unsigned& missed)
{
unsigned _covered = 0;
unsigned _missed = 0;
ptree cl;
cl.put( "<xmlattr>.name", c.name );
cl.put( "<xmlattr>.sourcefilename", c.file_name );
sourcefile.put( "<xmlattr>.name", c.file_name );
llvm::outs() << " " << c.name << ":" << "\n";
for( const PythonCoverage::Method& m: c.methods ) {
emitMethod( cl, sourcefile, m, _covered, _missed );
}
ptree counter;
counter.put( "<xmlattr>.type", "METHOD" );
counter.put( "<xmlattr>.covered", _covered );
counter.put( "<xmlattr>.missed", _missed );
cl.add_child( "counter", counter );
tree.add_child( "class", cl );
ptree* class_counter = nullptr;
ptree* method_counter = nullptr;
ptree* line_counter = nullptr;
try {
for( auto src: sourcefile.get_child( "counter" ) ) {
if( src.second.get<std::string>( "<xmlattr>.type" ) == "CLASS" ) {
class_counter = &src.second;
}
}
}
catch(...) {
ptree counter;
counter.put( "<xmlattr>.type", "CLASS" );
class_counter = &sourcefile.add_child( "counter", counter );
}
try {
for( auto src: sourcefile.get_child( "counter" ) ) {
if( src.second.get<std::string>( "<xmlattr>.type" ) == "METHOD" ) {
method_counter = &src.second;
}
}
}
catch(...) {
ptree counter;
counter.put( "<xmlattr>.type", "METHOD" );
method_counter = &sourcefile.add_child( "counter", counter );
}
try {
for( auto src: sourcefile.get_child( "counter" ) ) {
if( src.second.get<std::string>( "<xmlattr>.type" ) == "LINE" ) {
line_counter = &src.second;
}
}
}
catch(...) {
ptree counter;
counter.put( "<xmlattr>.type", "LINE" );
line_counter = &sourcefile.add_child( "counter", counter );
}
int class_c = class_counter->get<int>( "<xmlattr>.covered", 0 ) + 1;
int class_m = class_counter->get<int>( "<xmlattr>.missed", 0 );
int methods_c = method_counter->get<int>( "<xmlattr>.covered", 0 ) + _covered;
int methods_m = method_counter->get<int>( "<xmlattr>.missed", 0 ) + _missed;
int line_c = line_counter->get<int>( "<xmlattr>.covered", 0 ) + _covered;
int line_m = line_counter->get<int>( "<xmlattr>.missed", 0 ) + _missed;
class_counter->put( "<xmlattr>.covered", class_c );
class_counter->put( "<xmlattr>.missed", class_m );
method_counter->put( "<xmlattr>.covered", methods_c );
method_counter->put( "<xmlattr>.missed", methods_m );
line_counter->put( "<xmlattr>.covered", methods_c );
line_counter->put( "<xmlattr>.missed", methods_m );
covered += _covered;
missed += _missed;
}
void emitPackages(ptree& tree, const std::vector<PythonCoverage::Package>& packages, unsigned& covered, unsigned& missed)
{
for( auto p: packages ) {
unsigned _covered = 0;
unsigned _missed = 0;
ptree package;
package.put( "<xmlattr>.name", p.name );
ptree sourcefile;
sourcefile.put( "<xmlattr>.name", p.name ); // TODO: determine name properly
llvm::outs() << "Reporting: " << p.name << ":" << "\n";
if( !p.classes.empty() ) {
for( const PythonCoverage::Class& c: p.classes ) {
ptree* class_src = nullptr;
try {
for( auto src: package.get_child( "sourcefile") ) {
if( src.second.get<std::string>( "<xmlattr>.name" ) == c.file_name ) {
class_src = &src.second;
break;
}
}
} catch(...) {
ptree src;
src.put( "<xmlattr>.name", c.file_name );
class_src = &package.add_child( "sourcefile", src );
}
emitClass( package, *class_src, c, _covered, _missed );
package.add_child( "sourcefile", *class_src );
}
}
if( !p.methods.empty() ) {
for( const PythonCoverage::Method& m: p.methods ) {
emitMethod( package, sourcefile, m, _covered, _missed );
}
package.add_child( "sourcefile", sourcefile );
}
if ( _covered or _missed ) { // Emit only known packages
ptree counter_methods;
counter_methods.put( "<xmlattr>.type", "METHOD" );
counter_methods.put( "<xmlattr>.covered", _covered );
counter_methods.put( "<xmlattr>.missed", _missed );
package.add_child( "counter", counter_methods );
ptree counter_classes;
counter_classes.put( "<xmlattr>.type", "CLASS" );
counter_classes.put( "<xmlattr>.covered", p.classes.size() );
counter_classes.put( "<xmlattr>.missed", 0 );
package.add_child( "counter", counter_classes );
tree.add_child( "package", package );
covered += _covered;
missed += _missed;
}
}
}
public:
CoverageExporterJacoco()
{
}
/// \brief Print the CoverageMapping.
void print(const PythonCoverage& CoverageMapping, const std::string& file)
{
unsigned covered = 0;
unsigned missed = 0;
time_t stamp = time(0);
ptree report;
report.put( "<xmlattr>.name", Config::get().root_module );
ptree sessioninfo;
sessioninfo.put( "<xmlattr>.id", Config::get().root_module );
sessioninfo.put( "<xmlattr>.start", stamp );
report.add_child( "sessioninfo", sessioninfo );
ptree group;
group.put( "<xmlattr>.name", Config::get().root_module );
emitPackages( group, CoverageMapping.packages, covered, missed );
ptree counter;
counter.put( "<xmlattr>.type", "METHOD" );
counter.put( "<xmlattr>.covered", covered );
counter.put( "<xmlattr>.missed", missed );
group.add_child( "counter", counter );
report.add_child( "group", group );
ptree overal_counter;
overal_counter.put( "<xmlattr>.type", "METHOD" );
overal_counter.put( "<xmlattr>.covered", covered );
overal_counter.put( "<xmlattr>.missed", missed );
report.add_child( "counter", overal_counter );
doc.add_child( "report", report );
boost::property_tree::xml_writer_settings<std::string> w( ' ', 2 );
boost::property_tree::write_xml( file, doc, std::locale(), w );
std::error_code EC;
std::basic_ofstream<ptree::key_type::value_type> stream( file.c_str() );
stream.imbue( std::locale() );
stream << "<?xml version=\"1.0\" encoding=\""
<< w.encoding
<< "\"?>\n";
stream << JACOCO_DTD "\n";
boost::property_tree::xml_parser::write_xml_element(stream, std::basic_string<ptree::key_type::value_type>(), doc, -1, w);
stream.close();
}
};
Reporter& Reporter::get()
{
static Reporter instance;
return instance;
}
void Reporter::write_report(const std::string& path)
{
CoverageExporterJacoco exporter;
exporter.print(Coverage, path);
}