cmParseJacocoCoverage.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "cmParseJacocoCoverage.h"
  2. #include <cmConfigure.h>
  3. #include "cmCTest.h"
  4. #include "cmCTestCoverageHandler.h"
  5. #include "cmSystemTools.h"
  6. #include "cmXMLParser.h"
  7. #include <cmsys/Directory.hxx>
  8. #include <cmsys/FStream.hxx>
  9. #include <cmsys/Glob.hxx>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. class cmParseJacocoCoverage::XMLParser : public cmXMLParser
  13. {
  14. public:
  15. XMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
  16. : CTest(ctest)
  17. , Coverage(cont)
  18. {
  19. this->FilePath = "";
  20. this->PackagePath = "";
  21. this->PackageName = "";
  22. }
  23. ~XMLParser() CM_OVERRIDE {}
  24. protected:
  25. void EndElement(const std::string& /*name*/) CM_OVERRIDE {}
  26. void StartElement(const std::string& name, const char** atts) CM_OVERRIDE
  27. {
  28. if (name == "package") {
  29. this->PackageName = atts[1];
  30. this->PackagePath = "";
  31. } else if (name == "sourcefile") {
  32. std::string fileName = atts[1];
  33. if (this->PackagePath == "") {
  34. if (!this->FindPackagePath(fileName)) {
  35. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot find file: "
  36. << this->PackageName << "/" << 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::vector<std::string>::const_iterator fi = files.begin();
  111. fi != files.end(); ++fi) {
  112. std::string dir = cmsys::SystemTools::GetParentDirectory(*fi);
  113. if (cmsys::SystemTools::StringEndsWith(dir, this->PackageName.c_str())) {
  114. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  115. "Found package directory for " << fileName << ": "
  116. << dir << std::endl,
  117. this->Coverage.Quiet);
  118. this->PackagePath = dir;
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. private:
  125. std::string FilePath;
  126. std::string PackagePath;
  127. std::string PackageName;
  128. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  129. FileLinesType;
  130. cmCTest* CTest;
  131. cmCTestCoverageHandlerContainer& Coverage;
  132. };
  133. cmParseJacocoCoverage::cmParseJacocoCoverage(
  134. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  135. : Coverage(cont)
  136. , CTest(ctest)
  137. {
  138. }
  139. bool cmParseJacocoCoverage::LoadCoverageData(
  140. std::vector<std::string> const& files)
  141. {
  142. // load all the jacoco.xml files in the source directory
  143. cmsys::Directory dir;
  144. size_t i;
  145. std::string path;
  146. size_t numf = files.size();
  147. for (i = 0; i < numf; i++) {
  148. path = files[i];
  149. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  150. "Reading XML File " << path << std::endl,
  151. this->Coverage.Quiet);
  152. if (cmSystemTools::GetFilenameLastExtension(path) == ".xml") {
  153. if (!this->ReadJacocoXML(path.c_str())) {
  154. return false;
  155. }
  156. }
  157. }
  158. return true;
  159. }
  160. bool cmParseJacocoCoverage::ReadJacocoXML(const char* file)
  161. {
  162. cmParseJacocoCoverage::XMLParser parser(this->CTest, this->Coverage);
  163. parser.ParseFile(file);
  164. return true;
  165. }