From b049d9b1362cbef3a2a0e95ae07d1f5bef826d1c Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Tue, 19 Jul 2022 17:38:18 +0300 Subject: [PATCH 01/27] rebase --- server/src/KleeGenerator.cpp | 4 +- server/src/Paths.cpp | 2 +- server/src/Server.cpp | 7 +- server/src/building/BaseCommand.cpp | 53 ++- server/src/building/BaseCommand.h | 19 +- server/src/building/BuildDatabase.cpp | 121 +++++-- server/src/building/BuildDatabase.h | 39 +- server/src/building/CompileCommand.cpp | 20 +- server/src/building/CompileCommand.h | 10 - server/src/building/LinkCommand.cpp | 45 +-- server/src/building/LinkCommand.h | 14 +- server/src/building/Linker.cpp | 18 +- server/src/building/RunCommand.cpp | 14 +- server/src/building/RunCommand.h | 7 +- server/src/printers/NativeMakefilePrinter.cpp | 12 +- server/src/testgens/BaseTestGen.cpp | 1 + server/src/testgens/ProjectTestGen.cpp | 18 +- server/src/testgens/ProjectTestGen.h | 4 +- server/src/testgens/SnippetTestGen.cpp | 19 +- server/src/utils/GenerationUtils.cpp | 7 +- server/src/utils/GenerationUtils.h | 4 + server/test/framework/KleeGen_Tests.cpp | 4 +- server/test/framework/Server_Tests.cpp | 4 +- server/test/framework/Targets_Test.cpp | 334 +++++++++++++++--- server/test/framework/TestUtils.cpp | 63 +++- server/test/framework/TestUtils.h | 32 +- server/test/suites/targets/CMakeLists.txt | 11 +- server/test/suites/targets/get_10.c | 3 + server/test/suites/targets/get_20x.c | 7 + server/test/suites/targets/get_any_val.h | 7 + server/test/suites/targets/get_val_main.c | 9 + server/test/suites/targets/get_val_main_2.c | 9 + server/test/suites/targets/shared.c | 5 + server/test/suites/targets/shared.h | 6 + 34 files changed, 651 insertions(+), 281 deletions(-) create mode 100644 server/test/suites/targets/get_10.c create mode 100644 server/test/suites/targets/get_20x.c create mode 100644 server/test/suites/targets/get_any_val.h create mode 100644 server/test/suites/targets/get_val_main.c create mode 100644 server/test/suites/targets/get_val_main_2.c create mode 100644 server/test/suites/targets/shared.c create mode 100644 server/test/suites/targets/shared.h diff --git a/server/src/KleeGenerator.cpp b/server/src/KleeGenerator.cpp index b1e358f69..92573c92f 100644 --- a/server/src/KleeGenerator.cpp +++ b/server/src/KleeGenerator.cpp @@ -254,8 +254,8 @@ std::vector KleeGenerator::buildKleeFiles(const tests::TestsMap &tests return; } kleePrinter.srcLanguage = Paths::getSourceLanguage(filename); - auto includeFlags = { StringUtils::stringFormat("-I%s", - Paths::getFlagsDir(projectContext)) }; + std::vector includeFlags = { + StringUtils::stringFormat("-I%s", Paths::getFlagsDir(projectContext))}; auto buildDirPath = buildDatabase->getClientCompilationUnitInfo(filename)->getDirectory(); diff --git a/server/src/Paths.cpp b/server/src/Paths.cpp index 1c824f463..481ded3ab 100644 --- a/server/src/Paths.cpp +++ b/server/src/Paths.cpp @@ -27,7 +27,7 @@ namespace Paths { CollectionUtils::FileSet filtered = CollectionUtils::filterOut(paths, [&dirPaths, &filter](const fs::path &path) { return !std::any_of(dirPaths.begin(), dirPaths.end(), [&](const fs::path &dirPath) { - return path.parent_path() == dirPath && fs::exists(path) && filter(path); + return isSubPathOf(dirPath, path) && fs::exists(path) && filter(path); }); }); return filtered; diff --git a/server/src/Server.cpp b/server/src/Server.cpp index af4ff23eb..7883c7e1e 100644 --- a/server/src/Server.cpp +++ b/server/src/Server.cpp @@ -579,7 +579,8 @@ Status Server::TestsGenServiceImpl::PrintModulesContent(ServerContext *context, utbot::ProjectContext projectContext{ *request }; fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); - std::shared_ptr buildDatabase = BuildDatabase::create(projectContext); + std::shared_ptr buildDatabase = BuildDatabase::create(projectContext, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); StubSourcesFinder(buildDatabase).printAllModules(); return Status::OK; } @@ -654,7 +655,7 @@ Status Server::TestsGenServiceImpl::GetProjectTargets(ServerContext *context, try { utbot::ProjectContext projectContext{ request->projectcontext() }; - auto buildDatabase = BuildDatabase::create(projectContext); + auto buildDatabase = BuildDatabase::create(projectContext, GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto targets = buildDatabase->getAllTargets(); ProjectTargetsWriter targetsWriter{ response }; targetsWriter.writeResponse(projectContext, targets); @@ -676,7 +677,7 @@ Status Server::TestsGenServiceImpl::GetFileTargets(ServerContext *context, try { utbot::ProjectContext projectContext{ request->projectcontext() }; - auto buildDatabase = BuildDatabase::create(projectContext); + auto buildDatabase = BuildDatabase::create(projectContext, GrpcUtils::UTBOT_AUTO_TARGET_PATH); fs::path path = request->path(); auto targets = buildDatabase->getTargetsForSourceFile(path); FileTargetsWriter targetsWriter{ response }; diff --git a/server/src/building/BaseCommand.cpp b/server/src/building/BaseCommand.cpp index 8cbd20c3b..a672b77bd 100644 --- a/server/src/building/BaseCommand.cpp +++ b/server/src/building/BaseCommand.cpp @@ -7,11 +7,11 @@ #include "tasks/ShellExecTask.h" #include "utils/CollectionUtils.h" #include "utils/StringUtils.h" +#include "utils/path/FileSystemPath.h" #include "loguru.h" #include -#include "utils/path/FileSystemPath.h" #include #include #include @@ -20,26 +20,36 @@ namespace utbot { BaseCommand::BaseCommand(std::list commandLine, fs::path directory, bool shouldChangeDirectory) : commandLine(std::move(commandLine)), directory(std::move(directory)), shouldChangeDirectory{shouldChangeDirectory} { initOptimizationLevel(); + initCompiler(); + initOutput(); } BaseCommand::BaseCommand(std::vector commandLine, fs::path directory, bool shouldChangeDirectory) : commandLine(commandLine.begin(), commandLine.end()), directory(std::move(directory)), shouldChangeDirectory{shouldChangeDirectory} { initOptimizationLevel(); + initCompiler(); + initOutput(); } BaseCommand::BaseCommand(BaseCommand const &other) - : directory(other.directory), commandLine(other.commandLine), - environmentVariables(other.environmentVariables), shouldChangeDirectory(other.shouldChangeDirectory) { + : directory(other.directory), commandLine(other.commandLine), + environmentVariables(other.environmentVariables), shouldChangeDirectory(other.shouldChangeDirectory), + compiler(other.compiler), + output(other.output) { if (other.optimizationLevel.has_value()) { optimizationLevel = - std::next(commandLine.begin(), - std::distance(other.commandLine.begin(), - other.optimizationLevel.value())); + std::next(commandLine.begin(), + std::distance(other.commandLine.begin(), + other.optimizationLevel.value())); } } + BaseCommand::BaseCommand(BaseCommand &&other) noexcept : directory(std::move(other.directory)), commandLine(std::move(other.commandLine)), environmentVariables(std::move(other.environmentVariables)), - optimizationLevel(other.optimizationLevel), shouldChangeDirectory(other.shouldChangeDirectory) { + optimizationLevel(other.optimizationLevel), + compiler(other.compiler), + output(other.output), + shouldChangeDirectory(other.shouldChangeDirectory) { } void BaseCommand::initOptimizationLevel() { @@ -48,6 +58,19 @@ namespace utbot { optimizationLevel = it; } } + + void BaseCommand::initCompiler() { + auto it = commandLine.begin(); + compiler = it; + } + + void BaseCommand::initOutput() { + auto it = findOutput(); + if (it != commandLine.end()) { + output = it; + } + } + BaseCommand::iterator BaseCommand::findOutput() { auto it = std::find(commandLine.begin(), commandLine.end(), "-o"); if (it != commandLine.end()) { @@ -127,4 +150,20 @@ namespace utbot { optimizationLevel = addFlagToBegin(flag); } } + + fs::path BaseCommand::getCompiler() const { + return *compiler; + } + + void BaseCommand::setCompiler(fs::path compiler) { + *(this->compiler) = std::move(compiler); + } + + fs::path BaseCommand::getOutput() const { + return *output; + } + + void BaseCommand::setOutput(fs::path output) { + *(this->output) = std::move(output); + } } diff --git a/server/src/building/BaseCommand.h b/server/src/building/BaseCommand.h index 8646bb2cc..8e6d29ada 100644 --- a/server/src/building/BaseCommand.h +++ b/server/src/building/BaseCommand.h @@ -23,15 +23,22 @@ namespace utbot { using iterator = decltype(commandLine)::iterator; using const_iterator = decltype(commandLine)::const_iterator; + iterator compiler; + iterator output; + std::optional optimizationLevel; void initOptimizationLevel(); + void initCompiler(); + + void initOutput(); + [[nodiscard]] iterator findOutput(); iterator findOptimizationLevelFlag(); - public: + BaseCommand() = default; BaseCommand(std::list commandLine, fs::path directory, bool shouldChangeDirectory = false); @@ -42,13 +49,17 @@ namespace utbot { BaseCommand(BaseCommand &&other) noexcept; + public: + [[nodiscard]] std::list &getCommandLine(); [[nodiscard]] const std::list &getCommandLine() const; [[nodiscard]] const fs::path &getDirectory() const; - [[nodiscard]] virtual fs::path getOutput() const = 0; + [[nodiscard]] fs::path getOutput() const; + + void setOutput(fs::path output); [[nodiscard]] virtual bool isArchiveCommand() const = 0; @@ -89,6 +100,10 @@ namespace utbot { size_t erase_if(std::function f); void setOptimizationLevel(const std::string &flag); + + [[nodiscard]] fs::path getCompiler() const; + + void setCompiler(fs::path compiler); }; } diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 14d1bb896..4ff2f4a9d 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -13,8 +13,9 @@ #include #include -#include #include +#include "utils/GrpcUtils.h" +#include "utils/GenerationUtils.h" static std::string tryConvertOptionToPath(const std::string &possibleFilePath, const fs::path &dirPath) { @@ -30,32 +31,37 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath, return fs::exists(fullFilePath) ? fullFilePath.string() : possibleFilePath; } -BuildDatabase::BuildDatabase(const fs::path& buildCommandsJsonPath, - fs::path serverBuildDir, - utbot::ProjectContext projectContext) - : serverBuildDir(std::move(serverBuildDir)), projectContext(std::move(projectContext)) { +BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, + fs::path _serverBuildDir, + utbot::ProjectContext _projectContext, + const std::string &target) : serverBuildDir(std::move(_serverBuildDir)), + projectContext(std::move(_projectContext)), + buildCommandsJsonPath(std::move(_buildCommandsJsonPath)) { linkCommandsJsonPath = fs::canonical(buildCommandsJsonPath / "link_commands.json"); compileCommandsJsonPath = fs::canonical(buildCommandsJsonPath / "compile_commands.json"); if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); } + auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); - createClangCompileCommandsJson(buildCommandsJsonPath, compileCommandsJson); + initObjects(compileCommandsJson); initInfo(linkCommandsJson); filterInstalledFiles(); addLocalSharedLibraries(); fillTargetInfoParents(); + updateTarget(target); } -std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext) { +std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext, + const std::string &target) { fs::path compileCommandsJsonPath = - CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( - projectContext.projectPath, projectContext.buildDirRelativePath); + CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( + projectContext.projectPath, projectContext.buildDirRelativePath); fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); std::shared_ptr buildDatabase = - std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); + std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, target); return buildDatabase; } @@ -78,10 +84,8 @@ fs::path BuildDatabase::createExplicitObjectFileCompilationCommand(const std::sh } } -void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommandsJsonPath, - const nlohmann::json &compileCommandsJson) { - CollectionUtils::MapFileTo>> fileCompileCommands; - for (auto const& compileCommand: compileCommandsJson) { +void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) { + for (const nlohmann::json &compileCommand: compileCommandsJson) { auto objectInfo = std::make_shared(); fs::path directory = compileCommand.at("directory").get(); @@ -107,7 +111,8 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommands fs::path kleeFile = Paths::addSuffix(kleeFilePathTemplate, "_klee"); objectInfo->kleeFilesInfo = std::make_shared(kleeFile); - if (CollectionUtils::containsKey(objectFileInfos, outputFile) || CollectionUtils::containsKey(targetInfos, outputFile)) { + if (CollectionUtils::containsKey(objectFileInfos, outputFile) || + CollectionUtils::containsKey(targetInfos, outputFile)) { /* * If the condition above is true, that means that the output file * is built from multiple sources. Hence, it is not an object file, @@ -129,9 +134,9 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommands //create targetInfo targetInfo = targetInfos[outputFile] = std::make_shared(); targetInfo->commands.emplace_back( - std::initializer_list{ targetObjectInfo->command.getCompiler(), - "-o", outputFile, tmpObjectFileName }, - directory); + std::initializer_list{targetObjectInfo->command.getCompiler(), + "-o", outputFile, tmpObjectFileName}, + directory); targetInfo->addFile(tmpObjectFileName); } //redirect new compilation command to temporary file @@ -143,22 +148,13 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommands } else { objectFileInfos[outputFile] = objectInfo; } + compileCommands_temp.emplace_back(compileCommand, objectInfo); const fs::path &sourcePath = objectInfo->getSourcePath(); - if (!CollectionUtils::containsKey(sourceFileInfos, sourcePath) || - conflictPriorityMore(objectInfo, fileCompileCommands[sourcePath].second)) { - fileCompileCommands[sourcePath] = { compileCommand, objectInfo }; - } sourceFileInfos[sourcePath].emplace_back(objectInfo); } for (auto &[sourceFile, objectInfos]: sourceFileInfos) { - std::sort(objectInfos.begin(), objectInfos.end(), conflictPriorityMore); - } - nlohmann::json compileCommandsSingleFilesJson; - for (const auto &compileCommand: fileCompileCommands) { - compileCommandsSingleFilesJson.push_back(compileCommand.second.first); + std::stable_sort(objectInfos.begin(), objectInfos.end(), conflictPriorityMore); } - fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath); - JsonUtils::writeJsonToFile(clangCompileCommandsJsonPath, compileCommandsSingleFilesJson); } void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { @@ -204,6 +200,56 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { } } +void BuildDatabase::createClangCompileCommandsJson(const fs::path &target, const fs::path &buildCommandsJsonPath) { + CollectionUtils::MapFileTo>> fileCompileCommands; + for (const auto &[compileCommand, objectInfo]: compileCommands_temp) { + const fs::path &sourcePath = objectInfo->getSourcePath(); + if ((target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || + targetInfos[target]->files.contains(objectInfo->getOutputFile())) && + (!CollectionUtils::containsKey(fileCompileCommands, sourcePath) || + conflictPriorityMore(objectInfo, fileCompileCommands[sourcePath].second))) { + fileCompileCommands[sourcePath] = {compileCommand, objectInfo}; + } + } + + nlohmann::json compileCommandsSingleFilesJson; + for (const auto &compileCommand: fileCompileCommands) { + compileCommandsSingleFilesJson.push_back(compileCommand.second.first); + } + + fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath); + JsonUtils::writeJsonToFile(clangCompileCommandsJsonPath, compileCommandsSingleFilesJson); +} + +void BuildDatabase::updateTarget(std::string target) { + auto new_target = GenerationUtils::findTarget(getAllTargets(), target); + + if (new_target.has_value()) { + target = new_target.value(); + } else if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { + return; + } else { + throw CompilationDatabaseException("Can't find target: " + target); + } + + createClangCompileCommandsJson(target, buildCommandsJsonPath); + + for (auto &[sourceFile, objectInfos]: sourceFileInfos) { + std::sort(objectInfos.begin(), objectInfos.end(), [&](const std::shared_ptr &left, + const std::shared_ptr &right) { + if (CollectionUtils::containsKey(targetInfos, target)) { + if (CollectionUtils::containsKey(targetInfos[target]->files, left->getOutputFile())) { + return true; + } + if (CollectionUtils::containsKey(targetInfos[target]->files, right->getOutputFile())) { + return false; + } + } + return false; + }); + } +} + void BuildDatabase::mergeLibraryOptions(std::vector &jsonArguments) const { for (auto it = jsonArguments.begin(); it != jsonArguments.end(); it++) { if (*it == DynamicLibraryUtils::libraryDirOption || *it == DynamicLibraryUtils::linkFlag) { @@ -468,7 +514,8 @@ BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const { throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string()); } return sourceFileInfos.at(filepath)[0]; - } if (Paths::isObjectFile(filepath)) { + } + if (Paths::isObjectFile(filepath)) { if (!CollectionUtils::contains(objectFileInfos, filepath)) { throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string()); } @@ -477,8 +524,7 @@ BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const { throw CompilationDatabaseException("File is not a compilation unit or an object file: " + filepath.string()); } -std::shared_ptr -BuildDatabase::getClientLinkUnitInfo(const fs::path &filepath) const { +std::shared_ptr BuildDatabase::getClientLinkUnitInfo(const fs::path &filepath) const { if (Paths::isSourceFile(filepath)) { auto compilationInfo = getClientCompilationUnitInfo(filepath); return targetInfos.at(compilationInfo->linkUnit); @@ -491,8 +537,8 @@ BuildDatabase::getClientLinkUnitInfo(const fs::path &filepath) const { } bool BuildDatabase::conflictPriorityMore( - const std::shared_ptr &left, - const std::shared_ptr &right) { + const std::shared_ptr &left, + const std::shared_ptr &right) { if (StringUtils::contains(left->getOutputFile().string(), "64")) { return true; } @@ -563,7 +609,7 @@ void BuildDatabase::ObjectFileInfo::setOutputFile(const fs::path &file) { command.setOutput(file); } -void BuildDatabase::ObjectFileInfo::addFile(fs::path file) { +void BuildDatabase::BaseFileInfo::addFile(fs::path file) { files.insert(std::move(file)); } @@ -672,13 +718,14 @@ std::shared_ptr BuildDatabase::getPriorityTarget() co auto rootTargets = getRootTargets(); auto it = std::max_element(rootTargets.begin(), rootTargets.end(), - [&](std::shared_ptr a, - std::shared_ptr b) { + [&](const std::shared_ptr& a, + const std::shared_ptr& b) { return numberOfSources(a->getOutput()) < numberOfSources(b->getOutput()); }); return *it; } + fs::path BuildDatabase::newDirForFile(const fs::path &file) const { fs::path base = Paths::longestCommonPrefixPath(this->projectContext.buildDir(), this->projectContext.projectPath); diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 3015284c6..76fa47444 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -38,13 +38,17 @@ class BuildDatabase { struct BaseFileInfo { BaseFileInfo() = default; + virtual ~BaseFileInfo() = default; + // Object files and libraries that current command depends on + CollectionUtils::OrderedFileSet files; + // Libraries that current command depends on, but those are already installed and not built // within project CollectionUtils::OrderedFileSet installedFiles; - virtual void addFile(fs::path file) = 0; + void addFile(fs::path file); }; /* @@ -55,9 +59,6 @@ class BuildDatabase { // Compilation command utbot::CompileCommand command; - // Object files and libraries that current command depends on - CollectionUtils::OrderedFileSet files; - // Example of executable or a library which contains current objectFile fs::path linkUnit; @@ -74,8 +75,6 @@ class BuildDatabase { [[nodiscard]] bool is32bits() const; void setOutputFile(const fs::path &file); - - void addFile(fs::path file) override; }; /* @@ -88,24 +87,23 @@ class BuildDatabase { // Linkage command std::vector commands; - // Source files, object files and libraries that current command depends on - CollectionUtils::OrderedFileSet files; - // Units which contains current library std::vector parentLinkUnits; - void addFile(fs::path file) override; - // Executable or a library, the result of a command [[nodiscard]] fs::path getOutput() const; }; public: - BuildDatabase(const fs::path &buildCommandsJsonPath, - fs::path serverBuildDir, - utbot::ProjectContext projectContext); + BuildDatabase(fs::path _buildCommandsJsonPath, + fs::path _serverBuildDir, + utbot::ProjectContext _projectContext, + const std::string &target); + + static std::shared_ptr create(const utbot::ProjectContext &projectContext, + const std::string &target); - static std::shared_ptr create(const utbot::ProjectContext &projectContext); + void updateTarget(std::string target); const fs::path &getCompileCommandsJson(); const fs::path &getLinkCommandsJson(); @@ -204,6 +202,7 @@ class BuildDatabase { private: fs::path serverBuildDir; utbot::ProjectContext projectContext; + fs::path buildCommandsJsonPath; fs::path linkCommandsJsonPath; fs::path compileCommandsJsonPath; CollectionUtils::MapFileTo>> sourceFileInfos; @@ -214,6 +213,9 @@ class BuildDatabase { std::unordered_map, CollectionUtils::FileSet> linkUnitToStubFiles; + std::vector>> compileCommands_temp; + std::vector>> linkCommands_temp; + static bool conflictPriorityMore(const std::shared_ptr &left, const std::shared_ptr &right); @@ -221,13 +223,12 @@ class BuildDatabase { void addLocalSharedLibraries(); void fillTargetInfoParents(); static fs::path getCorrespondingBitcodeFile(const fs::path &filepath); - void createClangCompileCommandsJson(const fs::path &buildCommandsJsonPath, - const nlohmann::json &compileCommandsJson); + void initObjects(const nlohmann::json &compileCommandsJson); void initInfo(const nlohmann::json &linkCommandsJson); + void createClangCompileCommandsJson(const fs::path &target, const fs::path &buildCommandsJsonPath); void mergeLibraryOptions(std::vector &jsonArguments) const; fs::path newDirForFile(fs::path const& file) const; - fs::path - createExplicitObjectFileCompilationCommand(const std::shared_ptr &objectInfo); + fs::path createExplicitObjectFileCompilationCommand(const std::shared_ptr &objectInfo); using sharedLibrariesMap = std::unordered_map>; diff --git a/server/src/building/CompileCommand.cpp b/server/src/building/CompileCommand.cpp index be7d6fdb2..2adffcc85 100644 --- a/server/src/building/CompileCommand.cpp +++ b/server/src/building/CompileCommand.cpp @@ -21,9 +21,7 @@ namespace utbot { } CompileCommand::CompileCommand(CompileCommand &&other) noexcept : BaseCommand(std::move(other)), - sourcePath(other.sourcePath), - compiler(other.compiler), - output(other.output) { + sourcePath(other.sourcePath) { } CompileCommand &CompileCommand::operator=(const CompileCommand &other) { @@ -92,26 +90,10 @@ namespace utbot { *(this->sourcePath) = std::move(sourcePath); } - fs::path CompileCommand::getCompiler() const { - return *compiler; - } - - void CompileCommand::setCompiler(fs::path compiler) { - *(this->compiler) = std::move(compiler); - } - - fs::path CompileCommand::getOutput() const { - return *output; - } - bool CompileCommand::isArchiveCommand() const { return false; } - void CompileCommand::setOutput(fs::path output) { - *(this->output) = std::move(output); - } - void CompileCommand::removeCompilerFlagsAndOptions( const std::unordered_set &switchesToRemove) { size_t erased = diff --git a/server/src/building/CompileCommand.h b/server/src/building/CompileCommand.h index 193797c9c..85e4b7bea 100644 --- a/server/src/building/CompileCommand.h +++ b/server/src/building/CompileCommand.h @@ -15,8 +15,6 @@ namespace utbot { class CompileCommand : public BaseCommand { private: iterator sourcePath; - iterator compiler; - iterator output; public: CompileCommand() = default; @@ -39,16 +37,8 @@ namespace utbot { void setSourcePath(fs::path sourcePath); - [[nodiscard]] fs::path getCompiler() const; - - void setCompiler(fs::path compiler); - - [[nodiscard]] fs::path getOutput() const override; - [[nodiscard]] bool isArchiveCommand() const override; - void setOutput(fs::path output); - void removeCompilerFlagsAndOptions(const std::unordered_set &switchesToRemove); void removeIncludeFlags(); diff --git a/server/src/building/LinkCommand.cpp b/server/src/building/LinkCommand.cpp index 1fb1894d3..172961c15 100644 --- a/server/src/building/LinkCommand.cpp +++ b/server/src/building/LinkCommand.cpp @@ -11,12 +11,11 @@ namespace utbot { LinkCommand::LinkCommand(LinkCommand const &other) : BaseCommand(other) { - linker = commandLine.begin(); + compiler = commandLine.begin(); output = std::next(commandLine.begin(), std::distance(other.commandLine.begin(), other.output)); } - LinkCommand::LinkCommand(LinkCommand &&other) noexcept - : BaseCommand(std::move(other)), linker(other.linker), output(other.output) { + LinkCommand::LinkCommand(LinkCommand &&other) noexcept : BaseCommand(std::move(other)) { } LinkCommand &LinkCommand::operator=(const LinkCommand &other) { @@ -38,33 +37,34 @@ namespace utbot { } LinkCommand::LinkCommand(std::list arguments, fs::path directory, bool shouldChangeDirectory) - : BaseCommand(std::move(arguments), std::move(directory), shouldChangeDirectory) { - linker = commandLine.begin(); + : BaseCommand(std::move(arguments), std::move(directory), shouldChangeDirectory) { + compiler = commandLine.begin(); { auto it = findOutput(); if (it != commandLine.end()) { this->output = it; *this->output = Paths::getCCJsonFileFullPath(*it, this->directory); } else if (isArchiveCommand()) { - auto it = std::find_if(commandLine.begin(), commandLine.end(), [] (const std::string& argument) { - return Paths::isStaticLibraryFile(argument); + auto it = std::find_if(commandLine.begin(), commandLine.end(), [](const std::string &argument) { + return Paths::isStaticLibraryFile(argument); }); this->output = std::next(addFlagsBeforeIterator({"-o"}, it)); } else { auto path = this->directory / "a.out"; - this->output = std::next(addFlagsToBegin({ "-o", path })); + this->output = std::next(addFlagsToBegin({"-o", path})); } } } LinkCommand::LinkCommand(std::vector commandLine, fs::path directory, bool shouldChangeDirectory) - : LinkCommand(std::list{ commandLine.begin(), commandLine.end() }, - std::move(directory), shouldChangeDirectory) { + : LinkCommand(std::list{commandLine.begin(), commandLine.end()}, + std::move(directory), shouldChangeDirectory) { } - LinkCommand::LinkCommand(std::initializer_list commandLine, fs::path directory, bool shouldChangeDirectory) - : LinkCommand(std::list{ commandLine.begin(), commandLine.end() }, - std::move(directory), shouldChangeDirectory) { + LinkCommand::LinkCommand(std::initializer_list commandLine, fs::path directory, + bool shouldChangeDirectory) + : LinkCommand(std::list{commandLine.begin(), commandLine.end()}, + std::move(directory), shouldChangeDirectory) { } @@ -74,27 +74,12 @@ namespace utbot { std::swap(a.environmentVariables, b.environmentVariables); std::swap(a.optimizationLevel, b.optimizationLevel); - std::swap(a.linker, b.linker); + std::swap(a.compiler, b.compiler); std::swap(a.output, b.output); } - fs::path LinkCommand::getLinker() const { - return *linker; - } - - void LinkCommand::setLinker(fs::path linker) { - *(this->linker) = std::move(linker); - } - - fs::path LinkCommand::getOutput() const { - return *output; - } - - void LinkCommand::setOutput(fs::path output) { - *(this->output) = std::move(output); - } bool LinkCommand::isArchiveCommand() const { - return StringUtils::contains(getLinker().filename().c_str(), "ar"); + return StringUtils::contains(getCompiler().filename().c_str(), "ar"); } bool LinkCommand::isSharedLibraryCommand() const { diff --git a/server/src/building/LinkCommand.h b/server/src/building/LinkCommand.h index 7b444022a..72be77445 100644 --- a/server/src/building/LinkCommand.h +++ b/server/src/building/LinkCommand.h @@ -9,9 +9,6 @@ namespace utbot { class LinkCommand : public BaseCommand { - private: - iterator linker; - iterator output; public: LinkCommand() = default; @@ -28,18 +25,11 @@ namespace utbot { LinkCommand(std::vector commandLine, fs::path directory, bool shouldChangeDirectory = false); - LinkCommand(std::initializer_list commandLine, fs::path directory, bool shouldChangeDirectory = false); + LinkCommand(std::initializer_list commandLine, fs::path directory, + bool shouldChangeDirectory = false); friend void swap(LinkCommand &a, LinkCommand &b) noexcept; - [[nodiscard]] fs::path getLinker() const; - - void setLinker(fs::path linker); - - [[nodiscard]] fs::path getOutput() const override; - - void setOutput(fs::path output); - [[nodiscard]] bool isArchiveCommand() const override; [[nodiscard]] bool isSharedLibraryCommand() const; diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 15041d391..a30072b91 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -130,6 +130,8 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { Result Linker::linkWholeTarget(const fs::path &target) { auto requestTarget = testGen.getTargetPath(); + LOG_IF_S(ERROR, target != GrpcUtils::UTBOT_AUTO_TARGET_PATH && requestTarget != target) + << "Try link target that not specified by user"; testGen.setTargetPath(target); auto targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); @@ -148,15 +150,13 @@ Result Linker::linkWholeTarget(const fs::path &target) { insideFolder = false; } } - if (testGen.buildDatabase->isFirstObjectFileForSource(objectFile) && - !CollectionUtils::contains(testedFiles, objectInfo->getSourcePath()) && - insideFolder) { - filesToLink.emplace(objectFile, objectInfo->kleeFilesInfo->getKleeBitcodeFile()); + if (!CollectionUtils::contains(testedFiles, objectInfo->getSourcePath()) && insideFolder) { + fs::path bitcodeFile = objectInfo->kleeFilesInfo->getKleeBitcodeFile(); + filesToLink.emplace(objectFile, std::move(bitcodeFile)); } else { + fs::path bitcodeFile = testGen.buildDatabase->getBitcodeForSource(objectInfo->getSourcePath()); siblingObjectsToBuild.insert(objectInfo->getOutputFile()); - fs::path bitcodeFile = - testGen.buildDatabase->getBitcodeForSource(objectInfo->getSourcePath()); - filesToLink.emplace(objectFile, bitcodeFile); + filesToLink.emplace(objectFile, std::move(bitcodeFile)); } } @@ -512,7 +512,7 @@ std::string getArchiveArgument(std::string const &argument, if (StringUtils::startsWith(argument, "-")) { return ""; } - hasArchiveOption |= !argument.empty() && argument != linkCommand.getLinker(); + hasArchiveOption |= !argument.empty() && argument != linkCommand.getCompiler(); return argument; } @@ -673,7 +673,7 @@ Linker::getLinkActionsForExecutable(fs::path const &workingDir, BuildDatabase::TargetInfo const &linkUnitInfo, fs::path const &output, bool shouldChangeDirectory) { - + using namespace DynamicLibraryUtils; auto commands = CollectionUtils::transform( diff --git a/server/src/building/RunCommand.cpp b/server/src/building/RunCommand.cpp index 6f7920f89..421f59bdd 100644 --- a/server/src/building/RunCommand.cpp +++ b/server/src/building/RunCommand.cpp @@ -6,22 +6,22 @@ namespace utbot { RunCommand::RunCommand(std::vector commandLine, fs::path directory, bool shouldChangeDirectory) - : BaseCommand(std::move(commandLine), std::move(directory), shouldChangeDirectory) { - } - fs::path RunCommand::getOutput() const { - throw UnImplementedException("RunTestsCommand doesn't have output by default"); + : BaseCommand(std::move(commandLine), std::move(directory), shouldChangeDirectory) { } + std::string RunCommand::toStringWithChangingDirectoryToNew(const fs::path &targetDirectory) const { std::string baseCommand = toString(); return StringUtils::stringFormat(R"(cd "%s" && %s)", targetDirectory, baseCommand); } - RunCommand RunCommand::forceRemoveFile(const fs::path &path, fs::path const &workingDir, bool shouldChangeDirectory) { - return { { "rm", "-f", path }, workingDir, shouldChangeDirectory }; + + RunCommand + RunCommand::forceRemoveFile(const fs::path &path, fs::path const &workingDir, bool shouldChangeDirectory) { + return {{"rm", "-f", path}, workingDir, shouldChangeDirectory}; } RunCommand RunCommand::copyFile(const fs::path &from, const fs::path &to, fs::path const &workingDir) { - return { { "cp", from, to }, workingDir }; + return {{"cp", from, to}, workingDir}; } bool RunCommand::isArchiveCommand() const { diff --git a/server/src/building/RunCommand.h b/server/src/building/RunCommand.h index e76d63122..c651ac12d 100644 --- a/server/src/building/RunCommand.h +++ b/server/src/building/RunCommand.h @@ -10,14 +10,13 @@ namespace utbot { class RunCommand : public BaseCommand { public: RunCommand(std::vector commandLine, fs::path directory, bool shouldChangeDirectory = false); - fs::path getOutput() const override; std::string toStringWithChangingDirectoryToNew(const fs::path &targetDirectory) const override; - static RunCommand forceRemoveFile(fs::path const &path, fs::path const &workingDir, bool shouldChangeDirectory = false); - static RunCommand - copyFile(fs::path const &from, const fs::path &to, fs::path const &workingDir); + forceRemoveFile(fs::path const &path, fs::path const &workingDir, bool shouldChangeDirectory = false); + + static RunCommand copyFile(fs::path const &from, const fs::path &to, fs::path const &workingDir); bool isArchiveCommand() const override; }; diff --git a/server/src/printers/NativeMakefilePrinter.cpp b/server/src/printers/NativeMakefilePrinter.cpp index 1b6769fbe..4a0e026e8 100644 --- a/server/src/printers/NativeMakefilePrinter.cpp +++ b/server/src/printers/NativeMakefilePrinter.cpp @@ -353,7 +353,7 @@ namespace printer { { dynamicLinkCommand.toStringWithChangingDirectory() }); } else { utbot::LinkCommand dynamicLinkCommand = rootLinkUnitInfo->commands.front(); - dynamicLinkCommand.setLinker(cxxLinker); + dynamicLinkCommand.setCompiler(cxxLinker); dynamicLinkCommand.setOutput(testExecutablePath); dynamicLinkCommand.erase_if([&](std::string const &argument) { return CollectionUtils::contains(rootLinkUnitInfo->files, argument) || @@ -386,7 +386,7 @@ namespace printer { sharedOutput.value().parent_path()))); dynamicLinkCommand.addFlagToBegin("$(LDFLAGS)"); - dynamicLinkCommand.setLinker(getRelativePathForLinker(cxxLinker)); + dynamicLinkCommand.setCompiler(getRelativePathForLinker(cxxLinker)); dynamicLinkCommand.setOutput( getRelativePath(testExecutablePath)); @@ -514,13 +514,13 @@ namespace printer { } if (!linkCommand.isArchiveCommand()) { if (isExecutable && !transformExeToLib) { - linkCommand.setLinker(Paths::getLd()); + linkCommand.setCompiler(Paths::getLd()); for (std::string &argument : linkCommand.getCommandLine()) { transformCompilerFlagsToLinkerFlags(argument); } } else { - linkCommand.setLinker(CompilationUtils::getBundledCompilerPath( - CompilationUtils::getCompilerName(linkCommand.getLinker()))); + linkCommand.setCompiler(CompilationUtils::getBundledCompilerPath( + CompilationUtils::getCompilerName(linkCommand.getCompiler()))); } std::vector libraryDirectoriesFlags; for (std::string &argument : linkCommand.getCommandLine()) { @@ -555,7 +555,7 @@ namespace printer { } } - linkCommand.setLinker(getRelativePathForLinker(linkCommand.getLinker())); + linkCommand.setCompiler(getRelativePathForLinker(linkCommand.getCompiler())); for (std::string &argument : linkCommand.getCommandLine()) { tryChangeToRelativePath(argument); diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index 06c442700..34ac82c8d 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -54,6 +54,7 @@ void BaseTestGen::setTargetPath(fs::path _targetPath) { } void BaseTestGen::updateTargetSources() { + buildDatabase->updateTarget(getTargetPath()); targetSources = CollectionUtils::transformTo( buildDatabase->getArchiveObjectFiles(getTargetPath()), [this](fs::path const &objectPath) { return buildDatabase->getClientCompilationUnitInfo(objectPath)->getSourcePath(); diff --git a/server/src/testgens/ProjectTestGen.cpp b/server/src/testgens/ProjectTestGen.cpp index 05feec01c..a316e4f5b 100644 --- a/server/src/testgens/ProjectTestGen.cpp +++ b/server/src/testgens/ProjectTestGen.cpp @@ -8,18 +8,18 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, ProgressWriter *progressWriter, bool testMode, - bool autoDetect) - : BaseTestGen(request.projectcontext(), - request.settingscontext(), - progressWriter, - testMode), request(&request) { + bool autoSrcPaths) + : BaseTestGen(request.projectcontext(), + request.settingscontext(), + progressWriter, + testMode), request(&request) { fs::create_directories(projectContext.testDirPath); compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( - projectContext.projectPath, projectContext.buildDirRelativePath); - buildDatabase = - std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); + projectContext.projectPath, projectContext.buildDirRelativePath); + buildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, + request.targetpath()); compilationDatabase = CompilationUtils::getCompilationDatabase(compileCommandsJsonPath); - if (autoDetect) { + if (autoSrcPaths) { autoDetectSourcePathsIfNotEmpty(); } else { sourcePaths = compilationDatabase->getAllFiles(); diff --git a/server/src/testgens/ProjectTestGen.h b/server/src/testgens/ProjectTestGen.h index d96c754b5..0647c091e 100644 --- a/server/src/testgens/ProjectTestGen.h +++ b/server/src/testgens/ProjectTestGen.h @@ -11,14 +11,14 @@ class ProjectTestGen : public BaseTestGen { ProjectTestGen(const testsgen::ProjectRequest &request, ProgressWriter *progressWriter, bool testMode, - bool autoDetect = true); + bool autoSrcPaths = true); ~ProjectTestGen() override = default; std::string toString() override; const testsgen::ProjectRequest *getRequest() const; - + void setTargetForSource(fs::path const &sourcePath) override; private: diff --git a/server/src/testgens/SnippetTestGen.cpp b/server/src/testgens/SnippetTestGen.cpp index f1230c319..09a7faedc 100644 --- a/server/src/testgens/SnippetTestGen.cpp +++ b/server/src/testgens/SnippetTestGen.cpp @@ -3,21 +3,24 @@ #include "printers/CCJsonPrinter.h" #include "utils/CompilationUtils.h" +const std::string SNIPPET_TARGET = "executable"; + SnippetTestGen::SnippetTestGen(const testsgen::SnippetRequest &request, ProgressWriter *progressWriter, bool testMode) - : BaseTestGen(request.projectcontext(), - request.settingscontext(), - progressWriter, - testMode) { + : BaseTestGen(request.projectcontext(), + request.settingscontext(), + progressWriter, + testMode) { filePath = fs::weakly_canonical(request.filepath()); - sourcePaths = { filePath }; + sourcePaths = {filePath}; testingMethodsSourcePaths = sourcePaths; printer::CCJsonPrinter::createDummyBuildDB(sourcePaths, serverBuildDir); compileCommandsJsonPath = serverBuildDir; - utbot::ProjectContext projectContext{ request, serverBuildDir }; + utbot::ProjectContext projectContext{request, serverBuildDir}; buildDatabase = - std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); + std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, + serverBuildDir / SNIPPET_TARGET); compilationDatabase = CompilationUtils::getCompilationDatabase(serverBuildDir); setTargetForSource(filePath); setInitializedTestsMap(); @@ -28,6 +31,6 @@ std::string SnippetTestGen::toString() { } void SnippetTestGen::setTargetForSource(const fs::path &sourcePath) { - fs::path root = serverBuildDir / "executable"; + fs::path root = serverBuildDir / SNIPPET_TARGET; setTargetPath(root); } diff --git a/server/src/utils/GenerationUtils.cpp b/server/src/utils/GenerationUtils.cpp index 436cadadc..4c8a8f964 100644 --- a/server/src/utils/GenerationUtils.cpp +++ b/server/src/utils/GenerationUtils.cpp @@ -50,11 +50,16 @@ void GenerationUtils::generateCoverageAndResultsAndWriteStatus( std::optional GenerationUtils::findTarget(const BaseTestGen &baseTestGen, const std::string &name) { + return findTarget(baseTestGen.buildDatabase->getAllTargets(), name); +} + +std::optional +GenerationUtils::findTarget(const std::vector> &allTargets, + const std::string &name) { if (name.empty()) { LOG_S(INFO) << "Target was not chosen. Using UTBot: Auto target instead."; return GrpcUtils::UTBOT_AUTO_TARGET_PATH; } - auto allTargets = baseTestGen.buildDatabase->getAllTargets(); auto candidates = CollectionUtils::filterOut(allTargets, [&name](auto const &target) { fs::path output = target->getOutput(); return !(output == name || output.stem() == name || output.stem() == "lib" + name); diff --git a/server/src/utils/GenerationUtils.h b/server/src/utils/GenerationUtils.h index c86ee750f..07223fa33 100644 --- a/server/src/utils/GenerationUtils.h +++ b/server/src/utils/GenerationUtils.h @@ -24,6 +24,10 @@ using grpc::Status; using grpc::StatusCode; namespace GenerationUtils { + + std::optional + findTarget(const std::vector> &allTargets, const std::string &name); + std::optional findTarget(const BaseTestGen &baseTestGen, const std::string &name); template diff --git a/server/test/framework/KleeGen_Tests.cpp b/server/test/framework/KleeGen_Tests.cpp index df91a3ec2..16e98506b 100644 --- a/server/test/framework/KleeGen_Tests.cpp +++ b/server/test/framework/KleeGen_Tests.cpp @@ -54,8 +54,8 @@ namespace { fs::path testsDirPath = tmpDirPath / "test"; utbot::ProjectContext projectContext{ suite.name, "", testsDirPath, buildDirRelativePath }; - auto buildDatabase = - std::make_shared(suite.buildPath, suite.buildPath, projectContext); + auto buildDatabase = std::make_shared(suite.buildPath, suite.buildPath, projectContext, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); utbot::SettingsContext settingsContext{ true, true, 15, 0, true, false }; KleeGenerator generator(std::move(projectContext), std::move(settingsContext), tmpDirPath, diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index 00b5f7ac7..40b653c2d 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -64,8 +64,8 @@ namespace { void generateFiles(const fs::path &sourceFile, const utbot::ProjectContext &projectContext) { fs::path serverBuildDir = buildPath / "temp"; - auto buildDatabase = - std::make_shared(buildPath, serverBuildDir, projectContext); + auto buildDatabase = std::make_shared(buildPath, serverBuildDir, projectContext, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); fs::path compilerPath = CompilationUtils::getBundledCompilerPath(compilerName); CollectionUtils::FileSet stubsSources; fs::path root = buildDatabase->getRootForSource(sourceFile); diff --git a/server/test/framework/Targets_Test.cpp b/server/test/framework/Targets_Test.cpp index da41615c6..357b3b38e 100644 --- a/server/test/framework/Targets_Test.cpp +++ b/server/test/framework/Targets_Test.cpp @@ -11,14 +11,19 @@ class TargetsTest : public BaseTest { } fs::path parse_c = getTestFilePath("parse.c"); + fs::path get_10_c = getTestFilePath("get_10.c"); + fs::path get_20_c = getTestFilePath("get_20.c"); + fs::path shared_c = getTestFilePath("shared.c"); + fs::path get_val_main_c = getTestFilePath("get_val_main.c"); + fs::path get_val_main_2_c = getTestFilePath("get_val_main_2.c"); fs::path getTargetPathByName(BuildDatabase const &buildDatabase, fs::path const &fileName) { auto rootTargets = buildDatabase.getRootTargets(); auto it = - std::find_if(rootTargets.begin(), rootTargets.end(), - [&fileName](std::shared_ptr linkUnitInfo) { - return linkUnitInfo->getOutput().filename() == fileName; - }); + std::find_if(rootTargets.begin(), rootTargets.end(), + [&fileName](std::shared_ptr linkUnitInfo) { + return linkUnitInfo->getOutput().filename() == fileName; + }); assert(it != rootTargets.end()); return it->get()->getOutput(); } @@ -26,75 +31,290 @@ class TargetsTest : public BaseTest { using namespace testUtils; -TEST_F(TargetsTest, Valid_Target_Test_1) { - { - auto projectRequest = +TEST_F(TargetsTest, Valid_Target_Test_ls) { + auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); - auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); - auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - fs::path ls = getTargetPathByName(*testGen.buildDatabase, "ls"); - testGen.setTargetPath(ls); + auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); + auto testGen = FileTestGen(*request, writer.get(), TESTMODE); + fs::path ls = getTargetPathByName(*testGen.buildDatabase, "ls"); + testGen.setTargetPath(ls); - Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); - ASSERT_TRUE(status.ok()) << status.error_message(); + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + ASSERT_TRUE(status.ok()) << status.error_message(); - checkTestCasePredicates( + checkMinNumberOfTestsInFile(testGen, parse_c, 1); + checkTestCasePredicates( testGen.tests.at(parse_c).methods.begin().value().testCases, - std::vector({ [](const tests::Tests::MethodTestCase &testCase) { + std::vector({[](const tests::Tests::MethodTestCase &testCase) { std::string ret = testCase.returnValue.view->getEntryValue(nullptr); return ret == "\'l\'"; - } }), + }}), "parse"); - } - { - auto projectRequest = +} + +TEST_F(TargetsTest, Valid_Target_Test_cat) { + auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); - auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); - auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - fs::path cat = getTargetPathByName(*testGen.buildDatabase, "cat"); - testGen.setTargetPath(cat); + auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); + auto testGen = FileTestGen(*request, writer.get(), TESTMODE); + fs::path cat = getTargetPathByName(*testGen.buildDatabase, "cat"); + testGen.setTargetPath(cat); - Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); - ASSERT_TRUE(status.ok()) << status.error_message(); + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + ASSERT_TRUE(status.ok()) << status.error_message(); - checkTestCasePredicates( + checkMinNumberOfTestsInFile(testGen, parse_c, 1); + checkTestCasePredicates( testGen.tests.at(parse_c).methods.begin().value().testCases, - std::vector({ [](const tests::Tests::MethodTestCase &testCase) { + std::vector({[](const tests::Tests::MethodTestCase &testCase) { std::string ret = testCase.returnValue.view->getEntryValue(nullptr); return ret == "\'c\'"; - } }), + }}), "parse"); - } - { - auto projectRequest = +} + +TEST_F(TargetsTest, Valid_Target_Test_dummy) { + auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); - auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); - auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - fs::path dummy = getTargetPathByName(*testGen.buildDatabase, "dummy"); - testGen.setTargetPath(dummy); + auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); + auto testGen = FileTestGen(*request, writer.get(), TESTMODE); + fs::path dummy = getTargetPathByName(*testGen.buildDatabase, "dummy"); + testGen.setTargetPath(dummy); - Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); - ASSERT_FALSE(status.ok()); + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + ASSERT_FALSE(status.ok()); - int numberOfTests = testUtils::getNumberOfTests(testGen.tests); - EXPECT_EQ(0, numberOfTests); - } - { - auto projectRequest = + int numberOfTests = testUtils::getNumberOfTests(testGen.tests); + EXPECT_EQ(0, numberOfTests); +} + +TEST_F(TargetsTest, Valid_Target_Test_parse) { + auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); - auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); - auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - fs::path autoTarget = GrpcUtils::UTBOT_AUTO_TARGET_PATH; - testGen.setTargetPath(autoTarget); - - Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); - ASSERT_TRUE(status.ok()); - checkTestCasePredicates( - testGen.tests.at(parse_c).methods.begin().value().testCases, - std::vector({[](const tests::Tests::MethodTestCase &testCase) { - std::string ret = testCase.returnValue.view->getEntryValue(nullptr); - return ret == "\'c\'" || ret == "\'l\'"; - }}), - "parse"); - } + auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); + auto testGen = FileTestGen(*request, writer.get(), TESTMODE); + fs::path autoTarget = GrpcUtils::UTBOT_AUTO_TARGET_PATH; + testGen.setTargetPath(autoTarget); + + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + ASSERT_TRUE(status.ok()); + checkMinNumberOfTestsInFile(testGen, parse_c, 1); + checkTestCasePredicates( + testGen.tests.at(parse_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + std::string ret = testCase.returnValue.view->getEntryValue(nullptr); + return ret == "\'c\'" || ret == "\'l\'"; + }}), + "parse"); } + +TEST_F(TargetsTest, Valid_Target_Test_get_10) { + std::unique_ptr projectRequest = createProjectRequest( + projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + + auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); + fs::path get_10 = getTargetPathByName(*testGen.buildDatabase, "get_10"); + testGen.setTargetPath(get_10); + + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + ASSERT_TRUE(status.ok()) << status.error_message(); + + checkMinNumberOfTestsInFile(testGen, get_10_c, 1); + checkTestCasePredicates( + testGen.tests.at(get_10_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return testCase.returnValue.view->getEntryValue(nullptr) == "10"; + }}), + "get_any_val"); + + checkMinNumberOfTestsInFile(testGen, shared_c, 1); + checkTestCasePredicates( + testGen.tests.at(shared_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (10 + std::stoi(testCase.paramValues.front().view->getEntryValue(nullptr))); + }}), + "add_val"); + + checkMinNumberOfTestsInFile(testGen, get_val_main_c, 2); + checkTestCasePredicates( + testGen.tests.at(get_val_main_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (10 + 0); + }}), + "get_res"); + + checkNumberOfTestsInFile(testGen, parse_c, 0); + checkNumberOfTestsInFile(testGen, get_20_c, 0); + checkNumberOfTestsInFile(testGen, get_val_main_2_c, 0); +} + +TEST_F(TargetsTest, Valid_Target_Test_get_20) { + std::unique_ptr projectRequest = createProjectRequest( + projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + + auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); + fs::path get_20 = getTargetPathByName(*testGen.buildDatabase, "get_20"); + testGen.setTargetPath(get_20); + + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + ASSERT_TRUE(status.ok()) << status.error_message(); + + checkMinNumberOfTestsInFile(testGen, get_20_c, 1); + checkTestCasePredicates( + testGen.tests.at(get_20_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return testCase.returnValue.view->getEntryValue(nullptr) == "20"; + }}), + "get_any_val"); + + checkMinNumberOfTestsInFile(testGen, shared_c, 1); + checkTestCasePredicates( + testGen.tests.at(shared_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (20 + std::stoi(testCase.paramValues.front().view->getEntryValue(nullptr))); + }}), + "add_val"); + + checkMinNumberOfTestsInFile(testGen, get_val_main_c, 2); + checkTestCasePredicates( + testGen.tests.at(get_val_main_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (20 + 0); + }}), + "get_res"); + + checkNumberOfTestsInFile(testGen, parse_c, 0); + checkNumberOfTestsInFile(testGen, get_10_c, 0); + checkNumberOfTestsInFile(testGen, get_val_main_2_c, 0); +} + +TEST_F(TargetsTest, Valid_Target_Test_get_10_2) { + std::unique_ptr projectRequest = createProjectRequest( + projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + + auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); + fs::path get_10_2 = getTargetPathByName(*testGen.buildDatabase, "get_10_2"); + testGen.setTargetPath(get_10_2); + + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + ASSERT_TRUE(status.ok()) << status.error_message(); + + checkMinNumberOfTestsInFile(testGen, get_10_c, 1); + checkTestCasePredicates( + testGen.tests.at(get_10_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return testCase.returnValue.view->getEntryValue(nullptr) == "10"; + }}), + "get_any_val"); + + checkMinNumberOfTestsInFile(testGen, shared_c, 1); + checkTestCasePredicates( + testGen.tests.at(shared_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (10 + std::stoi(testCase.paramValues.front().view->getEntryValue(nullptr))); + }}), + "add_val"); + + checkMinNumberOfTestsInFile(testGen, get_val_main_2_c, 2); + checkTestCasePredicates( + testGen.tests.at(get_val_main_2_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (10 + 5); + }}), + "get_res"); + + checkNumberOfTestsInFile(testGen, parse_c, 0); + checkNumberOfTestsInFile(testGen, get_20_c, 0); + checkNumberOfTestsInFile(testGen, get_val_main_c, 0); +} + +TEST_F(TargetsTest, Valid_Target_Test_libshared) { + std::unique_ptr projectRequest = createProjectRequest( + projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + + auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); + fs::path lib_shared = getTargetPathByName(*testGen.buildDatabase, "libshared_get.so"); + testGen.setTargetPath(lib_shared); + + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + ASSERT_TRUE(status.ok()) << status.error_message(); + + checkMinNumberOfTestsInFile(testGen, get_20_c, 1); + checkTestCasePredicates( + testGen.tests.at(get_20_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return testCase.returnValue.view->getEntryValue(nullptr) == "20"; + }}), + "get_any_val"); + + checkMinNumberOfTestsInFile(testGen, shared_c, 1); + checkTestCasePredicates( + testGen.tests.at(shared_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (20 + std::stoi(testCase.paramValues.front().view->getEntryValue(nullptr))); + }}), + "add_val"); + + checkMinNumberOfTestsInFile(testGen, get_val_main_c, 1); + checkTestCasePredicates( + testGen.tests.at(get_val_main_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (20 + 0); + }}), + "get_res"); + + checkNumberOfTestsInFile(testGen, parse_c, 0); + checkNumberOfTestsInFile(testGen, get_10_c, 0); + checkNumberOfTestsInFile(testGen, get_val_main_2_c, 0); +} + +TEST_F(TargetsTest, Valid_Target_Test_get_libstatic) { + std::unique_ptr projectRequest = createProjectRequest( + projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + + auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); + fs::path lib_static = getTargetPathByName(*testGen.buildDatabase, "libstatic_get.a"); + testGen.setTargetPath(lib_static); + + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + ASSERT_TRUE(status.ok()) << status.error_message(); + + checkMinNumberOfTestsInFile(testGen, get_20_c, 1); + checkTestCasePredicates( + testGen.tests.at(get_20_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return testCase.returnValue.view->getEntryValue(nullptr) == "200"; + }}), + "get_any_val"); + + checkMinNumberOfTestsInFile(testGen, shared_c, 1); + checkTestCasePredicates( + testGen.tests.at(shared_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (200 + std::stoi(testCase.paramValues.front().view->getEntryValue(nullptr))); + }}), + "add_val"); + + checkMinNumberOfTestsInFile(testGen, get_val_main_c, 2); + checkTestCasePredicates( + testGen.tests.at(get_val_main_c).methods.begin().value().testCases, + std::vector({[](const tests::Tests::MethodTestCase &testCase) { + return std::stoi(testCase.returnValue.view->getEntryValue(nullptr)) == + (200 + 0); + }}), + "get_res"); + + checkNumberOfTestsInFile(testGen, parse_c, 0); + checkNumberOfTestsInFile(testGen, get_10_c, 0); + checkNumberOfTestsInFile(testGen, get_val_main_2_c, 0); +} + diff --git a/server/test/framework/TestUtils.cpp b/server/test/framework/TestUtils.cpp index 43acde556..28fa7bf43 100644 --- a/server/test/framework/TestUtils.cpp +++ b/server/test/framework/TestUtils.cpp @@ -76,10 +76,10 @@ namespace testUtils { CoverageLines actualLinesCovered; CoverageLines actualLinesUncovered; for (const auto &[filePath, fileCoverage] : coverageMap) { - for(const auto &sourceLine : fileCoverage.fullCoverageLines) { + for (const auto &sourceLine : fileCoverage.fullCoverageLines) { actualLinesCovered[filePath].insert(sourceLine.line); } - for(const auto &sourceLine : fileCoverage.noCoverageLines) { + for (const auto &sourceLine : fileCoverage.noCoverageLines) { actualLinesUncovered[filePath].insert(sourceLine.line); } } @@ -141,14 +141,14 @@ namespace testUtils { } const auto status = testResultMap.at(filename).at(testname).status(); EXPECT_TRUE((testsgen::TestStatus::TEST_PASSED == status) || - (testsgen::TestStatus::TEST_DEATH == status)); + (testsgen::TestStatus::TEST_DEATH == status)); } } void checkStatusesCount(const Coverage::TestResultMap &testResultMap, const std::vector &tests, - const StatusCountMap &expectedStatusCountMap) { + const StatusCountMap &expectedStatusCountMap) { StatusCountMap actualStatusCountMap; for (auto const &[filename, suitename, testname] : tests) { if (suitename == tests::Tests::ERROR_SUITE_NAME) { @@ -157,14 +157,14 @@ namespace testUtils { const auto status = testResultMap.at(filename).at(testname).status(); actualStatusCountMap[status]++; } - for (const auto& [status, count] : actualStatusCountMap) { + for (const auto &[status, count] : actualStatusCountMap) { ASSERT_GE(count, expectedStatusCountMap.at(status)); } } - int getNumberOfTests(const tests::TestsMap &tests) { + size_t getNumberOfTests(const tests::TestsMap &tests) { auto testFilePaths = CollectionUtils::getKeys(tests); - int testsCounter = 0; + size_t testsCounter = 0; for (const auto &[filename, cases] : tests) { for (const auto &[_, methodDescription] : cases.methods) { int size = methodDescription.testCases.size(); @@ -174,16 +174,40 @@ namespace testUtils { return testsCounter; } - void checkMinNumberOfTests(const tests::TestsMap &tests, int minNumber) { - int testsCounter = getNumberOfTests(tests); + size_t getNumberOfTestsForFile(const BaseTestGen &testGen, const std::string &fileName) { + auto tests = testGen.tests.find(fileName); + size_t testsCounter = 0; + if (tests != testGen.tests.end()) { + for (const auto &[methodName, methodDescription]: tests.value().methods) { + testsCounter += methodDescription.testCases.size(); + } + } + return testsCounter; + } + + void checkMinNumberOfTests(const tests::TestsMap &tests, size_t minNumber) { + size_t testsCounter = getNumberOfTests(tests); EXPECT_LE(minNumber, testsCounter) << "Number of test cases is too small"; } void checkMinNumberOfTests(const std::vector &testCases, - int minNumber) { + size_t minNumber) { EXPECT_LE(minNumber, testCases.size()) << "Number of test cases is too small"; } + void checkNumberOfTestsInFile(const BaseTestGen &testGen, std::string fileName, size_t number) { + size_t testCounter = getNumberOfTestsForFile(testGen, fileName); + EXPECT_EQ(number, testCounter) + << "Number of test cases in \"" << fileName << "\" not equal to predicate"; + } + + void + checkMinNumberOfTestsInFile(const BaseTestGen &testGen, std::string fileName, size_t number) { + size_t testCounter = getNumberOfTestsForFile(testGen, fileName); + EXPECT_LE(number, testCounter) + << "Number of test cases in \"" << fileName << "\" not equal to predicate"; + } + std::unique_ptr createProjectRequest(const std::string &projectName, const fs::path &projectPath, const std::string &buildDirRelativePath, @@ -192,8 +216,9 @@ namespace testUtils { bool verbose, int kleeTimeout) { auto projectContext = GrpcUtils::createProjectContext( - projectName, projectPath, projectPath / "tests", buildDirRelativePath); - auto settingsContext = GrpcUtils::createSettingsContext(true, verbose, kleeTimeout, 0, false, useStubs); + projectName, projectPath, projectPath / "tests", buildDirRelativePath); + auto settingsContext = + GrpcUtils::createSettingsContext(true, verbose, kleeTimeout, 0, false, useStubs); return GrpcUtils::createProjectRequest(std::move(projectContext), std::move(settingsContext), srcPaths); } @@ -222,13 +247,13 @@ namespace testUtils { } std::unique_ptr createClassRequest(const std::string &projectName, - const fs::path &projectPath, - const std::string &buildDirRelativePath, - const std::vector &srcPaths, - const fs::path &filePath, - int line, - bool verbose, - int kleeTimeout) { + const fs::path &projectPath, + const std::string &buildDirRelativePath, + const std::vector &srcPaths, + const fs::path &filePath, + int line, + bool verbose, + int kleeTimeout) { auto lineRequest = createLineRequest(projectName, projectPath, buildDirRelativePath, srcPaths, filePath, line, false, verbose, kleeTimeout); return GrpcUtils::createClassRequest(std::move(lineRequest)); diff --git a/server/test/framework/TestUtils.h b/server/test/framework/TestUtils.h index 3f9079b48..fcc34ae4d 100644 --- a/server/test/framework/TestUtils.h +++ b/server/test/framework/TestUtils.h @@ -47,12 +47,19 @@ namespace testUtils { const std::vector &tests, const StatusCountMap &expectedStatusCountMap); - int getNumberOfTests(const tests::TestsMap &tests); + size_t getNumberOfTests(const tests::TestsMap &tests); - void checkMinNumberOfTests(const tests::TestsMap &tests, int minNumber); + size_t getNumberOfTestsForFile(const BaseTestGen &testGen, const std::string &fileName); + + void checkMinNumberOfTests(const tests::TestsMap &tests, size_t minNumber); void checkMinNumberOfTests(const std::vector &testCases, - int minNumber); + size_t minNumber); + + void checkNumberOfTestsInFile(const BaseTestGen &testGen, std::string fileName, size_t number); + + void + checkMinNumberOfTestsInFile(const BaseTestGen &testGen, std::string fileName, size_t number); std::unique_ptr createSnippetRequest(const std::string &projectName, const fs::path &projectPath, @@ -81,13 +88,13 @@ namespace testUtils { bool verbose, int kleeTimeout); std::unique_ptr createClassRequest(const std::string &projectName, - const fs::path &projectPath, - const std::string &buildDirRelativePath, - const std::vector &srcPaths, - const fs::path &filePath, - int line, - bool verbose = true, - int kleeTimeout = 60); + const fs::path &projectPath, + const std::string &buildDirRelativePath, + const std::vector &srcPaths, + const fs::path &filePath, + int line, + bool verbose = true, + int kleeTimeout = 60); std::unique_ptr createCoverageAndResultsRequest(const std::string &projectName, @@ -107,7 +114,8 @@ namespace testUtils { void tryExecGetBuildCommands( const fs::path &path, CompilationUtils::CompilerName compilerName = CompilationUtils::CompilerName::CLANG, - BuildCommandsTool buildCommandsTool = CMAKE_BUILD_COMMANDS_TOOL, bool build = true); + BuildCommandsTool buildCommandsTool = CMAKE_BUILD_COMMANDS_TOOL, + bool build = true); fs::path getRelativeTestSuitePath(const std::string &suiteName); @@ -115,7 +123,7 @@ namespace testUtils { std::string unexpectedFileMessage(const fs::path &filePath); - std::vector createArgvVector(const std::vector &args); + std::vector createArgvVector(const std::vector &args); void setTargetForFirstSource(ProjectTestGen &testGen); diff --git a/server/test/suites/targets/CMakeLists.txt b/server/test/suites/targets/CMakeLists.txt index 1e5832741..f9915ebf3 100644 --- a/server/test/suites/targets/CMakeLists.txt +++ b/server/test/suites/targets/CMakeLists.txt @@ -14,6 +14,15 @@ add_executable(ls ls.c) add_executable(cat cat.c) add_executable(dummy dummy.c) +add_executable(get_10 get_10.c shared.c get_val_main.c) +add_executable(get_20 get_20x.c shared.c get_val_main.c) +add_executable(get_10_2 get_10.c shared.c get_val_main_2.c) + +add_library(shared_get SHARED get_20x.c shared.c get_val_main.c) + +add_library(static_get STATIC get_20x.c shared.c get_val_main.c) +target_compile_definitions(static_get PRIVATE TESTDEF) + target_link_libraries(ls parse) target_link_libraries(cat parse) -target_link_libraries(dummy parse) \ No newline at end of file +target_link_libraries(dummy parse) diff --git a/server/test/suites/targets/get_10.c b/server/test/suites/targets/get_10.c new file mode 100644 index 000000000..08b90a009 --- /dev/null +++ b/server/test/suites/targets/get_10.c @@ -0,0 +1,3 @@ +int get_any_val() { + return 10; +} diff --git a/server/test/suites/targets/get_20x.c b/server/test/suites/targets/get_20x.c new file mode 100644 index 000000000..5862d972f --- /dev/null +++ b/server/test/suites/targets/get_20x.c @@ -0,0 +1,7 @@ +int get_any_val() { +#ifndef TESTDEF + return 20; +#else + return 200; +#endif +} diff --git a/server/test/suites/targets/get_any_val.h b/server/test/suites/targets/get_any_val.h new file mode 100644 index 000000000..b90abfb65 --- /dev/null +++ b/server/test/suites/targets/get_any_val.h @@ -0,0 +1,7 @@ +#ifndef UNITTESTBOT_GET_ANY_VAL_H +#define UNITTESTBOT_GET_ANY_VAL_H + + +int get_any_val(); + +#endif // UNITTESTBOT_GET_ANY_VAL_H diff --git a/server/test/suites/targets/get_val_main.c b/server/test/suites/targets/get_val_main.c new file mode 100644 index 000000000..1bae6ae46 --- /dev/null +++ b/server/test/suites/targets/get_val_main.c @@ -0,0 +1,9 @@ +#include "shared.h" + +int get_res() { + return add_val(0); +} + +int main(void) { + return 0; +} diff --git a/server/test/suites/targets/get_val_main_2.c b/server/test/suites/targets/get_val_main_2.c new file mode 100644 index 000000000..138702d16 --- /dev/null +++ b/server/test/suites/targets/get_val_main_2.c @@ -0,0 +1,9 @@ +#include "shared.h" + +int get_res() { + return add_val(5); +} + +int main(void) { + return 0; +} diff --git a/server/test/suites/targets/shared.c b/server/test/suites/targets/shared.c new file mode 100644 index 000000000..50513f50f --- /dev/null +++ b/server/test/suites/targets/shared.c @@ -0,0 +1,5 @@ +#include "get_any_val.h" + +int add_val(int a) { + return a + get_any_val(); +} diff --git a/server/test/suites/targets/shared.h b/server/test/suites/targets/shared.h new file mode 100644 index 000000000..e60d28dbc --- /dev/null +++ b/server/test/suites/targets/shared.h @@ -0,0 +1,6 @@ +#ifndef UNITTESTBOT_SHARED_H +#define UNITTESTBOT_SHARED_H + +int add_val(int a); + +#endif // UNITTESTBOT_SHARED_H From f8d92f2e933b20d61200b06c58e5d86ea38ec184 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 21 Jul 2022 18:38:44 +0300 Subject: [PATCH 02/27] rebase --- server/src/building/BuildDatabase.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 4ff2f4a9d..be37692cc 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -8,14 +8,14 @@ #include "utils/DynamicLibraryUtils.h" #include "utils/JsonUtils.h" #include "utils/StringUtils.h" +#include "utils/GrpcUtils.h" +#include "utils/GenerationUtils.h" #include "loguru.h" #include #include #include -#include "utils/GrpcUtils.h" -#include "utils/GenerationUtils.h" static std::string tryConvertOptionToPath(const std::string &possibleFilePath, const fs::path &dirPath) { @@ -153,6 +153,7 @@ void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) { sourceFileInfos[sourcePath].emplace_back(objectInfo); } for (auto &[sourceFile, objectInfos]: sourceFileInfos) { + //Need stable sort for save order of 32 and 64 bits files std::stable_sort(objectInfos.begin(), objectInfos.end(), conflictPriorityMore); } } From e3eafc0ef72622c9b35e70cf6d4240901806d90e Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Mon, 25 Jul 2022 18:38:20 +0300 Subject: [PATCH 03/27] rebase --- server/test/framework/Targets_Test.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/server/test/framework/Targets_Test.cpp b/server/test/framework/Targets_Test.cpp index 357b3b38e..4a265fe1f 100644 --- a/server/test/framework/Targets_Test.cpp +++ b/server/test/framework/Targets_Test.cpp @@ -12,7 +12,7 @@ class TargetsTest : public BaseTest { fs::path parse_c = getTestFilePath("parse.c"); fs::path get_10_c = getTestFilePath("get_10.c"); - fs::path get_20_c = getTestFilePath("get_20.c"); + fs::path get_20x_c = getTestFilePath("get_20x.c"); fs::path shared_c = getTestFilePath("shared.c"); fs::path get_val_main_c = getTestFilePath("get_val_main.c"); fs::path get_val_main_2_c = getTestFilePath("get_val_main_2.c"); @@ -146,7 +146,7 @@ TEST_F(TargetsTest, Valid_Target_Test_get_10) { "get_res"); checkNumberOfTestsInFile(testGen, parse_c, 0); - checkNumberOfTestsInFile(testGen, get_20_c, 0); + checkNumberOfTestsInFile(testGen, get_20x_c, 0); checkNumberOfTestsInFile(testGen, get_val_main_2_c, 0); } @@ -161,9 +161,9 @@ TEST_F(TargetsTest, Valid_Target_Test_get_20) { Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); - checkMinNumberOfTestsInFile(testGen, get_20_c, 1); + checkMinNumberOfTestsInFile(testGen, get_20x_c, 1); checkTestCasePredicates( - testGen.tests.at(get_20_c).methods.begin().value().testCases, + testGen.tests.at(get_20x_c).methods.begin().value().testCases, std::vector({[](const tests::Tests::MethodTestCase &testCase) { return testCase.returnValue.view->getEntryValue(nullptr) == "20"; }}), @@ -230,7 +230,7 @@ TEST_F(TargetsTest, Valid_Target_Test_get_10_2) { "get_res"); checkNumberOfTestsInFile(testGen, parse_c, 0); - checkNumberOfTestsInFile(testGen, get_20_c, 0); + checkNumberOfTestsInFile(testGen, get_20x_c, 0); checkNumberOfTestsInFile(testGen, get_val_main_c, 0); } @@ -245,9 +245,9 @@ TEST_F(TargetsTest, Valid_Target_Test_libshared) { Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); - checkMinNumberOfTestsInFile(testGen, get_20_c, 1); + checkMinNumberOfTestsInFile(testGen, get_20x_c, 1); checkTestCasePredicates( - testGen.tests.at(get_20_c).methods.begin().value().testCases, + testGen.tests.at(get_20x_c).methods.begin().value().testCases, std::vector({[](const tests::Tests::MethodTestCase &testCase) { return testCase.returnValue.view->getEntryValue(nullptr) == "20"; }}), @@ -287,9 +287,9 @@ TEST_F(TargetsTest, Valid_Target_Test_get_libstatic) { Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); - checkMinNumberOfTestsInFile(testGen, get_20_c, 1); + checkMinNumberOfTestsInFile(testGen, get_20x_c, 1); checkTestCasePredicates( - testGen.tests.at(get_20_c).methods.begin().value().testCases, + testGen.tests.at(get_20x_c).methods.begin().value().testCases, std::vector({[](const tests::Tests::MethodTestCase &testCase) { return testCase.returnValue.view->getEntryValue(nullptr) == "200"; }}), From cdc2c98afcd4bf701de2ba5175b58bd95a11a5ff Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Tue, 26 Jul 2022 18:45:25 +0300 Subject: [PATCH 04/27] not end --- server/src/building/BuildDatabase.cpp | 7 ++- server/src/utils/GenerationUtils.cpp | 14 +++-- server/test/framework/Library_Test.cpp | 2 +- server/test/framework/Regression_Tests.cpp | 5 +- server/test/framework/Server_Tests.cpp | 14 ++--- server/test/framework/Stub_Tests.cpp | 24 ++++---- server/test/framework/Syntax_Tests.cpp | 6 +- server/test/framework/Targets_Test.cpp | 66 +++++++++------------- server/test/framework/TestUtils.cpp | 43 +++++++++++--- server/test/framework/TestUtils.h | 20 +++++-- 10 files changed, 112 insertions(+), 89 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index be37692cc..a95c8dbaf 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -223,12 +223,13 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &target, const } void BuildDatabase::updateTarget(std::string target) { - auto new_target = GenerationUtils::findTarget(getAllTargets(), target); + if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { + return; + } + auto new_target = GenerationUtils::findTarget(getAllTargets(), target); if (new_target.has_value()) { target = new_target.value(); - } else if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { - return; } else { throw CompilationDatabaseException("Can't find target: " + target); } diff --git a/server/src/utils/GenerationUtils.cpp b/server/src/utils/GenerationUtils.cpp index 4c8a8f964..494bb59a6 100644 --- a/server/src/utils/GenerationUtils.cpp +++ b/server/src/utils/GenerationUtils.cpp @@ -65,22 +65,24 @@ GenerationUtils::findTarget(const std::vectorgetOutput() << "\n"; + ss << target->getOutput() << "\n"; } - LOG_S(WARNING) << "\n"; + LOG_S(WARNING) << ss.str(); return std::nullopt; } else if (candidates.size() == 1) { return candidates[0]->getOutput(); } else { - LOG_S(WARNING) << "There are multiple candidates for given target. " + std::stringstream ss; + ss << "There are multiple candidates for given target. " "Please, specify full path of your preferred target. " "List of candidates:\n"; for (const auto &candidate : candidates) { - LOG_S(WARNING) << candidate->getOutput() << "\n"; + ss << candidate->getOutput() << "\n"; } - LOG_S(WARNING) << "\n"; + LOG_S(WARNING) << ss.str(); return std::nullopt; } } diff --git a/server/test/framework/Library_Test.cpp b/server/test/framework/Library_Test.cpp index 1a583616b..190540353 100644 --- a/server/test/framework/Library_Test.cpp +++ b/server/test/framework/Library_Test.cpp @@ -20,7 +20,7 @@ namespace { std::pair createTestForFunction(const fs::path &pathToFile, int lineNum, int kleeTimeout = 60) { auto lineRequest = testUtils::createLineRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, pathToFile, lineNum, true, false, kleeTimeout); + srcPaths, pathToFile, lineNum, std::nullopt, true, false, kleeTimeout); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(pathToFile); diff --git a/server/test/framework/Regression_Tests.cpp b/server/test/framework/Regression_Tests.cpp index 36a82f989..01a2b6373 100644 --- a/server/test/framework/Regression_Tests.cpp +++ b/server/test/framework/Regression_Tests.cpp @@ -28,7 +28,7 @@ namespace { std::pair createTestForFunction(const fs::path &pathToFile, int lineNum, bool verbose = true) { auto lineRequest = testUtils::createLineRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, pathToFile, lineNum, false, verbose, 0); + srcPaths, pathToFile, lineNum, std::nullopt, false, verbose, 0); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(pathToFile); @@ -40,10 +40,9 @@ namespace { std::pair createTestForFolder(const fs::path &pathToFolder, bool useStubs = true, bool verbose = true) { auto folderRequest = testUtils::createProjectRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, useStubs, verbose, 0); + srcPaths, std::nullopt,useStubs, verbose, 0); auto request = GrpcUtils::createFolderRequest(std::move(folderRequest), pathToFolder); auto testGen = FolderTestGen(*request, writer.get(), TESTMODE); - testGen.setTargetPath(GrpcUtils::UTBOT_AUTO_TARGET_PATH); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); return {testGen, status}; diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index 40b653c2d..85c85fb0c 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -1002,7 +1002,7 @@ namespace { TEST_P(Parameterized_Server_Test, Line_Test1) { auto request = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 17, false, false, 0); + basic_functions_c, 17, std::nullopt, false, false, 0); auto testGen = LineTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(basic_functions_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -1019,7 +1019,7 @@ namespace { TEST_P(Parameterized_Server_Test, Line_Test2) { auto request = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 17, false, false, 0); + basic_functions_c, 17, std::nullopt, false, false, 0); auto testGen = LineTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(basic_functions_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -1082,7 +1082,7 @@ namespace { TEST_P(Parameterized_Server_Test, Function_Test) { auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 6, false, false, 0); + basic_functions_c, 6, std::nullopt, false, false, 0); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(basic_functions_c); @@ -1110,7 +1110,7 @@ namespace { TEST_P(Parameterized_Server_Test, Predicate_Test_Integer) { auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 17, false, false, 0); + basic_functions_c, 17, std::nullopt, false, false, 0); auto predicateInfo = std::make_unique(); predicateInfo->set_predicate("=="); predicateInfo->set_returnvalue("36"); @@ -1133,7 +1133,7 @@ namespace { TEST_P(Parameterized_Server_Test, Predicate_Test_Str) { auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 32, false, false, 0); + basic_functions_c, 32, std::nullopt, false, false, 0); auto predicateInfo = std::make_unique(); predicateInfo->set_predicate("=="); predicateInfo->set_returnvalue("abacaba"); @@ -1157,7 +1157,7 @@ namespace { TEST_P(Parameterized_Server_Test, Symbolic_Stdin_Test) { auto request = std::make_unique(); auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - symbolic_stdin_c, 8, false, false, 0); + symbolic_stdin_c, 8, std::nullopt, false, false, 0); request->set_allocated_linerequest(lineRequest.release()); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(symbolic_stdin_c); @@ -1179,7 +1179,7 @@ namespace { TEST_P(Parameterized_Server_Test, Symbolic_Stdin_Long_Read) { auto request = std::make_unique(); auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - symbolic_stdin_c, 19, false, false, 0); + symbolic_stdin_c, 19, std::nullopt, false, false, 0); request->set_allocated_linerequest(lineRequest.release()); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(symbolic_stdin_c); diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index a14e36cf9..ff02862f2 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -111,7 +111,7 @@ namespace { TEST_F(Stub_Test, Project_Stubs_Test) { auto stubsWriter = std::make_unique(nullptr, false); - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, true); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, std::nullopt, true); auto testGen = std::make_unique(*request, writer.get(), TESTMODE); std::vector stubSources = { calc_sum_c, calc_mult_c, literals_foo_c }; Status status = @@ -122,7 +122,7 @@ namespace { TEST_F(Stub_Test, Implicit_Stubs_Test) { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - literals_foo_c, true); + literals_foo_c, std::nullopt, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -135,7 +135,7 @@ namespace { TEST_F(Stub_Test, Pregenerated_Stubs_Test) { { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - literals_foo_c, true); + literals_foo_c, std::nullopt, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -146,7 +146,7 @@ namespace { { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, literals_foo_c, true); + srcPaths, literals_foo_c, std::nullopt, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -159,7 +159,7 @@ namespace { TEST_F(Stub_Test, Multimodule_Lib_Heuristic_Test) { auto request = testUtils::createProjectRequest(projectName, suitePath, buildDirRelativePath, - { foreign, calc, suitePath, literals }, true); + { foreign, calc, suitePath, literals }, std::nullopt, true); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(foreign_bar_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -183,7 +183,7 @@ namespace { TEST_F(Stub_Test, File_Tests_With_Stubs) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, literals_foo_c, true); + srcPaths, literals_foo_c, std::nullopt, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -218,7 +218,7 @@ namespace { TEST_F(Stub_Test, Run_Tests_For_Unused_Function) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, calc_sum_c, true); + srcPaths, calc_sum_c, std::nullopt, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(calc_sum_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -239,7 +239,7 @@ namespace { TEST_F(Stub_Test, File_Tests_Without_Stubs) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, literals_foo_c, false); + srcPaths, literals_foo_c, std::nullopt, false); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -270,7 +270,7 @@ namespace { } TEST_F(Stub_Test, DISABLED_Sync_Stub_When_Source_Changed_Test) { - auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, literals_foo_c, true); + auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, literals_foo_c, std::nullopt, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -279,11 +279,11 @@ namespace { modifySources(sourcesToModify); std::string stubCode = modifyStubFile(sum_stub_c); - auto request2 = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, literals_foo_c, true); + auto request2 = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, literals_foo_c, std::nullopt, true); auto testGen2 = FileTestGen(*request2, writer.get(), TESTMODE); { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - literals_foo_c, true); + literals_foo_c, std::nullopt, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -292,7 +292,7 @@ namespace { } { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, literals_foo_c, true); + srcPaths, literals_foo_c, std::nullopt, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); diff --git a/server/test/framework/Syntax_Tests.cpp b/server/test/framework/Syntax_Tests.cpp index 8b5df170f..d6562ad39 100644 --- a/server/test/framework/Syntax_Tests.cpp +++ b/server/test/framework/Syntax_Tests.cpp @@ -81,7 +81,7 @@ namespace { std::pair createTestForFunction(const fs::path &pathToFile, int lineNum, int kleeTimeout = 60) { auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, pathToFile, lineNum, false, false, kleeTimeout); + srcPaths, pathToFile, lineNum, std::nullopt, false, false, kleeTimeout); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(pathToFile); @@ -2199,7 +2199,7 @@ namespace { TEST_F(Syntax_Test, Run_Tests_For_Linked_List) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, linked_list_c, true, false); + srcPaths, linked_list_c, std::nullopt, true, false); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(linked_list_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -2232,7 +2232,7 @@ namespace { TEST_F(Syntax_Test, Run_Tests_For_Tree) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, tree_c, true, false); + srcPaths, tree_c, std::nullopt, true, false); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(tree_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); diff --git a/server/test/framework/Targets_Test.cpp b/server/test/framework/Targets_Test.cpp index 4a265fe1f..80a151d68 100644 --- a/server/test/framework/Targets_Test.cpp +++ b/server/test/framework/Targets_Test.cpp @@ -16,28 +16,17 @@ class TargetsTest : public BaseTest { fs::path shared_c = getTestFilePath("shared.c"); fs::path get_val_main_c = getTestFilePath("get_val_main.c"); fs::path get_val_main_2_c = getTestFilePath("get_val_main_2.c"); - - fs::path getTargetPathByName(BuildDatabase const &buildDatabase, fs::path const &fileName) { - auto rootTargets = buildDatabase.getRootTargets(); - auto it = - std::find_if(rootTargets.begin(), rootTargets.end(), - [&fileName](std::shared_ptr linkUnitInfo) { - return linkUnitInfo->getOutput().filename() == fileName; - }); - assert(it != rootTargets.end()); - return it->get()->getOutput(); - } }; using namespace testUtils; TEST_F(TargetsTest, Valid_Target_Test_ls) { auto projectRequest = - createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "ls"); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - fs::path ls = getTargetPathByName(*testGen.buildDatabase, "ls"); - testGen.setTargetPath(ls); +// fs::path ls = getTargetPathByName(*testGen.buildDatabase, "ls"); +// testGen.setTargetPath(ls); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -54,11 +43,11 @@ TEST_F(TargetsTest, Valid_Target_Test_ls) { TEST_F(TargetsTest, Valid_Target_Test_cat) { auto projectRequest = - createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "cat"); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - fs::path cat = getTargetPathByName(*testGen.buildDatabase, "cat"); - testGen.setTargetPath(cat); +// fs::path cat = getTargetPathByName(*testGen.buildDatabase, "cat"); +// testGen.setTargetPath(cat); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -74,12 +63,11 @@ TEST_F(TargetsTest, Valid_Target_Test_cat) { } TEST_F(TargetsTest, Valid_Target_Test_dummy) { - auto projectRequest = - createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "dummy"); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - fs::path dummy = getTargetPathByName(*testGen.buildDatabase, "dummy"); - testGen.setTargetPath(dummy); +// fs::path dummy = getTargetPathByName(*testGen.buildDatabase, "dummy"); +// testGen.setTargetPath(dummy); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_FALSE(status.ok()); @@ -89,12 +77,11 @@ TEST_F(TargetsTest, Valid_Target_Test_dummy) { } TEST_F(TargetsTest, Valid_Target_Test_parse) { - auto projectRequest = - createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - fs::path autoTarget = GrpcUtils::UTBOT_AUTO_TARGET_PATH; - testGen.setTargetPath(autoTarget); +// fs::path autoTarget = GrpcUtils::UTBOT_AUTO_TARGET_PATH; +// testGen.setTargetPath(autoTarget); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()); @@ -110,11 +97,11 @@ TEST_F(TargetsTest, Valid_Target_Test_parse) { TEST_F(TargetsTest, Valid_Target_Test_get_10) { std::unique_ptr projectRequest = createProjectRequest( - projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + projectName, suitePath, buildDirRelativePath, srcPaths, "get_10", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); - fs::path get_10 = getTargetPathByName(*testGen.buildDatabase, "get_10"); - testGen.setTargetPath(get_10); +// fs::path get_10 = getTargetPathByName(*testGen.buildDatabase, "get_10"); +// testGen.setTargetPath(get_10); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -152,11 +139,10 @@ TEST_F(TargetsTest, Valid_Target_Test_get_10) { TEST_F(TargetsTest, Valid_Target_Test_get_20) { std::unique_ptr projectRequest = createProjectRequest( - projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + projectName, suitePath, buildDirRelativePath, srcPaths, "get_20", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); - fs::path get_20 = getTargetPathByName(*testGen.buildDatabase, "get_20"); - testGen.setTargetPath(get_20); +// fs::path get_20 = getTargetPathByName(*testGen.buildDatabase, "get_20"); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -194,11 +180,11 @@ TEST_F(TargetsTest, Valid_Target_Test_get_20) { TEST_F(TargetsTest, Valid_Target_Test_get_10_2) { std::unique_ptr projectRequest = createProjectRequest( - projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + projectName, suitePath, buildDirRelativePath, srcPaths, "get_10_2", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); - fs::path get_10_2 = getTargetPathByName(*testGen.buildDatabase, "get_10_2"); - testGen.setTargetPath(get_10_2); +// fs::path get_10_2 = getTargetPathByName(*testGen.buildDatabase, "get_10_2"); +// testGen.setTargetPath(get_10_2); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -236,11 +222,11 @@ TEST_F(TargetsTest, Valid_Target_Test_get_10_2) { TEST_F(TargetsTest, Valid_Target_Test_libshared) { std::unique_ptr projectRequest = createProjectRequest( - projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + projectName, suitePath, buildDirRelativePath, srcPaths, "libshared_get.so", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); - fs::path lib_shared = getTargetPathByName(*testGen.buildDatabase, "libshared_get.so"); - testGen.setTargetPath(lib_shared); +// fs::path lib_shared = getTargetPathByName(*testGen.buildDatabase, "libshared_get.so"); +// testGen.setTargetPath(lib_shared); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -278,11 +264,11 @@ TEST_F(TargetsTest, Valid_Target_Test_libshared) { TEST_F(TargetsTest, Valid_Target_Test_get_libstatic) { std::unique_ptr projectRequest = createProjectRequest( - projectName, suitePath, buildDirRelativePath, srcPaths, false, false, 15); + projectName, suitePath, buildDirRelativePath, srcPaths, "libstatic_get.a", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); - fs::path lib_static = getTargetPathByName(*testGen.buildDatabase, "libstatic_get.a"); - testGen.setTargetPath(lib_static); +// fs::path lib_static = getTargetPathByName(*testGen.buildDatabase, "libstatic_get.a"); +// testGen.setTargetPath(lib_static); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); diff --git a/server/test/framework/TestUtils.cpp b/server/test/framework/TestUtils.cpp index 28fa7bf43..60df5cdc2 100644 --- a/server/test/framework/TestUtils.cpp +++ b/server/test/framework/TestUtils.cpp @@ -212,6 +212,7 @@ namespace testUtils { const fs::path &projectPath, const std::string &buildDirRelativePath, const std::vector &srcPaths, + const std::optional &target, bool useStubs, bool verbose, int kleeTimeout) { @@ -219,8 +220,23 @@ namespace testUtils { projectName, projectPath, projectPath / "tests", buildDirRelativePath); auto settingsContext = GrpcUtils::createSettingsContext(true, verbose, kleeTimeout, 0, false, useStubs); + + + auto buildDatabase = BuildDatabase::create(utbot::ProjectContext(*projectContext), + GrpcUtils::UTBOT_AUTO_TARGET_PATH); + auto rootTargets = buildDatabase->getRootTargets(); + auto it = std::find_if(rootTargets.begin(), rootTargets.end(), + [&target](std::shared_ptr linkUnitInfo) { + return linkUnitInfo->getOutput().filename() == target; + }); + assert(it != rootTargets.end()); + std::string new_target = it->get()->getOutput(); + + return GrpcUtils::createProjectRequest(std::move(projectContext), - std::move(settingsContext), srcPaths); + std::move(settingsContext), + srcPaths, + new_target); } std::unique_ptr createFileRequest(const std::string &projectName, @@ -228,20 +244,27 @@ namespace testUtils { const std::string &buildDirRelativePath, const std::vector &srcPaths, const fs::path &filePath, + const std::optional &target, bool useStubs, - bool verbose) { + bool verbose, + int kleeTimeout) { auto projectRequest = createProjectRequest(projectName, projectPath, buildDirRelativePath, - srcPaths, useStubs, verbose); + srcPaths, target, useStubs, verbose, kleeTimeout); return GrpcUtils::createFileRequest(std::move(projectRequest), filePath); } - std::unique_ptr createLineRequest(const std::string &projectName, const fs::path &projectPath, + std::unique_ptr createLineRequest(const std::string &projectName, + const fs::path &projectPath, const std::string &buildDirRelativePath, - const std::vector &srcPaths, const fs::path &filePath, - int line, bool useStubs, - bool verbose, int kleeTimeout) { + const std::vector &srcPaths, + const fs::path &filePath, + int line, + const std::optional &target, + bool useStubs, + bool verbose, + int kleeTimeout) { auto projectRequest = createProjectRequest(projectName, projectPath, buildDirRelativePath, - srcPaths, useStubs, verbose, kleeTimeout); + srcPaths, target, useStubs, verbose, kleeTimeout); auto lineInfo = GrpcUtils::createSourceInfo(filePath, line); return GrpcUtils::createLineRequest(std::move(projectRequest), std::move(lineInfo)); } @@ -252,10 +275,12 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, + const std::optional &target, + bool useStubs, bool verbose, int kleeTimeout) { auto lineRequest = createLineRequest(projectName, projectPath, buildDirRelativePath, - srcPaths, filePath, line, false, verbose, kleeTimeout); + srcPaths, filePath, line, target, useStubs, verbose, kleeTimeout); return GrpcUtils::createClassRequest(std::move(lineRequest)); } diff --git a/server/test/framework/TestUtils.h b/server/test/framework/TestUtils.h index fcc34ae4d..012879f8f 100644 --- a/server/test/framework/TestUtils.h +++ b/server/test/framework/TestUtils.h @@ -69,6 +69,7 @@ namespace testUtils { const fs::path &projectPath, const std::string &buildDirRelativePath, const std::vector &srcPaths, + const std::optional &target = std::nullopt, bool useStubs = false, bool verbose = true, int kleeTimeout = 60); @@ -78,14 +79,21 @@ namespace testUtils { const std::string &buildDirRelativePath, const std::vector &srcPaths, const fs::path &filePath, + const std::optional &target = std::nullopt, bool useStubs = false, - bool verbose = true); + bool verbose = true, + int kleeTimeout = 60); - std::unique_ptr createLineRequest(const std::string &projectName, const fs::path &projectPath, + std::unique_ptr createLineRequest(const std::string &projectName, + const fs::path &projectPath, const std::string &buildDirRelativePath, - const std::vector &srcPaths, const fs::path &filePath, - int line, bool useStubs, - bool verbose, int kleeTimeout); + const std::vector &srcPaths, + const fs::path &filePath, + int line, + const std::optional &target = std::nullopt, + bool useStubs = false, + bool verbose = true, + int kleeTimeout = 60); std::unique_ptr createClassRequest(const std::string &projectName, const fs::path &projectPath, @@ -93,6 +101,8 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, + const std::optional &target = std::nullopt, + bool useStubs = false, bool verbose = true, int kleeTimeout = 60); From 0e09fc4ab94119088af0fc63f72d488dee685e87 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Wed, 27 Jul 2022 19:39:11 +0300 Subject: [PATCH 05/27] not end --- server/src/ProjectTarget.cpp | 21 ------------------- server/src/ProjectTarget.h | 29 --------------------------- server/src/building/BuildDatabase.cpp | 8 ++++++++ server/src/building/BuildDatabase.h | 2 ++ server/src/testgens/BaseTestGen.h | 10 ++++----- server/src/testgens/ProjectTestGen.h | 3 +-- server/src/utils/GrpcUtils.cpp | 2 +- server/src/utils/GrpcUtils.h | 1 - server/test/framework/TestUtils.h | 1 - 9 files changed, 16 insertions(+), 61 deletions(-) delete mode 100644 server/src/ProjectTarget.cpp delete mode 100644 server/src/ProjectTarget.h diff --git a/server/src/ProjectTarget.cpp b/server/src/ProjectTarget.cpp deleted file mode 100644 index 2e210095c..000000000 --- a/server/src/ProjectTarget.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "ProjectTarget.h" - -#include - -#include - -namespace utbot { - ProjectTarget::ProjectTarget(std::string name, fs::path path) - : name(std::move(name)), path(std::move(path)) { - } - ProjectTarget::ProjectTarget(const testsgen::ProjectTarget &projectTarget) - : ProjectTarget(projectTarget.name(), projectTarget.path()) { - } - - const std::string &ProjectTarget::getName() const { - return name; - } - const fs::path &ProjectTarget::getPath() const { - return path; - } -} diff --git a/server/src/ProjectTarget.h b/server/src/ProjectTarget.h deleted file mode 100644 index 3002c7b03..000000000 --- a/server/src/ProjectTarget.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef UNITTESTBOT_PROJECTTARGET_H -#define UNITTESTBOT_PROJECTTARGET_H - -#include "utils/path/FileSystemPath.h" - -#include - -namespace testsgen { - class ProjectTarget; -} - -namespace utbot { - class ProjectTarget { - public: - ProjectTarget(std::string name, fs::path path); - - explicit ProjectTarget(testsgen::ProjectTarget const &projectTarget); - - const std::string &getName() const; - const fs::path &getPath() const; - - private: - std::string name; - fs::path path; - }; -} - - -#endif // UNITTESTBOT_PROJECTTARGET_H diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index a95c8dbaf..5eafccaeb 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -733,3 +733,11 @@ fs::path BuildDatabase::newDirForFile(const fs::path &file) const { this->projectContext.projectPath); return Paths::createNewDirForFile(file, base, this->serverBuildDir); } + +CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const std::string &_target) { + return CollectionUtils::transformTo( + getArchiveObjectFiles(_target), + [this](fs::path const &objectPath) { + return getClientCompilationUnitInfo(objectPath)->getSourcePath(); + }); +} diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 76fa47444..a0ab0bce9 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -205,6 +205,7 @@ class BuildDatabase { fs::path buildCommandsJsonPath; fs::path linkCommandsJsonPath; fs::path compileCommandsJsonPath; + std::optional target; CollectionUtils::MapFileTo>> sourceFileInfos; CollectionUtils::MapFileTo> objectFileInfos; CollectionUtils::MapFileTo> targetInfos; @@ -229,6 +230,7 @@ class BuildDatabase { void mergeLibraryOptions(std::vector &jsonArguments) const; fs::path newDirForFile(fs::path const& file) const; fs::path createExplicitObjectFileCompilationCommand(const std::shared_ptr &objectInfo); + CollectionUtils::FileSet getSourceFilesForTarget(const std::string &_target); using sharedLibrariesMap = std::unordered_map>; diff --git a/server/src/testgens/BaseTestGen.h b/server/src/testgens/BaseTestGen.h index 611bf2e7d..e781ba1d4 100644 --- a/server/src/testgens/BaseTestGen.h +++ b/server/src/testgens/BaseTestGen.h @@ -2,7 +2,6 @@ #define UNITTESTBOT_BASETESTGEN_H #include "ProjectContext.h" -#include "ProjectTarget.h" #include "SettingsContext.h" #include "Tests.h" #include "building/BuildDatabase.h" @@ -33,7 +32,6 @@ class BaseTestGen { std::vector synchronizedStubs; types::TypeMaps types; - std::optional targetPath; CollectionUtils::FileSet targetSources; virtual std::string toString() = 0; @@ -42,9 +40,9 @@ class BaseTestGen { bool isBatched() const; - bool hasAutoTarget() const; - fs::path const &getTargetPath() const; - void setTargetPath(fs::path _targetPath); +// bool hasAutoTarget() const; +// fs::path const &getTargetPath() const; +// void setTargetPath(fs::path _targetPath); virtual ~BaseTestGen() = default; protected: @@ -55,7 +53,7 @@ class BaseTestGen { void setInitializedTestsMap(); - virtual void setTargetForSource(fs::path const& sourcePath) = 0; +// virtual void setTargetForSource(fs::path const& sourcePath) = 0; void updateTargetSources(); }; diff --git a/server/src/testgens/ProjectTestGen.h b/server/src/testgens/ProjectTestGen.h index 0647c091e..7dd09cd91 100644 --- a/server/src/testgens/ProjectTestGen.h +++ b/server/src/testgens/ProjectTestGen.h @@ -2,7 +2,6 @@ #define UNITTESTBOT_PROJECTTESTGEN_H #include "BaseTestGen.h" -#include "ProjectTarget.h" #include @@ -19,7 +18,7 @@ class ProjectTestGen : public BaseTestGen { const testsgen::ProjectRequest *getRequest() const; - void setTargetForSource(fs::path const &sourcePath) override; +// void setTargetForSource(fs::path const &sourcePath) override; private: testsgen::ProjectRequest const *const request; diff --git a/server/src/utils/GrpcUtils.cpp b/server/src/utils/GrpcUtils.cpp index b1483ecdc..fd11a0e82 100644 --- a/server/src/utils/GrpcUtils.cpp +++ b/server/src/utils/GrpcUtils.cpp @@ -2,7 +2,7 @@ namespace GrpcUtils { static const std::string UTBOT_AUTO_TARGET = "UTBot: auto"; - const std::string UTBOT_AUTO_TARGET_PATH = fs::path("/utbot/auto/target/path"); +// const std::string UTBOT_AUTO_TARGET_PATH = fs::path("/utbot/auto/target/path"); static inline const std::string UTBOT_AUTO_TARGET_DESCRIPTION = "Finds any target that contains the code under testing"; std::unique_ptr diff --git a/server/src/utils/GrpcUtils.h b/server/src/utils/GrpcUtils.h index 269567d6c..8406850fb 100644 --- a/server/src/utils/GrpcUtils.h +++ b/server/src/utils/GrpcUtils.h @@ -2,7 +2,6 @@ #define UNITTESTBOT_GRPCUTILS_H #include "ProjectContext.h" -#include "ProjectTarget.h" #include "utils/path/FileSystemPath.h" #include diff --git a/server/test/framework/TestUtils.h b/server/test/framework/TestUtils.h index 012879f8f..ab399eab1 100644 --- a/server/test/framework/TestUtils.h +++ b/server/test/framework/TestUtils.h @@ -3,7 +3,6 @@ #include "gtest/gtest.h" -#include "ProjectTarget.h" #include "Server.h" #include "Tests.h" #include "coverage/Coverage.h" From 0f16c2f14457ec9e8cf7c0f05f7a7b89c7e382a3 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 28 Jul 2022 15:45:59 +0300 Subject: [PATCH 06/27] not end --- server/src/building/BuildDatabase.cpp | 65 ++++++++++++++++----------- server/src/building/BuildDatabase.h | 21 ++++----- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 5eafccaeb..1e750beb9 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -34,11 +34,12 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath, BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext, - const std::string &target) : serverBuildDir(std::move(_serverBuildDir)), - projectContext(std::move(_projectContext)), - buildCommandsJsonPath(std::move(_buildCommandsJsonPath)) { - linkCommandsJsonPath = fs::canonical(buildCommandsJsonPath / "link_commands.json"); - compileCommandsJsonPath = fs::canonical(buildCommandsJsonPath / "compile_commands.json"); + std::string _target) : + serverBuildDir(std::move(_serverBuildDir)), + projectContext(std::move(_projectContext)), + buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), + linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), + compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); } @@ -51,7 +52,9 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, filterInstalledFiles(); addLocalSharedLibraries(); fillTargetInfoParents(); - updateTarget(target); + updateTarget(_target);, + target(std::move(_target)) + createClangCompileCommandsJson(_target); } std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext, @@ -201,41 +204,47 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { } } -void BuildDatabase::createClangCompileCommandsJson(const fs::path &target, const fs::path &buildCommandsJsonPath) { - CollectionUtils::MapFileTo>> fileCompileCommands; - for (const auto &[compileCommand, objectInfo]: compileCommands_temp) { - const fs::path &sourcePath = objectInfo->getSourcePath(); - if ((target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || - targetInfos[target]->files.contains(objectInfo->getOutputFile())) && - (!CollectionUtils::containsKey(fileCompileCommands, sourcePath) || - conflictPriorityMore(objectInfo, fileCompileCommands[sourcePath].second))) { - fileCompileCommands[sourcePath] = {compileCommand, objectInfo}; - } - } +void BuildDatabase::createClangCompileCommandsJson(const fs::path &_target) { + LOG_IF_S(ERROR, !isAutoTarget() && _target != target) << "Try change non-auto target"; + + CollectionUtils::FileSet targetFileSet = getArchiveObjectFiles(_target); nlohmann::json compileCommandsSingleFilesJson; - for (const auto &compileCommand: fileCompileCommands) { - compileCommandsSingleFilesJson.push_back(compileCommand.second.first); + if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { + for (const auto &[compileCommand, objectInfo]: compileCommands_temp) { + compileCommandsSingleFilesJson.push_back(compileCommand); + } + } else { + CollectionUtils::MapFileTo>> fileCompileCommands; + for (const auto &[compileCommand, objectInfo]: compileCommands_temp) { + const fs::path &sourcePath = objectInfo->getSourcePath(); + if (CollectionUtils::contains(targetFileSet, objectInfo->getOutputFile()) && + (!CollectionUtils::containsKey(fileCompileCommands, sourcePath) || + conflictPriorityMore(objectInfo, fileCompileCommands[sourcePath].second))) { + fileCompileCommands[sourcePath] = {compileCommand, objectInfo}; + } + } + for (const auto &compileCommand: fileCompileCommands) { + compileCommandsSingleFilesJson.push_back(compileCommand.second.first); + } } fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath); JsonUtils::writeJsonToFile(clangCompileCommandsJsonPath, compileCommandsSingleFilesJson); } -void BuildDatabase::updateTarget(std::string target) { - if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { +void BuildDatabase::updateTarget(const std::string &_target) { + if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { return; } - auto new_target = GenerationUtils::findTarget(getAllTargets(), target); + auto new_target = GenerationUtils::findTarget(getAllTargets(), _target); if (new_target.has_value()) { target = new_target.value(); } else { throw CompilationDatabaseException("Can't find target: " + target); } - createClangCompileCommandsJson(target, buildCommandsJsonPath); - for (auto &[sourceFile, objectInfos]: sourceFileInfos) { std::sort(objectInfos.begin(), objectInfos.end(), [&](const std::shared_ptr &left, const std::shared_ptr &right) { @@ -635,6 +644,7 @@ CollectionUtils::FileSet BuildDatabase::getStubFiles( } return {}; } + void BuildDatabase::assignStubFilesToLinkUnit( std::shared_ptr linkUnitInfo, CollectionUtils::FileSet stubs) { @@ -734,10 +744,15 @@ fs::path BuildDatabase::newDirForFile(const fs::path &file) const { return Paths::createNewDirForFile(file, base, this->serverBuildDir); } -CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const std::string &_target) { +CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path &_target) { + LOG_IF_S(ERROR, !isAutoTarget() && target != _target.c_str()) << "Try get sources for different target"; return CollectionUtils::transformTo( getArchiveObjectFiles(_target), [this](fs::path const &objectPath) { return getClientCompilationUnitInfo(objectPath)->getSourcePath(); }); } + +bool BuildDatabase::isAutoTarget() const { + return target == GrpcUtils::UTBOT_AUTO_TARGET_PATH; +} diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index a0ab0bce9..17cd491c0 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -98,12 +98,12 @@ class BuildDatabase { BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext, - const std::string &target); + std::string _target); static std::shared_ptr create(const utbot::ProjectContext &projectContext, const std::string &target); - void updateTarget(std::string target); + void updateTarget(const std::string &target); const fs::path &getCompileCommandsJson(); const fs::path &getLinkCommandsJson(); @@ -200,12 +200,12 @@ class BuildDatabase { std::shared_ptr getPriorityTarget() const; private: - fs::path serverBuildDir; - utbot::ProjectContext projectContext; - fs::path buildCommandsJsonPath; - fs::path linkCommandsJsonPath; - fs::path compileCommandsJsonPath; - std::optional target; + const fs::path serverBuildDir; + const utbot::ProjectContext projectContext; + const fs::path buildCommandsJsonPath; + const fs::path linkCommandsJsonPath; + const fs::path compileCommandsJsonPath; + const fs::path target; CollectionUtils::MapFileTo>> sourceFileInfos; CollectionUtils::MapFileTo> objectFileInfos; CollectionUtils::MapFileTo> targetInfos; @@ -226,11 +226,11 @@ class BuildDatabase { static fs::path getCorrespondingBitcodeFile(const fs::path &filepath); void initObjects(const nlohmann::json &compileCommandsJson); void initInfo(const nlohmann::json &linkCommandsJson); - void createClangCompileCommandsJson(const fs::path &target, const fs::path &buildCommandsJsonPath); + void createClangCompileCommandsJson(const fs::path _target); void mergeLibraryOptions(std::vector &jsonArguments) const; fs::path newDirForFile(fs::path const& file) const; fs::path createExplicitObjectFileCompilationCommand(const std::shared_ptr &objectInfo); - CollectionUtils::FileSet getSourceFilesForTarget(const std::string &_target); + CollectionUtils::FileSet getSourceFilesForTarget(const fs::path &_target); using sharedLibrariesMap = std::unordered_map>; @@ -238,6 +238,7 @@ class BuildDatabase { BaseFileInfo &info, sharedLibrariesMap &sharedLibraryFiles, bool objectFiles = false); + bool isAutoTarget() const; }; #endif //UNITTESTBOT_BUILDDATABASE_H From 5d1015c06c6172a886593fdb319b30bb7e1e1a64 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Mon, 1 Aug 2022 18:29:28 +0300 Subject: [PATCH 07/27] not end --- server/src/building/BuildDatabase.cpp | 247 +++++++++++++------ server/src/building/BuildDatabase.h | 61 ++++- server/src/building/Linker.cpp | 24 +- server/src/building/Linker.h | 2 +- server/src/printers/TestMakefilesPrinter.cpp | 4 +- server/src/testgens/BaseTestGen.cpp | 23 +- server/src/testgens/BaseTestGen.h | 7 +- server/src/testgens/ProjectTestGen.h | 2 +- server/src/types/TypesResolver.cpp | 2 +- server/src/utils/GrpcUtils.cpp | 2 +- server/src/utils/path/FileSystemPath.h | 10 +- server/test/framework/Library_Test.cpp | 3 +- 12 files changed, 250 insertions(+), 137 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 1e750beb9..20c2bcd2e 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -34,7 +34,7 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath, BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext, - std::string _target) : + bool createClangCC) : serverBuildDir(std::move(_serverBuildDir)), projectContext(std::move(_projectContext)), buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), @@ -52,9 +52,81 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, filterInstalledFiles(); addLocalSharedLibraries(); fillTargetInfoParents(); - updateTarget(_target);, - target(std::move(_target)) - createClangCompileCommandsJson(_target); + if (createClangCC) { + createClangCompileCommandsJson(); + } + target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; +} + +BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, + fs::path _serverBuildDir, + utbot::ProjectContext _projectContext, + const std::string &_target) : + serverBuildDir(std::move(_serverBuildDir)), + projectContext(std::move(_projectContext)), + buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), + linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), + compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { + + + BuildDatabase autoTargetBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, false); + if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { + target = std::move(autoTargetBuildDatabase.target); + sourceFileInfos = std::move(autoTargetBuildDatabase.sourceFileInfos); + objectFileInfos = std::move(autoTargetBuildDatabase.objectFileInfos); + objectFileTargets = std::move(autoTargetBuildDatabase.objectFileTargets); + targetInfos = std::move(autoTargetBuildDatabase.targetInfos); + linkUnitToStubFiles = std::move(autoTargetBuildDatabase.linkUnitToStubFiles); + compileCommands_temp = std::move(autoTargetBuildDatabase.compileCommands_temp); + + createClangCompileCommandsJson(); + return; + } + + if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { + throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); + } + +// fs::path target; + { + auto new_target = GenerationUtils::findTarget(autoTargetBuildDatabase.getAllTargets(), _target); + if (new_target.has_value()) { + target = new_target.value(); + } else { + throw CompilationDatabaseException("Can't find target: " + _target); + } + } + +// CollectionUtils::MapFileTo>> sourceFileInfos; +// CollectionUtils::MapFileTo> objectFileInfos; +// CollectionUtils::MapFileTo> objectFileTargets; + { + auto objectFilesList = autoTargetBuildDatabase.getArchiveObjectFiles(target); + for (const auto &objectFilePath: objectFilesList) { + auto objectFileInfo = autoTargetBuildDatabase.getClientCompilationObjectInfo(objectFilePath); + sourceFileInfos[objectFileInfo->getSourcePath()].push_back(objectFileInfo); + LOG_IF_S(DEBUG, sourceFileInfos[objectFileInfo->getSourcePath()].size() > 1) + << "Multiple compile commands for file \"" << objectFileInfo->getSourcePath() << "\" in target \"" + << target.string() << "\""; + objectFileInfos[objectFileInfo->getOutputFile()] = objectFileInfo; + objectFileTargets[objectFileInfo->getOutputFile()] = + autoTargetBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()]; + } + } +// CollectionUtils::MapFileTo> targetInfos; +// CollectionUtils::MapFileTo linkUnitToStubFiles; + { + auto targetFilesList = autoTargetBuildDatabase.getArchiveTargetFiles(target); + for (const auto &objectFilePath: targetFilesList) { + targetInfos[objectFilePath] = autoTargetBuildDatabase.targetInfos[objectFilePath]; + linkUnitToStubFiles[objectFilePath] = autoTargetBuildDatabase.linkUnitToStubFiles[objectFilePath]; + } + } +// std::vector>> compileCommands_temp; + + compileCommands_temp = autoTargetBuildDatabase.compileCommands_temp; + + createClangCompileCommandsJson(); } std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext, @@ -187,8 +259,10 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { auto targetInfo = targetInfos[output]; if (targetInfo == nullptr) { targetInfo = targetInfos[output] = std::make_shared(); + } else { + LOG_S(WARNING) << "Multiple commands for one file: " << output.string(); } - for (nlohmann::json const &jsonFile : linkCommand.at("files")) { + for (nlohmann::json const &jsonFile: linkCommand.at("files")) { auto filename = jsonFile.get(); fs::path currentFile = Paths::getCCJsonFileFullPath(filename, command.getDirectory()); targetInfo->addFile(currentFile); @@ -204,62 +278,47 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { } } -void BuildDatabase::createClangCompileCommandsJson(const fs::path &_target) { - LOG_IF_S(ERROR, !isAutoTarget() && _target != target) << "Try change non-auto target"; - - CollectionUtils::FileSet targetFileSet = getArchiveObjectFiles(_target); +void BuildDatabase::createClangCompileCommandsJson() { + CollectionUtils::MapFileTo>> fileCompileCommands; + for (const auto &[compileCommand, objectInfo]: compileCommands_temp) { + const fs::path &sourcePath = objectInfo->getSourcePath(); + if (CollectionUtils::contains(fileCompileCommands, sourcePath)) { + LOG_S(WARNING) << "Multiple compile commands for file \"" << sourcePath + << "\" use command for \"" << objectInfo->getOutputFile() << "\""; + } else if (CollectionUtils::contains(objectFileInfos, objectInfo->getOutputFile())) { + fileCompileCommands[sourcePath] = {compileCommand, objectInfo}; + } + } nlohmann::json compileCommandsSingleFilesJson; - if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { - for (const auto &[compileCommand, objectInfo]: compileCommands_temp) { - compileCommandsSingleFilesJson.push_back(compileCommand); - } - } else { - CollectionUtils::MapFileTo>> fileCompileCommands; - for (const auto &[compileCommand, objectInfo]: compileCommands_temp) { - const fs::path &sourcePath = objectInfo->getSourcePath(); - if (CollectionUtils::contains(targetFileSet, objectInfo->getOutputFile()) && - (!CollectionUtils::containsKey(fileCompileCommands, sourcePath) || - conflictPriorityMore(objectInfo, fileCompileCommands[sourcePath].second))) { - fileCompileCommands[sourcePath] = {compileCommand, objectInfo}; - } - } - for (const auto &compileCommand: fileCompileCommands) { - compileCommandsSingleFilesJson.push_back(compileCommand.second.first); - } + for (const auto &compileCommand: fileCompileCommands) { + compileCommandsSingleFilesJson.push_back(compileCommand.second.first); } fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath); JsonUtils::writeJsonToFile(clangCompileCommandsJsonPath, compileCommandsSingleFilesJson); } -void BuildDatabase::updateTarget(const std::string &_target) { - if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { - return; - } - - auto new_target = GenerationUtils::findTarget(getAllTargets(), _target); - if (new_target.has_value()) { - target = new_target.value(); - } else { - throw CompilationDatabaseException("Can't find target: " + target); - } - - for (auto &[sourceFile, objectInfos]: sourceFileInfos) { - std::sort(objectInfos.begin(), objectInfos.end(), [&](const std::shared_ptr &left, - const std::shared_ptr &right) { - if (CollectionUtils::containsKey(targetInfos, target)) { - if (CollectionUtils::containsKey(targetInfos[target]->files, left->getOutputFile())) { - return true; - } - if (CollectionUtils::containsKey(targetInfos[target]->files, right->getOutputFile())) { - return false; - } - } - return false; - }); - } -} +//void BuildDatabase::updateTarget(const fs::path &_target) { +// if (_target.string() == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { +// return; +// } +// +// for (auto &[sourceFile, objectInfos]: sourceFileInfos) { +// std::sort(objectInfos.begin(), objectInfos.end(), [&](const std::shared_ptr &left, +// const std::shared_ptr &right) { +// if (CollectionUtils::containsKey(targetInfos, target)) { +// if (CollectionUtils::containsKey(targetInfos[target]->files, left->getOutputFile())) { +// return true; +// } +// if (CollectionUtils::containsKey(targetInfos[target]->files, right->getOutputFile())) { +// return false; +// } +// } +// return false; +// }); +// } +//} void BuildDatabase::mergeLibraryOptions(std::vector &jsonArguments) const { for (auto it = jsonArguments.begin(); it != jsonArguments.end(); it++) { @@ -457,17 +516,37 @@ CollectionUtils::FileSet BuildDatabase::getArchiveObjectFiles(const fs::path &ar auto archiveObjectFiles = getArchiveObjectFiles(file); CollectionUtils::extend(result, archiveObjectFiles); } else { - const fs::path &sourcePath = getClientCompilationUnitInfo(file)->getSourcePath(); + const fs::path &sourcePath = getClientCompilationObjectInfo(file)->getSourcePath(); if (Paths::isSourceFile(sourcePath)) { result.insert(file); } else { - LOG_S(WARNING) << "Skipping source file " << file; + LOG_S(WARNING) << "Skipping not c/c++ source file: " << sourcePath.string(); } } } return result; } +CollectionUtils::FileSet BuildDatabase::getArchiveTargetFiles(const fs::path &archive) const { + if (Paths::isGtest(archive)) { + return {}; + } + if (!CollectionUtils::containsKey(targetInfos, archive)) { + throw CompilationDatabaseException( + "Couldn't find current archive file linkage information for " + archive.string()); + } + std::shared_ptr targetInfo = targetInfos.at(archive); + CollectionUtils::FileSet result = {archive}; + for (const auto &file: targetInfo->files) { + if (Paths::isLibraryFile(file)) { + auto archiveObjectFiles = getArchiveTargetFiles(file); + result.insert(file); + CollectionUtils::extend(result, archiveObjectFiles); + } + } + return result; +} + fs::path BuildDatabase::getRootForSource(const fs::path& path) const { fs::path normalizedPath = Paths::normalizedTrimmed(path); if (Paths::isSourceFile(normalizedPath)) { @@ -518,19 +597,28 @@ fs::path BuildDatabase::getBitcodeFile(const fs::path &filepath) const { } } -std::shared_ptr -BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const { + +std::shared_ptr BuildDatabase::getClientCompilationObjectInfo(const fs::path &filepath) const { + if (!CollectionUtils::contains(objectFileInfos, filepath)) { + throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string()); + } + return objectFileInfos.at(filepath); +} + +std::shared_ptr BuildDatabase::getClientCompilationSourceInfo(const fs::path &filepath) const { + if (!CollectionUtils::contains(sourceFileInfos, filepath)) { + throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string()); + } + LOG_IF_S(DEBUG, sourceFileInfos.at(filepath).size() > 1) << "More than one compile command for: " << filepath; + return sourceFileInfos.at(filepath)[0]; +} + +std::shared_ptr BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const { if (Paths::isSourceFile(filepath)) { - if (!CollectionUtils::contains(sourceFileInfos, filepath)) { - throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string()); - } - return sourceFileInfos.at(filepath)[0]; + return getClientCompilationSourceInfo(filepath); } if (Paths::isObjectFile(filepath)) { - if (!CollectionUtils::contains(objectFileInfos, filepath)) { - throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string()); - } - return objectFileInfos.at(filepath); + return getClientCompilationObjectInfo(filepath); } throw CompilationDatabaseException("File is not a compilation unit or an object file: " + filepath.string()); } @@ -638,7 +726,7 @@ fs::path BuildDatabase::TargetInfo::getOutput() const { CollectionUtils::FileSet BuildDatabase::getStubFiles( const std::shared_ptr &linkUnitInfo) const { - auto iterator = linkUnitToStubFiles.find(linkUnitInfo); + auto iterator = linkUnitToStubFiles.find(linkUnitInfo->getOutput()); if (iterator != linkUnitToStubFiles.end()) { return iterator->second; } @@ -648,7 +736,7 @@ CollectionUtils::FileSet BuildDatabase::getStubFiles( void BuildDatabase::assignStubFilesToLinkUnit( std::shared_ptr linkUnitInfo, CollectionUtils::FileSet stubs) { - linkUnitToStubFiles.emplace(linkUnitInfo, std::move(stubs)); + linkUnitToStubFiles.emplace(linkUnitInfo->getOutput(), std::move(stubs)); } std::vector> BuildDatabase::getRootTargets() const { @@ -690,13 +778,16 @@ BuildDatabase::getTargetsForSourceFile(const fs::path &sourceFilePath) const { }); } -std::vector -BuildDatabase::autoTargetListForFile(const fs::path &sourceFilePath, const fs::path &objectFile) const { - - auto result = CollectionUtils::transformTo>(getTargetsForSourceFile(sourceFilePath), - [&](const std::shared_ptr &targetInfo) { - return targetInfo->getOutput(); - }); +std::vector BuildDatabase::targetListForFile(const fs::path &sourceFilePath, + const fs::path &objectFile) const { + if (hasAutoTarget()) { + return {target}; + } + auto result = CollectionUtils::transformTo>( + getTargetsForSourceFile(sourceFilePath), + [&](const std::shared_ptr &targetInfo) { + return targetInfo->getOutput(); + }); std::vector parents; if (CollectionUtils::containsKey(objectFileTargets, objectFile)) { parents = objectFileTargets.at(objectFile); @@ -745,7 +836,7 @@ fs::path BuildDatabase::newDirForFile(const fs::path &file) const { } CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path &_target) { - LOG_IF_S(ERROR, !isAutoTarget() && target != _target.c_str()) << "Try get sources for different target"; + LOG_IF_S(WARNING, !hasAutoTarget() && target != _target.c_str()) << "Try get sources for different target"; return CollectionUtils::transformTo( getArchiveObjectFiles(_target), [this](fs::path const &objectPath) { @@ -753,6 +844,10 @@ CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path & }); } -bool BuildDatabase::isAutoTarget() const { +bool BuildDatabase::hasAutoTarget() const { return target == GrpcUtils::UTBOT_AUTO_TARGET_PATH; } + +fs::path BuildDatabase::getTargetPath() const { + return target; +} diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 17cd491c0..cfd627600 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -98,18 +98,23 @@ class BuildDatabase { BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext, - std::string _target); + bool createClangCC = true); + + BuildDatabase(fs::path _buildCommandsJsonPath, + fs::path _serverBuildDir, + utbot::ProjectContext _projectContext, + const std::string &_target); static std::shared_ptr create(const utbot::ProjectContext &projectContext, const std::string &target); - void updateTarget(const std::string &target); +// void updateTarget(const fs::path &_target); const fs::path &getCompileCommandsJson(); const fs::path &getLinkCommandsJson(); /** - * @brief Returns all object files that are contained in a library or executable + * @brief Returns all object files files that are contained in a library or executable * * Recursively iterates over all libraries inside current library or executable. Returns all * found object files @@ -119,6 +124,35 @@ class BuildDatabase { */ [[nodiscard]] CollectionUtils::FileSet getArchiveObjectFiles(const fs::path &archive) const; + /** + * @brief Returns all target that are contained in a library or executable + * + * Recursively iterates over all libraries inside current library or executable. Returns all + * found target + * @param archive Executable or a library for which to find target files + * @return Set of paths to targets path. + * @throws CompilationDatabaseException if files are wrong + */ + [[nodiscard]] CollectionUtils::FileSet getArchiveTargetFiles(const fs::path &archive) const; + + /** + * @brief Returns compile command information for object file + * + * @param filepath Path to object file + * @return ObjectFileInfo for object file + * @throws CompilationDatabaseException if files are wrong + */ + [[nodiscard]] std::shared_ptr getClientCompilationObjectInfo(const fs::path &filepath) const; + + /** + * @brief Returns compile command information for current source file + * + * @param filepath Path to source file or object file + * @return ObjectFileInfo for current source file + * @throws CompilationDatabaseException if files are wrong + */ + [[nodiscard]] std::shared_ptr getClientCompilationSourceInfo(const fs::path &filepath) const; + /** * @brief Returns compile command information for current source file or object file * @@ -193,29 +227,32 @@ class BuildDatabase { std::vector> getAllTargets() const; std::vector - autoTargetListForFile(const fs::path &sourceFilePath, const fs::path &objectFile) const; + targetListForFile(const fs::path &sourceFilePath, const fs::path &objectFile) const; std::vector> - getTargetsForSourceFile(fs::path const&sourceFilePath) const; + getTargetsForSourceFile(fs::path const &sourceFilePath) const; std::shared_ptr getPriorityTarget() const; + + CollectionUtils::FileSet getSourceFilesForTarget(const fs::path &_target); + + bool hasAutoTarget() const; + + fs::path getTargetPath() const; private: const fs::path serverBuildDir; const utbot::ProjectContext projectContext; const fs::path buildCommandsJsonPath; const fs::path linkCommandsJsonPath; const fs::path compileCommandsJsonPath; - const fs::path target; + fs::path target; CollectionUtils::MapFileTo>> sourceFileInfos; CollectionUtils::MapFileTo> objectFileInfos; CollectionUtils::MapFileTo> targetInfos; CollectionUtils::MapFileTo> objectFileTargets; - - std::unordered_map, CollectionUtils::FileSet> - linkUnitToStubFiles; + CollectionUtils::MapFileTo linkUnitToStubFiles; std::vector>> compileCommands_temp; - std::vector>> linkCommands_temp; static bool conflictPriorityMore(const std::shared_ptr &left, const std::shared_ptr &right); @@ -226,11 +263,10 @@ class BuildDatabase { static fs::path getCorrespondingBitcodeFile(const fs::path &filepath); void initObjects(const nlohmann::json &compileCommandsJson); void initInfo(const nlohmann::json &linkCommandsJson); - void createClangCompileCommandsJson(const fs::path _target); + void createClangCompileCommandsJson(); void mergeLibraryOptions(std::vector &jsonArguments) const; fs::path newDirForFile(fs::path const& file) const; fs::path createExplicitObjectFileCompilationCommand(const std::shared_ptr &objectInfo); - CollectionUtils::FileSet getSourceFilesForTarget(const fs::path &_target); using sharedLibrariesMap = std::unordered_map>; @@ -238,7 +274,6 @@ class BuildDatabase { BaseFileInfo &info, sharedLibrariesMap &sharedLibraryFiles, bool objectFiles = false); - bool isAutoTarget() const; }; #endif //UNITTESTBOT_BUILDDATABASE_H diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index a30072b91..844edcef9 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -84,13 +84,13 @@ Result Linker::linkForTarget(const fs::path &target, const f return stubsSetResult; } -std::vector Linker::getTargetList(const fs::path &sourceFile, const fs::path &objectFile) const { - if (testGen.hasAutoTarget()) { - return testGen.buildDatabase->autoTargetListForFile(sourceFile, objectFile); - } else { - return {testGen.getTargetPath()}; - } -} +//std::vector Linker::getTargetList(const fs::path &sourceFile, const fs::path &objectFile) const { +// if (testGen.hasAutoTarget()) { +// return testGen.buildDatabase->targetListForFile(sourceFile, objectFile); +// } else { +// return {testGen.buildDatabase->getTargetPath()}; +// } +//} void Linker::linkForOneFile(const fs::path &sourceFilePath) { ExecUtils::throwIfCancelled(); @@ -104,7 +104,8 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { if (!testGen.buildDatabase->isFirstObjectFileForSource(objectFile)) { return; } - std::vector targets = getTargetList(sourceFilePath, objectFile); +// std::vector targets = getTargetList(sourceFilePath, objectFile); + std::vector targets = testGen.buildDatabase->targetListForFile(sourceFilePath, objectFile); LOG_S(DEBUG) << "Linking bitcode for file " << sourceFilePath.filename(); for (size_t i = 0; i < targets.size(); i++) { const auto& target = targets[i]; @@ -121,7 +122,7 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { if (i + 1 == targets.size()) { addToGenerated({ objectFile }, {}); fs::path possibleBitcodeFileName = - testGen.buildDatabase->getBitcodeFile(testGen.getTargetPath()); + testGen.buildDatabase->getBitcodeFile(testGen.buildDatabase->getTargetPath()); brokenLinkFiles.insert(possibleBitcodeFileName); } } @@ -129,7 +130,7 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { } Result Linker::linkWholeTarget(const fs::path &target) { - auto requestTarget = testGen.getTargetPath(); + auto requestTarget = testGen.buildDatabase->getTargetPath(); LOG_IF_S(ERROR, target != GrpcUtils::UTBOT_AUTO_TARGET_PATH && requestTarget != target) << "Try link target that not specified by user"; testGen.setTargetPath(target); @@ -183,7 +184,8 @@ void Linker::linkForProject() { << sourceFile; return; } - std::vector targets = getTargetList(sourceFile, objectFile); +// std::vector targets = getTargetList(sourceFile, objectFile); + std::vector targets = testGen.buildDatabase->targetListForFile(sourceFile, objectFile); bool success = false; for (const auto &target : targets) { if (!CollectionUtils::contains(triedTargets, target)) { diff --git a/server/src/building/Linker.h b/server/src/building/Linker.h index 907927ed5..d12b13b48 100644 --- a/server/src/building/Linker.h +++ b/server/src/building/Linker.h @@ -62,7 +62,7 @@ class Linker { bool isForOneFile(); - std::vector getTargetList(const fs::path &sourceFile, const fs::path &objectFile) const; +// std::vector getTargetList(const fs::path &sourceFile, const fs::path &objectFile) const; Result linkForTarget(const fs::path &target, const fs::path &sourceFilePath, const std::shared_ptr &compilationUnitInfo, diff --git a/server/src/printers/TestMakefilesPrinter.cpp b/server/src/printers/TestMakefilesPrinter.cpp index 27b15fe1a..8445b7406 100644 --- a/server/src/printers/TestMakefilesPrinter.cpp +++ b/server/src/printers/TestMakefilesPrinter.cpp @@ -35,7 +35,7 @@ namespace printer { TestMakefilesPrinter( testGen.projectContext, testGen.buildDatabase, - testGen.getTargetPath(), + testGen.buildDatabase->getTargetPath(), CompilationUtils::getBundledCompilerPath(CompilationUtils::getCompilerName( testGen.compilationDatabase->getBuildCompilerPath())), stubSources) { @@ -120,4 +120,4 @@ namespace printer { NativeMakefilePrinter(sharedMakefilePrinter, path).ss.str(), NativeMakefilePrinter(objMakefilePrinter, path).ss.str()}; } -} \ No newline at end of file +} diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index 34ac82c8d..412bfa6b1 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -45,30 +45,17 @@ void BaseTestGen::setInitializedTestsMap() { } void BaseTestGen::setTargetPath(fs::path _targetPath) { - if (targetPath != _targetPath) { - targetPath = std::move(_targetPath); - if (!hasAutoTarget()) { - updateTargetSources(); + if (buildDatabase->getTargetPath() != _targetPath) { + if (_targetPath != GrpcUtils::UTBOT_AUTO_TARGET_PATH) { + updateTargetSources(_targetPath); } } } -void BaseTestGen::updateTargetSources() { - buildDatabase->updateTarget(getTargetPath()); - targetSources = CollectionUtils::transformTo( - buildDatabase->getArchiveObjectFiles(getTargetPath()), [this](fs::path const &objectPath) { - return buildDatabase->getClientCompilationUnitInfo(objectPath)->getSourcePath(); - }); +void BaseTestGen::updateTargetSources(fs::path _targetPath) { + targetSources = buildDatabase->getSourceFilesForTarget(_targetPath); for (auto it = tests.begin(); it != tests.end(); it++) { tests::Tests &test = it.value(); test.isFilePresentedInCommands = CollectionUtils::contains(targetSources, test.sourceFilePath); } } - -fs::path const &BaseTestGen::getTargetPath() const { - return targetPath.value(); -} - -bool BaseTestGen::hasAutoTarget() const { - return getTargetPath() == GrpcUtils::UTBOT_AUTO_TARGET_PATH; -} diff --git a/server/src/testgens/BaseTestGen.h b/server/src/testgens/BaseTestGen.h index e781ba1d4..db677825a 100644 --- a/server/src/testgens/BaseTestGen.h +++ b/server/src/testgens/BaseTestGen.h @@ -40,9 +40,8 @@ class BaseTestGen { bool isBatched() const; -// bool hasAutoTarget() const; // fs::path const &getTargetPath() const; -// void setTargetPath(fs::path _targetPath); + void setTargetPath(fs::path _targetPath); virtual ~BaseTestGen() = default; protected: @@ -53,9 +52,9 @@ class BaseTestGen { void setInitializedTestsMap(); -// virtual void setTargetForSource(fs::path const& sourcePath) = 0; + virtual void setTargetForSource(fs::path const& sourcePath) = 0; - void updateTargetSources(); + void updateTargetSources(fs::path _targetPath); }; diff --git a/server/src/testgens/ProjectTestGen.h b/server/src/testgens/ProjectTestGen.h index 7dd09cd91..741b8b3c5 100644 --- a/server/src/testgens/ProjectTestGen.h +++ b/server/src/testgens/ProjectTestGen.h @@ -18,7 +18,7 @@ class ProjectTestGen : public BaseTestGen { const testsgen::ProjectRequest *getRequest() const; -// void setTargetForSource(fs::path const &sourcePath) override; + void setTargetForSource(fs::path const &sourcePath) override; private: testsgen::ProjectRequest const *const request; diff --git a/server/src/types/TypesResolver.cpp b/server/src/types/TypesResolver.cpp index bf235887c..8074498bb 100644 --- a/server/src/types/TypesResolver.cpp +++ b/server/src/types/TypesResolver.cpp @@ -20,7 +20,7 @@ static bool canBeReplaced(const std::string &nameInMap, const std::string &name) return nameInMap.empty() && !name.empty(); } -template +template bool isCandidateToReplace(uint64_t id, std::unordered_map &someMap, std::string const &name) { diff --git a/server/src/utils/GrpcUtils.cpp b/server/src/utils/GrpcUtils.cpp index fd11a0e82..b1483ecdc 100644 --- a/server/src/utils/GrpcUtils.cpp +++ b/server/src/utils/GrpcUtils.cpp @@ -2,7 +2,7 @@ namespace GrpcUtils { static const std::string UTBOT_AUTO_TARGET = "UTBot: auto"; -// const std::string UTBOT_AUTO_TARGET_PATH = fs::path("/utbot/auto/target/path"); + const std::string UTBOT_AUTO_TARGET_PATH = fs::path("/utbot/auto/target/path"); static inline const std::string UTBOT_AUTO_TARGET_DESCRIPTION = "Finds any target that contains the code under testing"; std::unique_ptr diff --git a/server/src/utils/path/FileSystemPath.h b/server/src/utils/path/FileSystemPath.h index d67fea6eb..b25a8cd23 100644 --- a/server/src/utils/path/FileSystemPath.h +++ b/server/src/utils/path/FileSystemPath.h @@ -13,17 +13,13 @@ namespace fs { class path { public: - path(const std::filesystem::path& p) : path_(normalizedTrimmed(p)) { - } + path(const std::filesystem::path &p) : path_(normalizedTrimmed(p)) {} path() {} - path(const std::string& s) : path_(normalizedTrimmed(s)) { - } - - path(const char *s) : path_(normalizedTrimmed(s)) { + path(const std::string &s) : path_(normalizedTrimmed(s)) {} - } + path(const char *s) : path_(normalizedTrimmed(s)) {} path root_path() const { return path(path_.root_path()); diff --git a/server/test/framework/Library_Test.cpp b/server/test/framework/Library_Test.cpp index 190540353..5637c7393 100644 --- a/server/test/framework/Library_Test.cpp +++ b/server/test/framework/Library_Test.cpp @@ -24,8 +24,7 @@ namespace { auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(pathToFile); - Status status = - Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); + Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); return { testGen, status }; } }; From 5214a084cf6ca65c0a7c34f5339335887214f9d1 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Mon, 1 Aug 2022 19:39:29 +0300 Subject: [PATCH 08/27] not end --- server/src/building/BuildDatabase.cpp | 163 +++++++++--------- server/src/building/BuildDatabase.h | 11 +- server/src/printers/NativeMakefilePrinter.cpp | 2 +- server/test/framework/KleeGen_Tests.cpp | 3 +- server/test/framework/Server_Tests.cpp | 3 +- 5 files changed, 95 insertions(+), 87 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 20c2bcd2e..4aae941e3 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -31,102 +31,111 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath, return fs::exists(fullFilePath) ? fullFilePath.string() : possibleFilePath; } -BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, - fs::path _serverBuildDir, - utbot::ProjectContext _projectContext, - bool createClangCC) : - serverBuildDir(std::move(_serverBuildDir)), - projectContext(std::move(_projectContext)), - buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), - linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), - compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { - if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { - throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); - } - - auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); - auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); - - initObjects(compileCommandsJson); - initInfo(linkCommandsJson); - filterInstalledFiles(); - addLocalSharedLibraries(); - fillTargetInfoParents(); - if (createClangCC) { - createClangCompileCommandsJson(); - } - target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; -} +//BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, +// fs::path _serverBuildDir, +// utbot::ProjectContext _projectContext, +// bool createClangCC) : +// serverBuildDir(std::move(_serverBuildDir)), +// projectContext(std::move(_projectContext)), +// buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), +// linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), +// compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { +// if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { +// throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); +// } +// +// auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); +// auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); +// +// initObjects(compileCommandsJson); +// initInfo(linkCommandsJson); +// filterInstalledFiles(); +// addLocalSharedLibraries(); +// fillTargetInfoParents(); +// if (createClangCC) { +// createClangCompileCommandsJson(); +// } +// target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; +//} BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext, - const std::string &_target) : + const std::string &_target, + bool createClangCC) : serverBuildDir(std::move(_serverBuildDir)), projectContext(std::move(_projectContext)), buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { + if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || target.empty()) { + if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { + throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); + } - BuildDatabase autoTargetBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, false); - if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { - target = std::move(autoTargetBuildDatabase.target); - sourceFileInfos = std::move(autoTargetBuildDatabase.sourceFileInfos); - objectFileInfos = std::move(autoTargetBuildDatabase.objectFileInfos); - objectFileTargets = std::move(autoTargetBuildDatabase.objectFileTargets); - targetInfos = std::move(autoTargetBuildDatabase.targetInfos); - linkUnitToStubFiles = std::move(autoTargetBuildDatabase.linkUnitToStubFiles); - compileCommands_temp = std::move(autoTargetBuildDatabase.compileCommands_temp); + auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); + auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); - createClangCompileCommandsJson(); - return; - } + initObjects(compileCommandsJson); + initInfo(linkCommandsJson); + filterInstalledFiles(); + addLocalSharedLibraries(); + fillTargetInfoParents(); + if (createClangCC) { + createClangCompileCommandsJson(); + } + target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; + } else { + BuildDatabase autoTargetBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, + GrpcUtils::UTBOT_AUTO_TARGET_PATH, false); - if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { - throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); - } + if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { + throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); + } -// fs::path target; - { - auto new_target = GenerationUtils::findTarget(autoTargetBuildDatabase.getAllTargets(), _target); - if (new_target.has_value()) { - target = new_target.value(); - } else { - throw CompilationDatabaseException("Can't find target: " + _target); + // fs::path target; + { + auto new_target = GenerationUtils::findTarget(autoTargetBuildDatabase.getAllTargets(), _target); + if (new_target.has_value()) { + target = new_target.value(); + } else { + throw CompilationDatabaseException("Can't find target: " + _target); + } } - } -// CollectionUtils::MapFileTo>> sourceFileInfos; -// CollectionUtils::MapFileTo> objectFileInfos; -// CollectionUtils::MapFileTo> objectFileTargets; - { - auto objectFilesList = autoTargetBuildDatabase.getArchiveObjectFiles(target); - for (const auto &objectFilePath: objectFilesList) { - auto objectFileInfo = autoTargetBuildDatabase.getClientCompilationObjectInfo(objectFilePath); - sourceFileInfos[objectFileInfo->getSourcePath()].push_back(objectFileInfo); - LOG_IF_S(DEBUG, sourceFileInfos[objectFileInfo->getSourcePath()].size() > 1) - << "Multiple compile commands for file \"" << objectFileInfo->getSourcePath() << "\" in target \"" - << target.string() << "\""; - objectFileInfos[objectFileInfo->getOutputFile()] = objectFileInfo; - objectFileTargets[objectFileInfo->getOutputFile()] = - autoTargetBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()]; + // CollectionUtils::MapFileTo>> sourceFileInfos; + // CollectionUtils::MapFileTo> objectFileInfos; + // CollectionUtils::MapFileTo> objectFileTargets; + { + auto objectFilesList = autoTargetBuildDatabase.getArchiveObjectFiles(target); + for (const auto &objectFilePath: objectFilesList) { + auto objectFileInfo = autoTargetBuildDatabase.getClientCompilationObjectInfo(objectFilePath); + sourceFileInfos[objectFileInfo->getSourcePath()].push_back(objectFileInfo); + LOG_IF_S(DEBUG, sourceFileInfos[objectFileInfo->getSourcePath()].size() > 1) + << "Multiple compile commands for file \"" << objectFileInfo->getSourcePath() << "\" in target \"" + << target.string() << "\""; + objectFileInfos[objectFileInfo->getOutputFile()] = objectFileInfo; + objectFileTargets[objectFileInfo->getOutputFile()] = + autoTargetBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()]; + } } - } -// CollectionUtils::MapFileTo> targetInfos; -// CollectionUtils::MapFileTo linkUnitToStubFiles; - { - auto targetFilesList = autoTargetBuildDatabase.getArchiveTargetFiles(target); - for (const auto &objectFilePath: targetFilesList) { - targetInfos[objectFilePath] = autoTargetBuildDatabase.targetInfos[objectFilePath]; - linkUnitToStubFiles[objectFilePath] = autoTargetBuildDatabase.linkUnitToStubFiles[objectFilePath]; + // CollectionUtils::MapFileTo> targetInfos; + // CollectionUtils::MapFileTo linkUnitToStubFiles; + { + auto targetFilesList = autoTargetBuildDatabase.getArchiveTargetFiles(target); + for (const auto &objectFilePath: targetFilesList) { + targetInfos[objectFilePath] = autoTargetBuildDatabase.targetInfos[objectFilePath]; + linkUnitToStubFiles[objectFilePath] = autoTargetBuildDatabase.linkUnitToStubFiles[objectFilePath]; + } } - } -// std::vector>> compileCommands_temp; + // std::vector>> compileCommands_temp; - compileCommands_temp = autoTargetBuildDatabase.compileCommands_temp; - - createClangCompileCommandsJson(); + compileCommands_temp = autoTargetBuildDatabase.compileCommands_temp; + } + if (createClangCC) { + createClangCompileCommandsJson(); + } } std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext, diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index cfd627600..854f8bec1 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -95,15 +95,16 @@ class BuildDatabase { }; public: - BuildDatabase(fs::path _buildCommandsJsonPath, - fs::path _serverBuildDir, - utbot::ProjectContext _projectContext, - bool createClangCC = true); +// BuildDatabase(fs::path _buildCommandsJsonPath, +// fs::path _serverBuildDir, +// utbot::ProjectContext _projectContext, +// bool createClangCC = true); BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext, - const std::string &_target); + const std::string &_target = "", + bool createClangCC = true); static std::shared_ptr create(const utbot::ProjectContext &projectContext, const std::string &target); diff --git a/server/src/printers/NativeMakefilePrinter.cpp b/server/src/printers/NativeMakefilePrinter.cpp index 4a0e026e8..66d5e544b 100644 --- a/server/src/printers/NativeMakefilePrinter.cpp +++ b/server/src/printers/NativeMakefilePrinter.cpp @@ -123,7 +123,7 @@ namespace printer { CollectionUtils::FileSet const *stubSources, std::map> pathToShellVariable) : RelativeMakefilePrinter(pathToShellVariable), - projectContext(std::move(projectContext)), buildDatabase(buildDatabase), rootPath(std::move(rootPath)), + projectContext(std::move(projectContext)), buildDatabase(std::move(buildDatabase)), rootPath(std::move(rootPath)), primaryCompiler(std::move(primaryCompiler)), primaryCxxCompiler(CompilationUtils::toCppCompiler(this->primaryCompiler)), primaryCompilerName(CompilationUtils::getCompilerName(this->primaryCompiler)), diff --git a/server/test/framework/KleeGen_Tests.cpp b/server/test/framework/KleeGen_Tests.cpp index 16e98506b..76dc14112 100644 --- a/server/test/framework/KleeGen_Tests.cpp +++ b/server/test/framework/KleeGen_Tests.cpp @@ -54,8 +54,7 @@ namespace { fs::path testsDirPath = tmpDirPath / "test"; utbot::ProjectContext projectContext{ suite.name, "", testsDirPath, buildDirRelativePath }; - auto buildDatabase = std::make_shared(suite.buildPath, suite.buildPath, projectContext, - GrpcUtils::UTBOT_AUTO_TARGET_PATH); + auto buildDatabase = std::make_shared(suite.buildPath, suite.buildPath, projectContext); utbot::SettingsContext settingsContext{ true, true, 15, 0, true, false }; KleeGenerator generator(std::move(projectContext), std::move(settingsContext), tmpDirPath, diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index 85c85fb0c..029ab94ef 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -64,8 +64,7 @@ namespace { void generateFiles(const fs::path &sourceFile, const utbot::ProjectContext &projectContext) { fs::path serverBuildDir = buildPath / "temp"; - auto buildDatabase = std::make_shared(buildPath, serverBuildDir, projectContext, - GrpcUtils::UTBOT_AUTO_TARGET_PATH); + auto buildDatabase = std::make_shared(buildPath, serverBuildDir, projectContext); fs::path compilerPath = CompilationUtils::getBundledCompilerPath(compilerName); CollectionUtils::FileSet stubsSources; fs::path root = buildDatabase->getRootForSource(sourceFile); From 9a05d4a2438a25b4aa7b2f75970c4635223630e5 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Tue, 2 Aug 2022 15:05:21 +0300 Subject: [PATCH 09/27] not end --- server/src/building/BuildDatabase.cpp | 8 +++--- server/test/framework/Library_Test.cpp | 5 ++-- server/test/framework/Regression_Tests.cpp | 8 +++--- server/test/framework/Server_Tests.cpp | 14 +++++----- server/test/framework/Stub_Tests.cpp | 30 +++++++++++----------- server/test/framework/Syntax_Tests.cpp | 8 +++--- server/test/framework/TestUtils.cpp | 29 ++++++++++----------- server/test/framework/TestUtils.h | 8 +++--- 8 files changed, 57 insertions(+), 53 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 4aae941e3..bf4e6a56e 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -69,7 +69,7 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { - if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || target.empty()) { + if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _target.empty()) { if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); } @@ -789,9 +789,9 @@ BuildDatabase::getTargetsForSourceFile(const fs::path &sourceFilePath) const { std::vector BuildDatabase::targetListForFile(const fs::path &sourceFilePath, const fs::path &objectFile) const { - if (hasAutoTarget()) { - return {target}; - } +// if (!hasAutoTarget()) { +// return {target}; +// } auto result = CollectionUtils::transformTo>( getTargetsForSourceFile(sourceFilePath), [&](const std::shared_ptr &targetInfo) { diff --git a/server/test/framework/Library_Test.cpp b/server/test/framework/Library_Test.cpp index 5637c7393..8a05843ad 100644 --- a/server/test/framework/Library_Test.cpp +++ b/server/test/framework/Library_Test.cpp @@ -20,10 +20,11 @@ namespace { std::pair createTestForFunction(const fs::path &pathToFile, int lineNum, int kleeTimeout = 60) { auto lineRequest = testUtils::createLineRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, pathToFile, lineNum, std::nullopt, true, false, kleeTimeout); + srcPaths, pathToFile, lineNum, + "", true, false, + kleeTimeout); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); - testGen.setTargetForSource(pathToFile); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); return { testGen, status }; } diff --git a/server/test/framework/Regression_Tests.cpp b/server/test/framework/Regression_Tests.cpp index 01a2b6373..f35d9fb43 100644 --- a/server/test/framework/Regression_Tests.cpp +++ b/server/test/framework/Regression_Tests.cpp @@ -28,19 +28,21 @@ namespace { std::pair createTestForFunction(const fs::path &pathToFile, int lineNum, bool verbose = true) { auto lineRequest = testUtils::createLineRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, pathToFile, lineNum, std::nullopt, false, verbose, 0); + srcPaths, pathToFile, lineNum, + GrpcUtils::UTBOT_AUTO_TARGET_PATH, false, verbose, 0); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(pathToFile); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); - return { testGen, status }; + return {testGen, status}; } std::pair createTestForFolder(const fs::path &pathToFolder, bool useStubs = true, bool verbose = true) { auto folderRequest = testUtils::createProjectRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, std::nullopt,useStubs, verbose, 0); + srcPaths, GrpcUtils::UTBOT_AUTO_TARGET_PATH, useStubs, + verbose, 0); auto request = GrpcUtils::createFolderRequest(std::move(folderRequest), pathToFolder); auto testGen = FolderTestGen(*request, writer.get(), TESTMODE); diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index 029ab94ef..3096bcf73 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -1001,7 +1001,7 @@ namespace { TEST_P(Parameterized_Server_Test, Line_Test1) { auto request = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 17, std::nullopt, false, false, 0); + basic_functions_c, 17, GrpcUtils::UTBOT_AUTO_TARGET_PATH, false, false, 0); auto testGen = LineTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(basic_functions_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -1018,7 +1018,7 @@ namespace { TEST_P(Parameterized_Server_Test, Line_Test2) { auto request = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 17, std::nullopt, false, false, 0); + basic_functions_c, 17, GrpcUtils::UTBOT_AUTO_TARGET_PATH, false, false, 0); auto testGen = LineTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(basic_functions_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -1081,7 +1081,7 @@ namespace { TEST_P(Parameterized_Server_Test, Function_Test) { auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 6, std::nullopt, false, false, 0); + basic_functions_c, 6, GrpcUtils::UTBOT_AUTO_TARGET_PATH, false, false, 0); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(basic_functions_c); @@ -1109,7 +1109,7 @@ namespace { TEST_P(Parameterized_Server_Test, Predicate_Test_Integer) { auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 17, std::nullopt, false, false, 0); + basic_functions_c, 17, GrpcUtils::UTBOT_AUTO_TARGET_PATH, false, false, 0); auto predicateInfo = std::make_unique(); predicateInfo->set_predicate("=="); predicateInfo->set_returnvalue("36"); @@ -1132,7 +1132,7 @@ namespace { TEST_P(Parameterized_Server_Test, Predicate_Test_Str) { auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - basic_functions_c, 32, std::nullopt, false, false, 0); + basic_functions_c, 32, GrpcUtils::UTBOT_AUTO_TARGET_PATH, false, false, 0); auto predicateInfo = std::make_unique(); predicateInfo->set_predicate("=="); predicateInfo->set_returnvalue("abacaba"); @@ -1156,7 +1156,7 @@ namespace { TEST_P(Parameterized_Server_Test, Symbolic_Stdin_Test) { auto request = std::make_unique(); auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - symbolic_stdin_c, 8, std::nullopt, false, false, 0); + symbolic_stdin_c, 8, GrpcUtils::UTBOT_AUTO_TARGET_PATH, false, false, 0); request->set_allocated_linerequest(lineRequest.release()); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(symbolic_stdin_c); @@ -1178,7 +1178,7 @@ namespace { TEST_P(Parameterized_Server_Test, Symbolic_Stdin_Long_Read) { auto request = std::make_unique(); auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - symbolic_stdin_c, 19, std::nullopt, false, false, 0); + symbolic_stdin_c, 19, GrpcUtils::UTBOT_AUTO_TARGET_PATH, false, false, 0); request->set_allocated_linerequest(lineRequest.release()); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(symbolic_stdin_c); diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index ff02862f2..5abde02bb 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -111,9 +111,10 @@ namespace { TEST_F(Stub_Test, Project_Stubs_Test) { auto stubsWriter = std::make_unique(nullptr, false); - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, std::nullopt, true); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = std::make_unique(*request, writer.get(), TESTMODE); - std::vector stubSources = { calc_sum_c, calc_mult_c, literals_foo_c }; + std::vector stubSources = {calc_sum_c, calc_mult_c, literals_foo_c}; Status status = Server::TestsGenServiceImpl::ProcessProjectStubsRequest(testGen.get(), stubsWriter.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -122,7 +123,7 @@ namespace { TEST_F(Stub_Test, Implicit_Stubs_Test) { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - literals_foo_c, std::nullopt, true); + literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -135,7 +136,7 @@ namespace { TEST_F(Stub_Test, Pregenerated_Stubs_Test) { { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - literals_foo_c, std::nullopt, true); + literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -146,7 +147,7 @@ namespace { { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, literals_foo_c, std::nullopt, true); + srcPaths, literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -159,7 +160,8 @@ namespace { TEST_F(Stub_Test, Multimodule_Lib_Heuristic_Test) { auto request = testUtils::createProjectRequest(projectName, suitePath, buildDirRelativePath, - { foreign, calc, suitePath, literals }, std::nullopt, true); + {foreign, calc, suitePath, literals}, + GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(foreign_bar_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -179,11 +181,9 @@ namespace { EXPECT_EQ(expectedStubFiles, stubFiles); } - - TEST_F(Stub_Test, File_Tests_With_Stubs) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, literals_foo_c, std::nullopt, true); + srcPaths, literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -218,7 +218,7 @@ namespace { TEST_F(Stub_Test, Run_Tests_For_Unused_Function) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, calc_sum_c, std::nullopt, true); + srcPaths, calc_sum_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(calc_sum_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -239,7 +239,7 @@ namespace { TEST_F(Stub_Test, File_Tests_Without_Stubs) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, literals_foo_c, std::nullopt, false); + srcPaths, literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, false); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -270,7 +270,7 @@ namespace { } TEST_F(Stub_Test, DISABLED_Sync_Stub_When_Source_Changed_Test) { - auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, literals_foo_c, std::nullopt, true); + auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -279,11 +279,11 @@ namespace { modifySources(sourcesToModify); std::string stubCode = modifyStubFile(sum_stub_c); - auto request2 = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, literals_foo_c, std::nullopt, true); + auto request2 = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen2 = FileTestGen(*request2, writer.get(), TESTMODE); { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, - literals_foo_c, std::nullopt, true); + literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); @@ -292,7 +292,7 @@ namespace { } { auto request = createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, literals_foo_c, std::nullopt, true); + srcPaths, literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(literals_foo_c); diff --git a/server/test/framework/Syntax_Tests.cpp b/server/test/framework/Syntax_Tests.cpp index d6562ad39..38c806794 100644 --- a/server/test/framework/Syntax_Tests.cpp +++ b/server/test/framework/Syntax_Tests.cpp @@ -81,7 +81,8 @@ namespace { std::pair createTestForFunction(const fs::path &pathToFile, int lineNum, int kleeTimeout = 60) { auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, pathToFile, lineNum, std::nullopt, false, false, kleeTimeout); + srcPaths, pathToFile, lineNum, GrpcUtils::UTBOT_AUTO_TARGET_PATH, + false, false, kleeTimeout); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(pathToFile); @@ -2199,7 +2200,8 @@ namespace { TEST_F(Syntax_Test, Run_Tests_For_Linked_List) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, linked_list_c, std::nullopt, true, false); + srcPaths, linked_list_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true, + false); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(linked_list_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); @@ -2232,7 +2234,7 @@ namespace { TEST_F(Syntax_Test, Run_Tests_For_Tree) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, tree_c, std::nullopt, true, false); + srcPaths, tree_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true, false); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); testGen.setTargetForSource(tree_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); diff --git a/server/test/framework/TestUtils.cpp b/server/test/framework/TestUtils.cpp index 60df5cdc2..07997bb80 100644 --- a/server/test/framework/TestUtils.cpp +++ b/server/test/framework/TestUtils.cpp @@ -212,7 +212,7 @@ namespace testUtils { const fs::path &projectPath, const std::string &buildDirRelativePath, const std::vector &srcPaths, - const std::optional &target, + const std::string &target, bool useStubs, bool verbose, int kleeTimeout) { @@ -222,21 +222,20 @@ namespace testUtils { GrpcUtils::createSettingsContext(true, verbose, kleeTimeout, 0, false, useStubs); - auto buildDatabase = BuildDatabase::create(utbot::ProjectContext(*projectContext), - GrpcUtils::UTBOT_AUTO_TARGET_PATH); - auto rootTargets = buildDatabase->getRootTargets(); - auto it = std::find_if(rootTargets.begin(), rootTargets.end(), - [&target](std::shared_ptr linkUnitInfo) { - return linkUnitInfo->getOutput().filename() == target; - }); - assert(it != rootTargets.end()); - std::string new_target = it->get()->getOutput(); - +// auto buildDatabase = BuildDatabase::create(utbot::ProjectContext(*projectContext), +// GrpcUtils::UTBOT_AUTO_TARGET_PATH); +// auto rootTargets = buildDatabase->getRootTargets(); +// auto it = std::find_if(rootTargets.begin(), rootTargets.end(), +// [&target](std::shared_ptr linkUnitInfo) { +// return linkUnitInfo->getOutput().filename() == target; +// }); +// assert(it != rootTargets.end()); +// std::string new_target = it->get()->getOutput(); return GrpcUtils::createProjectRequest(std::move(projectContext), std::move(settingsContext), srcPaths, - new_target); + target); } std::unique_ptr createFileRequest(const std::string &projectName, @@ -244,7 +243,7 @@ namespace testUtils { const std::string &buildDirRelativePath, const std::vector &srcPaths, const fs::path &filePath, - const std::optional &target, + const std::string &target, bool useStubs, bool verbose, int kleeTimeout) { @@ -259,7 +258,7 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, - const std::optional &target, + const std::string &target, bool useStubs, bool verbose, int kleeTimeout) { @@ -275,7 +274,7 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, - const std::optional &target, + const std::string &target, bool useStubs, bool verbose, int kleeTimeout) { diff --git a/server/test/framework/TestUtils.h b/server/test/framework/TestUtils.h index ab399eab1..aa782936f 100644 --- a/server/test/framework/TestUtils.h +++ b/server/test/framework/TestUtils.h @@ -68,7 +68,7 @@ namespace testUtils { const fs::path &projectPath, const std::string &buildDirRelativePath, const std::vector &srcPaths, - const std::optional &target = std::nullopt, + const std::string &target = GrpcUtils::UTBOT_AUTO_TARGET_PATH, bool useStubs = false, bool verbose = true, int kleeTimeout = 60); @@ -78,7 +78,7 @@ namespace testUtils { const std::string &buildDirRelativePath, const std::vector &srcPaths, const fs::path &filePath, - const std::optional &target = std::nullopt, + const std::string &target = GrpcUtils::UTBOT_AUTO_TARGET_PATH, bool useStubs = false, bool verbose = true, int kleeTimeout = 60); @@ -89,7 +89,7 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, - const std::optional &target = std::nullopt, + const std::string &target = GrpcUtils::UTBOT_AUTO_TARGET_PATH, bool useStubs = false, bool verbose = true, int kleeTimeout = 60); @@ -100,7 +100,7 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, - const std::optional &target = std::nullopt, + const std::string &target = GrpcUtils::UTBOT_AUTO_TARGET_PATH, bool useStubs = false, bool verbose = true, int kleeTimeout = 60); From 090b9409153a288103a1ccdd281b9c550a9e33d5 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Tue, 2 Aug 2022 18:30:03 +0300 Subject: [PATCH 10/27] not end --- server/src/building/BuildDatabase.cpp | 11 +++++------ server/test/framework/Library_Test.cpp | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index bf4e6a56e..d27a675e4 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -89,13 +89,12 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, } else { BuildDatabase autoTargetBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, GrpcUtils::UTBOT_AUTO_TARGET_PATH, false); - - if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { - throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); - } - // fs::path target; - { + //TODO target incorrect name now + if (Paths::isSourceFile(_target)) { + fs::path root = autoTargetBuildDatabase.getRootForSource(_target); + target = root; + } else { auto new_target = GenerationUtils::findTarget(autoTargetBuildDatabase.getAllTargets(), _target); if (new_target.has_value()) { target = new_target.value(); diff --git a/server/test/framework/Library_Test.cpp b/server/test/framework/Library_Test.cpp index 8a05843ad..21d63481e 100644 --- a/server/test/framework/Library_Test.cpp +++ b/server/test/framework/Library_Test.cpp @@ -21,7 +21,7 @@ namespace { createTestForFunction(const fs::path &pathToFile, int lineNum, int kleeTimeout = 60) { auto lineRequest = testUtils::createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, pathToFile, lineNum, - "", true, false, + pathToFile, true, false, kleeTimeout); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); From eb8617666b001ad452180f5e81862fa1295b96dc Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Wed, 3 Aug 2022 18:44:06 +0300 Subject: [PATCH 11/27] not end --- server/src/Server.cpp | 7 +- server/src/Server.h | 4 - server/src/building/BuildDatabase.cpp | 167 ++++++++---------- server/src/building/BuildDatabase.h | 18 +- server/src/building/IRParser.cpp | 3 + server/src/building/Linker.cpp | 6 +- server/src/stubs/StubGen.cpp | 4 +- server/src/stubs/StubSourcesFinder.cpp | 2 +- server/src/testgens/BaseTestGen.cpp | 10 +- server/src/testgens/BaseTestGen.h | 2 +- server/src/testgens/ProjectTestGen.cpp | 5 +- server/src/testgens/SnippetTestGen.cpp | 5 +- server/src/utils/GenerationUtils.cpp | 4 +- server/src/utils/GenerationUtils.h | 16 +- server/test/framework/KleeGen_Tests.cpp | 2 +- server/test/framework/Regression_Tests.cpp | 8 +- server/test/framework/Server_Tests.cpp | 75 ++++---- server/test/framework/Stub_Tests.cpp | 9 +- server/test/framework/Targets_Test.cpp | 4 +- server/test/framework/TestUtils.cpp | 8 +- server/test/framework/TestUtils.h | 2 +- .../suites/library-project/CMakeLists.txt | 6 +- .../suites/library-project/lib/CMakeLists.txt | 2 +- .../test/suites/stub/lib/calc/CMakeLists.txt | 2 +- server/test/suites/targets/CMakeLists.txt | 7 +- 25 files changed, 183 insertions(+), 195 deletions(-) diff --git a/server/src/Server.cpp b/server/src/Server.cpp index 7883c7e1e..af4ff23eb 100644 --- a/server/src/Server.cpp +++ b/server/src/Server.cpp @@ -579,8 +579,7 @@ Status Server::TestsGenServiceImpl::PrintModulesContent(ServerContext *context, utbot::ProjectContext projectContext{ *request }; fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); - std::shared_ptr buildDatabase = BuildDatabase::create(projectContext, - GrpcUtils::UTBOT_AUTO_TARGET_PATH); + std::shared_ptr buildDatabase = BuildDatabase::create(projectContext); StubSourcesFinder(buildDatabase).printAllModules(); return Status::OK; } @@ -655,7 +654,7 @@ Status Server::TestsGenServiceImpl::GetProjectTargets(ServerContext *context, try { utbot::ProjectContext projectContext{ request->projectcontext() }; - auto buildDatabase = BuildDatabase::create(projectContext, GrpcUtils::UTBOT_AUTO_TARGET_PATH); + auto buildDatabase = BuildDatabase::create(projectContext); auto targets = buildDatabase->getAllTargets(); ProjectTargetsWriter targetsWriter{ response }; targetsWriter.writeResponse(projectContext, targets); @@ -677,7 +676,7 @@ Status Server::TestsGenServiceImpl::GetFileTargets(ServerContext *context, try { utbot::ProjectContext projectContext{ request->projectcontext() }; - auto buildDatabase = BuildDatabase::create(projectContext, GrpcUtils::UTBOT_AUTO_TARGET_PATH); + auto buildDatabase = BuildDatabase::create(projectContext); fs::path path = request->path(); auto targets = buildDatabase->getTargetsForSourceFile(path); FileTargetsWriter targetsWriter{ response }; diff --git a/server/src/Server.h b/server/src/Server.h index 121f79194..fd9c22dcc 100644 --- a/server/src/Server.h +++ b/server/src/Server.h @@ -116,10 +116,6 @@ class Server { MEASURE_FUNCTION_EXECUTION_TIME TestGenT testGen{ request, testsWriter.get(), testMode }; - if constexpr (std::is_base_of_v) { - fs::path targetPath = testGen.getRequest()->targetpath(); - testGen.setTargetPath(targetPath); - } TimeExecStatistics::clearStatistic(); Status status = ProcessBaseTestRequest(testGen, testsWriter.get()); TimeExecStatistics::printStatistic(); diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index d27a675e4..2d89e1d93 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -31,120 +31,104 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath, return fs::exists(fullFilePath) ? fullFilePath.string() : possibleFilePath; } -//BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, -// fs::path _serverBuildDir, -// utbot::ProjectContext _projectContext, -// bool createClangCC) : -// serverBuildDir(std::move(_serverBuildDir)), -// projectContext(std::move(_projectContext)), -// buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), -// linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), -// compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { -// if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { -// throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); -// } -// -// auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); -// auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); -// -// initObjects(compileCommandsJson); -// initInfo(linkCommandsJson); -// filterInstalledFiles(); -// addLocalSharedLibraries(); -// fillTargetInfoParents(); -// if (createClangCC) { -// createClangCompileCommandsJson(); -// } -// target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; -//} - BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext, - const std::string &_target, bool createClangCC) : serverBuildDir(std::move(_serverBuildDir)), projectContext(std::move(_projectContext)), buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { + if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { + throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); + } - if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _target.empty()) { - if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { - throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); - } + auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); + auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); - auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); - auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); + initObjects(compileCommandsJson); + initInfo(linkCommandsJson); + filterInstalledFiles(); + addLocalSharedLibraries(); + fillTargetInfoParents(); + if (createClangCC) { + createClangCompileCommandsJson(); + } + target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; +} - initObjects(compileCommandsJson); - initInfo(linkCommandsJson); - filterInstalledFiles(); - addLocalSharedLibraries(); - fillTargetInfoParents(); - if (createClangCC) { - createClangCompileCommandsJson(); - } - target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; +BuildDatabase::BuildDatabase(BuildDatabase& baseBuildDatabase, + const std::string &_target) : + serverBuildDir(baseBuildDatabase.serverBuildDir), + projectContext(baseBuildDatabase.projectContext), + buildCommandsJsonPath(baseBuildDatabase.buildCommandsJsonPath), + linkCommandsJsonPath(baseBuildDatabase.linkCommandsJsonPath), + compileCommandsJsonPath(baseBuildDatabase.compileCommandsJsonPath) { + +// BuildDatabase baseBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, false); + + +// fs::path target; + //TODO target incorrect name now + if (Paths::isSourceFile(_target)) { + fs::path root = baseBuildDatabase.getRootForSource(_target); + target = root; + } else if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _target.empty()) { + fs::path root = baseBuildDatabase.getRootForFirstSource(); + target = root; } else { - BuildDatabase autoTargetBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, - GrpcUtils::UTBOT_AUTO_TARGET_PATH, false); - // fs::path target; - //TODO target incorrect name now - if (Paths::isSourceFile(_target)) { - fs::path root = autoTargetBuildDatabase.getRootForSource(_target); - target = root; + auto new_target = GenerationUtils::findTarget(baseBuildDatabase.getAllTargets(), _target); + if (new_target.has_value()) { + target = new_target.value(); } else { - auto new_target = GenerationUtils::findTarget(autoTargetBuildDatabase.getAllTargets(), _target); - if (new_target.has_value()) { - target = new_target.value(); - } else { - throw CompilationDatabaseException("Can't find target: " + _target); - } + throw CompilationDatabaseException("Can't find target: " + _target); } + } - // CollectionUtils::MapFileTo>> sourceFileInfos; - // CollectionUtils::MapFileTo> objectFileInfos; - // CollectionUtils::MapFileTo> objectFileTargets; - { - auto objectFilesList = autoTargetBuildDatabase.getArchiveObjectFiles(target); - for (const auto &objectFilePath: objectFilesList) { - auto objectFileInfo = autoTargetBuildDatabase.getClientCompilationObjectInfo(objectFilePath); - sourceFileInfos[objectFileInfo->getSourcePath()].push_back(objectFileInfo); - LOG_IF_S(DEBUG, sourceFileInfos[objectFileInfo->getSourcePath()].size() > 1) - << "Multiple compile commands for file \"" << objectFileInfo->getSourcePath() << "\" in target \"" - << target.string() << "\""; - objectFileInfos[objectFileInfo->getOutputFile()] = objectFileInfo; - objectFileTargets[objectFileInfo->getOutputFile()] = - autoTargetBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()]; - } - } - // CollectionUtils::MapFileTo> targetInfos; - // CollectionUtils::MapFileTo linkUnitToStubFiles; - { - auto targetFilesList = autoTargetBuildDatabase.getArchiveTargetFiles(target); - for (const auto &objectFilePath: targetFilesList) { - targetInfos[objectFilePath] = autoTargetBuildDatabase.targetInfos[objectFilePath]; - linkUnitToStubFiles[objectFilePath] = autoTargetBuildDatabase.linkUnitToStubFiles[objectFilePath]; - } - } - // std::vector>> compileCommands_temp; - compileCommands_temp = autoTargetBuildDatabase.compileCommands_temp; +// CollectionUtils::MapFileTo>> sourceFileInfos; +// CollectionUtils::MapFileTo> objectFileInfos; +// CollectionUtils::MapFileTo> objectFileTargets; + { + auto objectFilesList = baseBuildDatabase.getArchiveObjectFiles(target); + for (const auto &objectFilePath: objectFilesList) { + auto objectFileInfo = baseBuildDatabase.getClientCompilationObjectInfo(objectFilePath); + sourceFileInfos[objectFileInfo->getSourcePath()].push_back(objectFileInfo); + LOG_IF_S(DEBUG, sourceFileInfos[objectFileInfo->getSourcePath()].size() > 1) + << "Multiple compile commands for file \"" << objectFileInfo->getSourcePath() << "\" in target \"" + << target.string() << "\""; + objectFileInfos[objectFileInfo->getOutputFile()] = objectFileInfo; + objectFileTargets[objectFileInfo->getOutputFile()] = + baseBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()]; + } } - if (createClangCC) { - createClangCompileCommandsJson(); +// CollectionUtils::MapFileTo> targetInfos; + { + auto targetFilesList = baseBuildDatabase.getArchiveTargetFiles(target); + for (const auto &objectFilePath: targetFilesList) { + targetInfos[objectFilePath] = baseBuildDatabase.targetInfos[objectFilePath]; + } } +// CollectionUtils::MapFileTo linkUnitToStubFiles; + linkUnitToStubFiles = baseBuildDatabase.linkUnitToStubFiles; + +// std::vector>> compileCommands_temp; + compileCommands_temp = baseBuildDatabase.compileCommands_temp; + + createClangCompileCommandsJson(); +} + +std::shared_ptr BuildDatabase::createBaseForTarget(const std::string &_target) { + return std::make_shared(std::move(BuildDatabase(*this, _target))); } -std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext, - const std::string &target) { +std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext) { fs::path compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( projectContext.projectPath, projectContext.buildDirRelativePath); fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); - std::shared_ptr buildDatabase = - std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, target); + std::shared_ptr buildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, true); return buildDatabase; } @@ -581,6 +565,11 @@ fs::path BuildDatabase::getRootForSource(const fs::path& path) const { } } + +fs::path BuildDatabase::getRootForFirstSource() const { + return getRootForSource(sourceFileInfos.begin()->first); +} + fs::path BuildDatabase::getBitcodeForSource(const fs::path &sourceFile) const { fs::path serverBuildObjectFilePath = newDirForFile(sourceFile); return Paths::addExtension(serverBuildObjectFilePath, ".bc"); diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 854f8bec1..ff04fc48b 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -95,21 +95,13 @@ class BuildDatabase { }; public: -// BuildDatabase(fs::path _buildCommandsJsonPath, -// fs::path _serverBuildDir, -// utbot::ProjectContext _projectContext, -// bool createClangCC = true); - BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext, - const std::string &_target = "", - bool createClangCC = true); - - static std::shared_ptr create(const utbot::ProjectContext &projectContext, - const std::string &target); + bool createClangCC); -// void updateTarget(const fs::path &_target); + static std::shared_ptr create(const utbot::ProjectContext &projectContext); + std::shared_ptr createBaseForTarget(const std::string &target); const fs::path &getCompileCommandsJson(); const fs::path &getLinkCommandsJson(); @@ -181,6 +173,7 @@ class BuildDatabase { * @throws CompilationDatabaseException if file is wrong */ [[nodiscard]] fs::path getRootForSource(const fs::path &path) const; + [[nodiscard]] fs::path getRootForFirstSource() const; [[nodiscard]] fs::path getBitcodeForSource(const fs::path &sourceFile) const; [[nodiscard]] fs::path getBitcodeFile(const fs::path &filepath) const; @@ -241,6 +234,9 @@ class BuildDatabase { fs::path getTargetPath() const; private: + BuildDatabase(BuildDatabase& baseBuildDatabase, + const std::string &_target); + const fs::path serverBuildDir; const utbot::ProjectContext projectContext; const fs::path buildCommandsJsonPath; diff --git a/server/src/building/IRParser.cpp b/server/src/building/IRParser.cpp index e602bf902..c460921bc 100644 --- a/server/src/building/IRParser.cpp +++ b/server/src/building/IRParser.cpp @@ -21,6 +21,9 @@ bool IRParser::parseModule(const fs::path &rootBitcode, tests::TestsMap &tests) llvm::LLVMContext context; auto module = getModule(rootBitcode, context); if (module) { + for(const auto& fff : module->getFunctionList()) { + LOG_S(ERROR) << "function ffffffffff: " << fff.getName().str(); + } for (auto it = tests.begin(); it != tests.end(); it++) { fs::path const &sourceFile = it.key(); tests::Tests &test = it.value(); diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 844edcef9..7b1cd2241 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -54,7 +54,7 @@ fs::path Linker::getSourceFilePath() { Result Linker::linkForTarget(const fs::path &target, const fs::path &sourceFilePath, const std::shared_ptr &compilationUnitInfo, const fs::path &objectFile) { - testGen.setTargetPath(target); +// testGen.setTargetPath(target); auto siblings = testGen.buildDatabase->getArchiveObjectFiles(target); auto stubSources = stubGen.getStubSources(target); @@ -133,7 +133,7 @@ Result Linker::linkWholeTarget(const fs::path &target) { auto requestTarget = testGen.buildDatabase->getTargetPath(); LOG_IF_S(ERROR, target != GrpcUtils::UTBOT_AUTO_TARGET_PATH && requestTarget != target) << "Try link target that not specified by user"; - testGen.setTargetPath(target); +// testGen.setTargetPath(target); auto targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); auto siblings = testGen.buildDatabase->getArchiveObjectFiles(target); @@ -164,7 +164,7 @@ Result Linker::linkWholeTarget(const fs::path &target) { kleeGenerator->buildByCDb(siblingObjectsToBuild, stubSources); auto result = link(filesToLink, target, "", std::nullopt, stubSources, false); //this is done in order to restore testGen.target in case of UTBot: auto - testGen.setTargetPath(requestTarget); +// testGen.setTargetPath(requestTarget); return result; } diff --git a/server/src/stubs/StubGen.cpp b/server/src/stubs/StubGen.cpp index 3f0e86ace..5cf28c67c 100644 --- a/server/src/stubs/StubGen.cpp +++ b/server/src/stubs/StubGen.cpp @@ -18,9 +18,9 @@ CollectionUtils::FileSet StubGen::getStubSources(const fs::path &target) { return {}; } fs::path testedFilePath = *testGen.testingMethodsSourcePaths.begin(); - auto stubSources = StubSourcesFinder(testGen.buildDatabase).excludeFind(testedFilePath, target); + auto stubSources = StubSourcesFinder(testGen.baseBuildDatabase).excludeFind(testedFilePath, target); return { stubSources.begin(), stubSources.end() }; -}; +} CollectionUtils::FileSet StubGen::findStubFilesBySignatures(const std::vector &signatures) { diff --git a/server/src/stubs/StubSourcesFinder.cpp b/server/src/stubs/StubSourcesFinder.cpp index 3c3b882d4..a06e51d03 100644 --- a/server/src/stubs/StubSourcesFinder.cpp +++ b/server/src/stubs/StubSourcesFinder.cpp @@ -19,7 +19,7 @@ std::vector StubSourcesFinder::find(const fs::path& testedFilePath) { } std::vector StubSourcesFinder::excludeFind(const fs::path &testedFilePath, - const fs::path &rootPath) { + const fs::path &rootPath) { auto allBitcodeFiles = buildDatabase->getArchiveObjectFiles(rootPath); CollectionUtils::FileSet libraryBitcodeFiles = getLibraryBitcodeFiles(testedFilePath); std::vector stubSources; diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index 412bfa6b1..d85c28998 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -45,11 +45,11 @@ void BaseTestGen::setInitializedTestsMap() { } void BaseTestGen::setTargetPath(fs::path _targetPath) { - if (buildDatabase->getTargetPath() != _targetPath) { - if (_targetPath != GrpcUtils::UTBOT_AUTO_TARGET_PATH) { - updateTargetSources(_targetPath); - } - } +// if (buildDatabase->getTargetPath() != _targetPath) { +// if (_targetPath != GrpcUtils::UTBOT_AUTO_TARGET_PATH) { +// updateTargetSources(_targetPath); +// } +// } } void BaseTestGen::updateTargetSources(fs::path _targetPath) { diff --git a/server/src/testgens/BaseTestGen.h b/server/src/testgens/BaseTestGen.h index db677825a..7e4ee2e81 100644 --- a/server/src/testgens/BaseTestGen.h +++ b/server/src/testgens/BaseTestGen.h @@ -24,6 +24,7 @@ class BaseTestGen { fs::path compileCommandsJsonPath; std::shared_ptr compilationDatabase; + std::shared_ptr baseBuildDatabase; std::shared_ptr buildDatabase; CollectionUtils::FileSet sourcePaths, testingMethodsSourcePaths; @@ -40,7 +41,6 @@ class BaseTestGen { bool isBatched() const; -// fs::path const &getTargetPath() const; void setTargetPath(fs::path _targetPath); virtual ~BaseTestGen() = default; diff --git a/server/src/testgens/ProjectTestGen.cpp b/server/src/testgens/ProjectTestGen.cpp index a316e4f5b..c56851fd4 100644 --- a/server/src/testgens/ProjectTestGen.cpp +++ b/server/src/testgens/ProjectTestGen.cpp @@ -16,8 +16,8 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, fs::create_directories(projectContext.testDirPath); compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( projectContext.projectPath, projectContext.buildDirRelativePath); - buildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, - request.targetpath()); + baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, false); + buildDatabase = baseBuildDatabase->createBaseForTarget(request.targetpath()); compilationDatabase = CompilationUtils::getCompilationDatabase(compileCommandsJsonPath); if (autoSrcPaths) { autoDetectSourcePathsIfNotEmpty(); @@ -26,6 +26,7 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, } testingMethodsSourcePaths = sourcePaths; setInitializedTestsMap(); + updateTargetSources(buildDatabase->getTargetPath()); } std::string ProjectTestGen::toString() { diff --git a/server/src/testgens/SnippetTestGen.cpp b/server/src/testgens/SnippetTestGen.cpp index 09a7faedc..f1fc765a7 100644 --- a/server/src/testgens/SnippetTestGen.cpp +++ b/server/src/testgens/SnippetTestGen.cpp @@ -18,9 +18,8 @@ SnippetTestGen::SnippetTestGen(const testsgen::SnippetRequest &request, printer::CCJsonPrinter::createDummyBuildDB(sourcePaths, serverBuildDir); compileCommandsJsonPath = serverBuildDir; utbot::ProjectContext projectContext{request, serverBuildDir}; - buildDatabase = - std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, - serverBuildDir / SNIPPET_TARGET); + baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, false); + buildDatabase = baseBuildDatabase->createBaseForTarget(serverBuildDir / SNIPPET_TARGET); compilationDatabase = CompilationUtils::getCompilationDatabase(serverBuildDir); setTargetForSource(filePath); setInitializedTestsMap(); diff --git a/server/src/utils/GenerationUtils.cpp b/server/src/utils/GenerationUtils.cpp index 494bb59a6..6a8e983d5 100644 --- a/server/src/utils/GenerationUtils.cpp +++ b/server/src/utils/GenerationUtils.cpp @@ -62,11 +62,11 @@ GenerationUtils::findTarget(const std::vectorgetOutput(); - return !(output == name || output.stem() == name || output.stem() == "lib" + name); + return !(output == name || output.stem() == name || output.stem() == "lib" + name || output.filename() == name); }); if (candidates.empty()) { std::stringstream ss; - ss << "Couldn't find appropriate target. List of available targets:\n"; + ss << "Couldn't find \"" << name << "\" target. List of available targets:\n"; for (const auto &target : allTargets) { ss << target->getOutput() << "\n"; } diff --git a/server/src/utils/GenerationUtils.h b/server/src/utils/GenerationUtils.h index 07223fa33..f8c9908fd 100644 --- a/server/src/utils/GenerationUtils.h +++ b/server/src/utils/GenerationUtils.h @@ -38,14 +38,14 @@ namespace GenerationUtils { ServerUtils::setThreadOptions(ctx, true); auto testsWriter = std::make_unique(); auto testGen = std::make_unique(request, testsWriter.get(), true); - if constexpr (std::is_base_of_v) { - auto targetPath = findTarget(*testGen, testGen->getRequest()->targetpath()); - if (!targetPath.has_value()) { - auto status = grpc::Status(grpc::INVALID_ARGUMENT, "Couldn't find appropriate target"); - return std::make_pair(std::move(testGen), status); - } - testGen->setTargetPath(targetPath.value()); - } +// if constexpr (std::is_base_of_v) { +// auto targetPath = findTarget(*testGen, testGen->getRequest()->targetpath()); +// if (!targetPath.has_value()) { +// auto status = grpc::Status(grpc::INVALID_ARGUMENT, "Couldn't find appropriate target"); +// return std::make_pair(std::move(testGen), status); +// } +// testGen->setTargetPath(targetPath.value()); +// } Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(*testGen, testsWriter.get()); if (status.error_code() == grpc::FAILED_PRECONDITION) { diff --git a/server/test/framework/KleeGen_Tests.cpp b/server/test/framework/KleeGen_Tests.cpp index 76dc14112..968d1f082 100644 --- a/server/test/framework/KleeGen_Tests.cpp +++ b/server/test/framework/KleeGen_Tests.cpp @@ -54,7 +54,7 @@ namespace { fs::path testsDirPath = tmpDirPath / "test"; utbot::ProjectContext projectContext{ suite.name, "", testsDirPath, buildDirRelativePath }; - auto buildDatabase = std::make_shared(suite.buildPath, suite.buildPath, projectContext); + auto buildDatabase = std::make_shared(suite.buildPath, suite.buildPath, projectContext, true); utbot::SettingsContext settingsContext{ true, true, 15, 0, true, false }; KleeGenerator generator(std::move(projectContext), std::move(settingsContext), tmpDirPath, diff --git a/server/test/framework/Regression_Tests.cpp b/server/test/framework/Regression_Tests.cpp index f35d9fb43..bf1c30121 100644 --- a/server/test/framework/Regression_Tests.cpp +++ b/server/test/framework/Regression_Tests.cpp @@ -29,10 +29,9 @@ namespace { createTestForFunction(const fs::path &pathToFile, int lineNum, bool verbose = true) { auto lineRequest = testUtils::createLineRequest(projectName, suitePath, buildDirRelativePath, srcPaths, pathToFile, lineNum, - GrpcUtils::UTBOT_AUTO_TARGET_PATH, false, verbose, 0); + pathToFile, false, verbose, 0); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); - testGen.setTargetForSource(pathToFile); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); return {testGen, status}; @@ -90,10 +89,11 @@ namespace { TEST_F(Regression_Test, Incomplete_Array_Type) { fs::path folderPath = suitePath / "SAT-760"; auto projectRequest = testUtils::createProjectRequest( - projectName, suitePath, buildDirRelativePath, { suitePath, folderPath }); + projectName, suitePath, buildDirRelativePath, { suitePath, folderPath }, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto request = GrpcUtils::createFolderRequest(std::move(projectRequest), folderPath); auto testGen = FolderTestGen(*request, writer.get(), TESTMODE); - testUtils::setTargetForFirstSource(testGen); +// testUtils::setTargetForFirstSource(testGen); fs::path source1 = folderPath / "SAT-760_1.c"; fs::path source2 = folderPath / "SAT-760_2.c"; diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index 3096bcf73..bf5aea359 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -64,10 +64,11 @@ namespace { void generateFiles(const fs::path &sourceFile, const utbot::ProjectContext &projectContext) { fs::path serverBuildDir = buildPath / "temp"; - auto buildDatabase = std::make_shared(buildPath, serverBuildDir, projectContext); + auto buildDatabase = BuildDatabase(buildPath, serverBuildDir, projectContext, false).createBaseForTarget( + sourceFile); fs::path compilerPath = CompilationUtils::getBundledCompilerPath(compilerName); CollectionUtils::FileSet stubsSources; - fs::path root = buildDatabase->getRootForSource(sourceFile); + fs::path root = buildDatabase->getTargetPath(); printer::TestMakefilesPrinter testMakefilePrinter(projectContext, buildDatabase, root, compilerPath, &stubsSources); testMakefilePrinter.addLinkTargetRecursively(root, ""); @@ -630,9 +631,10 @@ namespace { TEST_F(Server_Test, Char_Literals_Test) { std::string suite = "char"; setSuite(suite); - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -653,10 +655,9 @@ namespace { fs::path b_c = getTestFilePath("b.c"); fs::path main_c = getTestFilePath("main.c"); { - auto request = - createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "ex"); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -673,11 +674,10 @@ namespace { } })); } { - auto request = - createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "one"); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - fs::path one = testGen.buildDatabase->getClientLinkUnitInfo(a_c)->getOutput(); - testGen.setTargetPath(one); +// fs::path one = testGen.buildDatabase->getClientLinkUnitInfo(a_c)->getOutput(); +// testGen.setTargetPath(one); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -691,10 +691,10 @@ namespace { } { auto request = - createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "two"); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - fs::path two = testGen.buildDatabase->getClientLinkUnitInfo(b_c)->getOutput(); - testGen.setTargetPath(two); +// fs::path two = testGen.buildDatabase->getClientLinkUnitInfo(b_c)->getOutput(); +// testGen.setTargetPath(two); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -713,9 +713,10 @@ namespace { TEST_F(Server_Test, Datacom_Test) { std::string suite = "datacom"; setSuite(suite); - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -908,9 +909,10 @@ namespace { std::string suite = "small-project"; setSuite(suite); srcPaths = {suitePath, suitePath / "lib", suitePath / "src"}; - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -929,9 +931,10 @@ namespace { std::string suite = "small-project"; setSuite(suite); srcPaths = {}; - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -953,9 +956,10 @@ namespace { std::string suite = "small-project"; setSuite(suite); srcPaths = { suitePath / "lib"}; - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -987,11 +991,11 @@ namespace { } TEST_P(Parameterized_Server_Test, Folder_Test) { - auto projectRequest = - createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto request = GrpcUtils::createFolderRequest(std::move(projectRequest), suitePath / "inner"); auto testGen = FolderTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -1454,11 +1458,11 @@ namespace { std::string suite = "object-file"; setSuite(suite); static const std::string source2_c = getTestFilePath("source2.c"); - auto projectRequest = - createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), source2_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -1569,9 +1573,10 @@ namespace { TEST_P(Parameterized_Server_Test, Clang_Resources_Directory_Test) { std::string suite = "stddef"; setSuite(suite); - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -1582,9 +1587,10 @@ namespace { TEST_P(Parameterized_Server_Test, Installed_Dependency_Test) { std::string suite = "installed"; setSuite(suite); - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -1599,9 +1605,10 @@ namespace { std::string suite = "small-project"; setSuite(suite); srcPaths = {}; - auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); + auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, + GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - setTargetForFirstSource(testGen); +// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index 5abde02bb..be23f7424 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -161,16 +161,15 @@ namespace { TEST_F(Stub_Test, Multimodule_Lib_Heuristic_Test) { auto request = testUtils::createProjectRequest(projectName, suitePath, buildDirRelativePath, {foreign, calc, suitePath, literals}, - GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); + foreign_bar_c, true); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - testGen.setTargetForSource(foreign_bar_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 2); - auto root = testGen.buildDatabase->getRootForSource(foreign_bar_c); - auto linkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(root); - auto stubFiles = testGen.buildDatabase->getStubFiles(linkUnitInfo); + auto root = testGen.baseBuildDatabase->getRootForSource(foreign_bar_c); + auto linkUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(root); + auto stubFiles = testGen.baseBuildDatabase->getStubFiles(linkUnitInfo); utbot::ProjectContext projectContext{ projectName, suitePath, getTestDirectory(), buildDirRelativePath }; auto stubCandidates = { calc_sum_c }; diff --git a/server/test/framework/Targets_Test.cpp b/server/test/framework/Targets_Test.cpp index 80a151d68..c103b11f1 100644 --- a/server/test/framework/Targets_Test.cpp +++ b/server/test/framework/Targets_Test.cpp @@ -70,7 +70,7 @@ TEST_F(TargetsTest, Valid_Target_Test_dummy) { // testGen.setTargetPath(dummy); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); - ASSERT_FALSE(status.ok()); + ASSERT_TRUE(status.ok()) << status.error_message(); int numberOfTests = testUtils::getNumberOfTests(testGen.tests); EXPECT_EQ(0, numberOfTests); @@ -84,7 +84,7 @@ TEST_F(TargetsTest, Valid_Target_Test_parse) { // testGen.setTargetPath(autoTarget); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); - ASSERT_TRUE(status.ok()); + ASSERT_TRUE(status.ok()) << status.error_message(); checkMinNumberOfTestsInFile(testGen, parse_c, 1); checkTestCasePredicates( testGen.tests.at(parse_c).methods.begin().value().testCases, diff --git a/server/test/framework/TestUtils.cpp b/server/test/framework/TestUtils.cpp index 07997bb80..0bbe4bd50 100644 --- a/server/test/framework/TestUtils.cpp +++ b/server/test/framework/TestUtils.cpp @@ -409,10 +409,10 @@ namespace testUtils { return argv; } - void setTargetForFirstSource(ProjectTestGen &testGen) { - fs::path sourcePath = *testGen.testingMethodsSourcePaths.begin(); - testGen.setTargetForSource(sourcePath); - } +// void setTargetForFirstSource(ProjectTestGen &testGen) { +// fs::path sourcePath = *testGen.testingMethodsSourcePaths.begin(); +// testGen.setTargetForSource(sourcePath); +// } static void checkStatsCSV(const fs::path &statsPath, const std::vector &header, const std::vector &containedFiles) { diff --git a/server/test/framework/TestUtils.h b/server/test/framework/TestUtils.h index aa782936f..a7a88b98d 100644 --- a/server/test/framework/TestUtils.h +++ b/server/test/framework/TestUtils.h @@ -134,7 +134,7 @@ namespace testUtils { std::vector createArgvVector(const std::vector &args); - void setTargetForFirstSource(ProjectTestGen &testGen); +// void setTargetForFirstSource(ProjectTestGen &testGen); void checkGenerationStatsCSV(const fs::path &statsPath, const std::vector &containedFiles); diff --git a/server/test/suites/library-project/CMakeLists.txt b/server/test/suites/library-project/CMakeLists.txt index b5f2d1e55..a78fc66dd 100644 --- a/server/test/suites/library-project/CMakeLists.txt +++ b/server/test/suites/library-project/CMakeLists.txt @@ -6,10 +6,10 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -set(CMAKE_EXPORT_LINK_COMMANDS ON) +#set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +#set(CMAKE_EXPORT_LINK_COMMANDS ON) add_compile_options(-O2) add_subdirectory(lib) -add_subdirectory(src) \ No newline at end of file +add_subdirectory(src) diff --git a/server/test/suites/library-project/lib/CMakeLists.txt b/server/test/suites/library-project/lib/CMakeLists.txt index 4091512fe..f9081eeb8 100644 --- a/server/test/suites/library-project/lib/CMakeLists.txt +++ b/server/test/suites/library-project/lib/CMakeLists.txt @@ -1,4 +1,4 @@ add_library(lib) target_include_directories(lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) file(GLOB_RECURSE ALL_SOURCES "*.c" "*.h") -target_sources(lib PRIVATE ${ALL_SOURCES}) \ No newline at end of file +target_sources(lib PUBLIC ${ALL_SOURCES}) diff --git a/server/test/suites/stub/lib/calc/CMakeLists.txt b/server/test/suites/stub/lib/calc/CMakeLists.txt index 31068f05e..f7f4c7fca 100644 --- a/server/test/suites/stub/lib/calc/CMakeLists.txt +++ b/server/test/suites/stub/lib/calc/CMakeLists.txt @@ -1,3 +1,3 @@ add_library(calc) target_include_directories(calc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -target_sources(calc PRIVATE sum.c mult.c PUBLIC sum.h mult.h) \ No newline at end of file +target_sources(calc PRIVATE sum.c mult.c PUBLIC sum.h mult.h) diff --git a/server/test/suites/targets/CMakeLists.txt b/server/test/suites/targets/CMakeLists.txt index f9915ebf3..b2efbe687 100644 --- a/server/test/suites/targets/CMakeLists.txt +++ b/server/test/suites/targets/CMakeLists.txt @@ -13,6 +13,9 @@ add_library(parse STATIC parse.c) add_executable(ls ls.c) add_executable(cat cat.c) add_executable(dummy dummy.c) +target_link_libraries(ls parse) +target_link_libraries(cat parse) +target_link_libraries(dummy parse) add_executable(get_10 get_10.c shared.c get_val_main.c) add_executable(get_20 get_20x.c shared.c get_val_main.c) @@ -22,7 +25,3 @@ add_library(shared_get SHARED get_20x.c shared.c get_val_main.c) add_library(static_get STATIC get_20x.c shared.c get_val_main.c) target_compile_definitions(static_get PRIVATE TESTDEF) - -target_link_libraries(ls parse) -target_link_libraries(cat parse) -target_link_libraries(dummy parse) From a656b63931ee618ddbc87fd2838bef0a5a4d1fe0 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 4 Aug 2022 15:54:12 +0300 Subject: [PATCH 12/27] not end --- server/src/building/BuildDatabase.cpp | 4 ++-- server/src/building/IRParser.cpp | 3 --- server/src/building/Linker.cpp | 4 ++-- server/src/stubs/StubGen.cpp | 2 ++ server/src/stubs/StubSourcesFinder.cpp | 3 +-- server/src/testgens/BaseTestGen.cpp | 3 +++ server/test/framework/Stub_Tests.cpp | 10 ++++------ 7 files changed, 14 insertions(+), 15 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 2d89e1d93..ca517c1f2 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -104,14 +104,14 @@ BuildDatabase::BuildDatabase(BuildDatabase& baseBuildDatabase, } } // CollectionUtils::MapFileTo> targetInfos; +// CollectionUtils::MapFileTo linkUnitToStubFiles; { auto targetFilesList = baseBuildDatabase.getArchiveTargetFiles(target); for (const auto &objectFilePath: targetFilesList) { targetInfos[objectFilePath] = baseBuildDatabase.targetInfos[objectFilePath]; + linkUnitToStubFiles[objectFilePath] = baseBuildDatabase.linkUnitToStubFiles[objectFilePath]; } } -// CollectionUtils::MapFileTo linkUnitToStubFiles; - linkUnitToStubFiles = baseBuildDatabase.linkUnitToStubFiles; // std::vector>> compileCommands_temp; compileCommands_temp = baseBuildDatabase.compileCommands_temp; diff --git a/server/src/building/IRParser.cpp b/server/src/building/IRParser.cpp index c460921bc..e602bf902 100644 --- a/server/src/building/IRParser.cpp +++ b/server/src/building/IRParser.cpp @@ -21,9 +21,6 @@ bool IRParser::parseModule(const fs::path &rootBitcode, tests::TestsMap &tests) llvm::LLVMContext context; auto module = getModule(rootBitcode, context); if (module) { - for(const auto& fff : module->getFunctionList()) { - LOG_S(ERROR) << "function ffffffffff: " << fff.getName().str(); - } for (auto it = tests.begin(); it != tests.end(); it++) { fs::path const &sourceFile = it.key(); tests::Tests &test = it.value(); diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 7b1cd2241..1ce7664e5 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -114,7 +114,7 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { if (result.isSuccess()) { auto [targetBitcode, stubsSet, _] = result.getOpt().value(); addToGenerated({ objectFile }, targetBitcode); - auto&& targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); + auto&& targetUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(target); testGen.buildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, stubsSet); return; } else { @@ -202,7 +202,7 @@ void Linker::linkForProject() { return compilationUnitInfo->getOutputFile(); }); addToGenerated(objectFiles, linkres.bitcodeOutput); - auto &&targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); + auto &&targetUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(target); testGen.buildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, linkres.stubsSet); break; } else { diff --git a/server/src/stubs/StubGen.cpp b/server/src/stubs/StubGen.cpp index 5cf28c67c..681daa113 100644 --- a/server/src/stubs/StubGen.cpp +++ b/server/src/stubs/StubGen.cpp @@ -15,6 +15,8 @@ StubGen::StubGen(BaseTestGen &testGen) : testGen(testGen) { CollectionUtils::FileSet StubGen::getStubSources(const fs::path &target) { if (!testGen.needToBeMocked() || !testGen.settingsContext.useStubs) { + LOG_S(ERROR) << "?????? " << testGen.needToBeMocked() + << " " << testGen.settingsContext.useStubs; return {}; } fs::path testedFilePath = *testGen.testingMethodsSourcePaths.begin(); diff --git a/server/src/stubs/StubSourcesFinder.cpp b/server/src/stubs/StubSourcesFinder.cpp index a06e51d03..c3f231d41 100644 --- a/server/src/stubs/StubSourcesFinder.cpp +++ b/server/src/stubs/StubSourcesFinder.cpp @@ -18,8 +18,7 @@ std::vector StubSourcesFinder::find(const fs::path& testedFilePath) { return stubSources; } -std::vector StubSourcesFinder::excludeFind(const fs::path &testedFilePath, - const fs::path &rootPath) { +std::vector StubSourcesFinder::excludeFind(const fs::path &testedFilePath, const fs::path &rootPath) { auto allBitcodeFiles = buildDatabase->getArchiveObjectFiles(rootPath); CollectionUtils::FileSet libraryBitcodeFiles = getLibraryBitcodeFiles(testedFilePath); std::vector stubSources; diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index d85c28998..99aed0670 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -17,6 +17,9 @@ BaseTestGen::BaseTestGen(const testsgen::ProjectContext &projectContext, } bool BaseTestGen::needToBeMocked() const { + LOG_S(ERROR) << "?????? " << TypeUtils::isSameType(*this) + << " " << TypeUtils::isSameType(*this) + << " " << TypeUtils::isSameType(*this); return TypeUtils::isSameType(*this) || TypeUtils::isSameType(*this) || TypeUtils::isSameType(*this); diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index be23f7424..4209b9c79 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -170,21 +170,19 @@ namespace { auto root = testGen.baseBuildDatabase->getRootForSource(foreign_bar_c); auto linkUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(root); auto stubFiles = testGen.baseBuildDatabase->getStubFiles(linkUnitInfo); - utbot::ProjectContext projectContext{ projectName, suitePath, getTestDirectory(), - buildDirRelativePath }; auto stubCandidates = { calc_sum_c }; auto expectedStubFiles = CollectionUtils::transformTo( - stubCandidates, [&projectContext](fs::path const &path) { - return Paths::sourcePathToStubPath(projectContext, path); + stubCandidates, [&testGen](fs::path const &path) { + return Paths::sourcePathToStubPath(testGen.projectContext, path); }); EXPECT_EQ(expectedStubFiles, stubFiles); } TEST_F(Stub_Test, File_Tests_With_Stubs) { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, literals_foo_c, GrpcUtils::UTBOT_AUTO_TARGET_PATH, true); + srcPaths, literals_foo_c, literals_foo_c, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); - testGen.setTargetForSource(literals_foo_c); +// testGen.setTargetForSource(literals_foo_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 5); From 83fbfd0a9cff08ded9494576922b83d4ea9f8999 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 4 Aug 2022 18:42:41 +0300 Subject: [PATCH 13/27] not end --- server/src/ReturnTypesFetcher.cpp | 2 +- server/src/Server.cpp | 12 ++++++------ server/src/Synchronizer.cpp | 8 ++++---- server/src/building/BuildDatabase.cpp | 1 + server/src/building/BuildDatabase.h | 3 +++ server/src/building/Linker.cpp | 4 ++-- server/src/printers/TestMakefilesPrinter.cpp | 2 +- server/src/stubs/StubGen.cpp | 2 +- server/src/testgens/BaseTestGen.cpp | 3 --- server/src/testgens/BaseTestGen.h | 2 +- server/src/testgens/ProjectTestGen.cpp | 6 +++--- server/src/testgens/SnippetTestGen.cpp | 2 +- server/test/framework/Stub_Tests.cpp | 6 +++--- 13 files changed, 27 insertions(+), 26 deletions(-) diff --git a/server/src/ReturnTypesFetcher.cpp b/server/src/ReturnTypesFetcher.cpp index c3d138e71..28f4b99fd 100644 --- a/server/src/ReturnTypesFetcher.cpp +++ b/server/src/ReturnTypesFetcher.cpp @@ -10,7 +10,7 @@ void ReturnTypesFetcher::fetch(ProgressWriter *const progressWriter, testsMap[filePath]; } Fetcher(Fetcher::Options::Value::RETURN_TYPE_NAMES_ONLY, - testGen->compilationDatabase, testsMap, nullptr, nullptr, nullptr, + testGen->buildDatabase->compilationDatabase, testsMap, nullptr, nullptr, nullptr, testGen->compileCommandsJsonPath, false) .fetchWithProgress(progressWriter, "Fetching return types for functions", true); for (auto const &[sourceFilePath, test] : testsMap) { diff --git a/server/src/Server.cpp b/server/src/Server.cpp index af4ff23eb..ef8b16c34 100644 --- a/server/src/Server.cpp +++ b/server/src/Server.cpp @@ -198,11 +198,11 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, static std::string logMessage = "Traversing sources AST tree and fetching declarations."; LOG_S(DEBUG) << logMessage; Fetcher fetcher(Fetcher::Options::Value::ALL, - testGen.compilationDatabase, testGen.tests, &testGen.types, + testGen.buildDatabase->compilationDatabase, testGen.tests, &testGen.types, &sizeContext.pointerSize, &sizeContext.maximumAlignment, testGen.compileCommandsJsonPath, false); fetcher.fetchWithProgress(testGen.progressWriter, logMessage); - SourceToHeaderRewriter(testGen.projectContext, testGen.compilationDatabase, + SourceToHeaderRewriter(testGen.projectContext, testGen.buildDatabase->compilationDatabase, fetcher.getStructsToDeclare(), testGen.serverBuildDir) .generateTestHeaders(testGen.tests, testGen.progressWriter); types::TypesHandler typesHandler{ testGen.types, sizeContext }; @@ -218,7 +218,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, if (lineTestGen != nullptr) { if (isSameType(testGen) && Paths::isHeaderFile(lineTestGen->filePath)) { BordersFinder classFinder(lineTestGen->filePath, lineTestGen->line, - lineTestGen->compilationDatabase, + lineTestGen->buildDatabase->compilationDatabase, lineTestGen->compileCommandsJsonPath); classFinder.findClass(); lineInfo = std::make_shared(classFinder.getLineInfo()); @@ -255,7 +255,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, } auto generator = std::make_shared( testGen.projectContext, testGen.settingsContext, - testGen.serverBuildDir, testGen.compilationDatabase, typesHandler, + testGen.serverBuildDir, testGen.buildDatabase->compilationDatabase, typesHandler, pathSubstitution, testGen.buildDatabase, testGen.progressWriter); ReturnTypesFetcher returnTypesFetcher{ &testGen }; @@ -311,7 +311,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, std::shared_ptr Server::TestsGenServiceImpl::getLineInfo(LineTestGen &lineTestGen) { BordersFinder stmtFinder(lineTestGen.filePath, lineTestGen.line, - lineTestGen.compilationDatabase, + lineTestGen.buildDatabase->compilationDatabase, lineTestGen.compileCommandsJsonPath); stmtFinder.findFunction(); if (!stmtFinder.getLineInfo().initialized) { @@ -551,7 +551,7 @@ Status Server::TestsGenServiceImpl::ProcessProjectStubsRequest(BaseTestGen *test static std::string logMessage = "Traversing sources AST tree and fetching declarations."; LOG_S(DEBUG) << logMessage; Fetcher fetcher(Fetcher::Options::Value::TYPE | Fetcher::Options::Value::FUNCTION, - testGen->compilationDatabase, testGen->tests, &testGen->types, + testGen->buildDatabase->compilationDatabase, testGen->tests, &testGen->types, &sizeContext.pointerSize, &sizeContext.maximumAlignment, testGen->compileCommandsJsonPath, false); diff --git a/server/src/Synchronizer.cpp b/server/src/Synchronizer.cpp index 9435e0719..c592e8103 100644 --- a/server/src/Synchronizer.cpp +++ b/server/src/Synchronizer.cpp @@ -145,7 +145,7 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, auto options = Fetcher::Options::Value::FUNCTION | Fetcher::Options::Value::INCLUDE | Fetcher::Options::Value::TYPE; auto stubFetcher = - Fetcher(options, testGen->compilationDatabase, sourceFilesMap, &testGen->types, + Fetcher(options, testGen->buildDatabase->compilationDatabase, sourceFilesMap, &testGen->types, &sizeContext->pointerSize, &sizeContext->maximumAlignment, testGen->compileCommandsJsonPath, false); @@ -157,7 +157,7 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, auto stubsCdb = createStubsCompilationDatabase(stubFiles, ccJsonStubDirPath); auto sourceToHeaderRewriter = - SourceToHeaderRewriter(testGen->projectContext, testGen->compilationDatabase, + SourceToHeaderRewriter(testGen->projectContext, testGen->buildDatabase->compilationDatabase, stubFetcher.getStructsToDeclare(), testGen->serverBuildDir); for (const StubOperator &outdatedStub : outdatedStubs) { @@ -203,14 +203,14 @@ void Synchronizer::synchronizeWrappers(const CollectionUtils::FileSet &outdatedS sourceFilesNeedToRegenerateWrappers, testGen->progressWriter, "Generating wrappers", [this](fs::path const &sourceFilePath) { SourceToHeaderRewriter sourceToHeaderRewriter(testGen->projectContext, - testGen->compilationDatabase, nullptr, + testGen->buildDatabase->compilationDatabase, nullptr, testGen->serverBuildDir); std::string wrapper = sourceToHeaderRewriter.generateWrapper(sourceFilePath); printer::SourceWrapperPrinter(Paths::getSourceLanguage(sourceFilePath)).print(testGen->projectContext, sourceFilePath, wrapper); }); } const CollectionUtils::FileSet &Synchronizer::getAllFiles() const { - return testGen->compilationDatabase->getAllFiles(); + return testGen->baseBuildDatabase->compilationDatabase->getAllFiles(); } void Synchronizer::prepareDirectory(const fs::path &stubDirectory) { diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index ca517c1f2..378c440d0 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -289,6 +289,7 @@ void BuildDatabase::createClangCompileCommandsJson() { fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath); JsonUtils::writeJsonToFile(clangCompileCommandsJsonPath, compileCommandsSingleFilesJson); + compilationDatabase = CompilationUtils::getCompilationDatabase(compileCommandsJsonPath); } //void BuildDatabase::updateTarget(const fs::path &_target) { diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index ff04fc48b..388aec415 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -233,6 +233,8 @@ class BuildDatabase { bool hasAutoTarget() const; fs::path getTargetPath() const; + + std::shared_ptr compilationDatabase; private: BuildDatabase(BuildDatabase& baseBuildDatabase, const std::string &_target); @@ -249,6 +251,7 @@ class BuildDatabase { CollectionUtils::MapFileTo> objectFileTargets; CollectionUtils::MapFileTo linkUnitToStubFiles; + std::vector>> compileCommands_temp; static bool conflictPriorityMore(const std::shared_ptr &left, diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 1ce7664e5..7b1cd2241 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -114,7 +114,7 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { if (result.isSuccess()) { auto [targetBitcode, stubsSet, _] = result.getOpt().value(); addToGenerated({ objectFile }, targetBitcode); - auto&& targetUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(target); + auto&& targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); testGen.buildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, stubsSet); return; } else { @@ -202,7 +202,7 @@ void Linker::linkForProject() { return compilationUnitInfo->getOutputFile(); }); addToGenerated(objectFiles, linkres.bitcodeOutput); - auto &&targetUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(target); + auto &&targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); testGen.buildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, linkres.stubsSet); break; } else { diff --git a/server/src/printers/TestMakefilesPrinter.cpp b/server/src/printers/TestMakefilesPrinter.cpp index 8445b7406..dd5a2963b 100644 --- a/server/src/printers/TestMakefilesPrinter.cpp +++ b/server/src/printers/TestMakefilesPrinter.cpp @@ -37,7 +37,7 @@ namespace printer { testGen.buildDatabase, testGen.buildDatabase->getTargetPath(), CompilationUtils::getBundledCompilerPath(CompilationUtils::getCompilerName( - testGen.compilationDatabase->getBuildCompilerPath())), + testGen.buildDatabase->compilationDatabase->getBuildCompilerPath())), stubSources) { } diff --git a/server/src/stubs/StubGen.cpp b/server/src/stubs/StubGen.cpp index 681daa113..9d846649b 100644 --- a/server/src/stubs/StubGen.cpp +++ b/server/src/stubs/StubGen.cpp @@ -20,7 +20,7 @@ CollectionUtils::FileSet StubGen::getStubSources(const fs::path &target) { return {}; } fs::path testedFilePath = *testGen.testingMethodsSourcePaths.begin(); - auto stubSources = StubSourcesFinder(testGen.baseBuildDatabase).excludeFind(testedFilePath, target); + auto stubSources = StubSourcesFinder(testGen.buildDatabase).excludeFind(testedFilePath, target); return { stubSources.begin(), stubSources.end() }; } diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index 99aed0670..d85c28998 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -17,9 +17,6 @@ BaseTestGen::BaseTestGen(const testsgen::ProjectContext &projectContext, } bool BaseTestGen::needToBeMocked() const { - LOG_S(ERROR) << "?????? " << TypeUtils::isSameType(*this) - << " " << TypeUtils::isSameType(*this) - << " " << TypeUtils::isSameType(*this); return TypeUtils::isSameType(*this) || TypeUtils::isSameType(*this) || TypeUtils::isSameType(*this); diff --git a/server/src/testgens/BaseTestGen.h b/server/src/testgens/BaseTestGen.h index 7e4ee2e81..19162c8a9 100644 --- a/server/src/testgens/BaseTestGen.h +++ b/server/src/testgens/BaseTestGen.h @@ -23,7 +23,7 @@ class BaseTestGen { fs::path serverBuildDir; fs::path compileCommandsJsonPath; - std::shared_ptr compilationDatabase; +// std::shared_ptr compilationDatabase; std::shared_ptr baseBuildDatabase; std::shared_ptr buildDatabase; diff --git a/server/src/testgens/ProjectTestGen.cpp b/server/src/testgens/ProjectTestGen.cpp index c56851fd4..d51076eee 100644 --- a/server/src/testgens/ProjectTestGen.cpp +++ b/server/src/testgens/ProjectTestGen.cpp @@ -18,11 +18,11 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, projectContext.projectPath, projectContext.buildDirRelativePath); baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, false); buildDatabase = baseBuildDatabase->createBaseForTarget(request.targetpath()); - compilationDatabase = CompilationUtils::getCompilationDatabase(compileCommandsJsonPath); +// compilationDatabase = CompilationUtils::getCompilationDatabase(compileCommandsJsonPath); if (autoSrcPaths) { autoDetectSourcePathsIfNotEmpty(); } else { - sourcePaths = compilationDatabase->getAllFiles(); + sourcePaths = buildDatabase->compilationDatabase->getAllFiles(); } testingMethodsSourcePaths = sourcePaths; setInitializedTestsMap(); @@ -54,7 +54,7 @@ void ProjectTestGen::autoDetectSourcePathsIfNotEmpty() { // requestSourcePaths are from settings.json auto requestSourcePaths = getRequestSourcePaths(); // sourcePathsCandidates are from compile_commands.json - auto sourcePathsCandidates = compilationDatabase->getAllFiles(); + auto sourcePathsCandidates = buildDatabase->compilationDatabase->getAllFiles(); if (!requestSourcePaths.empty()) { sourcePaths = Paths::filterPathsByDirNames(sourcePathsCandidates, requestSourcePaths, Paths::isSourceFile); diff --git a/server/src/testgens/SnippetTestGen.cpp b/server/src/testgens/SnippetTestGen.cpp index f1fc765a7..01914ec6f 100644 --- a/server/src/testgens/SnippetTestGen.cpp +++ b/server/src/testgens/SnippetTestGen.cpp @@ -20,7 +20,7 @@ SnippetTestGen::SnippetTestGen(const testsgen::SnippetRequest &request, utbot::ProjectContext projectContext{request, serverBuildDir}; baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, false); buildDatabase = baseBuildDatabase->createBaseForTarget(serverBuildDir / SNIPPET_TARGET); - compilationDatabase = CompilationUtils::getCompilationDatabase(serverBuildDir); +// compilationDatabase = CompilationUtils::getCompilationDatabase(serverBuildDir); setTargetForSource(filePath); setInitializedTestsMap(); } diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index 4209b9c79..8c9bf0598 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -167,9 +167,9 @@ namespace { ASSERT_TRUE(status.ok()) << status.error_message(); EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 2); - auto root = testGen.baseBuildDatabase->getRootForSource(foreign_bar_c); - auto linkUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(root); - auto stubFiles = testGen.baseBuildDatabase->getStubFiles(linkUnitInfo); + auto root = testGen.buildDatabase->getRootForSource(foreign_bar_c); + auto linkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(root); + auto stubFiles = testGen.buildDatabase->getStubFiles(linkUnitInfo); auto stubCandidates = { calc_sum_c }; auto expectedStubFiles = CollectionUtils::transformTo( stubCandidates, [&testGen](fs::path const &path) { From cbb6dfdb6c31b595555ee176c301b5785f950e49 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Fri, 5 Aug 2022 18:04:36 +0300 Subject: [PATCH 14/27] not end --- server/src/BordersFinder.cpp | 2 +- server/src/BordersFinder.h | 2 +- server/src/KleeGenerator.cpp | 102 +++++++++++++++--------- server/src/KleeGenerator.h | 73 +++++++++-------- server/src/Server.cpp | 5 +- server/src/Synchronizer.cpp | 16 ++-- server/src/building/BuildDatabase.cpp | 10 +-- server/src/building/Linker.cpp | 2 +- server/src/fetchers/Fetcher.cpp | 3 +- server/src/stubs/StubGen.cpp | 7 +- server/src/testgens/FileTestGen.cpp | 2 +- server/test/framework/KleeGen_Tests.cpp | 78 +++++++++--------- server/test/framework/Stub_Tests.cpp | 6 +- 13 files changed, 173 insertions(+), 135 deletions(-) diff --git a/server/src/BordersFinder.cpp b/server/src/BordersFinder.cpp index 6a4f3165a..342ac086e 100644 --- a/server/src/BordersFinder.cpp +++ b/server/src/BordersFinder.cpp @@ -17,7 +17,7 @@ BordersFinder::BordersFinder(const fs::path &filePath, const std::shared_ptr &compilationDatabase, const fs::path &compileCommandsJsonPath) : line(line), classBorder(std::nullopt), clangToolRunner(compilationDatabase) { - buildRootPath = Paths::subtractPath(compileCommandsJsonPath.string(), CompilationUtils::UTBOT_BUILD_DIR_NAME); +// buildRootPath = Paths::subtractPath(compileCommandsJsonPath.string(), CompilationUtils::UTBOT_BUILD_DIR_NAME); lineInfo.filePath = filePath; } diff --git a/server/src/BordersFinder.h b/server/src/BordersFinder.h index 46d523377..1707083b6 100644 --- a/server/src/BordersFinder.h +++ b/server/src/BordersFinder.h @@ -31,7 +31,7 @@ class BordersFinder : public clang::ast_matchers::MatchFinder::MatchCallback { private: unsigned line; LineInfo lineInfo{}; - fs::path buildRootPath; +// fs::path buildRootPath; struct Borders { struct Position { unsigned line; diff --git a/server/src/KleeGenerator.cpp b/server/src/KleeGenerator.cpp index 92573c92f..35da7f43c 100644 --- a/server/src/KleeGenerator.cpp +++ b/server/src/KleeGenerator.cpp @@ -16,23 +16,43 @@ using namespace tests; -KleeGenerator::KleeGenerator( - utbot::ProjectContext projectContext, - utbot::SettingsContext settingsContext, - fs::path serverBuildDir, - std::shared_ptr compilationDatabase, - types::TypesHandler &typesHandler, - PathSubstitution filePathsSubstitution, - std::shared_ptr buildDatabase, - ProgressWriter const *progressWriter) - : projectContext(std::move(projectContext)), - settingsContext(std::move(settingsContext)), projectTmpPath(std::move(serverBuildDir)), - compilationDatabase(std::move(compilationDatabase)), - typesHandler(typesHandler), pathSubstitution(std::move(filePathsSubstitution)), - buildDatabase(std::move(buildDatabase)), progressWriter(progressWriter) { +//KleeGenerator::KleeGenerator( +// utbot::testGen.projectContext testGen.projectContext, +// utbot::SettingsContext settingsContext, +// fs::path serverBuildDir, +// std::shared_ptr compilationDatabase, +// types::TypesHandler &typesHandler, +// PathSubstitution filePathsSubstitution, +// std::shared_ptr testGen.buildDatabase, +// ProgressWriter const *progressWriter) +// : testGen.projectContext(std::move(testGen.projectContext)), +// settingsContext(std::move(settingsContext)), +// testGen.serverBuildDir(std::move(serverBuildDir)), +// compilationDatabase(std::move(compilationDatabase)), +// typesHandler(typesHandler), +// pathSubstitution(std::move(filePathsSubstitution)), +// testGen.buildDatabase(std::move(testGen.buildDatabase)), +// progressWriter(progressWriter) { +// try { +// fs::create_directories(this->testGen.serverBuildDir); +// fs::create_directories(Paths::getLogDir(this->testGen.projectContext.projectName)); +// } catch (const fs::filesystem_error &e) { +// throw FileSystemException("create_directories failed", e); +// } +//} +// +//auto generator = std::make_shared( +// testGen.testGen.projectContext, testGen.settingsContext, +// testGen.serverBuildDir, testGen.testGen.buildDatabase->compilationDatabase, typesHandler, +// pathSubstitution, testGen.testGen.buildDatabase, testGen.progressWriter); + +KleeGenerator::KleeGenerator(BaseTestGen &_testGen, types::TypesHandler &typesHandler, + PathSubstitution filePathsSubstitution) + : testGen(_testGen), typesHandler(typesHandler), + pathSubstitution(std::move(filePathsSubstitution)) { try { - fs::create_directories(this->projectTmpPath); - fs::create_directories(Paths::getLogDir(this->projectContext.projectName)); + fs::create_directories(this->testGen.serverBuildDir); + fs::create_directories(Paths::getLogDir(this->testGen.projectContext.projectName)); } catch (const fs::filesystem_error &e) { throw FileSystemException("create_directories failed", e); } @@ -55,10 +75,10 @@ KleeGenerator::buildByCDb(const CollectionUtils::MapFileTo &filesToBui } makefilePrinter.declareTarget("all", outfilePaths, {}); - fs::path makefile = projectTmpPath / "GenerationCompileMakefile.mk"; + fs::path makefile = testGen.serverBuildDir / "GenerationCompileMakefile.mk"; FileSystemUtils::writeToFile(makefile, makefilePrinter.ss.str()); - auto command = MakefileUtils::MakefileCommand(projectContext, makefile, "all"); + auto command = MakefileUtils::MakefileCommand(testGen.projectContext, makefile, "all"); ExecUtils::ExecutionResult res = command.run(); if (res.status != 0) { LOG_S(ERROR) << StringUtils::stringFormat("Make for \"%s\" failed.\nCommand: \"%s\"\n%s\n", @@ -81,7 +101,7 @@ KleeGenerator::buildByCDb(const CollectionUtils::FileSet &filesToBuild, const CollectionUtils::FileSet &stubSources) { CollectionUtils::MapFileTo filesMap; for (fs::path const &file : filesToBuild) { - filesMap[file] = buildDatabase->getBitcodeFile(file); + filesMap[file] = testGen.buildDatabase->getBitcodeFile(file); } return buildByCDb(filesMap, stubSources); } @@ -127,8 +147,11 @@ static const std::unordered_set UNSUPPORTED_FLAGS_AND_OPTIONS_KLEE std::optional KleeGenerator::getCompileCommandForKlee(const fs::path &hintPath, const CollectionUtils::FileSet &stubSources, - const std::vector &flags) const { - auto compilationUnitInfo = buildDatabase->getClientCompilationUnitInfo(hintPath); + const std::vector &flags, + bool forStub) const { + + auto compilationUnitInfo = forStub ? testGen.baseBuildDatabase->getClientCompilationUnitInfo(hintPath) + : testGen.buildDatabase->getClientCompilationUnitInfo(hintPath); auto command = compilationUnitInfo->command; auto srcFilePath = compilationUnitInfo->getSourcePath(); std::string newCompilerPath = getUTBotClangCompilerPath(command.getCompiler()); @@ -136,11 +159,12 @@ KleeGenerator::getCompileCommandForKlee(const fs::path &hintPath, srcFilePath = pathSubstitution.substituteLineFlag(srcFilePath); if (CollectionUtils::contains(stubSources, srcFilePath)) { - srcFilePath = Paths::sourcePathToStubPath(projectContext, srcFilePath); + srcFilePath = Paths::sourcePathToStubPath(testGen.projectContext, srcFilePath); } command.setSourcePath(srcFilePath); - auto outFilePath = buildDatabase->getBitcodeFile(compilationUnitInfo->getOutputFile()); + auto outFilePath = forStub ? testGen.baseBuildDatabase->getBitcodeFile(compilationUnitInfo->getOutputFile()) + : testGen.buildDatabase->getBitcodeFile(compilationUnitInfo->getOutputFile()); fs::create_directories(outFilePath.parent_path()); command.setOutput(outFilePath); command.setOptimizationLevel("-O0"); @@ -172,7 +196,7 @@ KleeGenerator::getCompileCommandsForKlee(const CollectionUtils::MapFileTo compileCommands; compileCommands.reserve(filesToBuild.size()); for (const auto &[fileToBuild, bitcode] : filesToBuild) { - auto optionalCommand = getCompileCommandForKlee(fileToBuild, stubSources, {}); + auto optionalCommand = getCompileCommandForKlee(fileToBuild, stubSources, {}, false); if (optionalCommand.has_value()) { auto command = std::move(optionalCommand).value(); command.setOutput(bitcode); @@ -188,8 +212,8 @@ Result KleeGenerator::defaultBuild(const fs::path &hintPath, const fs::path &buildDirPath, const std::vector &flags) { LOG_SCOPE_FUNCTION(DEBUG); - auto bitcodeFilePath = buildDatabase->getBitcodeFile(sourceFilePath); - auto optionalCommand = getCompileCommandForKlee(hintPath, {}, flags); + auto bitcodeFilePath = testGen.buildDatabase->getBitcodeFile(sourceFilePath); + auto optionalCommand = getCompileCommandForKlee(hintPath, {}, flags, false); if (!optionalCommand.has_value()) { std::string message = StringUtils::stringFormat( "Couldn't get command for klee file: %s\n" @@ -204,10 +228,10 @@ Result KleeGenerator::defaultBuild(const fs::path &hintPath, printer::DefaultMakefilePrinter makefilePrinter; auto commandWithChangingDirectory = utbot::CompileCommand(command, true); makefilePrinter.declareTarget("build", {commandWithChangingDirectory.getSourcePath()}, {commandWithChangingDirectory.toStringWithChangingDirectory()}); - fs::path makefile = projectTmpPath / "BCForKLEE.mk"; + fs::path makefile = testGen.serverBuildDir / "BCForKLEE.mk"; FileSystemUtils::writeToFile(makefile, makefilePrinter.ss.str()); - auto makefileCommand = MakefileUtils::MakefileCommand(projectContext, makefile, "build"); + auto makefileCommand = MakefileUtils::MakefileCommand(testGen.projectContext, makefile, "build"); auto [out, status, _] = makefileCommand.run(); if (status != 0) { LOG_S(ERROR) << "Compilation for " << sourceFilePath << " failed.\n" @@ -233,10 +257,10 @@ fs::path KleeGenerator::writeKleeFile( const std::function &methodFilter) { if (lineInfo) { return kleePrinter.writeTmpKleeFile( - tests, projectTmpPath, pathSubstitution, lineInfo->predicateInfo, lineInfo->methodName, + tests, testGen.serverBuildDir, pathSubstitution, lineInfo->predicateInfo, lineInfo->methodName, lineInfo->scopeName, lineInfo->forMethod, lineInfo->forClass, methodFilter); } else { - return kleePrinter.writeTmpKleeFile(tests, projectTmpPath, pathSubstitution, std::nullopt, + return kleePrinter.writeTmpKleeFile(tests, testGen.serverBuildDir, pathSubstitution, std::nullopt, "", "", false, false, methodFilter); } } @@ -245,9 +269,9 @@ std::vector KleeGenerator::buildKleeFiles(const tests::TestsMap &tests const std::shared_ptr &lineInfo) { std::vector outFiles; LOG_S(DEBUG) << "Building generated klee files..."; - printer::KleePrinter kleePrinter(&typesHandler, buildDatabase, utbot::Language::UNKNOWN); + printer::KleePrinter kleePrinter(&typesHandler, testGen.buildDatabase, utbot::Language::UNKNOWN); ExecUtils::doWorkWithProgress( - testsMap, progressWriter, "Building generated klee files", + testsMap, testGen.progressWriter, "Building generated klee files", [&](auto const &it) { const auto &[filename, tests] = it; if (lineInfo != nullptr && filename != lineInfo->filePath) { @@ -255,13 +279,13 @@ std::vector KleeGenerator::buildKleeFiles(const tests::TestsMap &tests } kleePrinter.srcLanguage = Paths::getSourceLanguage(filename); std::vector includeFlags = { - StringUtils::stringFormat("-I%s", Paths::getFlagsDir(projectContext))}; + StringUtils::stringFormat("-I%s", Paths::getFlagsDir(testGen.projectContext))}; auto buildDirPath = - buildDatabase->getClientCompilationUnitInfo(filename)->getDirectory(); + testGen.buildDatabase->getClientCompilationUnitInfo(filename)->getDirectory(); fs::path kleeFilePath = writeKleeFile(kleePrinter, tests, lineInfo); auto kleeFilesInfo = - buildDatabase->getClientCompilationUnitInfo(tests.sourceFilePath)->kleeFilesInfo; + testGen.buildDatabase->getClientCompilationUnitInfo(tests.sourceFilePath)->kleeFilesInfo; auto kleeBitcodeFile = defaultBuild(filename, kleeFilePath, buildDirPath, includeFlags); if (kleeBitcodeFile.isSuccess()) { outFiles.emplace_back(kleeBitcodeFile.getOpt().value()); @@ -281,7 +305,7 @@ std::vector KleeGenerator::buildKleeFiles(const tests::TestsMap &tests std::unordered_set correctMethods; for (const auto &[methodName, methodDescription] : tests.methods) { fs::path currentKleeFilePath = kleePrinter.writeTmpKleeFile( - tests, projectTmpPath, pathSubstitution, std::nullopt, + tests, testGen.serverBuildDir, pathSubstitution, std::nullopt, methodDescription.name, methodDescription.getClassName(), true, false); @@ -358,7 +382,11 @@ void KleeGenerator::parseKTestsToFinalCode( } std::shared_ptr KleeGenerator::getBuildDatabase() const { - return buildDatabase; + return testGen.buildDatabase; +} + +std::shared_ptr KleeGenerator::getBaseBuildDatabase() const { + return testGen.baseBuildDatabase; } void KleeGenerator::handleFailedFunctions(tests::TestsMap &testsMap) { diff --git a/server/src/KleeGenerator.h b/server/src/KleeGenerator.h index 8f91efeb3..afabff4bf 100644 --- a/server/src/KleeGenerator.h +++ b/server/src/KleeGenerator.h @@ -15,12 +15,14 @@ #include "streams/tests/TestsWriter.h" #include "types/Types.h" #include "utils/ExecUtils.h" - #include "utils/path/FileSystemPath.h" +#include "testgens/BaseTestGen.h" + #include #include #include + using json = nlohmann::json; /** @@ -31,30 +33,33 @@ class KleeGenerator { using TestsMap = tests::TestsMap; public: - /** - * @brief Also creates tmp directories for build files. - * @param projectContext contains context about current project. - * @param settingsContext contains context about settings applied for current request. - * @param serverBuildDir Path to folder on server machine where project build dirs are located. - * @param sourcesFilePaths Paths to project files. Files which are listed in - * [compile_commands.json](https://clang.llvm.org/docs/JSONCompilationDatabase.html), filtered - * by them. - * @param compilationDatabase Pointer to compile_commands.json object. - * @param typesHandler provides additional information about types. - * @param filePathsSubstitution Mapping from source file path to modified file. Required for - * line test generation requests. - * @param buildDatabase Instance of BuildDatabase which handles link and compile commands - * @throws fs::filesystem_error Thrown if it can't create tmp folder for some - * reasons. - */ - KleeGenerator(utbot::ProjectContext projectContext, - utbot::SettingsContext settingsContext, - fs::path serverBuildDir, - std::shared_ptr compilationDatabase, - types::TypesHandler &typesHandler, - PathSubstitution filePathsSubstitution, - std::shared_ptr buildDatabase = nullptr, - const ProgressWriter *progressWriter = DummyStreamWriter::getInstance()); +// /** +// * @brief Also creates tmp directories for build files. +// * @param projectContext contains context about current project. +// * @param settingsContext contains context about settings applied for current request. +// * @param serverBuildDir Path to folder on server machine where project build dirs are located. +// * @param sourcesFilePaths Paths to project files. Files which are listed in +// * [compile_commands.json](https://clang.llvm.org/docs/JSONCompilationDatabase.html), filtered +// * by them. +// * @param compilationDatabase Pointer to compile_commands.json object. +// * @param typesHandler provides additional information about types. +// * @param filePathsSubstitution Mapping from source file path to modified file. Required for +// * line test generation requests. +// * @param buildDatabase Instance of BuildDatabase which handles link and compile commands +// * @throws fs::filesystem_error Thrown if it can't create tmp folder for some +// * reasons. +// */ +// KleeGenerator(utbot::ProjectContext projectContext, +// utbot::SettingsContext settingsContext, +// fs::path serverBuildDir, +// std::shared_ptr compilationDatabase, +// types::TypesHandler &typesHandler, +// PathSubstitution filePathsSubstitution, +// std::shared_ptr buildDatabase = nullptr, +// const ProgressWriter *progressWriter = DummyStreamWriter::getInstance()); + + KleeGenerator(BaseTestGen &_testGen, types::TypesHandler &typesHandler, + PathSubstitution filePathsSubstitution); struct BuildFileInfo { fs::path outFilePath; @@ -130,6 +135,8 @@ class KleeGenerator { [[nodiscard]] std::shared_ptr getBuildDatabase() const; + [[nodiscard]] std::shared_ptr getBaseBuildDatabase() const; + void handleFailedFunctions(tests::TestsMap &testsMap); /** @@ -146,21 +153,23 @@ class KleeGenerator { std::optional getCompileCommandForKlee(const fs::path &hintPath, const CollectionUtils::FileSet &stubSources, - const std::vector &flags) const; + const std::vector &flags, + bool forStub) const; std::vector getCompileCommandsForKlee(const CollectionUtils::MapFileTo &filesToBuild, const CollectionUtils::FileSet &stubSources) const; private: - const utbot::ProjectContext projectContext; - const utbot::SettingsContext settingsContext; - fs::path projectTmpPath; - std::shared_ptr compilationDatabase; +// const utbot::ProjectContext projectContext; +// const utbot::SettingsContext settingsContext; + BaseTestGen &testGen;; +// fs::path projectTmpPath; +// std::shared_ptr compilationDatabase; types::TypesHandler typesHandler; PathSubstitution pathSubstitution; - std::shared_ptr buildDatabase; - const ProgressWriter *progressWriter; +// std::shared_ptr buildDatabase; +// const ProgressWriter *progressWriter; CollectionUtils::MapFileTo> failedFunctions; diff --git a/server/src/Server.cpp b/server/src/Server.cpp index ef8b16c34..e1ffe75ba 100644 --- a/server/src/Server.cpp +++ b/server/src/Server.cpp @@ -253,10 +253,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, pathSubstitution = { lineTestGen->filePath, flagFilePath }; } } - auto generator = std::make_shared( - testGen.projectContext, testGen.settingsContext, - testGen.serverBuildDir, testGen.buildDatabase->compilationDatabase, typesHandler, - pathSubstitution, testGen.buildDatabase, testGen.progressWriter); + auto generator = std::make_shared(testGen, typesHandler, pathSubstitution); ReturnTypesFetcher returnTypesFetcher{ &testGen }; returnTypesFetcher.fetch(testGen.progressWriter, synchronizer.getAllFiles()); diff --git a/server/src/Synchronizer.cpp b/server/src/Synchronizer.cpp index c592e8103..6d09b7db1 100644 --- a/server/src/Synchronizer.cpp +++ b/server/src/Synchronizer.cpp @@ -115,7 +115,11 @@ void Synchronizer::synchronize(const types::TypesHandler &typesHandler) { } auto outdatedSourcePaths = getOutdatedSourcePaths(); if (testGen->settingsContext.useStubs) { - auto outdatedStubs = getStubSetFromSources(outdatedSourcePaths); + auto outdatedStubsSourcePaths = CollectionUtils::filterOut( + testGen->baseBuildDatabase->compilationDatabase->getAllFiles(), [this](fs::path const &sourcePath) { + return !isProbablyOutdated(sourcePath); + }); + auto outdatedStubs = getStubSetFromSources(outdatedStubsSourcePaths); synchronizeStubs(outdatedStubs, typesHandler); } synchronizeWrappers(outdatedSourcePaths); @@ -123,7 +127,7 @@ void Synchronizer::synchronize(const types::TypesHandler &typesHandler) { void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, const types::TypesHandler &typesHandler) { - StubSet allStubs = getStubSetFromSources(getAllFiles()); + StubSet allStubs = getStubSetFromSources(testGen->baseBuildDatabase->compilationDatabase->getAllFiles()); auto stubDirPath = Paths::getStubsDirPath(testGen->projectContext); prepareDirectory(stubDirPath); auto filesInFolder = Paths::findFilesInFolder(stubDirPath); @@ -145,7 +149,7 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, auto options = Fetcher::Options::Value::FUNCTION | Fetcher::Options::Value::INCLUDE | Fetcher::Options::Value::TYPE; auto stubFetcher = - Fetcher(options, testGen->buildDatabase->compilationDatabase, sourceFilesMap, &testGen->types, + Fetcher(options, testGen->baseBuildDatabase->compilationDatabase, sourceFilesMap, &testGen->types, &sizeContext->pointerSize, &sizeContext->maximumAlignment, testGen->compileCommandsJsonPath, false); @@ -157,7 +161,7 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, auto stubsCdb = createStubsCompilationDatabase(stubFiles, ccJsonStubDirPath); auto sourceToHeaderRewriter = - SourceToHeaderRewriter(testGen->projectContext, testGen->buildDatabase->compilationDatabase, + SourceToHeaderRewriter(testGen->projectContext, testGen->baseBuildDatabase->compilationDatabase, stubFetcher.getStructsToDeclare(), testGen->serverBuildDir); for (const StubOperator &outdatedStub : outdatedStubs) { @@ -203,14 +207,14 @@ void Synchronizer::synchronizeWrappers(const CollectionUtils::FileSet &outdatedS sourceFilesNeedToRegenerateWrappers, testGen->progressWriter, "Generating wrappers", [this](fs::path const &sourceFilePath) { SourceToHeaderRewriter sourceToHeaderRewriter(testGen->projectContext, - testGen->buildDatabase->compilationDatabase, nullptr, + testGen->baseBuildDatabase->compilationDatabase, nullptr, testGen->serverBuildDir); std::string wrapper = sourceToHeaderRewriter.generateWrapper(sourceFilePath); printer::SourceWrapperPrinter(Paths::getSourceLanguage(sourceFilePath)).print(testGen->projectContext, sourceFilePath, wrapper); }); } const CollectionUtils::FileSet &Synchronizer::getAllFiles() const { - return testGen->baseBuildDatabase->compilationDatabase->getAllFiles(); + return testGen->buildDatabase->compilationDatabase->getAllFiles(); } void Synchronizer::prepareDirectory(const fs::path &stubDirectory) { diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 378c440d0..23b69e677 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -52,9 +52,9 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, filterInstalledFiles(); addLocalSharedLibraries(); fillTargetInfoParents(); - if (createClangCC) { +// if (createClangCC) { createClangCompileCommandsJson(); - } +// } target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; } @@ -289,7 +289,7 @@ void BuildDatabase::createClangCompileCommandsJson() { fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath); JsonUtils::writeJsonToFile(clangCompileCommandsJsonPath, compileCommandsSingleFilesJson); - compilationDatabase = CompilationUtils::getCompilationDatabase(compileCommandsJsonPath); + compilationDatabase = CompilationUtils::getCompilationDatabase(clangCompileCommandsJsonPath); } //void BuildDatabase::updateTarget(const fs::path &_target) { @@ -598,14 +598,14 @@ fs::path BuildDatabase::getBitcodeFile(const fs::path &filepath) const { std::shared_ptr BuildDatabase::getClientCompilationObjectInfo(const fs::path &filepath) const { if (!CollectionUtils::contains(objectFileInfos, filepath)) { - throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string()); + throw CompilationDatabaseException("Object file not found in compilation_commands.json: " + filepath.string()); } return objectFileInfos.at(filepath); } std::shared_ptr BuildDatabase::getClientCompilationSourceInfo(const fs::path &filepath) const { if (!CollectionUtils::contains(sourceFileInfos, filepath)) { - throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string()); + throw CompilationDatabaseException("Source file not found in compilation_commands.json: " + filepath.string()); } LOG_IF_S(DEBUG, sourceFileInfos.at(filepath).size() > 1) << "More than one compile command for: " << filepath; return sourceFileInfos.at(filepath)[0]; diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 7b1cd2241..5a849d55a 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -454,7 +454,7 @@ Result Linker::generateStubsMakefile( fs::path sourcePath = Paths::stubPathToSourcePath(testGen.projectContext, stubPath); fs::path bitcodeFile = kleeGenerator->getBuildDatabase()->getBitcodeFile(sourcePath); bitcodeFile = Paths::getStubBitcodeFilePath(bitcodeFile); - auto command = kleeGenerator->getCompileCommandForKlee(sourcePath, {}, {}); + auto command = kleeGenerator->getCompileCommandForKlee(sourcePath, {}, {}, true); command->setSourcePath(stubPath); command->setOutput(bitcodeFile); auto commandWithChangingDirectory = utbot::CompileCommand(command.value(), true); diff --git a/server/src/fetchers/Fetcher.cpp b/server/src/fetchers/Fetcher.cpp index 06b304479..06f4ac94b 100644 --- a/server/src/fetchers/Fetcher.cpp +++ b/server/src/fetchers/Fetcher.cpp @@ -31,8 +31,7 @@ Fetcher::Fetcher(Options options, : options(options), projectTests(&tests), projectTypes(types), pointerSize(pointerSize), maximumAlignment(maximumAlignment), fetchFunctionBodies(fetchFunctionBodies), clangToolRunner(compilationDatabase) { - buildRootPath = Paths::subtractPath(compileCommandsJsonPath.string(), - CompilationUtils::UTBOT_BUILD_DIR_NAME); + buildRootPath = Paths::subtractPath(compileCommandsJsonPath.string(), CompilationUtils::UTBOT_BUILD_DIR_NAME); if (options.has(Options::Value::TYPE)) { addMatcher(anyTypeDeclarationMatcher); addMatcher(structJustDeclMatcher); diff --git a/server/src/stubs/StubGen.cpp b/server/src/stubs/StubGen.cpp index 9d846649b..ef6c771fb 100644 --- a/server/src/stubs/StubGen.cpp +++ b/server/src/stubs/StubGen.cpp @@ -15,12 +15,10 @@ StubGen::StubGen(BaseTestGen &testGen) : testGen(testGen) { CollectionUtils::FileSet StubGen::getStubSources(const fs::path &target) { if (!testGen.needToBeMocked() || !testGen.settingsContext.useStubs) { - LOG_S(ERROR) << "?????? " << testGen.needToBeMocked() - << " " << testGen.settingsContext.useStubs; return {}; } fs::path testedFilePath = *testGen.testingMethodsSourcePaths.begin(); - auto stubSources = StubSourcesFinder(testGen.buildDatabase).excludeFind(testedFilePath, target); + auto stubSources = StubSourcesFinder(testGen.baseBuildDatabase).excludeFind(testedFilePath, target); return { stubSources.begin(), stubSources.end() }; } @@ -28,8 +26,7 @@ CollectionUtils::FileSet StubGen::findStubFilesBySignatures(const std::vector &signatures) { fs::path ccJsonDirPath = Paths::getUtbotBuildDir(testGen.projectContext) / "stubs_build_files"; - auto stubFiles = - Paths::findFilesInFolder(Paths::getStubsDirPath(testGen.projectContext)); + auto stubFiles = Paths::findFilesInFolder(Paths::getStubsDirPath(testGen.projectContext)); stubFiles = Synchronizer::dropHeaders(stubFiles); CollectionUtils::erase_if(stubFiles, [this](fs::path const &stubPath) { fs::path sourcePath = Paths::stubPathToSourcePath(testGen.projectContext, stubPath); diff --git a/server/src/testgens/FileTestGen.cpp b/server/src/testgens/FileTestGen.cpp index cd954ac85..0f3dba1fa 100644 --- a/server/src/testgens/FileTestGen.cpp +++ b/server/src/testgens/FileTestGen.cpp @@ -7,7 +7,7 @@ FileTestGen::FileTestGen(const testsgen::FileRequest &request, bool testMode) : ProjectTestGen(request.projectrequest(), progressWriter, testMode, false), filepath(fs::weakly_canonical(request.filepath())) { - testingMethodsSourcePaths = { filepath }; + testingMethodsSourcePaths = { filepath }; setInitializedTestsMap(); } diff --git a/server/test/framework/KleeGen_Tests.cpp b/server/test/framework/KleeGen_Tests.cpp index 968d1f082..ced1b5e5c 100644 --- a/server/test/framework/KleeGen_Tests.cpp +++ b/server/test/framework/KleeGen_Tests.cpp @@ -44,44 +44,48 @@ namespace { getTestFilePath("inner/inner_basic_functions.c") } }; } - KleeGenerator initKleeGenerator(const TestSuite &suite, std::string &errorMessage) { - std::shared_ptr compilationDatabase = - CompilationDatabase::autoDetectFromDirectory( - suite.buildPath.string(), errorMessage); - types::TypesHandler::SizeContext sizeContext; - types::TypeMaps typeMaps; - types::TypesHandler typesHandler(typeMaps, sizeContext); - fs::path testsDirPath = tmpDirPath / "test"; - utbot::ProjectContext projectContext{ suite.name, "", testsDirPath, - buildDirRelativePath }; - auto buildDatabase = std::make_shared(suite.buildPath, suite.buildPath, projectContext, true); - utbot::SettingsContext settingsContext{ true, true, 15, 0, true, false }; - KleeGenerator generator(std::move(projectContext), - std::move(settingsContext), tmpDirPath, - compilationDatabase, typesHandler, {}, buildDatabase); - return generator; - } +// KleeGenerator initKleeGenerator(const TestSuite &suite, std::string &errorMessage) { +// std::shared_ptr compilationDatabase = +// CompilationDatabase::autoDetectFromDirectory( +// suite.buildPath.string(), errorMessage); +// types::TypesHandler::SizeContext sizeContext; +// types::TypeMaps typeMaps; +// types::TypesHandler typesHandler(typeMaps, sizeContext); +// fs::path testsDirPath = tmpDirPath / "test"; +// utbot::ProjectContext projectContext{ suite.name, "", testsDirPath, +// buildDirRelativePath }; +// auto buildDatabase = std::make_shared(suite.buildPath, suite.buildPath, projectContext, true); +// utbot::SettingsContext settingsContext{ true, true, 15, 0, true, false }; +// KleeGenerator generator(std::move(projectContext), +// std::move(settingsContext), +// tmpDirPath, +// compilationDatabase, +// typesHandler, +// {}, +// buildDatabase); +// return generator; +// } }; - TEST_F(KleeGen_Test, BuildByCDb) { - std::string errorMessage; - auto generator = initKleeGenerator(testSuite, errorMessage); - ASSERT_TRUE(errorMessage.empty()); - CollectionUtils::FileSet sources(testSuite.sourcesFilePaths.begin(), testSuite.sourcesFilePaths.end()); - sources.erase(getTestFilePath("snippet.c")); - sources.erase(getTestFilePath("external_dependent.c")); - auto outFilesPaths = generator.buildByCDb(sources); - for (const auto &[actualFilePath, srcFilePath] : outFilesPaths) { - EXPECT_TRUE(fs::exists(actualFilePath)) << testUtils::fileNotExistsMessage(actualFilePath); - } - } +// TEST_F(KleeGen_Test, BuildByCDb) { +// std::string errorMessage; +// auto generator = initKleeGenerator(testSuite, errorMessage); +// ASSERT_TRUE(errorMessage.empty()); +// CollectionUtils::FileSet sources(testSuite.sourcesFilePaths.begin(), testSuite.sourcesFilePaths.end()); +// sources.erase(getTestFilePath("snippet.c")); +// sources.erase(getTestFilePath("external_dependent.c")); +// auto outFilesPaths = generator.buildByCDb(sources); +// for (const auto &[actualFilePath, srcFilePath] : outFilesPaths) { +// EXPECT_TRUE(fs::exists(actualFilePath)) << testUtils::fileNotExistsMessage(actualFilePath); +// } +// } - TEST_F(KleeGen_Test, DefaultBuild) { - std::string errorMessage; - auto generator = initKleeGenerator(testSuite, errorMessage); - ASSERT_TRUE(errorMessage.empty()); - fs::path sourceFilePath = *testSuite.sourcesFilePaths.begin(); - auto actualFilePath = generator.defaultBuild(sourceFilePath); - EXPECT_TRUE(fs::exists(actualFilePath.getOpt().value())); - } +// TEST_F(KleeGen_Test, DefaultBuild) { +// std::string errorMessage; +// auto generator = initKleeGenerator(testSuite, errorMessage); +// ASSERT_TRUE(errorMessage.empty()); +// fs::path sourceFilePath = *testSuite.sourcesFilePaths.begin(); +// auto actualFilePath = generator.defaultBuild(sourceFilePath); +// EXPECT_TRUE(fs::exists(actualFilePath.getOpt().value())); +// } } diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index 8c9bf0598..4209b9c79 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -167,9 +167,9 @@ namespace { ASSERT_TRUE(status.ok()) << status.error_message(); EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 2); - auto root = testGen.buildDatabase->getRootForSource(foreign_bar_c); - auto linkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(root); - auto stubFiles = testGen.buildDatabase->getStubFiles(linkUnitInfo); + auto root = testGen.baseBuildDatabase->getRootForSource(foreign_bar_c); + auto linkUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(root); + auto stubFiles = testGen.baseBuildDatabase->getStubFiles(linkUnitInfo); auto stubCandidates = { calc_sum_c }; auto expectedStubFiles = CollectionUtils::transformTo( stubCandidates, [&testGen](fs::path const &path) { From f8ff6af8fb8e66b8e24767c7fcfc6b81a0a5e7f3 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Mon, 8 Aug 2022 19:31:23 +0300 Subject: [PATCH 15/27] not end --- server/src/building/BuildDatabase.cpp | 31 +++++----- server/src/building/BuildDatabase.h | 19 +++--- server/src/building/IRParser.cpp | 2 +- server/src/building/Linker.cpp | 8 +-- server/src/printers/NativeMakefilePrinter.cpp | 60 ++++++++++--------- server/src/printers/NativeMakefilePrinter.h | 10 ++-- server/src/printers/TestMakefilesPrinter.cpp | 18 +++--- server/src/printers/TestMakefilesPrinter.h | 5 +- server/src/testgens/BaseTestGen.cpp | 9 ++- server/test/framework/Regression_Tests.cpp | 3 +- server/test/framework/Server_Tests.cpp | 36 +++++++---- server/test/framework/Stub_Tests.cpp | 14 ++--- 12 files changed, 120 insertions(+), 95 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 23b69e677..702db33f0 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -39,7 +39,8 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, projectContext(std::move(_projectContext)), buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), - compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { + compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")), + isAutoTarget(true) { if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); } @@ -64,7 +65,8 @@ BuildDatabase::BuildDatabase(BuildDatabase& baseBuildDatabase, projectContext(baseBuildDatabase.projectContext), buildCommandsJsonPath(baseBuildDatabase.buildCommandsJsonPath), linkCommandsJsonPath(baseBuildDatabase.linkCommandsJsonPath), - compileCommandsJsonPath(baseBuildDatabase.compileCommandsJsonPath) { + compileCommandsJsonPath(baseBuildDatabase.compileCommandsJsonPath), + isAutoTarget(false) { // BuildDatabase baseBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, false); @@ -77,6 +79,7 @@ BuildDatabase::BuildDatabase(BuildDatabase& baseBuildDatabase, } else if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _target.empty()) { fs::path root = baseBuildDatabase.getRootForFirstSource(); target = root; + isAutoTarget = true; } else { auto new_target = GenerationUtils::findTarget(baseBuildDatabase.getAllTargets(), _target); if (new_target.has_value()) { @@ -722,14 +725,14 @@ fs::path BuildDatabase::TargetInfo::getOutput() const { return commands[0].getOutput(); } -CollectionUtils::FileSet BuildDatabase::getStubFiles( - const std::shared_ptr &linkUnitInfo) const { - auto iterator = linkUnitToStubFiles.find(linkUnitInfo->getOutput()); - if (iterator != linkUnitToStubFiles.end()) { - return iterator->second; - } - return {}; -} +//CollectionUtils::FileSet BuildDatabase::getStubFiles( +// const std::shared_ptr &linkUnitInfo) const { +// auto iterator = linkUnitToStubFiles.find(linkUnitInfo->getOutput()); +// if (iterator != linkUnitToStubFiles.end()) { +// return iterator->second; +// } +// return {}; +//} void BuildDatabase::assignStubFilesToLinkUnit( std::shared_ptr linkUnitInfo, @@ -778,9 +781,9 @@ BuildDatabase::getTargetsForSourceFile(const fs::path &sourceFilePath) const { std::vector BuildDatabase::targetListForFile(const fs::path &sourceFilePath, const fs::path &objectFile) const { -// if (!hasAutoTarget()) { -// return {target}; -// } + if (!hasAutoTarget()) { + return { target }; + } auto result = CollectionUtils::transformTo>( getTargetsForSourceFile(sourceFilePath), [&](const std::shared_ptr &targetInfo) { @@ -843,7 +846,7 @@ CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path & } bool BuildDatabase::hasAutoTarget() const { - return target == GrpcUtils::UTBOT_AUTO_TARGET_PATH; + return isAutoTarget; } fs::path BuildDatabase::getTargetPath() const { diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 388aec415..747e1a284 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -197,15 +197,15 @@ class BuildDatabase { */ std::vector> getAllCompileCommands() const; - /** - * @brief Gets all stub files associated with given link unit - * - * @param linkUnitInfo link unit info (preferably library) - * - * @return set of file paths to stubs - */ - CollectionUtils::FileSet - getStubFiles(const std::shared_ptr &linkUnitInfo) const; +// /** +// * @brief Gets all stub files associated with given link unit +// * +// * @param linkUnitInfo link unit info (preferably library) +// * +// * @return set of file paths to stubs +// */ +// CollectionUtils::FileSet +// getStubFiles(const std::shared_ptr &linkUnitInfo) const; /** * @brief Assign set of file paths to stubs to given link unit @@ -245,6 +245,7 @@ class BuildDatabase { const fs::path linkCommandsJsonPath; const fs::path compileCommandsJsonPath; fs::path target; + bool isAutoTarget; CollectionUtils::MapFileTo>> sourceFileInfos; CollectionUtils::MapFileTo> objectFileInfos; CollectionUtils::MapFileTo> targetInfos; diff --git a/server/src/building/IRParser.cpp b/server/src/building/IRParser.cpp index e602bf902..2b8c95075 100644 --- a/server/src/building/IRParser.cpp +++ b/server/src/building/IRParser.cpp @@ -17,7 +17,7 @@ bool IRParser::parseModule(const fs::path &rootBitcode, tests::TestsMap &tests) { try { - LOG_S(MAX) << "Parse module: " << rootBitcode.c_str(); + LOG_S(DEBUG) << "Parse module: " << rootBitcode.c_str(); llvm::LLVMContext context; auto module = getModule(rootBitcode, context); if (module) { diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 5a849d55a..a189ed963 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -133,7 +133,7 @@ Result Linker::linkWholeTarget(const fs::path &target) { auto requestTarget = testGen.buildDatabase->getTargetPath(); LOG_IF_S(ERROR, target != GrpcUtils::UTBOT_AUTO_TARGET_PATH && requestTarget != target) << "Try link target that not specified by user"; -// testGen.setTargetPath(target); + testGen.setTargetPath(target); auto targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); auto siblings = testGen.buildDatabase->getArchiveObjectFiles(target); @@ -164,7 +164,7 @@ Result Linker::linkWholeTarget(const fs::path &target) { kleeGenerator->buildByCDb(siblingObjectsToBuild, stubSources); auto result = link(filesToLink, target, "", std::nullopt, stubSources, false); //this is done in order to restore testGen.target in case of UTBot: auto -// testGen.setTargetPath(requestTarget); + testGen.setTargetPath(requestTarget); return result; } @@ -202,8 +202,8 @@ void Linker::linkForProject() { return compilationUnitInfo->getOutputFile(); }); addToGenerated(objectFiles, linkres.bitcodeOutput); - auto &&targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); - testGen.buildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, linkres.stubsSet); + auto &&targetUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(target); + testGen.baseBuildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, linkres.stubsSet); break; } else { std::stringstream ss; diff --git a/server/src/printers/NativeMakefilePrinter.cpp b/server/src/printers/NativeMakefilePrinter.cpp index 66d5e544b..8fa48231b 100644 --- a/server/src/printers/NativeMakefilePrinter.cpp +++ b/server/src/printers/NativeMakefilePrinter.cpp @@ -116,14 +116,17 @@ namespace printer { } NativeMakefilePrinter::NativeMakefilePrinter( - utbot::ProjectContext projectContext, - std::shared_ptr buildDatabase, +// utbot::ProjectContext projectContext, +// std::shared_ptr buildDatabase, + const BaseTestGen& testGen, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources, std::map> pathToShellVariable) : RelativeMakefilePrinter(pathToShellVariable), - projectContext(std::move(projectContext)), buildDatabase(std::move(buildDatabase)), rootPath(std::move(rootPath)), + testGen(testGen), +// projectContext(std::move(projectContext)), buildDatabase(std::move(buildDatabase)), + rootPath(std::move(rootPath)), primaryCompiler(std::move(primaryCompiler)), primaryCxxCompiler(CompilationUtils::toCppCompiler(this->primaryCompiler)), primaryCompilerName(CompilationUtils::getCompilerName(this->primaryCompiler)), @@ -135,7 +138,7 @@ namespace printer { CompilationUtils::getCoverageLinkFlags(primaryCxxCompilerName), " ")), sanitizerLinkFlags(SanitizerUtils::getSanitizeLinkFlags(primaryCxxCompilerName)), - buildDirectory(Paths::getUtbotBuildDir(projectContext)), + buildDirectory(Paths::getUtbotBuildDir(testGen.projectContext)), dependencyDirectory(buildDirectory / "dependencies"), stubSources(stubSources) { @@ -164,13 +167,13 @@ namespace printer { } fs::path NativeMakefilePrinter::getTemporaryDependencyFile(fs::path const &file) { - fs::path relativePath = fs::relative(file, projectContext.projectPath); + fs::path relativePath = fs::relative(file, testGen.projectContext.projectPath); return getRelativePath(dependencyDirectory) / Paths::addExtension(relativePath, ".Td"); } fs::path NativeMakefilePrinter::getDependencyFile(fs::path const &file) { - fs::path relativePath = fs::relative(file, projectContext.projectPath); + fs::path relativePath = fs::relative(file, testGen.projectContext.projectPath); return getRelativePath(dependencyDirectory) / Paths::addExtension(relativePath, ".d"); } @@ -270,7 +273,7 @@ namespace printer { BuildResult NativeMakefilePrinter::addObjectFile(const fs::path &objectFile, const std::string &suffixForParentOfStubs) { - auto compilationUnitInfo = buildDatabase->getClientCompilationUnitInfo(objectFile); + auto compilationUnitInfo = testGen.buildDatabase->getClientCompilationUnitInfo(objectFile); fs::path sourcePath = compilationUnitInfo->getSourcePath(); fs::path pathToCompile; @@ -278,17 +281,17 @@ namespace printer { BuildResult::Type buildResultType; BuildResult buildResult; if (CollectionUtils::contains(*stubSources, sourcePath)) { - pathToCompile = Paths::sourcePathToStubPath(projectContext, sourcePath); - recompiledFile = Paths::getRecompiledFile(projectContext, pathToCompile); + pathToCompile = Paths::sourcePathToStubPath(testGen.projectContext, sourcePath); + recompiledFile = Paths::getRecompiledFile(testGen.projectContext, pathToCompile); buildResultType = BuildResult::Type::ALL_STUBS; } else { if (Paths::isCXXFile(sourcePath)) { pathToCompile = sourcePath; } else { - pathToCompile = Paths::getWrapperFilePath(projectContext, sourcePath); + pathToCompile = Paths::getWrapperFilePath(testGen.projectContext, sourcePath); } recompiledFile = - Paths::getRecompiledFile(projectContext, compilationUnitInfo->getOutputFile()); + Paths::getRecompiledFile(testGen.projectContext, compilationUnitInfo->getOutputFile()); buildResultType = BuildResult::Type::NO_STUBS; } @@ -299,7 +302,7 @@ namespace printer { } void NativeMakefilePrinter::addTestTarget(const fs::path &sourcePath) { - auto compilationUnitInfo = buildDatabase->getClientCompilationUnitInfo(sourcePath); + auto compilationUnitInfo = testGen.buildDatabase->getClientCompilationUnitInfo(sourcePath); auto testCompilationCommand = compilationUnitInfo->command; testCompilationCommand.setCompiler(getRelativePathForLinker(primaryCxxCompiler)); testCompilationCommand.setOptimizationLevel(OPTIMIZATION_FLAG); @@ -314,10 +317,10 @@ namespace printer { testCompilationCommand.addFlagToBegin(FPIC_FLAG); testCompilationCommand.addFlagsToBegin(SANITIZER_NEEDED_FLAGS); - fs::path testSourcePath = Paths::sourcePathToTestPath(projectContext, sourcePath); + fs::path testSourcePath = Paths::sourcePathToTestPath(testGen.projectContext, sourcePath); fs::path compilationDirectory = compilationUnitInfo->getDirectory(); - fs::path testObjectDir = Paths::getTestObjectDir(projectContext); - fs::path testSourceRelativePath = fs::relative(testSourcePath, projectContext.testDirPath); + fs::path testObjectDir = Paths::getTestObjectDir(testGen.projectContext); + fs::path testSourceRelativePath = fs::relative(testSourcePath, testGen.projectContext.testDirPath); fs::path testObjectPathRelative = getRelativePath( testObjectDir / Paths::addExtension(testSourceRelativePath, ".o")); testCompilationCommand.setOutput( @@ -332,7 +335,7 @@ namespace printer { artifacts.push_back(testCompilationCommand.getOutput()); - auto rootLinkUnitInfo = buildDatabase->getClientLinkUnitInfo(rootPath); + auto rootLinkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(rootPath); fs::path testExecutablePath = getTestExecutablePath(sourcePath); std::vector filesToLink{ "$(GTEST_MAIN)", "$(GTEST_ALL)", testCompilationCommand.getOutput(), @@ -399,14 +402,15 @@ namespace printer { } fs::path NativeMakefilePrinter::getTestExecutablePath(const fs::path &sourcePath) const { return Paths::removeExtension( - Paths::removeExtension(Paths::getRecompiledFile(projectContext, sourcePath))); + Paths::removeExtension(Paths::getRecompiledFile(testGen.projectContext, sourcePath))); } NativeMakefilePrinter::NativeMakefilePrinter(const NativeMakefilePrinter &baseMakefilePrinter, const fs::path &sourcePath) : RelativeMakefilePrinter(baseMakefilePrinter.pathToShellVariable), - projectContext(baseMakefilePrinter.projectContext), - buildDatabase(baseMakefilePrinter.buildDatabase), +// projectContext(baseMakefilePrinter.projectContext), +// buildDatabase(baseMakefilePrinter.buildDatabase), + testGen(baseMakefilePrinter.testGen), rootPath(baseMakefilePrinter.rootPath), primaryCompiler(baseMakefilePrinter.primaryCompiler), primaryCxxCompiler(baseMakefilePrinter.primaryCxxCompiler), @@ -427,7 +431,7 @@ namespace printer { fs::path testExecutablePath = getTestExecutablePath(sourcePath); - auto rootLinkUnitInfo = buildDatabase->getClientLinkUnitInfo(rootPath); + auto rootLinkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(rootPath); fs::path coverageInfoBinary = sharedOutput.value(); if (!Paths::isLibraryFile(coverageInfoBinary)) { @@ -469,7 +473,7 @@ namespace printer { return buildResults[unitFile] = buildResult; } - auto linkUnitInfo = buildDatabase->getClientLinkUnitInfo(unitFile); + auto linkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(unitFile); BuildResult::Type unitType = BuildResult::Type::NONE; CollectionUtils::MapFileTo fileMapping; auto unitBuildResults = CollectionUtils::transformTo>( @@ -490,7 +494,7 @@ namespace printer { bool isExecutable = !Paths::isLibraryFile(unitFile); fs::path recompiledFile = - Paths::getRecompiledFile(projectContext, linkUnitInfo->getOutput()); + Paths::getRecompiledFile(testGen.projectContext, linkUnitInfo->getOutput()); if (isExecutable && !transformExeToLib) { recompiledFile = Paths::isObjectFile(recompiledFile) ? recompiledFile : Paths::addExtension(recompiledFile, ".o"); @@ -530,9 +534,9 @@ namespace printer { getLibraryAbsolutePath(argument, linkCommand.getDirectory()); if (optionalLibraryAbsolutePath.has_value()) { const fs::path &absolutePath = optionalLibraryAbsolutePath.value(); - if (Paths::isSubPathOf(projectContext.buildDir(), absolutePath)) { + if (Paths::isSubPathOf(testGen.projectContext.buildDir(), absolutePath)) { fs::path recompiledDir = - Paths::getRecompiledFile(projectContext, absolutePath); + Paths::getRecompiledFile(testGen.projectContext, absolutePath); std::string directoryFlag = getLibraryDirectoryFlag(recompiledDir); libraryDirectoriesFlags.push_back(directoryFlag); } @@ -614,11 +618,11 @@ namespace printer { void NativeMakefilePrinter::addStubs(const CollectionUtils::FileSet &stubsSet) { auto stubObjectFiles = CollectionUtils::transformTo( Synchronizer::dropHeaders(stubsSet), [this](fs::path const &stub) { - fs::path sourcePath = Paths::stubPathToSourcePath(projectContext, stub); + fs::path sourcePath = Paths::stubPathToSourcePath(testGen.projectContext, stub); fs::path stubBuildFilePath = - Paths::getStubBuildFilePath(projectContext, sourcePath); - auto compilationUnitInfo = buildDatabase->getClientCompilationUnitInfo(sourcePath); - fs::path target = Paths::getRecompiledFile(projectContext, stub); + Paths::getStubBuildFilePath(testGen.projectContext, sourcePath); + auto compilationUnitInfo = testGen.baseBuildDatabase->getClientCompilationUnitInfo(sourcePath); + fs::path target = Paths::getRecompiledFile(testGen.projectContext, stub); addCompileTarget(stub, target, *compilationUnitInfo); return target; }); diff --git a/server/src/printers/NativeMakefilePrinter.h b/server/src/printers/NativeMakefilePrinter.h index d7e46787e..fc4dbd5ea 100644 --- a/server/src/printers/NativeMakefilePrinter.h +++ b/server/src/printers/NativeMakefilePrinter.h @@ -15,8 +15,9 @@ namespace printer { class NativeMakefilePrinter : public RelativeMakefilePrinter { friend class TestMakefilesPrinter; private: - const utbot::ProjectContext projectContext; - std::shared_ptr buildDatabase; + const BaseTestGen& testGen; +// const utbot::ProjectContext projectContext; +// std::shared_ptr buildDatabase; fs::path rootPath; fs::path primaryCompiler; @@ -67,8 +68,9 @@ namespace printer { bool transformExeToLib); public: - NativeMakefilePrinter(utbot::ProjectContext projectContext, - std::shared_ptr buildDatabase, + NativeMakefilePrinter(const BaseTestGen& testGen, +// utbot::ProjectContext projectContext, +// std::shared_ptr buildDatabase, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources, diff --git a/server/src/printers/TestMakefilesPrinter.cpp b/server/src/printers/TestMakefilesPrinter.cpp index dd5a2963b..57cc1c501 100644 --- a/server/src/printers/TestMakefilesPrinter.cpp +++ b/server/src/printers/TestMakefilesPrinter.cpp @@ -33,8 +33,7 @@ namespace printer { TestMakefilesPrinter::TestMakefilesPrinter(const BaseTestGen &testGen, CollectionUtils::FileSet const *stubSources) : TestMakefilesPrinter( - testGen.projectContext, - testGen.buildDatabase, + testGen, testGen.buildDatabase->getTargetPath(), CompilationUtils::getBundledCompilerPath(CompilationUtils::getCompilerName( testGen.buildDatabase->compilationDatabase->getBuildCompilerPath())), @@ -42,15 +41,18 @@ namespace printer { } TestMakefilesPrinter::TestMakefilesPrinter( - utbot::ProjectContext projectContext, - std::shared_ptr buildDatabase, + const BaseTestGen &testGen, +// utbot::ProjectContext projectContext, +// std::shared_ptr buildDatabase, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources) : - RelativeMakefilePrinter(Paths::getUtbotBuildDir(projectContext), Paths::getRelativeUtbotBuildDir(projectContext), projectContext.projectPath), - projectContext(projectContext), - sharedMakefilePrinter(projectContext, buildDatabase, rootPath, primaryCompiler, stubSources, pathToShellVariable), - objMakefilePrinter(projectContext, buildDatabase, rootPath, primaryCompiler, stubSources, pathToShellVariable) { + RelativeMakefilePrinter(Paths::getUtbotBuildDir(testGen.projectContext), + Paths::getRelativeUtbotBuildDir(testGen.projectContext), + testGen.projectContext.projectPath), + projectContext(testGen.projectContext), + sharedMakefilePrinter(testGen, rootPath, primaryCompiler, stubSources, pathToShellVariable), + objMakefilePrinter(testGen, rootPath, primaryCompiler, stubSources, pathToShellVariable) { } void diff --git a/server/src/printers/TestMakefilesPrinter.h b/server/src/printers/TestMakefilesPrinter.h index 9301adda6..18d4e9c48 100644 --- a/server/src/printers/TestMakefilesPrinter.h +++ b/server/src/printers/TestMakefilesPrinter.h @@ -30,8 +30,9 @@ namespace printer { CollectionUtils::FileSet const *stubSources); TestMakefilesPrinter( - utbot::ProjectContext projectContext, - std::shared_ptr buildDatabase, + const BaseTestGen &testGen, +// utbot::ProjectContext projectContext, +// std::shared_ptr buildDatabase, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources); diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index d85c28998..4227d0b74 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -45,11 +45,10 @@ void BaseTestGen::setInitializedTestsMap() { } void BaseTestGen::setTargetPath(fs::path _targetPath) { -// if (buildDatabase->getTargetPath() != _targetPath) { -// if (_targetPath != GrpcUtils::UTBOT_AUTO_TARGET_PATH) { -// updateTargetSources(_targetPath); -// } -// } + if (buildDatabase->hasAutoTarget() && buildDatabase->getTargetPath() != _targetPath) { + buildDatabase = std::move(baseBuildDatabase->createBaseForTarget(_targetPath)); + updateTargetSources(_targetPath); + } } void BaseTestGen::updateTargetSources(fs::path _targetPath) { diff --git a/server/test/framework/Regression_Tests.cpp b/server/test/framework/Regression_Tests.cpp index bf1c30121..4c91485e5 100644 --- a/server/test/framework/Regression_Tests.cpp +++ b/server/test/framework/Regression_Tests.cpp @@ -88,9 +88,10 @@ namespace { // struct definition, declaration, usage in separate files TEST_F(Regression_Test, Incomplete_Array_Type) { fs::path folderPath = suitePath / "SAT-760"; + //TODO auto target auto projectRequest = testUtils::createProjectRequest( projectName, suitePath, buildDirRelativePath, { suitePath, folderPath }, - GrpcUtils::UTBOT_AUTO_TARGET_PATH); + "SAT-760"); auto request = GrpcUtils::createFolderRequest(std::move(projectRequest), folderPath); auto testGen = FolderTestGen(*request, writer.get(), TESTMODE); // testUtils::setTargetForFirstSource(testGen); diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index bf5aea359..8b5d31b0d 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -56,31 +56,43 @@ namespace { void generateFiles(const fs::path &sourceFile, const fs::path &testsRelativeDir) { fs::path testsDirPath = getTestFilePath(testsRelativeDir); - utbot::ProjectContext projectContext{ projectName, suitePath, testsDirPath, - buildDirRelativePath }; - generateFiles(sourceFile, projectContext); + + auto projectContext = GrpcUtils::createProjectContext( + projectName, suitePath, testsDirPath, buildDirRelativePath); +// utbot::ProjectContext projectContext{ projectName, suitePath, testsDirPath, +// buildDirRelativePath }; + + auto settingsContext = GrpcUtils::createSettingsContext(true, false, 30, 0, false, false); + + auto request = GrpcUtils::createProjectRequest(std::move(projectContext), + std::move(settingsContext), + srcPaths, + sourceFile); + + auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); + + generateFiles(sourceFile, testGen); } - void generateFiles(const fs::path &sourceFile, - const utbot::ProjectContext &projectContext) { + void generateFiles(const fs::path &sourceFile, const BaseTestGen& testGen) { fs::path serverBuildDir = buildPath / "temp"; - auto buildDatabase = BuildDatabase(buildPath, serverBuildDir, projectContext, false).createBaseForTarget( - sourceFile); +// auto buildDatabase = BuildDatabase(buildPath, serverBuildDir, projectContext, false).createBaseForTarget( +// sourceFile); fs::path compilerPath = CompilationUtils::getBundledCompilerPath(compilerName); CollectionUtils::FileSet stubsSources; - fs::path root = buildDatabase->getTargetPath(); - printer::TestMakefilesPrinter testMakefilePrinter(projectContext, buildDatabase, - root, compilerPath, &stubsSources); + fs::path root = testGen.buildDatabase->getTargetPath(); + printer::TestMakefilesPrinter testMakefilePrinter(testGen, root, compilerPath, &stubsSources); testMakefilePrinter.addLinkTargetRecursively(root, ""); testMakefilePrinter.GetMakefiles(sourceFile).write(); auto compilationDatabase = CompilationUtils::getCompilationDatabase(buildPath); auto structsToDeclare = std::make_shared(); - SourceToHeaderRewriter sourceToHeaderRewriter(projectContext, compilationDatabase, + SourceToHeaderRewriter sourceToHeaderRewriter(testGen.projectContext, compilationDatabase, structsToDeclare, serverBuildDir); std::string wrapper = sourceToHeaderRewriter.generateWrapper(sourceFile); - printer::SourceWrapperPrinter(Paths::getSourceLanguage(sourceFile)).print(projectContext, sourceFile, wrapper); + printer::SourceWrapperPrinter(Paths::getSourceLanguage(sourceFile)).print(testGen.projectContext, + sourceFile, wrapper); } diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index 4209b9c79..768b840b1 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -169,13 +169,13 @@ namespace { auto root = testGen.baseBuildDatabase->getRootForSource(foreign_bar_c); auto linkUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(root); - auto stubFiles = testGen.baseBuildDatabase->getStubFiles(linkUnitInfo); - auto stubCandidates = { calc_sum_c }; - auto expectedStubFiles = CollectionUtils::transformTo( - stubCandidates, [&testGen](fs::path const &path) { - return Paths::sourcePathToStubPath(testGen.projectContext, path); - }); - EXPECT_EQ(expectedStubFiles, stubFiles); +// auto stubFiles = testGen.baseBuildDatabase->getStubFiles(linkUnitInfo); +// auto stubCandidates = { calc_sum_c }; +// auto expectedStubFiles = CollectionUtils::transformTo( +// stubCandidates, [&testGen](fs::path const &path) { +// return Paths::sourcePathToStubPath(testGen.projectContext, path); +// }); +// EXPECT_EQ(expectedStubFiles, stubFiles); } TEST_F(Stub_Test, File_Tests_With_Stubs) { From 6bdf1dd57b56ee4a47fe6ac82f10eabd35c20356 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Tue, 9 Aug 2022 13:12:31 +0300 Subject: [PATCH 16/27] pass tests --- server/src/building/BuildDatabase.cpp | 4 ---- server/test/framework/Targets_Test.cpp | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 702db33f0..ab9f4a9b4 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -717,10 +717,6 @@ bool BuildDatabase::ObjectFileInfo::is32bits() const { return CollectionUtils::contains(command.getCommandLine(), "-m32"); } -void BuildDatabase::TargetInfo::addFile(fs::path file) { - files.insert(std::move(file)); -} - fs::path BuildDatabase::TargetInfo::getOutput() const { return commands[0].getOutput(); } diff --git a/server/test/framework/Targets_Test.cpp b/server/test/framework/Targets_Test.cpp index c103b11f1..5202d78c9 100644 --- a/server/test/framework/Targets_Test.cpp +++ b/server/test/framework/Targets_Test.cpp @@ -70,7 +70,7 @@ TEST_F(TargetsTest, Valid_Target_Test_dummy) { // testGen.setTargetPath(dummy); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); - ASSERT_TRUE(status.ok()) << status.error_message(); + ASSERT_FALSE(status.ok()); int numberOfTests = testUtils::getNumberOfTests(testGen.tests); EXPECT_EQ(0, numberOfTests); From bbe940f27908749818a1be17e85659d671140270 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Tue, 9 Aug 2022 19:04:06 +0300 Subject: [PATCH 17/27] start refactoring --- server/src/BordersFinder.cpp | 1 - server/src/BordersFinder.h | 1 - server/src/KleeGenerator.cpp | 30 -------- server/src/KleeGenerator.h | 43 +++-------- server/src/building/BuildDatabase.cpp | 9 +-- server/src/building/BuildDatabase.h | 13 +--- server/src/building/Linker.cpp | 18 +---- server/src/building/Linker.h | 2 - server/src/printers/NativeMakefilePrinter.cpp | 5 -- server/src/printers/NativeMakefilePrinter.h | 4 - server/src/printers/TestMakefilesPrinter.cpp | 2 - server/src/printers/TestMakefilesPrinter.h | 2 - server/src/testgens/BaseTestGen.h | 1 - server/src/testgens/FileTestGen.cpp | 6 +- server/src/testgens/ProjectTestGen.cpp | 3 +- server/src/testgens/SnippetTestGen.cpp | 3 +- server/src/utils/GenerationUtils.h | 8 -- server/test/framework/KleeGen_Tests.cpp | 75 ++++++++++--------- server/test/framework/Regression_Tests.cpp | 5 +- server/test/framework/Server_Tests.cpp | 19 ----- server/test/framework/Stub_Tests.cpp | 5 +- server/test/framework/Syntax_Tests.cpp | 3 +- server/test/framework/Targets_Test.cpp | 14 ---- server/test/framework/TestUtils.cpp | 16 ---- server/test/framework/TestUtils.h | 2 - .../suites/library-project/CMakeLists.txt | 4 +- 26 files changed, 71 insertions(+), 223 deletions(-) diff --git a/server/src/BordersFinder.cpp b/server/src/BordersFinder.cpp index 342ac086e..fee93667b 100644 --- a/server/src/BordersFinder.cpp +++ b/server/src/BordersFinder.cpp @@ -17,7 +17,6 @@ BordersFinder::BordersFinder(const fs::path &filePath, const std::shared_ptr &compilationDatabase, const fs::path &compileCommandsJsonPath) : line(line), classBorder(std::nullopt), clangToolRunner(compilationDatabase) { -// buildRootPath = Paths::subtractPath(compileCommandsJsonPath.string(), CompilationUtils::UTBOT_BUILD_DIR_NAME); lineInfo.filePath = filePath; } diff --git a/server/src/BordersFinder.h b/server/src/BordersFinder.h index 1707083b6..71fd76f61 100644 --- a/server/src/BordersFinder.h +++ b/server/src/BordersFinder.h @@ -31,7 +31,6 @@ class BordersFinder : public clang::ast_matchers::MatchFinder::MatchCallback { private: unsigned line; LineInfo lineInfo{}; -// fs::path buildRootPath; struct Borders { struct Position { unsigned line; diff --git a/server/src/KleeGenerator.cpp b/server/src/KleeGenerator.cpp index 35da7f43c..2e906baa1 100644 --- a/server/src/KleeGenerator.cpp +++ b/server/src/KleeGenerator.cpp @@ -16,36 +16,6 @@ using namespace tests; -//KleeGenerator::KleeGenerator( -// utbot::testGen.projectContext testGen.projectContext, -// utbot::SettingsContext settingsContext, -// fs::path serverBuildDir, -// std::shared_ptr compilationDatabase, -// types::TypesHandler &typesHandler, -// PathSubstitution filePathsSubstitution, -// std::shared_ptr testGen.buildDatabase, -// ProgressWriter const *progressWriter) -// : testGen.projectContext(std::move(testGen.projectContext)), -// settingsContext(std::move(settingsContext)), -// testGen.serverBuildDir(std::move(serverBuildDir)), -// compilationDatabase(std::move(compilationDatabase)), -// typesHandler(typesHandler), -// pathSubstitution(std::move(filePathsSubstitution)), -// testGen.buildDatabase(std::move(testGen.buildDatabase)), -// progressWriter(progressWriter) { -// try { -// fs::create_directories(this->testGen.serverBuildDir); -// fs::create_directories(Paths::getLogDir(this->testGen.projectContext.projectName)); -// } catch (const fs::filesystem_error &e) { -// throw FileSystemException("create_directories failed", e); -// } -//} -// -//auto generator = std::make_shared( -// testGen.testGen.projectContext, testGen.settingsContext, -// testGen.serverBuildDir, testGen.testGen.buildDatabase->compilationDatabase, typesHandler, -// pathSubstitution, testGen.testGen.buildDatabase, testGen.progressWriter); - KleeGenerator::KleeGenerator(BaseTestGen &_testGen, types::TypesHandler &typesHandler, PathSubstitution filePathsSubstitution) : testGen(_testGen), typesHandler(typesHandler), diff --git a/server/src/KleeGenerator.h b/server/src/KleeGenerator.h index afabff4bf..8c89aa619 100644 --- a/server/src/KleeGenerator.h +++ b/server/src/KleeGenerator.h @@ -33,31 +33,16 @@ class KleeGenerator { using TestsMap = tests::TestsMap; public: -// /** -// * @brief Also creates tmp directories for build files. -// * @param projectContext contains context about current project. -// * @param settingsContext contains context about settings applied for current request. -// * @param serverBuildDir Path to folder on server machine where project build dirs are located. -// * @param sourcesFilePaths Paths to project files. Files which are listed in -// * [compile_commands.json](https://clang.llvm.org/docs/JSONCompilationDatabase.html), filtered -// * by them. -// * @param compilationDatabase Pointer to compile_commands.json object. -// * @param typesHandler provides additional information about types. -// * @param filePathsSubstitution Mapping from source file path to modified file. Required for -// * line test generation requests. -// * @param buildDatabase Instance of BuildDatabase which handles link and compile commands -// * @throws fs::filesystem_error Thrown if it can't create tmp folder for some -// * reasons. -// */ -// KleeGenerator(utbot::ProjectContext projectContext, -// utbot::SettingsContext settingsContext, -// fs::path serverBuildDir, -// std::shared_ptr compilationDatabase, -// types::TypesHandler &typesHandler, -// PathSubstitution filePathsSubstitution, -// std::shared_ptr buildDatabase = nullptr, -// const ProgressWriter *progressWriter = DummyStreamWriter::getInstance()); - + /** + * @brief Also creates tmp directories for build files. + * @param _testGen contains context. + * @param typesHandler provides additional information about types. + * @param filePathsSubstitution Mapping from source file path to modified file. Required for + * line test generation requests. + * @param buildDatabase Instance of BuildDatabase which handles link and compile commands + * @throws fs::filesystem_error Thrown if it can't create tmp folder for some + * reasons. + */ KleeGenerator(BaseTestGen &_testGen, types::TypesHandler &typesHandler, PathSubstitution filePathsSubstitution); @@ -161,15 +146,9 @@ class KleeGenerator { const CollectionUtils::FileSet &stubSources) const; private: -// const utbot::ProjectContext projectContext; -// const utbot::SettingsContext settingsContext; - BaseTestGen &testGen;; -// fs::path projectTmpPath; -// std::shared_ptr compilationDatabase; + BaseTestGen &testGen; types::TypesHandler typesHandler; PathSubstitution pathSubstitution; -// std::shared_ptr buildDatabase; -// const ProgressWriter *progressWriter; CollectionUtils::MapFileTo> failedFunctions; diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index ab9f4a9b4..c4c3429e4 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -33,8 +33,7 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath, BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, - utbot::ProjectContext _projectContext, - bool createClangCC) : + utbot::ProjectContext _projectContext) : serverBuildDir(std::move(_serverBuildDir)), projectContext(std::move(_projectContext)), buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), @@ -53,9 +52,7 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, filterInstalledFiles(); addLocalSharedLibraries(); fillTargetInfoParents(); -// if (createClangCC) { - createClangCompileCommandsJson(); -// } + createClangCompileCommandsJson(); target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; } @@ -131,7 +128,7 @@ std::shared_ptr BuildDatabase::create(const utbot::ProjectContext CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( projectContext.projectPath, projectContext.buildDirRelativePath); fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); - std::shared_ptr buildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, true); + std::shared_ptr buildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); return buildDatabase; } diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 747e1a284..922da217a 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -97,8 +97,7 @@ class BuildDatabase { public: BuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, - utbot::ProjectContext _projectContext, - bool createClangCC); + utbot::ProjectContext _projectContext); static std::shared_ptr create(const utbot::ProjectContext &projectContext); std::shared_ptr createBaseForTarget(const std::string &target); @@ -197,16 +196,6 @@ class BuildDatabase { */ std::vector> getAllCompileCommands() const; -// /** -// * @brief Gets all stub files associated with given link unit -// * -// * @param linkUnitInfo link unit info (preferably library) -// * -// * @return set of file paths to stubs -// */ -// CollectionUtils::FileSet -// getStubFiles(const std::shared_ptr &linkUnitInfo) const; - /** * @brief Assign set of file paths to stubs to given link unit * diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index a189ed963..02a329683 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -54,7 +54,7 @@ fs::path Linker::getSourceFilePath() { Result Linker::linkForTarget(const fs::path &target, const fs::path &sourceFilePath, const std::shared_ptr &compilationUnitInfo, const fs::path &objectFile) { -// testGen.setTargetPath(target); + testGen.setTargetPath(target); auto siblings = testGen.buildDatabase->getArchiveObjectFiles(target); auto stubSources = stubGen.getStubSources(target); @@ -84,14 +84,6 @@ Result Linker::linkForTarget(const fs::path &target, const f return stubsSetResult; } -//std::vector Linker::getTargetList(const fs::path &sourceFile, const fs::path &objectFile) const { -// if (testGen.hasAutoTarget()) { -// return testGen.buildDatabase->targetListForFile(sourceFile, objectFile); -// } else { -// return {testGen.buildDatabase->getTargetPath()}; -// } -//} - void Linker::linkForOneFile(const fs::path &sourceFilePath) { ExecUtils::throwIfCancelled(); @@ -104,7 +96,6 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { if (!testGen.buildDatabase->isFirstObjectFileForSource(objectFile)) { return; } -// std::vector targets = getTargetList(sourceFilePath, objectFile); std::vector targets = testGen.buildDatabase->targetListForFile(sourceFilePath, objectFile); LOG_S(DEBUG) << "Linking bitcode for file " << sourceFilePath.filename(); for (size_t i = 0; i < targets.size(); i++) { @@ -131,7 +122,7 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { Result Linker::linkWholeTarget(const fs::path &target) { auto requestTarget = testGen.buildDatabase->getTargetPath(); - LOG_IF_S(ERROR, target != GrpcUtils::UTBOT_AUTO_TARGET_PATH && requestTarget != target) + LOG_IF_S(WARNING, !testGen.buildDatabase->hasAutoTarget() && requestTarget != target) << "Try link target that not specified by user"; testGen.setTargetPath(target); @@ -153,11 +144,11 @@ Result Linker::linkWholeTarget(const fs::path &target) { } if (!CollectionUtils::contains(testedFiles, objectInfo->getSourcePath()) && insideFolder) { fs::path bitcodeFile = objectInfo->kleeFilesInfo->getKleeBitcodeFile(); - filesToLink.emplace(objectFile, std::move(bitcodeFile)); + filesToLink.emplace(objectFile, bitcodeFile); } else { fs::path bitcodeFile = testGen.buildDatabase->getBitcodeForSource(objectInfo->getSourcePath()); siblingObjectsToBuild.insert(objectInfo->getOutputFile()); - filesToLink.emplace(objectFile, std::move(bitcodeFile)); + filesToLink.emplace(objectFile, bitcodeFile); } } @@ -184,7 +175,6 @@ void Linker::linkForProject() { << sourceFile; return; } -// std::vector targets = getTargetList(sourceFile, objectFile); std::vector targets = testGen.buildDatabase->targetListForFile(sourceFile, objectFile); bool success = false; for (const auto &target : targets) { diff --git a/server/src/building/Linker.h b/server/src/building/Linker.h index d12b13b48..664a4d08a 100644 --- a/server/src/building/Linker.h +++ b/server/src/building/Linker.h @@ -62,8 +62,6 @@ class Linker { bool isForOneFile(); -// std::vector getTargetList(const fs::path &sourceFile, const fs::path &objectFile) const; - Result linkForTarget(const fs::path &target, const fs::path &sourceFilePath, const std::shared_ptr &compilationUnitInfo, const fs::path &objectFile); diff --git a/server/src/printers/NativeMakefilePrinter.cpp b/server/src/printers/NativeMakefilePrinter.cpp index 8fa48231b..4a047955b 100644 --- a/server/src/printers/NativeMakefilePrinter.cpp +++ b/server/src/printers/NativeMakefilePrinter.cpp @@ -116,8 +116,6 @@ namespace printer { } NativeMakefilePrinter::NativeMakefilePrinter( -// utbot::ProjectContext projectContext, -// std::shared_ptr buildDatabase, const BaseTestGen& testGen, fs::path const &rootPath, fs::path primaryCompiler, @@ -125,7 +123,6 @@ namespace printer { std::map> pathToShellVariable) : RelativeMakefilePrinter(pathToShellVariable), testGen(testGen), -// projectContext(std::move(projectContext)), buildDatabase(std::move(buildDatabase)), rootPath(std::move(rootPath)), primaryCompiler(std::move(primaryCompiler)), primaryCxxCompiler(CompilationUtils::toCppCompiler(this->primaryCompiler)), @@ -408,8 +405,6 @@ namespace printer { NativeMakefilePrinter::NativeMakefilePrinter(const NativeMakefilePrinter &baseMakefilePrinter, const fs::path &sourcePath) : RelativeMakefilePrinter(baseMakefilePrinter.pathToShellVariable), -// projectContext(baseMakefilePrinter.projectContext), -// buildDatabase(baseMakefilePrinter.buildDatabase), testGen(baseMakefilePrinter.testGen), rootPath(baseMakefilePrinter.rootPath), primaryCompiler(baseMakefilePrinter.primaryCompiler), diff --git a/server/src/printers/NativeMakefilePrinter.h b/server/src/printers/NativeMakefilePrinter.h index fc4dbd5ea..cf379d80b 100644 --- a/server/src/printers/NativeMakefilePrinter.h +++ b/server/src/printers/NativeMakefilePrinter.h @@ -16,8 +16,6 @@ namespace printer { friend class TestMakefilesPrinter; private: const BaseTestGen& testGen; -// const utbot::ProjectContext projectContext; -// std::shared_ptr buildDatabase; fs::path rootPath; fs::path primaryCompiler; @@ -69,8 +67,6 @@ namespace printer { public: NativeMakefilePrinter(const BaseTestGen& testGen, -// utbot::ProjectContext projectContext, -// std::shared_ptr buildDatabase, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources, diff --git a/server/src/printers/TestMakefilesPrinter.cpp b/server/src/printers/TestMakefilesPrinter.cpp index 57cc1c501..f7f015f6b 100644 --- a/server/src/printers/TestMakefilesPrinter.cpp +++ b/server/src/printers/TestMakefilesPrinter.cpp @@ -42,8 +42,6 @@ namespace printer { TestMakefilesPrinter::TestMakefilesPrinter( const BaseTestGen &testGen, -// utbot::ProjectContext projectContext, -// std::shared_ptr buildDatabase, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources) : diff --git a/server/src/printers/TestMakefilesPrinter.h b/server/src/printers/TestMakefilesPrinter.h index 18d4e9c48..4e9c57d6d 100644 --- a/server/src/printers/TestMakefilesPrinter.h +++ b/server/src/printers/TestMakefilesPrinter.h @@ -31,8 +31,6 @@ namespace printer { TestMakefilesPrinter( const BaseTestGen &testGen, -// utbot::ProjectContext projectContext, -// std::shared_ptr buildDatabase, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources); diff --git a/server/src/testgens/BaseTestGen.h b/server/src/testgens/BaseTestGen.h index 19162c8a9..c52b1ddd3 100644 --- a/server/src/testgens/BaseTestGen.h +++ b/server/src/testgens/BaseTestGen.h @@ -23,7 +23,6 @@ class BaseTestGen { fs::path serverBuildDir; fs::path compileCommandsJsonPath; -// std::shared_ptr compilationDatabase; std::shared_ptr baseBuildDatabase; std::shared_ptr buildDatabase; diff --git a/server/src/testgens/FileTestGen.cpp b/server/src/testgens/FileTestGen.cpp index 0f3dba1fa..47af24915 100644 --- a/server/src/testgens/FileTestGen.cpp +++ b/server/src/testgens/FileTestGen.cpp @@ -5,9 +5,9 @@ FileTestGen::FileTestGen(const testsgen::FileRequest &request, ProgressWriter *progressWriter, bool testMode) - : ProjectTestGen(request.projectrequest(), progressWriter, testMode, false), - filepath(fs::weakly_canonical(request.filepath())) { - testingMethodsSourcePaths = { filepath }; + : ProjectTestGen(request.projectrequest(), progressWriter, testMode, false), + filepath(fs::weakly_canonical(request.filepath())) { + testingMethodsSourcePaths = {filepath}; setInitializedTestsMap(); } diff --git a/server/src/testgens/ProjectTestGen.cpp b/server/src/testgens/ProjectTestGen.cpp index d51076eee..6ca6ae431 100644 --- a/server/src/testgens/ProjectTestGen.cpp +++ b/server/src/testgens/ProjectTestGen.cpp @@ -16,9 +16,8 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, fs::create_directories(projectContext.testDirPath); compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( projectContext.projectPath, projectContext.buildDirRelativePath); - baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, false); + baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); buildDatabase = baseBuildDatabase->createBaseForTarget(request.targetpath()); -// compilationDatabase = CompilationUtils::getCompilationDatabase(compileCommandsJsonPath); if (autoSrcPaths) { autoDetectSourcePathsIfNotEmpty(); } else { diff --git a/server/src/testgens/SnippetTestGen.cpp b/server/src/testgens/SnippetTestGen.cpp index 01914ec6f..c04a8e1d3 100644 --- a/server/src/testgens/SnippetTestGen.cpp +++ b/server/src/testgens/SnippetTestGen.cpp @@ -18,9 +18,8 @@ SnippetTestGen::SnippetTestGen(const testsgen::SnippetRequest &request, printer::CCJsonPrinter::createDummyBuildDB(sourcePaths, serverBuildDir); compileCommandsJsonPath = serverBuildDir; utbot::ProjectContext projectContext{request, serverBuildDir}; - baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext, false); + baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); buildDatabase = baseBuildDatabase->createBaseForTarget(serverBuildDir / SNIPPET_TARGET); -// compilationDatabase = CompilationUtils::getCompilationDatabase(serverBuildDir); setTargetForSource(filePath); setInitializedTestsMap(); } diff --git a/server/src/utils/GenerationUtils.h b/server/src/utils/GenerationUtils.h index f8c9908fd..796f4a987 100644 --- a/server/src/utils/GenerationUtils.h +++ b/server/src/utils/GenerationUtils.h @@ -38,14 +38,6 @@ namespace GenerationUtils { ServerUtils::setThreadOptions(ctx, true); auto testsWriter = std::make_unique(); auto testGen = std::make_unique(request, testsWriter.get(), true); -// if constexpr (std::is_base_of_v) { -// auto targetPath = findTarget(*testGen, testGen->getRequest()->targetpath()); -// if (!targetPath.has_value()) { -// auto status = grpc::Status(grpc::INVALID_ARGUMENT, "Couldn't find appropriate target"); -// return std::make_pair(std::move(testGen), status); -// } -// testGen->setTargetPath(targetPath.value()); -// } Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(*testGen, testsWriter.get()); if (status.error_code() == grpc::FAILED_PRECONDITION) { diff --git a/server/test/framework/KleeGen_Tests.cpp b/server/test/framework/KleeGen_Tests.cpp index ced1b5e5c..073136789 100644 --- a/server/test/framework/KleeGen_Tests.cpp +++ b/server/test/framework/KleeGen_Tests.cpp @@ -45,47 +45,54 @@ namespace { } // KleeGenerator initKleeGenerator(const TestSuite &suite, std::string &errorMessage) { -// std::shared_ptr compilationDatabase = -// CompilationDatabase::autoDetectFromDirectory( -// suite.buildPath.string(), errorMessage); // types::TypesHandler::SizeContext sizeContext; // types::TypeMaps typeMaps; // types::TypesHandler typesHandler(typeMaps, sizeContext); // fs::path testsDirPath = tmpDirPath / "test"; -// utbot::ProjectContext projectContext{ suite.name, "", testsDirPath, -// buildDirRelativePath }; -// auto buildDatabase = std::make_shared(suite.buildPath, suite.buildPath, projectContext, true); -// utbot::SettingsContext settingsContext{ true, true, 15, 0, true, false }; -// KleeGenerator generator(std::move(projectContext), -// std::move(settingsContext), -// tmpDirPath, -// compilationDatabase, -// typesHandler, -// {}, -// buildDatabase); +// auto projectContext = GrpcUtils::createProjectContext(suite.name, suitePath, testsDirPath, buildDirRelativePath); +// auto settingsContext = GrpcUtils::createSettingsContext(true, true, 15, 0, true, false); +// +// auto request = GrpcUtils::createProjectRequest(std::move(projectContext), +// std::move(settingsContext), +// srcPaths, +// GrpcUtils::UTBOT_AUTO_TARGET_PATH); +// +// auto request = testUtils::createProjectRequest(suite.name, suitePath, buildDirRelativePath, {}); +// +// auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); +// +// KleeGenerator generator(testGen, typesHandler, {}); // return generator; // } }; -// TEST_F(KleeGen_Test, BuildByCDb) { -// std::string errorMessage; -// auto generator = initKleeGenerator(testSuite, errorMessage); -// ASSERT_TRUE(errorMessage.empty()); -// CollectionUtils::FileSet sources(testSuite.sourcesFilePaths.begin(), testSuite.sourcesFilePaths.end()); -// sources.erase(getTestFilePath("snippet.c")); -// sources.erase(getTestFilePath("external_dependent.c")); -// auto outFilesPaths = generator.buildByCDb(sources); -// for (const auto &[actualFilePath, srcFilePath] : outFilesPaths) { -// EXPECT_TRUE(fs::exists(actualFilePath)) << testUtils::fileNotExistsMessage(actualFilePath); -// } -// } + TEST_F(KleeGen_Test, BuildByCDb) { + types::TypesHandler::SizeContext sizeContext; + types::TypeMaps typeMaps; + types::TypesHandler typesHandler(typeMaps, sizeContext); + auto request = testUtils::createProjectRequest(testSuite.name, suitePath, buildDirRelativePath, {}); + auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); + KleeGenerator generator(testGen, typesHandler, {}); + + CollectionUtils::FileSet sources(testSuite.sourcesFilePaths.begin(), testSuite.sourcesFilePaths.end()); + sources.erase(getTestFilePath("snippet.c")); + sources.erase(getTestFilePath("external_dependent.c")); + auto outFilesPaths = generator.buildByCDb(sources); + for (const auto &[actualFilePath, srcFilePath] : outFilesPaths) { + EXPECT_TRUE(fs::exists(actualFilePath)) << testUtils::fileNotExistsMessage(actualFilePath); + } + } + + TEST_F(KleeGen_Test, DefaultBuild) { + types::TypesHandler::SizeContext sizeContext; + types::TypeMaps typeMaps; + types::TypesHandler typesHandler(typeMaps, sizeContext); + auto request = testUtils::createProjectRequest(testSuite.name, suitePath, buildDirRelativePath, {}); + auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); + KleeGenerator generator(testGen, typesHandler, {}); -// TEST_F(KleeGen_Test, DefaultBuild) { -// std::string errorMessage; -// auto generator = initKleeGenerator(testSuite, errorMessage); -// ASSERT_TRUE(errorMessage.empty()); -// fs::path sourceFilePath = *testSuite.sourcesFilePaths.begin(); -// auto actualFilePath = generator.defaultBuild(sourceFilePath); -// EXPECT_TRUE(fs::exists(actualFilePath.getOpt().value())); -// } + fs::path sourceFilePath = *testSuite.sourcesFilePaths.begin(); + auto actualFilePath = generator.defaultBuild(sourceFilePath); + EXPECT_TRUE(fs::exists(actualFilePath.getOpt().value())); + } } diff --git a/server/test/framework/Regression_Tests.cpp b/server/test/framework/Regression_Tests.cpp index 4c91485e5..0df4c355d 100644 --- a/server/test/framework/Regression_Tests.cpp +++ b/server/test/framework/Regression_Tests.cpp @@ -88,13 +88,10 @@ namespace { // struct definition, declaration, usage in separate files TEST_F(Regression_Test, Incomplete_Array_Type) { fs::path folderPath = suitePath / "SAT-760"; - //TODO auto target auto projectRequest = testUtils::createProjectRequest( - projectName, suitePath, buildDirRelativePath, { suitePath, folderPath }, - "SAT-760"); + projectName, suitePath, buildDirRelativePath, { suitePath, folderPath }, "SAT-760"); auto request = GrpcUtils::createFolderRequest(std::move(projectRequest), folderPath); auto testGen = FolderTestGen(*request, writer.get(), TESTMODE); -// testUtils::setTargetForFirstSource(testGen); fs::path source1 = folderPath / "SAT-760_1.c"; fs::path source2 = folderPath / "SAT-760_2.c"; diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index 8b5d31b0d..81a5e05f3 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -59,8 +59,6 @@ namespace { auto projectContext = GrpcUtils::createProjectContext( projectName, suitePath, testsDirPath, buildDirRelativePath); -// utbot::ProjectContext projectContext{ projectName, suitePath, testsDirPath, -// buildDirRelativePath }; auto settingsContext = GrpcUtils::createSettingsContext(true, false, 30, 0, false, false); @@ -76,8 +74,6 @@ namespace { void generateFiles(const fs::path &sourceFile, const BaseTestGen& testGen) { fs::path serverBuildDir = buildPath / "temp"; -// auto buildDatabase = BuildDatabase(buildPath, serverBuildDir, projectContext, false).createBaseForTarget( -// sourceFile); fs::path compilerPath = CompilationUtils::getBundledCompilerPath(compilerName); CollectionUtils::FileSet stubsSources; fs::path root = testGen.buildDatabase->getTargetPath(); @@ -646,7 +642,6 @@ namespace { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -669,7 +664,6 @@ namespace { { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "ex"); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -688,8 +682,6 @@ namespace { { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "one"); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// fs::path one = testGen.buildDatabase->getClientLinkUnitInfo(a_c)->getOutput(); -// testGen.setTargetPath(one); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -705,8 +697,6 @@ namespace { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "two"); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// fs::path two = testGen.buildDatabase->getClientLinkUnitInfo(b_c)->getOutput(); -// testGen.setTargetPath(two); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -728,7 +718,6 @@ namespace { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -924,7 +913,6 @@ namespace { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -946,7 +934,6 @@ namespace { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -971,7 +958,6 @@ namespace { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -1007,7 +993,6 @@ namespace { GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto request = GrpcUtils::createFolderRequest(std::move(projectRequest), suitePath / "inner"); auto testGen = FolderTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -1474,7 +1459,6 @@ namespace { GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), source2_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -1588,7 +1572,6 @@ namespace { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -1602,7 +1585,6 @@ namespace { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -1620,7 +1602,6 @@ namespace { auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, GrpcUtils::UTBOT_AUTO_TARGET_PATH); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// setTargetForFirstSource(testGen); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index 768b840b1..27062f30c 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -167,8 +167,8 @@ namespace { ASSERT_TRUE(status.ok()) << status.error_message(); EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 2); - auto root = testGen.baseBuildDatabase->getRootForSource(foreign_bar_c); - auto linkUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(root); +// auto root = testGen.baseBuildDatabase->getRootForSource(foreign_bar_c); +// auto linkUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(root); // auto stubFiles = testGen.baseBuildDatabase->getStubFiles(linkUnitInfo); // auto stubCandidates = { calc_sum_c }; // auto expectedStubFiles = CollectionUtils::transformTo( @@ -182,7 +182,6 @@ namespace { auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath, srcPaths, literals_foo_c, literals_foo_c, true); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); -// testGen.setTargetForSource(literals_foo_c); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 5); diff --git a/server/test/framework/Syntax_Tests.cpp b/server/test/framework/Syntax_Tests.cpp index 38c806794..0cfe70559 100644 --- a/server/test/framework/Syntax_Tests.cpp +++ b/server/test/framework/Syntax_Tests.cpp @@ -81,11 +81,10 @@ namespace { std::pair createTestForFunction(const fs::path &pathToFile, int lineNum, int kleeTimeout = 60) { auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath, - srcPaths, pathToFile, lineNum, GrpcUtils::UTBOT_AUTO_TARGET_PATH, + srcPaths, pathToFile, lineNum, pathToFile, false, false, kleeTimeout); auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest)); auto testGen = FunctionTestGen(*request, writer.get(), TESTMODE); - testGen.setTargetForSource(pathToFile); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); size_t failed_build = TestRunner::buildTests(testGen.projectContext, testGen.tests); if (status.ok() && failed_build != 0) { diff --git a/server/test/framework/Targets_Test.cpp b/server/test/framework/Targets_Test.cpp index 5202d78c9..2a4547d64 100644 --- a/server/test/framework/Targets_Test.cpp +++ b/server/test/framework/Targets_Test.cpp @@ -25,8 +25,6 @@ TEST_F(TargetsTest, Valid_Target_Test_ls) { createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "ls"); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); -// fs::path ls = getTargetPathByName(*testGen.buildDatabase, "ls"); -// testGen.setTargetPath(ls); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -46,8 +44,6 @@ TEST_F(TargetsTest, Valid_Target_Test_cat) { createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "cat"); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); -// fs::path cat = getTargetPathByName(*testGen.buildDatabase, "cat"); -// testGen.setTargetPath(cat); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -66,8 +62,6 @@ TEST_F(TargetsTest, Valid_Target_Test_dummy) { auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths, "dummy"); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); -// fs::path dummy = getTargetPathByName(*testGen.buildDatabase, "dummy"); -// testGen.setTargetPath(dummy); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_FALSE(status.ok()); @@ -80,8 +74,6 @@ TEST_F(TargetsTest, Valid_Target_Test_parse) { auto projectRequest = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths); auto request = GrpcUtils::createFileRequest(std::move(projectRequest), parse_c); auto testGen = FileTestGen(*request, writer.get(), TESTMODE); -// fs::path autoTarget = GrpcUtils::UTBOT_AUTO_TARGET_PATH; -// testGen.setTargetPath(autoTarget); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -100,8 +92,6 @@ TEST_F(TargetsTest, Valid_Target_Test_get_10) { projectName, suitePath, buildDirRelativePath, srcPaths, "get_10", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); -// fs::path get_10 = getTargetPathByName(*testGen.buildDatabase, "get_10"); -// testGen.setTargetPath(get_10); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -225,8 +215,6 @@ TEST_F(TargetsTest, Valid_Target_Test_libshared) { projectName, suitePath, buildDirRelativePath, srcPaths, "libshared_get.so", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); -// fs::path lib_shared = getTargetPathByName(*testGen.buildDatabase, "libshared_get.so"); -// testGen.setTargetPath(lib_shared); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -267,8 +255,6 @@ TEST_F(TargetsTest, Valid_Target_Test_get_libstatic) { projectName, suitePath, buildDirRelativePath, srcPaths, "libstatic_get.a", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); -// fs::path lib_static = getTargetPathByName(*testGen.buildDatabase, "libstatic_get.a"); -// testGen.setTargetPath(lib_static); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); diff --git a/server/test/framework/TestUtils.cpp b/server/test/framework/TestUtils.cpp index 0bbe4bd50..7e11521f1 100644 --- a/server/test/framework/TestUtils.cpp +++ b/server/test/framework/TestUtils.cpp @@ -221,17 +221,6 @@ namespace testUtils { auto settingsContext = GrpcUtils::createSettingsContext(true, verbose, kleeTimeout, 0, false, useStubs); - -// auto buildDatabase = BuildDatabase::create(utbot::ProjectContext(*projectContext), -// GrpcUtils::UTBOT_AUTO_TARGET_PATH); -// auto rootTargets = buildDatabase->getRootTargets(); -// auto it = std::find_if(rootTargets.begin(), rootTargets.end(), -// [&target](std::shared_ptr linkUnitInfo) { -// return linkUnitInfo->getOutput().filename() == target; -// }); -// assert(it != rootTargets.end()); -// std::string new_target = it->get()->getOutput(); - return GrpcUtils::createProjectRequest(std::move(projectContext), std::move(settingsContext), srcPaths, @@ -409,11 +398,6 @@ namespace testUtils { return argv; } -// void setTargetForFirstSource(ProjectTestGen &testGen) { -// fs::path sourcePath = *testGen.testingMethodsSourcePaths.begin(); -// testGen.setTargetForSource(sourcePath); -// } - static void checkStatsCSV(const fs::path &statsPath, const std::vector &header, const std::vector &containedFiles) { EXPECT_TRUE(fs::exists(statsPath)); diff --git a/server/test/framework/TestUtils.h b/server/test/framework/TestUtils.h index a7a88b98d..86effaa28 100644 --- a/server/test/framework/TestUtils.h +++ b/server/test/framework/TestUtils.h @@ -134,8 +134,6 @@ namespace testUtils { std::vector createArgvVector(const std::vector &args); -// void setTargetForFirstSource(ProjectTestGen &testGen); - void checkGenerationStatsCSV(const fs::path &statsPath, const std::vector &containedFiles); void checkExecutionStatsCSV(const fs::path &statsPath, const std::vector &containedFiles); diff --git a/server/test/suites/library-project/CMakeLists.txt b/server/test/suites/library-project/CMakeLists.txt index a78fc66dd..d6e37191f 100644 --- a/server/test/suites/library-project/CMakeLists.txt +++ b/server/test/suites/library-project/CMakeLists.txt @@ -6,8 +6,8 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON) -#set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -#set(CMAKE_EXPORT_LINK_COMMANDS ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_EXPORT_LINK_COMMANDS ON) add_compile_options(-O2) From 9a32e2ec9e93fe891e22d13a6bcc1582cd907bbc Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Wed, 10 Aug 2022 17:27:07 +0300 Subject: [PATCH 18/27] refactoring --- server/src/KleeGenerator.h | 2 +- server/src/building/BuildDatabase.cpp | 91 ++++++------------------- server/src/building/BuildDatabase.h | 13 +--- server/src/building/Linker.cpp | 2 - server/src/testgens/BaseTestGen.cpp | 2 +- server/src/testgens/ProjectTestGen.cpp | 2 +- server/src/testgens/SnippetTestGen.cpp | 2 +- server/test/framework/KleeGen_Tests.cpp | 24 ------- server/test/framework/Stub_Tests.cpp | 12 +--- 9 files changed, 26 insertions(+), 124 deletions(-) diff --git a/server/src/KleeGenerator.h b/server/src/KleeGenerator.h index 8c89aa619..2d8669995 100644 --- a/server/src/KleeGenerator.h +++ b/server/src/KleeGenerator.h @@ -35,7 +35,7 @@ class KleeGenerator { public: /** * @brief Also creates tmp directories for build files. - * @param _testGen contains context. + * @param _testGen contains projectContext, settingsContext, BuildDatabase. * @param typesHandler provides additional information about types. * @param filePathsSubstitution Mapping from source file path to modified file. Required for * line test generation requests. diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index c4c3429e4..4d8c4f280 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -56,40 +56,15 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; } -BuildDatabase::BuildDatabase(BuildDatabase& baseBuildDatabase, +BuildDatabase::BuildDatabase(BuildDatabase &baseBuildDatabase, const std::string &_target) : serverBuildDir(baseBuildDatabase.serverBuildDir), projectContext(baseBuildDatabase.projectContext), buildCommandsJsonPath(baseBuildDatabase.buildCommandsJsonPath), linkCommandsJsonPath(baseBuildDatabase.linkCommandsJsonPath), compileCommandsJsonPath(baseBuildDatabase.compileCommandsJsonPath), - isAutoTarget(false) { - -// BuildDatabase baseBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, false); - - -// fs::path target; - //TODO target incorrect name now - if (Paths::isSourceFile(_target)) { - fs::path root = baseBuildDatabase.getRootForSource(_target); - target = root; - } else if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _target.empty()) { - fs::path root = baseBuildDatabase.getRootForFirstSource(); - target = root; - isAutoTarget = true; - } else { - auto new_target = GenerationUtils::findTarget(baseBuildDatabase.getAllTargets(), _target); - if (new_target.has_value()) { - target = new_target.value(); - } else { - throw CompilationDatabaseException("Can't find target: " + _target); - } - } - - -// CollectionUtils::MapFileTo>> sourceFileInfos; -// CollectionUtils::MapFileTo> objectFileInfos; -// CollectionUtils::MapFileTo> objectFileTargets; + target(_target), + isAutoTarget(_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { { auto objectFilesList = baseBuildDatabase.getArchiveObjectFiles(target); for (const auto &objectFilePath: objectFilesList) { @@ -103,23 +78,32 @@ BuildDatabase::BuildDatabase(BuildDatabase& baseBuildDatabase, baseBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()]; } } -// CollectionUtils::MapFileTo> targetInfos; -// CollectionUtils::MapFileTo linkUnitToStubFiles; + { auto targetFilesList = baseBuildDatabase.getArchiveTargetFiles(target); for (const auto &objectFilePath: targetFilesList) { targetInfos[objectFilePath] = baseBuildDatabase.targetInfos[objectFilePath]; - linkUnitToStubFiles[objectFilePath] = baseBuildDatabase.linkUnitToStubFiles[objectFilePath]; } } -// std::vector>> compileCommands_temp; compileCommands_temp = baseBuildDatabase.compileCommands_temp; - createClangCompileCommandsJson(); } -std::shared_ptr BuildDatabase::createBaseForTarget(const std::string &_target) { +std::shared_ptr BuildDatabase::createBuildDatabaseForSourceOrTarget(const std::string &_targetOrSourcePath) { + fs::path _target; + if (Paths::isSourceFile(_targetOrSourcePath)) { + _target = getRootForSource(_targetOrSourcePath); + } else if (_targetOrSourcePath == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _targetOrSourcePath.empty()) { + _target = getRootForFirstSource(); + } else { + auto new_target = GenerationUtils::findTarget(getAllTargets(), _targetOrSourcePath); + if (new_target.has_value()) { + _target = new_target.value(); + } else { + throw CompilationDatabaseException("Can't find target: " + _targetOrSourcePath); + } + } return std::make_shared(std::move(BuildDatabase(*this, _target))); } @@ -220,8 +204,7 @@ void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) { sourceFileInfos[sourcePath].emplace_back(objectInfo); } for (auto &[sourceFile, objectInfos]: sourceFileInfos) { - //Need stable sort for save order of 32 and 64 bits files - std::stable_sort(objectInfos.begin(), objectInfos.end(), conflictPriorityMore); + std::sort(objectInfos.begin(), objectInfos.end(), conflictPriorityMore); } } @@ -292,27 +275,6 @@ void BuildDatabase::createClangCompileCommandsJson() { compilationDatabase = CompilationUtils::getCompilationDatabase(clangCompileCommandsJsonPath); } -//void BuildDatabase::updateTarget(const fs::path &_target) { -// if (_target.string() == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { -// return; -// } -// -// for (auto &[sourceFile, objectInfos]: sourceFileInfos) { -// std::sort(objectInfos.begin(), objectInfos.end(), [&](const std::shared_ptr &left, -// const std::shared_ptr &right) { -// if (CollectionUtils::containsKey(targetInfos, target)) { -// if (CollectionUtils::containsKey(targetInfos[target]->files, left->getOutputFile())) { -// return true; -// } -// if (CollectionUtils::containsKey(targetInfos[target]->files, right->getOutputFile())) { -// return false; -// } -// } -// return false; -// }); -// } -//} - void BuildDatabase::mergeLibraryOptions(std::vector &jsonArguments) const { for (auto it = jsonArguments.begin(); it != jsonArguments.end(); it++) { if (*it == DynamicLibraryUtils::libraryDirOption || *it == DynamicLibraryUtils::linkFlag) { @@ -718,21 +680,6 @@ fs::path BuildDatabase::TargetInfo::getOutput() const { return commands[0].getOutput(); } -//CollectionUtils::FileSet BuildDatabase::getStubFiles( -// const std::shared_ptr &linkUnitInfo) const { -// auto iterator = linkUnitToStubFiles.find(linkUnitInfo->getOutput()); -// if (iterator != linkUnitToStubFiles.end()) { -// return iterator->second; -// } -// return {}; -//} - -void BuildDatabase::assignStubFilesToLinkUnit( - std::shared_ptr linkUnitInfo, - CollectionUtils::FileSet stubs) { - linkUnitToStubFiles.emplace(linkUnitInfo->getOutput(), std::move(stubs)); -} - std::vector> BuildDatabase::getRootTargets() const { return CollectionUtils::filterOut( CollectionUtils::getValues(targetInfos), diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 922da217a..f22ee4858 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -100,7 +100,7 @@ class BuildDatabase { utbot::ProjectContext _projectContext); static std::shared_ptr create(const utbot::ProjectContext &projectContext); - std::shared_ptr createBaseForTarget(const std::string &target); + std::shared_ptr createBuildDatabaseForSourceOrTarget(const std::string &_targetOrSourcePath); const fs::path &getCompileCommandsJson(); const fs::path &getLinkCommandsJson(); @@ -196,16 +196,6 @@ class BuildDatabase { */ std::vector> getAllCompileCommands() const; - /** - * @brief Assign set of file paths to stubs to given link unit - * - * @param linkUnitInfo link unit info (preferably library) - * @param stubs set of file paths to stubs - */ - void assignStubFilesToLinkUnit( - std::shared_ptr linkUnitInfo, - CollectionUtils::FileSet stubs); - std::vector> getRootTargets() const; std::vector> getAllTargets() const; @@ -239,7 +229,6 @@ class BuildDatabase { CollectionUtils::MapFileTo> objectFileInfos; CollectionUtils::MapFileTo> targetInfos; CollectionUtils::MapFileTo> objectFileTargets; - CollectionUtils::MapFileTo linkUnitToStubFiles; std::vector>> compileCommands_temp; diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 02a329683..f183ba79c 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -106,7 +106,6 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { auto [targetBitcode, stubsSet, _] = result.getOpt().value(); addToGenerated({ objectFile }, targetBitcode); auto&& targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); - testGen.buildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, stubsSet); return; } else { LOG_S(DEBUG) << "Linkage for target " << target.filename() << " failed: " << result.getError()->c_str(); @@ -193,7 +192,6 @@ void Linker::linkForProject() { }); addToGenerated(objectFiles, linkres.bitcodeOutput); auto &&targetUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(target); - testGen.baseBuildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, linkres.stubsSet); break; } else { std::stringstream ss; diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index 4227d0b74..a3daf1a60 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -46,7 +46,7 @@ void BaseTestGen::setInitializedTestsMap() { void BaseTestGen::setTargetPath(fs::path _targetPath) { if (buildDatabase->hasAutoTarget() && buildDatabase->getTargetPath() != _targetPath) { - buildDatabase = std::move(baseBuildDatabase->createBaseForTarget(_targetPath)); + buildDatabase = std::move(baseBuildDatabase->createBuildDatabaseForSourceOrTarget(_targetPath)); updateTargetSources(_targetPath); } } diff --git a/server/src/testgens/ProjectTestGen.cpp b/server/src/testgens/ProjectTestGen.cpp index 6ca6ae431..ddd8b4521 100644 --- a/server/src/testgens/ProjectTestGen.cpp +++ b/server/src/testgens/ProjectTestGen.cpp @@ -17,7 +17,7 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( projectContext.projectPath, projectContext.buildDirRelativePath); baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); - buildDatabase = baseBuildDatabase->createBaseForTarget(request.targetpath()); + buildDatabase = baseBuildDatabase->createBuildDatabaseForSourceOrTarget(request.targetpath()); if (autoSrcPaths) { autoDetectSourcePathsIfNotEmpty(); } else { diff --git a/server/src/testgens/SnippetTestGen.cpp b/server/src/testgens/SnippetTestGen.cpp index c04a8e1d3..5bb3f7887 100644 --- a/server/src/testgens/SnippetTestGen.cpp +++ b/server/src/testgens/SnippetTestGen.cpp @@ -19,7 +19,7 @@ SnippetTestGen::SnippetTestGen(const testsgen::SnippetRequest &request, compileCommandsJsonPath = serverBuildDir; utbot::ProjectContext projectContext{request, serverBuildDir}; baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); - buildDatabase = baseBuildDatabase->createBaseForTarget(serverBuildDir / SNIPPET_TARGET); + buildDatabase = baseBuildDatabase->createBuildDatabaseForSourceOrTarget(serverBuildDir / SNIPPET_TARGET); setTargetForSource(filePath); setInitializedTestsMap(); } diff --git a/server/test/framework/KleeGen_Tests.cpp b/server/test/framework/KleeGen_Tests.cpp index 073136789..525dcbd59 100644 --- a/server/test/framework/KleeGen_Tests.cpp +++ b/server/test/framework/KleeGen_Tests.cpp @@ -15,18 +15,15 @@ namespace { struct TestSuite { std::string name; - fs::path buildPath; CollectionUtils::FileSet sourcesFilePaths; }; - fs::path tmpDirPath = baseSuitePath / buildDirRelativePath; TestSuite testSuite; void SetUp() override { clearEnv(CompilationUtils::CompilerName::CLANG); testSuite = { suiteName, - buildPath, { getTestFilePath("assertion_failures.c"), getTestFilePath("basic_functions.c"), getTestFilePath("complex_structs.c"), @@ -43,27 +40,6 @@ namespace { getTestFilePath("types.c"), getTestFilePath("inner/inner_basic_functions.c") } }; } - -// KleeGenerator initKleeGenerator(const TestSuite &suite, std::string &errorMessage) { -// types::TypesHandler::SizeContext sizeContext; -// types::TypeMaps typeMaps; -// types::TypesHandler typesHandler(typeMaps, sizeContext); -// fs::path testsDirPath = tmpDirPath / "test"; -// auto projectContext = GrpcUtils::createProjectContext(suite.name, suitePath, testsDirPath, buildDirRelativePath); -// auto settingsContext = GrpcUtils::createSettingsContext(true, true, 15, 0, true, false); -// -// auto request = GrpcUtils::createProjectRequest(std::move(projectContext), -// std::move(settingsContext), -// srcPaths, -// GrpcUtils::UTBOT_AUTO_TARGET_PATH); -// -// auto request = testUtils::createProjectRequest(suite.name, suitePath, buildDirRelativePath, {}); -// -// auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); -// -// KleeGenerator generator(testGen, typesHandler, {}); -// return generator; -// } }; TEST_F(KleeGen_Test, BuildByCDb) { diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index 27062f30c..83e3cf214 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -10,6 +10,8 @@ #include "streams/stubs/ServerStubsWriter.h" #include +#include +#include namespace { using testUtils::createFileRequest; @@ -166,16 +168,6 @@ namespace { Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 2); - -// auto root = testGen.baseBuildDatabase->getRootForSource(foreign_bar_c); -// auto linkUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(root); -// auto stubFiles = testGen.baseBuildDatabase->getStubFiles(linkUnitInfo); -// auto stubCandidates = { calc_sum_c }; -// auto expectedStubFiles = CollectionUtils::transformTo( -// stubCandidates, [&testGen](fs::path const &path) { -// return Paths::sourcePathToStubPath(testGen.projectContext, path); -// }); -// EXPECT_EQ(expectedStubFiles, stubFiles); } TEST_F(Stub_Test, File_Tests_With_Stubs) { From d3f04cb378a162b2b15f3e91033553e399737103 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Tue, 16 Aug 2022 12:14:45 +0300 Subject: [PATCH 19/27] start refactoring after review --- server/src/KleeGenerator.cpp | 53 +++++++------- server/src/KleeGenerator.h | 6 +- server/src/ReturnTypesFetcher.cpp | 2 +- server/src/Server.cpp | 12 ++-- server/src/Synchronizer.cpp | 39 +++++++---- server/src/Synchronizer.h | 9 ++- server/src/building/BaseCommand.cpp | 20 +++--- server/src/building/BaseCommand.h | 8 +-- server/src/building/BuildDatabase.cpp | 2 +- server/src/building/CompileCommand.cpp | 6 +- server/src/building/LinkCommand.cpp | 8 +-- server/src/building/Linker.cpp | 70 +++++++++---------- server/src/coverage/GcovCoverageTool.cpp | 12 ++-- server/src/coverage/LlvmCoverageTool.cpp | 11 +-- server/src/coverage/TestRunner.cpp | 12 ++-- .../src/printers/DefaultMakefilePrinter.cpp | 4 ++ server/src/printers/DefaultMakefilePrinter.h | 4 ++ server/src/printers/NativeMakefilePrinter.cpp | 49 +++++++------ server/src/printers/TestMakefilesPrinter.cpp | 20 +++--- server/src/stubs/StubGen.cpp | 2 +- server/src/testgens/BaseTestGen.cpp | 8 +++ server/src/testgens/BaseTestGen.h | 10 ++- server/src/testgens/ProjectTestGen.cpp | 4 +- server/src/testgens/ProjectTestGen.h | 2 +- server/src/utils/CompilationUtils.cpp | 4 ++ server/src/utils/CompilationUtils.h | 2 + server/src/utils/GenerationUtils.cpp | 2 +- server/src/utils/GenerationUtils.h | 2 +- server/test/framework/Server_Tests.cpp | 2 +- server/test/framework/Stub_Tests.cpp | 4 +- server/test/framework/Targets_Test.cpp | 3 - server/test/framework/main.cpp | 5 +- 32 files changed, 223 insertions(+), 174 deletions(-) diff --git a/server/src/KleeGenerator.cpp b/server/src/KleeGenerator.cpp index 2e906baa1..1e1f7c9ec 100644 --- a/server/src/KleeGenerator.cpp +++ b/server/src/KleeGenerator.cpp @@ -16,6 +16,9 @@ using namespace tests; +static const std::string GENERATION_COMPILE_MAKEFILE = "GenerationCompileMakefile.mk"; +static const std::string GENERATION_KLEE_MAKEFILE = "GenerationKleeMakefile.mk"; + KleeGenerator::KleeGenerator(BaseTestGen &_testGen, types::TypesHandler &typesHandler, PathSubstitution filePathsSubstitution) : testGen(_testGen), typesHandler(typesHandler), @@ -44,11 +47,12 @@ KleeGenerator::buildByCDb(const CollectionUtils::MapFileTo &filesToBui { compileCommandWithChangingDirectory.toStringWithChangingDirectory() }); } - makefilePrinter.declareTarget("all", outfilePaths, {}); - fs::path makefile = testGen.serverBuildDir / "GenerationCompileMakefile.mk"; + makefilePrinter.declareTarget(printer::DefaultMakefilePrinter::TARGET_ALL, outfilePaths, {}); + const fs::path makefile = testGen.serverBuildDir / GENERATION_COMPILE_MAKEFILE; FileSystemUtils::writeToFile(makefile, makefilePrinter.ss.str()); - auto command = MakefileUtils::MakefileCommand(testGen.projectContext, makefile, "all"); + auto command = MakefileUtils::MakefileCommand(testGen.projectContext, makefile, + printer::DefaultMakefilePrinter::TARGET_ALL); ExecUtils::ExecutionResult res = command.run(); if (res.status != 0) { LOG_S(ERROR) << StringUtils::stringFormat("Make for \"%s\" failed.\nCommand: \"%s\"\n%s\n", @@ -71,7 +75,7 @@ KleeGenerator::buildByCDb(const CollectionUtils::FileSet &filesToBuild, const CollectionUtils::FileSet &stubSources) { CollectionUtils::MapFileTo filesMap; for (fs::path const &file : filesToBuild) { - filesMap[file] = testGen.buildDatabase->getBitcodeFile(file); + filesMap[file] = testGen.getBuildDatabase(false)->getBitcodeFile(file); } return buildByCDb(filesMap, stubSources); } @@ -120,12 +124,11 @@ KleeGenerator::getCompileCommandForKlee(const fs::path &hintPath, const std::vector &flags, bool forStub) const { - auto compilationUnitInfo = forStub ? testGen.baseBuildDatabase->getClientCompilationUnitInfo(hintPath) - : testGen.buildDatabase->getClientCompilationUnitInfo(hintPath); + auto compilationUnitInfo = testGen.getBuildDatabase(forStub)->getClientCompilationUnitInfo(hintPath); auto command = compilationUnitInfo->command; auto srcFilePath = compilationUnitInfo->getSourcePath(); - std::string newCompilerPath = getUTBotClangCompilerPath(command.getCompiler()); - command.setCompiler(newCompilerPath); + std::string newCompilerPath = getUTBotClangCompilerPath(command.getBuildTool()); + command.setBuildTool(newCompilerPath); srcFilePath = pathSubstitution.substituteLineFlag(srcFilePath); if (CollectionUtils::contains(stubSources, srcFilePath)) { @@ -133,8 +136,7 @@ KleeGenerator::getCompileCommandForKlee(const fs::path &hintPath, } command.setSourcePath(srcFilePath); - auto outFilePath = forStub ? testGen.baseBuildDatabase->getBitcodeFile(compilationUnitInfo->getOutputFile()) - : testGen.buildDatabase->getBitcodeFile(compilationUnitInfo->getOutputFile()); + auto outFilePath = testGen.getBuildDatabase(forStub)->getBitcodeFile(compilationUnitInfo->getOutputFile()); fs::create_directories(outFilePath.parent_path()); command.setOutput(outFilePath); command.setOptimizationLevel("-O0"); @@ -150,7 +152,7 @@ KleeGenerator::getCompileCommandForKlee(const fs::path &hintPath, "-D" + PrinterUtils::KLEE_MODE + "=1", SanitizerUtils::CLANG_SANITIZER_CHECKS_FLAG }; if (Paths::isCXXFile(srcFilePath)) { - command.addFlagToBegin(StringUtils::stringFormat("-I%s", Paths::getAccessPrivateLibPath())); + command.addFlagToBegin(CompilationUtils::getIncludePath(Paths::getAccessPrivateLibPath())); } command.addFlagsToBegin(flags); command.addFlagsToBegin(extraFlags); @@ -182,7 +184,7 @@ Result KleeGenerator::defaultBuild(const fs::path &hintPath, const fs::path &buildDirPath, const std::vector &flags) { LOG_SCOPE_FUNCTION(DEBUG); - auto bitcodeFilePath = testGen.buildDatabase->getBitcodeFile(sourceFilePath); + auto bitcodeFilePath = testGen.getBuildDatabase(false)->getBitcodeFile(sourceFilePath); auto optionalCommand = getCompileCommandForKlee(hintPath, {}, flags, false); if (!optionalCommand.has_value()) { std::string message = StringUtils::stringFormat( @@ -197,12 +199,15 @@ Result KleeGenerator::defaultBuild(const fs::path &hintPath, printer::DefaultMakefilePrinter makefilePrinter; auto commandWithChangingDirectory = utbot::CompileCommand(command, true); - makefilePrinter.declareTarget("build", {commandWithChangingDirectory.getSourcePath()}, {commandWithChangingDirectory.toStringWithChangingDirectory()}); - fs::path makefile = testGen.serverBuildDir / "BCForKLEE.mk"; + makefilePrinter.declareTarget(printer::DefaultMakefilePrinter::TARGET_BUILD, + {commandWithChangingDirectory.getSourcePath()}, + {commandWithChangingDirectory.toStringWithChangingDirectory()}); + fs::path makefile = testGen.serverBuildDir / GENERATION_KLEE_MAKEFILE; FileSystemUtils::writeToFile(makefile, makefilePrinter.ss.str()); - auto makefileCommand = MakefileUtils::MakefileCommand(testGen.projectContext, makefile, "build"); - auto [out, status, _] = makefileCommand.run(); + auto makefileCommand = MakefileUtils::MakefileCommand(testGen.projectContext, makefile, + printer::DefaultMakefilePrinter::TARGET_BUILD); + auto[out, status, _] = makefileCommand.run(); if (status != 0) { LOG_S(ERROR) << "Compilation for " << sourceFilePath << " failed.\n" << "Command: \"" << commandWithChangingDirectory.toString() << "\"\n" @@ -239,7 +244,7 @@ std::vector KleeGenerator::buildKleeFiles(const tests::TestsMap &tests const std::shared_ptr &lineInfo) { std::vector outFiles; LOG_S(DEBUG) << "Building generated klee files..."; - printer::KleePrinter kleePrinter(&typesHandler, testGen.buildDatabase, utbot::Language::UNKNOWN); + printer::KleePrinter kleePrinter(&typesHandler, testGen.getBuildDatabase(false), utbot::Language::UNKNOWN); ExecUtils::doWorkWithProgress( testsMap, testGen.progressWriter, "Building generated klee files", [&](auto const &it) { @@ -249,13 +254,13 @@ std::vector KleeGenerator::buildKleeFiles(const tests::TestsMap &tests } kleePrinter.srcLanguage = Paths::getSourceLanguage(filename); std::vector includeFlags = { - StringUtils::stringFormat("-I%s", Paths::getFlagsDir(testGen.projectContext))}; + CompilationUtils::getIncludePath(Paths::getFlagsDir(testGen.projectContext))}; auto buildDirPath = - testGen.buildDatabase->getClientCompilationUnitInfo(filename)->getDirectory(); + testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(filename)->getDirectory(); fs::path kleeFilePath = writeKleeFile(kleePrinter, tests, lineInfo); auto kleeFilesInfo = - testGen.buildDatabase->getClientCompilationUnitInfo(tests.sourceFilePath)->kleeFilesInfo; + testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(tests.sourceFilePath)->kleeFilesInfo; auto kleeBitcodeFile = defaultBuild(filename, kleeFilePath, buildDirPath, includeFlags); if (kleeBitcodeFile.isSuccess()) { outFiles.emplace_back(kleeBitcodeFile.getOpt().value()); @@ -351,12 +356,8 @@ void KleeGenerator::parseKTestsToFinalCode( LOG_S(DEBUG) << "Generated code for " << tests.methods.size() << " tests"; } -std::shared_ptr KleeGenerator::getBuildDatabase() const { - return testGen.buildDatabase; -} - -std::shared_ptr KleeGenerator::getBaseBuildDatabase() const { - return testGen.baseBuildDatabase; +fs::path KleeGenerator::getBitcodeFile(const fs::path &sourcePath) const { + return testGen.getBuildDatabase(false)->getBitcodeFile(sourcePath); } void KleeGenerator::handleFailedFunctions(tests::TestsMap &testsMap) { diff --git a/server/src/KleeGenerator.h b/server/src/KleeGenerator.h index 2d8669995..ab133ccb3 100644 --- a/server/src/KleeGenerator.h +++ b/server/src/KleeGenerator.h @@ -35,7 +35,7 @@ class KleeGenerator { public: /** * @brief Also creates tmp directories for build files. - * @param _testGen contains projectContext, settingsContext, BuildDatabase. + * @param _testGen contains context for current request. * @param typesHandler provides additional information about types. * @param filePathsSubstitution Mapping from source file path to modified file. Required for * line test generation requests. @@ -118,9 +118,7 @@ class KleeGenerator { const std::shared_ptr &lineInfo = nullptr, bool verbose = false); - [[nodiscard]] std::shared_ptr getBuildDatabase() const; - - [[nodiscard]] std::shared_ptr getBaseBuildDatabase() const; + [[nodiscard]] fs::path getBitcodeFile(const fs::path &sourcePath) const; void handleFailedFunctions(tests::TestsMap &testsMap); diff --git a/server/src/ReturnTypesFetcher.cpp b/server/src/ReturnTypesFetcher.cpp index 28f4b99fd..1b57342f4 100644 --- a/server/src/ReturnTypesFetcher.cpp +++ b/server/src/ReturnTypesFetcher.cpp @@ -10,7 +10,7 @@ void ReturnTypesFetcher::fetch(ProgressWriter *const progressWriter, testsMap[filePath]; } Fetcher(Fetcher::Options::Value::RETURN_TYPE_NAMES_ONLY, - testGen->buildDatabase->compilationDatabase, testsMap, nullptr, nullptr, nullptr, + testGen->getBuildDatabase(false)->compilationDatabase, testsMap, nullptr, nullptr, nullptr, testGen->compileCommandsJsonPath, false) .fetchWithProgress(progressWriter, "Fetching return types for functions", true); for (auto const &[sourceFilePath, test] : testsMap) { diff --git a/server/src/Server.cpp b/server/src/Server.cpp index e1ffe75ba..344919dfd 100644 --- a/server/src/Server.cpp +++ b/server/src/Server.cpp @@ -198,11 +198,11 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, static std::string logMessage = "Traversing sources AST tree and fetching declarations."; LOG_S(DEBUG) << logMessage; Fetcher fetcher(Fetcher::Options::Value::ALL, - testGen.buildDatabase->compilationDatabase, testGen.tests, &testGen.types, + testGen.getBuildDatabase(false)->compilationDatabase, testGen.tests, &testGen.types, &sizeContext.pointerSize, &sizeContext.maximumAlignment, testGen.compileCommandsJsonPath, false); fetcher.fetchWithProgress(testGen.progressWriter, logMessage); - SourceToHeaderRewriter(testGen.projectContext, testGen.buildDatabase->compilationDatabase, + SourceToHeaderRewriter(testGen.projectContext, testGen.getBuildDatabase(false)->compilationDatabase, fetcher.getStructsToDeclare(), testGen.serverBuildDir) .generateTestHeaders(testGen.tests, testGen.progressWriter); types::TypesHandler typesHandler{ testGen.types, sizeContext }; @@ -218,7 +218,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, if (lineTestGen != nullptr) { if (isSameType(testGen) && Paths::isHeaderFile(lineTestGen->filePath)) { BordersFinder classFinder(lineTestGen->filePath, lineTestGen->line, - lineTestGen->buildDatabase->compilationDatabase, + testGen.getBuildDatabase(false)->compilationDatabase, lineTestGen->compileCommandsJsonPath); classFinder.findClass(); lineInfo = std::make_shared(classFinder.getLineInfo()); @@ -256,7 +256,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, auto generator = std::make_shared(testGen, typesHandler, pathSubstitution); ReturnTypesFetcher returnTypesFetcher{ &testGen }; - returnTypesFetcher.fetch(testGen.progressWriter, synchronizer.getAllFiles()); + returnTypesFetcher.fetch(testGen.progressWriter, synchronizer.getSourceFiles()); LOG_S(DEBUG) << "Temporary build directory path: " << testGen.serverBuildDir; generator->buildKleeFiles(testGen.tests, lineInfo); generator->handleFailedFunctions(testGen.tests); @@ -308,7 +308,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, std::shared_ptr Server::TestsGenServiceImpl::getLineInfo(LineTestGen &lineTestGen) { BordersFinder stmtFinder(lineTestGen.filePath, lineTestGen.line, - lineTestGen.buildDatabase->compilationDatabase, + lineTestGen.getBuildDatabase(false)->compilationDatabase, lineTestGen.compileCommandsJsonPath); stmtFinder.findFunction(); if (!stmtFinder.getLineInfo().initialized) { @@ -548,7 +548,7 @@ Status Server::TestsGenServiceImpl::ProcessProjectStubsRequest(BaseTestGen *test static std::string logMessage = "Traversing sources AST tree and fetching declarations."; LOG_S(DEBUG) << logMessage; Fetcher fetcher(Fetcher::Options::Value::TYPE | Fetcher::Options::Value::FUNCTION, - testGen->buildDatabase->compilationDatabase, testGen->tests, &testGen->types, + testGen->getBuildDatabase(false)->compilationDatabase, testGen->tests, &testGen->types, &sizeContext.pointerSize, &sizeContext.maximumAlignment, testGen->compileCommandsJsonPath, false); diff --git a/server/src/Synchronizer.cpp b/server/src/Synchronizer.cpp index 6d09b7db1..76117752d 100644 --- a/server/src/Synchronizer.cpp +++ b/server/src/Synchronizer.cpp @@ -66,11 +66,19 @@ bool Synchronizer::isProbablyOutdated(const fs::path &srcFilePath) const { } CollectionUtils::FileSet Synchronizer::getOutdatedSourcePaths() const { - return CollectionUtils::filterOut(getAllFiles(), [this](fs::path const &sourcePath) { + return CollectionUtils::filterOut(getSourceFiles(), [this](fs::path const &sourcePath) { return !isProbablyOutdated(sourcePath); }); } +StubSet Synchronizer::getOutdatedStubs() const { + auto allFiles = getStubsFiles(); + auto outdatedStubs = CollectionUtils::filterOut(allFiles, [this](StubOperator const &stubOperator) { + return !isProbablyOutdated(stubOperator.getSourceFilePath()); + }); + return outdatedStubs; +} + bool Synchronizer::removeStubIfSourceAbsent(const StubOperator &stub) const { if (!fs::exists(stub.getSourceFilePath())) { try { @@ -113,21 +121,17 @@ void Synchronizer::synchronize(const types::TypesHandler &typesHandler) { if (TypeUtils::isSameType(*this->testGen)) { return; } - auto outdatedSourcePaths = getOutdatedSourcePaths(); if (testGen->settingsContext.useStubs) { - auto outdatedStubsSourcePaths = CollectionUtils::filterOut( - testGen->baseBuildDatabase->compilationDatabase->getAllFiles(), [this](fs::path const &sourcePath) { - return !isProbablyOutdated(sourcePath); - }); - auto outdatedStubs = getStubSetFromSources(outdatedStubsSourcePaths); + auto outdatedStubs = getOutdatedStubs(); synchronizeStubs(outdatedStubs, typesHandler); } + auto outdatedSourcePaths = getOutdatedSourcePaths(); synchronizeWrappers(outdatedSourcePaths); } void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, const types::TypesHandler &typesHandler) { - StubSet allStubs = getStubSetFromSources(testGen->baseBuildDatabase->compilationDatabase->getAllFiles()); + StubSet allStubs = getStubsFiles(); auto stubDirPath = Paths::getStubsDirPath(testGen->projectContext); prepareDirectory(stubDirPath); auto filesInFolder = Paths::findFilesInFolder(stubDirPath); @@ -149,7 +153,7 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, auto options = Fetcher::Options::Value::FUNCTION | Fetcher::Options::Value::INCLUDE | Fetcher::Options::Value::TYPE; auto stubFetcher = - Fetcher(options, testGen->baseBuildDatabase->compilationDatabase, sourceFilesMap, &testGen->types, + Fetcher(options, testGen->getBuildDatabase(true)->compilationDatabase, sourceFilesMap, &testGen->types, &sizeContext->pointerSize, &sizeContext->maximumAlignment, testGen->compileCommandsJsonPath, false); @@ -161,7 +165,7 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, auto stubsCdb = createStubsCompilationDatabase(stubFiles, ccJsonStubDirPath); auto sourceToHeaderRewriter = - SourceToHeaderRewriter(testGen->projectContext, testGen->baseBuildDatabase->compilationDatabase, + SourceToHeaderRewriter(testGen->projectContext, testGen->getBuildDatabase(true)->compilationDatabase, stubFetcher.getStructsToDeclare(), testGen->serverBuildDir); for (const StubOperator &outdatedStub : outdatedStubs) { @@ -194,7 +198,7 @@ Synchronizer::createStubsCompilationDatabase(StubSet &stubFiles, void Synchronizer::synchronizeWrappers(const CollectionUtils::FileSet &outdatedSourcePaths) const { auto sourceFilesNeedToRegenerateWrappers = outdatedSourcePaths; - for (fs::path const &sourceFilePath : getAllFiles()) { + for (fs::path const &sourceFilePath : getSourceFiles()) { if (!CollectionUtils::contains(sourceFilesNeedToRegenerateWrappers, sourceFilePath)) { auto wrapperFilePath = Paths::getWrapperFilePath(testGen->projectContext, sourceFilePath); @@ -207,14 +211,19 @@ void Synchronizer::synchronizeWrappers(const CollectionUtils::FileSet &outdatedS sourceFilesNeedToRegenerateWrappers, testGen->progressWriter, "Generating wrappers", [this](fs::path const &sourceFilePath) { SourceToHeaderRewriter sourceToHeaderRewriter(testGen->projectContext, - testGen->baseBuildDatabase->compilationDatabase, nullptr, + testGen->getBuildDatabase(true)->compilationDatabase, nullptr, testGen->serverBuildDir); std::string wrapper = sourceToHeaderRewriter.generateWrapper(sourceFilePath); printer::SourceWrapperPrinter(Paths::getSourceLanguage(sourceFilePath)).print(testGen->projectContext, sourceFilePath, wrapper); }); } -const CollectionUtils::FileSet &Synchronizer::getAllFiles() const { - return testGen->buildDatabase->compilationDatabase->getAllFiles(); + +const CollectionUtils::FileSet &Synchronizer::getSourceFiles() const { + return testGen->getBuildDatabase(false)->compilationDatabase->getAllFiles(); +} + +StubSet Synchronizer::getStubsFiles() const { + return getStubSetFromSources(testGen->getBuildDatabase(true)->compilationDatabase->getAllFiles()); } void Synchronizer::prepareDirectory(const fs::path &stubDirectory) { @@ -225,7 +234,7 @@ void Synchronizer::prepareDirectory(const fs::path &stubDirectory) { if (!Paths::isHeaderFile(stubPath)) { fs::path sourcePath = Paths::stubPathToSourcePath(testGen->projectContext, stubPath); - if (!CollectionUtils::contains(getAllFiles(), sourcePath)) { + if (!CollectionUtils::contains(getSourceFiles(), sourcePath)) { LOG_S(DEBUG) << "Found extra file in stub directory: " << stubPath << ". Removing it."; fs::remove(stubPath); diff --git a/server/src/Synchronizer.h b/server/src/Synchronizer.h index 30bca12b5..5daae8e3e 100644 --- a/server/src/Synchronizer.h +++ b/server/src/Synchronizer.h @@ -24,7 +24,9 @@ class Synchronizer { StubGen const *const stubGen; types::TypesHandler::SizeContext *sizeContext; - CollectionUtils::FileSet getOutdatedSourcePaths() const; + [[nodiscard]] CollectionUtils::FileSet getOutdatedSourcePaths() const; + + [[nodiscard]] std::unordered_set getOutdatedStubs() const; bool isProbablyOutdated(const fs::path &srcFilePath) const; @@ -42,9 +44,11 @@ class Synchronizer { void prepareDirectory(fs::path const& stubDirectory); static std::unordered_set + getStubSetFromSources(const CollectionUtils::FileSet &paths); public: static std::unordered_set + dropHeaders(const std::unordered_set &stubs); static CollectionUtils::FileSet dropHeaders(const CollectionUtils::FileSet &files); @@ -53,7 +57,8 @@ class Synchronizer { void synchronize(const types::TypesHandler &typesHandler); - const CollectionUtils::FileSet &getAllFiles() const; + [[nodiscard]] const CollectionUtils::FileSet &getSourceFiles() const; + [[nodiscard]] std::unordered_set getStubsFiles() const; }; diff --git a/server/src/building/BaseCommand.cpp b/server/src/building/BaseCommand.cpp index a672b77bd..f14d048a9 100644 --- a/server/src/building/BaseCommand.cpp +++ b/server/src/building/BaseCommand.cpp @@ -20,20 +20,20 @@ namespace utbot { BaseCommand::BaseCommand(std::list commandLine, fs::path directory, bool shouldChangeDirectory) : commandLine(std::move(commandLine)), directory(std::move(directory)), shouldChangeDirectory{shouldChangeDirectory} { initOptimizationLevel(); - initCompiler(); + initBuildTool(); initOutput(); } BaseCommand::BaseCommand(std::vector commandLine, fs::path directory, bool shouldChangeDirectory) : commandLine(commandLine.begin(), commandLine.end()), directory(std::move(directory)), shouldChangeDirectory{shouldChangeDirectory} { initOptimizationLevel(); - initCompiler(); + initBuildTool(); initOutput(); } BaseCommand::BaseCommand(BaseCommand const &other) : directory(other.directory), commandLine(other.commandLine), environmentVariables(other.environmentVariables), shouldChangeDirectory(other.shouldChangeDirectory), - compiler(other.compiler), + buildTool(other.buildTool), output(other.output) { if (other.optimizationLevel.has_value()) { optimizationLevel = @@ -47,7 +47,7 @@ namespace utbot { : directory(std::move(other.directory)), commandLine(std::move(other.commandLine)), environmentVariables(std::move(other.environmentVariables)), optimizationLevel(other.optimizationLevel), - compiler(other.compiler), + buildTool(other.buildTool), output(other.output), shouldChangeDirectory(other.shouldChangeDirectory) { } @@ -59,9 +59,9 @@ namespace utbot { } } - void BaseCommand::initCompiler() { + void BaseCommand::initBuildTool() { auto it = commandLine.begin(); - compiler = it; + buildTool = it; } void BaseCommand::initOutput() { @@ -151,12 +151,12 @@ namespace utbot { } } - fs::path BaseCommand::getCompiler() const { - return *compiler; + fs::path BaseCommand::getBuildTool() const { + return *buildTool; } - void BaseCommand::setCompiler(fs::path compiler) { - *(this->compiler) = std::move(compiler); + void BaseCommand::setBuildTool(fs::path buildTool) { + *(this->buildTool) = std::move(buildTool); } fs::path BaseCommand::getOutput() const { diff --git a/server/src/building/BaseCommand.h b/server/src/building/BaseCommand.h index 8e6d29ada..dcc0e409c 100644 --- a/server/src/building/BaseCommand.h +++ b/server/src/building/BaseCommand.h @@ -23,14 +23,14 @@ namespace utbot { using iterator = decltype(commandLine)::iterator; using const_iterator = decltype(commandLine)::const_iterator; - iterator compiler; + iterator buildTool; iterator output; std::optional optimizationLevel; void initOptimizationLevel(); - void initCompiler(); + void initBuildTool(); void initOutput(); @@ -101,9 +101,9 @@ namespace utbot { void setOptimizationLevel(const std::string &flag); - [[nodiscard]] fs::path getCompiler() const; + [[nodiscard]] fs::path getBuildTool() const; - void setCompiler(fs::path compiler); + void setBuildTool(fs::path buildTool); }; } diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 4d8c4f280..2bb9d1ea0 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -185,7 +185,7 @@ void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) { //create targetInfo targetInfo = targetInfos[outputFile] = std::make_shared(); targetInfo->commands.emplace_back( - std::initializer_list{targetObjectInfo->command.getCompiler(), + std::initializer_list{targetObjectInfo->command.getBuildTool(), "-o", outputFile, tmpObjectFileName}, directory); targetInfo->addFile(tmpObjectFileName); diff --git a/server/src/building/CompileCommand.cpp b/server/src/building/CompileCommand.cpp index 2adffcc85..f0ad99bbc 100644 --- a/server/src/building/CompileCommand.cpp +++ b/server/src/building/CompileCommand.cpp @@ -12,7 +12,7 @@ namespace utbot { CompileCommand::CompileCommand(CompileCommand const &other) : BaseCommand(other) { - compiler = commandLine.begin(); + buildTool = commandLine.begin(); sourcePath = std::next(commandLine.begin(), std::distance(other.commandLine.begin(), other.sourcePath)); @@ -51,7 +51,7 @@ namespace utbot { fs::path directory, fs::path sourcePath) : BaseCommand(std::move(arguments), std::move(directory)) { - compiler = commandLine.begin(); + buildTool = commandLine.begin(); { auto it = std::find_if(commandLine.begin(), commandLine.end(), [&sourcePath](std::string const &arg) { return fs::path(arg).filename() == sourcePath.filename(); @@ -78,7 +78,7 @@ namespace utbot { std::swap(a.optimizationLevel, b.optimizationLevel); std::swap(a.sourcePath, b.sourcePath); - std::swap(a.compiler, b.compiler); + std::swap(a.buildTool, b.buildTool); std::swap(a.output, b.output); } diff --git a/server/src/building/LinkCommand.cpp b/server/src/building/LinkCommand.cpp index 172961c15..302a7db8f 100644 --- a/server/src/building/LinkCommand.cpp +++ b/server/src/building/LinkCommand.cpp @@ -11,7 +11,7 @@ namespace utbot { LinkCommand::LinkCommand(LinkCommand const &other) : BaseCommand(other) { - compiler = commandLine.begin(); + buildTool = commandLine.begin(); output = std::next(commandLine.begin(), std::distance(other.commandLine.begin(), other.output)); } @@ -38,7 +38,7 @@ namespace utbot { LinkCommand::LinkCommand(std::list arguments, fs::path directory, bool shouldChangeDirectory) : BaseCommand(std::move(arguments), std::move(directory), shouldChangeDirectory) { - compiler = commandLine.begin(); + buildTool = commandLine.begin(); { auto it = findOutput(); if (it != commandLine.end()) { @@ -74,12 +74,12 @@ namespace utbot { std::swap(a.environmentVariables, b.environmentVariables); std::swap(a.optimizationLevel, b.optimizationLevel); - std::swap(a.compiler, b.compiler); + std::swap(a.buildTool, b.buildTool); std::swap(a.output, b.output); } bool LinkCommand::isArchiveCommand() const { - return StringUtils::contains(getCompiler().filename().c_str(), "ar"); + return StringUtils::contains(getBuildTool().filename().c_str(), "ar"); } bool LinkCommand::isSharedLibraryCommand() const { diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index f183ba79c..3793a8bbe 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -56,15 +56,15 @@ Result Linker::linkForTarget(const fs::path &target, const f const fs::path &objectFile) { testGen.setTargetPath(target); - auto siblings = testGen.buildDatabase->getArchiveObjectFiles(target); + auto siblings = testGen.getBuildDatabase(false)->getArchiveObjectFiles(target); auto stubSources = stubGen.getStubSources(target); CollectionUtils::MapFileTo filesToLink; for (const auto &sibling : siblings) { auto siblingCompilationUnitInfo = - testGen.buildDatabase->getClientCompilationUnitInfo(sibling); + testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sibling); fs::path siblingObjectFile = siblingCompilationUnitInfo->getOutputFile(); - fs::path bitcodeFile = testGen.buildDatabase->getBitcodeForSource( + fs::path bitcodeFile = testGen.getBuildDatabase(false)->getBitcodeForSource( siblingCompilationUnitInfo->getSourcePath()); if (CollectionUtils::contains(stubSources, siblingCompilationUnitInfo->getSourcePath())) { @@ -75,7 +75,7 @@ Result Linker::linkForTarget(const fs::path &target, const f } kleeGenerator->buildByCDb(filesToLink, stubSources); - auto linkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(sourceFilePath); + auto linkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(sourceFilePath); std::optional moduleOutput = linkUnitInfo->getOutput(); std::string suffixForParentOfStubs = StringUtils::stringFormat("___%s", Paths::mangle(moduleOutput.value().filename())); @@ -87,16 +87,16 @@ Result Linker::linkForTarget(const fs::path &target, const f void Linker::linkForOneFile(const fs::path &sourceFilePath) { ExecUtils::throwIfCancelled(); - auto compilationUnitInfo = testGen.buildDatabase->getClientCompilationUnitInfo(sourceFilePath); + auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sourceFilePath); fs::path objectFile = compilationUnitInfo->getOutputFile(); if (CollectionUtils::contains(testedFiles, sourceFilePath)) { return; } - if (!testGen.buildDatabase->isFirstObjectFileForSource(objectFile)) { + if (!testGen.getBuildDatabase(false)->isFirstObjectFileForSource(objectFile)) { return; } - std::vector targets = testGen.buildDatabase->targetListForFile(sourceFilePath, objectFile); + std::vector targets = testGen.getBuildDatabase(false)->targetListForFile(sourceFilePath, objectFile); LOG_S(DEBUG) << "Linking bitcode for file " << sourceFilePath.filename(); for (size_t i = 0; i < targets.size(); i++) { const auto& target = targets[i]; @@ -105,14 +105,14 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { if (result.isSuccess()) { auto [targetBitcode, stubsSet, _] = result.getOpt().value(); addToGenerated({ objectFile }, targetBitcode); - auto&& targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); + auto&& targetUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(target); return; } else { LOG_S(DEBUG) << "Linkage for target " << target.filename() << " failed: " << result.getError()->c_str(); if (i + 1 == targets.size()) { addToGenerated({ objectFile }, {}); fs::path possibleBitcodeFileName = - testGen.buildDatabase->getBitcodeFile(testGen.buildDatabase->getTargetPath()); + testGen.getBuildDatabase(false)->getBitcodeFile(testGen.getBuildDatabase(false)->getTargetPath()); brokenLinkFiles.insert(possibleBitcodeFileName); } } @@ -120,20 +120,20 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { } Result Linker::linkWholeTarget(const fs::path &target) { - auto requestTarget = testGen.buildDatabase->getTargetPath(); - LOG_IF_S(WARNING, !testGen.buildDatabase->hasAutoTarget() && requestTarget != target) + auto requestTarget = testGen.getBuildDatabase(false)->getTargetPath(); + LOG_IF_S(WARNING, !testGen.getBuildDatabase(false)->hasAutoTarget() && requestTarget != target) << "Try link target that not specified by user"; testGen.setTargetPath(target); - auto targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target); - auto siblings = testGen.buildDatabase->getArchiveObjectFiles(target); + auto targetUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(target); + auto siblings = testGen.getBuildDatabase(false)->getArchiveObjectFiles(target); auto stubSources = stubGen.getStubSources(target); CollectionUtils::MapFileTo filesToLink; CollectionUtils::FileSet siblingObjectsToBuild; for (const fs::path &objectFile : siblings) { - auto objectInfo = testGen.buildDatabase->getClientCompilationUnitInfo(objectFile); + auto objectInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(objectFile); bool insideFolder = true; if (auto folderTestGen = dynamic_cast(&testGen)) { fs::path folderGen = folderTestGen->folderPath; @@ -145,7 +145,7 @@ Result Linker::linkWholeTarget(const fs::path &target) { fs::path bitcodeFile = objectInfo->kleeFilesInfo->getKleeBitcodeFile(); filesToLink.emplace(objectFile, bitcodeFile); } else { - fs::path bitcodeFile = testGen.buildDatabase->getBitcodeForSource(objectInfo->getSourcePath()); + fs::path bitcodeFile = testGen.getBuildDatabase(false)->getBitcodeForSource(objectInfo->getSourcePath()); siblingObjectsToBuild.insert(objectInfo->getOutputFile()); filesToLink.emplace(objectFile, bitcodeFile); } @@ -164,17 +164,17 @@ void Linker::linkForProject() { testGen.tests, testGen.progressWriter, "Compiling and linking source files", [&](auto const &it) { fs::path const &sourceFile = it.first; - auto compilationUnitInfo = testGen.buildDatabase->getClientCompilationUnitInfo(sourceFile); + auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sourceFile); fs::path objectFile = compilationUnitInfo->getOutputFile(); if (!CollectionUtils::contains(testedFiles, sourceFile)) { - auto objectInfo = testGen.buildDatabase->getClientCompilationUnitInfo(sourceFile); + auto objectInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sourceFile); if (objectInfo->linkUnit.empty()) { LOG_S(WARNING) << "No executable or library found for current source file in " "link_commands.json: " << sourceFile; return; } - std::vector targets = testGen.buildDatabase->targetListForFile(sourceFile, objectFile); + std::vector targets = testGen.getBuildDatabase(false)->targetListForFile(sourceFile, objectFile); bool success = false; for (const auto &target : targets) { if (!CollectionUtils::contains(triedTargets, target)) { @@ -186,12 +186,11 @@ void Linker::linkForProject() { auto linkres = result.getOpt().value(); auto objectFiles = CollectionUtils::transformTo( linkres.presentedFiles, [&](const fs::path &sourceFile) { - auto compilationUnitInfo = testGen.buildDatabase->getClientCompilationUnitInfo( + auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo( sourceFile); return compilationUnitInfo->getOutputFile(); }); addToGenerated(objectFiles, linkres.bitcodeOutput); - auto &&targetUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(target); break; } else { std::stringstream ss; @@ -209,9 +208,9 @@ void Linker::linkForProject() { void Linker::addToGenerated(const CollectionUtils::FileSet &objectFiles, const fs::path &output) { for (const auto &objectFile : objectFiles) { - auto objectInfo = testGen.buildDatabase->getClientCompilationUnitInfo(objectFile); + auto objectInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(objectFile); const fs::path &sourcePath = objectInfo->getSourcePath(); - if (testGen.buildDatabase->isFirstObjectFileForSource(objectFile) && + if (testGen.getBuildDatabase(false)->isFirstObjectFileForSource(objectFile) && !CollectionUtils::contains(testedFiles, sourcePath)) { testedFiles.insert(sourcePath); bitcodeFileName[sourcePath] = output; @@ -256,7 +255,7 @@ std::vector Linker::getTestMethods() { isAnyOneLinked = true; for (const auto &[methodName, _] : tests.methods) { auto compilationUnitInfo = - testGen.buildDatabase->getClientCompilationUnitInfo(fileName); + testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(fileName); if (compilationUnitInfo->kleeFilesInfo->isCorrectMethod(methodName)) { testMethods.emplace_back(methodName, bitcodePath, @@ -283,7 +282,7 @@ std::vector Linker::getTestMethods() { method.classObj.has_value() && method.classObj->type.typeName() == lineInfo->scopeName)) { auto compilationUnitInfo = - testGen.buildDatabase->getClientCompilationUnitInfo(fileName); + testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(fileName); if (compilationUnitInfo->kleeFilesInfo->isCorrectMethod(methodName)) { tests::TestMethod testMethod{ methodName, bitcodeFileName.at(lineInfo->filePath), @@ -397,7 +396,7 @@ Result Linker::link(const CollectionUtils::MapFileTogetClientCompilationUnitInfo(objectFile); + auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(objectFile); auto sourcePath = compilationUnitInfo->getSourcePath(); if (CollectionUtils::containsKey(testGen.tests, sourcePath)) { testMakefilesPrinter.GetMakefiles(sourcePath).write(); @@ -434,13 +433,13 @@ Result Linker::generateStubsMakefile( methodDescription.name = symbol; return methodDescription; }); - auto rootLinkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(root); + auto rootLinkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(root); auto stubsSet = StubGen(testGen).findStubFilesBySignatures(signatures); printer::DefaultMakefilePrinter makefilePrinter; auto bitcodeStubFiles = CollectionUtils::transformTo>( Synchronizer::dropHeaders(stubsSet), [this, &makefilePrinter](const fs::path &stubPath) { fs::path sourcePath = Paths::stubPathToSourcePath(testGen.projectContext, stubPath); - fs::path bitcodeFile = kleeGenerator->getBuildDatabase()->getBitcodeFile(sourcePath); + fs::path bitcodeFile = kleeGenerator->getBitcodeFile(sourcePath); bitcodeFile = Paths::getStubBitcodeFilePath(bitcodeFile); auto command = kleeGenerator->getCompileCommandForKlee(sourcePath, {}, {}, true); command->setSourcePath(stubPath); @@ -468,8 +467,9 @@ Result Linker::linkWithStubsIfNeeded(const fs::path &linkMakefile, return errorMessage; } - auto command = MakefileUtils::MakefileCommand(testGen.projectContext, linkMakefile, "all"); - auto [out, status, _] = command.run(testGen.serverBuildDir); + auto command = MakefileUtils::MakefileCommand(testGen.projectContext, linkMakefile, + printer::DefaultMakefilePrinter::TARGET_ALL); + auto[out, status, _] = command.run(testGen.serverBuildDir); if (status != 0) { std::string errorMessage = StringUtils::stringFormat("link with stubs failed: %s", command.getFailedCommand()); @@ -502,7 +502,7 @@ std::string getArchiveArgument(std::string const &argument, if (StringUtils::startsWith(argument, "-")) { return ""; } - hasArchiveOption |= !argument.empty() && argument != linkCommand.getCompiler(); + hasArchiveOption |= !argument.empty() && argument != linkCommand.getBuildTool(); return argument; } @@ -716,7 +716,7 @@ Linker::declareRootLibraryTarget(printer::DefaultMakefilePrinter &bitcodeLinkMak linkActions.insert(linkActions.begin(), removeRootAction.toStringWithChangingDirectory()); bitcodeLinkMakefilePrinter.declareTarget(rootOutput, { output, STUB_BITCODE_FILES }, linkActions); - bitcodeLinkMakefilePrinter.declareTarget("all", { rootOutput }, {}); + bitcodeLinkMakefilePrinter.declareTarget(printer::DefaultMakefilePrinter::TARGET_ALL, { rootOutput }, {}); return rootOutput; } @@ -731,7 +731,7 @@ Linker::addLinkTargetRecursively(const fs::path &fileToBuild, const std::optional &testedFilePath, bool shouldChangeDirectory) { if (Paths::isObjectFile(fileToBuild)) { - auto compilationUnitInfo = testGen.buildDatabase->getClientCompilationUnitInfo(fileToBuild); + auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(fileToBuild); fs::path sourcePath = compilationUnitInfo->getSourcePath(); BuildResult::Type type = CollectionUtils::contains(stubSources, sourcePath) ? BuildResult::Type::ALL_STUBS @@ -744,7 +744,7 @@ Linker::addLinkTargetRecursively(const fs::path &fileToBuild, } return { bitcode, type }; } else { - auto linkUnit = testGen.buildDatabase->getClientLinkUnitInfo(fileToBuild); + auto linkUnit = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(fileToBuild); CollectionUtils::MapFileTo dependencies; // object file -> bitcode BuildResult::Type unitType = BuildResult::Type::NONE; for (auto const &subfile : linkUnit->files) { @@ -760,7 +760,7 @@ Linker::addLinkTargetRecursively(const fs::path &fileToBuild, } auto bitcodeDependencies = CollectionUtils::getValues(dependencies); fs::path prefixPath = getPrefixPath(bitcodeDependencies, testGen.serverBuildDir); - auto output = testGen.buildDatabase->getBitcodeFile(fileToBuild); + auto output = testGen.getBuildDatabase(false)->getBitcodeFile(fileToBuild); output = LinkerUtils::applySuffix(output, unitType, suffixForParentOfStubs); if (Paths::isLibraryFile(fileToBuild)) { auto archiveActions = getArchiveCommands(prefixPath, dependencies, *linkUnit, output, shouldChangeDirectory); @@ -786,7 +786,7 @@ Linker::addLinkTargetRecursively(const fs::path &fileToBuild, auto actions = CollectionUtils::transform( linkActions, std::bind(&utbot::LinkCommand::toStringWithChangingDirectory, std::placeholders::_1)); bitcodeLinkMakefilePrinter.declareTarget(output, bitcodeDependencies, actions); - bitcodeLinkMakefilePrinter.declareTarget("all", { output }, {}); + bitcodeLinkMakefilePrinter.declareTarget(printer::DefaultMakefilePrinter::TARGET_ALL, { output }, {}); } return { output, unitType }; } diff --git a/server/src/coverage/GcovCoverageTool.cpp b/server/src/coverage/GcovCoverageTool.cpp index 98793050a..e998db93c 100644 --- a/server/src/coverage/GcovCoverageTool.cpp +++ b/server/src/coverage/GcovCoverageTool.cpp @@ -18,6 +18,7 @@ #include "json.hpp" #include +#include "printers/DefaultMakefilePrinter.h" using Coverage::CoverageMap; using Coverage::FileCoverage; @@ -39,11 +40,12 @@ GcovCoverageTool::getBuildRunCommands(const std::vector &testsToLaunch projectContext, Paths::testPathToSourcePath(projectContext, testToLaunch.testFilePath)); auto gtestFlags = getGTestFlags(testToLaunch); - auto buildCommand = - MakefileUtils::MakefileCommand(projectContext, makefile, "build", gtestFlags); - auto runCommand = - MakefileUtils::MakefileCommand(projectContext, makefile, "run", gtestFlags); - result.push_back({ testToLaunch, buildCommand, runCommand }); + auto buildCommand = MakefileUtils::MakefileCommand(projectContext, makefile, + printer::DefaultMakefilePrinter::TARGET_BUILD, + gtestFlags); + auto runCommand = MakefileUtils::MakefileCommand(projectContext, makefile, + printer::DefaultMakefilePrinter::TARGET_RUN, gtestFlags); + result.push_back({testToLaunch, buildCommand, runCommand}); }); return result; } diff --git a/server/src/coverage/LlvmCoverageTool.cpp b/server/src/coverage/LlvmCoverageTool.cpp index bbe3888a3..723b23b23 100644 --- a/server/src/coverage/LlvmCoverageTool.cpp +++ b/server/src/coverage/LlvmCoverageTool.cpp @@ -14,6 +14,7 @@ #include "utils/MakefileUtils.h" #include "utils/StringUtils.h" #include "utils/path/FileSystemPath.h" +#include "printers/DefaultMakefilePrinter.h" #include "loguru.h" @@ -36,13 +37,15 @@ LlvmCoverageTool::getBuildRunCommands(const std::vector &testsToLaunch std::vector profileEnv; if (withCoverage) { auto profrawFilePath = Paths::getProfrawFilePath(projectContext, testName); - profileEnv = { StringUtils::stringFormat("LLVM_PROFILE_FILE=%s", profrawFilePath) }; + profileEnv = {StringUtils::stringFormat("LLVM_PROFILE_FILE=%s", profrawFilePath)}; } - auto buildCommand = MakefileUtils::MakefileCommand(projectContext, makefilePath, "build", + auto buildCommand = MakefileUtils::MakefileCommand(projectContext, makefilePath, + printer::DefaultMakefilePrinter::TARGET_BUILD, gtestFlags, profileEnv); - auto runCommand = MakefileUtils::MakefileCommand(projectContext, makefilePath, "run", + auto runCommand = MakefileUtils::MakefileCommand(projectContext, makefilePath, + printer::DefaultMakefilePrinter::TARGET_RUN, gtestFlags, profileEnv); - return BuildRunCommand{ testToLaunch, buildCommand, runCommand }; + return BuildRunCommand{testToLaunch, buildCommand, runCommand}; }); } diff --git a/server/src/coverage/TestRunner.cpp b/server/src/coverage/TestRunner.cpp index 53156752a..e55daad99 100644 --- a/server/src/coverage/TestRunner.cpp +++ b/server/src/coverage/TestRunner.cpp @@ -1,3 +1,4 @@ +#include "printers/DefaultMakefilePrinter.h" #include "TestRunner.h" #include "GTestLogger.h" #include "Paths.h" @@ -39,8 +40,10 @@ TestRunner::TestRunner( std::vector TestRunner::getTestsFromMakefile(const fs::path &makefile, const fs::path &testFilePath) { - auto cmdGetAllTests = MakefileUtils::MakefileCommand(projectContext, makefile, "run", "--gtest_list_tests", {"GTEST_FILTER=*"}); - auto [out, status, _] = cmdGetAllTests.run(projectContext.buildDir(), false); + auto cmdGetAllTests = MakefileUtils::MakefileCommand(projectContext, makefile, + printer::DefaultMakefilePrinter::TARGET_RUN, + "--gtest_list_tests", {"GTEST_FILTER=*"}); + auto[out, status, _] = cmdGetAllTests.run(projectContext.buildDir(), false); if (status != 0) { auto [err, _, logFilePath] = cmdGetAllTests.run(projectContext.buildDir(), true); progressWriter->writeProgress(StringUtils::stringFormat("command %s failed.\n" @@ -170,7 +173,8 @@ bool TestRunner::buildTest(const utbot::ProjectContext& projectContext, const fs ExecUtils::throwIfCancelled(); fs::path makefile = Paths::getMakefilePathFromSourceFilePath(projectContext, sourcePath); if (fs::exists(makefile)) { - auto command = MakefileUtils::MakefileCommand(projectContext, makefile, "build", "", {}); + auto command = MakefileUtils::MakefileCommand(projectContext, makefile, + printer::DefaultMakefilePrinter::TARGET_BUILD, "", {}); LOG_S(DEBUG) << "Try compile tests for: " << sourcePath.string(); auto[out, status, logFilePath] = command.run(projectContext.buildDir(), true); if (status != 0) { @@ -233,4 +237,4 @@ void TestRunner::cleanCoverage() { ExecUtils::throwIfCancelled(); coverageTool->cleanCoverage(); -} \ No newline at end of file +} diff --git a/server/src/printers/DefaultMakefilePrinter.cpp b/server/src/printers/DefaultMakefilePrinter.cpp index 962d15d3a..2981e5dd7 100644 --- a/server/src/printers/DefaultMakefilePrinter.cpp +++ b/server/src/printers/DefaultMakefilePrinter.cpp @@ -4,6 +4,10 @@ namespace printer { +const std::string DefaultMakefilePrinter::TARGET_ALL = "all"; +const std::string DefaultMakefilePrinter::TARGET_BUILD = "build"; +const std::string DefaultMakefilePrinter::TARGET_RUN = "run"; + DefaultMakefilePrinter::DefaultMakefilePrinter() { writeCopyrightHeader(); } diff --git a/server/src/printers/DefaultMakefilePrinter.h b/server/src/printers/DefaultMakefilePrinter.h index 94c60f73a..b355fae15 100644 --- a/server/src/printers/DefaultMakefilePrinter.h +++ b/server/src/printers/DefaultMakefilePrinter.h @@ -9,6 +9,10 @@ namespace printer { class DefaultMakefilePrinter : public Printer { public: + static const std::string TARGET_ALL; + static const std::string TARGET_BUILD; + static const std::string TARGET_RUN; + DefaultMakefilePrinter(); ~DefaultMakefilePrinter() override = default; diff --git a/server/src/printers/NativeMakefilePrinter.cpp b/server/src/printers/NativeMakefilePrinter.cpp index 4a047955b..329a8f69f 100644 --- a/server/src/printers/NativeMakefilePrinter.cpp +++ b/server/src/printers/NativeMakefilePrinter.cpp @@ -185,8 +185,8 @@ namespace printer { gtestCompilationArguments.setSourcePath(getRelativePath(gtestAllSourceFile)); gtestCompilationArguments.setOutput(gtestAllObjectFile); gtestCompilationArguments.addFlagsToBegin( - { stringFormat("-I%s", getRelativePath(gtestLib) / "googletest" / "include"), - stringFormat("-I%s", getRelativePath(gtestLib) / "googletest") }); + { CompilationUtils::getIncludePath(getRelativePath(gtestLib) / "googletest" / "include"), + CompilationUtils::getIncludePath(getRelativePath(gtestLib) / "googletest") }); declareTarget(gtestAllObjectFile, { gtestCompilationArguments.getSourcePath() }, { gtestCompilationArguments.toStringWithChangingDirectory() }); @@ -206,8 +206,8 @@ namespace printer { auto gtestCompilationArguments = defaultCompileCommand; gtestCompilationArguments.addFlagsToBegin( - {stringFormat("-I%s", getRelativePath(gtestLib / "googletest" / "include")), - stringFormat("-I%s", getRelativePath(gtestLib / "googletest"))}); + {CompilationUtils::getIncludePath(getRelativePath(gtestLib / "googletest" / "include")), + CompilationUtils::getIncludePath(getRelativePath(gtestLib / "googletest"))}); gtestCompilationArguments.setSourcePath(getRelativePath(gtestMainSourceFile)); gtestCompilationArguments.setOutput(gtestMainObjectFile); declareTarget(gtestMainObjectFile, { gtestCompilationArguments.getSourcePath() }, @@ -226,10 +226,10 @@ namespace printer { const BuildDatabase::ObjectFileInfo &compilationUnitInfo) { auto compileCommand = compilationUnitInfo.command; fs::path compiler = CompilationUtils::getBundledCompilerPath( - CompilationUtils::getCompilerName(compileCommand.getCompiler())); + CompilationUtils::getCompilerName(compileCommand.getBuildTool())); fs::path cxxCompiler = CompilationUtils::toCppCompiler(compiler); auto compilerName = CompilationUtils::getCompilerName(compiler); - compileCommand.setCompiler(getRelativePathForLinker(compiler)); + compileCommand.setBuildTool(getRelativePathForLinker(compiler)); compileCommand.setSourcePath(getRelativePath(sourcePath)); compileCommand.setOutput(getRelativePath(target)); @@ -270,7 +270,7 @@ namespace printer { BuildResult NativeMakefilePrinter::addObjectFile(const fs::path &objectFile, const std::string &suffixForParentOfStubs) { - auto compilationUnitInfo = testGen.buildDatabase->getClientCompilationUnitInfo(objectFile); + auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(objectFile); fs::path sourcePath = compilationUnitInfo->getSourcePath(); fs::path pathToCompile; @@ -299,17 +299,17 @@ namespace printer { } void NativeMakefilePrinter::addTestTarget(const fs::path &sourcePath) { - auto compilationUnitInfo = testGen.buildDatabase->getClientCompilationUnitInfo(sourcePath); + auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sourcePath); auto testCompilationCommand = compilationUnitInfo->command; - testCompilationCommand.setCompiler(getRelativePathForLinker(primaryCxxCompiler)); + testCompilationCommand.setBuildTool(getRelativePathForLinker(primaryCxxCompiler)); testCompilationCommand.setOptimizationLevel(OPTIMIZATION_FLAG); testCompilationCommand.removeCompilerFlagsAndOptions( UNSUPPORTED_FLAGS_AND_OPTIONS_TEST_MAKE); testCompilationCommand.removeIncludeFlags(); const fs::path gtestLib = Paths::getGtestLibPath(); - testCompilationCommand.addFlagToBegin(stringFormat("-I%s", getRelativePath(gtestLib / "googletest" / "include"))); + testCompilationCommand.addFlagToBegin(CompilationUtils::getIncludePath(getRelativePath(gtestLib / "googletest" / "include"))); if (Paths::isCXXFile(sourcePath)) { - testCompilationCommand.addFlagToBegin(stringFormat("-I%s", getRelativePath(Paths::getAccessPrivateLibPath()))); + testCompilationCommand.addFlagToBegin(CompilationUtils::getIncludePath(getRelativePath(Paths::getAccessPrivateLibPath()))); } testCompilationCommand.addFlagToBegin(FPIC_FLAG); testCompilationCommand.addFlagsToBegin(SANITIZER_NEEDED_FLAGS); @@ -332,7 +332,7 @@ namespace printer { artifacts.push_back(testCompilationCommand.getOutput()); - auto rootLinkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(rootPath); + auto rootLinkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(rootPath); fs::path testExecutablePath = getTestExecutablePath(sourcePath); std::vector filesToLink{ "$(GTEST_MAIN)", "$(GTEST_ALL)", testCompilationCommand.getOutput(), @@ -353,7 +353,7 @@ namespace printer { { dynamicLinkCommand.toStringWithChangingDirectory() }); } else { utbot::LinkCommand dynamicLinkCommand = rootLinkUnitInfo->commands.front(); - dynamicLinkCommand.setCompiler(cxxLinker); + dynamicLinkCommand.setBuildTool(cxxLinker); dynamicLinkCommand.setOutput(testExecutablePath); dynamicLinkCommand.erase_if([&](std::string const &argument) { return CollectionUtils::contains(rootLinkUnitInfo->files, argument) || @@ -386,7 +386,7 @@ namespace printer { sharedOutput.value().parent_path()))); dynamicLinkCommand.addFlagToBegin("$(LDFLAGS)"); - dynamicLinkCommand.setCompiler(getRelativePathForLinker(cxxLinker)); + dynamicLinkCommand.setBuildTool(getRelativePathForLinker(cxxLinker)); dynamicLinkCommand.setOutput( getRelativePath(testExecutablePath)); @@ -426,7 +426,7 @@ namespace printer { fs::path testExecutablePath = getTestExecutablePath(sourcePath); - auto rootLinkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(rootPath); + auto rootLinkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(rootPath); fs::path coverageInfoBinary = sharedOutput.value(); if (!Paths::isLibraryFile(coverageInfoBinary)) { @@ -448,8 +448,8 @@ namespace printer { testRunCommand.addEnvironmentVariable(SanitizerUtils::ASAN_OPTIONS_NAME, SanitizerUtils::ASAN_OPTIONS_VALUE); - declareTarget("build", { getRelativePath(testExecutablePath) }, {}); - declareTarget("run", { "build" }, + declareTarget(TARGET_BUILD, { getRelativePath(testExecutablePath) }, {}); + declareTarget(TARGET_RUN, { TARGET_BUILD }, { testRunCommand.toStringWithChangingDirectory() }); close(); @@ -468,7 +468,7 @@ namespace printer { return buildResults[unitFile] = buildResult; } - auto linkUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(unitFile); + auto linkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(unitFile); BuildResult::Type unitType = BuildResult::Type::NONE; CollectionUtils::MapFileTo fileMapping; auto unitBuildResults = CollectionUtils::transformTo>( @@ -513,13 +513,13 @@ namespace printer { } if (!linkCommand.isArchiveCommand()) { if (isExecutable && !transformExeToLib) { - linkCommand.setCompiler(Paths::getLd()); + linkCommand.setBuildTool(Paths::getLd()); for (std::string &argument : linkCommand.getCommandLine()) { transformCompilerFlagsToLinkerFlags(argument); } } else { - linkCommand.setCompiler(CompilationUtils::getBundledCompilerPath( - CompilationUtils::getCompilerName(linkCommand.getCompiler()))); + linkCommand.setBuildTool(CompilationUtils::getBundledCompilerPath( + CompilationUtils::getCompilerName(linkCommand.getBuildTool()))); } std::vector libraryDirectoriesFlags; for (std::string &argument : linkCommand.getCommandLine()) { @@ -554,7 +554,7 @@ namespace printer { } } - linkCommand.setCompiler(getRelativePathForLinker(linkCommand.getCompiler())); + linkCommand.setBuildTool(getRelativePathForLinker(linkCommand.getBuildTool())); for (std::string &argument : linkCommand.getCommandLine()) { tryChangeToRelativePath(argument); @@ -616,7 +616,7 @@ namespace printer { fs::path sourcePath = Paths::stubPathToSourcePath(testGen.projectContext, stub); fs::path stubBuildFilePath = Paths::getStubBuildFilePath(testGen.projectContext, sourcePath); - auto compilationUnitInfo = testGen.baseBuildDatabase->getClientCompilationUnitInfo(sourcePath); + auto compilationUnitInfo = testGen.getBuildDatabase(true)->getClientCompilationUnitInfo(sourcePath); fs::path target = Paths::getRecompiledFile(testGen.projectContext, stub); addCompileTarget(stub, target, *compilationUnitInfo); return target; @@ -654,8 +654,7 @@ namespace printer { } // if in -I flag if (argument.length() >= 3 && StringUtils::startsWith(argument, "-I")) { - argument = "-I" + - getRelativePath(argument.substr(2)).string(); + argument = CompilationUtils::getIncludePath(getRelativePath(argument.substr(2)).string()); } } } diff --git a/server/src/printers/TestMakefilesPrinter.cpp b/server/src/printers/TestMakefilesPrinter.cpp index f7f015f6b..4d8bd7a9f 100644 --- a/server/src/printers/TestMakefilesPrinter.cpp +++ b/server/src/printers/TestMakefilesPrinter.cpp @@ -34,9 +34,9 @@ namespace printer { CollectionUtils::FileSet const *stubSources) : TestMakefilesPrinter( testGen, - testGen.buildDatabase->getTargetPath(), + testGen.getBuildDatabase(false)->getTargetPath(), CompilationUtils::getBundledCompilerPath(CompilationUtils::getCompilerName( - testGen.buildDatabase->compilationDatabase->getBuildCompilerPath())), + testGen.getBuildDatabase(false)->compilationDatabase->getBuildCompilerPath())), stubSources) { } @@ -89,24 +89,24 @@ namespace printer { MakefileUtils::getMakeCommand(sharedMakefilePathRelative, "bin", true), " ") }); - generalMakefilePrinter.declareTarget("build", {FORCE}, { + generalMakefilePrinter.declareTarget(TARGET_BUILD, {FORCE}, { StringUtils::stringFormat("%s || %s", StringUtils::joinWith(MakefileUtils::getMakeCommand( - sharedMakefilePathRelative, "build", true), " "), + sharedMakefilePathRelative, TARGET_BUILD, true), " "), StringUtils::joinWith(MakefileUtils::getMakeCommand( - objMakefilePathRelative, "build", true), " ")) + objMakefilePathRelative, TARGET_BUILD, true), " ")) }); - generalMakefilePrinter.declareTarget("run", {FORCE}, { + generalMakefilePrinter.declareTarget(TARGET_RUN, {FORCE}, { StringUtils::stringFormat("%s && { %s; exit $$?; } || { %s && { %s; exit $$?; } }", StringUtils::joinWith(MakefileUtils::getMakeCommand( - sharedMakefilePathRelative, "build", true), " "), + sharedMakefilePathRelative, TARGET_BUILD, true), " "), StringUtils::joinWith(MakefileUtils::getMakeCommand( - sharedMakefilePathRelative, "run", true), " "), + sharedMakefilePathRelative, TARGET_RUN, true), " "), StringUtils::joinWith(MakefileUtils::getMakeCommand( - objMakefilePathRelative, "build", true), " "), + objMakefilePathRelative, TARGET_BUILD, true), " "), StringUtils::joinWith(MakefileUtils::getMakeCommand( - objMakefilePathRelative, "run", true), " ")) + objMakefilePathRelative, TARGET_RUN, true), " ")) }); generalMakefilePrinter.declareTarget("clean", {FORCE}, { StringUtils::joinWith( diff --git a/server/src/stubs/StubGen.cpp b/server/src/stubs/StubGen.cpp index ef6c771fb..7bc6e32b8 100644 --- a/server/src/stubs/StubGen.cpp +++ b/server/src/stubs/StubGen.cpp @@ -18,7 +18,7 @@ CollectionUtils::FileSet StubGen::getStubSources(const fs::path &target) { return {}; } fs::path testedFilePath = *testGen.testingMethodsSourcePaths.begin(); - auto stubSources = StubSourcesFinder(testGen.baseBuildDatabase).excludeFind(testedFilePath, target); + auto stubSources = StubSourcesFinder(testGen.getBuildDatabase(true)).excludeFind(testedFilePath, target); return { stubSources.begin(), stubSources.end() }; } diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index a3daf1a60..d72a692a3 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -58,3 +58,11 @@ void BaseTestGen::updateTargetSources(fs::path _targetPath) { test.isFilePresentedInCommands = CollectionUtils::contains(targetSources, test.sourceFilePath); } } + +std::shared_ptr BaseTestGen::getBuildDatabase(bool forStub) const { + return forStub ? baseBuildDatabase : buildDatabase; +} + +std::shared_ptr BaseTestGen::getBuildDatabase(bool forStub) { + return forStub ? baseBuildDatabase : buildDatabase; +} diff --git a/server/src/testgens/BaseTestGen.h b/server/src/testgens/BaseTestGen.h index c52b1ddd3..9f0996521 100644 --- a/server/src/testgens/BaseTestGen.h +++ b/server/src/testgens/BaseTestGen.h @@ -23,8 +23,6 @@ class BaseTestGen { fs::path serverBuildDir; fs::path compileCommandsJsonPath; - std::shared_ptr baseBuildDatabase; - std::shared_ptr buildDatabase; CollectionUtils::FileSet sourcePaths, testingMethodsSourcePaths; tests::TestsMap tests; @@ -43,7 +41,15 @@ class BaseTestGen { void setTargetPath(fs::path _targetPath); virtual ~BaseTestGen() = default; + + std::shared_ptr getBuildDatabase(bool forStub) const; + + std::shared_ptr getBuildDatabase(bool forStub); + protected: + std::shared_ptr baseBuildDatabase; + std::shared_ptr buildDatabase; + BaseTestGen(const testsgen::ProjectContext &projectContext, const testsgen::SettingsContext &settingsContext, ProgressWriter *progressWriter, diff --git a/server/src/testgens/ProjectTestGen.cpp b/server/src/testgens/ProjectTestGen.cpp index ddd8b4521..599a352e4 100644 --- a/server/src/testgens/ProjectTestGen.cpp +++ b/server/src/testgens/ProjectTestGen.cpp @@ -8,7 +8,7 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, ProgressWriter *progressWriter, bool testMode, - bool autoSrcPaths) + bool autoDetect) : BaseTestGen(request.projectcontext(), request.settingscontext(), progressWriter, @@ -18,7 +18,7 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, projectContext.projectPath, projectContext.buildDirRelativePath); baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); buildDatabase = baseBuildDatabase->createBuildDatabaseForSourceOrTarget(request.targetpath()); - if (autoSrcPaths) { + if (autoDetect) { autoDetectSourcePathsIfNotEmpty(); } else { sourcePaths = buildDatabase->compilationDatabase->getAllFiles(); diff --git a/server/src/testgens/ProjectTestGen.h b/server/src/testgens/ProjectTestGen.h index 741b8b3c5..9fe1f0696 100644 --- a/server/src/testgens/ProjectTestGen.h +++ b/server/src/testgens/ProjectTestGen.h @@ -10,7 +10,7 @@ class ProjectTestGen : public BaseTestGen { ProjectTestGen(const testsgen::ProjectRequest &request, ProgressWriter *progressWriter, bool testMode, - bool autoSrcPaths = true); + bool autoDetect = true); ~ProjectTestGen() override = default; diff --git a/server/src/utils/CompilationUtils.cpp b/server/src/utils/CompilationUtils.cpp index fde3c4d8a..406070f0b 100644 --- a/server/src/utils/CompilationUtils.cpp +++ b/server/src/utils/CompilationUtils.cpp @@ -228,4 +228,8 @@ namespace CompilationUtils { return std::nullopt; } } + + std::string getIncludePath(const fs::path &includePath) { + return "-I" + includePath.string(); + } } diff --git a/server/src/utils/CompilationUtils.h b/server/src/utils/CompilationUtils.h index 792c8a137..fd1449c0d 100644 --- a/server/src/utils/CompilationUtils.h +++ b/server/src/utils/CompilationUtils.h @@ -50,6 +50,8 @@ namespace CompilationUtils { fs::path getBundledCompilerPath(CompilerName compilerName); std::optional getResourceDirectory(const fs::path& buildCompilerPath); + + std::string getIncludePath(const fs::path &includePath); } #endif //UNITTESTBOT_COPMILATIONUTILS_H diff --git a/server/src/utils/GenerationUtils.cpp b/server/src/utils/GenerationUtils.cpp index 6a8e983d5..f481b8b65 100644 --- a/server/src/utils/GenerationUtils.cpp +++ b/server/src/utils/GenerationUtils.cpp @@ -50,7 +50,7 @@ void GenerationUtils::generateCoverageAndResultsAndWriteStatus( std::optional GenerationUtils::findTarget(const BaseTestGen &baseTestGen, const std::string &name) { - return findTarget(baseTestGen.buildDatabase->getAllTargets(), name); + return findTarget(baseTestGen.getBuildDatabase(false)->getAllTargets(), name); } std::optional diff --git a/server/src/utils/GenerationUtils.h b/server/src/utils/GenerationUtils.h index 796f4a987..06862aa25 100644 --- a/server/src/utils/GenerationUtils.h +++ b/server/src/utils/GenerationUtils.h @@ -44,7 +44,7 @@ namespace GenerationUtils { if (status.error_message() == FileNotPresentedInArtifactException::MESSAGE || status.error_message() == FileNotPresentedInCommandsException::MESSAGE) { fs::path path = status.error_details(); - auto targetsForSourceFile = testGen->buildDatabase->getTargetsForSourceFile(path); + auto targetsForSourceFile = testGen->getBuildDatabase(false)->getTargetsForSourceFile(path); LOG_S(WARNING) << "List of possible targets for current file:\n"; for (auto const& target: targetsForSourceFile) { LOG_S(WARNING) << target->getOutput() << "\n"; diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index 81a5e05f3..c65772f28 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -76,7 +76,7 @@ namespace { fs::path serverBuildDir = buildPath / "temp"; fs::path compilerPath = CompilationUtils::getBundledCompilerPath(compilerName); CollectionUtils::FileSet stubsSources; - fs::path root = testGen.buildDatabase->getTargetPath(); + fs::path root = testGen.getBuildDatabase(false)->getTargetPath(); printer::TestMakefilesPrinter testMakefilePrinter(testGen, root, compilerPath, &stubsSources); testMakefilePrinter.addLinkTargetRecursively(root, ""); diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index 83e3cf214..7080170f8 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -8,10 +8,10 @@ #include "coverage/CoverageAndResultsGenerator.h" #include "streams/coverage/ServerCoverageAndResultsWriter.h" #include "streams/stubs/ServerStubsWriter.h" +#include "stubs/StubGen.h" +#include "Synchronizer.h" #include -#include -#include namespace { using testUtils::createFileRequest; diff --git a/server/test/framework/Targets_Test.cpp b/server/test/framework/Targets_Test.cpp index 2a4547d64..5fc2e13d3 100644 --- a/server/test/framework/Targets_Test.cpp +++ b/server/test/framework/Targets_Test.cpp @@ -132,7 +132,6 @@ TEST_F(TargetsTest, Valid_Target_Test_get_20) { projectName, suitePath, buildDirRelativePath, srcPaths, "get_20", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); -// fs::path get_20 = getTargetPathByName(*testGen.buildDatabase, "get_20"); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); @@ -173,8 +172,6 @@ TEST_F(TargetsTest, Valid_Target_Test_get_10_2) { projectName, suitePath, buildDirRelativePath, srcPaths, "get_10_2", false, false, 15); auto testGen = ProjectTestGen(*projectRequest.get(), writer.get(), TESTMODE, true); -// fs::path get_10_2 = getTargetPathByName(*testGen.buildDatabase, "get_10_2"); -// testGen.setTargetPath(get_10_2); Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); diff --git a/server/test/framework/main.cpp b/server/test/framework/main.cpp index c3f99d813..997b50eb3 100644 --- a/server/test/framework/main.cpp +++ b/server/test/framework/main.cpp @@ -4,6 +4,7 @@ #include "loguru.h" #include +#include "printers/DefaultMakefilePrinter.h" //Usage: ./UTBot_UnitTests [--verbosity trace|debug|info|warning|error] int main(int argc, char **argv) { @@ -51,7 +52,9 @@ int main(int argc, char **argv) { for (auto const &subproject : { "executable", "static_library", "shared_library", "timeout" }) { for (auto const &compiler : { clang, gcc }) { - testUtils::tryExecGetBuildCommands(testUtils::getRelativeTestSuitePath("run") / subproject, compiler); + testUtils::tryExecGetBuildCommands( + testUtils::getRelativeTestSuitePath(printer::DefaultMakefilePrinter::TARGET_RUN) / subproject, + compiler); } } From b43371ebdd60f5f6b74cdce4f3ceef94a39f156e Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Tue, 16 Aug 2022 20:22:40 +0300 Subject: [PATCH 20/27] refactoring after review 2 --- server/src/Server.cpp | 4 +- server/src/building/BuildDatabase.cpp | 348 +++----------------- server/src/building/BuildDatabase.h | 43 +-- server/src/building/Linker.cpp | 5 +- server/src/building/ProjectBuildDatabase.h | 17 + server/src/building/ProjectBuildDatabse.cpp | 215 ++++++++++++ server/src/building/TargetBuildDatabase.cpp | 68 ++++ server/src/building/TargetBuildDatabase.h | 35 ++ server/src/streams/FileTargetsWriter.cpp | 4 +- server/src/streams/TargetsWriter.h | 6 +- server/src/utils/GenerationUtils.h | 6 +- 11 files changed, 405 insertions(+), 346 deletions(-) create mode 100644 server/src/building/ProjectBuildDatabase.h create mode 100644 server/src/building/ProjectBuildDatabse.cpp create mode 100644 server/src/building/TargetBuildDatabase.cpp create mode 100644 server/src/building/TargetBuildDatabase.h diff --git a/server/src/Server.cpp b/server/src/Server.cpp index 344919dfd..1bf550f85 100644 --- a/server/src/Server.cpp +++ b/server/src/Server.cpp @@ -675,9 +675,9 @@ Status Server::TestsGenServiceImpl::GetFileTargets(ServerContext *context, utbot::ProjectContext projectContext{ request->projectcontext() }; auto buildDatabase = BuildDatabase::create(projectContext); fs::path path = request->path(); - auto targets = buildDatabase->getTargetsForSourceFile(path); + auto targetPaths = buildDatabase->getTargetPathsForSourceFile(path); FileTargetsWriter targetsWriter{ response }; - targetsWriter.writeResponse(targets, projectContext); + targetsWriter.writeResponse(targetPaths, projectContext); } catch (CompilationDatabaseException const& e) { return failedToLoadCDbStatus(e); } diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 2bb9d1ea0..8e219ffd4 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -31,102 +31,17 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath, return fs::exists(fullFilePath) ? fullFilePath.string() : possibleFilePath; } -BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath, - fs::path _serverBuildDir, - utbot::ProjectContext _projectContext) : - serverBuildDir(std::move(_serverBuildDir)), - projectContext(std::move(_projectContext)), - buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), - linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), - compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")), - isAutoTarget(true) { - if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { - throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); - } - - auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); - auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); - - initObjects(compileCommandsJson); - initInfo(linkCommandsJson); - filterInstalledFiles(); - addLocalSharedLibraries(); - fillTargetInfoParents(); - createClangCompileCommandsJson(); - target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; -} - -BuildDatabase::BuildDatabase(BuildDatabase &baseBuildDatabase, - const std::string &_target) : - serverBuildDir(baseBuildDatabase.serverBuildDir), - projectContext(baseBuildDatabase.projectContext), - buildCommandsJsonPath(baseBuildDatabase.buildCommandsJsonPath), - linkCommandsJsonPath(baseBuildDatabase.linkCommandsJsonPath), - compileCommandsJsonPath(baseBuildDatabase.compileCommandsJsonPath), - target(_target), - isAutoTarget(_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { - { - auto objectFilesList = baseBuildDatabase.getArchiveObjectFiles(target); - for (const auto &objectFilePath: objectFilesList) { - auto objectFileInfo = baseBuildDatabase.getClientCompilationObjectInfo(objectFilePath); - sourceFileInfos[objectFileInfo->getSourcePath()].push_back(objectFileInfo); - LOG_IF_S(DEBUG, sourceFileInfos[objectFileInfo->getSourcePath()].size() > 1) - << "Multiple compile commands for file \"" << objectFileInfo->getSourcePath() << "\" in target \"" - << target.string() << "\""; - objectFileInfos[objectFileInfo->getOutputFile()] = objectFileInfo; - objectFileTargets[objectFileInfo->getOutputFile()] = - baseBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()]; - } - } - - { - auto targetFilesList = baseBuildDatabase.getArchiveTargetFiles(target); - for (const auto &objectFilePath: targetFilesList) { - targetInfos[objectFilePath] = baseBuildDatabase.targetInfos[objectFilePath]; - } - } - - compileCommands_temp = baseBuildDatabase.compileCommands_temp; - createClangCompileCommandsJson(); -} - -std::shared_ptr BuildDatabase::createBuildDatabaseForSourceOrTarget(const std::string &_targetOrSourcePath) { - fs::path _target; - if (Paths::isSourceFile(_targetOrSourcePath)) { - _target = getRootForSource(_targetOrSourcePath); - } else if (_targetOrSourcePath == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _targetOrSourcePath.empty()) { - _target = getRootForFirstSource(); - } else { - auto new_target = GenerationUtils::findTarget(getAllTargets(), _targetOrSourcePath); - if (new_target.has_value()) { - _target = new_target.value(); - } else { - throw CompilationDatabaseException("Can't find target: " + _targetOrSourcePath); - } - } - return std::make_shared(std::move(BuildDatabase(*this, _target))); -} - -std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext) { - fs::path compileCommandsJsonPath = - CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( - projectContext.projectPath, projectContext.buildDirRelativePath); - fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); - std::shared_ptr buildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); - return buildDatabase; -} - fs::path BuildDatabase::createExplicitObjectFileCompilationCommand(const std::shared_ptr &objectInfo) { if (Paths::isSourceFile(objectInfo->getSourcePath())) { auto outputFile = objectInfo->getOutputFile(); auto tmpObjectFileName = - Paths::createTemporaryObjectFile(outputFile, objectInfo->getSourcePath()); + Paths::createTemporaryObjectFile(outputFile, objectInfo->getSourcePath()); objectInfo->setOutputFile(tmpObjectFileName); // redirect existing compilation command to temporary file LOG_IF_S(ERROR, CollectionUtils::containsKey(objectFileInfos, tmpObjectFileName)) - << "Temporary object file name generated by UTBot is already present in the " - "project: " - << tmpObjectFileName; + << "Temporary object file name generated by UTBot is already present in the " + "project: " + << tmpObjectFileName; objectInfo->linkUnit = outputFile; objectFileInfos[tmpObjectFileName] = objectInfo; return tmpObjectFileName; @@ -135,131 +50,13 @@ fs::path BuildDatabase::createExplicitObjectFileCompilationCommand(const std::sh } } -void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) { - for (const nlohmann::json &compileCommand: compileCommandsJson) { - auto objectInfo = std::make_shared(); - - fs::path directory = compileCommand.at("directory").get(); - fs::path jsonFile = compileCommand.at("file").get(); - fs::path sourceFile = Paths::getCCJsonFileFullPath(jsonFile, directory); - - std::vector jsonArguments; - if (compileCommand.contains("command")) { - std::string command = compileCommand.at("command"); - jsonArguments = StringUtils::splitByWhitespaces(command); - } else { - jsonArguments = std::vector(compileCommand.at("arguments")); - } - std::transform(jsonArguments.begin(), jsonArguments.end(), jsonArguments.begin(), - [&directory](const std::string &argument) { - return tryConvertOptionToPath(argument, directory); - }); - objectInfo->command = utbot::CompileCommand(jsonArguments, directory, sourceFile); - objectInfo->command.removeWerror(); - fs::path outputFile = objectInfo->getOutputFile(); - fs::path kleeFilePathTemplate = - Paths::createNewDirForFile(sourceFile, projectContext.buildDir(), serverBuildDir); - fs::path kleeFile = Paths::addSuffix(kleeFilePathTemplate, "_klee"); - objectInfo->kleeFilesInfo = std::make_shared(kleeFile); - - if (CollectionUtils::containsKey(objectFileInfos, outputFile) || - CollectionUtils::containsKey(targetInfos, outputFile)) { - /* - * If the condition above is true, that means that the output file - * is built from multiple sources. Hence, it is not an object file, - * but an executable, and it should be treated as a target. - * This is a hack. This inconsistency is produced by Bear - * when it treats a Makefile command like - * gcc -o output a.c b.c c.c - * This code is creating artificial compile and link commands, similar - * to commands Bear generates from CMake command like - * add_executable(output a.c b.c c.c) - */ - auto targetInfo = targetInfos[outputFile]; - if (targetInfo == nullptr) { - LOG_S(DEBUG) << outputFile << " is treated as a target instead of an object file"; - auto targetObjectInfo = objectFileInfos[outputFile]; - auto tmpObjectFileName = createExplicitObjectFileCompilationCommand(targetObjectInfo); - objectFileInfos.erase(outputFile); - - //create targetInfo - targetInfo = targetInfos[outputFile] = std::make_shared(); - targetInfo->commands.emplace_back( - std::initializer_list{targetObjectInfo->command.getBuildTool(), - "-o", outputFile, tmpObjectFileName}, - directory); - targetInfo->addFile(tmpObjectFileName); - } - //redirect new compilation command to temporary file - auto tmpObjectFileName = createExplicitObjectFileCompilationCommand(objectInfo); - - //add new dependency to an implicit target - targetInfo->commands[0].addFlagToEnd(tmpObjectFileName); - targetInfo->addFile(tmpObjectFileName); - } else { - objectFileInfos[outputFile] = objectInfo; - } - compileCommands_temp.emplace_back(compileCommand, objectInfo); - const fs::path &sourcePath = objectInfo->getSourcePath(); - sourceFileInfos[sourcePath].emplace_back(objectInfo); - } - for (auto &[sourceFile, objectInfos]: sourceFileInfos) { - std::sort(objectInfos.begin(), objectInfos.end(), conflictPriorityMore); - } -} - -void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { - for (nlohmann::json const &linkCommand : linkCommandsJson) { - fs::path directory = linkCommand.at("directory").get(); - std::vector jsonArguments; - if (linkCommand.contains("command")) { - std::string command = linkCommand.at("command"); - jsonArguments = StringUtils::splitByWhitespaces(command); - } else { - jsonArguments = std::vector(linkCommand.at("arguments")); - } - if (StringUtils::endsWith(jsonArguments[0], "ranlib") || - StringUtils::endsWith(jsonArguments[0], "cmake")) { - continue; - } - std::transform(jsonArguments.begin(), jsonArguments.end(), jsonArguments.begin(), - [&directory](const std::string &argument) { - return tryConvertOptionToPath(argument, directory); - }); - - mergeLibraryOptions(jsonArguments); - - utbot::LinkCommand command(jsonArguments, directory); - fs::path const &output = command.getOutput(); - auto targetInfo = targetInfos[output]; - if (targetInfo == nullptr) { - targetInfo = targetInfos[output] = std::make_shared(); - } else { - LOG_S(WARNING) << "Multiple commands for one file: " << output.string(); - } - for (nlohmann::json const &jsonFile: linkCommand.at("files")) { - auto filename = jsonFile.get(); - fs::path currentFile = Paths::getCCJsonFileFullPath(filename, command.getDirectory()); - targetInfo->addFile(currentFile); - if (Paths::isObjectFile(currentFile)) { - if (!CollectionUtils::containsKey(objectFileInfos, currentFile)) { - throw CompilationDatabaseException("compile_commands.json doesn't contain a command for object file " - + currentFile.string()); - } - objectFileInfos[currentFile]->linkUnit = output; - } - } - targetInfo->commands.emplace_back(command); - } -} - void BuildDatabase::createClangCompileCommandsJson() { CollectionUtils::MapFileTo>> fileCompileCommands; for (const auto &[compileCommand, objectInfo]: compileCommands_temp) { const fs::path &sourcePath = objectInfo->getSourcePath(); if (CollectionUtils::contains(fileCompileCommands, sourcePath)) { LOG_S(WARNING) << "Multiple compile commands for file \"" << sourcePath - << "\" use command for \"" << objectInfo->getOutputFile() << "\""; + << "\" use command for \"" << objectInfo->getOutputFile() << "\""; } else if (CollectionUtils::contains(objectFileInfos, objectInfo->getOutputFile())) { fileCompileCommands[sourcePath] = {compileCommand, objectInfo}; } @@ -290,7 +87,7 @@ namespace { CollectionUtils::OrderedFileSet collectLibraryDirs(const utbot::BaseCommand &command) { using namespace DynamicLibraryUtils; CollectionUtils::OrderedFileSet libraryDirs; - for (std::string const &argument : command.getCommandLine()) { + for (std::string const &argument: command.getCommandLine()) { auto optionalLibraryPath = getLibraryAbsolutePath(argument, command.getDirectory()); if (optionalLibraryPath.has_value()) { libraryDirs.insert(optionalLibraryPath.value()); @@ -298,7 +95,7 @@ namespace { if (StringUtils::startsWith(argument, libraryDirOptionWl)) { auto commaSeparated = StringUtils::split(argument, ','); bool isRpathNext = false; - for (auto part : commaSeparated) { + for (auto part: commaSeparated) { if (part == rpathFlag) { isRpathNext = true; continue; @@ -318,7 +115,7 @@ namespace { CollectionUtils::MapFileTo libraryNames; - for (const auto &argument : command.getCommandLine()) { + for (const auto &argument: command.getCommandLine()) { if (Paths::isSharedLibraryFile(argument) && argument != command.getOutput() && !StringUtils::startsWith(argument, libraryDirOptionWl)) { libraryNames.emplace(argument, argument); @@ -345,9 +142,9 @@ void BuildDatabase::addLibrariesForCommand(utbot::BaseCommand &command, auto libraryDirs = collectLibraryDirs(command); auto libraryNames = collectLibraryNames(command); std::unordered_map argumentToFile; - for (auto const &[libraryName, argument] : libraryNames) { + for (auto const &[libraryName, argument]: libraryNames) { fs::path name = libraryName; - for (auto const &libraryDir : libraryDirs) { + for (auto const &libraryDir: libraryDirs) { if (CollectionUtils::containsKey(sharedLibraryFiles, name)) { if (CollectionUtils::containsKey(sharedLibraryFiles.at(name), libraryDir)) { name = sharedLibraryFiles.at(name).at(libraryDir); @@ -364,72 +161,13 @@ void BuildDatabase::addLibrariesForCommand(utbot::BaseCommand &command, } } } - for (auto &argument : command.getCommandLine()) { + for (auto &argument: command.getCommandLine()) { if (CollectionUtils::containsKey(argumentToFile, argument)) { argument = argumentToFile[argument]; } } } -void BuildDatabase::filterInstalledFiles() { - for (auto &it : targetInfos) { - auto &linkFile = it.first; - auto &targetInfo = it.second; - CollectionUtils::OrderedFileSet fileset; - targetInfo->installedFiles = - CollectionUtils::filterOut(targetInfo->files, [this](fs::path const &file) { - return CollectionUtils::containsKey(targetInfos, file) || - CollectionUtils::containsKey(objectFileInfos, file); - }); - if (!targetInfo->installedFiles.empty()) { - LOG_S(DEBUG) << "Target " << linkFile << " depends on " << targetInfo->installedFiles.size() << " installed files"; - } - CollectionUtils::erase_if(targetInfo->files, [&targetInfo](fs::path const &file) { - return CollectionUtils::contains(targetInfo->installedFiles, file); - }); - } -} - -void BuildDatabase::addLocalSharedLibraries() { - sharedLibrariesMap sharedLibraryFiles; - for (const auto &[linkFile, linkUnit] : targetInfos) { - if (Paths::isSharedLibraryFile(linkFile)) { - auto withoutVersion = CompilationUtils::removeSharedLibraryVersion(linkFile); - sharedLibraryFiles[withoutVersion.filename()][linkFile.parent_path()] = linkFile; - } - } - for (auto &[linkFile, targetInfo] : targetInfos) { - for (auto &command : targetInfo->commands) { - addLibrariesForCommand(command, *targetInfo, sharedLibraryFiles); - } - } - for (auto &[objectFile, objectInfo] : objectFileInfos) { - addLibrariesForCommand(objectInfo->command, *objectInfo, sharedLibraryFiles, true); - } -} - -void BuildDatabase::fillTargetInfoParents() { - CollectionUtils::MapFileTo> parentTargets; - for (const auto &[linkFile, linkUnit] : targetInfos) { - for (const fs::path &dependencyFile : linkUnit->files) { - if (Paths::isLibraryFile(dependencyFile)) { - parentTargets[dependencyFile].emplace_back(linkFile); - } - if (Paths::isObjectFile(dependencyFile)) { - objectFileTargets[dependencyFile].emplace_back(linkFile); - } - } - } - for (auto &[library, parents] : parentTargets) { - if (!CollectionUtils::containsKey(targetInfos, library)) { - throw CompilationDatabaseException( - "link_commands.json doesn't contain a command for building library: " + - library.string() + "\nReferenced from command for: " + (parents.empty() ? "none" : parents[0].string())); - } - targetInfos[library]->parentLinkUnits = std::move(parents); - } -} - const fs::path &BuildDatabase::getCompileCommandsJson() { return compileCommandsJsonPath; } @@ -441,7 +179,7 @@ const fs::path &BuildDatabase::getLinkCommandsJson() { std::vector> BuildDatabase::getAllCompileCommands() const { std::vector> result; - for (auto &[file, compilationUnit] : objectFileInfos) { + for (auto &[file, compilationUnit]: objectFileInfos) { result.emplace_back(compilationUnit); } return result; @@ -462,11 +200,11 @@ CollectionUtils::FileSet BuildDatabase::getArchiveObjectFiles(const fs::path &ar } if (!CollectionUtils::containsKey(targetInfos, archive)) { throw CompilationDatabaseException( - "Couldn't find current archive file linkage information for " + archive.string()); + "Couldn't find current archive file linkage information for " + archive.string()); } std::shared_ptr targetInfo = targetInfos.at(archive); CollectionUtils::FileSet result; - for (const auto &file : targetInfo->files) { + for (const auto &file: targetInfo->files) { if (Paths::isLibraryFile(file)) { auto archiveObjectFiles = getArchiveObjectFiles(file); CollectionUtils::extend(result, archiveObjectFiles); @@ -488,7 +226,7 @@ CollectionUtils::FileSet BuildDatabase::getArchiveTargetFiles(const fs::path &ar } if (!CollectionUtils::containsKey(targetInfos, archive)) { throw CompilationDatabaseException( - "Couldn't find current archive file linkage information for " + archive.string()); + "Couldn't find current archive file linkage information for " + archive.string()); } std::shared_ptr targetInfo = targetInfos.at(archive); CollectionUtils::FileSet result = {archive}; @@ -502,22 +240,25 @@ CollectionUtils::FileSet BuildDatabase::getArchiveTargetFiles(const fs::path &ar return result; } -fs::path BuildDatabase::getRootForSource(const fs::path& path) const { +fs::path BuildDatabase::getRootForSource(const fs::path &path) const { fs::path normalizedPath = Paths::normalizedTrimmed(path); if (Paths::isSourceFile(normalizedPath)) { if (!CollectionUtils::containsKey(sourceFileInfos, normalizedPath)) { - throw CompilationDatabaseException("No executable or library found for current source file in link_commands.json: " + path.string()); + throw CompilationDatabaseException( + "No executable or library found for current source file in link_commands.json: " + path.string()); } auto const &sourceFileInfo = sourceFileInfos.at(normalizedPath); auto linkUnit = sourceFileInfo[0]->linkUnit; if (linkUnit.empty()) { - throw CompilationDatabaseException("No executable or library found for current source file in link_commands.json: " + path.string()); + throw CompilationDatabaseException( + "No executable or library found for current source file in link_commands.json: " + path.string()); } return getRootForSource(linkUnit); } else { if (!CollectionUtils::containsKey(targetInfos, normalizedPath)) { - throw CompilationDatabaseException("No executable or library found for current library in link_commands.json: " + path.string()); + throw CompilationDatabaseException( + "No executable or library found for current library in link_commands.json: " + path.string()); } auto linkUnit = targetInfos.at(normalizedPath); if (!linkUnit->parentLinkUnits.empty()) { @@ -528,7 +269,6 @@ fs::path BuildDatabase::getRootForSource(const fs::path& path) const { } } - fs::path BuildDatabase::getRootForFirstSource() const { return getRootForSource(sourceFileInfos.begin()->first); } @@ -558,14 +298,16 @@ fs::path BuildDatabase::getBitcodeFile(const fs::path &filepath) const { } -std::shared_ptr BuildDatabase::getClientCompilationObjectInfo(const fs::path &filepath) const { +std::shared_ptr +BuildDatabase::getClientCompilationObjectInfo(const fs::path &filepath) const { if (!CollectionUtils::contains(objectFileInfos, filepath)) { throw CompilationDatabaseException("Object file not found in compilation_commands.json: " + filepath.string()); } return objectFileInfos.at(filepath); } -std::shared_ptr BuildDatabase::getClientCompilationSourceInfo(const fs::path &filepath) const { +std::shared_ptr +BuildDatabase::getClientCompilationSourceInfo(const fs::path &filepath) const { if (!CollectionUtils::contains(sourceFileInfos, filepath)) { throw CompilationDatabaseException("Source file not found in compilation_commands.json: " + filepath.string()); } @@ -573,7 +315,8 @@ std::shared_ptr BuildDatabase::getClientCompilati return sourceFileInfos.at(filepath)[0]; } -std::shared_ptr BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const { +std::shared_ptr +BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const { if (Paths::isSourceFile(filepath)) { return getClientCompilationSourceInfo(filepath); } @@ -595,7 +338,7 @@ std::shared_ptr BuildDatabase::getClientLinkUni filepath.string()); } -bool BuildDatabase::conflictPriorityMore( +bool BuildDatabase::ObjectFileInfo::conflictPriorityMore( const std::shared_ptr &left, const std::shared_ptr &right) { if (StringUtils::contains(left->getOutputFile().string(), "64")) { @@ -640,11 +383,11 @@ fs::path BuildDatabase::KleeFilesInfo::getKleeBitcodeFile() { return getKleeBitcodeFile(""); } -fs::path BuildDatabase::KleeFilesInfo::getKleeFile(const std::string& methodName) { +fs::path BuildDatabase::KleeFilesInfo::getKleeFile(const std::string &methodName) { return kleeFile; } -fs::path BuildDatabase::KleeFilesInfo::getKleeBitcodeFile(const std::string& methodName) { +fs::path BuildDatabase::KleeFilesInfo::getKleeBitcodeFile(const std::string &methodName) { return getCorrespondingBitcodeFile(getKleeFile()); } @@ -706,9 +449,9 @@ BuildDatabase::getTargetsForSourceFile(const fs::path &sourceFilePath) const { } auto linkUnitInfo = getClientLinkUnitInfo(unitFile); bool result = CollectionUtils::anyTrue(CollectionUtils::transform( - linkUnitInfo->files, [&containsSourceFilePath](fs::path const &subFile) { - return containsSourceFilePath(subFile); - })); + linkUnitInfo->files, [&containsSourceFilePath](fs::path const &subFile) { + return containsSourceFilePath(subFile); + })); return cache[unitFile] = result; }; @@ -719,28 +462,23 @@ BuildDatabase::getTargetsForSourceFile(const fs::path &sourceFilePath) const { }); } -std::vector BuildDatabase::targetListForFile(const fs::path &sourceFilePath, - const fs::path &objectFile) const { - if (!hasAutoTarget()) { - return { target }; - } +std::vector BuildDatabase::getTargetPathsForSourceFile(const fs::path &sourceFilePath) const { auto result = CollectionUtils::transformTo>( getTargetsForSourceFile(sourceFilePath), [&](const std::shared_ptr &targetInfo) { return targetInfo->getOutput(); }); + return result; +} + +std::vector BuildDatabase::getTargetPathsForObjectFile(const fs::path &objectFile) const { std::vector parents; if (CollectionUtils::containsKey(objectFileTargets, objectFile)) { parents = objectFileTargets.at(objectFile); } else { LOG_S(WARNING) << "No link unit parents were found for an object file: " << objectFile; } - result.insert( - result.end(), - std::make_move_iterator(parents.begin()), - std::make_move_iterator(parents.end()) - ); - return result; + return parents; } std::shared_ptr BuildDatabase::getPriorityTarget() const { @@ -754,7 +492,7 @@ std::shared_ptr BuildDatabase::getPriorityTarget() co } auto linkUnitInfo = getClientLinkUnitInfo(unitFile); int result = 0; - for (const fs::path &subFile : linkUnitInfo->files) { + for (const fs::path &subFile: linkUnitInfo->files) { result += numberOfSources(subFile); } return cache[unitFile] = result; @@ -762,8 +500,8 @@ std::shared_ptr BuildDatabase::getPriorityTarget() co auto rootTargets = getRootTargets(); auto it = std::max_element(rootTargets.begin(), rootTargets.end(), - [&](const std::shared_ptr& a, - const std::shared_ptr& b) { + [&](const std::shared_ptr &a, + const std::shared_ptr &b) { return numberOfSources(a->getOutput()) < numberOfSources(b->getOutput()); }); diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index f22ee4858..64fb80ad3 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -75,6 +75,9 @@ class BuildDatabase { [[nodiscard]] bool is32bits() const; void setOutputFile(const fs::path &file); + + static bool conflictPriorityMore(const std::shared_ptr &left, + const std::shared_ptr &right); }; /* @@ -95,18 +98,11 @@ class BuildDatabase { }; public: - BuildDatabase(fs::path _buildCommandsJsonPath, - fs::path _serverBuildDir, - utbot::ProjectContext _projectContext); - - static std::shared_ptr create(const utbot::ProjectContext &projectContext); - std::shared_ptr createBuildDatabaseForSourceOrTarget(const std::string &_targetOrSourcePath); - const fs::path &getCompileCommandsJson(); const fs::path &getLinkCommandsJson(); /** - * @brief Returns all object files files that are contained in a library or executable + * @brief Returns all object files that are contained in a library or executable * * Recursively iterates over all libraries inside current library or executable. Returns all * found object files @@ -197,56 +193,45 @@ class BuildDatabase { std::vector> getAllCompileCommands() const; std::vector> getRootTargets() const; - std::vector> getAllTargets() const; - - std::vector - targetListForFile(const fs::path &sourceFilePath, const fs::path &objectFile) const; + virtual std::vector> getAllTargets() const = 0; - std::vector> - getTargetsForSourceFile(fs::path const &sourceFilePath) const; + virtual std::vector + getTargetPathsForSourceFile(const fs::path &sourceFilePath) const; + virtual std::vector + getTargetPathsForObjectFile(const fs::path &objectFile) const; std::shared_ptr getPriorityTarget() const; CollectionUtils::FileSet getSourceFilesForTarget(const fs::path &_target); - bool hasAutoTarget() const; + virtual bool hasAutoTarget() const = 0; fs::path getTargetPath() const; std::shared_ptr compilationDatabase; -private: - BuildDatabase(BuildDatabase& baseBuildDatabase, - const std::string &_target); +protected: + BuildDatabase() ; const fs::path serverBuildDir; const utbot::ProjectContext projectContext; const fs::path buildCommandsJsonPath; const fs::path linkCommandsJsonPath; const fs::path compileCommandsJsonPath; - fs::path target; - bool isAutoTarget; CollectionUtils::MapFileTo>> sourceFileInfos; CollectionUtils::MapFileTo> objectFileInfos; CollectionUtils::MapFileTo> targetInfos; CollectionUtils::MapFileTo> objectFileTargets; - std::vector>> compileCommands_temp; - static bool conflictPriorityMore(const std::shared_ptr &left, - const std::shared_ptr &right); - - void filterInstalledFiles(); - void addLocalSharedLibraries(); - void fillTargetInfoParents(); static fs::path getCorrespondingBitcodeFile(const fs::path &filepath); - void initObjects(const nlohmann::json &compileCommandsJson); - void initInfo(const nlohmann::json &linkCommandsJson); void createClangCompileCommandsJson(); void mergeLibraryOptions(std::vector &jsonArguments) const; fs::path newDirForFile(fs::path const& file) const; fs::path createExplicitObjectFileCompilationCommand(const std::shared_ptr &objectInfo); + std::vector> getTargetsForSourceFile(fs::path const &sourceFilePath) const; + using sharedLibrariesMap = std::unordered_map>; void addLibrariesForCommand(utbot::BaseCommand &command, diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 3793a8bbe..c33eeb637 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -96,7 +96,7 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { if (!testGen.getBuildDatabase(false)->isFirstObjectFileForSource(objectFile)) { return; } - std::vector targets = testGen.getBuildDatabase(false)->targetListForFile(sourceFilePath, objectFile); + std::vector targets = testGen.getBuildDatabase(false)->getTargetPathsForObjectFile(objectFile); LOG_S(DEBUG) << "Linking bitcode for file " << sourceFilePath.filename(); for (size_t i = 0; i < targets.size(); i++) { const auto& target = targets[i]; @@ -174,7 +174,8 @@ void Linker::linkForProject() { << sourceFile; return; } - std::vector targets = testGen.getBuildDatabase(false)->targetListForFile(sourceFile, objectFile); + std::vector targets = testGen.getBuildDatabase(false)->getTargetPathsForObjectFile( + objectFile); bool success = false; for (const auto &target : targets) { if (!CollectionUtils::contains(triedTargets, target)) { diff --git a/server/src/building/ProjectBuildDatabase.h b/server/src/building/ProjectBuildDatabase.h new file mode 100644 index 000000000..3efbab2d1 --- /dev/null +++ b/server/src/building/ProjectBuildDatabase.h @@ -0,0 +1,17 @@ +#ifndef UTBOTCPP_PROJECTBUILDDATABASE_H +#define UTBOTCPP_PROJECTBUILDDATABASE_H + +#include "BuildDatabase.h" + +class ProjectBuildDatabase : BuildDatabase { +private: + ProjectBuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, + utbot::ProjectContext _projectContext); + +public: + static std::shared_ptr create(const utbot::ProjectContext &projectContext); + bool hasAutoTarget() const override; +}; + + +#endif //UTBOTCPP_PROJECTBUILDDATABASE_H diff --git a/server/src/building/ProjectBuildDatabse.cpp b/server/src/building/ProjectBuildDatabse.cpp new file mode 100644 index 000000000..af548ce4d --- /dev/null +++ b/server/src/building/ProjectBuildDatabse.cpp @@ -0,0 +1,215 @@ +#include "ProjectBuildDatabase.h" + +ProjectBuildDatabase::ProjectBuildDatabase(fs::path _buildCommandsJsonPath, + fs::path _serverBuildDir, + utbot::ProjectContext _projectContext) : + serverBuildDir(std::move(_serverBuildDir)), + projectContext(std::move(_projectContext)), + buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), + linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), + compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { + if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { + throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); + } + + auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); + auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); + + initObjects(compileCommandsJson); + initInfo(linkCommandsJson); + filterInstalledFiles(); + addLocalSharedLibraries(); + fillTargetInfoParents(); + createClangCompileCommandsJson(); + target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; +} + +std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext) { + fs::path compileCommandsJsonPath = + CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( + projectContext.projectPath, projectContext.buildDirRelativePath); + fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); + std::shared_ptr buildDatabase = std::make_shared(compileCommandsJsonPath, + serverBuildDir, projectContext); + return buildDatabase; +} + + +void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) { + for (const nlohmann::json &compileCommand: compileCommandsJson) { + auto objectInfo = std::make_shared(); + + fs::path directory = compileCommand.at("directory").get(); + fs::path jsonFile = compileCommand.at("file").get(); + fs::path sourceFile = Paths::getCCJsonFileFullPath(jsonFile, directory); + + std::vector jsonArguments; + if (compileCommand.contains("command")) { + std::string command = compileCommand.at("command"); + jsonArguments = StringUtils::splitByWhitespaces(command); + } else { + jsonArguments = std::vector(compileCommand.at("arguments")); + } + std::transform(jsonArguments.begin(), jsonArguments.end(), jsonArguments.begin(), + [&directory](const std::string &argument) { + return tryConvertOptionToPath(argument, directory); + }); + objectInfo->command = utbot::CompileCommand(jsonArguments, directory, sourceFile); + objectInfo->command.removeWerror(); + fs::path outputFile = objectInfo->getOutputFile(); + fs::path kleeFilePathTemplate = + Paths::createNewDirForFile(sourceFile, projectContext.buildDir(), serverBuildDir); + fs::path kleeFile = Paths::addSuffix(kleeFilePathTemplate, "_klee"); + objectInfo->kleeFilesInfo = std::make_shared(kleeFile); + + if (CollectionUtils::containsKey(objectFileInfos, outputFile) || + CollectionUtils::containsKey(targetInfos, outputFile)) { + /* + * If the condition above is true, that means that the output file + * is built from multiple sources. Hence, it is not an object file, + * but an executable, and it should be treated as a target. + * This is a hack. This inconsistency is produced by Bear + * when it treats a Makefile command like + * gcc -o output a.c b.c c.c + * This code is creating artificial compile and link commands, similar + * to commands Bear generates from CMake command like + * add_executable(output a.c b.c c.c) + */ + auto targetInfo = targetInfos[outputFile]; + if (targetInfo == nullptr) { + LOG_S(DEBUG) << outputFile << " is treated as a target instead of an object file"; + auto targetObjectInfo = objectFileInfos[outputFile]; + auto tmpObjectFileName = createExplicitObjectFileCompilationCommand(targetObjectInfo); + objectFileInfos.erase(outputFile); + + //create targetInfo + targetInfo = targetInfos[outputFile] = std::make_shared(); + targetInfo->commands.emplace_back( + std::initializer_list{targetObjectInfo->command.getBuildTool(), + "-o", outputFile, tmpObjectFileName}, + directory); + targetInfo->addFile(tmpObjectFileName); + } + //redirect new compilation command to temporary file + auto tmpObjectFileName = createExplicitObjectFileCompilationCommand(objectInfo); + + //add new dependency to an implicit target + targetInfo->commands[0].addFlagToEnd(tmpObjectFileName); + targetInfo->addFile(tmpObjectFileName); + } else { + objectFileInfos[outputFile] = objectInfo; + } + compileCommands_temp.emplace_back(compileCommand, objectInfo); + const fs::path &sourcePath = objectInfo->getSourcePath(); + sourceFileInfos[sourcePath].emplace_back(objectInfo); + } + for (auto &[sourceFile, objectInfos]: sourceFileInfos) { + std::sort(objectInfos.begin(), objectInfos.end(), BuildDatabase::ObjectFileInfo::conflictPriorityMore); + } +} + +void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { + for (nlohmann::json const &linkCommand : linkCommandsJson) { + fs::path directory = linkCommand.at("directory").get(); + std::vector jsonArguments; + if (linkCommand.contains("command")) { + std::string command = linkCommand.at("command"); + jsonArguments = StringUtils::splitByWhitespaces(command); + } else { + jsonArguments = std::vector(linkCommand.at("arguments")); + } + if (StringUtils::endsWith(jsonArguments[0], "ranlib") || + StringUtils::endsWith(jsonArguments[0], "cmake")) { + continue; + } + std::transform(jsonArguments.begin(), jsonArguments.end(), jsonArguments.begin(), + [&directory](const std::string &argument) { + return tryConvertOptionToPath(argument, directory); + }); + + mergeLibraryOptions(jsonArguments); + + utbot::LinkCommand command(jsonArguments, directory); + fs::path const &output = command.getOutput(); + auto targetInfo = targetInfos[output]; + if (targetInfo == nullptr) { + targetInfo = targetInfos[output] = std::make_shared(); + } else { + LOG_S(WARNING) << "Multiple commands for one file: " << output.string(); + } + for (nlohmann::json const &jsonFile: linkCommand.at("files")) { + auto filename = jsonFile.get(); + fs::path currentFile = Paths::getCCJsonFileFullPath(filename, command.getDirectory()); + targetInfo->addFile(currentFile); + if (Paths::isObjectFile(currentFile)) { + if (!CollectionUtils::containsKey(objectFileInfos, currentFile)) { + throw CompilationDatabaseException("compile_commands.json doesn't contain a command for object file " + + currentFile.string()); + } + objectFileInfos[currentFile]->linkUnit = output; + } + } + targetInfo->commands.emplace_back(command); + } +} + + +void BuildDatabase::filterInstalledFiles() { + for (auto &it : targetInfos) { + auto &linkFile = it.first; + auto &targetInfo = it.second; + CollectionUtils::OrderedFileSet fileset; + targetInfo->installedFiles = + CollectionUtils::filterOut(targetInfo->files, [this](fs::path const &file) { + return CollectionUtils::containsKey(targetInfos, file) || + CollectionUtils::containsKey(objectFileInfos, file); + }); + if (!targetInfo->installedFiles.empty()) { + LOG_S(DEBUG) << "Target " << linkFile << " depends on " << targetInfo->installedFiles.size() << " installed files"; + } + CollectionUtils::erase_if(targetInfo->files, [&targetInfo](fs::path const &file) { + return CollectionUtils::contains(targetInfo->installedFiles, file); + }); + } +} + +void BuildDatabase::addLocalSharedLibraries() { + sharedLibrariesMap sharedLibraryFiles; + for (const auto &[linkFile, linkUnit] : targetInfos) { + if (Paths::isSharedLibraryFile(linkFile)) { + auto withoutVersion = CompilationUtils::removeSharedLibraryVersion(linkFile); + sharedLibraryFiles[withoutVersion.filename()][linkFile.parent_path()] = linkFile; + } + } + for (auto &[linkFile, targetInfo] : targetInfos) { + for (auto &command : targetInfo->commands) { + addLibrariesForCommand(command, *targetInfo, sharedLibraryFiles); + } + } + for (auto &[objectFile, objectInfo] : objectFileInfos) { + addLibrariesForCommand(objectInfo->command, *objectInfo, sharedLibraryFiles, true); + } +} + +void BuildDatabase::fillTargetInfoParents() { + CollectionUtils::MapFileTo> parentTargets; + for (const auto &[linkFile, linkUnit] : targetInfos) { + for (const fs::path &dependencyFile : linkUnit->files) { + if (Paths::isLibraryFile(dependencyFile)) { + parentTargets[dependencyFile].emplace_back(linkFile); + } + if (Paths::isObjectFile(dependencyFile)) { + objectFileTargets[dependencyFile].emplace_back(linkFile); + } + } + } + for (auto &[library, parents] : parentTargets) { + if (!CollectionUtils::containsKey(targetInfos, library)) { + throw CompilationDatabaseException( + "link_commands.json doesn't contain a command for building library: " + + library.string() + "\nReferenced from command for: " + (parents.empty() ? "none" : parents[0].string())); + } + targetInfos[library]->parentLinkUnits = std::move(parents); + } +} + diff --git a/server/src/building/TargetBuildDatabase.cpp b/server/src/building/TargetBuildDatabase.cpp new file mode 100644 index 000000000..373a1d30b --- /dev/null +++ b/server/src/building/TargetBuildDatabase.cpp @@ -0,0 +1,68 @@ +#include "TargetBuildDatabase.h" + + +TargetBuildDatabase::TargetBuildDatabase(BuildDatabase &baseBuildDatabase, + const std::string &_target) : + serverBuildDir(baseBuildDatabase.serverBuildDir), + projectContext(baseBuildDatabase.projectContext), + buildCommandsJsonPath(baseBuildDatabase.buildCommandsJsonPath), + linkCommandsJsonPath(baseBuildDatabase.linkCommandsJsonPath), + compileCommandsJsonPath(baseBuildDatabase.compileCommandsJsonPath), + target(_target), + isAutoTarget(_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { + { + auto objectFilesList = baseBuildDatabase.getArchiveObjectFiles(target); + for (const auto &objectFilePath: objectFilesList) { + auto objectFileInfo = baseBuildDatabase.getClientCompilationObjectInfo(objectFilePath); + sourceFileInfos[objectFileInfo->getSourcePath()].push_back(objectFileInfo); + LOG_IF_S(DEBUG, sourceFileInfos[objectFileInfo->getSourcePath()].size() > 1) + << "Multiple compile commands for file \"" << objectFileInfo->getSourcePath() << "\" in target \"" + << target.string() << "\""; + objectFileInfos[objectFileInfo->getOutputFile()] = objectFileInfo; + objectFileTargets[objectFileInfo->getOutputFile()] = + baseBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()]; + } + } + + { + auto targetFilesList = baseBuildDatabase.getArchiveTargetFiles(target); + for (const auto &objectFilePath: targetFilesList) { + targetInfos[objectFilePath] = baseBuildDatabase.targetInfos[objectFilePath]; + } + } + + compileCommands_temp = baseBuildDatabase.compileCommands_temp; + createClangCompileCommandsJson(); +} + +std::shared_ptr BuildDatabase::createForSourceOrTarget(const std::string &_targetOrSourcePath) { + fs::path _target; + if (Paths::isSourceFile(_targetOrSourcePath)) { + _target = getRootForSource(_targetOrSourcePath); + } else if (_targetOrSourcePath == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _targetOrSourcePath.empty()) { + _target = getRootForFirstSource(); + } else { + auto new_target = GenerationUtils::findTarget(getAllTargets(), _targetOrSourcePath); + if (new_target.has_value()) { + _target = new_target.value(); + } else { + throw CompilationDatabaseException("Can't find target: " + _targetOrSourcePath); + } + } + return std::make_shared(std::move(BuildDatabase(*this, _target))); +} + +std::vector TargetBuildDatabase::getTargetPathsForSourceFile(const fs::path &sourceFilePath) const { + if (!hasAutoTarget()) { + return {target}; + } + return BuildDatabase::getTargetPathsForSourceFile(sourceFilePath); +} + + +std::vector TargetBuildDatabase::getTargetPathsForObjectFile(const fs::path &objectFile) const { + if (!hasAutoTarget()) { + return {target}; + } + return BuildDatabase::getTargetPathsForObjectFile(objectFile); +} diff --git a/server/src/building/TargetBuildDatabase.h b/server/src/building/TargetBuildDatabase.h new file mode 100644 index 000000000..61fdef237 --- /dev/null +++ b/server/src/building/TargetBuildDatabase.h @@ -0,0 +1,35 @@ +#ifndef UTBOTCPP_TARGETBUILDDATABASE_H +#define UTBOTCPP_TARGETBUILDDATABASE_H + +#include "BuildDatabase.h" + + +class TargetBuildDatabase : BuildDatabase { +private: + TargetBuildDatabase(BuildDatabase &baseBuildDatabase, const std::string &_target); + + void filterInstalledFiles(); + + void addLocalSharedLibraries(); + + void fillTargetInfoParents(); + + void initObjects(const nlohmann::json &compileCommandsJson); + + void initInfo(const nlohmann::json &linkCommandsJson); + + fs::path target; + bool isAutoTarget; +public: + std::shared_ptr createForSourceOrTarget(std::shared_ptr baseBuildDatabase, + std::string &_targetOrSourcePath); + + bool hasAutoTarget() const override; + + std::vector getTargetPathsForSourceFile(const fs::path &sourceFilePath) const override; + + std::vector getTargetPathsForObjectFile(const fs::path &objectFile) const override; +}; + + +#endif //UTBOTCPP_TARGETBUILDDATABASE_H diff --git a/server/src/streams/FileTargetsWriter.cpp b/server/src/streams/FileTargetsWriter.cpp index c71793aaf..467a99426 100644 --- a/server/src/streams/FileTargetsWriter.cpp +++ b/server/src/streams/FileTargetsWriter.cpp @@ -1,10 +1,10 @@ #include "FileTargetsWriter.h" void FileTargetsWriter::writeResponse( - const std::vector> &targets, + const std::vector &targetPaths, const utbot::ProjectContext &projectContext) { if (!hasStream()) { return; } - writeTargets(targets, projectContext); + writeTargets(targetPaths, projectContext); } diff --git a/server/src/streams/TargetsWriter.h b/server/src/streams/TargetsWriter.h index d6102ecb5..ad40fcada 100644 --- a/server/src/streams/TargetsWriter.h +++ b/server/src/streams/TargetsWriter.h @@ -13,13 +13,13 @@ class TargetsWriter : public MessageWriter { protected: using MessageWriter::writer; - void writeTargets(const std::vector> &targets, + void writeTargets(const std::vector &targetPaths, const utbot::ProjectContext &projectContext) { auto projectTarget = writer->add_targets(); *projectTarget = GrpcUtils::createAutoTarget(); - for (auto const &target : targets) { + for (auto const &target : targetPaths) { projectTarget = writer->add_targets(); - GrpcUtils::initProjectTarget(*projectTarget, projectContext, target->getOutput()); + GrpcUtils::initProjectTarget(*projectTarget, projectContext, target); } } }; diff --git a/server/src/utils/GenerationUtils.h b/server/src/utils/GenerationUtils.h index 06862aa25..0f2750393 100644 --- a/server/src/utils/GenerationUtils.h +++ b/server/src/utils/GenerationUtils.h @@ -44,10 +44,10 @@ namespace GenerationUtils { if (status.error_message() == FileNotPresentedInArtifactException::MESSAGE || status.error_message() == FileNotPresentedInCommandsException::MESSAGE) { fs::path path = status.error_details(); - auto targetsForSourceFile = testGen->getBuildDatabase(false)->getTargetsForSourceFile(path); + auto targetPaths = testGen->getBuildDatabase(true)->getTargetPathsForSourceFile(path); LOG_S(WARNING) << "List of possible targets for current file:\n"; - for (auto const& target: targetsForSourceFile) { - LOG_S(WARNING) << target->getOutput() << "\n"; + for (auto const& target: targetPaths) { + LOG_S(WARNING) << target << "\n"; } LOG_S(WARNING) << "\n"; } From e9eeb2d814a37a56b4dd1912548a9173a0fbb648 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Wed, 17 Aug 2022 19:32:29 +0300 Subject: [PATCH 21/27] refactoring after review 3 --- server/src/KleeGenerator.cpp | 294 +++++++++--------- server/src/KleeGenerator.h | 12 +- server/src/ReturnTypesFetcher.cpp | 2 +- server/src/Server.cpp | 148 ++++----- server/src/Synchronizer.cpp | 10 +- server/src/building/BuildDatabase.cpp | 55 ++-- server/src/building/BuildDatabase.h | 53 +++- server/src/building/Linker.cpp | 56 ++-- server/src/building/ProjectBuildDatabase.h | 18 +- server/src/building/ProjectBuildDatabse.cpp | 84 +++-- server/src/building/TargetBuildDatabase.cpp | 51 +-- server/src/building/TargetBuildDatabase.h | 21 +- server/src/printers/NativeMakefilePrinter.cpp | 12 +- server/src/printers/TestMakefilesPrinter.cpp | 4 +- server/src/streams/FileTargetsWriter.h | 2 +- server/src/streams/ProjectTargetsWriter.cpp | 4 +- server/src/streams/ProjectTargetsWriter.h | 2 +- server/src/stubs/StubGen.cpp | 2 +- server/src/stubs/StubSourcesFinder.cpp | 2 +- server/src/stubs/StubSourcesFinder.h | 6 +- server/src/testgens/BaseTestGen.cpp | 53 +++- server/src/testgens/BaseTestGen.h | 20 +- server/src/testgens/ProjectTestGen.cpp | 12 +- server/src/testgens/SnippetTestGen.cpp | 4 +- server/src/utils/GenerationUtils.cpp | 2 +- server/src/utils/GenerationUtils.h | 2 +- server/test/framework/KleeGen_Tests.cpp | 4 +- server/test/framework/Server_Tests.cpp | 2 +- 28 files changed, 534 insertions(+), 403 deletions(-) diff --git a/server/src/KleeGenerator.cpp b/server/src/KleeGenerator.cpp index 1e1f7c9ec..f04323052 100644 --- a/server/src/KleeGenerator.cpp +++ b/server/src/KleeGenerator.cpp @@ -19,13 +19,13 @@ using namespace tests; static const std::string GENERATION_COMPILE_MAKEFILE = "GenerationCompileMakefile.mk"; static const std::string GENERATION_KLEE_MAKEFILE = "GenerationKleeMakefile.mk"; -KleeGenerator::KleeGenerator(BaseTestGen &_testGen, types::TypesHandler &typesHandler, +KleeGenerator::KleeGenerator(BaseTestGen *_testGen, types::TypesHandler &typesHandler, PathSubstitution filePathsSubstitution) : testGen(_testGen), typesHandler(typesHandler), pathSubstitution(std::move(filePathsSubstitution)) { try { - fs::create_directories(this->testGen.serverBuildDir); - fs::create_directories(Paths::getLogDir(this->testGen.projectContext.projectName)); + fs::create_directories(this->testGen->serverBuildDir); + fs::create_directories(Paths::getLogDir(this->testGen->projectContext.projectName)); } catch (const fs::filesystem_error &e) { throw FileSystemException("create_directories failed", e); } @@ -39,34 +39,34 @@ KleeGenerator::buildByCDb(const CollectionUtils::MapFileTo &filesToBui printer::DefaultMakefilePrinter makefilePrinter; std::vector outfilePaths; - for (const auto &compileCommand : compileCommands) { + for (const auto &compileCommand: compileCommands) { fs::path output = compileCommand.getOutput(); outfilePaths.emplace_back(output); utbot::CompileCommand compileCommandWithChangingDirectory{compileCommand, true}; - makefilePrinter.declareTarget(output, { compileCommandWithChangingDirectory.getSourcePath() }, - { compileCommandWithChangingDirectory.toStringWithChangingDirectory() }); + makefilePrinter.declareTarget(output, {compileCommandWithChangingDirectory.getSourcePath()}, + {compileCommandWithChangingDirectory.toStringWithChangingDirectory()}); } makefilePrinter.declareTarget(printer::DefaultMakefilePrinter::TARGET_ALL, outfilePaths, {}); - const fs::path makefile = testGen.serverBuildDir / GENERATION_COMPILE_MAKEFILE; + const fs::path makefile = testGen->serverBuildDir / GENERATION_COMPILE_MAKEFILE; FileSystemUtils::writeToFile(makefile, makefilePrinter.ss.str()); - auto command = MakefileUtils::MakefileCommand(testGen.projectContext, makefile, + auto command = MakefileUtils::MakefileCommand(testGen->projectContext, makefile, printer::DefaultMakefilePrinter::TARGET_ALL); ExecUtils::ExecutionResult res = command.run(); if (res.status != 0) { LOG_S(ERROR) << StringUtils::stringFormat("Make for \"%s\" failed.\nCommand: \"%s\"\n%s\n", makefile, command.getFailedCommand(), res.output); throw ExecutionProcessException( - command.getFailedCommand(), - res.outPath.value() + command.getFailedCommand(), + res.outPath.value() ); } auto outFiles = CollectionUtils::transform( - compileCommands, [](utbot::CompileCommand const &compileCommand) { - return BuildFileInfo{ compileCommand.getOutput(), compileCommand.getSourcePath() }; - }); + compileCommands, [](utbot::CompileCommand const &compileCommand) { + return BuildFileInfo{compileCommand.getOutput(), compileCommand.getSourcePath()}; + }); return outFiles; } @@ -74,8 +74,8 @@ std::vector KleeGenerator::buildByCDb(const CollectionUtils::FileSet &filesToBuild, const CollectionUtils::FileSet &stubSources) { CollectionUtils::MapFileTo filesMap; - for (fs::path const &file : filesToBuild) { - filesMap[file] = testGen.getBuildDatabase(false)->getBitcodeFile(file); + for (fs::path const &file: filesToBuild) { + filesMap[file] = testGen->getTargetBuildDatabase()->getBitcodeFile(file); } return buildByCDb(filesMap, stubSources); } @@ -83,39 +83,39 @@ KleeGenerator::buildByCDb(const CollectionUtils::FileSet &filesToBuild, static std::string getUTBotClangCompilerPath(fs::path clientCompilerPath) { auto compilerName = CompilationUtils::getCompilerName(clientCompilerPath); switch (compilerName) { - case CompilationUtils::CompilerName::GCC: - return Paths::getUTBotClang(); - case CompilationUtils::CompilerName::GXX: - return Paths::getUTBotClangPP(); - case CompilationUtils::CompilerName::CLANG: - return Paths::getUTBotClang(); - case CompilationUtils::CompilerName::CLANGXX: - return Paths::getUTBotClangPP(); - default: - return clientCompilerPath; + case CompilationUtils::CompilerName::GCC: + return Paths::getUTBotClang(); + case CompilationUtils::CompilerName::GXX: + return Paths::getUTBotClangPP(); + case CompilationUtils::CompilerName::CLANG: + return Paths::getUTBotClang(); + case CompilationUtils::CompilerName::CLANGXX: + return Paths::getUTBotClangPP(); + default: + return clientCompilerPath; } } static const std::unordered_set UNSUPPORTED_FLAGS_AND_OPTIONS_KLEE = { - "--coverage", - "-fbranch-target-load-optimize", - "-fcx-fortran-rules", - "-fipa-cp-clone", - "-fipa-cp-cloneclang-10", - "-fira-loop-pressure", - "-fno-forward-propagate", - "-fno-if-conversion", - "-fno-sched-interblock", - "-fno-sched-spec-insn-heuristic", - "-fno-tree-dominator-opts", - "-fno-tree-sink", - "-fno-tree-sinkclang-10", - "-fpredictive-commoning", - "-fprofile-dir", - "-freschedule-modulo-scheduled-loops", - "-fsched2-use-superblocks", - "-fsel-sched-reschedule-pipelined", - "-ftree-loop-distribute-patterns", + "--coverage", + "-fbranch-target-load-optimize", + "-fcx-fortran-rules", + "-fipa-cp-clone", + "-fipa-cp-cloneclang-10", + "-fira-loop-pressure", + "-fno-forward-propagate", + "-fno-if-conversion", + "-fno-sched-interblock", + "-fno-sched-spec-insn-heuristic", + "-fno-tree-dominator-opts", + "-fno-tree-sink", + "-fno-tree-sinkclang-10", + "-fpredictive-commoning", + "-fprofile-dir", + "-freschedule-modulo-scheduled-loops", + "-fsched2-use-superblocks", + "-fsel-sched-reschedule-pipelined", + "-ftree-loop-distribute-patterns", }; std::optional @@ -123,8 +123,7 @@ KleeGenerator::getCompileCommandForKlee(const fs::path &hintPath, const CollectionUtils::FileSet &stubSources, const std::vector &flags, bool forStub) const { - - auto compilationUnitInfo = testGen.getBuildDatabase(forStub)->getClientCompilationUnitInfo(hintPath); + auto compilationUnitInfo = testGen->getClientCompilationUnitInfo(hintPath, forStub); auto command = compilationUnitInfo->command; auto srcFilePath = compilationUnitInfo->getSourcePath(); std::string newCompilerPath = getUTBotClangCompilerPath(command.getBuildTool()); @@ -132,32 +131,33 @@ KleeGenerator::getCompileCommandForKlee(const fs::path &hintPath, srcFilePath = pathSubstitution.substituteLineFlag(srcFilePath); if (CollectionUtils::contains(stubSources, srcFilePath)) { - srcFilePath = Paths::sourcePathToStubPath(testGen.projectContext, srcFilePath); + srcFilePath = Paths::sourcePathToStubPath(testGen->projectContext, srcFilePath); } command.setSourcePath(srcFilePath); - auto outFilePath = testGen.getBuildDatabase(forStub)->getBitcodeFile(compilationUnitInfo->getOutputFile()); + auto outFilePath = (forStub ? testGen->getProjectBuildDatabase()->getBitcodeFile(compilationUnitInfo->getOutputFile()) + : testGen->getTargetBuildDatabase()->getBitcodeFile(compilationUnitInfo->getOutputFile())); fs::create_directories(outFilePath.parent_path()); command.setOutput(outFilePath); command.setOptimizationLevel("-O0"); command.removeCompilerFlagsAndOptions(UNSUPPORTED_FLAGS_AND_OPTIONS_KLEE); - std::vector extraFlags{ "-emit-llvm", - "-c", - "-Xclang", - "-disable-O0-optnone", - "-g", - "-fstandalone-debug", - "-fno-discard-value-names", - "-fno-elide-constructors", - "-D" + PrinterUtils::KLEE_MODE + "=1", - SanitizerUtils::CLANG_SANITIZER_CHECKS_FLAG }; + std::vector extraFlags{"-emit-llvm", + "-c", + "-Xclang", + "-disable-O0-optnone", + "-g", + "-fstandalone-debug", + "-fno-discard-value-names", + "-fno-elide-constructors", + "-D" + PrinterUtils::KLEE_MODE + "=1", + SanitizerUtils::CLANG_SANITIZER_CHECKS_FLAG}; if (Paths::isCXXFile(srcFilePath)) { command.addFlagToBegin(CompilationUtils::getIncludePath(Paths::getAccessPrivateLibPath())); } command.addFlagsToBegin(flags); command.addFlagsToBegin(extraFlags); command.addFlagToBegin( - StringUtils::stringFormat("-iquote%s", compilationUnitInfo->getSourcePath().parent_path())); + StringUtils::stringFormat("-iquote%s", compilationUnitInfo->getSourcePath().parent_path())); LOG_S(MAX) << "New compile command with klee required flags: " << command.toString(); return command; } @@ -167,7 +167,7 @@ KleeGenerator::getCompileCommandsForKlee(const CollectionUtils::MapFileTo compileCommands; compileCommands.reserve(filesToBuild.size()); - for (const auto &[fileToBuild, bitcode] : filesToBuild) { + for (const auto &[fileToBuild, bitcode]: filesToBuild) { auto optionalCommand = getCompileCommandForKlee(fileToBuild, stubSources, {}, false); if (optionalCommand.has_value()) { auto command = std::move(optionalCommand).value(); @@ -184,13 +184,13 @@ Result KleeGenerator::defaultBuild(const fs::path &hintPath, const fs::path &buildDirPath, const std::vector &flags) { LOG_SCOPE_FUNCTION(DEBUG); - auto bitcodeFilePath = testGen.getBuildDatabase(false)->getBitcodeFile(sourceFilePath); + auto bitcodeFilePath = testGen->getTargetBuildDatabase()->getBitcodeFile(sourceFilePath); auto optionalCommand = getCompileCommandForKlee(hintPath, {}, flags, false); if (!optionalCommand.has_value()) { std::string message = StringUtils::stringFormat( - "Couldn't get command for klee file: %s\n" - "Please check if directory is in source directories in UTBot extension settings: %s", - sourceFilePath, hintPath.parent_path().string()); + "Couldn't get command for klee file: %s\n" + "Please check if directory is in source directories in UTBot extension settings: %s", + sourceFilePath, hintPath.parent_path().string()); throw BaseException(std::move(message)); } auto &command = optionalCommand.value(); @@ -202,10 +202,10 @@ Result KleeGenerator::defaultBuild(const fs::path &hintPath, makefilePrinter.declareTarget(printer::DefaultMakefilePrinter::TARGET_BUILD, {commandWithChangingDirectory.getSourcePath()}, {commandWithChangingDirectory.toStringWithChangingDirectory()}); - fs::path makefile = testGen.serverBuildDir / GENERATION_KLEE_MAKEFILE; + fs::path makefile = testGen->serverBuildDir / GENERATION_KLEE_MAKEFILE; FileSystemUtils::writeToFile(makefile, makefilePrinter.ss.str()); - auto makefileCommand = MakefileUtils::MakefileCommand(testGen.projectContext, makefile, + auto makefileCommand = MakefileUtils::MakefileCommand(testGen->projectContext, makefile, printer::DefaultMakefilePrinter::TARGET_BUILD); auto[out, status, _] = makefileCommand.run(); if (status != 0) { @@ -226,103 +226,104 @@ Result KleeGenerator::defaultBuild(const fs::path &sourceFilePath, fs::path KleeGenerator::writeKleeFile( - printer::KleePrinter &kleePrinter, - Tests const &tests, - const std::shared_ptr &lineInfo, - const std::function &methodFilter) { + printer::KleePrinter &kleePrinter, + Tests const &tests, + const std::shared_ptr &lineInfo, + const std::function &methodFilter) { if (lineInfo) { return kleePrinter.writeTmpKleeFile( - tests, testGen.serverBuildDir, pathSubstitution, lineInfo->predicateInfo, lineInfo->methodName, - lineInfo->scopeName, lineInfo->forMethod, lineInfo->forClass, methodFilter); + tests, testGen->serverBuildDir, pathSubstitution, lineInfo->predicateInfo, lineInfo->methodName, + lineInfo->scopeName, lineInfo->forMethod, lineInfo->forClass, methodFilter); } else { - return kleePrinter.writeTmpKleeFile(tests, testGen.serverBuildDir, pathSubstitution, std::nullopt, + return kleePrinter.writeTmpKleeFile(tests, testGen->serverBuildDir, pathSubstitution, std::nullopt, "", "", false, false, methodFilter); } } std::vector KleeGenerator::buildKleeFiles(const tests::TestsMap &testsMap, - const std::shared_ptr &lineInfo) { + const std::shared_ptr &lineInfo) { std::vector outFiles; LOG_S(DEBUG) << "Building generated klee files..."; - printer::KleePrinter kleePrinter(&typesHandler, testGen.getBuildDatabase(false), utbot::Language::UNKNOWN); + printer::KleePrinter kleePrinter(&typesHandler, testGen->getTargetBuildDatabase(), utbot::Language::UNKNOWN); ExecUtils::doWorkWithProgress( - testsMap, testGen.progressWriter, "Building generated klee files", - [&](auto const &it) { - const auto &[filename, tests] = it; - if (lineInfo != nullptr && filename != lineInfo->filePath) { - return; - } - kleePrinter.srcLanguage = Paths::getSourceLanguage(filename); - std::vector includeFlags = { - CompilationUtils::getIncludePath(Paths::getFlagsDir(testGen.projectContext))}; - auto buildDirPath = - testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(filename)->getDirectory(); - - fs::path kleeFilePath = writeKleeFile(kleePrinter, tests, lineInfo); - auto kleeFilesInfo = - testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(tests.sourceFilePath)->kleeFilesInfo; - auto kleeBitcodeFile = defaultBuild(filename, kleeFilePath, buildDirPath, includeFlags); - if (kleeBitcodeFile.isSuccess()) { - outFiles.emplace_back(kleeBitcodeFile.getOpt().value()); - kleeFilesInfo->setAllAreCorrect(true); - LOG_S(MAX) << "Klee filepath: " << outFiles.back(); - } else { - if (lineInfo) { - throw BaseException("Couldn't compile klee file for current line."); + testsMap, testGen->progressWriter, "Building generated klee files", + [&](auto const &it) { + const auto &[filename, tests] = it; + if (lineInfo != nullptr && filename != lineInfo->filePath) { + return; } - auto tempKleeFilePath = Paths::addSuffix(kleeFilePath, "_temp"); - fs::copy(kleeFilePath, tempKleeFilePath, fs::copy_options::overwrite_existing); - LOG_S(DEBUG) + kleePrinter.srcLanguage = Paths::getSourceLanguage(filename); + std::vector includeFlags = { + CompilationUtils::getIncludePath(Paths::getFlagsDir(testGen->projectContext))}; + auto buildDirPath = + testGen->getClientCompilationUnitInfo(filename)->getDirectory(); + + fs::path kleeFilePath = writeKleeFile(kleePrinter, tests, lineInfo); + auto kleeFilesInfo = + testGen->getClientCompilationUnitInfo( + tests.sourceFilePath)->kleeFilesInfo; + auto kleeBitcodeFile = defaultBuild(filename, kleeFilePath, buildDirPath, includeFlags); + if (kleeBitcodeFile.isSuccess()) { + outFiles.emplace_back(kleeBitcodeFile.getOpt().value()); + kleeFilesInfo->setAllAreCorrect(true); + LOG_S(MAX) << "Klee filepath: " << outFiles.back(); + } else { + if (lineInfo) { + throw BaseException("Couldn't compile klee file for current line."); + } + auto tempKleeFilePath = Paths::addSuffix(kleeFilePath, "_temp"); + fs::copy(kleeFilePath, tempKleeFilePath, fs::copy_options::overwrite_existing); + LOG_S(DEBUG) << "File " << kleeFilePath << " couldn't be compiled so it's copy is backed up in " << tempKleeFilePath << ". Proceeding with generating klee file containing restricted number " "of functions"; - std::unordered_set correctMethods; - for (const auto &[methodName, methodDescription] : tests.methods) { - fs::path currentKleeFilePath = kleePrinter.writeTmpKleeFile( - tests, testGen.serverBuildDir, pathSubstitution, std::nullopt, - methodDescription.name, - methodDescription.getClassName(), - true, false); - auto currentKleeBitcodeFile = - defaultBuild(filename, currentKleeFilePath, buildDirPath, includeFlags); - if (currentKleeBitcodeFile.isSuccess()) { - correctMethods.insert(methodDescription.name); + std::unordered_set correctMethods; + for (const auto &[methodName, methodDescription]: tests.methods) { + fs::path currentKleeFilePath = kleePrinter.writeTmpKleeFile( + tests, testGen->serverBuildDir, pathSubstitution, std::nullopt, + methodDescription.name, + methodDescription.getClassName(), + true, false); + auto currentKleeBitcodeFile = + defaultBuild(filename, currentKleeFilePath, buildDirPath, includeFlags); + if (currentKleeBitcodeFile.isSuccess()) { + correctMethods.insert(methodDescription.name); + } else { + std::stringstream message; + message << "Function '" << methodName + << "' was skipped, as there was an error in compilation klee file " + "for it"; + LOG_S(WARNING) << message.str(); + failedFunctions[filename].emplace_back(message.str()); + } + } + kleeFilesInfo->setCorrectMethods(std::move(correctMethods)); + + auto kleeFilePath = writeKleeFile( + kleePrinter, tests, lineInfo, + [&kleeFilesInfo](tests::Tests::MethodDescription const &method) -> bool { + return kleeFilesInfo->isCorrectMethod(method.name); + }); + auto kleeBitcodeFile = + defaultBuild(filename, kleeFilePath, buildDirPath, includeFlags); + if (kleeBitcodeFile.isSuccess()) { + outFiles.emplace_back(kleeBitcodeFile.getOpt().value()); } else { - std::stringstream message; - message << "Function '" << methodName - << "' was skipped, as there was an error in compilation klee file " - "for it"; - LOG_S(WARNING) << message.str(); - failedFunctions[filename].emplace_back(message.str()); + throw BaseException("Couldn't compile klee file from correct methods."); } } - kleeFilesInfo->setCorrectMethods(std::move(correctMethods)); - - auto kleeFilePath = writeKleeFile( - kleePrinter, tests, lineInfo, - [&kleeFilesInfo](tests::Tests::MethodDescription const &method) -> bool { - return kleeFilesInfo->isCorrectMethod(method.name); - }); - auto kleeBitcodeFile = - defaultBuild(filename, kleeFilePath, buildDirPath, includeFlags); - if (kleeBitcodeFile.isSuccess()) { - outFiles.emplace_back(kleeBitcodeFile.getOpt().value()); - } else { - throw BaseException("Couldn't compile klee file from correct methods."); - } - } - }); + }); return outFiles; } void KleeGenerator::parseKTestsToFinalCode( - tests::Tests &tests, - const std::unordered_map &methodNameToReturnTypeMap, - const std::vector &kleeOutput, - const std::shared_ptr &lineInfo, - bool verbose) { - for (const auto &batch : kleeOutput) { + tests::Tests &tests, + const std::unordered_map &methodNameToReturnTypeMap, + const std::vector &kleeOutput, + const std::shared_ptr &lineInfo, + bool verbose) { + for (const auto &batch: kleeOutput) { bool filterByFlag = (lineInfo != nullptr && !lineInfo->forMethod && !lineInfo->forClass && !lineInfo->predicateInfo.has_value()); tests::KTestObjectParser KTestObjectParser(typesHandler); @@ -345,24 +346,25 @@ void KleeGenerator::parseKTestsToFinalCode( continue; } auto predicate = - lineInfo ? lineInfo->predicateInfo : std::optional{}; + lineInfo ? lineInfo->predicateInfo : std::optional{}; testsPrinter.genCode(methodDescription, predicate, verbose); } - printer::HeaderPrinter(Paths::getSourceLanguage(tests.sourceFilePath)).print(tests.testHeaderFilePath, tests.sourceFilePath, - tests.headerCode); + printer::HeaderPrinter(Paths::getSourceLanguage(tests.sourceFilePath)).print(tests.testHeaderFilePath, + tests.sourceFilePath, + tests.headerCode); testsPrinter.joinToFinalCode(tests, tests.testHeaderFilePath); LOG_S(DEBUG) << "Generated code for " << tests.methods.size() << " tests"; } fs::path KleeGenerator::getBitcodeFile(const fs::path &sourcePath) const { - return testGen.getBuildDatabase(false)->getBitcodeFile(sourcePath); + return testGen->getTargetBuildDatabase()->getBitcodeFile(sourcePath); } void KleeGenerator::handleFailedFunctions(tests::TestsMap &testsMap) { - for (auto &[fileName, tests] : failedFunctions) { - for (const auto &commentBlock : tests) { + for (auto &[fileName, tests]: failedFunctions) { + for (const auto &commentBlock: tests) { testsMap[fileName].commentBlocks.emplace_back(commentBlock); } } diff --git a/server/src/KleeGenerator.h b/server/src/KleeGenerator.h index ab133ccb3..db9af7440 100644 --- a/server/src/KleeGenerator.h +++ b/server/src/KleeGenerator.h @@ -43,7 +43,7 @@ class KleeGenerator { * @throws fs::filesystem_error Thrown if it can't create tmp folder for some * reasons. */ - KleeGenerator(BaseTestGen &_testGen, types::TypesHandler &typesHandler, + KleeGenerator(BaseTestGen *_testGen, types::TypesHandler &typesHandler, PathSubstitution filePathsSubstitution); struct BuildFileInfo { @@ -144,17 +144,17 @@ class KleeGenerator { const CollectionUtils::FileSet &stubSources) const; private: - BaseTestGen &testGen; + BaseTestGen *testGen; types::TypesHandler typesHandler; PathSubstitution pathSubstitution; CollectionUtils::MapFileTo> failedFunctions; fs::path writeKleeFile( - printer::KleePrinter &kleePrinter, - Tests const &tests, - const std::shared_ptr &lineInfo, - const std::function &methodFilter = + printer::KleePrinter &kleePrinter, + Tests const &tests, + const std::shared_ptr &lineInfo, + const std::function &methodFilter = [](tests::Tests::MethodDescription const &) { return true; }); }; diff --git a/server/src/ReturnTypesFetcher.cpp b/server/src/ReturnTypesFetcher.cpp index 1b57342f4..9fa4dde67 100644 --- a/server/src/ReturnTypesFetcher.cpp +++ b/server/src/ReturnTypesFetcher.cpp @@ -10,7 +10,7 @@ void ReturnTypesFetcher::fetch(ProgressWriter *const progressWriter, testsMap[filePath]; } Fetcher(Fetcher::Options::Value::RETURN_TYPE_NAMES_ONLY, - testGen->getBuildDatabase(false)->compilationDatabase, testsMap, nullptr, nullptr, nullptr, + testGen->getTargetBuildDatabase()->compilationDatabase, testsMap, nullptr, nullptr, nullptr, testGen->compileCommandsJsonPath, false) .fetchWithProgress(progressWriter, "Fetching return types for functions", true); for (auto const &[sourceFilePath, test] : testsMap) { diff --git a/server/src/Server.cpp b/server/src/Server.cpp index 1bf550f85..ea820385e 100644 --- a/server/src/Server.cpp +++ b/server/src/Server.cpp @@ -32,6 +32,7 @@ #include "utils/stats/TestsGenerationStats.h" #include "utils/stats/TestsExecutionStats.h" #include "utils/TypeUtils.h" +#include "building/ProjectBuildDatabase.h" #include #include @@ -147,7 +148,7 @@ Status Server::TestsGenServiceImpl::GenerateLineTests(ServerContext *context, } Status Server::TestsGenServiceImpl::GenerateAssertionFailTests( - ServerContext *context, const AssertionRequest *request, ServerWriter *writer) { + ServerContext *context, const AssertionRequest *request, ServerWriter *writer) { return BaseTestGenerate(context, *request, writer); } @@ -166,9 +167,9 @@ Status Server::TestsGenServiceImpl::Handshake(ServerContext *context, } Status Server::TestsGenServiceImpl::CreateTestsCoverageAndResult( - ServerContext *context, - const CoverageAndResultsRequest *request, - ServerWriter *writer) { + ServerContext *context, + const CoverageAndResultsRequest *request, + ServerWriter *writer) { LOG_S(INFO) << "CreateTestsCoverageAndResult receive:\n" << request->DebugString(); auto coverageAndResultsWriter = std::make_unique(writer); @@ -198,14 +199,14 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, static std::string logMessage = "Traversing sources AST tree and fetching declarations."; LOG_S(DEBUG) << logMessage; Fetcher fetcher(Fetcher::Options::Value::ALL, - testGen.getBuildDatabase(false)->compilationDatabase, testGen.tests, &testGen.types, + testGen.getTargetBuildDatabase()->compilationDatabase, testGen.tests, &testGen.types, &sizeContext.pointerSize, &sizeContext.maximumAlignment, testGen.compileCommandsJsonPath, false); fetcher.fetchWithProgress(testGen.progressWriter, logMessage); - SourceToHeaderRewriter(testGen.projectContext, testGen.getBuildDatabase(false)->compilationDatabase, + SourceToHeaderRewriter(testGen.projectContext, testGen.getTargetBuildDatabase()->compilationDatabase, fetcher.getStructsToDeclare(), testGen.serverBuildDir) - .generateTestHeaders(testGen.tests, testGen.progressWriter); - types::TypesHandler typesHandler{ testGen.types, sizeContext }; + .generateTestHeaders(testGen.tests, testGen.progressWriter); + types::TypesHandler typesHandler{testGen.types, sizeContext}; testGen.progressWriter->writeProgress("Generating stub files", 0.0); StubGen stubGen(testGen); @@ -218,7 +219,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, if (lineTestGen != nullptr) { if (isSameType(testGen) && Paths::isHeaderFile(lineTestGen->filePath)) { BordersFinder classFinder(lineTestGen->filePath, lineTestGen->line, - testGen.getBuildDatabase(false)->compilationDatabase, + testGen.getTargetBuildDatabase()->compilationDatabase, lineTestGen->compileCommandsJsonPath); classFinder.findClass(); lineInfo = std::make_shared(classFinder.getLineInfo()); @@ -248,32 +249,34 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, if (lineTestGen->needToAddPathFlag()) { LOG_S(DEBUG) << "Added test line flag for file " << lineInfo->filePath; fs::path flagFilePath = - printer::KleePrinter(&typesHandler, nullptr, Paths::getSourceLanguage(lineInfo->filePath)) - .addTestLineFlag(lineInfo, lineInfo->forAssert, testGen.projectContext); - pathSubstitution = { lineTestGen->filePath, flagFilePath }; + printer::KleePrinter(&typesHandler, nullptr, Paths::getSourceLanguage(lineInfo->filePath)) + .addTestLineFlag(lineInfo, lineInfo->forAssert, testGen.projectContext); + pathSubstitution = {lineTestGen->filePath, flagFilePath}; } } - auto generator = std::make_shared(testGen, typesHandler, pathSubstitution); + auto generator = std::make_shared(&testGen, typesHandler, pathSubstitution); - ReturnTypesFetcher returnTypesFetcher{ &testGen }; + ReturnTypesFetcher returnTypesFetcher{&testGen}; returnTypesFetcher.fetch(testGen.progressWriter, synchronizer.getSourceFiles()); LOG_S(DEBUG) << "Temporary build directory path: " << testGen.serverBuildDir; generator->buildKleeFiles(testGen.tests, lineInfo); generator->handleFailedFunctions(testGen.tests); testGen.progressWriter->writeProgress("Building files", 0.0); - Linker linker{ testGen, stubGen, lineInfo, generator }; + Linker linker{testGen, stubGen, lineInfo, generator}; linker.prepareArtifacts(); auto testMethods = linker.getTestMethods(); - KleeRunner kleeRunner{ testGen.projectContext, testGen.settingsContext, - testGen.serverBuildDir }; + KleeRunner kleeRunner{testGen.projectContext, testGen.settingsContext, + testGen.serverBuildDir}; bool interactiveMode = (dynamic_cast(&testGen) != nullptr); auto generationStartTime = std::chrono::steady_clock::now(); StatsUtils::TestsGenerationStatsFileMap generationStatsMap(testGen.projectContext, - std::chrono::duration_cast(generationStartTime - preprocessingStartTime)); + std::chrono::duration_cast( + generationStartTime - + preprocessingStartTime)); kleeRunner.runKlee(testMethods, testGen.tests, generator, testGen.methodNameToReturnTypeMap, lineInfo, testsWriter, testGen.isBatched(), interactiveMode, generationStatsMap); LOG_S(INFO) << "KLEE time: " << std::chrono::duration_cast - (generationStatsMap.getTotal().kleeStats.getKleeTime()).count() << " ms\n"; + (generationStatsMap.getTotal().kleeStats.getKleeTime()).count() << " ms\n"; printer::CSVPrinter printer = generationStatsMap.toCSV(); FileSystemUtils::writeToFile(Paths::getGenerationStatsCSVPath(testGen.projectContext), printer.getStream().str()); @@ -283,7 +286,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, std::string command = e.what(); return Status(StatusCode::FAILED_PRECONDITION, "Executing command\n" + command.substr(0, 100) + - "...\nfailed. See more info in console logs."); + "...\nfailed. See more info in console logs."); } catch (const NoTestGeneratedException &e) { return Status(StatusCode::FAILED_PRECONDITION, e.what()); } catch (const CancellationException &e) { @@ -308,12 +311,12 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, std::shared_ptr Server::TestsGenServiceImpl::getLineInfo(LineTestGen &lineTestGen) { BordersFinder stmtFinder(lineTestGen.filePath, lineTestGen.line, - lineTestGen.getBuildDatabase(false)->compilationDatabase, + lineTestGen.getTargetBuildDatabase()->compilationDatabase, lineTestGen.compileCommandsJsonPath); stmtFinder.findFunction(); if (!stmtFinder.getLineInfo().initialized) { throw NoTestGeneratedException( - "Maybe you tried to generate tests placing cursor on invalid line."); + "Maybe you tried to generate tests placing cursor on invalid line."); } if (isSameType(lineTestGen) && !StringUtils::contains(stmtFinder.getLineInfo().stmtString, "assert")) { @@ -322,12 +325,13 @@ std::shared_ptr Server::TestsGenServiceImpl::getLineInfo(LineTestGen & auto lineInfo = std::make_shared(stmtFinder.getLineInfo()); if (auto predicateInfo = dynamic_cast(&lineTestGen)) { lineInfo->predicateInfo = LineInfo::PredicateInfo( - { predicateInfo->type, predicateInfo->predicate, predicateInfo->returnValue }); + {predicateInfo->type, predicateInfo->predicate, predicateInfo->returnValue}); } auto &methods = lineTestGen.tests.at(lineInfo->filePath).methods; CollectionUtils::erase_if(methods, [&lineInfo](auto const &method) { return (lineInfo->forMethod && method.name != lineInfo->methodName) || - (lineInfo->forClass && method.isClassMethod() && method.classObj->type.typeName() != lineInfo->scopeName); + (lineInfo->forClass && method.isClassMethod() && + method.classObj->type.typeName() != lineInfo->scopeName); }); return lineInfo; } @@ -371,7 +375,7 @@ void Server::gtestLog(void *channel, const loguru::Message &message) { } } -loguru::Verbosity MaxNameToVerbosityCallback(const char* name) { +loguru::Verbosity MaxNameToVerbosityCallback(const char *name) { if (strcmp(name, "TestLogLevel") == 0) { return loguru::Verbosity_INFO; } else if (strcmp(name, "ServerLogLevel") == 0) { @@ -381,17 +385,17 @@ loguru::Verbosity MaxNameToVerbosityCallback(const char* name) { } Status Server::TestsGenServiceImpl::provideLoggingCallbacks( - const std::string &callbackPrefix, - ServerWriter *writer, - const std::string &logLevel, - loguru::log_handler_t handler, - std::map &channelStorage, - bool openFiles) { + const std::string &callbackPrefix, + ServerWriter *writer, + const std::string &logLevel, + loguru::log_handler_t handler, + std::map &channelStorage, + bool openFiles) { const auto &client = RequestEnvironment::getClientId(); auto oldValue = channelStorage[client].load(std::memory_order_relaxed); if (!oldValue && channelStorage[client].compare_exchange_weak( - oldValue, true, std::memory_order_release, std::memory_order_relaxed)) { - WriterData data{ writer, std::mutex(), client }; + oldValue, true, std::memory_order_release, std::memory_order_relaxed)) { + WriterData data{writer, std::mutex(), client}; fs::path logFilePath = Paths::getLogDir(); if (!fs::exists(logFilePath)) { fs::create_directories(logFilePath); @@ -483,7 +487,7 @@ Status Server::TestsGenServiceImpl::Heartbeat(ServerContext *context, Status Server::TestsGenServiceImpl::RegisterClient(ServerContext *context, const RegisterClientRequest *request, DummyResponse *response) { - const std::string& name = request->clientid(); + const std::string &name = request->clientid(); ServerUtils::registerClient(clients, name); return Status::OK; } @@ -525,7 +529,7 @@ Status Server::TestsGenServiceImpl::GenerateProjectStubs(ServerContext *context, LOG_S(INFO) << "GenerateProjectStubs receive:\n" << request->DebugString(); auto stubsWriter = - std::make_unique(writer, GrpcUtils::synchronizeCode(*request)); + std::make_unique(writer, GrpcUtils::synchronizeCode(*request)); ServerUtils::setThreadOptions(context, testMode); auto lock = acquireLock(stubsWriter.get()); @@ -542,13 +546,13 @@ Status Server::TestsGenServiceImpl::GenerateProjectStubs(ServerContext *context, Status Server::TestsGenServiceImpl::ProcessProjectStubsRequest(BaseTestGen *testGen, StubsWriter *stubsWriter) { types::TypesHandler::SizeContext sizeContext; - types::TypesHandler typesHandler{ testGen->types, sizeContext }; + types::TypesHandler typesHandler{testGen->types, sizeContext}; StubGen stubGen(*testGen); static std::string logMessage = "Traversing sources AST tree and fetching declarations."; LOG_S(DEBUG) << logMessage; Fetcher fetcher(Fetcher::Options::Value::TYPE | Fetcher::Options::Value::FUNCTION, - testGen->getBuildDatabase(false)->compilationDatabase, testGen->tests, &testGen->types, + testGen->getTargetBuildDatabase()->compilationDatabase, testGen->tests, &testGen->types, &sizeContext.pointerSize, &sizeContext.maximumAlignment, testGen->compileCommandsJsonPath, false); @@ -574,9 +578,9 @@ Status Server::TestsGenServiceImpl::PrintModulesContent(ServerContext *context, MEASURE_FUNCTION_EXECUTION_TIME - utbot::ProjectContext projectContext{ *request }; + utbot::ProjectContext projectContext{*request}; fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); - std::shared_ptr buildDatabase = BuildDatabase::create(projectContext); + std::shared_ptr buildDatabase = ProjectBuildDatabase::create(projectContext); StubSourcesFinder(buildDatabase).printAllModules(); return Status::OK; } @@ -592,12 +596,12 @@ Status Server::TestsGenServiceImpl::GetSourceCode(ServerContext *context, MEASURE_FUNCTION_EXECUTION_TIME const std::string &filePath = request->filepath(); - std::ifstream stream{ filePath }; + std::ifstream stream{filePath}; if (!stream) { return Status(StatusCode::INVALID_ARGUMENT, "Failed to find file:\n" + filePath); } auto code = std::make_unique(std::istreambuf_iterator(stream), - std::istreambuf_iterator()); + std::istreambuf_iterator()); response->set_allocated_code(code.release()); return Status::OK; } @@ -607,34 +611,34 @@ Server::TestsGenServiceImpl::ConfigureProject(ServerContext *context, const ProjectConfigRequest *request, ServerWriter *response) { LOG_S(INFO) << "CheckProjectConfiguration receive:\n" << request->DebugString(); - ProjectConfigWriter writer{ response }; + ProjectConfigWriter writer{response}; ServerUtils::setThreadOptions(context, testMode); auto lock = acquireLock(&writer); MEASURE_FUNCTION_EXECUTION_TIME - utbot::ProjectContext utbotProjectContext{ request->projectcontext() }; + utbot::ProjectContext utbotProjectContext{request->projectcontext()}; fs::path buildDirPath = - fs::path(utbotProjectContext.projectPath) / utbotProjectContext.buildDirRelativePath; + fs::path(utbotProjectContext.projectPath) / utbotProjectContext.buildDirRelativePath; switch (request->configmode()) { - case ConfigMode::CHECK: - return UserProjectConfiguration::CheckProjectConfiguration(buildDirPath, writer); - case ConfigMode::CREATE_BUILD_DIR: - return UserProjectConfiguration::RunBuildDirectoryCreation(buildDirPath, writer); - case ConfigMode::GENERATE_JSON_FILES: { - std::vector cmakeOptions(request->cmakeoptions().begin(), request->cmakeoptions().end()); - return UserProjectConfiguration::RunProjectConfigurationCommands( - buildDirPath, utbotProjectContext, cmakeOptions, writer); - } - case ConfigMode::ALL: { - std::vector cmakeOptions(request->cmakeoptions().begin(), request->cmakeoptions().end()); - return UserProjectConfiguration::RunProjectReConfigurationCommands( - buildDirPath, fs::path(utbotProjectContext.projectPath), - utbotProjectContext, cmakeOptions, writer); - } - default: - return {StatusCode::CANCELLED, "Invalid request type."}; + case ConfigMode::CHECK: + return UserProjectConfiguration::CheckProjectConfiguration(buildDirPath, writer); + case ConfigMode::CREATE_BUILD_DIR: + return UserProjectConfiguration::RunBuildDirectoryCreation(buildDirPath, writer); + case ConfigMode::GENERATE_JSON_FILES: { + std::vector cmakeOptions(request->cmakeoptions().begin(), request->cmakeoptions().end()); + return UserProjectConfiguration::RunProjectConfigurationCommands( + buildDirPath, utbotProjectContext, cmakeOptions, writer); + } + case ConfigMode::ALL: { + std::vector cmakeOptions(request->cmakeoptions().begin(), request->cmakeoptions().end()); + return UserProjectConfiguration::RunProjectReConfigurationCommands( + buildDirPath, fs::path(utbotProjectContext.projectPath), + utbotProjectContext, cmakeOptions, writer); + } + default: + return {StatusCode::CANCELLED, "Invalid request type."}; } } @@ -650,10 +654,10 @@ Status Server::TestsGenServiceImpl::GetProjectTargets(ServerContext *context, MEASURE_FUNCTION_EXECUTION_TIME try { - utbot::ProjectContext projectContext{ request->projectcontext() }; - auto buildDatabase = BuildDatabase::create(projectContext); - auto targets = buildDatabase->getAllTargets(); - ProjectTargetsWriter targetsWriter{ response }; + utbot::ProjectContext projectContext{request->projectcontext()}; + auto buildDatabase = ProjectBuildDatabase::create(projectContext); + std::vector targets = buildDatabase->getAllTargetPaths(); + ProjectTargetsWriter targetsWriter(response); targetsWriter.writeResponse(projectContext, targets); } catch (CompilationDatabaseException const &e) { return failedToLoadCDbStatus(e); @@ -672,13 +676,13 @@ Status Server::TestsGenServiceImpl::GetFileTargets(ServerContext *context, MEASURE_FUNCTION_EXECUTION_TIME try { - utbot::ProjectContext projectContext{ request->projectcontext() }; - auto buildDatabase = BuildDatabase::create(projectContext); + utbot::ProjectContext projectContext{request->projectcontext()}; + auto buildDatabase = ProjectBuildDatabase::create(projectContext); fs::path path = request->path(); auto targetPaths = buildDatabase->getTargetPathsForSourceFile(path); - FileTargetsWriter targetsWriter{ response }; + FileTargetsWriter targetsWriter{response}; targetsWriter.writeResponse(targetPaths, projectContext); - } catch (CompilationDatabaseException const& e) { + } catch (CompilationDatabaseException const &e) { return failedToLoadCDbStatus(e); } return Status::OK; @@ -686,7 +690,7 @@ Status Server::TestsGenServiceImpl::GetFileTargets(ServerContext *context, RequestLockMutex &Server::TestsGenServiceImpl::getLock() { std::string const &client = RequestEnvironment::getClientId(); - auto [iterator, inserted] = locks.try_emplace(client); + auto[iterator, inserted] = locks.try_emplace(client); return iterator->second; } @@ -694,10 +698,10 @@ std::unique_lock Server::TestsGenServiceImpl::acquireLock(ProgressWriter *writer) { auto &lock = getLock(); if (lock.try_lock()) { - return std::unique_lock{ lock, std::adopt_lock }; + return std::unique_lock{lock, std::adopt_lock}; } if (writer != nullptr) { writer->writeProgress("Waiting for previous task to be finished"); } - return std::unique_lock{ lock }; + return std::unique_lock{lock}; } diff --git a/server/src/Synchronizer.cpp b/server/src/Synchronizer.cpp index 76117752d..f2ec94c6e 100644 --- a/server/src/Synchronizer.cpp +++ b/server/src/Synchronizer.cpp @@ -153,7 +153,7 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, auto options = Fetcher::Options::Value::FUNCTION | Fetcher::Options::Value::INCLUDE | Fetcher::Options::Value::TYPE; auto stubFetcher = - Fetcher(options, testGen->getBuildDatabase(true)->compilationDatabase, sourceFilesMap, &testGen->types, + Fetcher(options, testGen->getProjectBuildDatabase()->compilationDatabase, sourceFilesMap, &testGen->types, &sizeContext->pointerSize, &sizeContext->maximumAlignment, testGen->compileCommandsJsonPath, false); @@ -165,7 +165,7 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs, auto stubsCdb = createStubsCompilationDatabase(stubFiles, ccJsonStubDirPath); auto sourceToHeaderRewriter = - SourceToHeaderRewriter(testGen->projectContext, testGen->getBuildDatabase(true)->compilationDatabase, + SourceToHeaderRewriter(testGen->projectContext, testGen->getProjectBuildDatabase()->compilationDatabase, stubFetcher.getStructsToDeclare(), testGen->serverBuildDir); for (const StubOperator &outdatedStub : outdatedStubs) { @@ -211,7 +211,7 @@ void Synchronizer::synchronizeWrappers(const CollectionUtils::FileSet &outdatedS sourceFilesNeedToRegenerateWrappers, testGen->progressWriter, "Generating wrappers", [this](fs::path const &sourceFilePath) { SourceToHeaderRewriter sourceToHeaderRewriter(testGen->projectContext, - testGen->getBuildDatabase(true)->compilationDatabase, nullptr, + testGen->getProjectBuildDatabase()->compilationDatabase, nullptr, testGen->serverBuildDir); std::string wrapper = sourceToHeaderRewriter.generateWrapper(sourceFilePath); printer::SourceWrapperPrinter(Paths::getSourceLanguage(sourceFilePath)).print(testGen->projectContext, sourceFilePath, wrapper); @@ -219,11 +219,11 @@ void Synchronizer::synchronizeWrappers(const CollectionUtils::FileSet &outdatedS } const CollectionUtils::FileSet &Synchronizer::getSourceFiles() const { - return testGen->getBuildDatabase(false)->compilationDatabase->getAllFiles(); + return testGen->getTargetBuildDatabase()->compilationDatabase->getAllFiles(); } StubSet Synchronizer::getStubsFiles() const { - return getStubSetFromSources(testGen->getBuildDatabase(true)->compilationDatabase->getAllFiles()); + return getStubSetFromSources(testGen->getProjectBuildDatabase()->compilationDatabase->getAllFiles()); } void Synchronizer::prepareDirectory(const fs::path &stubDirectory) { diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 8e219ffd4..4da671115 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -16,19 +16,27 @@ #include #include #include +#include -static std::string tryConvertOptionToPath(const std::string &possibleFilePath, - const fs::path &dirPath) { - if (StringUtils::startsWith(possibleFilePath, "-")) { - return possibleFilePath; - } - fs::path fullFilePath; - try { - fullFilePath = Paths::getCCJsonFileFullPath(possibleFilePath, dirPath); - } catch (...) { - return possibleFilePath; - } - return fs::exists(fullFilePath) ? fullFilePath.string() : possibleFilePath; +BuildDatabase::BuildDatabase( + fs::path serverBuildDir, + fs::path buildCommandsJsonPath, + fs::path linkCommandsJsonPath, + fs::path compileCommandsJsonPath, + utbot::ProjectContext projectContext +) : serverBuildDir(std::move(serverBuildDir)), + buildCommandsJsonPath(std::move(buildCommandsJsonPath)), + linkCommandsJsonPath(std::move(linkCommandsJsonPath)), + compileCommandsJsonPath(std::move(compileCommandsJsonPath)), + projectContext(std::move(projectContext)) { +} + +BuildDatabase::BuildDatabase(BuildDatabase *baseBuildDatabase) : + serverBuildDir(baseBuildDatabase->serverBuildDir), + projectContext(baseBuildDatabase->projectContext), + buildCommandsJsonPath(baseBuildDatabase->buildCommandsJsonPath), + linkCommandsJsonPath(baseBuildDatabase->linkCommandsJsonPath), + compileCommandsJsonPath(baseBuildDatabase->compileCommandsJsonPath) { } fs::path BuildDatabase::createExplicitObjectFileCompilationCommand(const std::shared_ptr &objectInfo) { @@ -435,10 +443,18 @@ std::vector> BuildDatabase::getAllTar return CollectionUtils::getValues(targetInfos); } +std::vector BuildDatabase::getAllTargetPaths() const { + return CollectionUtils::transformTo>(CollectionUtils::getValues(targetInfos), + [](const std::shared_ptr &targetInfo) { + return targetInfo->getOutput(); + }); +} + std::vector> BuildDatabase::getTargetsForSourceFile(const fs::path &sourceFilePath) const { CollectionUtils::MapFileTo cache; - std::function containsSourceFilePath = [&](fs::path const &unitFile) { + std::function< + bool(fs::path const &)> containsSourceFilePath = [&](fs::path const &unitFile) { if (CollectionUtils::containsKey(cache, unitFile)) { return cache[unitFile]; } @@ -483,7 +499,8 @@ std::vector BuildDatabase::getTargetPathsForObjectFile(const fs::path std::shared_ptr BuildDatabase::getPriorityTarget() const { CollectionUtils::MapFileTo cache; - std::function numberOfSources = [&](fs::path const &unitFile) { + std::function< + int(fs::path const &)> numberOfSources = [&](fs::path const &unitFile) { if (CollectionUtils::containsKey(cache, unitFile)) { return cache[unitFile]; } @@ -515,7 +532,7 @@ fs::path BuildDatabase::newDirForFile(const fs::path &file) const { } CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path &_target) { - LOG_IF_S(WARNING, !hasAutoTarget() && target != _target.c_str()) << "Try get sources for different target"; + LOG_IF_S(WARNING, !hasAutoTarget() && getTargetPath() != _target.c_str()) << "Try get sources for different target"; return CollectionUtils::transformTo( getArchiveObjectFiles(_target), [this](fs::path const &objectPath) { @@ -523,10 +540,10 @@ CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path & }); } -bool BuildDatabase::hasAutoTarget() const { - return isAutoTarget; +std::shared_ptr BuildDatabase::getTargetInfo(const fs::path &_target) { + return targetInfos[_target]; } -fs::path BuildDatabase::getTargetPath() const { - return target; +std::vector>> BuildDatabase::getCompileCommands_temp() { + return compileCommands_temp; } diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 64fb80ad3..134e059c9 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -22,12 +22,17 @@ class BuildDatabase { explicit KleeFilesInfo(fs::path kleeFile); void setCorrectMethods(std::unordered_set correctMethods); + void setAllAreCorrect(bool allAreCorrect); bool isCorrectMethod(const std::string &method); + fs::path getKleeFile(); + fs::path getKleeBitcodeFile(); + fs::path getKleeFile(const std::string &methodName); + fs::path getKleeBitcodeFile(const std::string &methodName); private: @@ -67,8 +72,10 @@ class BuildDatabase { // Directory from where to execute the command [[nodiscard]] fs::path const &getDirectory() const; + // User source file [[nodiscard]] fs::path getSourcePath() const; + // User object file [[nodiscard]] fs::path getOutputFile() const; @@ -99,6 +106,7 @@ class BuildDatabase { public: const fs::path &getCompileCommandsJson(); + const fs::path &getLinkCommandsJson(); /** @@ -158,6 +166,7 @@ class BuildDatabase { * @throws CompilationDatabaseException if files are wrong */ [[nodiscard]] std::shared_ptr getClientLinkUnitInfo(const fs::path &filepath) const; + [[nodiscard]] fs::path getObjectFile(const fs::path &sourceFile) const; /** @@ -168,8 +177,11 @@ class BuildDatabase { * @throws CompilationDatabaseException if file is wrong */ [[nodiscard]] fs::path getRootForSource(const fs::path &path) const; + [[nodiscard]] fs::path getRootForFirstSource() const; + [[nodiscard]] fs::path getBitcodeForSource(const fs::path &sourceFile) const; + [[nodiscard]] fs::path getBitcodeFile(const fs::path &filepath) const; /** @@ -193,41 +205,62 @@ class BuildDatabase { std::vector> getAllCompileCommands() const; std::vector> getRootTargets() const; - virtual std::vector> getAllTargets() const = 0; - virtual std::vector - getTargetPathsForSourceFile(const fs::path &sourceFilePath) const; - virtual std::vector - getTargetPathsForObjectFile(const fs::path &objectFile) const; + std::vector> getAllTargets() const; + + std::vector getAllTargetPaths() const; + + virtual std::vector getTargetPathsForSourceFile(const fs::path &sourceFilePath) const; + + virtual std::vector getTargetPathsForObjectFile(const fs::path &objectFile) const; std::shared_ptr getPriorityTarget() const; CollectionUtils::FileSet getSourceFilesForTarget(const fs::path &_target); + std::shared_ptr getTargetInfo(const fs::path &_target); + + //TODO change + std::vector>> getCompileCommands_temp(); + virtual bool hasAutoTarget() const = 0; - fs::path getTargetPath() const; + virtual fs::path getTargetPath() const = 0; std::shared_ptr compilationDatabase; -protected: - BuildDatabase() ; +protected: const fs::path serverBuildDir; - const utbot::ProjectContext projectContext; const fs::path buildCommandsJsonPath; const fs::path linkCommandsJsonPath; const fs::path compileCommandsJsonPath; + const utbot::ProjectContext projectContext; CollectionUtils::MapFileTo>> sourceFileInfos; CollectionUtils::MapFileTo> objectFileInfos; CollectionUtils::MapFileTo> targetInfos; CollectionUtils::MapFileTo> objectFileTargets; + //TODO change std::vector>> compileCommands_temp; + BuildDatabase( + fs::path serverBuildDir, + fs::path buildCommandsJsonPath, + fs::path linkCommandsJsonPath, + fs::path compileCommandsJsonPath, + utbot::ProjectContext projectContext + ); + + BuildDatabase(BuildDatabase *baseBuildDatabase); + static fs::path getCorrespondingBitcodeFile(const fs::path &filepath); + void createClangCompileCommandsJson(); + void mergeLibraryOptions(std::vector &jsonArguments) const; - fs::path newDirForFile(fs::path const& file) const; + + fs::path newDirForFile(fs::path const &file) const; + fs::path createExplicitObjectFileCompilationCommand(const std::shared_ptr &objectInfo); std::vector> getTargetsForSourceFile(fs::path const &sourceFilePath) const; diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index c33eeb637..9fd10c87a 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -56,15 +56,15 @@ Result Linker::linkForTarget(const fs::path &target, const f const fs::path &objectFile) { testGen.setTargetPath(target); - auto siblings = testGen.getBuildDatabase(false)->getArchiveObjectFiles(target); + auto siblings = testGen.getTargetBuildDatabase()->getArchiveObjectFiles(target); auto stubSources = stubGen.getStubSources(target); CollectionUtils::MapFileTo filesToLink; for (const auto &sibling : siblings) { auto siblingCompilationUnitInfo = - testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sibling); + testGen.getClientCompilationUnitInfo(sibling); fs::path siblingObjectFile = siblingCompilationUnitInfo->getOutputFile(); - fs::path bitcodeFile = testGen.getBuildDatabase(false)->getBitcodeForSource( + fs::path bitcodeFile = testGen.getTargetBuildDatabase()->getBitcodeForSource( siblingCompilationUnitInfo->getSourcePath()); if (CollectionUtils::contains(stubSources, siblingCompilationUnitInfo->getSourcePath())) { @@ -75,7 +75,7 @@ Result Linker::linkForTarget(const fs::path &target, const f } kleeGenerator->buildByCDb(filesToLink, stubSources); - auto linkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(sourceFilePath); + auto linkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(sourceFilePath); std::optional moduleOutput = linkUnitInfo->getOutput(); std::string suffixForParentOfStubs = StringUtils::stringFormat("___%s", Paths::mangle(moduleOutput.value().filename())); @@ -87,16 +87,16 @@ Result Linker::linkForTarget(const fs::path &target, const f void Linker::linkForOneFile(const fs::path &sourceFilePath) { ExecUtils::throwIfCancelled(); - auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sourceFilePath); + auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(sourceFilePath); fs::path objectFile = compilationUnitInfo->getOutputFile(); if (CollectionUtils::contains(testedFiles, sourceFilePath)) { return; } - if (!testGen.getBuildDatabase(false)->isFirstObjectFileForSource(objectFile)) { + if (!testGen.getTargetBuildDatabase()->isFirstObjectFileForSource(objectFile)) { return; } - std::vector targets = testGen.getBuildDatabase(false)->getTargetPathsForObjectFile(objectFile); + std::vector targets = testGen.getTargetBuildDatabase()->getTargetPathsForObjectFile(objectFile); LOG_S(DEBUG) << "Linking bitcode for file " << sourceFilePath.filename(); for (size_t i = 0; i < targets.size(); i++) { const auto& target = targets[i]; @@ -105,14 +105,14 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { if (result.isSuccess()) { auto [targetBitcode, stubsSet, _] = result.getOpt().value(); addToGenerated({ objectFile }, targetBitcode); - auto&& targetUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(target); + auto&& targetUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(target); return; } else { LOG_S(DEBUG) << "Linkage for target " << target.filename() << " failed: " << result.getError()->c_str(); if (i + 1 == targets.size()) { addToGenerated({ objectFile }, {}); fs::path possibleBitcodeFileName = - testGen.getBuildDatabase(false)->getBitcodeFile(testGen.getBuildDatabase(false)->getTargetPath()); + testGen.getTargetBuildDatabase()->getBitcodeFile(testGen.getTargetBuildDatabase()->getTargetPath()); brokenLinkFiles.insert(possibleBitcodeFileName); } } @@ -120,20 +120,20 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) { } Result Linker::linkWholeTarget(const fs::path &target) { - auto requestTarget = testGen.getBuildDatabase(false)->getTargetPath(); - LOG_IF_S(WARNING, !testGen.getBuildDatabase(false)->hasAutoTarget() && requestTarget != target) + auto requestTarget = testGen.getTargetBuildDatabase()->getTargetPath(); + LOG_IF_S(WARNING, !testGen.getTargetBuildDatabase()->hasAutoTarget() && requestTarget != target) << "Try link target that not specified by user"; testGen.setTargetPath(target); - auto targetUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(target); - auto siblings = testGen.getBuildDatabase(false)->getArchiveObjectFiles(target); + auto targetUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(target); + auto siblings = testGen.getTargetBuildDatabase()->getArchiveObjectFiles(target); auto stubSources = stubGen.getStubSources(target); CollectionUtils::MapFileTo filesToLink; CollectionUtils::FileSet siblingObjectsToBuild; for (const fs::path &objectFile : siblings) { - auto objectInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(objectFile); + auto objectInfo = testGen.getClientCompilationUnitInfo(objectFile); bool insideFolder = true; if (auto folderTestGen = dynamic_cast(&testGen)) { fs::path folderGen = folderTestGen->folderPath; @@ -145,7 +145,7 @@ Result Linker::linkWholeTarget(const fs::path &target) { fs::path bitcodeFile = objectInfo->kleeFilesInfo->getKleeBitcodeFile(); filesToLink.emplace(objectFile, bitcodeFile); } else { - fs::path bitcodeFile = testGen.getBuildDatabase(false)->getBitcodeForSource(objectInfo->getSourcePath()); + fs::path bitcodeFile = testGen.getTargetBuildDatabase()->getBitcodeForSource(objectInfo->getSourcePath()); siblingObjectsToBuild.insert(objectInfo->getOutputFile()); filesToLink.emplace(objectFile, bitcodeFile); } @@ -164,17 +164,17 @@ void Linker::linkForProject() { testGen.tests, testGen.progressWriter, "Compiling and linking source files", [&](auto const &it) { fs::path const &sourceFile = it.first; - auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sourceFile); + auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(sourceFile); fs::path objectFile = compilationUnitInfo->getOutputFile(); if (!CollectionUtils::contains(testedFiles, sourceFile)) { - auto objectInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sourceFile); + auto objectInfo = testGen.getClientCompilationUnitInfo(sourceFile); if (objectInfo->linkUnit.empty()) { LOG_S(WARNING) << "No executable or library found for current source file in " "link_commands.json: " << sourceFile; return; } - std::vector targets = testGen.getBuildDatabase(false)->getTargetPathsForObjectFile( + std::vector targets = testGen.getTargetBuildDatabase()->getTargetPathsForObjectFile( objectFile); bool success = false; for (const auto &target : targets) { @@ -187,7 +187,7 @@ void Linker::linkForProject() { auto linkres = result.getOpt().value(); auto objectFiles = CollectionUtils::transformTo( linkres.presentedFiles, [&](const fs::path &sourceFile) { - auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo( + auto compilationUnitInfo = testGen.getClientCompilationUnitInfo( sourceFile); return compilationUnitInfo->getOutputFile(); }); @@ -209,9 +209,9 @@ void Linker::linkForProject() { void Linker::addToGenerated(const CollectionUtils::FileSet &objectFiles, const fs::path &output) { for (const auto &objectFile : objectFiles) { - auto objectInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(objectFile); + auto objectInfo = testGen.getClientCompilationUnitInfo(objectFile); const fs::path &sourcePath = objectInfo->getSourcePath(); - if (testGen.getBuildDatabase(false)->isFirstObjectFileForSource(objectFile) && + if (testGen.getTargetBuildDatabase()->isFirstObjectFileForSource(objectFile) && !CollectionUtils::contains(testedFiles, sourcePath)) { testedFiles.insert(sourcePath); bitcodeFileName[sourcePath] = output; @@ -256,7 +256,7 @@ std::vector Linker::getTestMethods() { isAnyOneLinked = true; for (const auto &[methodName, _] : tests.methods) { auto compilationUnitInfo = - testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(fileName); + testGen.getClientCompilationUnitInfo(fileName); if (compilationUnitInfo->kleeFilesInfo->isCorrectMethod(methodName)) { testMethods.emplace_back(methodName, bitcodePath, @@ -283,7 +283,7 @@ std::vector Linker::getTestMethods() { method.classObj.has_value() && method.classObj->type.typeName() == lineInfo->scopeName)) { auto compilationUnitInfo = - testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(fileName); + testGen.getClientCompilationUnitInfo(fileName); if (compilationUnitInfo->kleeFilesInfo->isCorrectMethod(methodName)) { tests::TestMethod testMethod{ methodName, bitcodeFileName.at(lineInfo->filePath), @@ -397,7 +397,7 @@ Result Linker::link(const CollectionUtils::MapFileTogetClientCompilationUnitInfo(objectFile); + auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(objectFile); auto sourcePath = compilationUnitInfo->getSourcePath(); if (CollectionUtils::containsKey(testGen.tests, sourcePath)) { testMakefilesPrinter.GetMakefiles(sourcePath).write(); @@ -434,7 +434,7 @@ Result Linker::generateStubsMakefile( methodDescription.name = symbol; return methodDescription; }); - auto rootLinkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(root); + auto rootLinkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(root); auto stubsSet = StubGen(testGen).findStubFilesBySignatures(signatures); printer::DefaultMakefilePrinter makefilePrinter; auto bitcodeStubFiles = CollectionUtils::transformTo>( @@ -732,7 +732,7 @@ Linker::addLinkTargetRecursively(const fs::path &fileToBuild, const std::optional &testedFilePath, bool shouldChangeDirectory) { if (Paths::isObjectFile(fileToBuild)) { - auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(fileToBuild); + auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(fileToBuild); fs::path sourcePath = compilationUnitInfo->getSourcePath(); BuildResult::Type type = CollectionUtils::contains(stubSources, sourcePath) ? BuildResult::Type::ALL_STUBS @@ -745,7 +745,7 @@ Linker::addLinkTargetRecursively(const fs::path &fileToBuild, } return { bitcode, type }; } else { - auto linkUnit = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(fileToBuild); + auto linkUnit = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(fileToBuild); CollectionUtils::MapFileTo dependencies; // object file -> bitcode BuildResult::Type unitType = BuildResult::Type::NONE; for (auto const &subfile : linkUnit->files) { @@ -761,7 +761,7 @@ Linker::addLinkTargetRecursively(const fs::path &fileToBuild, } auto bitcodeDependencies = CollectionUtils::getValues(dependencies); fs::path prefixPath = getPrefixPath(bitcodeDependencies, testGen.serverBuildDir); - auto output = testGen.getBuildDatabase(false)->getBitcodeFile(fileToBuild); + auto output = testGen.getTargetBuildDatabase()->getBitcodeFile(fileToBuild); output = LinkerUtils::applySuffix(output, unitType, suffixForParentOfStubs); if (Paths::isLibraryFile(fileToBuild)) { auto archiveActions = getArchiveCommands(prefixPath, dependencies, *linkUnit, output, shouldChangeDirectory); diff --git a/server/src/building/ProjectBuildDatabase.h b/server/src/building/ProjectBuildDatabase.h index 3efbab2d1..1e0b803c0 100644 --- a/server/src/building/ProjectBuildDatabase.h +++ b/server/src/building/ProjectBuildDatabase.h @@ -3,14 +3,26 @@ #include "BuildDatabase.h" -class ProjectBuildDatabase : BuildDatabase { -private: +class ProjectBuildDatabase : public BuildDatabase { +public: ProjectBuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext); -public: static std::shared_ptr create(const utbot::ProjectContext &projectContext); + + void initObjects(const nlohmann::json &compileCommandsJson); + + void initInfo(const nlohmann::json &linkCommandsJson); + + void filterInstalledFiles(); + + void addLocalSharedLibraries(); + + void fillTargetInfoParents(); + bool hasAutoTarget() const override; + + fs::path getTargetPath() const override; }; diff --git a/server/src/building/ProjectBuildDatabse.cpp b/server/src/building/ProjectBuildDatabse.cpp index af548ce4d..807bc0dcf 100644 --- a/server/src/building/ProjectBuildDatabse.cpp +++ b/server/src/building/ProjectBuildDatabse.cpp @@ -1,13 +1,33 @@ #include "ProjectBuildDatabase.h" +#include "utils/GrpcUtils.h" +#include "exceptions/CompilationDatabaseException.h" +#include "utils/JsonUtils.h" +#include "loguru.h" +#include "utils/StringUtils.h" + + +static std::string tryConvertOptionToPath(const std::string &possibleFilePath, + const fs::path &dirPath) { + if (StringUtils::startsWith(possibleFilePath, "-")) { + return possibleFilePath; + } + fs::path fullFilePath; + try { + fullFilePath = Paths::getCCJsonFileFullPath(possibleFilePath, dirPath); + } catch (...) { + return possibleFilePath; + } + return fs::exists(fullFilePath) ? fullFilePath.string() : possibleFilePath; +} ProjectBuildDatabase::ProjectBuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, utbot::ProjectContext _projectContext) : - serverBuildDir(std::move(_serverBuildDir)), - projectContext(std::move(_projectContext)), - buildCommandsJsonPath(std::move(_buildCommandsJsonPath)), - linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")), - compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) { + BuildDatabase(_serverBuildDir, + _buildCommandsJsonPath, + fs::canonical(_buildCommandsJsonPath / "link_commands.json"), + fs::canonical(_buildCommandsJsonPath / "compile_commands.json"), + std::move(_projectContext)) { if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) { throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files"); } @@ -21,21 +41,20 @@ ProjectBuildDatabase::ProjectBuildDatabase(fs::path _buildCommandsJsonPath, addLocalSharedLibraries(); fillTargetInfoParents(); createClangCompileCommandsJson(); - target = GrpcUtils::UTBOT_AUTO_TARGET_PATH; } -std::shared_ptr BuildDatabase::create(const utbot::ProjectContext &projectContext) { +std::shared_ptr ProjectBuildDatabase::create(const utbot::ProjectContext &projectContext) { fs::path compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( projectContext.projectPath, projectContext.buildDirRelativePath); fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); - std::shared_ptr buildDatabase = std::make_shared(compileCommandsJsonPath, - serverBuildDir, projectContext); + std::shared_ptr buildDatabase = std::make_shared( + std::move(ProjectBuildDatabase(compileCommandsJsonPath, serverBuildDir, projectContext))); return buildDatabase; } -void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) { +void ProjectBuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) { for (const nlohmann::json &compileCommand: compileCommandsJson) { auto objectInfo = std::make_shared(); @@ -108,8 +127,8 @@ void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) { } } -void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { - for (nlohmann::json const &linkCommand : linkCommandsJson) { +void ProjectBuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { + for (nlohmann::json const &linkCommand: linkCommandsJson) { fs::path directory = linkCommand.at("directory").get(); std::vector jsonArguments; if (linkCommand.contains("command")) { @@ -143,8 +162,9 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { targetInfo->addFile(currentFile); if (Paths::isObjectFile(currentFile)) { if (!CollectionUtils::containsKey(objectFileInfos, currentFile)) { - throw CompilationDatabaseException("compile_commands.json doesn't contain a command for object file " - + currentFile.string()); + throw CompilationDatabaseException( + "compile_commands.json doesn't contain a command for object file " + + currentFile.string()); } objectFileInfos[currentFile]->linkUnit = output; } @@ -154,8 +174,8 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) { } -void BuildDatabase::filterInstalledFiles() { - for (auto &it : targetInfos) { +void ProjectBuildDatabase::filterInstalledFiles() { + for (auto &it: targetInfos) { auto &linkFile = it.first; auto &targetInfo = it.second; CollectionUtils::OrderedFileSet fileset; @@ -165,7 +185,8 @@ void BuildDatabase::filterInstalledFiles() { CollectionUtils::containsKey(objectFileInfos, file); }); if (!targetInfo->installedFiles.empty()) { - LOG_S(DEBUG) << "Target " << linkFile << " depends on " << targetInfo->installedFiles.size() << " installed files"; + LOG_S(DEBUG) << "Target " << linkFile << " depends on " << targetInfo->installedFiles.size() + << " installed files"; } CollectionUtils::erase_if(targetInfo->files, [&targetInfo](fs::path const &file) { return CollectionUtils::contains(targetInfo->installedFiles, file); @@ -173,28 +194,28 @@ void BuildDatabase::filterInstalledFiles() { } } -void BuildDatabase::addLocalSharedLibraries() { +void ProjectBuildDatabase::addLocalSharedLibraries() { sharedLibrariesMap sharedLibraryFiles; - for (const auto &[linkFile, linkUnit] : targetInfos) { + for (const auto &[linkFile, linkUnit]: targetInfos) { if (Paths::isSharedLibraryFile(linkFile)) { auto withoutVersion = CompilationUtils::removeSharedLibraryVersion(linkFile); sharedLibraryFiles[withoutVersion.filename()][linkFile.parent_path()] = linkFile; } } - for (auto &[linkFile, targetInfo] : targetInfos) { - for (auto &command : targetInfo->commands) { + for (auto &[linkFile, targetInfo]: targetInfos) { + for (auto &command: targetInfo->commands) { addLibrariesForCommand(command, *targetInfo, sharedLibraryFiles); } } - for (auto &[objectFile, objectInfo] : objectFileInfos) { + for (auto &[objectFile, objectInfo]: objectFileInfos) { addLibrariesForCommand(objectInfo->command, *objectInfo, sharedLibraryFiles, true); } } -void BuildDatabase::fillTargetInfoParents() { +void ProjectBuildDatabase::fillTargetInfoParents() { CollectionUtils::MapFileTo> parentTargets; - for (const auto &[linkFile, linkUnit] : targetInfos) { - for (const fs::path &dependencyFile : linkUnit->files) { + for (const auto &[linkFile, linkUnit]: targetInfos) { + for (const fs::path &dependencyFile: linkUnit->files) { if (Paths::isLibraryFile(dependencyFile)) { parentTargets[dependencyFile].emplace_back(linkFile); } @@ -203,13 +224,22 @@ void BuildDatabase::fillTargetInfoParents() { } } } - for (auto &[library, parents] : parentTargets) { + for (auto &[library, parents]: parentTargets) { if (!CollectionUtils::containsKey(targetInfos, library)) { throw CompilationDatabaseException( "link_commands.json doesn't contain a command for building library: " + - library.string() + "\nReferenced from command for: " + (parents.empty() ? "none" : parents[0].string())); + library.string() + "\nReferenced from command for: " + + (parents.empty() ? "none" : parents[0].string())); } targetInfos[library]->parentLinkUnits = std::move(parents); } } + +bool ProjectBuildDatabase::hasAutoTarget() const { + return true; +} + +fs::path ProjectBuildDatabase::getTargetPath() const { + throw CompilationDatabaseException("Incorrect method for project build database"); +} diff --git a/server/src/building/TargetBuildDatabase.cpp b/server/src/building/TargetBuildDatabase.cpp index 373a1d30b..6ae7f2031 100644 --- a/server/src/building/TargetBuildDatabase.cpp +++ b/server/src/building/TargetBuildDatabase.cpp @@ -1,55 +1,55 @@ #include "TargetBuildDatabase.h" +#include "loguru.h" +#include "utils/GrpcUtils.h" +#include "utils/GenerationUtils.h" -TargetBuildDatabase::TargetBuildDatabase(BuildDatabase &baseBuildDatabase, - const std::string &_target) : - serverBuildDir(baseBuildDatabase.serverBuildDir), - projectContext(baseBuildDatabase.projectContext), - buildCommandsJsonPath(baseBuildDatabase.buildCommandsJsonPath), - linkCommandsJsonPath(baseBuildDatabase.linkCommandsJsonPath), - compileCommandsJsonPath(baseBuildDatabase.compileCommandsJsonPath), +TargetBuildDatabase::TargetBuildDatabase(BuildDatabase *baseBuildDatabase, + const fs::path &_target) : + BuildDatabase(baseBuildDatabase), target(_target), isAutoTarget(_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { { - auto objectFilesList = baseBuildDatabase.getArchiveObjectFiles(target); + auto objectFilesList = baseBuildDatabase->getArchiveObjectFiles(target); for (const auto &objectFilePath: objectFilesList) { - auto objectFileInfo = baseBuildDatabase.getClientCompilationObjectInfo(objectFilePath); + auto objectFileInfo = baseBuildDatabase->getClientCompilationObjectInfo(objectFilePath); sourceFileInfos[objectFileInfo->getSourcePath()].push_back(objectFileInfo); LOG_IF_S(DEBUG, sourceFileInfos[objectFileInfo->getSourcePath()].size() > 1) - << "Multiple compile commands for file \"" << objectFileInfo->getSourcePath() << "\" in target \"" - << target.string() << "\""; + << "Multiple compile commands for file \"" << objectFileInfo->getSourcePath() << "\" in target \"" + << target.string() << "\""; objectFileInfos[objectFileInfo->getOutputFile()] = objectFileInfo; - objectFileTargets[objectFileInfo->getOutputFile()] = - baseBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()]; + objectFileTargets[objectFileInfo->getOutputFile()] = baseBuildDatabase->getTargetPathsForObjectFile( + objectFileInfo->getOutputFile()); } } { - auto targetFilesList = baseBuildDatabase.getArchiveTargetFiles(target); + auto targetFilesList = baseBuildDatabase->getArchiveTargetFiles(target); for (const auto &objectFilePath: targetFilesList) { - targetInfos[objectFilePath] = baseBuildDatabase.targetInfos[objectFilePath]; + targetInfos[objectFilePath] = baseBuildDatabase->getTargetInfo(objectFilePath); } } - compileCommands_temp = baseBuildDatabase.compileCommands_temp; + compileCommands_temp = baseBuildDatabase->getCompileCommands_temp(); createClangCompileCommandsJson(); } -std::shared_ptr BuildDatabase::createForSourceOrTarget(const std::string &_targetOrSourcePath) { +std::shared_ptr TargetBuildDatabase::createForSourceOrTarget(BuildDatabase *baseBuildDatabase, + const std::string &_targetOrSourcePath) { fs::path _target; if (Paths::isSourceFile(_targetOrSourcePath)) { - _target = getRootForSource(_targetOrSourcePath); + _target = baseBuildDatabase->getRootForSource(_targetOrSourcePath); } else if (_targetOrSourcePath == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _targetOrSourcePath.empty()) { - _target = getRootForFirstSource(); + _target = baseBuildDatabase->getRootForFirstSource(); } else { - auto new_target = GenerationUtils::findTarget(getAllTargets(), _targetOrSourcePath); + auto new_target = GenerationUtils::findTarget(baseBuildDatabase->getAllTargets(), _targetOrSourcePath); if (new_target.has_value()) { _target = new_target.value(); } else { throw CompilationDatabaseException("Can't find target: " + _targetOrSourcePath); } } - return std::make_shared(std::move(BuildDatabase(*this, _target))); + return std::make_shared(std::move(TargetBuildDatabase(baseBuildDatabase, _target))); } std::vector TargetBuildDatabase::getTargetPathsForSourceFile(const fs::path &sourceFilePath) const { @@ -59,10 +59,17 @@ std::vector TargetBuildDatabase::getTargetPathsForSourceFile(const fs: return BuildDatabase::getTargetPathsForSourceFile(sourceFilePath); } - std::vector TargetBuildDatabase::getTargetPathsForObjectFile(const fs::path &objectFile) const { if (!hasAutoTarget()) { return {target}; } return BuildDatabase::getTargetPathsForObjectFile(objectFile); } + +bool TargetBuildDatabase::hasAutoTarget() const { + return isAutoTarget; +} + +fs::path TargetBuildDatabase::getTargetPath() const { + return target; +} diff --git a/server/src/building/TargetBuildDatabase.h b/server/src/building/TargetBuildDatabase.h index 61fdef237..8a099247e 100644 --- a/server/src/building/TargetBuildDatabase.h +++ b/server/src/building/TargetBuildDatabase.h @@ -4,32 +4,23 @@ #include "BuildDatabase.h" -class TargetBuildDatabase : BuildDatabase { +class TargetBuildDatabase : public BuildDatabase { private: - TargetBuildDatabase(BuildDatabase &baseBuildDatabase, const std::string &_target); - - void filterInstalledFiles(); - - void addLocalSharedLibraries(); - - void fillTargetInfoParents(); - - void initObjects(const nlohmann::json &compileCommandsJson); - - void initInfo(const nlohmann::json &linkCommandsJson); + TargetBuildDatabase(BuildDatabase *baseBuildDatabase, const fs::path &_target); fs::path target; bool isAutoTarget; public: - std::shared_ptr createForSourceOrTarget(std::shared_ptr baseBuildDatabase, - std::string &_targetOrSourcePath); + static std::shared_ptr createForSourceOrTarget(BuildDatabase *baseBuildDatabase, + const std::string &_targetOrSourcePath); bool hasAutoTarget() const override; + fs::path getTargetPath() const override; + std::vector getTargetPathsForSourceFile(const fs::path &sourceFilePath) const override; std::vector getTargetPathsForObjectFile(const fs::path &objectFile) const override; }; - #endif //UTBOTCPP_TARGETBUILDDATABASE_H diff --git a/server/src/printers/NativeMakefilePrinter.cpp b/server/src/printers/NativeMakefilePrinter.cpp index 329a8f69f..ac42a32f5 100644 --- a/server/src/printers/NativeMakefilePrinter.cpp +++ b/server/src/printers/NativeMakefilePrinter.cpp @@ -270,7 +270,7 @@ namespace printer { BuildResult NativeMakefilePrinter::addObjectFile(const fs::path &objectFile, const std::string &suffixForParentOfStubs) { - auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(objectFile); + auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(objectFile); fs::path sourcePath = compilationUnitInfo->getSourcePath(); fs::path pathToCompile; @@ -299,7 +299,7 @@ namespace printer { } void NativeMakefilePrinter::addTestTarget(const fs::path &sourcePath) { - auto compilationUnitInfo = testGen.getBuildDatabase(false)->getClientCompilationUnitInfo(sourcePath); + auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(sourcePath); auto testCompilationCommand = compilationUnitInfo->command; testCompilationCommand.setBuildTool(getRelativePathForLinker(primaryCxxCompiler)); testCompilationCommand.setOptimizationLevel(OPTIMIZATION_FLAG); @@ -332,7 +332,7 @@ namespace printer { artifacts.push_back(testCompilationCommand.getOutput()); - auto rootLinkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(rootPath); + auto rootLinkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath); fs::path testExecutablePath = getTestExecutablePath(sourcePath); std::vector filesToLink{ "$(GTEST_MAIN)", "$(GTEST_ALL)", testCompilationCommand.getOutput(), @@ -426,7 +426,7 @@ namespace printer { fs::path testExecutablePath = getTestExecutablePath(sourcePath); - auto rootLinkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(rootPath); + auto rootLinkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath); fs::path coverageInfoBinary = sharedOutput.value(); if (!Paths::isLibraryFile(coverageInfoBinary)) { @@ -468,7 +468,7 @@ namespace printer { return buildResults[unitFile] = buildResult; } - auto linkUnitInfo = testGen.getBuildDatabase(false)->getClientLinkUnitInfo(unitFile); + auto linkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(unitFile); BuildResult::Type unitType = BuildResult::Type::NONE; CollectionUtils::MapFileTo fileMapping; auto unitBuildResults = CollectionUtils::transformTo>( @@ -616,7 +616,7 @@ namespace printer { fs::path sourcePath = Paths::stubPathToSourcePath(testGen.projectContext, stub); fs::path stubBuildFilePath = Paths::getStubBuildFilePath(testGen.projectContext, sourcePath); - auto compilationUnitInfo = testGen.getBuildDatabase(true)->getClientCompilationUnitInfo(sourcePath); + auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(sourcePath, true); fs::path target = Paths::getRecompiledFile(testGen.projectContext, stub); addCompileTarget(stub, target, *compilationUnitInfo); return target; diff --git a/server/src/printers/TestMakefilesPrinter.cpp b/server/src/printers/TestMakefilesPrinter.cpp index 4d8bd7a9f..eda546078 100644 --- a/server/src/printers/TestMakefilesPrinter.cpp +++ b/server/src/printers/TestMakefilesPrinter.cpp @@ -34,9 +34,9 @@ namespace printer { CollectionUtils::FileSet const *stubSources) : TestMakefilesPrinter( testGen, - testGen.getBuildDatabase(false)->getTargetPath(), + testGen.getTargetBuildDatabase()->getTargetPath(), CompilationUtils::getBundledCompilerPath(CompilationUtils::getCompilerName( - testGen.getBuildDatabase(false)->compilationDatabase->getBuildCompilerPath())), + testGen.getTargetBuildDatabase()->compilationDatabase->getBuildCompilerPath())), stubSources) { } diff --git a/server/src/streams/FileTargetsWriter.h b/server/src/streams/FileTargetsWriter.h index 05e3df5d2..293e22f32 100644 --- a/server/src/streams/FileTargetsWriter.h +++ b/server/src/streams/FileTargetsWriter.h @@ -11,7 +11,7 @@ class FileTargetsWriter : public TargetsWriter { : TargetsWriter(response) { } - void writeResponse(const std::vector> &targets, + void writeResponse(const std::vector &targetPaths, const utbot::ProjectContext& projectContext); }; diff --git a/server/src/streams/ProjectTargetsWriter.cpp b/server/src/streams/ProjectTargetsWriter.cpp index 4592430e4..0414eeb3a 100644 --- a/server/src/streams/ProjectTargetsWriter.cpp +++ b/server/src/streams/ProjectTargetsWriter.cpp @@ -6,11 +6,11 @@ ProjectTargetsWriter::ProjectTargetsWriter(testsgen::ProjectTargetsResponse *res void ProjectTargetsWriter::writeResponse( const utbot::ProjectContext &projectContext, - const std::vector> &targets) { + const std::vector &targetPaths) { if (!hasStream()) { return; } - writeTargets(targets, projectContext); + writeTargets(targetPaths, projectContext); auto utbotAutoTarget = std::make_unique(GrpcUtils::createAutoTarget()); writer->set_allocated_prioritytarget(utbotAutoTarget.release()); } diff --git a/server/src/streams/ProjectTargetsWriter.h b/server/src/streams/ProjectTargetsWriter.h index 814bfbe1a..5f535ffa9 100644 --- a/server/src/streams/ProjectTargetsWriter.h +++ b/server/src/streams/ProjectTargetsWriter.h @@ -13,7 +13,7 @@ class ProjectTargetsWriter : public TargetsWriter> &targets); + const std::vector &targetPaths); }; diff --git a/server/src/stubs/StubGen.cpp b/server/src/stubs/StubGen.cpp index 7bc6e32b8..c4fe2cd16 100644 --- a/server/src/stubs/StubGen.cpp +++ b/server/src/stubs/StubGen.cpp @@ -18,7 +18,7 @@ CollectionUtils::FileSet StubGen::getStubSources(const fs::path &target) { return {}; } fs::path testedFilePath = *testGen.testingMethodsSourcePaths.begin(); - auto stubSources = StubSourcesFinder(testGen.getBuildDatabase(true)).excludeFind(testedFilePath, target); + auto stubSources = StubSourcesFinder(testGen.getProjectBuildDatabase()).excludeFind(testedFilePath, target); return { stubSources.begin(), stubSources.end() }; } diff --git a/server/src/stubs/StubSourcesFinder.cpp b/server/src/stubs/StubSourcesFinder.cpp index c3f231d41..da5a5cbe1 100644 --- a/server/src/stubs/StubSourcesFinder.cpp +++ b/server/src/stubs/StubSourcesFinder.cpp @@ -2,7 +2,7 @@ #include "loguru.h" -StubSourcesFinder::StubSourcesFinder(std::shared_ptr buildDatabase) +StubSourcesFinder::StubSourcesFinder(std::shared_ptr buildDatabase) : buildDatabase(std::move(buildDatabase)) { } diff --git a/server/src/stubs/StubSourcesFinder.h b/server/src/stubs/StubSourcesFinder.h index 7ee47987e..10461eb90 100644 --- a/server/src/stubs/StubSourcesFinder.h +++ b/server/src/stubs/StubSourcesFinder.h @@ -1,7 +1,7 @@ #ifndef UNITTESTBOT_STUBSOURCESFINDER_H #define UNITTESTBOT_STUBSOURCESFINDER_H -#include "building/BuildDatabase.h" +#include "building/ProjectBuildDatabase.h" #include "stubs/Stubs.h" #include "utils/path/FileSystemPath.h" @@ -10,7 +10,7 @@ class StubSourcesFinder { public: - explicit StubSourcesFinder(std::shared_ptr buildDatabase); + explicit StubSourcesFinder(std::shared_ptr buildDatabase); std::vector find(const fs::path& testedFilePath); @@ -19,7 +19,7 @@ class StubSourcesFinder { void printAllModules(); private: - std::shared_ptr buildDatabase; + std::shared_ptr buildDatabase; CollectionUtils::FileSet getLibraryBitcodeFiles(const fs::path &testedFilePath); }; diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index d72a692a3..d9daf6aaa 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -1,18 +1,20 @@ #include "BaseTestGen.h" +#include "exceptions/CompilationDatabaseException.h" #include "FileTestGen.h" #include "FolderTestGen.h" #include "LineTestGen.h" #include "utils/ExecUtils.h" #include "utils/ServerUtils.h" #include "utils/TypeUtils.h" +#include "loguru.h" BaseTestGen::BaseTestGen(const testsgen::ProjectContext &projectContext, const testsgen::SettingsContext &settingsContext, ProgressWriter *progressWriter, bool testMode) - : projectContext(projectContext), - settingsContext(settingsContext), progressWriter(progressWriter) { + : projectContext(projectContext), + settingsContext(settingsContext), progressWriter(progressWriter) { serverBuildDir = Paths::getUtbotBuildDir(this->projectContext); } @@ -29,40 +31,65 @@ bool BaseTestGen::isBatched() const { void BaseTestGen::setInitializedTestsMap() { tests.clear(); - for (const fs::path & sourcePath : testingMethodsSourcePaths) { + for (const fs::path &sourcePath: testingMethodsSourcePaths) { tests::Tests testsSuite; testsSuite.sourceFilePath = sourcePath; testsSuite.sourceFileNameNoExt = testsSuite.sourceFilePath.stem().string(); testsSuite.relativeFileDir = - Paths::getRelativeDirPath(projectContext, testsSuite.sourceFilePath); + Paths::getRelativeDirPath(projectContext, testsSuite.sourceFilePath); testsSuite.testFilename = Paths::sourcePathToTestName(testsSuite.sourceFilePath); testsSuite.testHeaderFilePath = - Paths::getGeneratedHeaderPath(projectContext, testsSuite.sourceFilePath); + Paths::getGeneratedHeaderPath(projectContext, testsSuite.sourceFilePath); testsSuite.testSourceFilePath = - Paths::sourcePathToTestPath(projectContext, testsSuite.sourceFilePath); + Paths::sourcePathToTestPath(projectContext, testsSuite.sourceFilePath); tests[testsSuite.sourceFilePath] = testsSuite; } } void BaseTestGen::setTargetPath(fs::path _targetPath) { - if (buildDatabase->hasAutoTarget() && buildDatabase->getTargetPath() != _targetPath) { - buildDatabase = std::move(baseBuildDatabase->createBuildDatabaseForSourceOrTarget(_targetPath)); + if (targetBuildDatabase->hasAutoTarget() && targetBuildDatabase->getTargetPath() != _targetPath) { + targetBuildDatabase = std::move( + TargetBuildDatabase::createForSourceOrTarget(projectBuildDatabase.get(), _targetPath)); updateTargetSources(_targetPath); } } void BaseTestGen::updateTargetSources(fs::path _targetPath) { - targetSources = buildDatabase->getSourceFilesForTarget(_targetPath); + targetSources = targetBuildDatabase->getSourceFilesForTarget(_targetPath); for (auto it = tests.begin(); it != tests.end(); it++) { tests::Tests &test = it.value(); test.isFilePresentedInCommands = CollectionUtils::contains(targetSources, test.sourceFilePath); } } -std::shared_ptr BaseTestGen::getBuildDatabase(bool forStub) const { - return forStub ? baseBuildDatabase : buildDatabase; +std::shared_ptr BaseTestGen::getProjectBuildDatabase() const { + return projectBuildDatabase; } -std::shared_ptr BaseTestGen::getBuildDatabase(bool forStub) { - return forStub ? baseBuildDatabase : buildDatabase; +std::shared_ptr BaseTestGen::getTargetBuildDatabase() const { + return targetBuildDatabase; +} + +std::shared_ptr BaseTestGen::getProjectBuildDatabase() { + return projectBuildDatabase; +} + +std::shared_ptr BaseTestGen::getTargetBuildDatabase() { + return targetBuildDatabase; +} + +std::shared_ptr +BaseTestGen::getClientCompilationUnitInfo(const fs::path &path, bool fullProject) const { + std::shared_ptr objectFileInfo; + try { + objectFileInfo = targetBuildDatabase->getClientCompilationUnitInfo(path); + } catch (CompilationDatabaseException &e) { + if (fullProject) { + objectFileInfo = projectBuildDatabase->getClientCompilationUnitInfo(path); + LOG_S(WARNING) << "Can't find in target: " << path; + } else { + throw CompilationDatabaseException(e.what()); + } + } + return objectFileInfo; } diff --git a/server/src/testgens/BaseTestGen.h b/server/src/testgens/BaseTestGen.h index 9f0996521..5e87e0219 100644 --- a/server/src/testgens/BaseTestGen.h +++ b/server/src/testgens/BaseTestGen.h @@ -4,7 +4,8 @@ #include "ProjectContext.h" #include "SettingsContext.h" #include "Tests.h" -#include "building/BuildDatabase.h" +#include "building/ProjectBuildDatabase.h" +#include "building/TargetBuildDatabase.h" #include "printers/TestsPrinter.h" #include "streams/tests/TestsWriter.h" #include "stubs/Stubs.h" @@ -42,13 +43,20 @@ class BaseTestGen { virtual ~BaseTestGen() = default; - std::shared_ptr getBuildDatabase(bool forStub) const; + std::shared_ptr getProjectBuildDatabase() const; - std::shared_ptr getBuildDatabase(bool forStub); + std::shared_ptr getTargetBuildDatabase() const; + + std::shared_ptr getProjectBuildDatabase(); + + std::shared_ptr getTargetBuildDatabase(); + + std::shared_ptr + getClientCompilationUnitInfo(const fs::path &path, bool fullProject = false) const; protected: - std::shared_ptr baseBuildDatabase; - std::shared_ptr buildDatabase; + std::shared_ptr projectBuildDatabase; + std::shared_ptr targetBuildDatabase; BaseTestGen(const testsgen::ProjectContext &projectContext, const testsgen::SettingsContext &settingsContext, @@ -57,7 +65,7 @@ class BaseTestGen { void setInitializedTestsMap(); - virtual void setTargetForSource(fs::path const& sourcePath) = 0; + virtual void setTargetForSource(fs::path const &sourcePath) = 0; void updateTargetSources(fs::path _targetPath); }; diff --git a/server/src/testgens/ProjectTestGen.cpp b/server/src/testgens/ProjectTestGen.cpp index 599a352e4..c4b7f3870 100644 --- a/server/src/testgens/ProjectTestGen.cpp +++ b/server/src/testgens/ProjectTestGen.cpp @@ -16,16 +16,16 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, fs::create_directories(projectContext.testDirPath); compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( projectContext.projectPath, projectContext.buildDirRelativePath); - baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); - buildDatabase = baseBuildDatabase->createBuildDatabaseForSourceOrTarget(request.targetpath()); + projectBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); + targetBuildDatabase = TargetBuildDatabase::createForSourceOrTarget(projectBuildDatabase.get(), request.targetpath()); if (autoDetect) { autoDetectSourcePathsIfNotEmpty(); } else { - sourcePaths = buildDatabase->compilationDatabase->getAllFiles(); + sourcePaths = targetBuildDatabase->compilationDatabase->getAllFiles(); } testingMethodsSourcePaths = sourcePaths; setInitializedTestsMap(); - updateTargetSources(buildDatabase->getTargetPath()); + updateTargetSources(targetBuildDatabase->getTargetPath()); } std::string ProjectTestGen::toString() { @@ -36,7 +36,7 @@ std::string ProjectTestGen::toString() { } void ProjectTestGen::setTargetForSource(const fs::path &sourcePath) { - fs::path root = buildDatabase->getRootForSource(sourcePath); + fs::path root = targetBuildDatabase->getRootForSource(sourcePath); setTargetPath(root); } @@ -53,7 +53,7 @@ void ProjectTestGen::autoDetectSourcePathsIfNotEmpty() { // requestSourcePaths are from settings.json auto requestSourcePaths = getRequestSourcePaths(); // sourcePathsCandidates are from compile_commands.json - auto sourcePathsCandidates = buildDatabase->compilationDatabase->getAllFiles(); + auto sourcePathsCandidates = targetBuildDatabase->compilationDatabase->getAllFiles(); if (!requestSourcePaths.empty()) { sourcePaths = Paths::filterPathsByDirNames(sourcePathsCandidates, requestSourcePaths, Paths::isSourceFile); diff --git a/server/src/testgens/SnippetTestGen.cpp b/server/src/testgens/SnippetTestGen.cpp index 5bb3f7887..328af8294 100644 --- a/server/src/testgens/SnippetTestGen.cpp +++ b/server/src/testgens/SnippetTestGen.cpp @@ -18,8 +18,8 @@ SnippetTestGen::SnippetTestGen(const testsgen::SnippetRequest &request, printer::CCJsonPrinter::createDummyBuildDB(sourcePaths, serverBuildDir); compileCommandsJsonPath = serverBuildDir; utbot::ProjectContext projectContext{request, serverBuildDir}; - baseBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); - buildDatabase = baseBuildDatabase->createBuildDatabaseForSourceOrTarget(serverBuildDir / SNIPPET_TARGET); + projectBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); + targetBuildDatabase = TargetBuildDatabase::createForSourceOrTarget(projectBuildDatabase.get(), serverBuildDir / SNIPPET_TARGET); setTargetForSource(filePath); setInitializedTestsMap(); } diff --git a/server/src/utils/GenerationUtils.cpp b/server/src/utils/GenerationUtils.cpp index f481b8b65..d0343cff6 100644 --- a/server/src/utils/GenerationUtils.cpp +++ b/server/src/utils/GenerationUtils.cpp @@ -50,7 +50,7 @@ void GenerationUtils::generateCoverageAndResultsAndWriteStatus( std::optional GenerationUtils::findTarget(const BaseTestGen &baseTestGen, const std::string &name) { - return findTarget(baseTestGen.getBuildDatabase(false)->getAllTargets(), name); + return findTarget(baseTestGen.getTargetBuildDatabase()->getAllTargets(), name); } std::optional diff --git a/server/src/utils/GenerationUtils.h b/server/src/utils/GenerationUtils.h index 0f2750393..c80cc229f 100644 --- a/server/src/utils/GenerationUtils.h +++ b/server/src/utils/GenerationUtils.h @@ -44,7 +44,7 @@ namespace GenerationUtils { if (status.error_message() == FileNotPresentedInArtifactException::MESSAGE || status.error_message() == FileNotPresentedInCommandsException::MESSAGE) { fs::path path = status.error_details(); - auto targetPaths = testGen->getBuildDatabase(true)->getTargetPathsForSourceFile(path); + auto targetPaths = testGen->getProjectBuildDatabase()->getTargetPathsForSourceFile(path); LOG_S(WARNING) << "List of possible targets for current file:\n"; for (auto const& target: targetPaths) { LOG_S(WARNING) << target << "\n"; diff --git a/server/test/framework/KleeGen_Tests.cpp b/server/test/framework/KleeGen_Tests.cpp index 525dcbd59..51486f1c6 100644 --- a/server/test/framework/KleeGen_Tests.cpp +++ b/server/test/framework/KleeGen_Tests.cpp @@ -48,7 +48,7 @@ namespace { types::TypesHandler typesHandler(typeMaps, sizeContext); auto request = testUtils::createProjectRequest(testSuite.name, suitePath, buildDirRelativePath, {}); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - KleeGenerator generator(testGen, typesHandler, {}); + KleeGenerator generator(&testGen, typesHandler, {}); CollectionUtils::FileSet sources(testSuite.sourcesFilePaths.begin(), testSuite.sourcesFilePaths.end()); sources.erase(getTestFilePath("snippet.c")); @@ -65,7 +65,7 @@ namespace { types::TypesHandler typesHandler(typeMaps, sizeContext); auto request = testUtils::createProjectRequest(testSuite.name, suitePath, buildDirRelativePath, {}); auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE); - KleeGenerator generator(testGen, typesHandler, {}); + KleeGenerator generator(&testGen, typesHandler, {}); fs::path sourceFilePath = *testSuite.sourcesFilePaths.begin(); auto actualFilePath = generator.defaultBuild(sourceFilePath); diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index c65772f28..d75ad7ec2 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -76,7 +76,7 @@ namespace { fs::path serverBuildDir = buildPath / "temp"; fs::path compilerPath = CompilationUtils::getBundledCompilerPath(compilerName); CollectionUtils::FileSet stubsSources; - fs::path root = testGen.getBuildDatabase(false)->getTargetPath(); + fs::path root = testGen.getTargetBuildDatabase()->getTargetPath(); printer::TestMakefilesPrinter testMakefilePrinter(testGen, root, compilerPath, &stubsSources); testMakefilePrinter.addLinkTargetRecursively(root, ""); From 772629163f5446c97bf4eb044c20461aa1408ddf Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 18 Aug 2022 11:36:20 +0300 Subject: [PATCH 22/27] refactoring after review 4 --- server/src/building/BuildDatabase.cpp | 26 ++++++++++----------- server/src/building/BuildDatabase.h | 14 ++++++----- server/src/building/ProjectBuildDatabse.cpp | 1 - server/src/building/TargetBuildDatabase.cpp | 1 - server/src/testgens/BaseTestGen.cpp | 12 ++++------ 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 4da671115..2e8d6a08c 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -59,20 +59,17 @@ fs::path BuildDatabase::createExplicitObjectFileCompilationCommand(const std::sh } void BuildDatabase::createClangCompileCommandsJson() { - CollectionUtils::MapFileTo>> fileCompileCommands; - for (const auto &[compileCommand, objectInfo]: compileCommands_temp) { - const fs::path &sourcePath = objectInfo->getSourcePath(); - if (CollectionUtils::contains(fileCompileCommands, sourcePath)) { - LOG_S(WARNING) << "Multiple compile commands for file \"" << sourcePath - << "\" use command for \"" << objectInfo->getOutputFile() << "\""; - } else if (CollectionUtils::contains(objectFileInfos, objectInfo->getOutputFile())) { - fileCompileCommands[sourcePath] = {compileCommand, objectInfo}; - } + CollectionUtils::MapFileTo fileCompileCommands; + for (const auto &[sourcePath, objectInfos]: sourceFileInfos) { + const std::shared_ptr &objectInfo = objectInfos.front(); + fileCompileCommands[sourcePath] = {{"directory", objectInfo->command.getDirectory()}, + {"command", objectInfo->command.toString()}, + {"file", objectInfo->command.getSourcePath()}}; } nlohmann::json compileCommandsSingleFilesJson; for (const auto &compileCommand: fileCompileCommands) { - compileCommandsSingleFilesJson.push_back(compileCommand.second.first); + compileCommandsSingleFilesJson.push_back(compileCommand.second); } fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath); @@ -334,6 +331,11 @@ BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const { throw CompilationDatabaseException("File is not a compilation unit or an object file: " + filepath.string()); } + +[[nodiscard]] bool BuildDatabase::hasUnitInfo(const fs::path &filepath) const { + return CollectionUtils::contains(sourceFileInfos, filepath) || CollectionUtils::contains(objectFileInfos, filepath); +} + std::shared_ptr BuildDatabase::getClientLinkUnitInfo(const fs::path &filepath) const { if (Paths::isSourceFile(filepath)) { auto compilationInfo = getClientCompilationUnitInfo(filepath); @@ -543,7 +545,3 @@ CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path & std::shared_ptr BuildDatabase::getTargetInfo(const fs::path &_target) { return targetInfos[_target]; } - -std::vector>> BuildDatabase::getCompileCommands_temp() { - return compileCommands_temp; -} diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 134e059c9..52ac0f647 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -158,6 +158,14 @@ class BuildDatabase { */ [[nodiscard]] std::shared_ptr getClientCompilationUnitInfo(const fs::path &filepath) const; + /** + * @brief Returns true if BuildDatabase contains information for current source file or object file + * + * @param filepath Path to source file or object file + * @return true if contains current source file or object file + */ + [[nodiscard]] bool hasUnitInfo(const fs::path &filepath) const; + /** * @brief Returns link command information for current executable or library * @@ -220,9 +228,6 @@ class BuildDatabase { std::shared_ptr getTargetInfo(const fs::path &_target); - //TODO change - std::vector>> getCompileCommands_temp(); - virtual bool hasAutoTarget() const = 0; virtual fs::path getTargetPath() const = 0; @@ -240,9 +245,6 @@ class BuildDatabase { CollectionUtils::MapFileTo> targetInfos; CollectionUtils::MapFileTo> objectFileTargets; - //TODO change - std::vector>> compileCommands_temp; - BuildDatabase( fs::path serverBuildDir, fs::path buildCommandsJsonPath, diff --git a/server/src/building/ProjectBuildDatabse.cpp b/server/src/building/ProjectBuildDatabse.cpp index 807bc0dcf..b9826d7ac 100644 --- a/server/src/building/ProjectBuildDatabse.cpp +++ b/server/src/building/ProjectBuildDatabse.cpp @@ -118,7 +118,6 @@ void ProjectBuildDatabase::initObjects(const nlohmann::json &compileCommandsJson } else { objectFileInfos[outputFile] = objectInfo; } - compileCommands_temp.emplace_back(compileCommand, objectInfo); const fs::path &sourcePath = objectInfo->getSourcePath(); sourceFileInfos[sourcePath].emplace_back(objectInfo); } diff --git a/server/src/building/TargetBuildDatabase.cpp b/server/src/building/TargetBuildDatabase.cpp index 6ae7f2031..61bcb8bbf 100644 --- a/server/src/building/TargetBuildDatabase.cpp +++ b/server/src/building/TargetBuildDatabase.cpp @@ -30,7 +30,6 @@ TargetBuildDatabase::TargetBuildDatabase(BuildDatabase *baseBuildDatabase, } } - compileCommands_temp = baseBuildDatabase->getCompileCommands_temp(); createClangCompileCommandsJson(); } diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index d9daf6aaa..16f3c98d6 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -81,15 +81,11 @@ std::shared_ptr BaseTestGen::getTargetBuildDatabase() { std::shared_ptr BaseTestGen::getClientCompilationUnitInfo(const fs::path &path, bool fullProject) const { std::shared_ptr objectFileInfo; - try { + if (targetBuildDatabase->hasUnitInfo(path) || !fullProject) { objectFileInfo = targetBuildDatabase->getClientCompilationUnitInfo(path); - } catch (CompilationDatabaseException &e) { - if (fullProject) { - objectFileInfo = projectBuildDatabase->getClientCompilationUnitInfo(path); - LOG_S(WARNING) << "Can't find in target: " << path; - } else { - throw CompilationDatabaseException(e.what()); - } + } else { + objectFileInfo = projectBuildDatabase->getClientCompilationUnitInfo(path); + LOG_S(WARNING) << "Can't find in target: " << path; } return objectFileInfo; } From dfdfd3b3d9f8d78ff0a745d17a79983d98bb2cbf Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 18 Aug 2022 14:41:26 +0300 Subject: [PATCH 23/27] refactoring after review 5 --- server/src/KleeGenerator.cpp | 4 +- server/src/KleeGenerator.h | 4 +- server/src/building/BaseCommand.cpp | 14 ++---- server/src/building/BaseCommand.h | 8 ++-- server/src/building/CompileCommand.cpp | 3 -- server/src/building/LinkCommand.cpp | 37 +++++++------- server/src/building/LinkCommand.h | 2 + server/src/building/Linker.cpp | 2 +- server/src/building/Linker.h | 2 +- server/src/printers/NativeMakefilePrinter.cpp | 48 +++++++++---------- server/src/printers/NativeMakefilePrinter.h | 4 +- server/src/printers/TestMakefilesPrinter.cpp | 16 +++---- server/src/printers/TestMakefilesPrinter.h | 6 +-- server/test/framework/Server_Tests.cpp | 2 +- 14 files changed, 70 insertions(+), 82 deletions(-) diff --git a/server/src/KleeGenerator.cpp b/server/src/KleeGenerator.cpp index f04323052..e8c805bb4 100644 --- a/server/src/KleeGenerator.cpp +++ b/server/src/KleeGenerator.cpp @@ -19,9 +19,9 @@ using namespace tests; static const std::string GENERATION_COMPILE_MAKEFILE = "GenerationCompileMakefile.mk"; static const std::string GENERATION_KLEE_MAKEFILE = "GenerationKleeMakefile.mk"; -KleeGenerator::KleeGenerator(BaseTestGen *_testGen, types::TypesHandler &typesHandler, +KleeGenerator::KleeGenerator(BaseTestGen *testGen, types::TypesHandler &typesHandler, PathSubstitution filePathsSubstitution) - : testGen(_testGen), typesHandler(typesHandler), + : testGen(testGen), typesHandler(typesHandler), pathSubstitution(std::move(filePathsSubstitution)) { try { fs::create_directories(this->testGen->serverBuildDir); diff --git a/server/src/KleeGenerator.h b/server/src/KleeGenerator.h index db9af7440..0b3b4647b 100644 --- a/server/src/KleeGenerator.h +++ b/server/src/KleeGenerator.h @@ -35,7 +35,7 @@ class KleeGenerator { public: /** * @brief Also creates tmp directories for build files. - * @param _testGen contains context for current request. + * @param testGen contains request and build information . * @param typesHandler provides additional information about types. * @param filePathsSubstitution Mapping from source file path to modified file. Required for * line test generation requests. @@ -43,7 +43,7 @@ class KleeGenerator { * @throws fs::filesystem_error Thrown if it can't create tmp folder for some * reasons. */ - KleeGenerator(BaseTestGen *_testGen, types::TypesHandler &typesHandler, + KleeGenerator(BaseTestGen *testGen, types::TypesHandler &typesHandler, PathSubstitution filePathsSubstitution); struct BuildFileInfo { diff --git a/server/src/building/BaseCommand.cpp b/server/src/building/BaseCommand.cpp index f14d048a9..0e9a70574 100644 --- a/server/src/building/BaseCommand.cpp +++ b/server/src/building/BaseCommand.cpp @@ -20,20 +20,18 @@ namespace utbot { BaseCommand::BaseCommand(std::list commandLine, fs::path directory, bool shouldChangeDirectory) : commandLine(std::move(commandLine)), directory(std::move(directory)), shouldChangeDirectory{shouldChangeDirectory} { initOptimizationLevel(); - initBuildTool(); initOutput(); } + BaseCommand::BaseCommand(std::vector commandLine, fs::path directory, bool shouldChangeDirectory) : commandLine(commandLine.begin(), commandLine.end()), directory(std::move(directory)), shouldChangeDirectory{shouldChangeDirectory} { initOptimizationLevel(); - initBuildTool(); initOutput(); } BaseCommand::BaseCommand(BaseCommand const &other) : directory(other.directory), commandLine(other.commandLine), environmentVariables(other.environmentVariables), shouldChangeDirectory(other.shouldChangeDirectory), - buildTool(other.buildTool), output(other.output) { if (other.optimizationLevel.has_value()) { optimizationLevel = @@ -47,7 +45,6 @@ namespace utbot { : directory(std::move(other.directory)), commandLine(std::move(other.commandLine)), environmentVariables(std::move(other.environmentVariables)), optimizationLevel(other.optimizationLevel), - buildTool(other.buildTool), output(other.output), shouldChangeDirectory(other.shouldChangeDirectory) { } @@ -59,11 +56,6 @@ namespace utbot { } } - void BaseCommand::initBuildTool() { - auto it = commandLine.begin(); - buildTool = it; - } - void BaseCommand::initOutput() { auto it = findOutput(); if (it != commandLine.end()) { @@ -152,11 +144,11 @@ namespace utbot { } fs::path BaseCommand::getBuildTool() const { - return *buildTool; + return *commandLine.begin(); } void BaseCommand::setBuildTool(fs::path buildTool) { - *(this->buildTool) = std::move(buildTool); + *(commandLine.begin()) = std::move(buildTool); } fs::path BaseCommand::getOutput() const { diff --git a/server/src/building/BaseCommand.h b/server/src/building/BaseCommand.h index dcc0e409c..14ca850b5 100644 --- a/server/src/building/BaseCommand.h +++ b/server/src/building/BaseCommand.h @@ -14,6 +14,9 @@ namespace utbot { class BaseCommand { + private: + void initOutput(); + protected: bool shouldChangeDirectory = false; fs::path directory; @@ -23,17 +26,12 @@ namespace utbot { using iterator = decltype(commandLine)::iterator; using const_iterator = decltype(commandLine)::const_iterator; - iterator buildTool; iterator output; std::optional optimizationLevel; void initOptimizationLevel(); - void initBuildTool(); - - void initOutput(); - [[nodiscard]] iterator findOutput(); iterator findOptimizationLevelFlag(); diff --git a/server/src/building/CompileCommand.cpp b/server/src/building/CompileCommand.cpp index f0ad99bbc..f21c007c3 100644 --- a/server/src/building/CompileCommand.cpp +++ b/server/src/building/CompileCommand.cpp @@ -12,7 +12,6 @@ namespace utbot { CompileCommand::CompileCommand(CompileCommand const &other) : BaseCommand(other) { - buildTool = commandLine.begin(); sourcePath = std::next(commandLine.begin(), std::distance(other.commandLine.begin(), other.sourcePath)); @@ -51,7 +50,6 @@ namespace utbot { fs::path directory, fs::path sourcePath) : BaseCommand(std::move(arguments), std::move(directory)) { - buildTool = commandLine.begin(); { auto it = std::find_if(commandLine.begin(), commandLine.end(), [&sourcePath](std::string const &arg) { return fs::path(arg).filename() == sourcePath.filename(); @@ -78,7 +76,6 @@ namespace utbot { std::swap(a.optimizationLevel, b.optimizationLevel); std::swap(a.sourcePath, b.sourcePath); - std::swap(a.buildTool, b.buildTool); std::swap(a.output, b.output); } diff --git a/server/src/building/LinkCommand.cpp b/server/src/building/LinkCommand.cpp index 302a7db8f..bc2f88a2a 100644 --- a/server/src/building/LinkCommand.cpp +++ b/server/src/building/LinkCommand.cpp @@ -11,11 +11,10 @@ namespace utbot { LinkCommand::LinkCommand(LinkCommand const &other) : BaseCommand(other) { - buildTool = commandLine.begin(); output = std::next(commandLine.begin(), std::distance(other.commandLine.begin(), other.output)); } - LinkCommand::LinkCommand(LinkCommand &&other) noexcept : BaseCommand(std::move(other)) { + LinkCommand::LinkCommand(LinkCommand &&other) noexcept: BaseCommand(std::move(other)) { } LinkCommand &LinkCommand::operator=(const LinkCommand &other) { @@ -38,22 +37,7 @@ namespace utbot { LinkCommand::LinkCommand(std::list arguments, fs::path directory, bool shouldChangeDirectory) : BaseCommand(std::move(arguments), std::move(directory), shouldChangeDirectory) { - buildTool = commandLine.begin(); - { - auto it = findOutput(); - if (it != commandLine.end()) { - this->output = it; - *this->output = Paths::getCCJsonFileFullPath(*it, this->directory); - } else if (isArchiveCommand()) { - auto it = std::find_if(commandLine.begin(), commandLine.end(), [](const std::string &argument) { - return Paths::isStaticLibraryFile(argument); - }); - this->output = std::next(addFlagsBeforeIterator({"-o"}, it)); - } else { - auto path = this->directory / "a.out"; - this->output = std::next(addFlagsToBegin({"-o", path})); - } - } + initOutput(); } LinkCommand::LinkCommand(std::vector commandLine, fs::path directory, bool shouldChangeDirectory) @@ -74,7 +58,6 @@ namespace utbot { std::swap(a.environmentVariables, b.environmentVariables); std::swap(a.optimizationLevel, b.optimizationLevel); - std::swap(a.buildTool, b.buildTool); std::swap(a.output, b.output); } @@ -85,4 +68,20 @@ namespace utbot { bool LinkCommand::isSharedLibraryCommand() const { return CollectionUtils::contains(commandLine, "-shared"); } + + void LinkCommand::initOutput() { + auto it = findOutput(); + if (it != commandLine.end()) { + this->output = it; + *this->output = Paths::getCCJsonFileFullPath(*it, this->directory); + } else if (isArchiveCommand()) { + it = std::find_if(commandLine.begin(), commandLine.end(), [](const std::string &argument) { + return Paths::isStaticLibraryFile(argument); + }); + this->output = std::next(addFlagsBeforeIterator({"-o"}, it)); + } else { + auto path = this->directory / "a.out"; + this->output = std::next(addFlagsToBegin({"-o", path})); + } + } } diff --git a/server/src/building/LinkCommand.h b/server/src/building/LinkCommand.h index 72be77445..8d835bd5f 100644 --- a/server/src/building/LinkCommand.h +++ b/server/src/building/LinkCommand.h @@ -9,6 +9,8 @@ namespace utbot { class LinkCommand : public BaseCommand { + private: + void initOutput(); public: LinkCommand() = default; diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 9fd10c87a..7f5374e94 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -345,7 +345,7 @@ Result Linker::link(const CollectionUtils::MapFileTo #include #include #include #include -#include class Linker { public: diff --git a/server/src/printers/NativeMakefilePrinter.cpp b/server/src/printers/NativeMakefilePrinter.cpp index ac42a32f5..e4c68b4c3 100644 --- a/server/src/printers/NativeMakefilePrinter.cpp +++ b/server/src/printers/NativeMakefilePrinter.cpp @@ -116,7 +116,7 @@ namespace printer { } NativeMakefilePrinter::NativeMakefilePrinter( - const BaseTestGen& testGen, + const BaseTestGen* testGen, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources, @@ -135,7 +135,7 @@ namespace printer { CompilationUtils::getCoverageLinkFlags(primaryCxxCompilerName), " ")), sanitizerLinkFlags(SanitizerUtils::getSanitizeLinkFlags(primaryCxxCompilerName)), - buildDirectory(Paths::getUtbotBuildDir(testGen.projectContext)), + buildDirectory(Paths::getUtbotBuildDir(testGen->projectContext)), dependencyDirectory(buildDirectory / "dependencies"), stubSources(stubSources) { @@ -164,13 +164,13 @@ namespace printer { } fs::path NativeMakefilePrinter::getTemporaryDependencyFile(fs::path const &file) { - fs::path relativePath = fs::relative(file, testGen.projectContext.projectPath); + fs::path relativePath = fs::relative(file, testGen->projectContext.projectPath); return getRelativePath(dependencyDirectory) / Paths::addExtension(relativePath, ".Td"); } fs::path NativeMakefilePrinter::getDependencyFile(fs::path const &file) { - fs::path relativePath = fs::relative(file, testGen.projectContext.projectPath); + fs::path relativePath = fs::relative(file, testGen->projectContext.projectPath); return getRelativePath(dependencyDirectory) / Paths::addExtension(relativePath, ".d"); } @@ -270,7 +270,7 @@ namespace printer { BuildResult NativeMakefilePrinter::addObjectFile(const fs::path &objectFile, const std::string &suffixForParentOfStubs) { - auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(objectFile); + auto compilationUnitInfo = testGen->getClientCompilationUnitInfo(objectFile); fs::path sourcePath = compilationUnitInfo->getSourcePath(); fs::path pathToCompile; @@ -278,17 +278,17 @@ namespace printer { BuildResult::Type buildResultType; BuildResult buildResult; if (CollectionUtils::contains(*stubSources, sourcePath)) { - pathToCompile = Paths::sourcePathToStubPath(testGen.projectContext, sourcePath); - recompiledFile = Paths::getRecompiledFile(testGen.projectContext, pathToCompile); + pathToCompile = Paths::sourcePathToStubPath(testGen->projectContext, sourcePath); + recompiledFile = Paths::getRecompiledFile(testGen->projectContext, pathToCompile); buildResultType = BuildResult::Type::ALL_STUBS; } else { if (Paths::isCXXFile(sourcePath)) { pathToCompile = sourcePath; } else { - pathToCompile = Paths::getWrapperFilePath(testGen.projectContext, sourcePath); + pathToCompile = Paths::getWrapperFilePath(testGen->projectContext, sourcePath); } recompiledFile = - Paths::getRecompiledFile(testGen.projectContext, compilationUnitInfo->getOutputFile()); + Paths::getRecompiledFile(testGen->projectContext, compilationUnitInfo->getOutputFile()); buildResultType = BuildResult::Type::NO_STUBS; } @@ -299,7 +299,7 @@ namespace printer { } void NativeMakefilePrinter::addTestTarget(const fs::path &sourcePath) { - auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(sourcePath); + auto compilationUnitInfo = testGen->getClientCompilationUnitInfo(sourcePath); auto testCompilationCommand = compilationUnitInfo->command; testCompilationCommand.setBuildTool(getRelativePathForLinker(primaryCxxCompiler)); testCompilationCommand.setOptimizationLevel(OPTIMIZATION_FLAG); @@ -314,10 +314,10 @@ namespace printer { testCompilationCommand.addFlagToBegin(FPIC_FLAG); testCompilationCommand.addFlagsToBegin(SANITIZER_NEEDED_FLAGS); - fs::path testSourcePath = Paths::sourcePathToTestPath(testGen.projectContext, sourcePath); + fs::path testSourcePath = Paths::sourcePathToTestPath(testGen->projectContext, sourcePath); fs::path compilationDirectory = compilationUnitInfo->getDirectory(); - fs::path testObjectDir = Paths::getTestObjectDir(testGen.projectContext); - fs::path testSourceRelativePath = fs::relative(testSourcePath, testGen.projectContext.testDirPath); + fs::path testObjectDir = Paths::getTestObjectDir(testGen->projectContext); + fs::path testSourceRelativePath = fs::relative(testSourcePath, testGen->projectContext.testDirPath); fs::path testObjectPathRelative = getRelativePath( testObjectDir / Paths::addExtension(testSourceRelativePath, ".o")); testCompilationCommand.setOutput( @@ -332,7 +332,7 @@ namespace printer { artifacts.push_back(testCompilationCommand.getOutput()); - auto rootLinkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath); + auto rootLinkUnitInfo = testGen->getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath); fs::path testExecutablePath = getTestExecutablePath(sourcePath); std::vector filesToLink{ "$(GTEST_MAIN)", "$(GTEST_ALL)", testCompilationCommand.getOutput(), @@ -399,7 +399,7 @@ namespace printer { } fs::path NativeMakefilePrinter::getTestExecutablePath(const fs::path &sourcePath) const { return Paths::removeExtension( - Paths::removeExtension(Paths::getRecompiledFile(testGen.projectContext, sourcePath))); + Paths::removeExtension(Paths::getRecompiledFile(testGen->projectContext, sourcePath))); } NativeMakefilePrinter::NativeMakefilePrinter(const NativeMakefilePrinter &baseMakefilePrinter, @@ -426,7 +426,7 @@ namespace printer { fs::path testExecutablePath = getTestExecutablePath(sourcePath); - auto rootLinkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath); + auto rootLinkUnitInfo = testGen->getTargetBuildDatabase()->getClientLinkUnitInfo(rootPath); fs::path coverageInfoBinary = sharedOutput.value(); if (!Paths::isLibraryFile(coverageInfoBinary)) { @@ -468,7 +468,7 @@ namespace printer { return buildResults[unitFile] = buildResult; } - auto linkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(unitFile); + auto linkUnitInfo = testGen->getTargetBuildDatabase()->getClientLinkUnitInfo(unitFile); BuildResult::Type unitType = BuildResult::Type::NONE; CollectionUtils::MapFileTo fileMapping; auto unitBuildResults = CollectionUtils::transformTo>( @@ -489,7 +489,7 @@ namespace printer { bool isExecutable = !Paths::isLibraryFile(unitFile); fs::path recompiledFile = - Paths::getRecompiledFile(testGen.projectContext, linkUnitInfo->getOutput()); + Paths::getRecompiledFile(testGen->projectContext, linkUnitInfo->getOutput()); if (isExecutable && !transformExeToLib) { recompiledFile = Paths::isObjectFile(recompiledFile) ? recompiledFile : Paths::addExtension(recompiledFile, ".o"); @@ -529,9 +529,9 @@ namespace printer { getLibraryAbsolutePath(argument, linkCommand.getDirectory()); if (optionalLibraryAbsolutePath.has_value()) { const fs::path &absolutePath = optionalLibraryAbsolutePath.value(); - if (Paths::isSubPathOf(testGen.projectContext.buildDir(), absolutePath)) { + if (Paths::isSubPathOf(testGen->projectContext.buildDir(), absolutePath)) { fs::path recompiledDir = - Paths::getRecompiledFile(testGen.projectContext, absolutePath); + Paths::getRecompiledFile(testGen->projectContext, absolutePath); std::string directoryFlag = getLibraryDirectoryFlag(recompiledDir); libraryDirectoriesFlags.push_back(directoryFlag); } @@ -613,11 +613,11 @@ namespace printer { void NativeMakefilePrinter::addStubs(const CollectionUtils::FileSet &stubsSet) { auto stubObjectFiles = CollectionUtils::transformTo( Synchronizer::dropHeaders(stubsSet), [this](fs::path const &stub) { - fs::path sourcePath = Paths::stubPathToSourcePath(testGen.projectContext, stub); + fs::path sourcePath = Paths::stubPathToSourcePath(testGen->projectContext, stub); fs::path stubBuildFilePath = - Paths::getStubBuildFilePath(testGen.projectContext, sourcePath); - auto compilationUnitInfo = testGen.getClientCompilationUnitInfo(sourcePath, true); - fs::path target = Paths::getRecompiledFile(testGen.projectContext, stub); + Paths::getStubBuildFilePath(testGen->projectContext, sourcePath); + auto compilationUnitInfo = testGen->getClientCompilationUnitInfo(sourcePath, true); + fs::path target = Paths::getRecompiledFile(testGen->projectContext, stub); addCompileTarget(stub, target, *compilationUnitInfo); return target; }); diff --git a/server/src/printers/NativeMakefilePrinter.h b/server/src/printers/NativeMakefilePrinter.h index cf379d80b..2052617f4 100644 --- a/server/src/printers/NativeMakefilePrinter.h +++ b/server/src/printers/NativeMakefilePrinter.h @@ -15,7 +15,7 @@ namespace printer { class NativeMakefilePrinter : public RelativeMakefilePrinter { friend class TestMakefilesPrinter; private: - const BaseTestGen& testGen; + const BaseTestGen* testGen; fs::path rootPath; fs::path primaryCompiler; @@ -66,7 +66,7 @@ namespace printer { bool transformExeToLib); public: - NativeMakefilePrinter(const BaseTestGen& testGen, + NativeMakefilePrinter(const BaseTestGen* testGen, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources, diff --git a/server/src/printers/TestMakefilesPrinter.cpp b/server/src/printers/TestMakefilesPrinter.cpp index eda546078..88a23fbb3 100644 --- a/server/src/printers/TestMakefilesPrinter.cpp +++ b/server/src/printers/TestMakefilesPrinter.cpp @@ -30,25 +30,25 @@ namespace printer { sharedMakefileContent(std::move(sharedMakefileStr)), objMakefileContent(std::move(objMakefileStr)) { } - TestMakefilesPrinter::TestMakefilesPrinter(const BaseTestGen &testGen, + TestMakefilesPrinter::TestMakefilesPrinter(const BaseTestGen *testGen, CollectionUtils::FileSet const *stubSources) : TestMakefilesPrinter( testGen, - testGen.getTargetBuildDatabase()->getTargetPath(), + testGen->getTargetBuildDatabase()->getTargetPath(), CompilationUtils::getBundledCompilerPath(CompilationUtils::getCompilerName( - testGen.getTargetBuildDatabase()->compilationDatabase->getBuildCompilerPath())), + testGen->getTargetBuildDatabase()->compilationDatabase->getBuildCompilerPath())), stubSources) { } TestMakefilesPrinter::TestMakefilesPrinter( - const BaseTestGen &testGen, + const BaseTestGen *testGen, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources) : - RelativeMakefilePrinter(Paths::getUtbotBuildDir(testGen.projectContext), - Paths::getRelativeUtbotBuildDir(testGen.projectContext), - testGen.projectContext.projectPath), - projectContext(testGen.projectContext), + RelativeMakefilePrinter(Paths::getUtbotBuildDir(testGen->projectContext), + Paths::getRelativeUtbotBuildDir(testGen->projectContext), + testGen->projectContext.projectPath), + projectContext(testGen->projectContext), sharedMakefilePrinter(testGen, rootPath, primaryCompiler, stubSources, pathToShellVariable), objMakefilePrinter(testGen, rootPath, primaryCompiler, stubSources, pathToShellVariable) { } diff --git a/server/src/printers/TestMakefilesPrinter.h b/server/src/printers/TestMakefilesPrinter.h index 4e9c57d6d..535a0e5da 100644 --- a/server/src/printers/TestMakefilesPrinter.h +++ b/server/src/printers/TestMakefilesPrinter.h @@ -19,18 +19,18 @@ namespace printer { void write() const; }; - class TestMakefilesPrinter: public RelativeMakefilePrinter { + class TestMakefilesPrinter : public RelativeMakefilePrinter { private: utbot::ProjectContext projectContext; printer::NativeMakefilePrinter sharedMakefilePrinter; printer::NativeMakefilePrinter objMakefilePrinter; public: - TestMakefilesPrinter(const BaseTestGen &testGen, + TestMakefilesPrinter(const BaseTestGen *testGen, CollectionUtils::FileSet const *stubSources); TestMakefilesPrinter( - const BaseTestGen &testGen, + const BaseTestGen *testGen, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources); diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index d75ad7ec2..6982d0d01 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -77,7 +77,7 @@ namespace { fs::path compilerPath = CompilationUtils::getBundledCompilerPath(compilerName); CollectionUtils::FileSet stubsSources; fs::path root = testGen.getTargetBuildDatabase()->getTargetPath(); - printer::TestMakefilesPrinter testMakefilePrinter(testGen, root, compilerPath, &stubsSources); + printer::TestMakefilesPrinter testMakefilePrinter(&testGen, root, compilerPath, &stubsSources); testMakefilePrinter.addLinkTargetRecursively(root, ""); testMakefilePrinter.GetMakefiles(sourceFile).write(); From 2ef7b8e902323eb71b4f3f74f7dc439491ee7339 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 18 Aug 2022 15:12:17 +0300 Subject: [PATCH 24/27] refactoring after review 6 --- server/src/KleeGenerator.h | 2 +- server/src/building/CompileCommand.cpp | 24 +++++++++++++--------- server/src/building/CompileCommand.h | 2 ++ server/src/building/ProjectBuildDatabase.h | 13 ++++++------ server/test/framework/TestUtils.cpp | 16 +++++++-------- server/test/framework/TestUtils.h | 8 ++++---- server/test/framework/main.cpp | 2 +- 7 files changed, 37 insertions(+), 30 deletions(-) diff --git a/server/src/KleeGenerator.h b/server/src/KleeGenerator.h index 0b3b4647b..3134ef8c0 100644 --- a/server/src/KleeGenerator.h +++ b/server/src/KleeGenerator.h @@ -35,7 +35,7 @@ class KleeGenerator { public: /** * @brief Also creates tmp directories for build files. - * @param testGen contains request and build information . + * @param testGen contains request and build information. * @param typesHandler provides additional information about types. * @param filePathsSubstitution Mapping from source file path to modified file. Required for * line test generation requests. diff --git a/server/src/building/CompileCommand.cpp b/server/src/building/CompileCommand.cpp index f21c007c3..84482a256 100644 --- a/server/src/building/CompileCommand.cpp +++ b/server/src/building/CompileCommand.cpp @@ -57,16 +57,7 @@ namespace utbot { this->sourcePath = it; *this->sourcePath = sourcePath; } - { - auto it = findOutput(); - if (it != commandLine.end()) { - this->output = it; - *this->output = Paths::getCCJsonFileFullPath(*it, this->directory); - } else { - auto path = Paths::getCCJsonFileFullPath(Paths::replaceExtension(*this->sourcePath, ".o"), this->directory); - this->output = std::next(addFlagsToBegin({ "-o", path })); - } - } + initOutput(); } void swap(CompileCommand &a, CompileCommand &b) noexcept { @@ -107,9 +98,22 @@ namespace utbot { return StringUtils::startsWith(arg, "-I"); }); } + void CompileCommand::removeWerror() { CollectionUtils::erase_if(commandLine, [](const std::string &arg) { return StringUtils::startsWith(arg, "-Werror"); }); } + + void CompileCommand::initOutput() { + auto it = findOutput(); + if (it != commandLine.end()) { + this->output = it; + *this->output = Paths::getCCJsonFileFullPath(*it, this->directory); + } else { + auto path = Paths::getCCJsonFileFullPath(Paths::replaceExtension(*this->sourcePath, ".o"), this->directory); + this->output = std::next(addFlagsToBegin({"-o", path})); + } + + } } diff --git a/server/src/building/CompileCommand.h b/server/src/building/CompileCommand.h index 85e4b7bea..9e696a1d4 100644 --- a/server/src/building/CompileCommand.h +++ b/server/src/building/CompileCommand.h @@ -16,6 +16,8 @@ namespace utbot { private: iterator sourcePath; + void initOutput(); + public: CompileCommand() = default; diff --git a/server/src/building/ProjectBuildDatabase.h b/server/src/building/ProjectBuildDatabase.h index 1e0b803c0..a7909cbaf 100644 --- a/server/src/building/ProjectBuildDatabase.h +++ b/server/src/building/ProjectBuildDatabase.h @@ -4,12 +4,7 @@ #include "BuildDatabase.h" class ProjectBuildDatabase : public BuildDatabase { -public: - ProjectBuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, - utbot::ProjectContext _projectContext); - - static std::shared_ptr create(const utbot::ProjectContext &projectContext); - +private: void initObjects(const nlohmann::json &compileCommandsJson); void initInfo(const nlohmann::json &linkCommandsJson); @@ -20,6 +15,12 @@ class ProjectBuildDatabase : public BuildDatabase { void fillTargetInfoParents(); +public: + ProjectBuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, + utbot::ProjectContext _projectContext); + + static std::shared_ptr create(const utbot::ProjectContext &projectContext); + bool hasAutoTarget() const override; fs::path getTargetPath() const override; diff --git a/server/test/framework/TestUtils.cpp b/server/test/framework/TestUtils.cpp index 7e11521f1..c9965e7a8 100644 --- a/server/test/framework/TestUtils.cpp +++ b/server/test/framework/TestUtils.cpp @@ -212,7 +212,7 @@ namespace testUtils { const fs::path &projectPath, const std::string &buildDirRelativePath, const std::vector &srcPaths, - const std::string &target, + const std::string &targetOrSourcePath, bool useStubs, bool verbose, int kleeTimeout) { @@ -224,7 +224,7 @@ namespace testUtils { return GrpcUtils::createProjectRequest(std::move(projectContext), std::move(settingsContext), srcPaths, - target); + targetOrSourcePath); } std::unique_ptr createFileRequest(const std::string &projectName, @@ -232,12 +232,12 @@ namespace testUtils { const std::string &buildDirRelativePath, const std::vector &srcPaths, const fs::path &filePath, - const std::string &target, + const std::string &targetOrSourcePath, bool useStubs, bool verbose, int kleeTimeout) { auto projectRequest = createProjectRequest(projectName, projectPath, buildDirRelativePath, - srcPaths, target, useStubs, verbose, kleeTimeout); + srcPaths, targetOrSourcePath, useStubs, verbose, kleeTimeout); return GrpcUtils::createFileRequest(std::move(projectRequest), filePath); } @@ -247,12 +247,12 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, - const std::string &target, + const std::string &targetOrSourcePath, bool useStubs, bool verbose, int kleeTimeout) { auto projectRequest = createProjectRequest(projectName, projectPath, buildDirRelativePath, - srcPaths, target, useStubs, verbose, kleeTimeout); + srcPaths, targetOrSourcePath, useStubs, verbose, kleeTimeout); auto lineInfo = GrpcUtils::createSourceInfo(filePath, line); return GrpcUtils::createLineRequest(std::move(projectRequest), std::move(lineInfo)); } @@ -263,12 +263,12 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, - const std::string &target, + const std::string &targetOrSourcePath, bool useStubs, bool verbose, int kleeTimeout) { auto lineRequest = createLineRequest(projectName, projectPath, buildDirRelativePath, - srcPaths, filePath, line, target, useStubs, verbose, kleeTimeout); + srcPaths, filePath, line, targetOrSourcePath, useStubs, verbose, kleeTimeout); return GrpcUtils::createClassRequest(std::move(lineRequest)); } diff --git a/server/test/framework/TestUtils.h b/server/test/framework/TestUtils.h index 86effaa28..e037a1ffd 100644 --- a/server/test/framework/TestUtils.h +++ b/server/test/framework/TestUtils.h @@ -68,7 +68,7 @@ namespace testUtils { const fs::path &projectPath, const std::string &buildDirRelativePath, const std::vector &srcPaths, - const std::string &target = GrpcUtils::UTBOT_AUTO_TARGET_PATH, + const std::string &targetOrSourcePath = GrpcUtils::UTBOT_AUTO_TARGET_PATH, bool useStubs = false, bool verbose = true, int kleeTimeout = 60); @@ -78,7 +78,7 @@ namespace testUtils { const std::string &buildDirRelativePath, const std::vector &srcPaths, const fs::path &filePath, - const std::string &target = GrpcUtils::UTBOT_AUTO_TARGET_PATH, + const std::string &targetOrSourcePath = GrpcUtils::UTBOT_AUTO_TARGET_PATH, bool useStubs = false, bool verbose = true, int kleeTimeout = 60); @@ -89,7 +89,7 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, - const std::string &target = GrpcUtils::UTBOT_AUTO_TARGET_PATH, + const std::string &targetOrSourcePath = GrpcUtils::UTBOT_AUTO_TARGET_PATH, bool useStubs = false, bool verbose = true, int kleeTimeout = 60); @@ -100,7 +100,7 @@ namespace testUtils { const std::vector &srcPaths, const fs::path &filePath, int line, - const std::string &target = GrpcUtils::UTBOT_AUTO_TARGET_PATH, + const std::string &targetOrSourcePath = GrpcUtils::UTBOT_AUTO_TARGET_PATH, bool useStubs = false, bool verbose = true, int kleeTimeout = 60); diff --git a/server/test/framework/main.cpp b/server/test/framework/main.cpp index 997b50eb3..6e900c9e0 100644 --- a/server/test/framework/main.cpp +++ b/server/test/framework/main.cpp @@ -1,10 +1,10 @@ #include "TestUtils.h" #include "utils/CLIUtils.h" +#include "printers/DefaultMakefilePrinter.h" #include "loguru.h" #include -#include "printers/DefaultMakefilePrinter.h" //Usage: ./UTBot_UnitTests [--verbosity trace|debug|info|warning|error] int main(int argc, char **argv) { From 65386a854fd63e93f16302d56328e5a0713138a7 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 18 Aug 2022 15:41:43 +0300 Subject: [PATCH 25/27] refactoring after review 7 --- server/src/building/BuildDatabase.cpp | 1 - server/src/building/BuildDatabase.h | 10 +++------- server/src/building/ProjectBuildDatabase.h | 4 ---- server/src/building/ProjectBuildDatabse.cpp | 9 --------- server/src/building/TargetBuildDatabase.cpp | 7 +++++++ server/src/building/TargetBuildDatabase.h | 6 ++++-- 6 files changed, 14 insertions(+), 23 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 2e8d6a08c..487ba0ee6 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -534,7 +534,6 @@ fs::path BuildDatabase::newDirForFile(const fs::path &file) const { } CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path &_target) { - LOG_IF_S(WARNING, !hasAutoTarget() && getTargetPath() != _target.c_str()) << "Try get sources for different target"; return CollectionUtils::transformTo( getArchiveObjectFiles(_target), [this](fs::path const &objectPath) { diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 52ac0f647..8c17f1d92 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -212,11 +212,11 @@ class BuildDatabase { */ std::vector> getAllCompileCommands() const; - std::vector> getRootTargets() const; + virtual std::vector> getRootTargets() const; - std::vector> getAllTargets() const; + virtual std::vector> getAllTargets() const; - std::vector getAllTargetPaths() const; + virtual std::vector getAllTargetPaths() const; virtual std::vector getTargetPathsForSourceFile(const fs::path &sourceFilePath) const; @@ -228,10 +228,6 @@ class BuildDatabase { std::shared_ptr getTargetInfo(const fs::path &_target); - virtual bool hasAutoTarget() const = 0; - - virtual fs::path getTargetPath() const = 0; - std::shared_ptr compilationDatabase; protected: diff --git a/server/src/building/ProjectBuildDatabase.h b/server/src/building/ProjectBuildDatabase.h index a7909cbaf..6bf49b5da 100644 --- a/server/src/building/ProjectBuildDatabase.h +++ b/server/src/building/ProjectBuildDatabase.h @@ -20,10 +20,6 @@ class ProjectBuildDatabase : public BuildDatabase { utbot::ProjectContext _projectContext); static std::shared_ptr create(const utbot::ProjectContext &projectContext); - - bool hasAutoTarget() const override; - - fs::path getTargetPath() const override; }; diff --git a/server/src/building/ProjectBuildDatabse.cpp b/server/src/building/ProjectBuildDatabse.cpp index b9826d7ac..fe9a1c644 100644 --- a/server/src/building/ProjectBuildDatabse.cpp +++ b/server/src/building/ProjectBuildDatabse.cpp @@ -233,12 +233,3 @@ void ProjectBuildDatabase::fillTargetInfoParents() { targetInfos[library]->parentLinkUnits = std::move(parents); } } - - -bool ProjectBuildDatabase::hasAutoTarget() const { - return true; -} - -fs::path ProjectBuildDatabase::getTargetPath() const { - throw CompilationDatabaseException("Incorrect method for project build database"); -} diff --git a/server/src/building/TargetBuildDatabase.cpp b/server/src/building/TargetBuildDatabase.cpp index 61bcb8bbf..d0be093f4 100644 --- a/server/src/building/TargetBuildDatabase.cpp +++ b/server/src/building/TargetBuildDatabase.cpp @@ -51,6 +51,13 @@ std::shared_ptr TargetBuildDatabase::createForSourceOrTarge return std::make_shared(std::move(TargetBuildDatabase(baseBuildDatabase, _target))); } +std::vector> TargetBuildDatabase::getRootTargets() const { + if (!hasAutoTarget()) { + return {targetInfos.at(target)}; + } + return BuildDatabase::getRootTargets(); +} + std::vector TargetBuildDatabase::getTargetPathsForSourceFile(const fs::path &sourceFilePath) const { if (!hasAutoTarget()) { return {target}; diff --git a/server/src/building/TargetBuildDatabase.h b/server/src/building/TargetBuildDatabase.h index 8a099247e..721dd089d 100644 --- a/server/src/building/TargetBuildDatabase.h +++ b/server/src/building/TargetBuildDatabase.h @@ -14,9 +14,11 @@ class TargetBuildDatabase : public BuildDatabase { static std::shared_ptr createForSourceOrTarget(BuildDatabase *baseBuildDatabase, const std::string &_targetOrSourcePath); - bool hasAutoTarget() const override; + bool hasAutoTarget() const; - fs::path getTargetPath() const override; + fs::path getTargetPath() const; + + std::vector> getRootTargets() const override; std::vector getTargetPathsForSourceFile(const fs::path &sourceFilePath) const override; From c5b814905abf746d4732aea55815d97bc404b95b Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 18 Aug 2022 18:16:03 +0300 Subject: [PATCH 26/27] refactoring after review 8 --- server/src/Server.cpp | 5 ++--- server/src/Synchronizer.cpp | 3 +-- server/src/Synchronizer.h | 3 +-- server/src/building/Linker.cpp | 30 +++++----------------------- server/src/stubs/StubGen.cpp | 30 +++++++++++++++++++++++++++- server/src/stubs/StubGen.h | 1 + server/test/framework/Stub_Tests.cpp | 10 ++++++++++ 7 files changed, 49 insertions(+), 33 deletions(-) diff --git a/server/src/Server.cpp b/server/src/Server.cpp index ea820385e..da2413d1b 100644 --- a/server/src/Server.cpp +++ b/server/src/Server.cpp @@ -210,7 +210,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen, testGen.progressWriter->writeProgress("Generating stub files", 0.0); StubGen stubGen(testGen); - Synchronizer synchronizer(&testGen, &stubGen, &sizeContext); + Synchronizer synchronizer(&testGen, &sizeContext); synchronizer.synchronize(typesHandler); std::shared_ptr lineInfo = nullptr; @@ -547,7 +547,6 @@ Status Server::TestsGenServiceImpl::ProcessProjectStubsRequest(BaseTestGen *test StubsWriter *stubsWriter) { types::TypesHandler::SizeContext sizeContext; types::TypesHandler typesHandler{testGen->types, sizeContext}; - StubGen stubGen(*testGen); static std::string logMessage = "Traversing sources AST tree and fetching declarations."; LOG_S(DEBUG) << logMessage; @@ -557,7 +556,7 @@ Status Server::TestsGenServiceImpl::ProcessProjectStubsRequest(BaseTestGen *test testGen->compileCommandsJsonPath, false); fetcher.fetchWithProgress(testGen->progressWriter, logMessage); - Synchronizer synchronizer(testGen, &stubGen, &sizeContext); + Synchronizer synchronizer(testGen, &sizeContext); synchronizer.synchronize(typesHandler); stubsWriter->writeResponse(testGen->synchronizedStubs, testGen->projectContext.testDirPath); return Status::OK; diff --git a/server/src/Synchronizer.cpp b/server/src/Synchronizer.cpp index f2ec94c6e..649686b43 100644 --- a/server/src/Synchronizer.cpp +++ b/server/src/Synchronizer.cpp @@ -40,9 +40,8 @@ bool StubOperator::isHeader() const { } Synchronizer::Synchronizer(BaseTestGen *testGen, - StubGen const *stubGen, types::TypesHandler::SizeContext *sizeContext) - : testGen(testGen), stubGen(stubGen), sizeContext(sizeContext) { + : testGen(testGen), sizeContext(sizeContext) { } bool Synchronizer::isProbablyOutdated(const fs::path &srcFilePath) const { diff --git a/server/src/Synchronizer.h b/server/src/Synchronizer.h index 5daae8e3e..4a8c1ccf8 100644 --- a/server/src/Synchronizer.h +++ b/server/src/Synchronizer.h @@ -21,7 +21,6 @@ class StubOperator { class Synchronizer { BaseTestGen *const testGen; - StubGen const *const stubGen; types::TypesHandler::SizeContext *sizeContext; [[nodiscard]] CollectionUtils::FileSet getOutdatedSourcePaths() const; @@ -53,7 +52,7 @@ class Synchronizer { static CollectionUtils::FileSet dropHeaders(const CollectionUtils::FileSet &files); - Synchronizer(BaseTestGen *testGen, StubGen const *stubGen, types::TypesHandler::SizeContext *sizeContext); + Synchronizer(BaseTestGen *testGen, types::TypesHandler::SizeContext *sizeContext); void synchronize(const types::TypesHandler &typesHandler); diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 7f5374e94..56d42307b 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -410,32 +410,12 @@ static const std::string STUB_BITCODE_FILES_NAME = "STUB_BITCODE_FILES"; static const std::string STUB_BITCODE_FILES = "$(STUB_BITCODE_FILES)"; Result Linker::generateStubsMakefile( - const fs::path &root, const fs::path &outputFile, const fs::path &stubsMakefile) const { - ShellExecTask::ExecutionParameters nmCommand( - Paths::getLLVMnm(), - { "--print-file-name", "--undefined-only", "--just-symbol-name", outputFile }); - auto [out, status, _] = ShellExecTask::runShellCommandTask(nmCommand, testGen.serverBuildDir); - if (status != 0) { - std::string errorMessage = - StringUtils::stringFormat("llvm-nm on %s failed: %s", outputFile, out); - LOG_S(ERROR) << errorMessage; - return errorMessage; + const fs::path &root, const fs::path &outputFile, const fs::path &stubsMakefile) const { + auto result = StubGen(testGen).getStubSetForObject(outputFile); + if (!result.isSuccess()) { + return result; } - auto symbols = - CollectionUtils::transform(StringUtils::split(out, '\n'), [](std::string const &line) { - return StringUtils::splitByWhitespaces(line).back(); - }); - CollectionUtils::erase_if(symbols, [](std::string const &symbol) { - return StringUtils::startsWith(symbol, "__ubsan") || - StringUtils::startsWith(symbol, "klee_"); - }); - auto signatures = CollectionUtils::transform(symbols, [](std::string const &symbol) { - Tests::MethodDescription methodDescription; - methodDescription.name = symbol; - return methodDescription; - }); - auto rootLinkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(root); - auto stubsSet = StubGen(testGen).findStubFilesBySignatures(signatures); + auto stubsSet = result.getOpt().value(); printer::DefaultMakefilePrinter makefilePrinter; auto bitcodeStubFiles = CollectionUtils::transformTo>( Synchronizer::dropHeaders(stubsSet), [this, &makefilePrinter](const fs::path &stubPath) { diff --git a/server/src/stubs/StubGen.cpp b/server/src/stubs/StubGen.cpp index c4fe2cd16..676014b38 100644 --- a/server/src/stubs/StubGen.cpp +++ b/server/src/stubs/StubGen.cpp @@ -8,7 +8,8 @@ #include "clang-utils/SourceToHeaderRewriter.h" #include "printers/CCJsonPrinter.h" #include "streams/stubs/StubsWriter.h" - +#include "loguru.h" +#include "environment/EnvironmentPaths.h" StubGen::StubGen(BaseTestGen &testGen) : testGen(testGen) { } @@ -99,3 +100,30 @@ bool StubGen::cmpMethodsDecl(const Tests::MethodDescription &decl1, } return true; } + +Result StubGen::getStubSetForObject(const fs::path &objectFilePath) { + ShellExecTask::ExecutionParameters nmCommand( + Paths::getLLVMnm(), + { "--print-file-name", "--undefined-only", "--just-symbol-name", objectFilePath }); + auto [out, status, _] = ShellExecTask::runShellCommandTask(nmCommand, testGen.serverBuildDir); + if (status != 0) { + std::string errorMessage = + StringUtils::stringFormat("llvm-nm on %s failed: %s", objectFilePath, out); + LOG_S(ERROR) << errorMessage; + return errorMessage; + } + auto symbols = + CollectionUtils::transform(StringUtils::split(out, '\n'), [](std::string const &line) { + return StringUtils::splitByWhitespaces(line).back(); + }); + CollectionUtils::erase_if(symbols, [](std::string const &symbol) { + return StringUtils::startsWith(symbol, "__ubsan") || + StringUtils::startsWith(symbol, "klee_"); + }); + auto signatures = CollectionUtils::transform(symbols, [](std::string const &symbol) { + Tests::MethodDescription methodDescription; + methodDescription.name = symbol; + return methodDescription; + }); + return findStubFilesBySignatures(signatures); +} diff --git a/server/src/stubs/StubGen.h b/server/src/stubs/StubGen.h index 397d17b0a..87ed4972f 100644 --- a/server/src/stubs/StubGen.h +++ b/server/src/stubs/StubGen.h @@ -21,6 +21,7 @@ class StubGen { static tests::Tests mergeSourceFileIntoStub(const tests::Tests &methodDescription, const tests::Tests &srcFile); + Result getStubSetForObject(const fs::path &objectFilePath); private: BaseTestGen &testGen; diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index 7080170f8..143080505 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -168,6 +168,16 @@ namespace { Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get()); ASSERT_TRUE(status.ok()) << status.error_message(); EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 2); + + const fs::path objectFile = testGen.getClientCompilationUnitInfo(foreign_bar_c)->getOutputFile(); + auto result = StubGen(testGen).getStubSetForObject(objectFile); + ASSERT_TRUE(result.isSuccess()); + auto stubCandidates = {calc_sum_c}; + auto expectedStubFiles = CollectionUtils::transformTo( + stubCandidates, [&testGen](fs::path const &path) { + return Paths::sourcePathToStubPath(testGen.projectContext, path); + }); + EXPECT_EQ(expectedStubFiles, result.getOpt().value()); } TEST_F(Stub_Test, File_Tests_With_Stubs) { From 840cdd97c444c6f7b177fe8be70f5c57a827acb3 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Mon, 22 Aug 2022 18:27:23 +0300 Subject: [PATCH 27/27] refactoring after review 9 --- server/src/Server.cpp | 6 +-- server/src/building/ProjectBuildDatabase.h | 6 +-- server/src/building/ProjectBuildDatabse.cpp | 12 ++---- server/src/building/TargetBuildDatabase.cpp | 40 ++++++++----------- server/src/building/TargetBuildDatabase.h | 6 +-- server/src/coverage/GcovCoverageTool.cpp | 2 +- server/src/coverage/TestRunner.cpp | 3 +- server/src/printers/NativeMakefilePrinter.cpp | 2 +- server/src/printers/NativeMakefilePrinter.h | 4 +- server/src/printers/RelativeMakefilePrinter.h | 1 + server/src/testgens/BaseTestGen.cpp | 3 +- server/src/testgens/ProjectTestGen.cpp | 2 +- server/src/testgens/SnippetTestGen.cpp | 2 +- 13 files changed, 39 insertions(+), 50 deletions(-) diff --git a/server/src/Server.cpp b/server/src/Server.cpp index da2413d1b..8a5365f03 100644 --- a/server/src/Server.cpp +++ b/server/src/Server.cpp @@ -579,7 +579,7 @@ Status Server::TestsGenServiceImpl::PrintModulesContent(ServerContext *context, utbot::ProjectContext projectContext{*request}; fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); - std::shared_ptr buildDatabase = ProjectBuildDatabase::create(projectContext); + std::shared_ptr buildDatabase = std::make_shared(projectContext); StubSourcesFinder(buildDatabase).printAllModules(); return Status::OK; } @@ -654,7 +654,7 @@ Status Server::TestsGenServiceImpl::GetProjectTargets(ServerContext *context, try { utbot::ProjectContext projectContext{request->projectcontext()}; - auto buildDatabase = ProjectBuildDatabase::create(projectContext); + auto buildDatabase = std::make_shared(projectContext); std::vector targets = buildDatabase->getAllTargetPaths(); ProjectTargetsWriter targetsWriter(response); targetsWriter.writeResponse(projectContext, targets); @@ -676,7 +676,7 @@ Status Server::TestsGenServiceImpl::GetFileTargets(ServerContext *context, try { utbot::ProjectContext projectContext{request->projectcontext()}; - auto buildDatabase = ProjectBuildDatabase::create(projectContext); + auto buildDatabase = std::make_shared(projectContext); fs::path path = request->path(); auto targetPaths = buildDatabase->getTargetPathsForSourceFile(path); FileTargetsWriter targetsWriter{response}; diff --git a/server/src/building/ProjectBuildDatabase.h b/server/src/building/ProjectBuildDatabase.h index 6bf49b5da..ddf8c339e 100644 --- a/server/src/building/ProjectBuildDatabase.h +++ b/server/src/building/ProjectBuildDatabase.h @@ -16,10 +16,10 @@ class ProjectBuildDatabase : public BuildDatabase { void fillTargetInfoParents(); public: - ProjectBuildDatabase(fs::path _buildCommandsJsonPath, fs::path _serverBuildDir, - utbot::ProjectContext _projectContext); + ProjectBuildDatabase(fs::path buildCommandsJsonPath, fs::path serverBuildDir, + utbot::ProjectContext projectContext); - static std::shared_ptr create(const utbot::ProjectContext &projectContext); + ProjectBuildDatabase(utbot::ProjectContext projectContext); }; diff --git a/server/src/building/ProjectBuildDatabse.cpp b/server/src/building/ProjectBuildDatabse.cpp index fe9a1c644..299ada427 100644 --- a/server/src/building/ProjectBuildDatabse.cpp +++ b/server/src/building/ProjectBuildDatabse.cpp @@ -43,14 +43,10 @@ ProjectBuildDatabase::ProjectBuildDatabase(fs::path _buildCommandsJsonPath, createClangCompileCommandsJson(); } -std::shared_ptr ProjectBuildDatabase::create(const utbot::ProjectContext &projectContext) { - fs::path compileCommandsJsonPath = - CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( - projectContext.projectPath, projectContext.buildDirRelativePath); - fs::path serverBuildDir = Paths::getUtbotBuildDir(projectContext); - std::shared_ptr buildDatabase = std::make_shared( - std::move(ProjectBuildDatabase(compileCommandsJsonPath, serverBuildDir, projectContext))); - return buildDatabase; +ProjectBuildDatabase::ProjectBuildDatabase(utbot::ProjectContext projectContext) : ProjectBuildDatabase( + CompilationUtils::substituteRemotePathToCompileCommandsJsonPath(projectContext.projectPath, + projectContext.buildDirRelativePath), + Paths::getUtbotBuildDir(projectContext), std::move(projectContext)) { } diff --git a/server/src/building/TargetBuildDatabase.cpp b/server/src/building/TargetBuildDatabase.cpp index d0be093f4..fc9a3b74f 100644 --- a/server/src/building/TargetBuildDatabase.cpp +++ b/server/src/building/TargetBuildDatabase.cpp @@ -4,11 +4,23 @@ #include "utils/GrpcUtils.h" #include "utils/GenerationUtils.h" -TargetBuildDatabase::TargetBuildDatabase(BuildDatabase *baseBuildDatabase, - const fs::path &_target) : - BuildDatabase(baseBuildDatabase), - target(_target), - isAutoTarget(_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) { +TargetBuildDatabase::TargetBuildDatabase(BuildDatabase *baseBuildDatabase, const std::string &targetOrSourcePath) : + BuildDatabase(baseBuildDatabase) { + if (Paths::isSourceFile(targetOrSourcePath)) { + target = baseBuildDatabase->getRootForSource(targetOrSourcePath); + } else if (targetOrSourcePath == GrpcUtils::UTBOT_AUTO_TARGET_PATH || targetOrSourcePath.empty()) { + target = baseBuildDatabase->getRootForFirstSource(); + } else { + auto new_target = GenerationUtils::findTarget(baseBuildDatabase->getAllTargets(), targetOrSourcePath); + if (new_target.has_value()) { + target = new_target.value(); + } else { + throw CompilationDatabaseException("Can't find target: " + targetOrSourcePath); + } + } + + isAutoTarget = target == GrpcUtils::UTBOT_AUTO_TARGET_PATH; + { auto objectFilesList = baseBuildDatabase->getArchiveObjectFiles(target); for (const auto &objectFilePath: objectFilesList) { @@ -33,24 +45,6 @@ TargetBuildDatabase::TargetBuildDatabase(BuildDatabase *baseBuildDatabase, createClangCompileCommandsJson(); } -std::shared_ptr TargetBuildDatabase::createForSourceOrTarget(BuildDatabase *baseBuildDatabase, - const std::string &_targetOrSourcePath) { - fs::path _target; - if (Paths::isSourceFile(_targetOrSourcePath)) { - _target = baseBuildDatabase->getRootForSource(_targetOrSourcePath); - } else if (_targetOrSourcePath == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _targetOrSourcePath.empty()) { - _target = baseBuildDatabase->getRootForFirstSource(); - } else { - auto new_target = GenerationUtils::findTarget(baseBuildDatabase->getAllTargets(), _targetOrSourcePath); - if (new_target.has_value()) { - _target = new_target.value(); - } else { - throw CompilationDatabaseException("Can't find target: " + _targetOrSourcePath); - } - } - return std::make_shared(std::move(TargetBuildDatabase(baseBuildDatabase, _target))); -} - std::vector> TargetBuildDatabase::getRootTargets() const { if (!hasAutoTarget()) { return {targetInfos.at(target)}; diff --git a/server/src/building/TargetBuildDatabase.h b/server/src/building/TargetBuildDatabase.h index 721dd089d..18d87de25 100644 --- a/server/src/building/TargetBuildDatabase.h +++ b/server/src/building/TargetBuildDatabase.h @@ -6,13 +6,11 @@ class TargetBuildDatabase : public BuildDatabase { private: - TargetBuildDatabase(BuildDatabase *baseBuildDatabase, const fs::path &_target); - fs::path target; bool isAutoTarget; + public: - static std::shared_ptr createForSourceOrTarget(BuildDatabase *baseBuildDatabase, - const std::string &_targetOrSourcePath); + TargetBuildDatabase(BuildDatabase *baseBuildDatabase, const std::string &targetOrSourcePath); bool hasAutoTarget() const; diff --git a/server/src/coverage/GcovCoverageTool.cpp b/server/src/coverage/GcovCoverageTool.cpp index e998db93c..176e29665 100644 --- a/server/src/coverage/GcovCoverageTool.cpp +++ b/server/src/coverage/GcovCoverageTool.cpp @@ -13,12 +13,12 @@ #include "utils/MakefileUtils.h" #include "utils/StringUtils.h" #include "utils/path/FileSystemPath.h" +#include "printers/DefaultMakefilePrinter.h" #include "loguru.h" #include "json.hpp" #include -#include "printers/DefaultMakefilePrinter.h" using Coverage::CoverageMap; using Coverage::FileCoverage; diff --git a/server/src/coverage/TestRunner.cpp b/server/src/coverage/TestRunner.cpp index e55daad99..6e3b79806 100644 --- a/server/src/coverage/TestRunner.cpp +++ b/server/src/coverage/TestRunner.cpp @@ -1,5 +1,6 @@ -#include "printers/DefaultMakefilePrinter.h" #include "TestRunner.h" + +#include "printers/DefaultMakefilePrinter.h" #include "GTestLogger.h" #include "Paths.h" #include "TimeExecStatistics.h" diff --git a/server/src/printers/NativeMakefilePrinter.cpp b/server/src/printers/NativeMakefilePrinter.cpp index e4c68b4c3..510102113 100644 --- a/server/src/printers/NativeMakefilePrinter.cpp +++ b/server/src/printers/NativeMakefilePrinter.cpp @@ -116,7 +116,7 @@ namespace printer { } NativeMakefilePrinter::NativeMakefilePrinter( - const BaseTestGen* testGen, + const BaseTestGen *testGen, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources, diff --git a/server/src/printers/NativeMakefilePrinter.h b/server/src/printers/NativeMakefilePrinter.h index 2052617f4..d6ce86631 100644 --- a/server/src/printers/NativeMakefilePrinter.h +++ b/server/src/printers/NativeMakefilePrinter.h @@ -15,7 +15,7 @@ namespace printer { class NativeMakefilePrinter : public RelativeMakefilePrinter { friend class TestMakefilesPrinter; private: - const BaseTestGen* testGen; + const BaseTestGen *testGen; fs::path rootPath; fs::path primaryCompiler; @@ -66,7 +66,7 @@ namespace printer { bool transformExeToLib); public: - NativeMakefilePrinter(const BaseTestGen* testGen, + NativeMakefilePrinter(const BaseTestGen *testGen, fs::path const &rootPath, fs::path primaryCompiler, CollectionUtils::FileSet const *stubSources, diff --git a/server/src/printers/RelativeMakefilePrinter.h b/server/src/printers/RelativeMakefilePrinter.h index 07504d42b..4f699b2ea 100644 --- a/server/src/printers/RelativeMakefilePrinter.h +++ b/server/src/printers/RelativeMakefilePrinter.h @@ -1,5 +1,6 @@ #ifndef UNITTESTBOT_RELATIVEMAKEFILEPRINTER_H #define UNITTESTBOT_RELATIVEMAKEFILEPRINTER_H + #include "printers/DefaultMakefilePrinter.h" namespace printer { diff --git a/server/src/testgens/BaseTestGen.cpp b/server/src/testgens/BaseTestGen.cpp index 16f3c98d6..a07a34c54 100644 --- a/server/src/testgens/BaseTestGen.cpp +++ b/server/src/testgens/BaseTestGen.cpp @@ -48,8 +48,7 @@ void BaseTestGen::setInitializedTestsMap() { void BaseTestGen::setTargetPath(fs::path _targetPath) { if (targetBuildDatabase->hasAutoTarget() && targetBuildDatabase->getTargetPath() != _targetPath) { - targetBuildDatabase = std::move( - TargetBuildDatabase::createForSourceOrTarget(projectBuildDatabase.get(), _targetPath)); + targetBuildDatabase = std::make_shared(projectBuildDatabase.get(), _targetPath); updateTargetSources(_targetPath); } } diff --git a/server/src/testgens/ProjectTestGen.cpp b/server/src/testgens/ProjectTestGen.cpp index c4b7f3870..6c6ca59b1 100644 --- a/server/src/testgens/ProjectTestGen.cpp +++ b/server/src/testgens/ProjectTestGen.cpp @@ -17,7 +17,7 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request, compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath( projectContext.projectPath, projectContext.buildDirRelativePath); projectBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); - targetBuildDatabase = TargetBuildDatabase::createForSourceOrTarget(projectBuildDatabase.get(), request.targetpath()); + targetBuildDatabase = std::make_shared(projectBuildDatabase.get(), request.targetpath()); if (autoDetect) { autoDetectSourcePathsIfNotEmpty(); } else { diff --git a/server/src/testgens/SnippetTestGen.cpp b/server/src/testgens/SnippetTestGen.cpp index 328af8294..600521db3 100644 --- a/server/src/testgens/SnippetTestGen.cpp +++ b/server/src/testgens/SnippetTestGen.cpp @@ -19,7 +19,7 @@ SnippetTestGen::SnippetTestGen(const testsgen::SnippetRequest &request, compileCommandsJsonPath = serverBuildDir; utbot::ProjectContext projectContext{request, serverBuildDir}; projectBuildDatabase = std::make_shared(compileCommandsJsonPath, serverBuildDir, projectContext); - targetBuildDatabase = TargetBuildDatabase::createForSourceOrTarget(projectBuildDatabase.get(), serverBuildDir / SNIPPET_TARGET); + targetBuildDatabase = std::make_shared(projectBuildDatabase.get(), serverBuildDir / SNIPPET_TARGET); setTargetForSource(filePath); setInitializedTestsMap(); }