CMake/Source/CTest/cmParseBlanketJSCoverage.cxx

140 lines
4.1 KiB
C++
Raw Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
2016-09-27 22:01:08 +03:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmParseBlanketJSCoverage.h"
#include "cmCTest.h"
#include "cmCTestCoverageHandler.h"
#include "cmSystemTools.h"
#include <cmsys/FStream.hxx>
#include <stdio.h>
#include <stdlib.h>
class cmParseBlanketJSCoverage::JSONParser
{
public:
typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
FileLinesType;
JSONParser(cmCTestCoverageHandlerContainer& cont)
: Coverage(cont)
{
}
virtual ~JSONParser() {}
std::string getValue(std::string line, int type)
{
size_t begIndex;
size_t endIndex;
endIndex = line.rfind(',');
begIndex = line.find_first_of(':');
if (type == 0) {
// A unique substring to remove the extra characters
// around the files name in the JSON (extra " and ,)
std::string foundFileName =
line.substr(begIndex + 3, endIndex - (begIndex + 4));
return foundFileName;
}
2016-08-18 21:04:21 +03:00
return line.substr(begIndex, line.npos);
}
bool ParseFile(std::string const& file)
{
FileLinesType localCoverageVector;
std::string filename;
bool foundFile = false;
bool inSource = false;
std::string covResult;
std::string line;
cmsys::ifstream in(file.c_str());
if (!in) {
return false;
}
while (cmSystemTools::GetLineFromStream(in, line)) {
if (line.find("filename") != line.npos) {
if (foundFile) {
/*
* Upon finding a second file name, generate a
* vector within the total coverage to capture the
* information in the local vector
*/
FileLinesType& CoverageVector =
this->Coverage.TotalCoverage[filename];
CoverageVector = localCoverageVector;
localCoverageVector.clear();
}
foundFile = true;
inSource = false;
filename = getValue(line, 0);
} else if ((line.find("coverage") != line.npos) && foundFile &&
inSource) {
/*
* 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
*/
std::string result = getValue(line, 1);
result = result.substr(2, result.npos);
if (result == "\"\"") {
// Empty quotation marks indicate that the
// line is not executable
localCoverageVector.push_back(-1);
} else {
// Else, it contains the number of time executed
localCoverageVector.push_back(atoi(result.c_str()));
}
} else if (line.find("source") != line.npos) {
inSource = true;
}
}
// On exit, capture end of last file covered.
FileLinesType& CoverageVector = this->Coverage.TotalCoverage[filename];
CoverageVector = localCoverageVector;
localCoverageVector.clear();
return true;
}
private:
cmCTestCoverageHandlerContainer& Coverage;
};
cmParseBlanketJSCoverage::cmParseBlanketJSCoverage(
cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
: Coverage(cont)
, CTest(ctest)
{
}
bool cmParseBlanketJSCoverage::LoadCoverageData(std::vector<std::string> files)
{
size_t i = 0;
std::string path;
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);
if (!this->ReadJSONFile(files[i])) {
return false;
}
}
return true;
}
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);
parser.ParseFile(file);
return true;
}