cmParseGTMCoverage.cxx 7.8 KB

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