-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
417 lines (353 loc) · 8.95 KB
/
main.cpp
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
/* NPath complexity analyser for C++.
Copyright (C) 2007 Eddy Pronk
Gnocchi 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 2
of the License, or (at your option) any later version.
Gnocchi 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 GNU Emacs; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <boost/bind.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/program_options.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>
#include "gcov_reader.hpp"
#include "analyser.hpp"
#include "reporter.hpp"
#include "npath_counter.hpp"
extern "C"
{
#include "config.h"
#include "system.h"
#include "version.h"
#include <getopt.h>
}
namespace po = boost::program_options;
namespace fs = boost::filesystem;
using namespace std;
using boost::lexical_cast;
static void print_version (void);
struct block_index
{
typedef std::multimap<int,int> block_map_t;
block_map_t block_map_;
string filename_;
const string& source_filename()
{
filename_ = "foo.c";
return filename_;
}
void on_line_inserted(int lineno ,int blockno)
{
block_map_.insert(make_pair(lineno, blockno));
}
std::string prefix_with_block_number(int lineno)
{
string prefix;
std::multimap<int,int>::iterator pos = block_map_.lower_bound(lineno);
if(pos != block_map_.upper_bound(lineno))
{
prefix = lexical_cast<std::string>(pos->second);
++pos;
}
for(;pos != block_map_.upper_bound(lineno); ++pos)
{
prefix += "," + lexical_cast<std::string>(pos->second);
}
return prefix;
}
};
struct npath_annotator
{
block_index index;
map<int,int> annotation;
string filename_;
npath_annotator(block_index& i) : index(i)
{
}
const string& source_filename() { return filename_; }
void annotate_file(const gcov_reader& reader, const data_model& f, const std::vector<long>& complexity)
{
if(f.function.filename != "/home/epronk/gnocchi/trunk/gcov_reader.cpp")
return;
filename_ = reader.function().filename.string();
std::multimap<int,int>::const_iterator pos = reader.block_map().begin();
for(;pos != reader.block_map().end(); ++pos)
{
if(annotation[pos->first] < complexity[pos->second])
{
annotation[pos->first] = complexity[pos->second];
}
}
}
std::string prefix_with_npath(int lineno)
{
if(annotation.find(lineno) != annotation.end())
return lexical_cast<std::string>(annotation.find(lineno)->second);
else
return "";
}
};
void print_file(const std::string& filename, boost::function< std::string(int) > prefix)
{
ifstream is(filename.c_str());
ofstream os((filename + ".gcov").c_str());
os << " -: 0:Source:" << filename << endl;
int lineno = 1;
while(!is.eof())
{
char buffer[1024];
is.getline(buffer, 1024);
os << setw(9) << prefix(lineno) << ":" << setw(5) << right << lineno << ":"
<< buffer << endl;
++lineno;
}
}
class file_processor : public reporter
{
Analyser analyser_;
typedef vector<fs::path> filelist_t;
typedef map<fs::path, fs::path> filemap_t;
filelist_t filelist;
filemap_t filemap;
public:
file_processor()
: analyser_(*this)
{
}
private:
virtual void on_function(const data_model& param)
{
filemap_t::iterator found = filemap.find(param.function.filename);
if (found != filemap.end())
{
cout << found->second.string();
}
else
{
cout << param.function.filename.string();
}
CountType npath_delta = param.npath_complexity_e - param.npath_complexity;
int cyclomatic_delta = param.cyclomatic_complexity_e - param.cyclomatic_complexity;
cout << ":" << param.function.line_number << ":"
<< " mccabe=" << param.cyclomatic_complexity;
if(cyclomatic_delta > 0)
cout << "(+" << cyclomatic_delta << ")";
cout << " npath=" << param.npath_complexity;
if(npath_delta > 0)
cout << "(+" << npath_delta << ") ";
cout << endl;
//assert(npath_delta >= 0);
if (npath_delta < 0)
{
cout << "overflow" << endl;
cout << param.npath_complexity_e << endl;
cout << param.npath_complexity << endl;
}
assert(cyclomatic_delta >= 0);
}
public:
void find_file( const fs::path& dir_path)
{
if ( !fs::exists( dir_path ) )
{
cout << "doesn't exist " << dir_path << endl;
return;
}
try
{
if ( fs::is_directory( dir_path ))
{
fs::directory_iterator end_itr;
for(fs::directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
try
{
filemap[itr->path().leaf()] = *itr;
}
catch(const std::exception& e)
{
cout << "FIXME " << e.what() << endl;
}
if ( fs::is_directory( *itr ) )
{
find_file(*itr);
}
else
{
if (fs::extension(*itr) == ".gcno")
{
filelist.push_back(*itr);
}
}
}
}
else
{
filelist.push_back(dir_path);
}
}
catch(const std::exception& e)
{
cout << "FIXME " << e.what() << endl;
}
}
void process_file(const fs::path& path)
{
if (!fs::exists(path))
{
cout << "doesn't exist " << path << endl;
return;
}
if(fs::is_directory(path))
find_file(path);
else
filelist.push_back(path);
}
int filelist_size()
{
return filelist.size();
}
void annotate_with_block_numbers()
{
if(filelist.size() == 1)
{
string filename = (filelist.begin())->string();
gcov_reader reader(analyser_);
block_index index;
reader.on_line_insert.connect(boost::bind(&block_index::on_line_inserted, &index, _1, _2));
reader.open(filename);
print_file(index.source_filename(), boost::bind(&block_index::prefix_with_block_number, &index, _1));
}
else
{
// error
}
}
void annotate_with_npath()
{
if(filelist.size() == 1)
{
string filename = (filelist.begin())->string();
gcov_reader reader(analyser_);
block_index index;
npath_annotator annotator(index);
analyser_.on_complexity_calculated.connect(boost::bind(&npath_annotator::annotate_file, &annotator, _1, _2, _3));
reader.open(filename);
print_file(annotator.source_filename(), boost::bind(&npath_annotator::prefix_with_npath, &annotator, _1));
}
else
{
// error
}
}
void build_filelist(const vector<string>& files)
{
BOOST_FOREACH(string s, files)
{
process_file(s);
}
}
void report()
{
BOOST_FOREACH(fs::path filename, filelist)
{
gcov_reader file(analyser_);
file.open(filename);
}
analyser_.report(1); // threshold);
}
};
int main(int ac, char* av[])
{
//fs::path::default_name_check( fs::native );
try
{
po::options_description generic("Generic options");
int threshold(0);
generic.add_options()
("version,v", "print version string")
("help,h", "produce help message")
("verbose", "be verbose");
po::options_description config("Configuration");
config.add_options()
("threshold,t", po::value<int>(&threshold)->default_value(1),
"npath threshold");
config.add_options()
("annotate-npath", "Write gcov style annotated source files with npath")
("annotate-block", "Write gcov style annotated source files with block no.");
po::options_description hidden("Hidden options");
hidden.add_options()
("input-file", po::value< vector<string> >(), "input file");
po::positional_options_description p;
p.add("input-file", -1);
po::options_description cmdline_options;
cmdline_options.add(generic).add(config).add(hidden);
po::options_description visible("Allowed options");
visible.add(generic).add(config);
po::variables_map options;
store(po::command_line_parser(ac, av).
options(cmdline_options).positional(p).run(), options);
notify(options);
if (options.count("help"))
{
cout << visible << "\n";
return 0;
}
if (options.count("version"))
{
print_version();
return 0;
}
file_processor processor;
if (options.count("input-file"))
{
processor.build_filelist(options["input-file"].as< vector<string> >());
}
else
{
cerr << "gnocchi: no input files" << endl;
return 1;
}
if(options.count("annotate-block"))
{
processor.annotate_with_block_numbers();
}
else if(options.count("annotate-npath"))
{
processor.annotate_with_npath();
}
else
{
processor.report();
}
}
catch(exception& e)
{
cout << e.what() << "\n";
return 1;
}
return 0;
}
static void
print_version (void)
{
cout << "gnocchi " << version_string;
printf ("Copyright (C) 2007 Eddy Pronk.\n");
printf ("This is free software; see the source for copying conditions.\n"
"There is NO warranty; not even for MERCHANTABILITY or \n"
"FITNESS FOR A PARTICULAR PURPOSE.\n\n");
}