cmParseCoberturaCoverage.cxx 4.8 KB

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