cmParseDelphiCoverage.cxx 6.6 KB

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