Repository navigation
Expand file tree
/
Copy pathF1X.cpp
More file actions
321 lines (271 loc) · 10.1 KB
/
Copy pathF1X.cpp
File metadata and controls
321 lines (271 loc) · 10.1 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
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
/*
This file is part of f1x.
Copyright (C) 2016 Sergey Mechtaev, Gao Xiang, Shin Hwei Tan, Abhik Roychoudhury
f1x 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
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <ctime>
#include <sstream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include "Core.h"
#include "Global.h"
#include "Repair.h"
#include "Util.h"
namespace po = boost::program_options;
namespace fs = boost::filesystem;
using std::vector;
using std::string;
void initializeTrivialLogger(bool verbose) {
boost::log::add_common_attributes();
boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
boost::log::add_console_log(std::cerr, boost::log::keywords::format = "[%TimeStamp%] [%Severity%]\t%Message%");
if (verbose) {
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::debug);
} else {
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info);
}
}
std::vector<ProjectFile> parseFilesArg(const std::vector<std::string> &args) {
std::vector<ProjectFile> files;
for (auto &arg : args) {
boost::filesystem::path file;
unsigned fromLine = 0;
unsigned toLine = 0;
auto colonIndex = arg.find(":");
if (colonIndex == std::string::npos) {
file = boost::filesystem::path(arg);
} else {
std::string pathStr = arg.substr(0, colonIndex);
file = boost::filesystem::path(pathStr);
std::string rangeStr = arg.substr(colonIndex + 1);
auto dashIndex = rangeStr.find("-");
if (dashIndex == std::string::npos) {
try {
fromLine = std::stoi(rangeStr);
toLine = fromLine;
} catch (...) {
throw parse_error("wrong range format: " + rangeStr);
}
} else {
std::string fromStr = rangeStr.substr(0, dashIndex);
std::string toStr = rangeStr.substr(dashIndex + 1);
try {
fromLine = std::stoi(fromStr);
toLine = std::stoi(toStr);
} catch (...) {
throw parse_error("wrong range format: " + rangeStr);
}
}
}
file = relativeTo(fs::current_path(), fs::canonical(file));
if (! boost::filesystem::exists(file)) {
throw parse_error("source file does not exist: " + file.string());
}
files.push_back(ProjectFile{file, fromLine, toLine});
}
return files;
}
int main (int argc, char *argv[]) {
string buildCmd = "make -e";
vector<ProjectFile> files;
fs::path output;
vector<string> tests;
unsigned testTimeout;
fs::path driver;
po::options_description general("Usage: f1x OPTIONS\n\nSupported options");
general.add_options()
("driver,d", po::value<string>()->value_name("PATH"), "test driver")
("tests,t", po::value<vector<string>>()->multitoken()->value_name("ID..."), "list of test IDs")
("test-timeout,T", po::value<unsigned>()->value_name("MS"), "test execution timeout")
("files,f", po::value<vector<string>>()->multitoken()->value_name("PATH..."), "list of source files to repair")
("localize,l", po::value<unsigned>()->value_name("NUM"), ("number of files to localize (default: " + std::to_string(cfg.filesToLocalize) + ")").c_str())
("build,b", po::value<string>()->value_name("CMD"), ("build command (default: " + buildCmd + ")").c_str())
("output,o", po::value<string>()->value_name("PATH"), "output patch file or directory (default: f1x-TIME)")
("all,a", "generate all patches")
("cost,c", po::value<string>()->value_name("FUNCTION"), "patch prioritization (default: syntactic-diff)")
("verbose,v", "produce extended output")
("help,h", "produce help message and exit")
("version", "print version and exit")
("output-stat", po::value<string>()->value_name("PATH"), "output execution statistics")
("output-space", po::value<string>()->value_name("PATH"), "[DEBUG] output search space")
("output-one-per-loc", "output single optimal patch per location")
("output-top", po::value<unsigned>()->value_name("N"), "find top N patches")
("enable-cleanup", "remove intermediate data")
("enable-metadata", "output patch metadata")
("enable-validation", "validate found patches")
("enable-assignment", "synthesize assignments")
("enable-llvm-cov", "use llvm-cov instead of gcov")
("disable-guard", "don't synthesize guards")
("disable-vteq", "[DEBUG] don't apply value-based analysis")
("disable-dteq", "[DEBUG] don't apply dependency-based analysis")
("disable-testprior", "[DEBUG] don't prioritize tests")
;
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).options(general).run(), vm);
po::notify(vm);
} catch(po::error& e) {
BOOST_LOG_TRIVIAL(error) << e.what() << " (use --help)";
return ERROR_EXIT_CODE;
}
if (vm.count("help")) {
std::cout << general << std::endl;
return ERROR_EXIT_CODE;
}
if (vm.count("cost")) {
std::string costFunction = vm["cost"].as<string>();
if (costFunction == "syntactic-diff") {
cfg.patchPrioritization = PatchPrioritization::SYNTACTIC_DIFF;
} else if (costFunction == "semantic-diff") {
cfg.patchPrioritization = PatchPrioritization::SEMANTIC_DIFF;
if (!vm.count("all")) {
cfg.generateAll = true;
BOOST_LOG_TRIVIAL(info) << "generating all patches to apply dynamic prioritization";
}
} else {
BOOST_LOG_TRIVIAL(error) << "supported cost functions: syntactic-diff, semantic-diff";
return ERROR_EXIT_CODE;
}
}
if (vm.count("version")) {
std::cout << "f1x " << F1X_VERSION_MAJOR <<
"." << F1X_VERSION_MINOR <<
"." << F1X_VERSION_PATCH << std::endl;
return ERROR_EXIT_CODE;
}
if (vm.count("verbose")) {
cfg.verbose = true;
}
initializeTrivialLogger(cfg.verbose);
if (vm.count("all")) {
cfg.generateAll = true;
}
if (vm.count("output-one-per-loc")) {
cfg.outputOnePerLocation = true;
}
if (vm.count("output-top")) {
cfg.outputTop = vm["output-top"].as<unsigned>();
}
if (vm.count("enable-metadata")) {
cfg.outputPatchMetadata = true;
}
if (vm.count("enable-cleanup")) {
cfg.removeIntermediateData = true;
}
if (vm.count("enable-validation")) {
cfg.validatePatches = true;
}
if (vm.count("enable-llvm-cov")) {
cfg.useLLVMCov = true;
}
if (vm.count("disable-vteq")) {
cfg.valueTEQ = false;
}
if (vm.count("disable-testprior")) {
cfg.testPrioritization = TestPrioritization::FIXED_ORDER;
}
if (vm.count("disable-guard")) {
cfg.addGuards = false;
}
if (vm.count("output-space")) {
cfg.searchSpaceFile = fs::absolute(vm["output-space"].as<string>()).string();
}
if (!vm.count("tests")) {
BOOST_LOG_TRIVIAL(error) << "tests are not specified (use --help)";
return ERROR_EXIT_CODE;
}
tests = vm["tests"].as<vector<string>>();
if (!vm.count("driver")) {
BOOST_LOG_TRIVIAL(error) << "test driver is not specified (use --help)";
return ERROR_EXIT_CODE;
}
driver = fs::absolute(vm["driver"].as<string>());
if (!vm.count("test-timeout")) {
BOOST_LOG_TRIVIAL(error) << "test execution timeout is not specified (use --help)";
return ERROR_EXIT_CODE;
}
testTimeout = vm["test-timeout"].as<unsigned>();
if (vm.count("localize")) {
cfg.filesToLocalize = vm["localize"].as<unsigned>();
}
if (vm.count("files")) {
vector<string> fileArgs = vm["files"].as<vector<string>>();
try {
files = parseFilesArg(fileArgs);
} catch (const parse_error& e) {
BOOST_LOG_TRIVIAL(error) << e.what();
return ERROR_EXIT_CODE;
}
}
if (vm.count("build")) {
buildCmd = vm["build"].as<string>();
}
if (!vm.count("output")) {
std::time_t now = std::time(0);
struct std::tm tstruct;
char timeRepr[80];
tstruct = *localtime(&now);
strftime(timeRepr, sizeof(timeRepr), "f1x-%Y_%m_%d-%H_%M_%S", &tstruct);
std::stringstream name;
name << timeRepr;
if (!cfg.generateAll)
name << ".patch";
output = fs::path(name.str());
} else {
output = fs::path(vm["output"].as<string>());
}
output = fs::absolute(output);
if (fs::exists(output)) {
BOOST_LOG_TRIVIAL(warning) << "existing " << output << " will be overwritten";
fs::remove_all(output);
}
fs::path dataDir = fs::temp_directory_path() / fs::unique_path();
fs::create_directory(dataDir);
BOOST_LOG_TRIVIAL(info) << "intermediate data directory: " << dataDir;
cfg.dataDir = dataDir.string();
RepairStatus status;
{
Project project(files, buildCmd);
TestingFramework tester(project, driver, testTimeout);
status = repair(project, tester, tests, output);
}
// NOTE: project is already destroyed here
if (cfg.removeIntermediateData) {
fs::remove_all(dataDir);
}
switch (status) {
case RepairStatus::SUCCESS:
if (cfg.generateAll) {
BOOST_LOG_TRIVIAL(info) << "patches successfully generated: " << output;
} else {
BOOST_LOG_TRIVIAL(info) << "patch successfully generated: " << output;
}
return SUCCESS_EXIT_CODE;
case RepairStatus::FAILURE:
BOOST_LOG_TRIVIAL(info) << "no patch found";
return FAILURE_EXIT_CODE;
case RepairStatus::ERROR:
return ERROR_EXIT_CODE;
case RepairStatus::NO_NEGATIVE_TESTS:
BOOST_LOG_TRIVIAL(info) << "nothing to do";
return NO_NEGATIVE_TESTS_EXIT_CODE;
}
return ERROR_EXIT_CODE;
}