cmParseGTMCoverage.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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))
  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. // This section accounts for lines that were previously marked
  105. // as non-executable code (-1), if the parser comes back with
  106. // a non-zero count, increase the count by 1 to push the line
  107. // into the executable code set in addtion to the count found.
  108. if(coverageVector[lineoffset + linenumber] == -1 &&
  109. count > 0)
  110. {
  111. coverageVector[lineoffset + linenumber] += count+1;
  112. }
  113. else
  114. {
  115. coverageVector[lineoffset + linenumber] += count;
  116. }
  117. lastoffset = lineoffset;
  118. }
  119. }
  120. else
  121. {
  122. cmCTestLog(this->CTest, ERROR_MESSAGE,
  123. "Can not find mumps file : "
  124. << routine << " referenced in this line of mcov data:\n"
  125. "[" << line << "]\n");
  126. }
  127. lastfunction = function;
  128. lastroutine = routine;
  129. lastpath = filepath;
  130. }
  131. return true;
  132. }
  133. bool cmParseGTMCoverage::FindFunctionInMumpsFile(std::string const& filepath,
  134. std::string const& function,
  135. int& lineoffset)
  136. {
  137. cmsys::ifstream in(filepath.c_str());
  138. if(!in)
  139. {
  140. return false;
  141. }
  142. std::string line;
  143. int linenum = 0;
  144. while( cmSystemTools::GetLineFromStream(in, line))
  145. {
  146. std::string::size_type pos = line.find(function.c_str());
  147. if(pos == 0)
  148. {
  149. char nextchar = line[function.size()];
  150. if(nextchar == ' ' || nextchar == '('|| nextchar == '\t')
  151. {
  152. lineoffset = linenum;
  153. return true;
  154. }
  155. }
  156. if(pos == 1)
  157. {
  158. char prevchar = line[0];
  159. char nextchar = line[function.size()+1];
  160. if(prevchar == '%' && (nextchar == ' ' || nextchar == '('))
  161. {
  162. lineoffset = linenum;
  163. return true;
  164. }
  165. }
  166. linenum++; // move to next line count
  167. }
  168. lineoffset = 0;
  169. cmCTestLog(this->CTest, ERROR_MESSAGE,
  170. "Could not find entry point : "
  171. << function << " in " << filepath << "\n");
  172. return false;
  173. }
  174. bool cmParseGTMCoverage::ParseMCOVLine(std::string const& line,
  175. std::string& routine,
  176. std::string& function,
  177. int& linenumber,
  178. int& count)
  179. {
  180. // this method parses lines from the .mcov file
  181. // each line has ^COVERAGE(...) in it, and there
  182. // are several varients of coverage lines:
  183. //
  184. // ^COVERAGE("DIC11","PR1",0)="2:0:0:0"
  185. // ( file , entry, line ) = "number_executed:timing_info"
  186. // ^COVERAGE("%RSEL","SRC")="1:0:0:0"
  187. // ( file , entry ) = "number_executed:timing_info"
  188. // ^COVERAGE("%RSEL","init",8,"FOR_LOOP",1)=1
  189. // ( file , entry, line, IGNORE ) =number_executed
  190. std::vector<std::string> args;
  191. std::string::size_type pos = line.find('(', 0);
  192. // if no ( is found, then return line has no coverage
  193. if(pos == std::string::npos)
  194. {
  195. return false;
  196. }
  197. std::string arg;
  198. bool done = false;
  199. // separate out all of the comma separated arguments found
  200. // in the COVERAGE(...) line
  201. while(line[pos] && !done)
  202. {
  203. // save the char we are looking at
  204. char cur = line[pos];
  205. // , or ) means end of argument
  206. if(cur == ',' || cur == ')')
  207. {
  208. // save the argument into the argument vector
  209. args.push_back(arg);
  210. // start on a new argument
  211. arg = "";
  212. // if we are at the end of the ), then finish while loop
  213. if(cur == ')')
  214. {
  215. done = true;
  216. }
  217. }
  218. else
  219. {
  220. // all chars except ", (, and % get stored in the arg string
  221. if(cur != '\"' && cur != '(' && cur != '%')
  222. {
  223. arg.append(1, line[pos]);
  224. }
  225. }
  226. // move to next char
  227. pos++;
  228. }
  229. // now parse the right hand side of the =
  230. pos = line.find('=');
  231. // no = found, this is an error
  232. if(pos == line.npos)
  233. {
  234. return false;
  235. }
  236. pos++; // move past =
  237. // if the next positing is not a ", then this is a
  238. // COVERAGE(..)=count line and turn the rest of the string
  239. // past the = into an integer and set it to count
  240. if(line[pos] != '\"')
  241. {
  242. count = atoi(line.substr(pos).c_str());
  243. }
  244. else
  245. {
  246. // this means line[pos] is a ", and we have a
  247. // COVERAGE(...)="1:0:0:0" type of line
  248. pos++; // move past "
  249. // find the first : past the "
  250. std::string::size_type pos2 = line.find(':', pos);
  251. // turn the string between the " and the first : into an integer
  252. // and set it to count
  253. count = atoi(line.substr(pos, pos2-pos).c_str());
  254. }
  255. // less then two arguments is an error
  256. if(args.size() < 2)
  257. {
  258. cmCTestLog(this->CTest, ERROR_MESSAGE,
  259. "Error parsing mcov line: [" << line << "]\n");
  260. return false;
  261. }
  262. routine = args[0]; // the routine is the first argument
  263. function = args[1]; // the function in the routine is the second
  264. // in the two argument only format
  265. // ^COVERAGE("%RSEL","SRC"), the line offset is 0
  266. if(args.size() == 2)
  267. {
  268. // To avoid double counting of line 0 of each entry point,
  269. // Don't count the lines that do not give an explicit line
  270. // number.
  271. routine="";
  272. function="";
  273. }
  274. else
  275. {
  276. // this is the format for this line
  277. // ^COVERAGE("%RSEL","SRC",count)
  278. linenumber = atoi(args[2].c_str());
  279. }
  280. return true;
  281. }