cmParseGTMCoverage.cxx 7.9 KB

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