cmParseGTMCoverage.cxx 7.6 KB

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