cmParseCacheCoverage.cxx 6.7 KB

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