-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathSynchronizer.cpp
More file actions
270 lines (238 loc) · 11.4 KB
/
Copy pathSynchronizer.cpp
File metadata and controls
270 lines (238 loc) · 11.4 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
#include "Synchronizer.h"
#include "clang-utils/SourceToHeaderRewriter.h"
#include "exceptions/FileSystemException.h"
#include "fetchers/Fetcher.h"
#include "printers/CCJsonPrinter.h"
#include "printers/SourceWrapperPrinter.h"
#include "printers/StubsPrinter.h"
#include "streams/stubs/StubsWriter.h"
#include "testgens/SnippetTestGen.h"
#include "utils/TypeUtils.h"
#include "loguru.h"
#include <iterator>
#include <utility>
#include <fstream>
using StubSet = std::unordered_set<StubOperator, HashUtils::StubHash>;
StubOperator::StubOperator(fs::path filePath, bool header) : sourceFilePath(std::move(filePath)), header(header) {}
StubOperator::StubOperator(fs::path filePath) : sourceFilePath(std::move(filePath)), header(Paths::isHeaderFile(this->sourceFilePath)) {}
bool StubOperator::operator==(const StubOperator &other) const {
return this->sourceFilePath == other.sourceFilePath && this->header == other.header;
}
fs::path StubOperator::getStubPath(const utbot::ProjectContext &projectContext) const {
return header ? Paths::sourcePathToStubHeaderPath(projectContext, sourceFilePath)
: Paths::sourcePathToStubPath(projectContext, sourceFilePath);
}
fs::path StubOperator::getSourceFilePath() const {
return sourceFilePath;
}
bool StubOperator::isHeader() const {
return header;
}
Synchronizer::Synchronizer(BaseTestGen *testGen,
types::TypesHandler::SizeContext *sizeContext)
: testGen(testGen), sizeContext(sizeContext) {
}
long long Synchronizer::getFileOutdatedTime(const fs::path &filePath) const {
return TimeUtils::convertFileToSystemClock(fs::last_write_time(filePath))
.time_since_epoch()
.count();
}
bool Synchronizer::isProbablyOutdatedStubs(const fs::path &srcFilePath) const {
fs::path stubFilePath = Paths::sourcePathToStubPath(testGen->projectContext, srcFilePath);
if (!fs::exists(stubFilePath)) {
return true;
}
std::ifstream stubFile(stubFilePath);
std::string sLine;
getline(stubFile, sLine);
long long stubTimestamp, srcTimestamp;
try {
stubTimestamp = std::stoll(sLine.substr(2));
} catch (...) {
return true;
}
if (!fs::exists(srcFilePath)) {
srcTimestamp = time(nullptr);
} else {
srcTimestamp = Synchronizer::getFileOutdatedTime(srcFilePath);
}
return stubTimestamp <= srcTimestamp;
}
bool Synchronizer::isProbablyOutdatedWrappers(const fs::path &srcFilePath) const {
fs::path wrapperFilePath = Paths::getWrapperFilePath(testGen->projectContext, srcFilePath);
if (!fs::exists(wrapperFilePath)) {
return true;
}
long long wrapperTimestamp, srcTimestamp;
wrapperTimestamp = Synchronizer::getFileOutdatedTime(wrapperFilePath);
if (!fs::exists(srcFilePath)) {
srcTimestamp = time(nullptr);
} else {
srcTimestamp = Synchronizer::getFileOutdatedTime(srcFilePath);
}
return wrapperTimestamp <= srcTimestamp;
}
CollectionUtils::FileSet Synchronizer::getOutdatedSourcePaths() const {
auto allFiles = testGen->getTargetSourceFiles();
auto outdatedSources = CollectionUtils::filterOut(testGen->getTargetSourceFiles(),
[this](fs::path const &sourcePath) {
return !isProbablyOutdatedWrappers(sourcePath);
});
return outdatedSources;
}
StubSet Synchronizer::getOutdatedStubs() const {
auto allFiles = getStubsFiles();
auto outdatedStubs = CollectionUtils::filterOut(allFiles, [this](StubOperator const &stubOperator) {
return !isProbablyOutdatedStubs(stubOperator.getSourceFilePath());
});
return outdatedStubs;
}
bool Synchronizer::removeStubIfSourceAbsent(const StubOperator &stub) const {
if (!fs::exists(stub.getSourceFilePath())) {
try {
fs::remove(stub.getStubPath(testGen->projectContext));
return true;
} catch (const fs::filesystem_error &e) {
std::string message = StringUtils::stringFormat("Failed to delete stub file: '%s'",
stub.getStubPath(testGen->projectContext));
LOG_S(ERROR) << message
<< e.what();
throw FileSystemException(message, e);
}
return false;
}
return false;
}
StubSet Synchronizer::getStubSetFromSources(const CollectionUtils::FileSet &sourcePaths) {
StubSet result;
for (bool header : {false, true}) {
auto stubs = CollectionUtils::transformTo<StubSet>(
sourcePaths, [header](const fs::path &sourcePath) {
return StubOperator(sourcePath, header);
});
result = CollectionUtils::unionSet(stubs, result);
}
return result;
}
StubSet Synchronizer::dropHeaders(const StubSet &stubs) {
return CollectionUtils::filterOut(stubs, [](const StubOperator &stub) {
return stub.isHeader();
});
}
CollectionUtils::FileSet Synchronizer::dropHeaders(const CollectionUtils::FileSet &files) {
return CollectionUtils::filterOut(files, [](const fs::path &file) {
return Paths::isHeaderFile(file);
});
}
void Synchronizer::synchronize(const types::TypesHandler &typesHandler) {
if (TypeUtils::isSameType<SnippetTestGen>(*this->testGen)) {
return;
}
if (testGen->settingsContext.useStubs) {
auto outdatedStubs = getOutdatedStubs();
synchronizeStubs(outdatedStubs, typesHandler);
}
auto outdatedSourcePaths = getOutdatedSourcePaths();
synchronizeWrappers(outdatedSourcePaths, typesHandler);
}
void Synchronizer::synchronizeStubs(StubSet &outdatedStubs,
const types::TypesHandler &typesHandler) {
StubSet allStubs = getStubsFiles();
auto stubDirPath = Paths::getStubsDirPath(testGen->projectContext);
prepareDirectory(stubDirPath);
auto filesInFolder = Paths::findFilesInFolder(stubDirPath);
auto presentStubs = CollectionUtils::transformTo<StubSet>(
dropHeaders(filesInFolder), [this](const fs::path &stubPath) {
return StubOperator(Paths::stubPathToSourcePath(this->testGen->projectContext, stubPath),
Paths::isHeaderFile(stubPath));
});
StubSet stubFiles = CollectionUtils::unionSet(allStubs, presentStubs);
tests::TestsMap stubFilesMap, sourceFilesMap;
for (const auto &outdatedStub : outdatedStubs) {
removeStubIfSourceAbsent(outdatedStub);
fs::path stubPath = outdatedStub.getStubPath(testGen->projectContext);
if (fs::exists(stubPath)) {
stubFilesMap[stubPath].sourceFilePath = stubPath;
}
sourceFilesMap[outdatedStub.getSourceFilePath()].sourceFilePath = outdatedStub.getSourceFilePath();
}
auto options = Fetcher::Options::Value::FUNCTION | Fetcher::Options::Value::INCLUDE | Fetcher::Options::Value::TYPE;
auto stubFetcher =
Fetcher(options, testGen->getProjectBuildDatabase()->compilationDatabase, sourceFilesMap, &testGen->types,
&sizeContext->maximumAlignment,
testGen->compileCommandsJsonPath, false);
stubFetcher.fetchWithProgress(testGen->progressWriter, "Finding source files required for stubs", true);
fs::path ccJsonStubDirPath =
Paths::getUTBotBuildDir(testGen->projectContext) / "stubs_build_files";
// todo: is it needed?
auto stubsCdb = createStubsCompilationDatabase(stubFiles, ccJsonStubDirPath);
auto sourceToHeaderRewriter =
SourceToHeaderRewriter(testGen->projectContext, testGen->getProjectBuildDatabase()->compilationDatabase,
stubFetcher.getStructsToDeclare(), testGen->serverBuildDir, typesHandler);
for (const StubOperator &outdatedStub : outdatedStubs) {
fs::path stubPath = outdatedStub.getStubPath(testGen->projectContext);
Tests const &methodDescription = stubFilesMap[stubPath];
tests::Tests tests = StubGen::mergeSourceFileIntoStub(
methodDescription, sourceFilesMap.at(outdatedStub.getSourceFilePath()));
if (outdatedStub.isHeader()) {
std::string code = sourceToHeaderRewriter.generateStubHeader(tests, outdatedStub.getSourceFilePath());
testGen->synchronizedStubs.emplace_back(stubPath, code);
} else {
printer::StubsPrinter stubsPrinter(Paths::getSourceLanguage(stubPath));
Stubs stubFile = stubsPrinter.genStubFile(tests, typesHandler, testGen->projectContext);
testGen->synchronizedStubs.emplace_back(stubFile);
}
}
StubsWriter::writeStubsFilesOnServer(testGen->synchronizedStubs, testGen->projectContext.getTestDirAbsPath());
}
std::shared_ptr<CompilationDatabase>
Synchronizer::createStubsCompilationDatabase(StubSet &stubFiles,
const fs::path &ccJsonStubDirPath) const {
printer::CCJsonPrinter::createDummyBuildDB(
CollectionUtils::transformTo<CollectionUtils::FileSet>(
stubFiles, [](const StubOperator &stub) { return stub.getSourceFilePath(); }),
ccJsonStubDirPath);
return CompilationUtils::getCompilationDatabase(ccJsonStubDirPath);
}
void Synchronizer::synchronizeWrappers(const CollectionUtils::FileSet &outdatedSourcePaths,
const types::TypesHandler &typesHandler) const {
auto sourceFilesNeedToRegenerateWrappers = outdatedSourcePaths;
for (fs::path const &sourceFilePath: testGen->getTargetSourceFiles()) {
if (!CollectionUtils::contains(sourceFilesNeedToRegenerateWrappers, sourceFilePath)) {
auto wrapperFilePath =
Paths::getWrapperFilePath(testGen->projectContext, sourceFilePath);
if (!fs::exists(wrapperFilePath)) {
sourceFilesNeedToRegenerateWrappers.insert(sourceFilePath);
}
}
}
ExecUtils::doWorkWithProgress(
sourceFilesNeedToRegenerateWrappers, testGen->progressWriter,
"Generating wrappers", [this, &typesHandler](fs::path const &sourceFilePath) {
SourceToHeaderRewriter sourceToHeaderRewriter(testGen->projectContext,
testGen->getProjectBuildDatabase()->compilationDatabase, nullptr,
testGen->serverBuildDir, typesHandler);
std::string wrapper = sourceToHeaderRewriter.generateWrapper(sourceFilePath);
printer::SourceWrapperPrinter(Paths::getSourceLanguage(sourceFilePath)).print(testGen->projectContext, sourceFilePath, wrapper);
});
}
StubSet Synchronizer::getStubsFiles() const {
return getStubSetFromSources(testGen->getProjectBuildDatabase()->compilationDatabase->getAllFiles());
}
void Synchronizer::prepareDirectory(const fs::path &stubDirectory) {
fs::create_directories(stubDirectory);
for (const auto &entry: fs::recursive_directory_iterator(stubDirectory)) {
if (entry.is_regular_file()) {
fs::path stubPath = entry.path();
if (!Paths::isHeaderFile(stubPath)) {
fs::path sourcePath =
Paths::stubPathToSourcePath(testGen->projectContext, stubPath);
if (!CollectionUtils::contains(testGen->getProjectSourceFiles(), sourcePath)) {
LOG_S(DEBUG) << "Found extra file in stub directory: " << stubPath
<< ". Removing it.";
fs::remove(stubPath);
}
}
}
}
}