cmParseBlanketJSCoverage.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc.
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmParseBlanketJSCoverage.h"
  11. #include "cmCTest.h"
  12. #include "cmCTestCoverageHandler.h"
  13. #include "cmSystemTools.h"
  14. #include <cmsys/FStream.hxx>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. class cmParseBlanketJSCoverage::JSONParser
  18. {
  19. public:
  20. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  21. FileLinesType;
  22. JSONParser(cmCTestCoverageHandlerContainer& cont)
  23. : Coverage(cont)
  24. {
  25. }
  26. virtual ~JSONParser() {}
  27. std::string getValue(std::string line, int type)
  28. {
  29. size_t begIndex;
  30. size_t endIndex;
  31. endIndex = line.rfind(',');
  32. begIndex = line.find_first_of(':');
  33. if (type == 0) {
  34. // A unique substring to remove the extra characters
  35. // around the files name in the JSON (extra " and ,)
  36. std::string foundFileName =
  37. line.substr(begIndex + 3, endIndex - (begIndex + 4));
  38. return foundFileName;
  39. }
  40. return line.substr(begIndex, line.npos);
  41. }
  42. bool ParseFile(std::string const& file)
  43. {
  44. FileLinesType localCoverageVector;
  45. std::string filename;
  46. bool foundFile = false;
  47. bool inSource = false;
  48. std::string covResult;
  49. std::string line;
  50. cmsys::ifstream in(file.c_str());
  51. if (!in) {
  52. return false;
  53. }
  54. while (cmSystemTools::GetLineFromStream(in, line)) {
  55. if (line.find("filename") != line.npos) {
  56. if (foundFile) {
  57. /*
  58. * Upon finding a second file name, generate a
  59. * vector within the total coverage to capture the
  60. * information in the local vector
  61. */
  62. FileLinesType& CoverageVector =
  63. this->Coverage.TotalCoverage[filename];
  64. CoverageVector = localCoverageVector;
  65. localCoverageVector.clear();
  66. }
  67. foundFile = true;
  68. inSource = false;
  69. filename = getValue(line, 0);
  70. } else if ((line.find("coverage") != line.npos) && foundFile &&
  71. inSource) {
  72. /*
  73. * two types of "coverage" in the JSON structure
  74. *
  75. * The coverage result over the file or set of files
  76. * and the coverage for each individual line
  77. *
  78. * FoundFile and foundSource ensure that
  79. * only the value of the line coverage is captured
  80. */
  81. std::string result = getValue(line, 1);
  82. result = result.substr(2, result.npos);
  83. if (result == "\"\"") {
  84. // Empty quotation marks indicate that the
  85. // line is not executable
  86. localCoverageVector.push_back(-1);
  87. } else {
  88. // Else, it contains the number of time executed
  89. localCoverageVector.push_back(atoi(result.c_str()));
  90. }
  91. } else if (line.find("source") != line.npos) {
  92. inSource = true;
  93. }
  94. }
  95. // On exit, capture end of last file covered.
  96. FileLinesType& CoverageVector = this->Coverage.TotalCoverage[filename];
  97. CoverageVector = localCoverageVector;
  98. localCoverageVector.clear();
  99. return true;
  100. }
  101. private:
  102. cmCTestCoverageHandlerContainer& Coverage;
  103. };
  104. cmParseBlanketJSCoverage::cmParseBlanketJSCoverage(
  105. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  106. : Coverage(cont)
  107. , CTest(ctest)
  108. {
  109. }
  110. bool cmParseBlanketJSCoverage::LoadCoverageData(std::vector<std::string> files)
  111. {
  112. size_t i = 0;
  113. std::string path;
  114. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  115. "Found " << files.size() << " Files" << std::endl,
  116. this->Coverage.Quiet);
  117. for (i = 0; i < files.size(); i++) {
  118. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  119. "Reading JSON File " << files[i] << std::endl,
  120. this->Coverage.Quiet);
  121. if (!this->ReadJSONFile(files[i])) {
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127. bool cmParseBlanketJSCoverage::ReadJSONFile(std::string const& file)
  128. {
  129. cmParseBlanketJSCoverage::JSONParser parser(this->Coverage);
  130. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  131. "Parsing " << file << std::endl, this->Coverage.Quiet);
  132. parser.ParseFile(file);
  133. return true;
  134. }