-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarningdiff.cpp
executable file
·527 lines (437 loc) · 14.6 KB
/
warningdiff.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
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
#if 0
input="$(readlink -f "$(command -v "$0")")"
output="${0%.*}"
#// Compile ourselves
if [ "$input" -nt "$output" ] ; then
printf 'Compiling %s...\n' "${output##*/}" >&2
${CXX:-g++} -std=c++11 -Wall -Wextra "$input" -o "$output" > /dev/null < /dev/null || exit 1
fi
#// Run the executable
exec "$output" "$@"
exit
#endif // 0
/*
* Copyright (C) 2017 Daniel Scharrer
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the author(s) be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cstddef>
#include <fstream>
#include <iostream>
#include <cstdio>
#include <sstream>
#include <unordered_set>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/multi_array.hpp>
struct warning {
// Original warning line
std::string text;
// Parsed warning information
std::size_t line;
std::size_t pos;
std::string message;
// Corresponding code lines
std::string prev;
std::string code;
std::string next;
std::string indentation;
bool operator==(const warning & other) const {
return text == other.text;
}
};
typedef std::vector<warning> warnings;
typedef std::map<std::string, warnings> parsed_warnings;
typedef std::map<std::string, std::pair<warnings, warnings>> matched_warnings;
std::string exec(const std::string & cmd) {
std::string result;
FILE * pipe = popen(cmd.c_str(), "r");
if(pipe) {
while(!feof(pipe)) {
char buffer[2014];
size_t count = fread(buffer, 1, sizeof(buffer), pipe);
result.append(buffer, count);
}
pclose(pipe);
}
return result;
}
std::string escape(const std::string & str) {
return str; // TODO
}
std::unordered_set<std::string> git_get_files(const std::string & repo, const std::string & commit) {
std::unordered_set<std::string> result;
std::string contents = exec("git --git-dir=" + escape(repo) + " ls-tree --full-tree --name-only -r "
+ escape(commit)
+ " 2>/dev/null");
if(!contents.empty()) {
std::istringstream iss(contents);
std::string line;
while(std::getline(iss, line)) {
boost::trim(line);
result.insert(line);
}
}
return result;
}
std::vector<std::string> git_get_file(const std::string & repo, const std::string & filename,
const std::string & commit) {
std::vector<std::string> result;
std::string contents = exec("git --git-dir=" + escape(repo) + " show "
+ escape(commit) + ":" + escape(filename)
+ " 2>/dev/null");
if(!contents.empty()) {
std::istringstream iss(contents);
std::string line;
while(std::getline(iss, line)) {
boost::trim(line);
result.push_back(line);
}
}
return result;
}
std::string git_get_old_filename(const std::string & repo, const std::string & filename,
const std::string & old_commit, const std::string & commit) {
std::string result = exec("git --git-dir=" + escape(repo) + " log --oneline --name-only --follow "
+ escape(old_commit) + ".." + escape(commit) + " -- " + escape(filename)
+ " 2>/dev/null");
if(result.size() <= 1) {
return std::string();
}
std::size_t end = result.size();
std::size_t pos = result.find_last_of('\n');
if(pos != std::string::npos && pos + 1 == result.size()) {
end = pos;
pos = result.find_last_of('\n', pos - 1);
}
if(pos == std::string::npos) {
return std::string();
}
return result.substr(pos + 1, end - pos - 1);
}
struct WarningSorter {
bool operator()(const warning & a, const warning & b) {
if(a.line < b.line) {
return false;
} else if(a.line > b.line) {
return true;
}
if(a.pos < b.pos) {
return false;
} else if(a.pos > b.pos) {
return true;
}
return a.text.compare(b.text) > 0;
}
};
parsed_warnings parse(const std::string prefix, const std::string & repo,
const std::string & listfile, const std::string & commit) {
parsed_warnings result;
std::ifstream ifs(listfile);
if(!ifs.is_open()) {
std::cerr << "could not open \"" << listfile << "\"\n";
std::terminate();
}
std::unordered_set<std::string> files = git_get_files(repo, commit);
std::unordered_map<std::string, std::string> ifiles;
ifiles.reserve(files.size());
for(const std::string & file : files) {
ifiles.insert({ boost::to_lower_copy(file), file });
}
// Parse warning lines and group them by file
std::string line;
while(std::getline(ifs, line)) {
boost::trim(line);
if(line.empty()) {
continue;
}
std::string file;
warning w;
w.text = line;
w.line = std::size_t(-1);
w.pos = std::size_t(-1);
w.message = line;
std::size_t file_start = 0;
if(line[0] == '[') {
file_start = 1;
}
std::size_t file_end = line.find(':', file_start);
std::size_t alt_file_end = line.find('(', file_start);
if(file_end == std::string::npos || (alt_file_end != std::string::npos && alt_file_end < file_end)) {
file_end = alt_file_end;
}
if(file_end != std::string::npos && file_end != file_start) {
file = line.substr(file_start, file_end - file_start);
w.message = line.substr(file_end);
std::size_t line_start = file_end + 1;
std::size_t line_end = line.find_first_not_of("0123456789", line_start);
if(line_end != std::string::npos && line_end != line_start) {
try {
w.line = boost::lexical_cast<std::size_t>(line.substr(line_start, line_end - line_start));
w.message = line.substr(line_end);
std::size_t pos_start = line_end + 1;
std::size_t pos_end = line.find_first_not_of("0123456789", pos_start);
if(pos_end != std::string::npos && pos_end != pos_start) {
try {
w.pos = boost::lexical_cast<std::size_t>(line.substr(pos_start, pos_end - pos_start));
w.message = line.substr(pos_end);
} catch(...) { }
}
} catch(...) { }
}
}
std::size_t message_end = w.message.find(": Lines: ");
if(message_end != std::string::npos) {
w.message.resize(message_end);
}
while(true) {
size_t pos = w.message.find(" line ");
if(pos == std::string::npos) {
break;
}
size_t end = pos + 6;
if(end < w.message.length() && w.message[end] == '\'') {
end++;
}
while(end < w.message.length() && std::isdigit(w.message[end])) {
end++;
}
w.message.erase(pos, end - pos);
}
std::replace(file.begin(), file.end(), '\\', '/');
if(!file.empty() && file[0] != '/') {
bool prefixed = boost::starts_with(file, prefix);
if(prefixed) {
file = file.substr(prefix.length());
}
if(files.find(file) == files.end()) {
auto it = ifiles.find(boost::to_lower_copy(file));
if(it != ifiles.end()) {
file = it->second;
} else if(prefixed) {
std::cerr << "Could not find file \"" << file << "\" at commit " << commit << "\n";
}
}
}
result[file].push_back(w);
}
for(parsed_warnings::value_type & file : result) {
// Get the corresponding code line for each warning as well as some context
std::vector<std::string> code;
if(!file.first.empty() && file.first[0] != '/') {
code = git_get_file(repo, file.first, commit);
}
if(!code.empty()) {
for(warning & w : file.second) {
if(w.line != std::size_t(-1) && w.line > 0 && w.line <= code.size()) {
if(w.line > 1) {
w.prev = code[w.line - 2];
}
w.code = code[w.line - 1];
if(!w.code.empty()) {
std::size_t start = 0;
std::size_t end = w.code.length();
while(start < w.code.length() && std::isspace(w.code[start])) {
start++;
}
while(end > start && std::isspace(w.code[end - 1])) {
end++;
}
if(start < end) {
w.indentation = w.code.substr(0, start);
w.code = w.code.substr(start, end - start);
}
}
if(w.line + 1 <= code.size()) {
w.next = code[w.line];
}
}
}
}
// Sort warnings in reverse within a file by line number and position in the line
// Reverse sort allows us to avoid a reverse step later
std::sort(file.second.begin(), file.second.end(), WarningSorter());
file.second.erase(std::unique(file.second.begin(), file.second.end()), file.second.end());
}
return result;
}
matched_warnings match_files(const std::string & repo,
const parsed_warnings & a, const std::string commit_a,
const parsed_warnings & b, const std::string commit_b) {
matched_warnings result;
for(const parsed_warnings::value_type & file : b) {
std::string filename;
parsed_warnings::const_iterator i = a.end();
if(!file.first.empty() && file.first[0] != '/') {
std::string old_file = git_get_old_filename(repo, file.first, commit_a, commit_b);
if(b.find(old_file) == b.end()) {
filename = old_file;
i = a.find(filename);
}
}
if(i == a.end()) {
filename = file.first;
i = a.find(filename);
}
if(i == a.end()) {
result[filename] = std::make_pair(warnings(), file.second);
} else {
result[filename] = std::make_pair(i->second, file.second);
}
}
for(const parsed_warnings::value_type & file : a) {
if(result.find(file.first) == result.end()) {
result[file.first] = std::make_pair(file.second, warnings());
}
}
return result;
}
constexpr size_t insert_cost = 8 * 1024;
constexpr size_t edit_impossible = 3 * insert_cost + 1;
size_t edit_cost(const warning & a, const warning & b) {
if(a.text == b.text) {
return 0; // same warning
}
if(a.message != b.message || a.pos != b.pos || a.code != b.code) {
return edit_impossible; // different warning
}
if(a.pos == std::size_t(-1) && a.code.empty()) {
return edit_impossible; // no idea if the line moved or not
}
// Prefer warnings with closer line positions
size_t cost = std::min(size_t(std::labs(long(a.line) - long(b.line))), insert_cost / 4 - 1);
// Prefer lines with similar context
if(a.prev != b.prev) {
cost += insert_cost / 4;
}
if(a.next != b.next) {
cost += insert_cost / 4;
}
if(a.indentation != b.indentation) {
cost += std::max(std::min(size_t(std::labs(long(a.indentation.length()) - long(b.indentation.length()))),
insert_cost / 4 / 32),
size_t(1)) * 32;
}
return cost;
}
typedef std::vector<std::pair<char, const warning *>> change_list;
typedef std::unordered_multimap<std::string, size_t> change_remove_index;
void match_warnings(change_list & changes, change_remove_index & old_warnings, bool require_code = false) {
for(change_list::value_type & entry : changes) {
if(entry.first != '+' || entry.second->message.empty()) {
continue;
}
if((require_code || entry.second->pos == std::size_t(-1)) && entry.second->code.length() < 10) {
continue;
}
auto it = old_warnings.find(entry.second->message);
std::unordered_multimap<std::string, size_t>::iterator best = old_warnings.end();
std::size_t min_cost = std::numeric_limits<std::ptrdiff_t>::max();
for(; it != old_warnings.end() && it->first == entry.second->message; it++) {
std::size_t cost = edit_cost(*changes[it->second].second, *entry.second);
if(cost < edit_impossible && cost < min_cost) {
best = it;
min_cost = cost;
}
}
if(best != old_warnings.end()) {
entry.first = '=';
changes[best->second].first = '=';
old_warnings.erase(best);
}
}
}
change_list match_lines(const warnings & a, const warnings & b) {
// Calculate edit costs
boost::multi_array<size_t, 2> m(boost::extents[a.size() + 1][b.size() + 1]);
for(std::size_t i = 0; i <= a.size(); i++) {
m[i][0] = i * insert_cost;
}
for(std::size_t j = 0; j <= b.size(); j++) {
m[0][j] = j * insert_cost;
}
for(std::size_t i = 1; i <= a.size(); i++) {
for(std::size_t j = 1; j <= b.size(); j++) {
size_t cost = m[i - 1][j - 1] + edit_cost(a[i - 1], b[j - 1]);
cost = std::min(cost, m[i][j - 1] + insert_cost);
cost = std::min(cost, m[i - 1][j] + insert_cost);
m[i][j] = cost;
}
}
change_list changes;
change_remove_index old_warnings;
// Find the insert/remove(/replace) sequence with the cheapest edit cost by backtracing the path
// Warnings were sorted in reverse so we get the correct order here
std::size_t i = a.size(), j = b.size();
while(i != 0 || j != 0) {
if(i != 0 && m[i][j] == m[i - 1][j] + insert_cost) {
i--;
old_warnings.insert({ a[i].message, changes.size() });
changes.push_back({ '-', &a[i] });
} else if(j != 0 && m[i][j] == m[i][j - 1] + insert_cost) {
j--;
changes.push_back({ '+', &b[j] });
} else {
i--;
j--;
}
}
// Greedily match remaining insertions to the closest matching removed warning in the same file
match_warnings(changes, old_warnings);
return changes;
}
int main(int argc, const char * argv[]) {
if(argc < 7) {
std::cerr << "Usage: warningdiff <fileprefix> <gitrepo> <commita> <listfilea> <commitb> <listfileb>\n";
return 1;
}
std::string prefix = argv[1];
std::string repo = argv[2];
std::string commit_a = argv[3];
std::string list_a = argv[4];
std::string commit_b = argv[5];
std::string list_b = argv[6];
parsed_warnings a = parse(prefix, repo, list_a, commit_a);
parsed_warnings b = parse(prefix, repo, list_b, commit_b);
matched_warnings files = match_files(repo, a, commit_a, b, commit_b);
change_list changes;
change_remove_index old_warnings;
for(const matched_warnings::value_type & file : files) {
change_list file_changes = match_lines(file.second.first, file.second.second);
for(change_list::value_type & entry : file_changes) {
if(entry.first == '-') {
old_warnings.insert({ entry.second->message, changes.size() });
}
if(entry.first != '=') {
changes.push_back(entry);
}
}
}
// Greedily match remaining insertions to the closest matching removed warning in any file
match_warnings(changes, old_warnings, /* require_code = */ true);
for(const change_list::value_type & entry : changes) {
if(entry.first != '=') {
std::cout << entry.first << entry.second->text << '\n';
}
}
return 0;
}