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 = "";
  19. this->PackagePath = "";
  20. this->PackageName = "";
  21. }
  22. ~XMLParser() CM_OVERRIDE {}
  23. protected:
  24. void EndElement(const std::string& /*name*/) CM_OVERRIDE {}
  25. void StartElement(const std::string& name, const char** atts) CM_OVERRIDE
  26. {
  27. if (name == "package") {
  28. this->PackageName = atts[1];
  29. this->PackagePath = "";
  30. } else if (name == "sourcefile") {
  31. std::string fileName = atts[1];
  32. if (this->PackagePath == "") {
  33. if (!this->FindPackagePath(fileName)) {
  34. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot find file: "
  35. << this->PackageName << "/" << fileName << std::endl);
  36. this->Coverage.Error++;
  37. return;
  38. }
  39. }
  40. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  41. "Reading file: " << fileName << std::endl,
  42. this->Coverage.Quiet);
  43. this->FilePath = this->PackagePath + "/" + fileName;
  44. cmsys::ifstream fin(this->FilePath.c_str());
  45. if (!fin) {
  46. cmCTestLog(this->CTest, ERROR_MESSAGE,
  47. "Jacoco Coverage: Error opening " << this->FilePath
  48. << std::endl);
  49. }
  50. std::string line;
  51. FileLinesType& curFileLines =
  52. this->Coverage.TotalCoverage[this->FilePath];
  53. if (fin) {
  54. curFileLines.push_back(-1);
  55. }
  56. while (cmSystemTools::GetLineFromStream(fin, line)) {
  57. curFileLines.push_back(-1);
  58. }
  59. } else if (name == "line") {
  60. int tagCount = 0;
  61. int nr = -1;
  62. int ci = -1;
  63. while (true) {
  64. if (strcmp(atts[tagCount], "ci") == 0) {
  65. ci = atoi(atts[tagCount + 1]);
  66. } else if (strcmp(atts[tagCount], "nr") == 0) {
  67. nr = atoi(atts[tagCount + 1]);
  68. }
  69. if (ci > -1 && nr > 0) {
  70. FileLinesType& curFileLines =
  71. this->Coverage.TotalCoverage[this->FilePath];
  72. if (!curFileLines.empty()) {
  73. curFileLines[nr - 1] = ci;
  74. }
  75. break;
  76. }
  77. ++tagCount;
  78. }
  79. }
  80. }
  81. virtual bool FindPackagePath(std::string const& fileName)
  82. {
  83. // Search for the source file in the source directory.
  84. if (this->PackagePathFound(fileName, this->Coverage.SourceDir)) {
  85. return true;
  86. }
  87. // If not found there, check the binary directory.
  88. if (this->PackagePathFound(fileName, this->Coverage.BinaryDir)) {
  89. return true;
  90. }
  91. return false;
  92. }
  93. virtual bool PackagePathFound(std::string const& fileName,
  94. std::string const& baseDir)
  95. {
  96. // Search for the file in the baseDir and its subdirectories.
  97. std::string packageGlob = baseDir;
  98. packageGlob += "/";
  99. packageGlob += fileName;
  100. cmsys::Glob gl;
  101. gl.RecurseOn();
  102. gl.RecurseThroughSymlinksOn();
  103. gl.FindFiles(packageGlob);
  104. std::vector<std::string> const& files = gl.GetFiles();
  105. if (files.empty()) {
  106. return false;
  107. }
  108. // Check if any of the locations found match our package.
  109. for (std::vector<std::string>::const_iterator fi = files.begin();
  110. fi != files.end(); ++fi) {
  111. std::string dir = cmsys::SystemTools::GetParentDirectory(*fi);
  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. }