-
Notifications
You must be signed in to change notification settings - Fork 0
Kcov updated #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Kcov updated #1
Changes from all commits
0cbe262
59d1ab4
4f8be00
ba0728d
08b9750
1cbabce
acfd2ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,9 @@ include_directories("${CMAKE_CURRENT_BINARY_DIR}") | |
|
||
find_package(run_klee REQUIRED) | ||
|
||
# | ||
find_package(pugixml REQUIRED) | ||
|
||
option(ENABLE_PRECOMPILED_HEADERS "Enable precompiled headers" ON) | ||
|
||
if (ENABLE_PRECOMPILED_HEADERS) | ||
|
@@ -141,6 +144,7 @@ target_link_libraries(UnitTestBotLib PUBLIC clangTooling clangBasic clangASTMatc | |
protobuf::libprotobuf | ||
loguru | ||
kleeRunner | ||
pugixml | ||
) | ||
if (ENABLE_PRECOMPILED_HEADERS) | ||
target_precompile_headers(UnitTestBotLib PUBLIC pch.h) | ||
|
@@ -153,6 +157,7 @@ add_llvm_executable(utbot main.cpp) | |
target_link_libraries(utbot PUBLIC | ||
UnitTestBotLib | ||
loguru | ||
pugixml ######### | ||
) | ||
|
||
################################################################################ | ||
|
@@ -180,6 +185,7 @@ if (ENABLE_UNIT_TESTS) | |
PUBLIC | ||
gtest | ||
UnitTestBotLib | ||
pugixml | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the comment related to |
||
) | ||
|
||
if (ENABLE_PRECOMPILED_HEADERS) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// | ||
// Created by andrey on 05.06.2022. | ||
// | ||
|
||
#include "KcovCoverageTool.h" | ||
|
||
#include "Coverage.h" | ||
#include "Paths.h" | ||
#include "TimeExecStatistics.h" | ||
#include "environment/EnvironmentPaths.h" | ||
#include "exceptions/CoverageGenerationException.h" | ||
#include "utils/ArgumentsUtils.h" | ||
#include "utils/CollectionUtils.h" | ||
#include "utils/FileSystemUtils.h" | ||
#include "utils/JsonUtils.h" | ||
#include "utils/MakefileUtils.h" | ||
#include "utils/StringUtils.h" | ||
#include "utils/path/FileSystemPath.h" | ||
#include "pugixml.hpp" | ||
#include <regex> | ||
|
||
#include "loguru.h" | ||
|
||
using Coverage::CoverageMap; | ||
using Coverage::FileCoverage; | ||
|
||
|
||
KcovCoverageTool::KcovCoverageTool(utbot::ProjectContext projectContext, | ||
ProgressWriter const *progressWriter) | ||
: CoverageTool(progressWriter), projectContext(projectContext) { | ||
} | ||
|
||
std::vector<BuildRunCommand> | ||
KcovCoverageTool::getBuildRunCommands(const std::vector<UnitTest> &testsToLaunch, bool withCoverage) { | ||
ExecUtils::throwIfCancelled(); | ||
|
||
std::vector<BuildRunCommand> result; | ||
ExecUtils::doWorkWithProgress( | ||
testsToLaunch, progressWriter, "Collecting build and run commands", | ||
[&](UnitTest const &testToLaunch) { | ||
auto makefile = Paths::getMakefilePathFromSourceFilePath( | ||
projectContext, | ||
Paths::testPathToSourcePath(projectContext, testToLaunch.testFilePath)); | ||
auto gtestFlags = getTestFilter(testToLaunch); | ||
auto buildCommand = | ||
MakefileUtils::makefileCommand(projectContext, makefile, "build", gtestFlags); | ||
auto runCommand = | ||
MakefileUtils::makefileCommand(projectContext, makefile, "run", gtestFlags); | ||
result.push_back({testToLaunch, buildCommand, runCommand}); | ||
}); | ||
return result; | ||
} | ||
|
||
std::vector<ShellExecTask> | ||
KcovCoverageTool::getCoverageCommands(const std::vector<UnitTest> &testsToLaunch) { | ||
return {}; | ||
} | ||
|
||
|
||
static void addLine(uint32_t lineNumber, bool covered, FileCoverage &fileCoverage) { | ||
assert(lineNumber > 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than calling the |
||
if (covered) { | ||
fileCoverage.fullCoverageLines.insert({lineNumber}); | ||
} else { | ||
fileCoverage.noCoverageLines.insert({lineNumber}); | ||
} | ||
} | ||
|
||
static void setLineNumbers(pugi::xml_node lines, FileCoverage &fileCoverage) { | ||
for (pugi::xml_node line: lines.children("line")) { | ||
uint32_t lineNumber = line.attribute("number").as_int(); | ||
bool covered = line.attribute("hits").as_int() > 0; | ||
addLine(lineNumber, covered, fileCoverage); | ||
} | ||
} | ||
|
||
Coverage::CoverageMap KcovCoverageTool::getCoverageInfo() const { | ||
ExecUtils::throwIfCancelled(); | ||
|
||
CoverageMap coverageMap; | ||
|
||
auto coverageReportDirPath = Paths::getKcovReportDir(projectContext); | ||
if (!fs::exists(coverageReportDirPath)) { | ||
std::string message = "Couldn't find coverage directory at " + coverageReportDirPath.string(); | ||
LOG_S(ERROR) << message; | ||
throw CoverageGenerationException(message); | ||
} | ||
LOG_S(INFO) << "Reading coverage file"; | ||
|
||
|
||
std::string pathToReport = ""; | ||
for (const auto &entry: fs::directory_iterator(coverageReportDirPath.string())) { | ||
std::regex reg("(calc.).*$"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can avoid building |
||
if (std::regex_search(entry.path().string(), reg)) { | ||
pathToReport = entry.path().string() + "/cov.xml"; | ||
break; | ||
} | ||
} | ||
Comment on lines
+91
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's extract this block to private method |
||
|
||
auto path = pathToReport.c_str(); | ||
|
||
pugi::xml_document doc; | ||
pugi::xml_parse_result result = doc.load_file(path); | ||
auto tools = doc.child("coverage").child("packages").child("package").child("classes"); | ||
|
||
|
||
for (pugi::xml_node fileIter = tools.child("class"); fileIter; fileIter = fileIter.next_sibling("class")) { | ||
std::string filePathString = fileIter.attribute("filename").as_string(); | ||
fs::path filePath(filePathString); | ||
|
||
pugi::xml_node lines = fileIter.child("lines"); | ||
|
||
setLineNumbers(lines, coverageMap[filePath]); | ||
//setFunctionBorders(lines, coverageMap[filePath]); | ||
} | ||
|
||
return coverageMap; | ||
} | ||
|
||
|
||
nlohmann::json KcovCoverageTool::getTotals() const { | ||
return nlohmann::json(); | ||
} | ||
|
||
void KcovCoverageTool::cleanCoverage() const { | ||
|
||
} | ||
|
||
fs::path KcovCoverageTool::getKcovReportFile() const { | ||
return nullptr; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// Created by andrey on 05.06.2022. | ||
// | ||
|
||
#ifndef UNITTESTBOT_KCOVCOVERAGETOOL_H | ||
#define UNITTESTBOT_KCOVCOVERAGETOOL_H | ||
|
||
#include "CoverageAndResultsGenerator.h" | ||
#include "CoverageTool.h" | ||
|
||
class KcovCoverageTool : public CoverageTool { | ||
public: | ||
KcovCoverageTool(utbot::ProjectContext projectContext, const ProgressWriter *progressWriter); | ||
|
||
std::vector<BuildRunCommand> getBuildRunCommands(const std::vector<UnitTest> &testsToLaunch, | ||
bool withCoverage) override; | ||
|
||
std::vector<ShellExecTask> getCoverageCommands(const std::vector<UnitTest> &testFilePath) override; | ||
|
||
[[nodiscard]] Coverage::CoverageMap getCoverageInfo() const override; | ||
[[nodiscard]] nlohmann::json getTotals() const override; | ||
void cleanCoverage() const override; | ||
private: | ||
const utbot::ProjectContext projectContext; | ||
|
||
fs::path getKcovReportFile() const; | ||
}; | ||
|
||
|
||
#endif //UNITTESTBOT_KCOVCOVERAGETOOL_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -377,7 +377,9 @@ namespace printer { | |
|
||
declareTarget("bin", { FORCE }, { stringFormat("echo %s", coverageInfoBinary) }); | ||
|
||
utbot::RunCommand testRunCommand{ { testExecutablePath.string(), "$(GTEST_FLAGS)" }, | ||
std::string kcovRunCommand = "kcov " + (projectContext.buildDir.string() + "/report ") + testExecutablePath.string(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please reuse |
||
|
||
utbot::RunCommand testRunCommand{ { kcovRunCommand, "$(GTEST_FLAGS)" }, | ||
buildDirectory }; | ||
testRunCommand.addEnvironmentVariable("PATH", "$$PATH:$(pwd)"); | ||
if (primaryCompilerName == CompilationUtils::CompilerName::GCC) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,10 +82,10 @@ namespace CompilationUtils { | |
switch (compilerName) { | ||
case CompilerName::GCC: | ||
case CompilerName::GXX: | ||
return { "--coverage" }; | ||
return { "-g", "--coverage" }; | ||
case CompilerName::CLANG: | ||
case CompilerName::CLANGXX: | ||
return { "-fprofile-instr-generate", "-fcoverage-mapping" }; | ||
return { "-g", "-fprofile-instr-generate", "-fcoverage-mapping" }; | ||
Comment on lines
+85
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to add |
||
default: | ||
break; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to link with
pugixml
again since it is already linked toUnitTestBotLib
. The same practice should be applicable tologuru
library