1
0

cmParseDelphiCoverage.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  119. endnamepos << "File not found " << lastoffset << std::endl);
  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. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  128. "Coverage found for file: " << filename << std::endl);
  129. break;
  130. }
  131. pos = lastoffset+1;
  132. }
  133. /*
  134. * Glob through the source directory for the
  135. * file found above
  136. */
  137. cmsys::Glob gl;
  138. gl.RecurseOn();
  139. gl.RecurseThroughSymlinksOff();
  140. std::string glob = Coverage.SourceDir + "*/" + filename;
  141. gl.FindFiles(glob);
  142. std::vector<std::string> const& files = gl.GetFiles();
  143. if(files.size() == 0)
  144. {
  145. /*
  146. * If that doesn't find any matching files
  147. * return a failure.
  148. */
  149. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  150. "Unable to find file matching" << glob << std::endl);
  151. return false;
  152. }
  153. FileLinesType& coverageVector =
  154. this->Coverage.TotalCoverage[files[0]];
  155. /*
  156. * Initialize the file to have all code between 'begin' and
  157. * 'end' tags marked as executable
  158. */
  159. this->initializeDelphiFile(files[0],coverageVector);
  160. cmsys::ifstream in(file);
  161. if(!in)
  162. {
  163. return false;
  164. }
  165. /*
  166. * Now read the HTML file, looking for the lines that have an
  167. * "inline" in it. Then parse out the "class" value of that
  168. * line to determine if the line is executed or not.
  169. *
  170. * Sample HTML line:
  171. *
  172. * <tr class="covered"><td>47</td><td><pre style="display:inline;">
  173. * &nbsp;CheckEquals(1,2-1);</pre></td></tr>
  174. *
  175. */
  176. while( cmSystemTools::GetLineFromStream(in, line))
  177. {
  178. if(line.find("inline") == line.npos)
  179. {
  180. continue;
  181. }
  182. lastoffset = line.find("class=");
  183. endcovpos = line.find(">",lastoffset);
  184. lineresult = line.substr(lastoffset+7,(endcovpos-8)-lastoffset);
  185. if(lineresult == "covered")
  186. {
  187. afterLineNum = line.find('<',endcovpos+5);
  188. filelineoffset= line.substr(endcovpos+5,
  189. afterLineNum-(endcovpos+5));
  190. coverageVector[atoi(filelineoffset.c_str())-1] = 1;
  191. }
  192. }
  193. return true;
  194. }
  195. private:
  196. cmCTest* CTest;
  197. cmCTestCoverageHandlerContainer& Coverage;
  198. };
  199. cmParseDelphiCoverage::cmParseDelphiCoverage(
  200. cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
  201. :Coverage(cont), CTest(ctest)
  202. {
  203. }
  204. bool cmParseDelphiCoverage::LoadCoverageData(
  205. const std::vector<std::string> files)
  206. {
  207. size_t i;
  208. std::string path;
  209. size_t numf = files.size();
  210. for (i = 0; i < numf; i++)
  211. {
  212. path = files[i];
  213. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  214. "Reading HTML File " << path << std::endl);
  215. if(cmSystemTools::GetFilenameLastExtension(path) == ".html")
  216. {
  217. if(!this->ReadDelphiHTML(path.c_str()))
  218. {
  219. return false;
  220. }
  221. }
  222. }
  223. return true;
  224. }
  225. bool cmParseDelphiCoverage::ReadDelphiHTML(const char* file)
  226. {
  227. cmParseDelphiCoverage::HTMLParser
  228. parser(this->CTest, this->Coverage);
  229. parser.ParseFile(file);
  230. return true;
  231. }