Skip to content

Commit dfdfd3b

Browse files
author
Vladislav Kalugin
committed
refactoring after review 5
1 parent 7726291 commit dfdfd3b

14 files changed

+70
-82
lines changed

server/src/KleeGenerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ using namespace tests;
1919
static const std::string GENERATION_COMPILE_MAKEFILE = "GenerationCompileMakefile.mk";
2020
static const std::string GENERATION_KLEE_MAKEFILE = "GenerationKleeMakefile.mk";
2121

22-
KleeGenerator::KleeGenerator(BaseTestGen *_testGen, types::TypesHandler &typesHandler,
22+
KleeGenerator::KleeGenerator(BaseTestGen *testGen, types::TypesHandler &typesHandler,
2323
PathSubstitution filePathsSubstitution)
24-
: testGen(_testGen), typesHandler(typesHandler),
24+
: testGen(testGen), typesHandler(typesHandler),
2525
pathSubstitution(std::move(filePathsSubstitution)) {
2626
try {
2727
fs::create_directories(this->testGen->serverBuildDir);

server/src/KleeGenerator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class KleeGenerator {
3535
public:
3636
/**
3737
* @brief Also creates tmp directories for build files.
38-
* @param _testGen contains context for current request.
38+
* @param testGen contains request and build information .
3939
* @param typesHandler provides additional information about types.
4040
* @param filePathsSubstitution Mapping from source file path to modified file. Required for
4141
* line test generation requests.
4242
* @param buildDatabase Instance of BuildDatabase which handles link and compile commands
4343
* @throws fs::filesystem_error Thrown if it can't create tmp folder for some
4444
* reasons.
4545
*/
46-
KleeGenerator(BaseTestGen *_testGen, types::TypesHandler &typesHandler,
46+
KleeGenerator(BaseTestGen *testGen, types::TypesHandler &typesHandler,
4747
PathSubstitution filePathsSubstitution);
4848

4949
struct BuildFileInfo {

server/src/building/BaseCommand.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ namespace utbot {
2020
BaseCommand::BaseCommand(std::list<std::string> commandLine, fs::path directory, bool shouldChangeDirectory)
2121
: commandLine(std::move(commandLine)), directory(std::move(directory)), shouldChangeDirectory{shouldChangeDirectory} {
2222
initOptimizationLevel();
23-
initBuildTool();
2423
initOutput();
2524
}
25+
2626
BaseCommand::BaseCommand(std::vector<std::string> commandLine, fs::path directory, bool shouldChangeDirectory)
2727
: commandLine(commandLine.begin(), commandLine.end()), directory(std::move(directory)), shouldChangeDirectory{shouldChangeDirectory} {
2828
initOptimizationLevel();
29-
initBuildTool();
3029
initOutput();
3130
}
3231

3332
BaseCommand::BaseCommand(BaseCommand const &other)
3433
: directory(other.directory), commandLine(other.commandLine),
3534
environmentVariables(other.environmentVariables), shouldChangeDirectory(other.shouldChangeDirectory),
36-
buildTool(other.buildTool),
3735
output(other.output) {
3836
if (other.optimizationLevel.has_value()) {
3937
optimizationLevel =
@@ -47,7 +45,6 @@ namespace utbot {
4745
: directory(std::move(other.directory)), commandLine(std::move(other.commandLine)),
4846
environmentVariables(std::move(other.environmentVariables)),
4947
optimizationLevel(other.optimizationLevel),
50-
buildTool(other.buildTool),
5148
output(other.output),
5249
shouldChangeDirectory(other.shouldChangeDirectory) {
5350
}
@@ -59,11 +56,6 @@ namespace utbot {
5956
}
6057
}
6158

62-
void BaseCommand::initBuildTool() {
63-
auto it = commandLine.begin();
64-
buildTool = it;
65-
}
66-
6759
void BaseCommand::initOutput() {
6860
auto it = findOutput();
6961
if (it != commandLine.end()) {
@@ -152,11 +144,11 @@ namespace utbot {
152144
}
153145

154146
fs::path BaseCommand::getBuildTool() const {
155-
return *buildTool;
147+
return *commandLine.begin();
156148
}
157149

158150
void BaseCommand::setBuildTool(fs::path buildTool) {
159-
*(this->buildTool) = std::move(buildTool);
151+
*(commandLine.begin()) = std::move(buildTool);
160152
}
161153

162154
fs::path BaseCommand::getOutput() const {

server/src/building/BaseCommand.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
namespace utbot {
1616
class BaseCommand {
17+
private:
18+
void initOutput();
19+
1720
protected:
1821
bool shouldChangeDirectory = false;
1922
fs::path directory;
@@ -23,17 +26,12 @@ namespace utbot {
2326
using iterator = decltype(commandLine)::iterator;
2427
using const_iterator = decltype(commandLine)::const_iterator;
2528

26-
iterator buildTool;
2729
iterator output;
2830

2931
std::optional<iterator> optimizationLevel;
3032

3133
void initOptimizationLevel();
3234

33-
void initBuildTool();
34-
35-
void initOutput();
36-
3735
[[nodiscard]] iterator findOutput();
3836

3937
iterator findOptimizationLevelFlag();

server/src/building/CompileCommand.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
namespace utbot {
1414
CompileCommand::CompileCommand(CompileCommand const &other) : BaseCommand(other) {
15-
buildTool = commandLine.begin();
1615
sourcePath =
1716
std::next(commandLine.begin(),
1817
std::distance<const_iterator>(other.commandLine.begin(), other.sourcePath));
@@ -51,7 +50,6 @@ namespace utbot {
5150
fs::path directory,
5251
fs::path sourcePath)
5352
: BaseCommand(std::move(arguments), std::move(directory)) {
54-
buildTool = commandLine.begin();
5553
{
5654
auto it = std::find_if(commandLine.begin(), commandLine.end(), [&sourcePath](std::string const &arg) {
5755
return fs::path(arg).filename() == sourcePath.filename();
@@ -78,7 +76,6 @@ namespace utbot {
7876
std::swap(a.optimizationLevel, b.optimizationLevel);
7977

8078
std::swap(a.sourcePath, b.sourcePath);
81-
std::swap(a.buildTool, b.buildTool);
8279
std::swap(a.output, b.output);
8380
}
8481

server/src/building/LinkCommand.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111

1212
namespace utbot {
1313
LinkCommand::LinkCommand(LinkCommand const &other) : BaseCommand(other) {
14-
buildTool = commandLine.begin();
1514
output = std::next(commandLine.begin(), std::distance<const_iterator>(other.commandLine.begin(), other.output));
1615
}
1716

18-
LinkCommand::LinkCommand(LinkCommand &&other) noexcept : BaseCommand(std::move(other)) {
17+
LinkCommand::LinkCommand(LinkCommand &&other) noexcept: BaseCommand(std::move(other)) {
1918
}
2019

2120
LinkCommand &LinkCommand::operator=(const LinkCommand &other) {
@@ -38,22 +37,7 @@ namespace utbot {
3837

3938
LinkCommand::LinkCommand(std::list<std::string> arguments, fs::path directory, bool shouldChangeDirectory)
4039
: BaseCommand(std::move(arguments), std::move(directory), shouldChangeDirectory) {
41-
buildTool = commandLine.begin();
42-
{
43-
auto it = findOutput();
44-
if (it != commandLine.end()) {
45-
this->output = it;
46-
*this->output = Paths::getCCJsonFileFullPath(*it, this->directory);
47-
} else if (isArchiveCommand()) {
48-
auto it = std::find_if(commandLine.begin(), commandLine.end(), [](const std::string &argument) {
49-
return Paths::isStaticLibraryFile(argument);
50-
});
51-
this->output = std::next(addFlagsBeforeIterator({"-o"}, it));
52-
} else {
53-
auto path = this->directory / "a.out";
54-
this->output = std::next(addFlagsToBegin({"-o", path}));
55-
}
56-
}
40+
initOutput();
5741
}
5842

5943
LinkCommand::LinkCommand(std::vector<std::string> commandLine, fs::path directory, bool shouldChangeDirectory)
@@ -74,7 +58,6 @@ namespace utbot {
7458
std::swap(a.environmentVariables, b.environmentVariables);
7559
std::swap(a.optimizationLevel, b.optimizationLevel);
7660

77-
std::swap(a.buildTool, b.buildTool);
7861
std::swap(a.output, b.output);
7962
}
8063

@@ -85,4 +68,20 @@ namespace utbot {
8568
bool LinkCommand::isSharedLibraryCommand() const {
8669
return CollectionUtils::contains(commandLine, "-shared");
8770
}
71+
72+
void LinkCommand::initOutput() {
73+
auto it = findOutput();
74+
if (it != commandLine.end()) {
75+
this->output = it;
76+
*this->output = Paths::getCCJsonFileFullPath(*it, this->directory);
77+
} else if (isArchiveCommand()) {
78+
it = std::find_if(commandLine.begin(), commandLine.end(), [](const std::string &argument) {
79+
return Paths::isStaticLibraryFile(argument);
80+
});
81+
this->output = std::next(addFlagsBeforeIterator({"-o"}, it));
82+
} else {
83+
auto path = this->directory / "a.out";
84+
this->output = std::next(addFlagsToBegin({"-o", path}));
85+
}
86+
}
8887
}

server/src/building/LinkCommand.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace utbot {
1111
class LinkCommand : public BaseCommand {
12+
private:
13+
void initOutput();
1214

1315
public:
1416
LinkCommand() = default;

server/src/building/Linker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ Result<Linker::LinkResult> Linker::link(const CollectionUtils::MapFileTo<fs::pat
345345
FileSystemUtils::writeToFile(stubsMakefile, "");
346346

347347
printer::DefaultMakefilePrinter bitcodeLinkMakefilePrinter;
348-
printer::TestMakefilesPrinter testMakefilesPrinter{ testGen, &stubSources };
348+
printer::TestMakefilesPrinter testMakefilesPrinter(&testGen, &stubSources);
349349
bitcodeLinkMakefilePrinter.declareInclude(stubsMakefile);
350350
auto[targetBitcode, _] = addLinkTargetRecursively(target, bitcodeLinkMakefilePrinter, stubSources, bitcodeFiles,
351351
suffixForParentOfStubs, false, testedFilePath, true);

server/src/building/Linker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
#include "utils/CollectionUtils.h"
1313
#include "utils/MakefileUtils.h"
1414
#include "utils/Void.h"
15+
#include "stubs/StubGen.h"
1516

1617
#include <functional>
1718
#include <string>
1819
#include <unordered_map>
1920
#include <unordered_set>
2021
#include <vector>
21-
#include <stubs/StubGen.h>
2222

2323
class Linker {
2424
public:

server/src/printers/NativeMakefilePrinter.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ namespace printer {
116116
}
117117

118118
NativeMakefilePrinter::NativeMakefilePrinter(
119-
const BaseTestGen& testGen,
119+
const BaseTestGen* testGen,
120120
fs::path const &rootPath,
121121
fs::path primaryCompiler,
122122
CollectionUtils::FileSet const *stubSources,
@@ -135,7 +135,7 @@ namespace printer {
135135
CompilationUtils::getCoverageLinkFlags(primaryCxxCompilerName), " ")),
136136
sanitizerLinkFlags(SanitizerUtils::getSanitizeLinkFlags(primaryCxxCompilerName)),
137137

138-
buildDirectory(Paths::getUtbotBuildDir(testGen.projectContext)),
138+
buildDirectory(Paths::getUtbotBuildDir(testGen->projectContext)),
139139
dependencyDirectory(buildDirectory / "dependencies"),
140140
stubSources(stubSources) {
141141

@@ -164,13 +164,13 @@ namespace printer {
164164
}
165165

166166
fs::path NativeMakefilePrinter::getTemporaryDependencyFile(fs::path const &file) {
167-
fs::path relativePath = fs::relative(file, testGen.projectContext.projectPath);
167+
fs::path relativePath = fs::relative(file, testGen->projectContext.projectPath);
168168
return getRelativePath(dependencyDirectory) /
169169
Paths::addExtension(relativePath, ".Td");
170170
}
171171

172172
fs::path NativeMakefilePrinter::getDependencyFile(fs::path const &file) {
173-
fs::path relativePath = fs::relative(file, testGen.projectContext.projectPath);
173+
fs::path relativePath = fs::relative(file, testGen->projectContext.projectPath);
174174
return getRelativePath(dependencyDirectory) /
175175
Paths::addExtension(relativePath, ".d");
176176
}
@@ -270,25 +270,25 @@ namespace printer {
270270
BuildResult NativeMakefilePrinter::addObjectFile(const fs::path &objectFile,
271271
const std::string &suffixForParentOfStubs) {
272272

273-
auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(objectFile);
273+
auto compilationUnitInfo = testGen->getClientCompilationUnitInfo(objectFile);
274274
fs::path sourcePath = compilationUnitInfo->getSourcePath();
275275

276276
fs::path pathToCompile;
277277
fs::path recompiledFile;
278278
BuildResult::Type buildResultType;
279279
BuildResult buildResult;
280280
if (CollectionUtils::contains(*stubSources, sourcePath)) {
281-
pathToCompile = Paths::sourcePathToStubPath(testGen.projectContext, sourcePath);
282-
recompiledFile = Paths::getRecompiledFile(testGen.projectContext, pathToCompile);
281+
pathToCompile = Paths::sourcePathToStubPath(testGen->projectContext, sourcePath);
282+
recompiledFile = Paths::getRecompiledFile(testGen->projectContext, pathToCompile);
283283
buildResultType = BuildResult::Type::ALL_STUBS;
284284
} else {
285285
if (Paths::isCXXFile(sourcePath)) {
286286
pathToCompile = sourcePath;
287287
} else {
288-
pathToCompile = Paths::getWrapperFilePath(testGen.projectContext, sourcePath);
288+
pathToCompile = Paths::getWrapperFilePath(testGen->projectContext, sourcePath);
289289
}
290290
recompiledFile =
291-
Paths::getRecompiledFile(testGen.projectContext, compilationUnitInfo->getOutputFile());
291+
Paths::getRecompiledFile(testGen->projectContext, compilationUnitInfo->getOutputFile());
292292
buildResultType = BuildResult::Type::NO_STUBS;
293293
}
294294

@@ -299,7 +299,7 @@ namespace printer {
299299
}
300300

301301
void NativeMakefilePrinter::addTestTarget(const fs::path &sourcePath) {
302-
auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(sourcePath);
302+
auto compilationUnitInfo = testGen->getClientCompilationUnitInfo(sourcePath);
303303
auto testCompilationCommand = compilationUnitInfo->command;
304304
testCompilationCommand.setBuildTool(getRelativePathForLinker(primaryCxxCompiler));
305305
testCompilationCommand.setOptimizationLevel(OPTIMIZATION_FLAG);
@@ -314,10 +314,10 @@ namespace printer {
314314
testCompilationCommand.addFlagToBegin(FPIC_FLAG);
315315
testCompilationCommand.addFlagsToBegin(SANITIZER_NEEDED_FLAGS);
316316

317-
fs::path testSourcePath = Paths::sourcePathToTestPath(testGen.projectContext, sourcePath);
317+
fs::path testSourcePath = Paths::sourcePathToTestPath(testGen->projectContext, sourcePath);
318318
fs::path compilationDirectory = compilationUnitInfo->getDirectory();
319-
fs::path testObjectDir = Paths::getTestObjectDir(testGen.projectContext);
320-
fs::path testSourceRelativePath = fs::relative(testSourcePath, testGen.projectContext.testDirPath);
319+
fs::path testObjectDir = Paths::getTestObjectDir(testGen->projectContext);
320+
fs::path testSourceRelativePath = fs::relative(testSourcePath, testGen->projectContext.testDirPath);
321321
fs::path testObjectPathRelative = getRelativePath(
322322
testObjectDir / Paths::addExtension(testSourceRelativePath, ".o"));
323323
testCompilationCommand.setOutput(
@@ -332,7 +332,7 @@ namespace printer {
332332

333333
artifacts.push_back(testCompilationCommand.getOutput());
334334

335-
auto rootLinkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath);
335+
auto rootLinkUnitInfo = testGen->getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath);
336336
fs::path testExecutablePath = getTestExecutablePath(sourcePath);
337337

338338
std::vector<std::string> filesToLink{ "$(GTEST_MAIN)", "$(GTEST_ALL)", testCompilationCommand.getOutput(),
@@ -399,7 +399,7 @@ namespace printer {
399399
}
400400
fs::path NativeMakefilePrinter::getTestExecutablePath(const fs::path &sourcePath) const {
401401
return Paths::removeExtension(
402-
Paths::removeExtension(Paths::getRecompiledFile(testGen.projectContext, sourcePath)));
402+
Paths::removeExtension(Paths::getRecompiledFile(testGen->projectContext, sourcePath)));
403403
}
404404

405405
NativeMakefilePrinter::NativeMakefilePrinter(const NativeMakefilePrinter &baseMakefilePrinter,
@@ -426,7 +426,7 @@ namespace printer {
426426

427427
fs::path testExecutablePath = getTestExecutablePath(sourcePath);
428428

429-
auto rootLinkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath);
429+
auto rootLinkUnitInfo = testGen->getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath);
430430

431431
fs::path coverageInfoBinary = sharedOutput.value();
432432
if (!Paths::isLibraryFile(coverageInfoBinary)) {
@@ -468,7 +468,7 @@ namespace printer {
468468
return buildResults[unitFile] = buildResult;
469469
}
470470

471-
auto linkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(unitFile);
471+
auto linkUnitInfo = testGen->getTargetBuildDatabase()->getClientLinkUnitInfo(unitFile);
472472
BuildResult::Type unitType = BuildResult::Type::NONE;
473473
CollectionUtils::MapFileTo<fs::path> fileMapping;
474474
auto unitBuildResults = CollectionUtils::transformTo<std::vector<BuildResult>>(
@@ -489,7 +489,7 @@ namespace printer {
489489
bool isExecutable = !Paths::isLibraryFile(unitFile);
490490

491491
fs::path recompiledFile =
492-
Paths::getRecompiledFile(testGen.projectContext, linkUnitInfo->getOutput());
492+
Paths::getRecompiledFile(testGen->projectContext, linkUnitInfo->getOutput());
493493
if (isExecutable && !transformExeToLib) {
494494
recompiledFile = Paths::isObjectFile(recompiledFile) ?
495495
recompiledFile : Paths::addExtension(recompiledFile, ".o");
@@ -529,9 +529,9 @@ namespace printer {
529529
getLibraryAbsolutePath(argument, linkCommand.getDirectory());
530530
if (optionalLibraryAbsolutePath.has_value()) {
531531
const fs::path &absolutePath = optionalLibraryAbsolutePath.value();
532-
if (Paths::isSubPathOf(testGen.projectContext.buildDir(), absolutePath)) {
532+
if (Paths::isSubPathOf(testGen->projectContext.buildDir(), absolutePath)) {
533533
fs::path recompiledDir =
534-
Paths::getRecompiledFile(testGen.projectContext, absolutePath);
534+
Paths::getRecompiledFile(testGen->projectContext, absolutePath);
535535
std::string directoryFlag = getLibraryDirectoryFlag(recompiledDir);
536536
libraryDirectoriesFlags.push_back(directoryFlag);
537537
}
@@ -613,11 +613,11 @@ namespace printer {
613613
void NativeMakefilePrinter::addStubs(const CollectionUtils::FileSet &stubsSet) {
614614
auto stubObjectFiles = CollectionUtils::transformTo<CollectionUtils::FileSet>(
615615
Synchronizer::dropHeaders(stubsSet), [this](fs::path const &stub) {
616-
fs::path sourcePath = Paths::stubPathToSourcePath(testGen.projectContext, stub);
616+
fs::path sourcePath = Paths::stubPathToSourcePath(testGen->projectContext, stub);
617617
fs::path stubBuildFilePath =
618-
Paths::getStubBuildFilePath(testGen.projectContext, sourcePath);
619-
auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(sourcePath, true);
620-
fs::path target = Paths::getRecompiledFile(testGen.projectContext, stub);
618+
Paths::getStubBuildFilePath(testGen->projectContext, sourcePath);
619+
auto compilationUnitInfo = testGen->getClientCompilationUnitInfo(sourcePath, true);
620+
fs::path target = Paths::getRecompiledFile(testGen->projectContext, stub);
621621
addCompileTarget(stub, target, *compilationUnitInfo);
622622
return target;
623623
});

0 commit comments

Comments
 (0)