1
0

cmParseJacocoCoverage.cxx 5.1 KB

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