From 89bdb82885828d7d62b28f51c942fdb65df79420 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Mon, 10 Oct 2022 21:57:51 +0800 Subject: [PATCH] fix generation for coreutils (#487) --- server/src/building/ProjectBuildDatabse.cpp | 21 ++--- server/src/commands/Commands.h | 2 +- server/src/coverage/GcovCoverageTool.cpp | 20 +++-- server/src/coverage/LlvmCoverageTool.cpp | 86 ++++++++++++--------- server/src/coverage/TestRunner.cpp | 18 +++-- 5 files changed, 85 insertions(+), 62 deletions(-) diff --git a/server/src/building/ProjectBuildDatabse.cpp b/server/src/building/ProjectBuildDatabse.cpp index a5c5a7e5..9d5cfbf5 100644 --- a/server/src/building/ProjectBuildDatabse.cpp +++ b/server/src/building/ProjectBuildDatabse.cpp @@ -40,15 +40,18 @@ ProjectBuildDatabase::ProjectBuildDatabase(fs::path _buildCommandsJsonPath, 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(); + try { + auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath); + auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath); + initObjects(compileCommandsJson); + initInfo(linkCommandsJson); + filterInstalledFiles(); + addLocalSharedLibraries(); + fillTargetInfoParents(); + createClangCompileCommandsJson(); + } catch (const std::exception &e) { + return; + } } ProjectBuildDatabase::ProjectBuildDatabase(utbot::ProjectContext projectContext) : ProjectBuildDatabase( diff --git a/server/src/commands/Commands.h b/server/src/commands/Commands.h index ee8e0e5a..be7f4968 100644 --- a/server/src/commands/Commands.h +++ b/server/src/commands/Commands.h @@ -248,7 +248,7 @@ namespace Commands { bool generateForStaticFunctions = true; bool verbose = false; int32_t timeoutPerFunction = 30; - int32_t timeoutPerTest = 0; + int32_t timeoutPerTest = 30; bool noDeterministicSearcher = false; bool noStubs = false; }; diff --git a/server/src/coverage/GcovCoverageTool.cpp b/server/src/coverage/GcovCoverageTool.cpp index 176e2966..2757fc3f 100644 --- a/server/src/coverage/GcovCoverageTool.cpp +++ b/server/src/coverage/GcovCoverageTool.cpp @@ -124,15 +124,19 @@ CoverageMap GcovCoverageTool::getCoverageInfo() const { ExecUtils::doWorkWithProgress( FileSystemUtils::DirectoryIterator(covJsonDirPath), progressWriter, "Reading coverage files", [&coverageMap](auto const &entry) { - auto jsonPath = entry.path(); - auto coverageJson = JsonUtils::getJsonFromFile(jsonPath); - for (const nlohmann::json &jsonFile : coverageJson.at("files")) { - fs::path filePath(std::filesystem::path(jsonFile.at("file"))); - if (Paths::isGtest(filePath)) { - continue; + try { + auto jsonPath = entry.path(); + auto coverageJson = JsonUtils::getJsonFromFile(jsonPath); + for (const nlohmann::json &jsonFile: coverageJson.at("files")) { + fs::path filePath(std::filesystem::path(jsonFile.at("file"))); + if (Paths::isGtest(filePath)) { + continue; + } + setLineNumbers(jsonFile, coverageMap[filePath]); + setFunctionBorders(jsonFile, coverageMap[filePath]); } - setLineNumbers(jsonFile, coverageMap[filePath]); - setFunctionBorders(jsonFile, coverageMap[filePath]); + } catch (const std::exception &e) { + return; } }); return coverageMap; diff --git a/server/src/coverage/LlvmCoverageTool.cpp b/server/src/coverage/LlvmCoverageTool.cpp index 723b23b2..63883c48 100644 --- a/server/src/coverage/LlvmCoverageTool.cpp +++ b/server/src/coverage/LlvmCoverageTool.cpp @@ -153,43 +153,46 @@ Coverage::CoverageMap LlvmCoverageTool::getCoverageInfo() const { LOG_S(ERROR) << "Can't found coverage.json at " << covJsonPath.string(); throw CoverageGenerationException("Can't found coverage.json at " + covJsonPath.string()); } - LOG_S(INFO) << "Reading coverage.json"; - - nlohmann::json coverageJson = JsonUtils::getJsonFromFile(covJsonPath); - - // Parsing is based on LLVM coverage mapping format - ExecUtils::doWorkWithProgress( - coverageJson.at("data"), progressWriter, "Reading coverage.json", - [&coverageMap](const nlohmann::json &data) { - for (const nlohmann::json &function : data.at("functions")) { - std::string filename = function.at("filenames").at(0); - // no need to show coverage for gtest library - if (Paths::isGtest(filename)) { - continue; - } - for (const nlohmann::json ®ion : function.at("regions")) { - // In an LLVM coverage mapping format a region is an array with line and - // character position - FileCoverage::SourcePosition startPosition{ region.at(0).get() - 1, - region.at(1).get() - 1 }; - FileCoverage::SourcePosition endPosition{ region.at(2).get() - 1, - region.at(3).get() - 1 }; - FileCoverage::SourceRange sourceRange{ startPosition, endPosition }; - // The 4th element in LLVM coverage mapping format of a region - if (region.at(4).get() == 0) { - coverageMap[filename].uncoveredRanges.push_back(sourceRange); - } else if (region.at(4).get() >= 1) { - coverageMap[filename].coveredRanges.push_back(sourceRange); + try { + LOG_S(INFO) << "Reading coverage.json"; + nlohmann::json coverageJson = JsonUtils::getJsonFromFile(covJsonPath); + + // Parsing is based on LLVM coverage mapping format + ExecUtils::doWorkWithProgress( + coverageJson.at("data"), progressWriter, "Reading coverage.json", + [&coverageMap](const nlohmann::json &data) { + for (const nlohmann::json &function: data.at("functions")) { + std::string filename = function.at("filenames").at(0); + // no need to show coverage for gtest library + if (Paths::isGtest(filename)) { + continue; + } + for (const nlohmann::json ®ion: function.at("regions")) { + // In an LLVM coverage mapping format a region is an array with line and + // character position + FileCoverage::SourcePosition startPosition{region.at(0).get() - 1, + region.at(1).get() - 1}; + FileCoverage::SourcePosition endPosition{region.at(2).get() - 1, + region.at(3).get() - 1}; + FileCoverage::SourceRange sourceRange{startPosition, endPosition}; + // The 4th element in LLVM coverage mapping format of a region + if (region.at(4).get() == 0) { + coverageMap[filename].uncoveredRanges.push_back(sourceRange); + } else if (region.at(4).get() >= 1) { + coverageMap[filename].coveredRanges.push_back(sourceRange); + } + } } - } - } - }); + }); - for (const auto &item : coverageMap) { - countLineCoverage(coverageMap, item.first); - } + for (const auto &item: coverageMap) { + countLineCoverage(coverageMap, item.first); + } - return coverageMap; + return coverageMap; + } catch (const std::exception &e) { + throw CoverageGenerationException("Can't parse coverage.json at " + covJsonPath.string()); + } } void LlvmCoverageTool::countLineCoverage(Coverage::CoverageMap &coverageMap, @@ -223,9 +226,18 @@ void LlvmCoverageTool::checkLineForPartial(Coverage::FileCoverage::SourceLine li } nlohmann::json LlvmCoverageTool::getTotals() const { - fs::path covJsonPath = Paths::getCoverageJsonPath(projectContext); - nlohmann::json coverageJson = JsonUtils::getJsonFromFile(covJsonPath); - return coverageJson.at("data").back().at("totals"); + try { + fs::path covJsonPath = Paths::getCoverageJsonPath(projectContext); + nlohmann::json coverageJson = JsonUtils::getJsonFromFile(covJsonPath); + return coverageJson.at("data").back().at("totals"); + } catch (const std::exception &e) { + return {{ + "lines", { + {"count", 0}, + {"covered", 0}, + {"percent", (double) 0.0} + }}}; + } } diff --git a/server/src/coverage/TestRunner.cpp b/server/src/coverage/TestRunner.cpp index 891c7fb3..4da409f9 100644 --- a/server/src/coverage/TestRunner.cpp +++ b/server/src/coverage/TestRunner.cpp @@ -213,14 +213,18 @@ testsgen::TestResultObject TestRunner::runTest(const BuildRunCommand &command, testRes.set_status(testsgen::TEST_DEATH); return testRes; } - nlohmann::json gtestResultsJson = JsonUtils::getJsonFromFile(Paths::getGTestResultsJsonPath(projectContext)); - if (!google::protobuf::util::TimeUtil::FromString(gtestResultsJson["time"], testRes.mutable_executiontime())) { - LOG_S(WARNING) << "Cannot parse duration of test execution"; - } - if (gtestResultsJson["failures"] != 0) { + try { + nlohmann::json gtestResultsJson = JsonUtils::getJsonFromFile(Paths::getGTestResultsJsonPath(projectContext)); + if (!google::protobuf::util::TimeUtil::FromString(gtestResultsJson["time"], testRes.mutable_executiontime())) { + LOG_S(WARNING) << "Cannot parse duration of test execution"; + } + if (gtestResultsJson["failures"] != 0) { + testRes.set_status(testsgen::TEST_FAILED); + } else { + testRes.set_status(testsgen::TEST_PASSED); + } + } catch (const std::exception &e) { testRes.set_status(testsgen::TEST_FAILED); - } else { - testRes.set_status(testsgen::TEST_PASSED); } return testRes; }