1
0

cmParseCacheCoverage.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "cmParseCacheCoverage.h"
  2. #include "cmSystemTools.h"
  3. #include <cmsys/Directory.hxx>
  4. #include <cmsys/FStream.hxx>
  5. #include <cmsys/Glob.hxx>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. cmParseCacheCoverage::cmParseCacheCoverage(
  9. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  10. : cmParseMumpsCoverage(cont, ctest)
  11. {
  12. }
  13. bool cmParseCacheCoverage::LoadCoverageData(const char* d)
  14. {
  15. // load all the .mcov files in the specified directory
  16. cmsys::Directory dir;
  17. if (!dir.Load(d)) {
  18. return false;
  19. }
  20. size_t numf;
  21. unsigned int i;
  22. numf = dir.GetNumberOfFiles();
  23. for (i = 0; i < numf; i++) {
  24. std::string file = dir.GetFile(i);
  25. if (file != "." && file != ".." && !cmSystemTools::FileIsDirectory(file)) {
  26. std::string path = d;
  27. path += "/";
  28. path += file;
  29. if (cmSystemTools::GetFilenameLastExtension(path) == ".cmcov") {
  30. if (!this->ReadCMCovFile(path.c_str())) {
  31. return false;
  32. }
  33. }
  34. }
  35. }
  36. return true;
  37. }
  38. // not currently used, but leave it in case we want it in the future
  39. void cmParseCacheCoverage::RemoveUnCoveredFiles()
  40. {
  41. // loop over the coverage data computed and remove all files
  42. // that only have -1 or 0 for the lines.
  43. cmCTestCoverageHandlerContainer::TotalCoverageMap::iterator ci =
  44. this->Coverage.TotalCoverage.begin();
  45. while (ci != this->Coverage.TotalCoverage.end()) {
  46. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& v = ci->second;
  47. bool nothing = true;
  48. for (cmCTestCoverageHandlerContainer::SingleFileCoverageVector::iterator
  49. i = v.begin();
  50. i != v.end(); ++i) {
  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, "Empty file : "
  96. << file << " 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 << " line:\n"
  108. "["
  109. << line << "]\n");
  110. }
  111. std::string routine;
  112. std::string filepath;
  113. while (cmSystemTools::GetLineFromStream(in, line)) {
  114. // clear out line argument vector
  115. separateLine.clear();
  116. // parse the comma separated line
  117. this->SplitString(separateLine, line);
  118. // might have more because code could have a quoted , in it
  119. // but we only care about the first 3 args anyway
  120. if (separateLine.size() < 4) {
  121. cmCTestLog(this->CTest, ERROR_MESSAGE,
  122. "Bad line of cmcov file expected at least 4 found: "
  123. << separateLine.size() << " " << file << " line:\n"
  124. "["
  125. << line << "]\n");
  126. for (std::string::size_type i = 0; i < separateLine.size(); ++i) {
  127. cmCTestLog(this->CTest, ERROR_MESSAGE, "" << separateLine[1] << " ");
  128. }
  129. cmCTestLog(this->CTest, ERROR_MESSAGE, "\n");
  130. return false;
  131. }
  132. // if we do not have a routine yet, then it should be
  133. // the first argument in the vector
  134. if (routine.empty()) {
  135. routine = separateLine[0];
  136. // Find the full path to the file
  137. if (!this->FindMumpsFile(routine, filepath)) {
  138. cmCTestLog(this->CTest, ERROR_MESSAGE,
  139. "Could not find mumps file for routine: " << routine
  140. << "\n");
  141. filepath = "";
  142. continue; // move to next line
  143. }
  144. }
  145. // if we have a routine name, check for end of routine
  146. else {
  147. // Totals in arg 0 marks the end of a routine
  148. if (separateLine[0].substr(0, 6) == "Totals") {
  149. routine = ""; // at the end of this routine
  150. filepath = "";
  151. continue; // move to next line
  152. }
  153. }
  154. // if the file path was not found for the routine
  155. // move to next line. We should have already warned
  156. // after the call to FindMumpsFile that we did not find
  157. // it, so don't report again to cut down on output
  158. if (filepath.empty()) {
  159. continue;
  160. }
  161. // now we are ready to set the coverage from the line of data
  162. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& coverageVector =
  163. this->Coverage.TotalCoverage[filepath];
  164. std::string::size_type linenumber = atoi(separateLine[1].c_str()) - 1;
  165. int count = atoi(separateLine[2].c_str());
  166. if (linenumber > coverageVector.size()) {
  167. cmCTestLog(this->CTest, ERROR_MESSAGE,
  168. "Parse error line is greater than number of lines in file: "
  169. << linenumber << " " << filepath << "\n");
  170. continue; // skip setting count to avoid crash
  171. }
  172. // now add to count for linenumber
  173. // for some reason the cache coverage adds extra lines to the
  174. // end of the file in some cases. Since they do not exist, we will
  175. // mark them as non executable
  176. while (linenumber >= coverageVector.size()) {
  177. coverageVector.push_back(-1);
  178. }
  179. // Accounts for lines that were previously marked
  180. // as non-executable code (-1). if the parser comes back with
  181. // a non-zero count, increase the count by 1 to push the line
  182. // into the executable code set in addition to the count found.
  183. if (coverageVector[linenumber] == -1 && count > 0) {
  184. coverageVector[linenumber] += count + 1;
  185. } else {
  186. coverageVector[linenumber] += count;
  187. }
  188. }
  189. return true;
  190. }