cmParseCacheCoverage.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "cmParseCacheCoverage.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 <utility>
  12. cmParseCacheCoverage::cmParseCacheCoverage(
  13. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  14. : cmParseMumpsCoverage(cont, ctest)
  15. {
  16. }
  17. bool cmParseCacheCoverage::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 = cmStrCat(d, '/', file);
  31. if (cmSystemTools::GetFilenameLastExtension(path) == ".cmcov") {
  32. if (!this->ReadCMCovFile(path.c_str())) {
  33. return false;
  34. }
  35. }
  36. }
  37. }
  38. return true;
  39. }
  40. // not currently used, but leave it in case we want it in the future
  41. void cmParseCacheCoverage::RemoveUnCoveredFiles()
  42. {
  43. // loop over the coverage data computed and remove all files
  44. // that only have -1 or 0 for the lines.
  45. cmCTestCoverageHandlerContainer::TotalCoverageMap::iterator ci =
  46. this->Coverage.TotalCoverage.begin();
  47. while (ci != this->Coverage.TotalCoverage.end()) {
  48. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& v = ci->second;
  49. bool nothing = true;
  50. for (int i : v) {
  51. if (i > 0) {
  52. nothing = false;
  53. break;
  54. }
  55. }
  56. if (nothing) {
  57. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  58. "No coverage found in: " << ci->first << std::endl,
  59. this->Coverage.Quiet);
  60. this->Coverage.TotalCoverage.erase(ci++);
  61. } else {
  62. ++ci;
  63. }
  64. }
  65. }
  66. bool cmParseCacheCoverage::SplitString(std::vector<std::string>& args,
  67. std::string const& line)
  68. {
  69. std::string::size_type pos1 = 0;
  70. std::string::size_type pos2 = line.find(',', 0);
  71. if (pos2 == std::string::npos) {
  72. return false;
  73. }
  74. std::string arg;
  75. while (pos2 != std::string::npos) {
  76. arg = line.substr(pos1, pos2 - pos1);
  77. args.push_back(arg);
  78. pos1 = pos2 + 1;
  79. pos2 = line.find(',', pos1);
  80. }
  81. arg = line.substr(pos1);
  82. args.push_back(arg);
  83. return true;
  84. }
  85. bool cmParseCacheCoverage::ReadCMCovFile(const char* file)
  86. {
  87. cmsys::ifstream in(file);
  88. if (!in) {
  89. cmCTestLog(this->CTest, ERROR_MESSAGE, "Can not open : " << file << "\n");
  90. return false;
  91. }
  92. std::string line;
  93. std::vector<std::string> separateLine;
  94. if (!cmSystemTools::GetLineFromStream(in, line)) {
  95. cmCTestLog(this->CTest, ERROR_MESSAGE,
  96. "Empty file : " << file
  97. << " referenced in this line of cmcov data:\n"
  98. "["
  99. << line << "]\n");
  100. return false;
  101. }
  102. separateLine.clear();
  103. this->SplitString(separateLine, line);
  104. if (separateLine.size() != 4 || separateLine[0] != "Routine" ||
  105. separateLine[1] != "Line" || separateLine[2] != "RtnLine" ||
  106. separateLine[3] != "Code") {
  107. cmCTestLog(this->CTest, ERROR_MESSAGE,
  108. "Bad first line of cmcov file : " << file
  109. << " line:\n"
  110. "["
  111. << line << "]\n");
  112. }
  113. std::string routine;
  114. std::string filepath;
  115. while (cmSystemTools::GetLineFromStream(in, line)) {
  116. // clear out line argument vector
  117. separateLine.clear();
  118. // parse the comma separated line
  119. this->SplitString(separateLine, line);
  120. // might have more because code could have a quoted , in it
  121. // but we only care about the first 3 args anyway
  122. if (separateLine.size() < 4) {
  123. cmCTestLog(this->CTest, ERROR_MESSAGE,
  124. "Bad line of cmcov file expected at least 4 found: "
  125. << separateLine.size() << " " << file
  126. << " line:\n"
  127. "["
  128. << line << "]\n");
  129. for (std::string::size_type i = 0; i < separateLine.size(); ++i) {
  130. cmCTestLog(this->CTest, ERROR_MESSAGE, "" << separateLine[1] << " ");
  131. }
  132. cmCTestLog(this->CTest, ERROR_MESSAGE, "\n");
  133. return false;
  134. }
  135. // if we do not have a routine yet, then it should be
  136. // the first argument in the vector
  137. if (routine.empty()) {
  138. routine = separateLine[0];
  139. // Find the full path to the file
  140. if (!this->FindMumpsFile(routine, filepath)) {
  141. cmCTestLog(this->CTest, ERROR_MESSAGE,
  142. "Could not find mumps file for routine: " << routine
  143. << "\n");
  144. filepath.clear();
  145. continue; // move to next line
  146. }
  147. }
  148. // if we have a routine name, check for end of routine
  149. else {
  150. // Totals in arg 0 marks the end of a routine
  151. if (separateLine[0].substr(0, 6) == "Totals") {
  152. routine.clear(); // at the end of this routine
  153. filepath.clear();
  154. continue; // move to next line
  155. }
  156. }
  157. // if the file path was not found for the routine
  158. // move to next line. We should have already warned
  159. // after the call to FindMumpsFile that we did not find
  160. // it, so don't report again to cut down on output
  161. if (filepath.empty()) {
  162. continue;
  163. }
  164. // now we are ready to set the coverage from the line of data
  165. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& coverageVector =
  166. this->Coverage.TotalCoverage[filepath];
  167. std::string::size_type linenumber = atoi(separateLine[1].c_str()) - 1;
  168. int count = atoi(separateLine[2].c_str());
  169. if (linenumber > coverageVector.size()) {
  170. cmCTestLog(this->CTest, ERROR_MESSAGE,
  171. "Parse error line is greater than number of lines in file: "
  172. << linenumber << " " << filepath << "\n");
  173. continue; // skip setting count to avoid crash
  174. }
  175. // now add to count for linenumber
  176. // for some reason the cache coverage adds extra lines to the
  177. // end of the file in some cases. Since they do not exist, we will
  178. // mark them as non executable
  179. while (linenumber >= coverageVector.size()) {
  180. coverageVector.push_back(-1);
  181. }
  182. // Accounts for lines that were previously marked
  183. // as non-executable code (-1). if the parser comes back with
  184. // a non-zero count, increase the count by 1 to push the line
  185. // into the executable code set in addition to the count found.
  186. if (coverageVector[linenumber] == -1 && count > 0) {
  187. coverageVector[linenumber] += count + 1;
  188. } else {
  189. coverageVector[linenumber] += count;
  190. }
  191. }
  192. return true;
  193. }