-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcssort.cc
246 lines (199 loc) · 6.57 KB
/
cssort.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
/*
* 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 "abstract-writer.hh"
#include "version.hh"
#include <boost/foreach.hpp>
#include <boost/program_options.hpp>
#include <boost/regex.hpp>
class SortFactory {
public:
AbstractWriter* create(const std::string &key, EColorMode cm);
};
template <class TItem>
class GenericSort: public AbstractWriter {
private:
typedef std::vector<TItem> TCont;
TCont cont_;
TScanProps scanProps_;
EColorMode cm_;
public:
GenericSort(const EColorMode cm):
cm_(cm)
{
}
virtual void flush() {
// sort the container
std::sort(cont_.begin(), cont_.end());
// use the same output format is the input format
AbstractWriter *writer = createWriter(this->inputFormat(), cm_);
writer->setScanProps(scanProps_);
// write the data
BOOST_FOREACH(const Defect &def, cont_)
writer->handleDef(def);
// flush data and destroy writer
writer->flush();
delete writer;
}
virtual const TScanProps& getScanProps() const {
return scanProps_;
}
virtual void setScanProps(const TScanProps &scanProps) {
scanProps_ = scanProps;
}
protected:
virtual void handleDef(const Defect &def) {
cont_.push_back(static_cast<const TItem &>(def));
}
};
inline bool cmpFileNames(const Defect &a, const Defect &b) {
const TEvtList &ea = a.events;
const TEvtList &eb = b.events;
// first compare the key events
bool result;
if (cmpEvents(&result, ea[a.keyEventIdx], eb[b.keyEventIdx]))
return result;
// the key events are incomparable, compare all events
for (unsigned idx = 0;; ++idx) {
if (eb.size() <= idx)
// this includes the case where the events are equal
return false;
if (ea.size() <= idx)
return true;
if (cmpEvents(&result, ea[idx], eb[idx]))
return result;
}
}
struct DefByChecker: public Defect { };
bool operator<(const DefByChecker &a, const DefByChecker &b) {
// compare checker names
RETURN_IF_COMPARED(a, b, checker);
// resolve key events
const DefEvent &ea = a.events[a.keyEventIdx];
const DefEvent &eb = b.events[b.keyEventIdx];
if ("SHELLCHECK_WARNING" == a.checker /* == b.checker */) {
// sort ShellCheck warnings by the [SC1234] suffixes
const boost::regex reCode("^.* \\[SC([0-9]+)\\]$");
std::string aCode, bCode;
boost::smatch sm;
if (boost::regex_match(ea.msg, sm, reCode))
aCode = sm[1];
if (boost::regex_match(eb.msg, sm, reCode))
bCode = sm[1];
if (aCode < bCode)
return true;
if (bCode < aCode)
return false;
}
// compare name of the key events
RETURN_IF_COMPARED(ea, eb, event);
return cmpFileNames(a, b);
}
struct DefByPath: public Defect { };
bool operator<(const DefByPath &a, const DefByPath &b) {
return cmpFileNames(a, b);
}
AbstractWriter* SortFactory::create(const std::string &key, const EColorMode cm)
{
if (!key.compare("checker"))
return new GenericSort<DefByChecker>(cm);
if (!key.compare("path"))
return new GenericSort<DefByPath>(cm);
// no comparator matched
return 0;
}
static std::string name;
namespace po = boost::program_options;
template <class TDesc, class TStream>
void printUsage(TStream &str, const TDesc &desc) {
desc.print(str);
// TODO: output some additional documentation
}
int main(int argc, char *argv[])
{
using std::string;
::name = argv[0];
po::variables_map vm;
po::options_description desc(string("Usage: ") + name
+ " [options] [file1.err [...]], where options are");
typedef std::vector<string> TStringList;
string key;
try {
desc.add_options()
("key", po::value<string>(&key)->default_value("path"),
"checker, path")
("quiet,q", "do not report any parsing errors");
addColorOptions(&desc);
desc.add_options()
("help", "produce help message")
("version", "print version");
po::options_description hidden("");
hidden.add_options()
("input-file", po::value<TStringList>(), "input file");
po::positional_options_description p;
p.add("input-file", -1);
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
po::options_description opts;
opts.add(desc).add(hidden);
po::store(po::command_line_parser(argc, argv).
options(opts).positional(p).run(), vm);
po::notify(vm);
}
catch (po::error &e) {
std::cerr << name << ": error: " << e.what() << "\n\n";
printUsage(std::cerr, desc);
return 1;
}
if (vm.count("help")) {
printUsage(std::cout, desc);
return 0;
}
if (vm.count("version")) {
std::cout << CS_VERSION << "\n";
return 0;
}
EColorMode cm;
const char *err;
if (!readColorOptions(&cm, &err, vm)) {
std::cerr << name << ": error: " << err << std::endl;
return 1;
}
SortFactory factory;
AbstractWriter *eng = factory.create(key, cm);
if (!eng) {
std::cerr << name << ": error: unknown key: " << key << "\n\n";
printUsage(std::cerr, desc);
return 1;
}
const bool silent = vm.count("quiet");
bool hasError = false;
if (!vm.count("input-file")) {
hasError = !eng->handleFile("-", silent);
}
else {
const TStringList &files = vm["input-file"].as<TStringList>();
BOOST_FOREACH(const string &fileName, files) {
if (!eng->handleFile(fileName, silent))
hasError = true;
}
}
eng->flush();
delete eng;
return hasError;
}