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 "cmSystemTools.h"
  12. #include <cmsys/Directory.hxx>
  13. #include <cmsys/FStream.hxx>
  14. #include <cmsys/Glob.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. } else {
  40. return line.substr(begIndex, line.npos);
  41. }
  42. }
  43. bool ParseFile(std::string const& file)
  44. {
  45. FileLinesType localCoverageVector;
  46. std::string filename;
  47. bool foundFile = false;
  48. bool inSource = false;
  49. std::string covResult;
  50. std::string line;
  51. cmsys::ifstream in(file.c_str());
  52. if (!in) {
  53. return false;
  54. }
  55. while (cmSystemTools::GetLineFromStream(in, line)) {
  56. if (line.find("filename") != line.npos) {
  57. if (foundFile) {
  58. /*
  59. * Upon finding a second file name, generate a
  60. * vector within the total coverage to capture the
  61. * information in the local vector
  62. */
  63. FileLinesType& CoverageVector =
  64. this->Coverage.TotalCoverage[filename];
  65. CoverageVector = localCoverageVector;
  66. localCoverageVector.clear();
  67. }
  68. foundFile = true;
  69. inSource = false;
  70. filename = getValue(line, 0).c_str();
  71. } else if ((line.find("coverage") != line.npos) && foundFile &&
  72. inSource) {
  73. /*
  74. * two types of "coverage" in the JSON structure
  75. *
  76. * The coverage result over the file or set of files
  77. * and the coverage for each individual line
  78. *
  79. * FoundFile and foundSource ensure that
  80. * only the value of the line coverage is captured
  81. */
  82. std::string result = getValue(line, 1);
  83. result = result.substr(2, result.npos);
  84. if (result == "\"\"") {
  85. // Empty quotation marks indicate that the
  86. // line is not executable
  87. localCoverageVector.push_back(-1);
  88. } else {
  89. // Else, it contains the number of time executed
  90. localCoverageVector.push_back(atoi(result.c_str()));
  91. }
  92. } else if (line.find("source") != line.npos) {
  93. inSource = true;
  94. }
  95. }
  96. // On exit, capture end of last file covered.
  97. FileLinesType& CoverageVector = this->Coverage.TotalCoverage[filename];
  98. CoverageVector = localCoverageVector;
  99. localCoverageVector.clear();
  100. return true;
  101. }
  102. private:
  103. cmCTestCoverageHandlerContainer& Coverage;
  104. };
  105. cmParseBlanketJSCoverage::cmParseBlanketJSCoverage(
  106. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  107. : Coverage(cont)
  108. , CTest(ctest)
  109. {
  110. }
  111. bool cmParseBlanketJSCoverage::LoadCoverageData(std::vector<std::string> files)
  112. {
  113. size_t i = 0;
  114. std::string path;
  115. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  116. "Found " << files.size() << " Files" << std::endl,
  117. this->Coverage.Quiet);
  118. for (i = 0; i < files.size(); i++) {
  119. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  120. "Reading JSON File " << files[i] << std::endl,
  121. this->Coverage.Quiet);
  122. if (!this->ReadJSONFile(files[i])) {
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. bool cmParseBlanketJSCoverage::ReadJSONFile(std::string const& file)
  129. {
  130. cmParseBlanketJSCoverage::JSONParser parser(this->Coverage);
  131. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  132. "Parsing " << file << std::endl, this->Coverage.Quiet);
  133. parser.ParseFile(file);
  134. return true;
  135. }