cmake/Source/CTest/cmParseBlanketJSCoverage.cxx

140 lines
4.1 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2015-04-27 22:25:09 +02:00
#include "cmParseBlanketJSCoverage.h"
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
#include <cstdio>
#include <cstdlib>
#include "cmsys/FStream.hxx"
2016-10-30 18:24:19 +01:00
#include "cmCTest.h"
#include "cmCTestCoverageHandler.h"
2016-07-09 11:21:54 +02:00
#include "cmSystemTools.h"
2016-10-30 18:24:19 +01:00
2015-04-27 22:25:09 +02:00
class cmParseBlanketJSCoverage::JSONParser
2016-07-09 11:21:54 +02:00
{
2015-04-27 22:25:09 +02:00
public:
2020-02-01 23:06:01 +01:00
using FileLinesType =
cmCTestCoverageHandlerContainer::SingleFileCoverageVector;
2015-04-27 22:25:09 +02:00
JSONParser(cmCTestCoverageHandlerContainer& cont)
2016-07-09 11:21:54 +02:00
: Coverage(cont)
{
}
2015-04-27 22:25:09 +02:00
2019-11-11 23:01:05 +01:00
virtual ~JSONParser() = default;
2015-04-27 22:25:09 +02:00
2017-07-20 19:35:53 +02:00
std::string getValue(std::string const& line, int type)
2016-07-09 11:21:54 +02:00
{
2015-04-27 22:25:09 +02:00
size_t begIndex;
size_t endIndex;
endIndex = line.rfind(',');
begIndex = line.find_first_of(':');
2016-07-09 11:21:54 +02:00
if (type == 0) {
2015-04-27 22:25:09 +02:00
// A unique substring to remove the extra characters
// around the files name in the JSON (extra " and ,)
std::string foundFileName =
2016-07-09 11:21:54 +02:00
line.substr(begIndex + 3, endIndex - (begIndex + 4));
2015-04-27 22:25:09 +02:00
return foundFileName;
}
2017-07-20 19:35:53 +02:00
return line.substr(begIndex);
2016-07-09 11:21:54 +02:00
}
bool ParseFile(std::string const& file)
{
2015-04-27 22:25:09 +02:00
FileLinesType localCoverageVector;
std::string filename;
bool foundFile = false;
2016-07-09 11:21:54 +02:00
bool inSource = false;
2015-04-27 22:25:09 +02:00
std::string covResult;
std::string line;
cmsys::ifstream in(file.c_str());
2016-07-09 11:21:54 +02:00
if (!in) {
2015-04-27 22:25:09 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
while (cmSystemTools::GetLineFromStream(in, line)) {
2017-07-20 19:35:53 +02:00
if (line.find("filename") != std::string::npos) {
2016-07-09 11:21:54 +02:00
if (foundFile) {
2015-04-27 22:25:09 +02:00
/*
2017-07-20 19:35:53 +02:00
* Upon finding a second file name, generate a
* vector within the total coverage to capture the
* information in the local vector
*/
2015-04-27 22:25:09 +02:00
FileLinesType& CoverageVector =
2016-07-09 11:21:54 +02:00
this->Coverage.TotalCoverage[filename];
2015-04-27 22:25:09 +02:00
CoverageVector = localCoverageVector;
localCoverageVector.clear();
}
2016-07-09 11:21:54 +02:00
foundFile = true;
inSource = false;
2021-09-14 00:13:48 +02:00
filename = this->getValue(line, 0);
2017-07-20 19:35:53 +02:00
} else if ((line.find("coverage") != std::string::npos) && foundFile &&
2016-07-09 11:21:54 +02:00
inSource) {
2015-04-27 22:25:09 +02:00
/*
2017-07-20 19:35:53 +02:00
* two types of "coverage" in the JSON structure
*
* The coverage result over the file or set of files
* and the coverage for each individual line
*
* FoundFile and foundSource ensure that
* only the value of the line coverage is captured
*/
2021-09-14 00:13:48 +02:00
std::string result = this->getValue(line, 1);
2017-07-20 19:35:53 +02:00
result = result.substr(2);
2016-07-09 11:21:54 +02:00
if (result == "\"\"") {
2015-04-27 22:25:09 +02:00
// Empty quotation marks indicate that the
// line is not executable
localCoverageVector.push_back(-1);
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
// Else, it contains the number of time executed
localCoverageVector.push_back(atoi(result.c_str()));
}
2017-07-20 19:35:53 +02:00
} else if (line.find("source") != std::string::npos) {
2016-07-09 11:21:54 +02:00
inSource = true;
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// On exit, capture end of last file covered.
2016-07-09 11:21:54 +02:00
FileLinesType& CoverageVector = this->Coverage.TotalCoverage[filename];
2015-04-27 22:25:09 +02:00
CoverageVector = localCoverageVector;
localCoverageVector.clear();
return true;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
private:
cmCTestCoverageHandlerContainer& Coverage;
};
cmParseBlanketJSCoverage::cmParseBlanketJSCoverage(
2016-07-09 11:21:54 +02:00
cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
: Coverage(cont)
, CTest(ctest)
{
}
2015-04-27 22:25:09 +02:00
2020-02-01 23:06:01 +01:00
bool cmParseBlanketJSCoverage::LoadCoverageData(
std::vector<std::string> const& files)
2016-07-09 11:21:54 +02:00
{
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"Found " << files.size() << " Files" << std::endl,
this->Coverage.Quiet);
2018-01-26 17:06:56 +01:00
for (std::string const& file : files) {
2016-07-09 11:21:54 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
2018-01-26 17:06:56 +01:00
"Reading JSON File " << file << std::endl,
2016-07-09 11:21:54 +02:00
this->Coverage.Quiet);
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
if (!this->ReadJSONFile(file)) {
2015-04-27 22:25:09 +02:00
return false;
}
}
2016-07-09 11:21:54 +02:00
return true;
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
bool cmParseBlanketJSCoverage::ReadJSONFile(std::string const& file)
{
cmParseBlanketJSCoverage::JSONParser parser(this->Coverage);
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"Parsing " << file << std::endl, this->Coverage.Quiet);
2015-04-27 22:25:09 +02:00
parser.ParseFile(file);
return true;
2016-07-09 11:21:54 +02:00
}