cmParsePythonCoverage.cxx 2.8 KB

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