cmParseGTMCoverage.cxx 7.4 KB

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