cmParseGTMCoverage.cxx 8.0 KB

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