cmParseGTMCoverage.cxx 7.9 KB

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