cmParseDelphiCoverage.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.size() > 0))
  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.size() > 0) && 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. endnamepos = 0;
  133. lastoffset =0;
  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.size() == 0)
  146. {
  147. /*
  148. * If that doesn't find any matching files
  149. * return a failure.
  150. */
  151. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  152. "Unable to find file matching" << glob << std::endl);
  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. cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
  216. "Reading HTML File " << path << std::endl);
  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. };