2014-10-24 01:03:30 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc.
|
|
|
|
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2016-04-29 16:40:20 +03:00
|
|
|
#include "cmParseBlanketJSCoverage.h"
|
|
|
|
|
2014-10-24 01:03:30 +04:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include <cmsys/Directory.hxx>
|
|
|
|
#include <cmsys/FStream.hxx>
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <cmsys/Glob.hxx>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-10-24 01:03:30 +04:00
|
|
|
|
|
|
|
class cmParseBlanketJSCoverage::JSONParser
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2014-10-24 01:03:30 +04:00
|
|
|
public:
|
2016-05-16 17:34:04 +03:00
|
|
|
typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
|
|
|
|
FileLinesType;
|
2014-10-24 01:03:30 +04:00
|
|
|
JSONParser(cmCTestCoverageHandlerContainer& cont)
|
2016-05-16 17:34:04 +03:00
|
|
|
: Coverage(cont)
|
|
|
|
{
|
|
|
|
}
|
2014-10-24 01:03:30 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
virtual ~JSONParser() {}
|
2014-10-24 01:03:30 +04:00
|
|
|
|
|
|
|
std::string getValue(std::string line, int type)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2014-10-24 01:03:30 +04:00
|
|
|
size_t begIndex;
|
|
|
|
size_t endIndex;
|
|
|
|
endIndex = line.rfind(',');
|
|
|
|
begIndex = line.find_first_of(':');
|
2016-05-16 17:34:04 +03:00
|
|
|
if (type == 0) {
|
2014-10-24 01:03:30 +04:00
|
|
|
// A unique substring to remove the extra characters
|
|
|
|
// around the files name in the JSON (extra " and ,)
|
|
|
|
std::string foundFileName =
|
2016-05-16 17:34:04 +03:00
|
|
|
line.substr(begIndex + 3, endIndex - (begIndex + 4));
|
2014-10-24 01:03:30 +04:00
|
|
|
return foundFileName;
|
|
|
|
}
|
2016-08-18 21:04:21 +03:00
|
|
|
return line.substr(begIndex, line.npos);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-05-26 22:58:51 +03:00
|
|
|
bool ParseFile(std::string const& file)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2014-10-24 01:03:30 +04:00
|
|
|
FileLinesType localCoverageVector;
|
|
|
|
std::string filename;
|
|
|
|
bool foundFile = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
bool inSource = false;
|
2014-10-24 01:03:30 +04:00
|
|
|
std::string covResult;
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
cmsys::ifstream in(file.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!in) {
|
2014-10-24 01:03:30 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
while (cmSystemTools::GetLineFromStream(in, line)) {
|
|
|
|
if (line.find("filename") != line.npos) {
|
|
|
|
if (foundFile) {
|
2014-10-24 01:03:30 +04:00
|
|
|
/*
|
|
|
|
* Upon finding a second file name, generate a
|
|
|
|
* vector within the total coverage to capture the
|
|
|
|
* information in the local vector
|
|
|
|
*/
|
|
|
|
FileLinesType& CoverageVector =
|
2016-05-16 17:34:04 +03:00
|
|
|
this->Coverage.TotalCoverage[filename];
|
2014-10-24 01:03:30 +04:00
|
|
|
CoverageVector = localCoverageVector;
|
|
|
|
localCoverageVector.clear();
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
foundFile = true;
|
|
|
|
inSource = false;
|
2016-05-26 23:52:22 +03:00
|
|
|
filename = getValue(line, 0);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if ((line.find("coverage") != line.npos) && foundFile &&
|
|
|
|
inSource) {
|
2014-10-24 01:03:30 +04: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
|
|
|
|
*/
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string result = getValue(line, 1);
|
|
|
|
result = result.substr(2, result.npos);
|
|
|
|
if (result == "\"\"") {
|
2014-10-24 01:03:30 +04:00
|
|
|
// Empty quotation marks indicate that the
|
|
|
|
// line is not executable
|
|
|
|
localCoverageVector.push_back(-1);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2014-10-24 01:03:30 +04:00
|
|
|
// Else, it contains the number of time executed
|
|
|
|
localCoverageVector.push_back(atoi(result.c_str()));
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (line.find("source") != line.npos) {
|
|
|
|
inSource = true;
|
2014-10-24 01:03:30 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-10-24 01:03:30 +04:00
|
|
|
|
|
|
|
// On exit, capture end of last file covered.
|
2016-05-16 17:34:04 +03:00
|
|
|
FileLinesType& CoverageVector = this->Coverage.TotalCoverage[filename];
|
2014-10-24 01:03:30 +04:00
|
|
|
CoverageVector = localCoverageVector;
|
|
|
|
localCoverageVector.clear();
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
|
2014-10-24 01:03:30 +04:00
|
|
|
private:
|
|
|
|
cmCTestCoverageHandlerContainer& Coverage;
|
|
|
|
};
|
|
|
|
|
|
|
|
cmParseBlanketJSCoverage::cmParseBlanketJSCoverage(
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
|
|
|
|
: Coverage(cont)
|
|
|
|
, CTest(ctest)
|
|
|
|
{
|
|
|
|
}
|
2014-10-24 01:03:30 +04:00
|
|
|
|
|
|
|
bool cmParseBlanketJSCoverage::LoadCoverageData(std::vector<std::string> files)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
2014-10-24 01:03:30 +04:00
|
|
|
std::string path;
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Found " << files.size() << " Files" << std::endl,
|
|
|
|
this->Coverage.Quiet);
|
|
|
|
for (i = 0; i < files.size(); i++) {
|
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Reading JSON File " << files[i] << std::endl,
|
|
|
|
this->Coverage.Quiet);
|
2014-10-24 01:03:30 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->ReadJSONFile(files[i])) {
|
2014-10-24 01:03:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
2014-10-24 01:03:30 +04:00
|
|
|
|
2016-05-26 22:58:51 +03:00
|
|
|
bool cmParseBlanketJSCoverage::ReadJSONFile(std::string const& file)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
cmParseBlanketJSCoverage::JSONParser parser(this->Coverage);
|
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Parsing " << file << std::endl, this->Coverage.Quiet);
|
2014-10-24 01:03:30 +04:00
|
|
|
parser.ParseFile(file);
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|