cmParseCoberturaCoverage.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "cmParseCoberturaCoverage.h"
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include "cmsys/FStream.hxx"
  5. #include "cmCTest.h"
  6. #include "cmCTestCoverageHandler.h"
  7. #include "cmStringAlgorithms.h"
  8. #include "cmSystemTools.h"
  9. #include "cmXMLParser.h"
  10. class cmParseCoberturaCoverage::XMLParser : public cmXMLParser
  11. {
  12. public:
  13. XMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
  14. : FilePaths{ cont.SourceDir, cont.BinaryDir }
  15. , CTest(ctest)
  16. , Coverage(cont)
  17. {
  18. }
  19. protected:
  20. void EndElement(std::string const& name) override
  21. {
  22. if (name == "source") {
  23. this->InSource = false;
  24. } else if (name == "sources") {
  25. this->InSources = false;
  26. } else if (name == "class") {
  27. this->SkipThisClass = false;
  28. }
  29. }
  30. void CharacterDataHandler(char const* data, int length) override
  31. {
  32. std::string tmp;
  33. tmp.insert(0, data, length);
  34. if (this->InSources && this->InSource) {
  35. this->FilePaths.push_back(tmp);
  36. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  37. "Adding Source: " << tmp << std::endl,
  38. this->Coverage.Quiet);
  39. }
  40. }
  41. void StartElement(std::string const& name, char const** atts) override
  42. {
  43. std::string FoundSource;
  44. std::string finalpath;
  45. if (name == "source") {
  46. this->InSource = true;
  47. } else if (name == "sources") {
  48. this->InSources = true;
  49. } else if (name == "class") {
  50. int tagCount = 0;
  51. while (true) {
  52. if (strcmp(atts[tagCount], "filename") == 0) {
  53. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  54. "Reading file: " << atts[tagCount + 1]
  55. << std::endl,
  56. this->Coverage.Quiet);
  57. std::string filename = atts[tagCount + 1];
  58. this->CurFileName.clear();
  59. // Check if this is an absolute path that falls within our
  60. // source or binary directories.
  61. for (std::string const& filePath : this->FilePaths) {
  62. if (cmHasPrefix(filename, filePath)) {
  63. this->CurFileName = filename;
  64. break;
  65. }
  66. }
  67. if (this->CurFileName.empty()) {
  68. // Check if this is a path that is relative to our source or
  69. // binary directories.
  70. for (std::string const& filePath : this->FilePaths) {
  71. finalpath = cmStrCat(filePath, '/', filename);
  72. if (cmSystemTools::FileExists(finalpath)) {
  73. this->CurFileName = finalpath;
  74. break;
  75. }
  76. }
  77. }
  78. cmsys::ifstream fin(this->CurFileName.c_str());
  79. if (this->CurFileName.empty() || !fin) {
  80. this->CurFileName =
  81. cmStrCat(this->Coverage.BinaryDir, '/', atts[tagCount + 1]);
  82. fin.open(this->CurFileName.c_str());
  83. if (!fin) {
  84. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  85. "Skipping system file " << filename
  86. << std::endl,
  87. this->Coverage.Quiet);
  88. this->SkipThisClass = true;
  89. break;
  90. }
  91. }
  92. std::string line;
  93. FileLinesType& curFileLines =
  94. this->Coverage.TotalCoverage[this->CurFileName];
  95. while (cmSystemTools::GetLineFromStream(fin, line)) {
  96. curFileLines.push_back(-1);
  97. }
  98. break;
  99. }
  100. ++tagCount;
  101. }
  102. } else if (name == "line") {
  103. int tagCount = 0;
  104. int curNumber = -1;
  105. int curHits = -1;
  106. while (true) {
  107. if (this->SkipThisClass) {
  108. break;
  109. }
  110. if (strcmp(atts[tagCount], "hits") == 0) {
  111. curHits = atoi(atts[tagCount + 1]);
  112. } else if (strcmp(atts[tagCount], "number") == 0) {
  113. curNumber = atoi(atts[tagCount + 1]);
  114. }
  115. if (curHits > -1 && curNumber > 0) {
  116. FileLinesType& curFileLines =
  117. this->Coverage.TotalCoverage[this->CurFileName];
  118. {
  119. curFileLines[curNumber - 1] = curHits;
  120. }
  121. break;
  122. }
  123. ++tagCount;
  124. }
  125. }
  126. }
  127. private:
  128. bool InSources = false;
  129. bool InSource = false;
  130. bool SkipThisClass = false;
  131. std::vector<std::string> FilePaths;
  132. using FileLinesType =
  133. cmCTestCoverageHandlerContainer::SingleFileCoverageVector;
  134. cmCTest* CTest;
  135. cmCTestCoverageHandlerContainer& Coverage;
  136. std::string CurFileName;
  137. };
  138. cmParseCoberturaCoverage::cmParseCoberturaCoverage(
  139. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  140. : Coverage(cont)
  141. , CTest(ctest)
  142. {
  143. }
  144. bool cmParseCoberturaCoverage::ReadCoverageXML(char const* xmlFile)
  145. {
  146. cmParseCoberturaCoverage::XMLParser parser(this->CTest, this->Coverage);
  147. parser.ParseFile(xmlFile);
  148. return true;
  149. }