cmParseGTMCoverage.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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, "Can not find mumps file : "
  74. << lastroutine
  75. << " referenced in this line of mcov data:\n"
  76. "["
  77. << line << "]\n");
  78. }
  79. continue;
  80. }
  81. // Find the full path to the file
  82. bool found = this->FindMumpsFile(routine, filepath);
  83. if (found) {
  84. int lineoffset = 0;
  85. if (this->FindFunctionInMumpsFile(filepath, function, lineoffset)) {
  86. cmCTestCoverageHandlerContainer::SingleFileCoverageVector&
  87. coverageVector = this->Coverage.TotalCoverage[filepath];
  88. // This section accounts for lines that were previously marked
  89. // as non-executable code (-1), if the parser comes back with
  90. // a non-zero count, increase the count by 1 to push the line
  91. // into the executable code set in addition to the count found.
  92. if (coverageVector[lineoffset + linenumber] == -1 && count > 0) {
  93. coverageVector[lineoffset + linenumber] += count + 1;
  94. } else {
  95. coverageVector[lineoffset + linenumber] += count;
  96. }
  97. lastoffset = lineoffset;
  98. }
  99. } else {
  100. cmCTestLog(this->CTest, ERROR_MESSAGE, "Can not find mumps file : "
  101. << routine << " referenced in this line of mcov data:\n"
  102. "["
  103. << line << "]\n");
  104. }
  105. lastfunction = function;
  106. lastroutine = routine;
  107. lastpath = filepath;
  108. }
  109. return true;
  110. }
  111. bool cmParseGTMCoverage::FindFunctionInMumpsFile(std::string const& filepath,
  112. std::string const& function,
  113. int& lineoffset)
  114. {
  115. cmsys::ifstream in(filepath.c_str());
  116. if (!in) {
  117. return false;
  118. }
  119. std::string line;
  120. int linenum = 0;
  121. while (cmSystemTools::GetLineFromStream(in, line)) {
  122. std::string::size_type pos = line.find(function);
  123. if (pos == 0) {
  124. char nextchar = line[function.size()];
  125. if (nextchar == ' ' || nextchar == '(' || nextchar == '\t') {
  126. lineoffset = linenum;
  127. return true;
  128. }
  129. }
  130. if (pos == 1) {
  131. char prevchar = line[0];
  132. char nextchar = line[function.size() + 1];
  133. if (prevchar == '%' && (nextchar == ' ' || nextchar == '(')) {
  134. lineoffset = linenum;
  135. return true;
  136. }
  137. }
  138. linenum++; // move to next line count
  139. }
  140. lineoffset = 0;
  141. cmCTestLog(this->CTest, ERROR_MESSAGE, "Could not find entry point : "
  142. << function << " in " << filepath << "\n");
  143. return false;
  144. }
  145. bool cmParseGTMCoverage::ParseMCOVLine(std::string const& line,
  146. std::string& routine,
  147. std::string& function, int& linenumber,
  148. int& count)
  149. {
  150. // this method parses lines from the .mcov file
  151. // each line has ^COVERAGE(...) in it, and there
  152. // are several varients of coverage lines:
  153. //
  154. // ^COVERAGE("DIC11","PR1",0)="2:0:0:0"
  155. // ( file , entry, line ) = "number_executed:timing_info"
  156. // ^COVERAGE("%RSEL","SRC")="1:0:0:0"
  157. // ( file , entry ) = "number_executed:timing_info"
  158. // ^COVERAGE("%RSEL","init",8,"FOR_LOOP",1)=1
  159. // ( file , entry, line, IGNORE ) =number_executed
  160. std::vector<std::string> args;
  161. std::string::size_type pos = line.find('(', 0);
  162. // if no ( is found, then return line has no coverage
  163. if (pos == std::string::npos) {
  164. return false;
  165. }
  166. std::string arg;
  167. bool done = false;
  168. // separate out all of the comma separated arguments found
  169. // in the COVERAGE(...) line
  170. while (line[pos] && !done) {
  171. // save the char we are looking at
  172. char cur = line[pos];
  173. // , or ) means end of argument
  174. if (cur == ',' || cur == ')') {
  175. // save the argument into the argument vector
  176. args.push_back(arg);
  177. // start on a new argument
  178. arg.clear();
  179. // if we are at the end of the ), then finish while loop
  180. if (cur == ')') {
  181. done = true;
  182. }
  183. } else {
  184. // all chars except ", (, and % get stored in the arg string
  185. if (cur != '\"' && cur != '(' && cur != '%') {
  186. arg.append(1, line[pos]);
  187. }
  188. }
  189. // move to next char
  190. pos++;
  191. }
  192. // now parse the right hand side of the =
  193. pos = line.find('=');
  194. // no = found, this is an error
  195. if (pos == std::string::npos) {
  196. return false;
  197. }
  198. pos++; // move past =
  199. // if the next positing is not a ", then this is a
  200. // COVERAGE(..)=count line and turn the rest of the string
  201. // past the = into an integer and set it to count
  202. if (line[pos] != '\"') {
  203. count = atoi(line.substr(pos).c_str());
  204. } else {
  205. // this means line[pos] is a ", and we have a
  206. // COVERAGE(...)="1:0:0:0" type of line
  207. pos++; // move past "
  208. // find the first : past the "
  209. std::string::size_type pos2 = line.find(':', pos);
  210. // turn the string between the " and the first : into an integer
  211. // and set it to count
  212. count = atoi(line.substr(pos, pos2 - pos).c_str());
  213. }
  214. // less then two arguments is an error
  215. if (args.size() < 2) {
  216. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error parsing mcov line: ["
  217. << line << "]\n");
  218. return false;
  219. }
  220. routine = args[0]; // the routine is the first argument
  221. function = args[1]; // the function in the routine is the second
  222. // in the two argument only format
  223. // ^COVERAGE("%RSEL","SRC"), the line offset is 0
  224. if (args.size() == 2) {
  225. // To avoid double counting of line 0 of each entry point,
  226. // Don't count the lines that do not give an explicit line
  227. // number.
  228. routine.clear();
  229. function.clear();
  230. } else {
  231. // this is the format for this line
  232. // ^COVERAGE("%RSEL","SRC",count)
  233. linenumber = atoi(args[2].c_str());
  234. }
  235. return true;
  236. }