cmParseJacocoCoverage.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "cmParseJacocoCoverage.h"
  2. #include "cmCTest.h"
  3. #include "cmCTestCoverageHandler.h"
  4. #include "cmSystemTools.h"
  5. #include "cmXMLParser.h"
  6. #include "cmsys/Directory.hxx"
  7. #include "cmsys/FStream.hxx"
  8. #include "cmsys/Glob.hxx"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. class cmParseJacocoCoverage::XMLParser : public cmXMLParser
  12. {
  13. public:
  14. XMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
  15. : CTest(ctest)
  16. , Coverage(cont)
  17. {
  18. this->FilePath.clear();
  19. this->PackagePath.clear();
  20. this->PackageName.clear();
  21. }
  22. ~XMLParser() override {}
  23. protected:
  24. void EndElement(const std::string& /*name*/) override {}
  25. void StartElement(const std::string& name, const char** atts) override
  26. {
  27. if (name == "package") {
  28. this->PackageName = atts[1];
  29. this->PackagePath.clear();
  30. } else if (name == "sourcefile") {
  31. std::string fileName = atts[1];
  32. if (this->PackagePath.empty()) {
  33. if (!this->FindPackagePath(fileName)) {
  34. cmCTestLog(this->CTest, ERROR_MESSAGE,
  35. "Cannot find file: " << this->PackageName << "/"
  36. << fileName << std::endl);
  37. this->Coverage.Error++;
  38. return;
  39. }
  40. }
  41. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  42. "Reading file: " << fileName << std::endl,
  43. this->Coverage.Quiet);
  44. this->FilePath = this->PackagePath + "/" + fileName;
  45. cmsys::ifstream fin(this->FilePath.c_str());
  46. if (!fin) {
  47. cmCTestLog(this->CTest, ERROR_MESSAGE,
  48. "Jacoco Coverage: Error opening " << this->FilePath
  49. << std::endl);
  50. }
  51. std::string line;
  52. FileLinesType& curFileLines =
  53. this->Coverage.TotalCoverage[this->FilePath];
  54. if (fin) {
  55. curFileLines.push_back(-1);
  56. }
  57. while (cmSystemTools::GetLineFromStream(fin, line)) {
  58. curFileLines.push_back(-1);
  59. }
  60. } else if (name == "line") {
  61. int tagCount = 0;
  62. int nr = -1;
  63. int ci = -1;
  64. while (true) {
  65. if (strcmp(atts[tagCount], "ci") == 0) {
  66. ci = atoi(atts[tagCount + 1]);
  67. } else if (strcmp(atts[tagCount], "nr") == 0) {
  68. nr = atoi(atts[tagCount + 1]);
  69. }
  70. if (ci > -1 && nr > 0) {
  71. FileLinesType& curFileLines =
  72. this->Coverage.TotalCoverage[this->FilePath];
  73. if (!curFileLines.empty()) {
  74. curFileLines[nr - 1] = ci;
  75. }
  76. break;
  77. }
  78. ++tagCount;
  79. }
  80. }
  81. }
  82. virtual bool FindPackagePath(std::string const& fileName)
  83. {
  84. // Search for the source file in the source directory.
  85. if (this->PackagePathFound(fileName, this->Coverage.SourceDir)) {
  86. return true;
  87. }
  88. // If not found there, check the binary directory.
  89. if (this->PackagePathFound(fileName, this->Coverage.BinaryDir)) {
  90. return true;
  91. }
  92. return false;
  93. }
  94. virtual bool PackagePathFound(std::string const& fileName,
  95. std::string const& baseDir)
  96. {
  97. // Search for the file in the baseDir and its subdirectories.
  98. std::string packageGlob = baseDir;
  99. packageGlob += "/";
  100. packageGlob += fileName;
  101. cmsys::Glob gl;
  102. gl.RecurseOn();
  103. gl.RecurseThroughSymlinksOn();
  104. gl.FindFiles(packageGlob);
  105. std::vector<std::string> const& files = gl.GetFiles();
  106. if (files.empty()) {
  107. return false;
  108. }
  109. // Check if any of the locations found match our package.
  110. for (std::string const& f : files) {
  111. std::string dir = cmsys::SystemTools::GetParentDirectory(f);
  112. if (cmsys::SystemTools::StringEndsWith(dir, this->PackageName.c_str())) {
  113. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  114. "Found package directory for " << fileName << ": "
  115. << dir << std::endl,
  116. this->Coverage.Quiet);
  117. this->PackagePath = dir;
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. private:
  124. std::string FilePath;
  125. std::string PackagePath;
  126. std::string PackageName;
  127. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  128. FileLinesType;
  129. cmCTest* CTest;
  130. cmCTestCoverageHandlerContainer& Coverage;
  131. };
  132. cmParseJacocoCoverage::cmParseJacocoCoverage(
  133. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  134. : Coverage(cont)
  135. , CTest(ctest)
  136. {
  137. }
  138. bool cmParseJacocoCoverage::LoadCoverageData(
  139. std::vector<std::string> const& files)
  140. {
  141. // load all the jacoco.xml files in the source directory
  142. cmsys::Directory dir;
  143. size_t i;
  144. std::string path;
  145. size_t numf = files.size();
  146. for (i = 0; i < numf; i++) {
  147. path = files[i];
  148. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  149. "Reading XML File " << path << std::endl,
  150. this->Coverage.Quiet);
  151. if (cmSystemTools::GetFilenameLastExtension(path) == ".xml") {
  152. if (!this->ReadJacocoXML(path.c_str())) {
  153. return false;
  154. }
  155. }
  156. }
  157. return true;
  158. }
  159. bool cmParseJacocoCoverage::ReadJacocoXML(const char* file)
  160. {
  161. cmParseJacocoCoverage::XMLParser parser(this->CTest, this->Coverage);
  162. parser.ParseFile(file);
  163. return true;
  164. }