cmParseDelphiCoverage.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "cmParseDelphiCoverage.h"
  2. #include "cmCTest.h"
  3. #include "cmCTestCoverageHandler.h"
  4. #include "cmSystemTools.h"
  5. #include "cmsys/FStream.hxx"
  6. #include "cmsys/Glob.hxx"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. class cmParseDelphiCoverage::HTMLParser
  10. {
  11. public:
  12. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  13. FileLinesType;
  14. HTMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
  15. : CTest(ctest)
  16. , Coverage(cont)
  17. {
  18. }
  19. virtual ~HTMLParser() {}
  20. bool initializeDelphiFile(
  21. std::string const& filename,
  22. cmParseDelphiCoverage::HTMLParser::FileLinesType& coverageVector)
  23. {
  24. std::string line;
  25. size_t comPos;
  26. size_t semiPos;
  27. bool blockComFlag = false;
  28. bool lineComFlag = false;
  29. std::vector<std::string> beginSet;
  30. cmsys::ifstream in(filename.c_str());
  31. if (!in) {
  32. return false;
  33. }
  34. while (cmSystemTools::GetLineFromStream(in, line)) {
  35. lineComFlag = false;
  36. // Unique cases found in lines.
  37. size_t beginPos = line.find("begin");
  38. // Check that the begin is the first non-space string on the line
  39. if ((beginPos == line.find_first_not_of(' ')) &&
  40. beginPos != std::string::npos) {
  41. beginSet.push_back("begin");
  42. coverageVector.push_back(-1);
  43. continue;
  44. }
  45. if (line.find('{') != std::string::npos) {
  46. blockComFlag = true;
  47. } else if (line.find('}') != std::string::npos) {
  48. blockComFlag = false;
  49. coverageVector.push_back(-1);
  50. continue;
  51. } else if ((line.find("end;") != std::string::npos) &&
  52. !beginSet.empty()) {
  53. beginSet.pop_back();
  54. coverageVector.push_back(-1);
  55. continue;
  56. }
  57. // This checks for comments after lines of code, finding the
  58. // comment symbol after the ending semicolon.
  59. comPos = line.find("//");
  60. if (comPos != std::string::npos) {
  61. semiPos = line.find(';');
  62. if (comPos < semiPos) {
  63. lineComFlag = true;
  64. }
  65. }
  66. // Based up what was found, add a line to the coverageVector
  67. if (!beginSet.empty() && !line.empty() && !blockComFlag &&
  68. !lineComFlag) {
  69. coverageVector.push_back(0);
  70. } else {
  71. coverageVector.push_back(-1);
  72. }
  73. }
  74. return true;
  75. }
  76. bool ParseFile(const char* file)
  77. {
  78. std::string line = file;
  79. std::string lineresult;
  80. std::string lastroutine;
  81. std::string filename;
  82. std::string filelineoffset;
  83. size_t afterLineNum = 0;
  84. size_t lastoffset = 0;
  85. size_t endcovpos = 0;
  86. size_t endnamepos = 0;
  87. size_t pos = 0;
  88. /*
  89. * This first 'while' section goes through the found HTML
  90. * file name and attempts to capture the source file name
  91. * which is set as part of the HTML file name: the name of
  92. * the file is found in parenthesis '()'
  93. *
  94. * See test HTML file name: UTCovTest(UTCovTest.pas).html.
  95. *
  96. * Find the text inside each pair of parenthesis and check
  97. * to see if it ends in '.pas'. If it can't be found,
  98. * exit the function.
  99. */
  100. while (true) {
  101. lastoffset = line.find('(', pos);
  102. if (lastoffset == std::string::npos) {
  103. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, endnamepos
  104. << "File not found " << lastoffset << std::endl,
  105. this->Coverage.Quiet);
  106. return false;
  107. }
  108. endnamepos = line.find(')', lastoffset);
  109. filename = line.substr(lastoffset + 1, (endnamepos - 1) - lastoffset);
  110. if (filename.find(".pas") != std::string::npos) {
  111. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  112. "Coverage found for file: " << filename
  113. << std::endl,
  114. this->Coverage.Quiet);
  115. break;
  116. }
  117. pos = lastoffset + 1;
  118. }
  119. /*
  120. * Glob through the source directory for the
  121. * file found above
  122. */
  123. cmsys::Glob gl;
  124. gl.RecurseOn();
  125. gl.RecurseThroughSymlinksOff();
  126. std::string glob = Coverage.SourceDir + "*/" + filename;
  127. gl.FindFiles(glob);
  128. std::vector<std::string> const& files = gl.GetFiles();
  129. if (files.empty()) {
  130. /*
  131. * If that doesn't find any matching files
  132. * return a failure.
  133. */
  134. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  135. "Unable to find file matching" << glob << std::endl,
  136. this->Coverage.Quiet);
  137. return false;
  138. }
  139. FileLinesType& coverageVector = this->Coverage.TotalCoverage[files[0]];
  140. /*
  141. * Initialize the file to have all code between 'begin' and
  142. * 'end' tags marked as executable
  143. */
  144. this->initializeDelphiFile(files[0], coverageVector);
  145. cmsys::ifstream in(file);
  146. if (!in) {
  147. return false;
  148. }
  149. /*
  150. * Now read the HTML file, looking for the lines that have an
  151. * "inline" in it. Then parse out the "class" value of that
  152. * line to determine if the line is executed or not.
  153. *
  154. * Sample HTML line:
  155. *
  156. * <tr class="covered"><td>47</td><td><pre style="display:inline;">
  157. * &nbsp;CheckEquals(1,2-1);</pre></td></tr>
  158. *
  159. */
  160. while (cmSystemTools::GetLineFromStream(in, line)) {
  161. if (line.find("inline") == std::string::npos) {
  162. continue;
  163. }
  164. lastoffset = line.find("class=");
  165. endcovpos = line.find('>', lastoffset);
  166. lineresult = line.substr(lastoffset + 7, (endcovpos - 8) - lastoffset);
  167. if (lineresult == "covered") {
  168. afterLineNum = line.find('<', endcovpos + 5);
  169. filelineoffset =
  170. line.substr(endcovpos + 5, afterLineNum - (endcovpos + 5));
  171. coverageVector[atoi(filelineoffset.c_str()) - 1] = 1;
  172. }
  173. }
  174. return true;
  175. }
  176. private:
  177. cmCTest* CTest;
  178. cmCTestCoverageHandlerContainer& Coverage;
  179. };
  180. cmParseDelphiCoverage::cmParseDelphiCoverage(
  181. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  182. : Coverage(cont)
  183. , CTest(ctest)
  184. {
  185. }
  186. bool cmParseDelphiCoverage::LoadCoverageData(
  187. std::vector<std::string> const& files)
  188. {
  189. size_t i;
  190. std::string path;
  191. size_t numf = files.size();
  192. for (i = 0; i < numf; i++) {
  193. path = files[i];
  194. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  195. "Reading HTML File " << path << std::endl,
  196. this->Coverage.Quiet);
  197. if (cmSystemTools::GetFilenameLastExtension(path) == ".html") {
  198. if (!this->ReadDelphiHTML(path.c_str())) {
  199. return false;
  200. }
  201. }
  202. }
  203. return true;
  204. }
  205. bool cmParseDelphiCoverage::ReadDelphiHTML(const char* file)
  206. {
  207. cmParseDelphiCoverage::HTMLParser parser(this->CTest, this->Coverage);
  208. parser.ParseFile(file);
  209. return true;
  210. }