cmParseDelphiCoverage.cxx 6.6 KB

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