cmParsePythonCoverage.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 std::string& name, const char** atts)
  20. {
  21. if(name == "class")
  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. cmsys::ifstream fin(this->CurFileName.c_str());
  33. if(!fin)
  34. {
  35. this->CurFileName = this->Coverage.BinaryDir + "/" +
  36. atts[tagCount+1];
  37. fin.open(this->CurFileName.c_str());
  38. if (!fin)
  39. {
  40. cmCTestLog(this->CTest, ERROR_MESSAGE,
  41. "Python Coverage: Error opening " << this->CurFileName
  42. << std::endl);
  43. this->Coverage.Error++;
  44. break;
  45. }
  46. }
  47. std::string line;
  48. FileLinesType& curFileLines =
  49. this->Coverage.TotalCoverage[this->CurFileName];
  50. curFileLines.push_back(-1);
  51. while(cmSystemTools::GetLineFromStream(fin, line))
  52. {
  53. curFileLines.push_back(-1);
  54. }
  55. break;
  56. }
  57. ++tagCount;
  58. }
  59. }
  60. else if(name == "line")
  61. {
  62. int tagCount = 0;
  63. int curNumber = -1;
  64. int curHits = -1;
  65. while(true)
  66. {
  67. if(strcmp(atts[tagCount], "hits") == 0)
  68. {
  69. curHits = atoi(atts[tagCount+1]);
  70. }
  71. else if(strcmp(atts[tagCount], "number") == 0)
  72. {
  73. curNumber = atoi(atts[tagCount+1]);
  74. }
  75. if(curHits > -1 && curNumber > 0)
  76. {
  77. FileLinesType& curFileLines =
  78. this->Coverage.TotalCoverage[this->CurFileName];
  79. curFileLines[curNumber-1] = curHits;
  80. break;
  81. }
  82. ++tagCount;
  83. }
  84. }
  85. }
  86. virtual void EndElement(const std::string&) {}
  87. private:
  88. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  89. FileLinesType;
  90. cmCTest* CTest;
  91. cmCTestCoverageHandlerContainer& Coverage;
  92. std::string CurFileName;
  93. };
  94. cmParsePythonCoverage::cmParsePythonCoverage(
  95. cmCTestCoverageHandlerContainer& cont,
  96. cmCTest* ctest)
  97. :Coverage(cont), CTest(ctest)
  98. {
  99. }
  100. bool cmParsePythonCoverage::ReadCoverageXML(const char* xmlFile)
  101. {
  102. cmParsePythonCoverage::XMLParser parser(this->CTest, this->Coverage);
  103. parser.ParseFile(xmlFile);
  104. return true;
  105. }