2016-04-29 16:40:20 +03:00
|
|
|
#include "cmParseDelphiCoverage.h"
|
|
|
|
|
2014-09-29 22:52:10 +04:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "cmXMLParser.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-09-29 22:52:10 +04:00
|
|
|
|
|
|
|
class cmParseDelphiCoverage::HTMLParser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
|
2016-05-16 17:34:04 +03:00
|
|
|
FileLinesType;
|
2014-09-29 22:52:10 +04:00
|
|
|
HTMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
|
2016-05-16 17:34:04 +03:00
|
|
|
: CTest(ctest)
|
|
|
|
, Coverage(cont)
|
|
|
|
{
|
|
|
|
}
|
2014-09-29 22:52:10 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
virtual ~HTMLParser() {}
|
2014-09-29 22:52:10 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool initializeDelphiFile(
|
2016-05-26 22:58:51 +03:00
|
|
|
std::string const& filename,
|
2016-05-16 17:34:04 +03:00
|
|
|
cmParseDelphiCoverage::HTMLParser::FileLinesType& coverageVector)
|
|
|
|
{
|
2014-09-29 22:52:10 +04:00
|
|
|
std::string line;
|
|
|
|
size_t comPos;
|
|
|
|
size_t semiPos;
|
2016-05-16 17:34:04 +03:00
|
|
|
bool blockComFlag = false;
|
|
|
|
bool lineComFlag = false;
|
2014-09-29 22:52:10 +04:00
|
|
|
std::vector<std::string> beginSet;
|
|
|
|
cmsys::ifstream in(filename.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!in) {
|
2014-09-29 22:52:10 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
while (cmSystemTools::GetLineFromStream(in, line)) {
|
|
|
|
lineComFlag = false;
|
2014-09-29 22:52:10 +04:00
|
|
|
// Unique cases found in lines.
|
|
|
|
size_t beginPos = line.find("begin");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// Check that the begin is the first non-space string on the line
|
|
|
|
if ((beginPos == line.find_first_not_of(' ')) && beginPos != line.npos) {
|
2014-09-29 22:52:10 +04:00
|
|
|
beginSet.push_back("begin");
|
|
|
|
coverageVector.push_back(-1);
|
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (line.find('{') != line.npos) {
|
|
|
|
blockComFlag = true;
|
|
|
|
} else if (line.find('}') != line.npos) {
|
|
|
|
blockComFlag = false;
|
2014-09-29 22:52:10 +04:00
|
|
|
coverageVector.push_back(-1);
|
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if ((line.find("end;") != line.npos) && !beginSet.empty()) {
|
2014-09-29 22:52:10 +04:00
|
|
|
beginSet.pop_back();
|
|
|
|
coverageVector.push_back(-1);
|
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-09-29 22:52:10 +04:00
|
|
|
|
|
|
|
// This checks for comments after lines of code, finding the
|
|
|
|
// comment symbol after the ending semicolon.
|
|
|
|
comPos = line.find("//");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (comPos != line.npos) {
|
|
|
|
semiPos = line.find(';');
|
|
|
|
if (comPos < semiPos) {
|
|
|
|
lineComFlag = true;
|
2014-09-29 22:52:10 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
// Based up what was found, add a line to the coverageVector
|
|
|
|
if (!beginSet.empty() && line != "" && !blockComFlag && !lineComFlag) {
|
2014-09-29 22:52:10 +04:00
|
|
|
coverageVector.push_back(0);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2014-09-29 22:52:10 +04:00
|
|
|
coverageVector.push_back(-1);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
2014-09-29 22:52:10 +04:00
|
|
|
bool ParseFile(const char* file)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
std::string line = file;
|
2014-09-29 22:52:10 +04:00
|
|
|
std::string lineresult;
|
|
|
|
std::string lastroutine;
|
|
|
|
std::string filename;
|
|
|
|
std::string filelineoffset;
|
|
|
|
size_t afterLineNum = 0;
|
|
|
|
size_t lastoffset = 0;
|
|
|
|
size_t endcovpos = 0;
|
|
|
|
size_t endnamepos = 0;
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This first 'while' section goes through the found HTML
|
|
|
|
* file name and attempts to capture the source file name
|
|
|
|
* which is set as part of the HTML file name: the name of
|
|
|
|
* the file is found in parenthesis '()'
|
|
|
|
*
|
|
|
|
* See test HTML file name: UTCovTest(UTCovTest.pas).html.
|
|
|
|
*
|
|
|
|
* Find the text inside each pair of parenthesis and check
|
|
|
|
* to see if it ends in '.pas'. If it can't be found,
|
|
|
|
* exit the function.
|
|
|
|
*/
|
2016-05-16 17:34:04 +03:00
|
|
|
while (true) {
|
|
|
|
lastoffset = line.find('(', pos);
|
|
|
|
if (lastoffset == line.npos) {
|
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, endnamepos
|
|
|
|
<< "File not found " << lastoffset << std::endl,
|
|
|
|
this->Coverage.Quiet);
|
2014-09-29 22:52:10 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
endnamepos = line.find(')', lastoffset);
|
|
|
|
filename = line.substr(lastoffset + 1, (endnamepos - 1) - lastoffset);
|
|
|
|
if (filename.find(".pas") != filename.npos) {
|
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Coverage found for file: " << filename
|
|
|
|
<< std::endl,
|
|
|
|
this->Coverage.Quiet);
|
2014-09-29 22:52:10 +04:00
|
|
|
break;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
pos = lastoffset + 1;
|
|
|
|
}
|
2014-09-29 22:52:10 +04:00
|
|
|
/*
|
|
|
|
* Glob through the source directory for the
|
|
|
|
* file found above
|
|
|
|
*/
|
|
|
|
cmsys::Glob gl;
|
|
|
|
gl.RecurseOn();
|
|
|
|
gl.RecurseThroughSymlinksOff();
|
|
|
|
std::string glob = Coverage.SourceDir + "*/" + filename;
|
|
|
|
gl.FindFiles(glob);
|
|
|
|
std::vector<std::string> const& files = gl.GetFiles();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (files.empty()) {
|
2014-09-29 22:52:10 +04:00
|
|
|
/*
|
|
|
|
* If that doesn't find any matching files
|
|
|
|
* return a failure.
|
|
|
|
*/
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Unable to find file matching" << glob << std::endl,
|
|
|
|
this->Coverage.Quiet);
|
2014-09-29 22:52:10 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
FileLinesType& coverageVector = this->Coverage.TotalCoverage[files[0]];
|
2014-09-29 22:52:10 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the file to have all code between 'begin' and
|
|
|
|
* 'end' tags marked as executable
|
|
|
|
*/
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
this->initializeDelphiFile(files[0], coverageVector);
|
2014-09-29 22:52:10 +04:00
|
|
|
|
|
|
|
cmsys::ifstream in(file);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!in) {
|
2014-09-29 22:52:10 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-09-29 22:52:10 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now read the HTML file, looking for the lines that have an
|
|
|
|
* "inline" in it. Then parse out the "class" value of that
|
|
|
|
* line to determine if the line is executed or not.
|
|
|
|
*
|
|
|
|
* Sample HTML line:
|
|
|
|
*
|
|
|
|
* <tr class="covered"><td>47</td><td><pre style="display:inline;">
|
|
|
|
* CheckEquals(1,2-1);</pre></td></tr>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
while (cmSystemTools::GetLineFromStream(in, line)) {
|
|
|
|
if (line.find("inline") == line.npos) {
|
2014-09-29 22:52:10 +04:00
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-09-29 22:52:10 +04:00
|
|
|
|
|
|
|
lastoffset = line.find("class=");
|
2016-05-24 22:24:10 +03:00
|
|
|
endcovpos = line.find('>', lastoffset);
|
2016-05-16 17:34:04 +03:00
|
|
|
lineresult = line.substr(lastoffset + 7, (endcovpos - 8) - lastoffset);
|
|
|
|
|
|
|
|
if (lineresult == "covered") {
|
|
|
|
afterLineNum = line.find('<', endcovpos + 5);
|
|
|
|
filelineoffset =
|
|
|
|
line.substr(endcovpos + 5, afterLineNum - (endcovpos + 5));
|
|
|
|
coverageVector[atoi(filelineoffset.c_str()) - 1] = 1;
|
2014-09-29 22:52:10 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
2014-09-29 22:52:10 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
private:
|
|
|
|
cmCTest* CTest;
|
|
|
|
cmCTestCoverageHandlerContainer& Coverage;
|
2014-09-29 22:52:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
cmParseDelphiCoverage::cmParseDelphiCoverage(
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
|
|
|
|
: Coverage(cont)
|
|
|
|
, CTest(ctest)
|
|
|
|
{
|
|
|
|
}
|
2014-09-29 22:52:10 +04:00
|
|
|
|
|
|
|
bool cmParseDelphiCoverage::LoadCoverageData(
|
2016-05-26 22:58:51 +03:00
|
|
|
std::vector<std::string> const& files)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2014-09-29 22:52:10 +04:00
|
|
|
size_t i;
|
|
|
|
std::string path;
|
|
|
|
size_t numf = files.size();
|
2016-05-16 17:34:04 +03:00
|
|
|
for (i = 0; i < numf; i++) {
|
2014-09-29 22:52:10 +04:00
|
|
|
path = files[i];
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
|
|
|
|
"Reading HTML File " << path << std::endl,
|
|
|
|
this->Coverage.Quiet);
|
|
|
|
if (cmSystemTools::GetFilenameLastExtension(path) == ".html") {
|
|
|
|
if (!this->ReadDelphiHTML(path.c_str())) {
|
2014-09-29 22:52:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 14:24:58 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
2014-09-29 22:52:10 +04:00
|
|
|
|
|
|
|
bool cmParseDelphiCoverage::ReadDelphiHTML(const char* file)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
cmParseDelphiCoverage::HTMLParser parser(this->CTest, this->Coverage);
|
2014-09-29 22:52:10 +04:00
|
|
|
parser.ParseFile(file);
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|