cmParseGTMCoverage.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "cmStandardIncludes.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "cmSystemTools.h"
  5. #include "cmParseGTMCoverage.h"
  6. #include <cmsys/Directory.hxx>
  7. #include <cmsys/Glob.hxx>
  8. #include <cmsys/FStream.hxx>
  9. cmParseGTMCoverage::cmParseGTMCoverage(cmCTestCoverageHandlerContainer& cont,
  10. cmCTest* ctest)
  11. :cmParseMumpsCoverage(cont, ctest)
  12. {
  13. }
  14. bool cmParseGTMCoverage::LoadCoverageData(const char* d)
  15. {
  16. // load all the .mcov files in the specified directory
  17. cmsys::Directory dir;
  18. if(!dir.Load(d))
  19. {
  20. return false;
  21. }
  22. size_t numf;
  23. unsigned int i;
  24. numf = dir.GetNumberOfFiles();
  25. for (i = 0; i < numf; i++)
  26. {
  27. std::string file = dir.GetFile(i);
  28. if(file != "." && file != ".."
  29. && !cmSystemTools::FileIsDirectory(file.c_str()))
  30. {
  31. std::string path = d;
  32. path += "/";
  33. path += file;
  34. if(cmSystemTools::GetFilenameLastExtension(path) == ".mcov")
  35. {
  36. if(!this->ReadMCovFile(path.c_str()))
  37. {
  38. return false;
  39. }
  40. }
  41. }
  42. }
  43. return true;
  44. }
  45. bool cmParseGTMCoverage::ReadMCovFile(const char* file)
  46. {
  47. cmsys::ifstream in(file);
  48. if(!in)
  49. {
  50. return false;
  51. }
  52. std::string line;
  53. std::string lastfunction;
  54. std::string lastroutine;
  55. std::string lastpath;
  56. int lastoffset = 0;
  57. while( cmSystemTools::GetLineFromStream(in, line))
  58. {
  59. // only look at lines that have coverage data
  60. if(line.find("^ZZCOVERAGE") == line.npos)
  61. {
  62. continue;
  63. }
  64. std::string filepath;
  65. std::string function;
  66. std::string routine;
  67. int linenumber = 0;
  68. int count = 0;
  69. this->ParseMCOVLine(line, routine, function, linenumber, count);
  70. // skip this one
  71. if(routine == "RSEL")
  72. {
  73. continue;
  74. }
  75. // no need to search the file if we just did it
  76. if(function == lastfunction && lastroutine == routine)
  77. {
  78. if(lastpath.size())
  79. {
  80. this->Coverage.TotalCoverage[lastpath][lastoffset + linenumber]
  81. += count;
  82. }
  83. else
  84. {
  85. cmCTestLog(this->CTest, ERROR_MESSAGE,
  86. "Can not find mumps file : "
  87. << lastroutine <<
  88. " referenced in this line of mcov data:\n"
  89. "[" << line << "]\n");
  90. }
  91. continue;
  92. }
  93. // Find the full path to the file
  94. bool found = this->FindMumpsFile(routine, filepath);
  95. if(found)
  96. {
  97. int lineoffset = 0;
  98. if(this->FindFunctionInMumpsFile(filepath,
  99. function,
  100. lineoffset))
  101. {
  102. cmCTestCoverageHandlerContainer::SingleFileCoverageVector&
  103. coverageVector = this->Coverage.TotalCoverage[filepath];
  104. coverageVector[lineoffset + linenumber] += count;
  105. lastoffset = lineoffset;
  106. }
  107. }
  108. else
  109. {
  110. cmCTestLog(this->CTest, ERROR_MESSAGE,
  111. "Can not find mumps file : "
  112. << routine << " referenced in this line of mcov data:\n"
  113. "[" << line << "]\n");
  114. }
  115. lastfunction = function;
  116. lastroutine = routine;
  117. lastpath = filepath;
  118. }
  119. return true;
  120. }
  121. bool cmParseGTMCoverage::FindFunctionInMumpsFile(std::string const& filepath,
  122. std::string const& function,
  123. int& lineoffset)
  124. {
  125. cmsys::ifstream in(filepath.c_str());
  126. if(!in)
  127. {
  128. return false;
  129. }
  130. std::string line;
  131. int linenum = 0;
  132. while( cmSystemTools::GetLineFromStream(in, line))
  133. {
  134. std::string::size_type pos = line.find(function.c_str());
  135. if(pos == 0)
  136. {
  137. char nextchar = line[function.size()];
  138. if(nextchar == ' ' || nextchar == '(')
  139. {
  140. lineoffset = linenum;
  141. return true;
  142. }
  143. }
  144. if(pos == 1)
  145. {
  146. char prevchar = line[0];
  147. char nextchar = line[function.size()+1];
  148. if(prevchar == '%' && (nextchar == ' ' || nextchar == '('))
  149. {
  150. lineoffset = linenum;
  151. return true;
  152. }
  153. }
  154. linenum++; // move to next line count
  155. }
  156. lineoffset = 0;
  157. cmCTestLog(this->CTest, ERROR_MESSAGE,
  158. "Could not find entry point : "
  159. << function << " in " << filepath << "\n");
  160. return false;
  161. }
  162. bool cmParseGTMCoverage::ParseMCOVLine(std::string const& line,
  163. std::string& routine,
  164. std::string& function,
  165. int& linenumber,
  166. int& count)
  167. {
  168. // this method parses lines from the .mcov file
  169. // each line has ^COVERAGE(...) in it, and there
  170. // are several varients of coverage lines:
  171. //
  172. // ^COVERAGE("DIC11","PR1",0)="2:0:0:0"
  173. // ( file , entry, line ) = "number_executed:timing_info"
  174. // ^COVERAGE("%RSEL","SRC")="1:0:0:0"
  175. // ( file , entry ) = "number_executed:timing_info"
  176. // ^COVERAGE("%RSEL","init",8,"FOR_LOOP",1)=1
  177. // ( file , entry, line, IGNORE ) =number_executed
  178. std::vector<std::string> args;
  179. std::string::size_type pos = line.find('(', 0);
  180. // if no ( is found, then return line has no coverage
  181. if(pos == std::string::npos)
  182. {
  183. return false;
  184. }
  185. std::string arg;
  186. bool done = false;
  187. // separate out all of the comma separated arguments found
  188. // in the COVERAGE(...) line
  189. while(line[pos] && !done)
  190. {
  191. // save the char we are looking at
  192. char cur = line[pos];
  193. // , or ) means end of argument
  194. if(cur == ',' || cur == ')')
  195. {
  196. // save the argument into the argument vector
  197. args.push_back(arg);
  198. // start on a new argument
  199. arg = "";
  200. // if we are at the end of the ), then finish while loop
  201. if(cur == ')')
  202. {
  203. done = true;
  204. }
  205. }
  206. else
  207. {
  208. // all chars except ", (, and % get stored in the arg string
  209. if(cur != '\"' && cur != '(' && cur != '%')
  210. {
  211. arg.append(1, line[pos]);
  212. }
  213. }
  214. // move to next char
  215. pos++;
  216. }
  217. // now parse the right hand side of the =
  218. pos = line.find('=');
  219. // no = found, this is an error
  220. if(pos == line.npos)
  221. {
  222. return false;
  223. }
  224. pos++; // move past =
  225. // if the next positing is not a ", then this is a
  226. // COVERAGE(..)=count line and turn the rest of the string
  227. // past the = into an integer and set it to count
  228. if(line[pos] != '\"')
  229. {
  230. count = atoi(line.substr(pos).c_str());
  231. }
  232. else
  233. {
  234. // this means line[pos] is a ", and we have a
  235. // COVERAGE(...)="1:0:0:0" type of line
  236. pos++; // move past "
  237. // find the first : past the "
  238. std::string::size_type pos2 = line.find(':', pos);
  239. // turn the string between the " and the first : into an integer
  240. // and set it to count
  241. count = atoi(line.substr(pos, pos2-pos).c_str());
  242. }
  243. // less then two arguments is an error
  244. if(args.size() < 2)
  245. {
  246. cmCTestLog(this->CTest, ERROR_MESSAGE,
  247. "Error parsing mcov line: [" << line << "]\n");
  248. return false;
  249. }
  250. routine = args[0]; // the routine is the first argument
  251. function = args[1]; // the function in the routine is the second
  252. // in the two argument only format
  253. // ^COVERAGE("%RSEL","SRC"), the line offset is 0
  254. if(args.size() == 2)
  255. {
  256. linenumber = 0;
  257. }
  258. else
  259. {
  260. // this is the format for this line
  261. // ^COVERAGE("%RSEL","SRC",count)
  262. linenumber = atoi(args[2].c_str());
  263. }
  264. return true;
  265. }