cmParseCoberturaCoverage.cxx 4.2 KB

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