cmake/Source/CTest/cmParseGTMCoverage.cxx

241 lines
7.6 KiB
C++
Raw Normal View History

2012-06-27 20:52:58 +03:00
#include "cmParseGTMCoverage.h"
2016-07-09 11:21:54 +02:00
#include "cmSystemTools.h"
2012-06-27 20:52:58 +03:00
#include <cmsys/Directory.hxx>
2014-08-03 19:52:23 +02:00
#include <cmsys/FStream.hxx>
2016-07-09 11:21:54 +02:00
#include <cmsys/Glob.hxx>
#include <stdio.h>
#include <stdlib.h>
2012-06-27 20:52:58 +03:00
cmParseGTMCoverage::cmParseGTMCoverage(cmCTestCoverageHandlerContainer& cont,
cmCTest* ctest)
2016-07-09 11:21:54 +02:00
: cmParseMumpsCoverage(cont, ctest)
2012-06-27 20:52:58 +03:00
{
}
bool cmParseGTMCoverage::LoadCoverageData(const char* d)
{
// load all the .mcov files in the specified directory
cmsys::Directory dir;
2016-07-09 11:21:54 +02:00
if (!dir.Load(d)) {
2012-06-27 20:52:58 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
size_t numf;
unsigned int i;
numf = dir.GetNumberOfFiles();
2016-07-09 11:21:54 +02:00
for (i = 0; i < numf; i++) {
2012-06-27 20:52:58 +03:00
std::string file = dir.GetFile(i);
2016-07-09 11:21:54 +02:00
if (file != "." && file != ".." && !cmSystemTools::FileIsDirectory(file)) {
2012-06-27 20:52:58 +03:00
std::string path = d;
path += "/";
path += file;
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetFilenameLastExtension(path) == ".mcov") {
if (!this->ReadMCovFile(path.c_str())) {
2012-06-27 20:52:58 +03:00
return false;
}
}
}
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
return true;
}
bool cmParseGTMCoverage::ReadMCovFile(const char* file)
{
2014-08-03 19:52:23 +02:00
cmsys::ifstream in(file);
2016-07-09 11:21:54 +02:00
if (!in) {
2012-06-27 20:52:58 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
std::string line;
std::string lastfunction;
std::string lastroutine;
std::string lastpath;
int lastoffset = 0;
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(in, line)) {
2012-06-27 20:52:58 +03:00
// only look at lines that have coverage data
2016-07-09 11:21:54 +02:00
if (line.find("^ZZCOVERAGE") == line.npos) {
2012-06-27 20:52:58 +03:00
continue;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
std::string filepath;
std::string function;
std::string routine;
int linenumber = 0;
int count = 0;
this->ParseMCOVLine(line, routine, function, linenumber, count);
// skip this one
2016-07-09 11:21:54 +02:00
if (routine == "RSEL") {
2012-06-27 20:52:58 +03:00
continue;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
// no need to search the file if we just did it
2016-07-09 11:21:54 +02:00
if (function == lastfunction && lastroutine == routine) {
if (!lastpath.empty()) {
this->Coverage.TotalCoverage[lastpath][lastoffset + linenumber] +=
count;
} else {
cmCTestLog(this->CTest, ERROR_MESSAGE, "Can not find mumps file : "
<< lastroutine
<< " referenced in this line of mcov data:\n"
"["
<< line << "]\n");
2012-06-27 20:52:58 +03:00
}
2016-07-09 11:21:54 +02:00
continue;
}
2012-06-27 20:52:58 +03:00
// Find the full path to the file
bool found = this->FindMumpsFile(routine, filepath);
2016-07-09 11:21:54 +02:00
if (found) {
2013-11-03 12:27:13 +02:00
int lineoffset = 0;
2016-07-09 11:21:54 +02:00
if (this->FindFunctionInMumpsFile(filepath, function, lineoffset)) {
2012-06-27 20:52:58 +03:00
cmCTestCoverageHandlerContainer::SingleFileCoverageVector&
coverageVector = this->Coverage.TotalCoverage[filepath];
2015-04-27 22:25:09 +02:00
// This section accounts for lines that were previously marked
// as non-executable code (-1), if the parser comes back with
// a non-zero count, increase the count by 1 to push the line
// into the executable code set in addtion to the count found.
2016-07-09 11:21:54 +02:00
if (coverageVector[lineoffset + linenumber] == -1 && count > 0) {
coverageVector[lineoffset + linenumber] += count + 1;
} else {
2015-04-27 22:25:09 +02:00
coverageVector[lineoffset + linenumber] += count;
2012-06-27 20:52:58 +03:00
}
2016-07-09 11:21:54 +02:00
lastoffset = lineoffset;
2012-06-27 20:52:58 +03:00
}
2016-07-09 11:21:54 +02:00
} else {
cmCTestLog(this->CTest, ERROR_MESSAGE, "Can not find mumps file : "
<< routine << " referenced in this line of mcov data:\n"
"["
<< line << "]\n");
}
2012-06-27 20:52:58 +03:00
lastfunction = function;
lastroutine = routine;
lastpath = filepath;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
return true;
}
bool cmParseGTMCoverage::FindFunctionInMumpsFile(std::string const& filepath,
std::string const& function,
int& lineoffset)
{
2014-08-03 19:52:23 +02:00
cmsys::ifstream in(filepath.c_str());
2016-07-09 11:21:54 +02:00
if (!in) {
2012-06-27 20:52:58 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
std::string line;
int linenum = 0;
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(in, line)) {
std::string::size_type pos = line.find(function);
if (pos == 0) {
2012-06-27 20:52:58 +03:00
char nextchar = line[function.size()];
2016-07-09 11:21:54 +02:00
if (nextchar == ' ' || nextchar == '(' || nextchar == '\t') {
2012-06-27 20:52:58 +03:00
lineoffset = linenum;
return true;
}
2016-07-09 11:21:54 +02:00
}
if (pos == 1) {
2012-06-27 20:52:58 +03:00
char prevchar = line[0];
2016-07-09 11:21:54 +02:00
char nextchar = line[function.size() + 1];
if (prevchar == '%' && (nextchar == ' ' || nextchar == '(')) {
2012-06-27 20:52:58 +03:00
lineoffset = linenum;
return true;
}
}
2016-07-09 11:21:54 +02:00
linenum++; // move to next line count
}
2012-06-27 20:52:58 +03:00
lineoffset = 0;
2016-07-09 11:21:54 +02:00
cmCTestLog(this->CTest, ERROR_MESSAGE, "Could not find entry point : "
<< function << " in " << filepath << "\n");
2012-06-27 20:52:58 +03:00
return false;
}
bool cmParseGTMCoverage::ParseMCOVLine(std::string const& line,
std::string& routine,
2016-07-09 11:21:54 +02:00
std::string& function, int& linenumber,
2012-06-27 20:52:58 +03:00
int& count)
{
// this method parses lines from the .mcov file
// each line has ^COVERAGE(...) in it, and there
// are several varients of coverage lines:
//
// ^COVERAGE("DIC11","PR1",0)="2:0:0:0"
// ( file , entry, line ) = "number_executed:timing_info"
// ^COVERAGE("%RSEL","SRC")="1:0:0:0"
// ( file , entry ) = "number_executed:timing_info"
// ^COVERAGE("%RSEL","init",8,"FOR_LOOP",1)=1
// ( file , entry, line, IGNORE ) =number_executed
2015-04-27 22:25:09 +02:00
std::vector<std::string> args;
2012-06-27 20:52:58 +03:00
std::string::size_type pos = line.find('(', 0);
// if no ( is found, then return line has no coverage
2016-07-09 11:21:54 +02:00
if (pos == std::string::npos) {
2012-06-27 20:52:58 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
std::string arg;
bool done = false;
// separate out all of the comma separated arguments found
// in the COVERAGE(...) line
2016-07-09 11:21:54 +02:00
while (line[pos] && !done) {
2012-06-27 20:52:58 +03:00
// save the char we are looking at
char cur = line[pos];
// , or ) means end of argument
2016-07-09 11:21:54 +02:00
if (cur == ',' || cur == ')') {
2012-06-27 20:52:58 +03:00
// save the argument into the argument vector
args.push_back(arg);
// start on a new argument
arg = "";
// if we are at the end of the ), then finish while loop
2016-07-09 11:21:54 +02:00
if (cur == ')') {
2012-06-27 20:52:58 +03:00
done = true;
}
2016-07-09 11:21:54 +02:00
} else {
2012-06-27 20:52:58 +03:00
// all chars except ", (, and % get stored in the arg string
2016-07-09 11:21:54 +02:00
if (cur != '\"' && cur != '(' && cur != '%') {
2012-06-27 20:52:58 +03:00
arg.append(1, line[pos]);
}
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
// move to next char
pos++;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
// now parse the right hand side of the =
pos = line.find('=');
// no = found, this is an error
2016-07-09 11:21:54 +02:00
if (pos == line.npos) {
2012-06-27 20:52:58 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
pos++; // move past =
// if the next positing is not a ", then this is a
// COVERAGE(..)=count line and turn the rest of the string
// past the = into an integer and set it to count
2016-07-09 11:21:54 +02:00
if (line[pos] != '\"') {
2012-06-27 20:52:58 +03:00
count = atoi(line.substr(pos).c_str());
2016-07-09 11:21:54 +02:00
} else {
2012-06-27 20:52:58 +03:00
// this means line[pos] is a ", and we have a
// COVERAGE(...)="1:0:0:0" type of line
pos++; // move past "
// find the first : past the "
std::string::size_type pos2 = line.find(':', pos);
// turn the string between the " and the first : into an integer
// and set it to count
2016-07-09 11:21:54 +02:00
count = atoi(line.substr(pos, pos2 - pos).c_str());
}
2012-06-27 20:52:58 +03:00
// less then two arguments is an error
2016-07-09 11:21:54 +02:00
if (args.size() < 2) {
cmCTestLog(this->CTest, ERROR_MESSAGE, "Error parsing mcov line: ["
<< line << "]\n");
2012-06-27 20:52:58 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
routine = args[0]; // the routine is the first argument
2012-06-27 20:52:58 +03:00
function = args[1]; // the function in the routine is the second
// in the two argument only format
// ^COVERAGE("%RSEL","SRC"), the line offset is 0
2016-07-09 11:21:54 +02:00
if (args.size() == 2) {
2015-04-27 22:25:09 +02:00
// To avoid double counting of line 0 of each entry point,
// Don't count the lines that do not give an explicit line
// number.
2016-07-09 11:21:54 +02:00
routine = "";
function = "";
} else {
2012-06-27 20:52:58 +03:00
// this is the format for this line
// ^COVERAGE("%RSEL","SRC",count)
linenumber = atoi(args[2].c_str());
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
return true;
}