cmParseCoberturaCoverage.cxx 5.0 KB

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