cmParseCacheCoverage.cxx 6.7 KB

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