cmParseMumpsCoverage.cxx 4.3 KB

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