cmParseMumpsCoverage.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "cmParseMumpsCoverage.h"
  2. #include "cmCTest.h"
  3. #include "cmCTestCoverageHandler.h"
  4. #include "cmSystemTools.h"
  5. #include "cmsys/FStream.hxx"
  6. #include "cmsys/Glob.hxx"
  7. #include <map>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. cmParseMumpsCoverage::cmParseMumpsCoverage(
  12. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  13. : Coverage(cont)
  14. , CTest(ctest)
  15. {
  16. }
  17. cmParseMumpsCoverage::~cmParseMumpsCoverage() = default;
  18. bool cmParseMumpsCoverage::ReadCoverageFile(const char* file)
  19. {
  20. // Read the gtm_coverage.mcov file, that has two lines of data:
  21. // packages:/full/path/to/Vista/Packages
  22. // coverage_dir:/full/path/to/dir/with/*.mcov
  23. cmsys::ifstream in(file);
  24. if (!in) {
  25. return false;
  26. }
  27. std::string line;
  28. while (cmSystemTools::GetLineFromStream(in, line)) {
  29. std::string::size_type pos = line.find(':', 0);
  30. std::string packages;
  31. if (pos != std::string::npos) {
  32. std::string type = line.substr(0, pos);
  33. std::string path = line.substr(pos + 1);
  34. if (type == "packages") {
  35. this->LoadPackages(path.c_str());
  36. } else if (type == "coverage_dir") {
  37. this->LoadCoverageData(path.c_str());
  38. } else {
  39. cmCTestLog(this->CTest, ERROR_MESSAGE,
  40. "Parse Error in Mumps coverage file :\n"
  41. << file << "\ntype: [" << type << "]\npath:[" << path
  42. << "]\n"
  43. "input line: ["
  44. << line << "]\n");
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. void cmParseMumpsCoverage::InitializeMumpsFile(std::string& file)
  51. {
  52. // initialize the coverage information for a given mumps file
  53. cmsys::ifstream in(file.c_str());
  54. if (!in) {
  55. return;
  56. }
  57. std::string line;
  58. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& coverageVector =
  59. this->Coverage.TotalCoverage[file];
  60. if (!cmSystemTools::GetLineFromStream(in, line)) {
  61. return;
  62. }
  63. // first line of a .m file can never be run
  64. coverageVector.push_back(-1);
  65. while (cmSystemTools::GetLineFromStream(in, line)) {
  66. // putting in a 0 for a line means it is executable code
  67. // putting in a -1 for a line means it is not executable code
  68. int val = -1; // assume line is not executable
  69. bool found = false;
  70. std::string::size_type i = 0;
  71. // (1) Search for the first whitespace or semicolon character on a line.
  72. // This will skip over labels if the line starts with one, or will simply
  73. // be the first character on the line for non-label lines.
  74. for (; i < line.size(); ++i) {
  75. if (line[i] == ' ' || line[i] == '\t' || line[i] == ';') {
  76. found = true;
  77. break;
  78. }
  79. }
  80. if (found) {
  81. // (2) If the first character found above is whitespace or a period
  82. // then continue the search for the first following non-whitespace
  83. // character.
  84. if (line[i] == ' ' || line[i] == '\t') {
  85. while (i < line.size() &&
  86. (line[i] == ' ' || line[i] == '\t' || line[i] == '.')) {
  87. i++;
  88. }
  89. }
  90. // (3) If the character found is not a semicolon then the line counts for
  91. // coverage.
  92. if (i < line.size() && line[i] != ';') {
  93. val = 0;
  94. }
  95. }
  96. coverageVector.push_back(val);
  97. }
  98. }
  99. bool cmParseMumpsCoverage::LoadPackages(const char* d)
  100. {
  101. cmsys::Glob glob;
  102. glob.RecurseOn();
  103. std::string pat = d;
  104. pat += "/*.m";
  105. glob.FindFiles(pat);
  106. for (std::string& file : glob.GetFiles()) {
  107. std::string name = cmSystemTools::GetFilenameName(file);
  108. this->RoutineToDirectory[name.substr(0, name.size() - 2)] = file;
  109. // initialize each file, this is left out until CDash is fixed
  110. // to handle large numbers of files
  111. this->InitializeMumpsFile(file);
  112. }
  113. return true;
  114. }
  115. bool cmParseMumpsCoverage::FindMumpsFile(std::string const& routine,
  116. std::string& filepath)
  117. {
  118. std::map<std::string, std::string>::iterator i =
  119. this->RoutineToDirectory.find(routine);
  120. if (i != this->RoutineToDirectory.end()) {
  121. filepath = i->second;
  122. return true;
  123. }
  124. // try some alternate names
  125. const char* tryname[] = { "GUX", "GTM", "ONT", nullptr };
  126. for (int k = 0; tryname[k] != nullptr; k++) {
  127. std::string routine2 = routine + tryname[k];
  128. i = this->RoutineToDirectory.find(routine2);
  129. if (i != this->RoutineToDirectory.end()) {
  130. filepath = i->second;
  131. return true;
  132. }
  133. }
  134. return false;
  135. }