cmParsePythonCoverage.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "cmStandardIncludes.h"
  2. #include "cmSystemTools.h"
  3. #include "cmXMLParser.h"
  4. #include "cmParsePythonCoverage.h"
  5. #include <cmsys/Directory.hxx>
  6. //----------------------------------------------------------------------------
  7. class cmParsePythonCoverage::XMLParser: public cmXMLParser
  8. {
  9. public:
  10. XMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
  11. : CTest(ctest), Coverage(cont)
  12. {
  13. }
  14. virtual ~XMLParser()
  15. {
  16. }
  17. protected:
  18. virtual void StartElement(const char* name, const char** atts)
  19. {
  20. if(strcmp(name, "class") == 0)
  21. {
  22. int tagCount = 0;
  23. while(true)
  24. {
  25. if(strcmp(atts[tagCount], "filename") == 0)
  26. {
  27. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Reading file: "
  28. << atts[tagCount+1] << std::endl);
  29. this->CurFileName = this->Coverage.SourceDir + "/" +
  30. atts[tagCount+1];
  31. FileLinesType& curFileLines =
  32. this->Coverage.TotalCoverage[this->CurFileName];
  33. std::ifstream fin(this->CurFileName.c_str());
  34. if(!fin)
  35. {
  36. cmCTestLog(this->CTest, ERROR_MESSAGE,
  37. "Python Coverage: Error opening " << this->CurFileName
  38. << std::endl);
  39. this->Coverage.Error++;
  40. break;
  41. }
  42. std::string line;
  43. curFileLines.push_back(-1);
  44. while(cmSystemTools::GetLineFromStream(fin, line))
  45. {
  46. curFileLines.push_back(-1);
  47. }
  48. break;
  49. }
  50. ++tagCount;
  51. }
  52. }
  53. else if(strcmp(name, "line") == 0)
  54. {
  55. int tagCount = 0;
  56. int curNumber = -1;
  57. int curHits = -1;
  58. while(true)
  59. {
  60. if(strcmp(atts[tagCount], "hits") == 0)
  61. {
  62. curHits = atoi(atts[tagCount+1]);
  63. }
  64. else if(strcmp(atts[tagCount], "number") == 0)
  65. {
  66. curNumber = atoi(atts[tagCount+1]);
  67. }
  68. if(curHits > -1 && curNumber > -1)
  69. {
  70. FileLinesType& curFileLines =
  71. this->Coverage.TotalCoverage[this->CurFileName];
  72. curFileLines[curNumber] = curHits;
  73. break;
  74. }
  75. ++tagCount;
  76. }
  77. }
  78. }
  79. virtual void EndElement(const char*) {}
  80. private:
  81. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  82. FileLinesType;
  83. cmCTest* CTest;
  84. cmCTestCoverageHandlerContainer& Coverage;
  85. std::string CurFileName;
  86. };
  87. cmParsePythonCoverage::cmParsePythonCoverage(
  88. cmCTestCoverageHandlerContainer& cont,
  89. cmCTest* ctest)
  90. :Coverage(cont), CTest(ctest)
  91. {
  92. }
  93. bool cmParsePythonCoverage::ReadCoverageXML(const char* xmlFile)
  94. {
  95. cmParsePythonCoverage::XMLParser parser(this->CTest, this->Coverage);
  96. parser.ParseFile(xmlFile);
  97. return true;
  98. }