cmCTestCoverageHandler.cxx 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestCoverageHandler.h"
  11. #include "cmParsePHPCoverage.h"
  12. #include "cmParseGTMCoverage.h"
  13. #include "cmParseCacheCoverage.h"
  14. #include "cmCTest.h"
  15. #include "cmake.h"
  16. #include "cmMakefile.h"
  17. #include "cmSystemTools.h"
  18. #include "cmGeneratedFileStream.h"
  19. #include "cmXMLSafe.h"
  20. #include <cmsys/Process.h>
  21. #include <cmsys/RegularExpression.hxx>
  22. #include <cmsys/Glob.hxx>
  23. #include <cmsys/stl/iterator>
  24. #include <cmsys/stl/algorithm>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <float.h>
  28. #define SAFEDIV(x,y) (((y)!=0)?((x)/(y)):(0))
  29. class cmCTestRunProcess
  30. {
  31. public:
  32. cmCTestRunProcess()
  33. {
  34. this->Process = cmsysProcess_New();
  35. this->PipeState = -1;
  36. this->TimeOut = -1;
  37. }
  38. ~cmCTestRunProcess()
  39. {
  40. if(!(this->PipeState == -1)
  41. && !(this->PipeState == cmsysProcess_Pipe_None )
  42. && !(this->PipeState == cmsysProcess_Pipe_Timeout))
  43. {
  44. this->WaitForExit();
  45. }
  46. cmsysProcess_Delete(this->Process);
  47. }
  48. void SetCommand(const char* command)
  49. {
  50. this->CommandLineStrings.clear();
  51. this->CommandLineStrings.push_back(command);;
  52. }
  53. void AddArgument(const char* arg)
  54. {
  55. if(arg)
  56. {
  57. this->CommandLineStrings.push_back(arg);
  58. }
  59. }
  60. void SetWorkingDirectory(const char* dir)
  61. {
  62. this->WorkingDirectory = dir;
  63. }
  64. void SetTimeout(double t)
  65. {
  66. this->TimeOut = t;
  67. }
  68. bool StartProcess()
  69. {
  70. std::vector<const char*> args;
  71. for(std::vector<std::string>::iterator i =
  72. this->CommandLineStrings.begin();
  73. i != this->CommandLineStrings.end(); ++i)
  74. {
  75. args.push_back(i->c_str());
  76. }
  77. args.push_back(0); // null terminate
  78. cmsysProcess_SetCommand(this->Process, &*args.begin());
  79. if(this->WorkingDirectory.size())
  80. {
  81. cmsysProcess_SetWorkingDirectory(this->Process,
  82. this->WorkingDirectory.c_str());
  83. }
  84. cmsysProcess_SetOption(this->Process,
  85. cmsysProcess_Option_HideWindow, 1);
  86. if(this->TimeOut != -1)
  87. {
  88. cmsysProcess_SetTimeout(this->Process, this->TimeOut);
  89. }
  90. cmsysProcess_Execute(this->Process);
  91. this->PipeState = cmsysProcess_GetState(this->Process);
  92. // if the process is running or exited return true
  93. if(this->PipeState == cmsysProcess_State_Executing
  94. || this->PipeState == cmsysProcess_State_Exited)
  95. {
  96. return true;
  97. }
  98. return false;
  99. }
  100. void SetStdoutFile(const char* fname)
  101. {
  102. cmsysProcess_SetPipeFile(this->Process, cmsysProcess_Pipe_STDOUT, fname);
  103. }
  104. void SetStderrFile(const char* fname)
  105. {
  106. cmsysProcess_SetPipeFile(this->Process, cmsysProcess_Pipe_STDERR, fname);
  107. }
  108. int WaitForExit(double* timeout =0)
  109. {
  110. this->PipeState = cmsysProcess_WaitForExit(this->Process,
  111. timeout);
  112. return this->PipeState;
  113. }
  114. int GetProcessState() { return this->PipeState;}
  115. private:
  116. int PipeState;
  117. cmsysProcess* Process;
  118. std::vector<std::string> CommandLineStrings;
  119. std::string WorkingDirectory;
  120. double TimeOut;
  121. };
  122. //----------------------------------------------------------------------
  123. //----------------------------------------------------------------------
  124. cmCTestCoverageHandler::cmCTestCoverageHandler()
  125. {
  126. }
  127. //----------------------------------------------------------------------
  128. void cmCTestCoverageHandler::Initialize()
  129. {
  130. this->Superclass::Initialize();
  131. this->CustomCoverageExclude.clear();
  132. this->SourceLabels.clear();
  133. this->LabelIdMap.clear();
  134. this->Labels.clear();
  135. this->LabelFilter.clear();
  136. }
  137. //----------------------------------------------------------------------------
  138. void cmCTestCoverageHandler::CleanCoverageLogFiles(std::ostream& log)
  139. {
  140. std::string logGlob = this->CTest->GetCTestConfiguration("BuildDirectory");
  141. logGlob += "/Testing/";
  142. logGlob += this->CTest->GetCurrentTag();
  143. logGlob += "/CoverageLog*";
  144. cmsys::Glob gl;
  145. gl.FindFiles(logGlob.c_str());
  146. std::vector<std::string> const& files = gl.GetFiles();
  147. for(std::vector<std::string>::const_iterator fi = files.begin();
  148. fi != files.end(); ++fi)
  149. {
  150. log << "Removing old coverage log: " << *fi << "\n";
  151. cmSystemTools::RemoveFile(fi->c_str());
  152. }
  153. }
  154. //----------------------------------------------------------------------
  155. bool cmCTestCoverageHandler::StartCoverageLogFile(
  156. cmGeneratedFileStream& covLogFile, int logFileCount)
  157. {
  158. char covLogFilename[1024];
  159. sprintf(covLogFilename, "CoverageLog-%d", logFileCount);
  160. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Open file: "
  161. << covLogFilename << std::endl);
  162. if(!this->StartResultingXML(cmCTest::PartCoverage,
  163. covLogFilename, covLogFile))
  164. {
  165. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file: "
  166. << covLogFilename << std::endl);
  167. return false;
  168. }
  169. std::string local_start_time = this->CTest->CurrentTime();
  170. this->CTest->StartXML(covLogFile, this->AppendXML);
  171. covLogFile << "<CoverageLog>" << std::endl
  172. << "\t<StartDateTime>" << local_start_time << "</StartDateTime>"
  173. << "\t<StartTime>"
  174. << static_cast<unsigned int>(cmSystemTools::GetTime())
  175. << "</StartTime>"
  176. << std::endl;
  177. return true;
  178. }
  179. //----------------------------------------------------------------------
  180. void cmCTestCoverageHandler::EndCoverageLogFile(cmGeneratedFileStream& ostr,
  181. int logFileCount)
  182. {
  183. std::string local_end_time = this->CTest->CurrentTime();
  184. ostr << "\t<EndDateTime>" << local_end_time << "</EndDateTime>" << std::endl
  185. << "\t<EndTime>" <<
  186. static_cast<unsigned int>(cmSystemTools::GetTime())
  187. << "</EndTime>" << std::endl
  188. << "</CoverageLog>" << std::endl;
  189. this->CTest->EndXML(ostr);
  190. char covLogFilename[1024];
  191. sprintf(covLogFilename, "CoverageLog-%d.xml", logFileCount);
  192. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Close file: "
  193. << covLogFilename << std::endl);
  194. ostr.Close();
  195. }
  196. //----------------------------------------------------------------------
  197. bool cmCTestCoverageHandler::ShouldIDoCoverage(const char* file,
  198. const char* srcDir,
  199. const char* binDir)
  200. {
  201. if(this->IsFilteredOut(file))
  202. {
  203. return false;
  204. }
  205. std::vector<cmsys::RegularExpression>::iterator sit;
  206. for ( sit = this->CustomCoverageExcludeRegex.begin();
  207. sit != this->CustomCoverageExcludeRegex.end(); ++ sit )
  208. {
  209. if ( sit->find(file) )
  210. {
  211. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " File " << file
  212. << " is excluded in CTestCustom.ctest" << std::endl;);
  213. return false;
  214. }
  215. }
  216. std::string fSrcDir = cmSystemTools::CollapseFullPath(srcDir);
  217. std::string fBinDir = cmSystemTools::CollapseFullPath(binDir);
  218. std::string fFile = cmSystemTools::CollapseFullPath(file);
  219. bool sourceSubDir = cmSystemTools::IsSubDirectory(fFile.c_str(),
  220. fSrcDir.c_str());
  221. bool buildSubDir = cmSystemTools::IsSubDirectory(fFile.c_str(),
  222. fBinDir.c_str());
  223. // Always check parent directory of the file.
  224. std::string fileDir = cmSystemTools::GetFilenamePath(fFile.c_str());
  225. std::string checkDir;
  226. // We also need to check the binary/source directory pair.
  227. if ( sourceSubDir && buildSubDir )
  228. {
  229. if ( fSrcDir.size() > fBinDir.size() )
  230. {
  231. checkDir = fSrcDir;
  232. }
  233. else
  234. {
  235. checkDir = fBinDir;
  236. }
  237. }
  238. else if ( sourceSubDir )
  239. {
  240. checkDir = fSrcDir;
  241. }
  242. else if ( buildSubDir )
  243. {
  244. checkDir = fBinDir;
  245. }
  246. std::string ndc
  247. = cmSystemTools::FileExistsInParentDirectories(".NoDartCoverage",
  248. fFile.c_str(), checkDir.c_str());
  249. if ( ndc.size() )
  250. {
  251. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Found: " << ndc.c_str()
  252. << " so skip coverage of " << file << std::endl);
  253. return false;
  254. }
  255. // By now checkDir should be set to parent directory of the file.
  256. // Get the relative path to the file an apply it to the opposite directory.
  257. // If it is the same as fileDir, then ignore, otherwise check.
  258. std::string relPath;
  259. if(checkDir.size() )
  260. {
  261. relPath = cmSystemTools::RelativePath(checkDir.c_str(),
  262. fFile.c_str());
  263. }
  264. else
  265. {
  266. relPath = fFile;
  267. }
  268. if ( checkDir == fSrcDir )
  269. {
  270. checkDir = fBinDir;
  271. }
  272. else
  273. {
  274. checkDir = fSrcDir;
  275. }
  276. fFile = checkDir + "/" + relPath;
  277. fFile = cmSystemTools::GetFilenamePath(fFile.c_str());
  278. if ( fileDir == fFile )
  279. {
  280. // This is in-source build, so we trust the previous check.
  281. return true;
  282. }
  283. ndc = cmSystemTools::FileExistsInParentDirectories(".NoDartCoverage",
  284. fFile.c_str(), checkDir.c_str());
  285. if ( ndc.size() )
  286. {
  287. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Found: " << ndc.c_str()
  288. << " so skip coverage of: " << file << std::endl);
  289. return false;
  290. }
  291. // Ok, nothing in source tree, nothing in binary tree
  292. return true;
  293. }
  294. //----------------------------------------------------------------------
  295. //clearly it would be nice if this were broken up into a few smaller
  296. //functions and commented...
  297. int cmCTestCoverageHandler::ProcessHandler()
  298. {
  299. this->CTest->ClearSubmitFiles(cmCTest::PartCoverage);
  300. int error = 0;
  301. // do we have time for this
  302. if (this->CTest->GetRemainingTimeAllowed() < 120)
  303. {
  304. return error;
  305. }
  306. std::string coverage_start_time = this->CTest->CurrentTime();
  307. unsigned int coverage_start_time_time = static_cast<unsigned int>(
  308. cmSystemTools::GetTime());
  309. std::string sourceDir
  310. = this->CTest->GetCTestConfiguration("SourceDirectory");
  311. std::string binaryDir
  312. = this->CTest->GetCTestConfiguration("BuildDirectory");
  313. this->LoadLabels();
  314. cmGeneratedFileStream ofs;
  315. double elapsed_time_start = cmSystemTools::GetTime();
  316. if ( !this->StartLogFile("Coverage", ofs) )
  317. {
  318. cmCTestLog(this->CTest, ERROR_MESSAGE,
  319. "Cannot create LastCoverage.log file" << std::endl);
  320. }
  321. ofs << "Performing coverage: " << elapsed_time_start << std::endl;
  322. this->CleanCoverageLogFiles(ofs);
  323. cmSystemTools::ConvertToUnixSlashes(sourceDir);
  324. cmSystemTools::ConvertToUnixSlashes(binaryDir);
  325. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Performing coverage" << std::endl);
  326. cmCTestCoverageHandlerContainer cont;
  327. cont.Error = error;
  328. cont.SourceDir = sourceDir;
  329. cont.BinaryDir = binaryDir;
  330. cont.OFS = &ofs;
  331. // setup the regex exclude stuff
  332. this->CustomCoverageExcludeRegex.clear();
  333. std::vector<cmStdString>::iterator rexIt;
  334. for ( rexIt = this->CustomCoverageExclude.begin();
  335. rexIt != this->CustomCoverageExclude.end();
  336. ++ rexIt )
  337. {
  338. this->CustomCoverageExcludeRegex.push_back(
  339. cmsys::RegularExpression(rexIt->c_str()));
  340. }
  341. if(this->HandleBullseyeCoverage(&cont))
  342. {
  343. return cont.Error;
  344. }
  345. int file_count = 0;
  346. file_count += this->HandleGCovCoverage(&cont);
  347. error = cont.Error;
  348. if ( file_count < 0 )
  349. {
  350. return error;
  351. }
  352. file_count += this->HandleTracePyCoverage(&cont);
  353. error = cont.Error;
  354. if ( file_count < 0 )
  355. {
  356. return error;
  357. }
  358. file_count += this->HandlePHPCoverage(&cont);
  359. error = cont.Error;
  360. if ( file_count < 0 )
  361. {
  362. return error;
  363. }
  364. file_count += this->HandleMumpsCoverage(&cont);
  365. error = cont.Error;
  366. if ( file_count < 0 )
  367. {
  368. return error;
  369. }
  370. std::set<std::string> uncovered = this->FindUncoveredFiles(&cont);
  371. if ( file_count == 0 )
  372. {
  373. cmCTestLog(this->CTest, WARNING,
  374. " Cannot find any coverage files. Ignoring Coverage request."
  375. << std::endl);
  376. return error;
  377. }
  378. cmGeneratedFileStream covSumFile;
  379. cmGeneratedFileStream covLogFile;
  380. if(!this->StartResultingXML(cmCTest::PartCoverage, "Coverage", covSumFile))
  381. {
  382. cmCTestLog(this->CTest, ERROR_MESSAGE,
  383. "Cannot open coverage summary file." << std::endl);
  384. return -1;
  385. }
  386. this->CTest->StartXML(covSumFile, this->AppendXML);
  387. // Produce output xml files
  388. covSumFile << "<Coverage>" << std::endl
  389. << "\t<StartDateTime>" << coverage_start_time << "</StartDateTime>"
  390. << std::endl
  391. << "\t<StartTime>" << coverage_start_time_time << "</StartTime>"
  392. << std::endl;
  393. int logFileCount = 0;
  394. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  395. {
  396. return -1;
  397. }
  398. cmCTestCoverageHandlerContainer::TotalCoverageMap::iterator fileIterator;
  399. int cnt = 0;
  400. long total_tested = 0;
  401. long total_untested = 0;
  402. //std::string fullSourceDir = sourceDir + "/";
  403. //std::string fullBinaryDir = binaryDir + "/";
  404. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  405. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  406. " Accumulating results (each . represents one file):" << std::endl);
  407. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  408. std::vector<std::string> errorsWhileAccumulating;
  409. file_count = 0;
  410. for ( fileIterator = cont.TotalCoverage.begin();
  411. fileIterator != cont.TotalCoverage.end();
  412. ++fileIterator )
  413. {
  414. cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);
  415. file_count ++;
  416. if ( file_count % 50 == 0 )
  417. {
  418. cmCTestLog(this->CTest, HANDLER_OUTPUT, " processed: " << file_count
  419. << " out of "
  420. << cont.TotalCoverage.size() << std::endl);
  421. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  422. }
  423. const std::string fullFileName = fileIterator->first;
  424. bool shouldIDoCoverage
  425. = this->ShouldIDoCoverage(fullFileName.c_str(),
  426. sourceDir.c_str(), binaryDir.c_str());
  427. if ( !shouldIDoCoverage )
  428. {
  429. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  430. ".NoDartCoverage found, so skip coverage check for: "
  431. << fullFileName.c_str()
  432. << std::endl);
  433. continue;
  434. }
  435. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  436. "Process file: " << fullFileName << std::endl);
  437. if ( !cmSystemTools::FileExists(fullFileName.c_str()) )
  438. {
  439. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot find file: "
  440. << fullFileName.c_str() << std::endl);
  441. continue;
  442. }
  443. if ( ++cnt % 100 == 0 )
  444. {
  445. this->EndCoverageLogFile(covLogFile, logFileCount);
  446. logFileCount ++;
  447. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  448. {
  449. return -1;
  450. }
  451. }
  452. const std::string fileName
  453. = cmSystemTools::GetFilenameName(fullFileName.c_str());
  454. std::string shortFileName =
  455. this->CTest->GetShortPathToFile(fullFileName.c_str());
  456. const cmCTestCoverageHandlerContainer::SingleFileCoverageVector& fcov
  457. = fileIterator->second;
  458. covLogFile << "\t<File Name=\"" << cmXMLSafe(fileName)
  459. << "\" FullPath=\"" << cmXMLSafe(shortFileName) << "\">\n"
  460. << "\t\t<Report>" << std::endl;
  461. std::ifstream ifs(fullFileName.c_str());
  462. if ( !ifs)
  463. {
  464. cmOStringStream ostr;
  465. ostr << "Cannot open source file: " << fullFileName.c_str();
  466. errorsWhileAccumulating.push_back(ostr.str());
  467. error ++;
  468. continue;
  469. }
  470. int tested = 0;
  471. int untested = 0;
  472. cmCTestCoverageHandlerContainer::SingleFileCoverageVector::size_type cc;
  473. std::string line;
  474. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  475. "Actually performing coverage for: " << fullFileName << std::endl);
  476. for ( cc= 0; cc < fcov.size(); cc ++ )
  477. {
  478. if ( !cmSystemTools::GetLineFromStream(ifs, line) &&
  479. cc != fcov.size() -1 )
  480. {
  481. cmOStringStream ostr;
  482. ostr << "Problem reading source file: " << fullFileName.c_str()
  483. << " line:" << cc << " out total: " << fcov.size()-1;
  484. errorsWhileAccumulating.push_back(ostr.str());
  485. error ++;
  486. break;
  487. }
  488. covLogFile << "\t\t<Line Number=\"" << cc << "\" Count=\"" << fcov[cc]
  489. << "\">"
  490. << cmXMLSafe(line) << "</Line>" << std::endl;
  491. if ( fcov[cc] == 0 )
  492. {
  493. untested ++;
  494. }
  495. else if ( fcov[cc] > 0 )
  496. {
  497. tested ++;
  498. }
  499. }
  500. if ( cmSystemTools::GetLineFromStream(ifs, line) )
  501. {
  502. cmOStringStream ostr;
  503. ostr << "Looks like there are more lines in the file: " << line;
  504. errorsWhileAccumulating.push_back(ostr.str());
  505. }
  506. float cper = 0;
  507. float cmet = 0;
  508. if ( tested + untested > 0 )
  509. {
  510. cper = (100 * SAFEDIV(static_cast<float>(tested),
  511. static_cast<float>(tested + untested)));
  512. cmet = ( SAFEDIV(static_cast<float>(tested + 10),
  513. static_cast<float>(tested + untested + 10)));
  514. }
  515. total_tested += tested;
  516. total_untested += untested;
  517. covLogFile << "\t\t</Report>" << std::endl
  518. << "\t</File>" << std::endl;
  519. covSumFile << "\t<File Name=\"" << cmXMLSafe(fileName)
  520. << "\" FullPath=\"" << cmXMLSafe(
  521. this->CTest->GetShortPathToFile(fullFileName.c_str()))
  522. << "\" Covered=\"" << (tested+untested > 0 ? "true":"false") << "\">\n"
  523. << "\t\t<LOCTested>" << tested << "</LOCTested>\n"
  524. << "\t\t<LOCUnTested>" << untested << "</LOCUnTested>\n"
  525. << "\t\t<PercentCoverage>";
  526. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  527. covSumFile.precision(2);
  528. covSumFile << (cper) << "</PercentCoverage>\n"
  529. << "\t\t<CoverageMetric>";
  530. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  531. covSumFile.precision(2);
  532. covSumFile << (cmet) << "</CoverageMetric>\n";
  533. this->WriteXMLLabels(covSumFile, shortFileName);
  534. covSumFile << "\t</File>" << std::endl;
  535. }
  536. //Handle all the files in the extra coverage globs that have no cov data
  537. for(std::set<std::string>::iterator i = uncovered.begin();
  538. i != uncovered.end(); ++i)
  539. {
  540. std::string fileName = cmSystemTools::GetFilenameName(*i);
  541. std::string fullPath = cont.SourceDir + "/" + *i;
  542. covLogFile << "\t<File Name=\"" << cmXMLSafe(fileName)
  543. << "\" FullPath=\"" << cmXMLSafe(*i) << "\">\n"
  544. << "\t\t<Report>" << std::endl;
  545. std::ifstream ifs(fullPath.c_str());
  546. if (!ifs)
  547. {
  548. cmOStringStream ostr;
  549. ostr << "Cannot open source file: " << fullPath.c_str();
  550. errorsWhileAccumulating.push_back(ostr.str());
  551. error ++;
  552. continue;
  553. }
  554. int untested = 0;
  555. std::string line;
  556. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  557. "Actually performing coverage for: " << i->c_str() << std::endl);
  558. while (cmSystemTools::GetLineFromStream(ifs, line))
  559. {
  560. covLogFile << "\t\t<Line Number=\"" << untested << "\" Count=\"0\">"
  561. << cmXMLSafe(line) << "</Line>" << std::endl;
  562. untested ++;
  563. }
  564. covLogFile << "\t\t</Report>\n\t</File>" << std::endl;
  565. total_untested += untested;
  566. covSumFile << "\t<File Name=\"" << cmXMLSafe(fileName)
  567. << "\" FullPath=\"" << cmXMLSafe(i->c_str())
  568. << "\" Covered=\"true\">\n"
  569. << "\t\t<LOCTested>0</LOCTested>\n"
  570. << "\t\t<LOCUnTested>" << untested << "</LOCUnTested>\n"
  571. << "\t\t<PercentCoverage>0</PercentCoverage>\n"
  572. << "\t\t<CoverageMetric>0</CoverageMetric>\n";
  573. this->WriteXMLLabels(covSumFile, *i);
  574. covSumFile << "\t</File>" << std::endl;
  575. }
  576. this->EndCoverageLogFile(covLogFile, logFileCount);
  577. if ( errorsWhileAccumulating.size() > 0 )
  578. {
  579. cmCTestLog(this->CTest, ERROR_MESSAGE, std::endl);
  580. cmCTestLog(this->CTest, ERROR_MESSAGE,
  581. "Error(s) while accumulating results:" << std::endl);
  582. std::vector<std::string>::iterator erIt;
  583. for ( erIt = errorsWhileAccumulating.begin();
  584. erIt != errorsWhileAccumulating.end();
  585. ++ erIt )
  586. {
  587. cmCTestLog(this->CTest, ERROR_MESSAGE,
  588. " " << erIt->c_str() << std::endl);
  589. }
  590. }
  591. long total_lines = total_tested + total_untested;
  592. float percent_coverage = 100 * SAFEDIV(static_cast<float>(total_tested),
  593. static_cast<float>(total_lines));
  594. if ( total_lines == 0 )
  595. {
  596. percent_coverage = 0;
  597. }
  598. std::string end_time = this->CTest->CurrentTime();
  599. covSumFile << "\t<LOCTested>" << total_tested << "</LOCTested>\n"
  600. << "\t<LOCUntested>" << total_untested << "</LOCUntested>\n"
  601. << "\t<LOC>" << total_lines << "</LOC>\n"
  602. << "\t<PercentCoverage>";
  603. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  604. covSumFile.precision(2);
  605. covSumFile << (percent_coverage)<< "</PercentCoverage>\n"
  606. << "\t<EndDateTime>" << end_time << "</EndDateTime>\n"
  607. << "\t<EndTime>" <<
  608. static_cast<unsigned int>(cmSystemTools::GetTime())
  609. << "</EndTime>\n";
  610. covSumFile << "<ElapsedMinutes>" <<
  611. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  612. << "</ElapsedMinutes>"
  613. << "</Coverage>" << std::endl;
  614. this->CTest->EndXML(covSumFile);
  615. cmCTestLog(this->CTest, HANDLER_OUTPUT, "" << std::endl
  616. << "\tCovered LOC: "
  617. << total_tested << std::endl
  618. << "\tNot covered LOC: " << total_untested << std::endl
  619. << "\tTotal LOC: " << total_lines << std::endl
  620. << "\tPercentage Coverage: "
  621. << std::setiosflags(std::ios::fixed)
  622. << std::setprecision(2)
  623. << (percent_coverage) << "%" << std::endl);
  624. ofs << "\tCovered LOC: " << total_tested << std::endl
  625. << "\tNot covered LOC: " << total_untested << std::endl
  626. << "\tTotal LOC: " << total_lines << std::endl
  627. << "\tPercentage Coverage: "
  628. << std::setiosflags(std::ios::fixed)
  629. << std::setprecision(2)
  630. << (percent_coverage) << "%" << std::endl;
  631. if ( error )
  632. {
  633. return -1;
  634. }
  635. return 0;
  636. }
  637. //----------------------------------------------------------------------
  638. void cmCTestCoverageHandler::PopulateCustomVectors(cmMakefile *mf)
  639. {
  640. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  641. " Add coverage exclude regular expressions." << std::endl);
  642. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_COVERAGE_EXCLUDE",
  643. this->CustomCoverageExclude);
  644. this->CTest->PopulateCustomVector(mf, "CTEST_EXTRA_COVERAGE_GLOB",
  645. this->ExtraCoverageGlobs);
  646. std::vector<cmStdString>::iterator it;
  647. for ( it = this->CustomCoverageExclude.begin();
  648. it != this->CustomCoverageExclude.end();
  649. ++ it )
  650. {
  651. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " Add coverage exclude: "
  652. << it->c_str() << std::endl);
  653. }
  654. for ( it = this->ExtraCoverageGlobs.begin();
  655. it != this->ExtraCoverageGlobs.end(); ++it)
  656. {
  657. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " Add coverage glob: "
  658. << it->c_str() << std::endl);
  659. }
  660. }
  661. //----------------------------------------------------------------------
  662. // Fix for issue #4971 where the case of the drive letter component of
  663. // the filenames might be different when analyzing gcov output.
  664. //
  665. // Compare file names: fnc(fn1) == fnc(fn2) // fnc == file name compare
  666. //
  667. #ifdef _WIN32
  668. #define fnc(s) cmSystemTools::LowerCase(s)
  669. #else
  670. #define fnc(s) s
  671. #endif
  672. //----------------------------------------------------------------------
  673. bool IsFileInDir(const std::string &infile, const std::string &indir)
  674. {
  675. std::string file = cmSystemTools::CollapseFullPath(infile.c_str());
  676. std::string dir = cmSystemTools::CollapseFullPath(indir.c_str());
  677. if (
  678. file.size() > dir.size() &&
  679. (fnc(file.substr(0, dir.size())) == fnc(dir)) &&
  680. file[dir.size()] == '/'
  681. )
  682. {
  683. return true;
  684. }
  685. return false;
  686. }
  687. //----------------------------------------------------------------------
  688. int cmCTestCoverageHandler::HandlePHPCoverage(
  689. cmCTestCoverageHandlerContainer* cont)
  690. {
  691. cmParsePHPCoverage cov(*cont, this->CTest);
  692. std::string coverageDir = this->CTest->GetBinaryDir() + "/xdebugCoverage";
  693. if(cmSystemTools::FileIsDirectory(coverageDir.c_str()))
  694. {
  695. cov.ReadPHPCoverageDirectory(coverageDir.c_str());
  696. }
  697. return static_cast<int>(cont->TotalCoverage.size());
  698. }
  699. //----------------------------------------------------------------------
  700. int cmCTestCoverageHandler::HandleMumpsCoverage(
  701. cmCTestCoverageHandlerContainer* cont)
  702. {
  703. // try gtm coverage
  704. cmParseGTMCoverage cov(*cont, this->CTest);
  705. std::string coverageFile = this->CTest->GetBinaryDir() +
  706. "/gtm_coverage.mcov";
  707. if(cmSystemTools::FileExists(coverageFile.c_str()))
  708. {
  709. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  710. "Parsing Cache Coverage: " << coverageFile
  711. << std::endl);
  712. cov.ReadCoverageFile(coverageFile.c_str());
  713. return static_cast<int>(cont->TotalCoverage.size());
  714. }
  715. else
  716. {
  717. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  718. " Cannot find foobar GTM coverage file: " << coverageFile
  719. << std::endl);
  720. }
  721. cmParseCacheCoverage ccov(*cont, this->CTest);
  722. coverageFile = this->CTest->GetBinaryDir() +
  723. "/cache_coverage.cmcov";
  724. if(cmSystemTools::FileExists(coverageFile.c_str()))
  725. {
  726. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  727. "Parsing Cache Coverage: " << coverageFile
  728. << std::endl);
  729. ccov.ReadCoverageFile(coverageFile.c_str());
  730. }
  731. else
  732. {
  733. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  734. " Cannot find Cache coverage file: " << coverageFile
  735. << std::endl);
  736. }
  737. return static_cast<int>(cont->TotalCoverage.size());
  738. }
  739. struct cmCTestCoverageHandlerLocale
  740. {
  741. cmCTestCoverageHandlerLocale()
  742. {
  743. if(const char* l = cmSystemTools::GetEnv("LC_ALL"))
  744. {
  745. lc_all = l;
  746. }
  747. if(lc_all != "C")
  748. {
  749. cmSystemTools::PutEnv("LC_ALL=C");
  750. }
  751. }
  752. ~cmCTestCoverageHandlerLocale()
  753. {
  754. if(!lc_all.empty())
  755. {
  756. cmSystemTools::PutEnv(("LC_ALL=" + lc_all).c_str());
  757. }
  758. else
  759. {
  760. cmSystemTools::UnsetEnv("LC_ALL");
  761. }
  762. }
  763. std::string lc_all;
  764. };
  765. //----------------------------------------------------------------------
  766. int cmCTestCoverageHandler::HandleGCovCoverage(
  767. cmCTestCoverageHandlerContainer* cont)
  768. {
  769. std::string gcovCommand
  770. = this->CTest->GetCTestConfiguration("CoverageCommand");
  771. std::string gcovExtraFlags
  772. = this->CTest->GetCTestConfiguration("CoverageExtraFlags");
  773. // Style 1
  774. std::string st1gcovOutputRex1
  775. = "[0-9]+\\.[0-9]+% of [0-9]+ (source |)lines executed in file (.*)$";
  776. std::string st1gcovOutputRex2 = "^Creating (.*\\.gcov)\\.";
  777. cmsys::RegularExpression st1re1(st1gcovOutputRex1.c_str());
  778. cmsys::RegularExpression st1re2(st1gcovOutputRex2.c_str());
  779. // Style 2
  780. std::string st2gcovOutputRex1 = "^File *[`'](.*)'$";
  781. std::string st2gcovOutputRex2
  782. = "Lines executed: *[0-9]+\\.[0-9]+% of [0-9]+$";
  783. std::string st2gcovOutputRex3 = "^(.*)reating [`'](.*\\.gcov)'";
  784. std::string st2gcovOutputRex4 = "^(.*):unexpected EOF *$";
  785. std::string st2gcovOutputRex5 = "^(.*):cannot open source file*$";
  786. std::string st2gcovOutputRex6
  787. = "^(.*):source file is newer than graph file `(.*)'$";
  788. cmsys::RegularExpression st2re1(st2gcovOutputRex1.c_str());
  789. cmsys::RegularExpression st2re2(st2gcovOutputRex2.c_str());
  790. cmsys::RegularExpression st2re3(st2gcovOutputRex3.c_str());
  791. cmsys::RegularExpression st2re4(st2gcovOutputRex4.c_str());
  792. cmsys::RegularExpression st2re5(st2gcovOutputRex5.c_str());
  793. cmsys::RegularExpression st2re6(st2gcovOutputRex6.c_str());
  794. std::vector<std::string> files;
  795. this->FindGCovFiles(files);
  796. std::vector<std::string>::iterator it;
  797. if ( files.size() == 0 )
  798. {
  799. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  800. " Cannot find any GCov coverage files."
  801. << std::endl);
  802. // No coverage files is a valid thing, so the exit code is 0
  803. return 0;
  804. }
  805. std::string testingDir = this->CTest->GetBinaryDir() + "/Testing";
  806. std::string tempDir = testingDir + "/CoverageInfo";
  807. std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
  808. cmSystemTools::MakeDirectory(tempDir.c_str());
  809. cmSystemTools::ChangeDirectory(tempDir.c_str());
  810. int gcovStyle = 0;
  811. std::set<std::string> missingFiles;
  812. std::string actualSourceFile = "";
  813. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  814. " Processing coverage (each . represents one file):" << std::endl);
  815. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  816. int file_count = 0;
  817. // make sure output from gcov is in English!
  818. cmCTestCoverageHandlerLocale locale_C;
  819. static_cast<void>(locale_C);
  820. // files is a list of *.da and *.gcda files with coverage data in them.
  821. // These are binary files that you give as input to gcov so that it will
  822. // give us text output we can analyze to summarize coverage.
  823. //
  824. for ( it = files.begin(); it != files.end(); ++ it )
  825. {
  826. cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);
  827. // Call gcov to get coverage data for this *.gcda file:
  828. //
  829. std::string fileDir = cmSystemTools::GetFilenamePath(it->c_str());
  830. std::string command = "\"" + gcovCommand + "\" " +
  831. gcovExtraFlags + " " +
  832. "-o \"" + fileDir + "\" " +
  833. "\"" + *it + "\"";
  834. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, command.c_str()
  835. << std::endl);
  836. std::string output = "";
  837. std::string errors = "";
  838. int retVal = 0;
  839. *cont->OFS << "* Run coverage for: " << fileDir.c_str() << std::endl;
  840. *cont->OFS << " Command: " << command.c_str() << std::endl;
  841. int res = this->CTest->RunCommand(command.c_str(), &output, &errors,
  842. &retVal, tempDir.c_str(), 0 /*this->TimeOut*/);
  843. *cont->OFS << " Output: " << output.c_str() << std::endl;
  844. *cont->OFS << " Errors: " << errors.c_str() << std::endl;
  845. if ( ! res )
  846. {
  847. cmCTestLog(this->CTest, ERROR_MESSAGE,
  848. "Problem running coverage on file: " << it->c_str() << std::endl);
  849. cmCTestLog(this->CTest, ERROR_MESSAGE,
  850. "Command produced error: " << errors << std::endl);
  851. cont->Error ++;
  852. continue;
  853. }
  854. if ( retVal != 0 )
  855. {
  856. cmCTestLog(this->CTest, ERROR_MESSAGE, "Coverage command returned: "
  857. << retVal << " while processing: " << it->c_str() << std::endl);
  858. cmCTestLog(this->CTest, ERROR_MESSAGE,
  859. "Command produced error: " << cont->Error << std::endl);
  860. }
  861. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  862. "--------------------------------------------------------------"
  863. << std::endl
  864. << output << std::endl
  865. << "--------------------------------------------------------------"
  866. << std::endl);
  867. std::vector<cmStdString> lines;
  868. std::vector<cmStdString>::iterator line;
  869. cmSystemTools::Split(output.c_str(), lines);
  870. for ( line = lines.begin(); line != lines.end(); ++line)
  871. {
  872. std::string sourceFile;
  873. std::string gcovFile;
  874. cmCTestLog(this->CTest, DEBUG, "Line: [" << line->c_str() << "]"
  875. << std::endl);
  876. if ( line->size() == 0 )
  877. {
  878. // Ignore empty line; probably style 2
  879. }
  880. else if ( st1re1.find(line->c_str()) )
  881. {
  882. if ( gcovStyle == 0 )
  883. {
  884. gcovStyle = 1;
  885. }
  886. if ( gcovStyle != 1 )
  887. {
  888. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e1"
  889. << std::endl);
  890. cont->Error ++;
  891. break;
  892. }
  893. actualSourceFile = "";
  894. sourceFile = st1re1.match(2);
  895. }
  896. else if ( st1re2.find(line->c_str() ) )
  897. {
  898. if ( gcovStyle == 0 )
  899. {
  900. gcovStyle = 1;
  901. }
  902. if ( gcovStyle != 1 )
  903. {
  904. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e2"
  905. << std::endl);
  906. cont->Error ++;
  907. break;
  908. }
  909. gcovFile = st1re2.match(1);
  910. }
  911. else if ( st2re1.find(line->c_str() ) )
  912. {
  913. if ( gcovStyle == 0 )
  914. {
  915. gcovStyle = 2;
  916. }
  917. if ( gcovStyle != 2 )
  918. {
  919. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e3"
  920. << std::endl);
  921. cont->Error ++;
  922. break;
  923. }
  924. actualSourceFile = "";
  925. sourceFile = st2re1.match(1);
  926. }
  927. else if ( st2re2.find(line->c_str() ) )
  928. {
  929. if ( gcovStyle == 0 )
  930. {
  931. gcovStyle = 2;
  932. }
  933. if ( gcovStyle != 2 )
  934. {
  935. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e4"
  936. << std::endl);
  937. cont->Error ++;
  938. break;
  939. }
  940. }
  941. else if ( st2re3.find(line->c_str() ) )
  942. {
  943. if ( gcovStyle == 0 )
  944. {
  945. gcovStyle = 2;
  946. }
  947. if ( gcovStyle != 2 )
  948. {
  949. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e5"
  950. << std::endl);
  951. cont->Error ++;
  952. break;
  953. }
  954. gcovFile = st2re3.match(2);
  955. }
  956. else if ( st2re4.find(line->c_str() ) )
  957. {
  958. if ( gcovStyle == 0 )
  959. {
  960. gcovStyle = 2;
  961. }
  962. if ( gcovStyle != 2 )
  963. {
  964. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e6"
  965. << std::endl);
  966. cont->Error ++;
  967. break;
  968. }
  969. cmCTestLog(this->CTest, WARNING, "Warning: " << st2re4.match(1)
  970. << " had unexpected EOF" << std::endl);
  971. }
  972. else if ( st2re5.find(line->c_str() ) )
  973. {
  974. if ( gcovStyle == 0 )
  975. {
  976. gcovStyle = 2;
  977. }
  978. if ( gcovStyle != 2 )
  979. {
  980. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e7"
  981. << std::endl);
  982. cont->Error ++;
  983. break;
  984. }
  985. cmCTestLog(this->CTest, WARNING, "Warning: Cannot open file: "
  986. << st2re5.match(1) << std::endl);
  987. }
  988. else if ( st2re6.find(line->c_str() ) )
  989. {
  990. if ( gcovStyle == 0 )
  991. {
  992. gcovStyle = 2;
  993. }
  994. if ( gcovStyle != 2 )
  995. {
  996. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e8"
  997. << std::endl);
  998. cont->Error ++;
  999. break;
  1000. }
  1001. cmCTestLog(this->CTest, WARNING, "Warning: File: " << st2re6.match(1)
  1002. << " is newer than " << st2re6.match(2) << std::endl);
  1003. }
  1004. else
  1005. {
  1006. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1007. "Unknown gcov output line: [" << line->c_str() << "]" << std::endl);
  1008. cont->Error ++;
  1009. //abort();
  1010. }
  1011. // If the last line of gcov output gave us a valid value for gcovFile,
  1012. // and we have an actualSourceFile, then insert a (or add to existing)
  1013. // SingleFileCoverageVector for actualSourceFile:
  1014. //
  1015. if ( !gcovFile.empty() && !actualSourceFile.empty() )
  1016. {
  1017. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& vec
  1018. = cont->TotalCoverage[actualSourceFile];
  1019. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " in gcovFile: "
  1020. << gcovFile << std::endl);
  1021. std::ifstream ifile(gcovFile.c_str());
  1022. if ( ! ifile )
  1023. {
  1024. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open file: "
  1025. << gcovFile << std::endl);
  1026. }
  1027. else
  1028. {
  1029. long cnt = -1;
  1030. std::string nl;
  1031. while ( cmSystemTools::GetLineFromStream(ifile, nl) )
  1032. {
  1033. cnt ++;
  1034. //TODO: Handle gcov 3.0 non-coverage lines
  1035. // Skip empty lines
  1036. if ( !nl.size() )
  1037. {
  1038. continue;
  1039. }
  1040. // Skip unused lines
  1041. if ( nl.size() < 12 )
  1042. {
  1043. continue;
  1044. }
  1045. // Read the coverage count from the beginning of the gcov output
  1046. // line
  1047. std::string prefix = nl.substr(0, 12);
  1048. int cov = atoi(prefix.c_str());
  1049. // Read the line number starting at the 10th character of the gcov
  1050. // output line
  1051. std::string lineNumber = nl.substr(10, 5);
  1052. int lineIdx = atoi(lineNumber.c_str())-1;
  1053. if ( lineIdx >= 0 )
  1054. {
  1055. while ( vec.size() <= static_cast<size_t>(lineIdx) )
  1056. {
  1057. vec.push_back(-1);
  1058. }
  1059. // Initially all entries are -1 (not used). If we get coverage
  1060. // information, increment it to 0 first.
  1061. if ( vec[lineIdx] < 0 )
  1062. {
  1063. if ( cov > 0 || prefix.find("#") != prefix.npos )
  1064. {
  1065. vec[lineIdx] = 0;
  1066. }
  1067. }
  1068. vec[lineIdx] += cov;
  1069. }
  1070. }
  1071. }
  1072. actualSourceFile = "";
  1073. }
  1074. if ( !sourceFile.empty() && actualSourceFile.empty() )
  1075. {
  1076. gcovFile = "";
  1077. // Is it in the source dir or the binary dir?
  1078. //
  1079. if ( IsFileInDir(sourceFile, cont->SourceDir) )
  1080. {
  1081. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " produced s: "
  1082. << sourceFile.c_str() << std::endl);
  1083. *cont->OFS << " produced in source dir: " << sourceFile.c_str()
  1084. << std::endl;
  1085. actualSourceFile
  1086. = cmSystemTools::CollapseFullPath(sourceFile.c_str());
  1087. }
  1088. else if ( IsFileInDir(sourceFile, cont->BinaryDir) )
  1089. {
  1090. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " produced b: "
  1091. << sourceFile.c_str() << std::endl);
  1092. *cont->OFS << " produced in binary dir: " << sourceFile.c_str()
  1093. << std::endl;
  1094. actualSourceFile
  1095. = cmSystemTools::CollapseFullPath(sourceFile.c_str());
  1096. }
  1097. if ( actualSourceFile.empty() )
  1098. {
  1099. if ( missingFiles.find(sourceFile) == missingFiles.end() )
  1100. {
  1101. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1102. "Something went wrong" << std::endl);
  1103. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1104. "Cannot find file: ["
  1105. << sourceFile.c_str() << "]" << std::endl);
  1106. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1107. " in source dir: ["
  1108. << cont->SourceDir.c_str() << "]"
  1109. << std::endl);
  1110. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1111. " or binary dir: ["
  1112. << cont->BinaryDir.size() << "]"
  1113. << std::endl);
  1114. *cont->OFS << " Something went wrong. Cannot find file: "
  1115. << sourceFile.c_str()
  1116. << " in source dir: " << cont->SourceDir.c_str()
  1117. << " or binary dir: " << cont->BinaryDir.c_str() << std::endl;
  1118. missingFiles.insert(sourceFile);
  1119. }
  1120. }
  1121. }
  1122. }
  1123. file_count++;
  1124. if ( file_count % 50 == 0 )
  1125. {
  1126. cmCTestLog(this->CTest, HANDLER_OUTPUT, " processed: " << file_count
  1127. << " out of " << files.size() << std::endl);
  1128. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  1129. }
  1130. }
  1131. cmSystemTools::ChangeDirectory(currentDirectory.c_str());
  1132. return file_count;
  1133. }
  1134. //----------------------------------------------------------------------------
  1135. void cmCTestCoverageHandler::FindGCovFiles(std::vector<std::string>& files)
  1136. {
  1137. cmsys::Glob gl;
  1138. gl.RecurseOn();
  1139. gl.RecurseThroughSymlinksOff();
  1140. for(LabelMapType::const_iterator lmi = this->TargetDirs.begin();
  1141. lmi != this->TargetDirs.end(); ++lmi)
  1142. {
  1143. // Skip targets containing no interesting labels.
  1144. if(!this->IntersectsFilter(lmi->second))
  1145. {
  1146. continue;
  1147. }
  1148. // Coverage files appear next to their object files in the target
  1149. // support directory.
  1150. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1151. " globbing for coverage in: " << lmi->first << std::endl);
  1152. std::string daGlob = lmi->first;
  1153. daGlob += "/*.da";
  1154. gl.FindFiles(daGlob);
  1155. files.insert(files.end(), gl.GetFiles().begin(), gl.GetFiles().end());
  1156. daGlob = lmi->first;
  1157. daGlob += "/*.gcda";
  1158. gl.FindFiles(daGlob);
  1159. files.insert(files.end(), gl.GetFiles().begin(), gl.GetFiles().end());
  1160. }
  1161. }
  1162. //----------------------------------------------------------------------
  1163. int cmCTestCoverageHandler::HandleTracePyCoverage(
  1164. cmCTestCoverageHandlerContainer* cont)
  1165. {
  1166. cmsys::Glob gl;
  1167. gl.RecurseOn();
  1168. gl.RecurseThroughSymlinksOff();
  1169. std::string daGlob = cont->BinaryDir + "/*.cover";
  1170. gl.FindFiles(daGlob);
  1171. std::vector<std::string> files = gl.GetFiles();
  1172. if ( files.size() == 0 )
  1173. {
  1174. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1175. " Cannot find any Python Trace.py coverage files."
  1176. << std::endl);
  1177. // No coverage files is a valid thing, so the exit code is 0
  1178. return 0;
  1179. }
  1180. std::string testingDir = this->CTest->GetBinaryDir() + "/Testing";
  1181. std::string tempDir = testingDir + "/CoverageInfo";
  1182. std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
  1183. cmSystemTools::MakeDirectory(tempDir.c_str());
  1184. cmSystemTools::ChangeDirectory(tempDir.c_str());
  1185. cmSystemTools::ChangeDirectory(currentDirectory.c_str());
  1186. std::vector<std::string>::iterator fileIt;
  1187. int file_count = 0;
  1188. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  1189. {
  1190. std::string fileName = this->FindFile(cont, *fileIt);
  1191. if ( fileName.empty() )
  1192. {
  1193. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1194. "Cannot find source Python file corresponding to: "
  1195. << fileIt->c_str() << std::endl);
  1196. continue;
  1197. }
  1198. std::string actualSourceFile
  1199. = cmSystemTools::CollapseFullPath(fileName.c_str());
  1200. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1201. " Check coverage for file: " << actualSourceFile.c_str()
  1202. << std::endl);
  1203. cmCTestCoverageHandlerContainer::SingleFileCoverageVector* vec
  1204. = &cont->TotalCoverage[actualSourceFile];
  1205. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1206. " in file: " << fileIt->c_str() << std::endl);
  1207. std::ifstream ifile(fileIt->c_str());
  1208. if ( ! ifile )
  1209. {
  1210. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open file: "
  1211. << fileIt->c_str() << std::endl);
  1212. }
  1213. else
  1214. {
  1215. long cnt = -1;
  1216. std::string nl;
  1217. while ( cmSystemTools::GetLineFromStream(ifile, nl) )
  1218. {
  1219. cnt ++;
  1220. // Skip empty lines
  1221. if ( !nl.size() )
  1222. {
  1223. continue;
  1224. }
  1225. // Skip unused lines
  1226. if ( nl.size() < 12 )
  1227. {
  1228. continue;
  1229. }
  1230. // Read the coverage count from the beginning of the Trace.py output
  1231. // line
  1232. std::string prefix = nl.substr(0, 6);
  1233. if ( prefix[5] != ' ' && prefix[5] != ':' )
  1234. {
  1235. // This is a hack. We should really do something more elaborate
  1236. prefix = nl.substr(0, 7);
  1237. if ( prefix[6] != ' ' && prefix[6] != ':' )
  1238. {
  1239. prefix = nl.substr(0, 8);
  1240. if ( prefix[7] != ' ' && prefix[7] != ':' )
  1241. {
  1242. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1243. "Currently the limit is maximum coverage of 999999"
  1244. << std::endl);
  1245. }
  1246. }
  1247. }
  1248. int cov = atoi(prefix.c_str());
  1249. if ( prefix[prefix.size()-1] != ':' )
  1250. {
  1251. // This line does not have ':' so no coverage here. That said,
  1252. // Trace.py does not handle not covered lines versus comments etc.
  1253. // So, this will be set to 0.
  1254. cov = 0;
  1255. }
  1256. cmCTestLog(this->CTest, DEBUG, "Prefix: " << prefix.c_str()
  1257. << " cov: " << cov
  1258. << std::endl);
  1259. // Read the line number starting at the 10th character of the gcov
  1260. // output line
  1261. long lineIdx = cnt;
  1262. if ( lineIdx >= 0 )
  1263. {
  1264. while ( vec->size() <=
  1265. static_cast<size_t>(lineIdx) )
  1266. {
  1267. vec->push_back(-1);
  1268. }
  1269. // Initially all entries are -1 (not used). If we get coverage
  1270. // information, increment it to 0 first.
  1271. if ( (*vec)[lineIdx] < 0 )
  1272. {
  1273. if ( cov >= 0 )
  1274. {
  1275. (*vec)[lineIdx] = 0;
  1276. }
  1277. }
  1278. (*vec)[lineIdx] += cov;
  1279. }
  1280. }
  1281. }
  1282. ++ file_count;
  1283. }
  1284. cmSystemTools::ChangeDirectory(currentDirectory.c_str());
  1285. return file_count;
  1286. }
  1287. //----------------------------------------------------------------------
  1288. std::string cmCTestCoverageHandler::FindFile(
  1289. cmCTestCoverageHandlerContainer* cont,
  1290. std::string fileName)
  1291. {
  1292. std::string fileNameNoE
  1293. = cmSystemTools::GetFilenameWithoutLastExtension(fileName);
  1294. // First check in source and binary directory
  1295. std::string fullName = cont->SourceDir + "/" + fileNameNoE + ".py";
  1296. if ( cmSystemTools::FileExists(fullName.c_str()) )
  1297. {
  1298. return fullName;
  1299. }
  1300. fullName = cont->BinaryDir + "/" + fileNameNoE + ".py";
  1301. if ( cmSystemTools::FileExists(fullName.c_str()) )
  1302. {
  1303. return fullName;
  1304. }
  1305. return "";
  1306. }
  1307. // This is a header put on each marked up source file
  1308. namespace
  1309. {
  1310. const char* bullseyeHelp[] =
  1311. {" Coverage produced by bullseye covbr tool: ",
  1312. " www.bullseye.com/help/ref_covbr.html",
  1313. " * An arrow --> indicates incomplete coverage.",
  1314. " * An X indicates a function that was invoked, a switch label that ",
  1315. " was exercised, a try-block that finished, or an exception handler ",
  1316. " that was invoked.",
  1317. " * A T or F indicates a boolean decision that evaluated true or false,",
  1318. " respectively.",
  1319. " * A t or f indicates a boolean condition within a decision if the ",
  1320. " condition evaluated true or false, respectively.",
  1321. " * A k indicates a constant decision or condition.",
  1322. " * The slash / means this probe is excluded from summary results. ",
  1323. 0};
  1324. }
  1325. //----------------------------------------------------------------------
  1326. int cmCTestCoverageHandler::RunBullseyeCoverageBranch(
  1327. cmCTestCoverageHandlerContainer* cont,
  1328. std::set<cmStdString>& coveredFileNames,
  1329. std::vector<std::string>& files,
  1330. std::vector<std::string>& filesFullPath)
  1331. {
  1332. if(files.size() != filesFullPath.size())
  1333. {
  1334. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1335. "Files and full path files not the same size?:\n");
  1336. return 0;
  1337. }
  1338. // create the output stream for the CoverageLog-N.xml file
  1339. cmGeneratedFileStream covLogFile;
  1340. int logFileCount = 0;
  1341. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  1342. {
  1343. return -1;
  1344. }
  1345. // for each file run covbr on that file to get the coverage
  1346. // information for that file
  1347. std::string outputFile;
  1348. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1349. "run covbr: "
  1350. << std::endl);
  1351. if(!this->RunBullseyeCommand(cont, "covbr", 0, outputFile))
  1352. {
  1353. cmCTestLog(this->CTest, ERROR_MESSAGE, "error running covbr for." << "\n");
  1354. return -1;
  1355. }
  1356. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1357. "covbr output in " << outputFile
  1358. << std::endl);
  1359. // open the output file
  1360. std::ifstream fin(outputFile.c_str());
  1361. if(!fin)
  1362. {
  1363. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1364. "Cannot open coverage file: " <<
  1365. outputFile.c_str() << std::endl);
  1366. return 0;
  1367. }
  1368. std::map<cmStdString, cmStdString> fileMap;
  1369. std::vector<std::string>::iterator fp = filesFullPath.begin();
  1370. for(std::vector<std::string>::iterator f = files.begin();
  1371. f != files.end(); ++f, ++fp)
  1372. {
  1373. fileMap[*f] = *fp;
  1374. }
  1375. int count =0; // keep count of the number of files
  1376. // Now parse each line from the bullseye cov log file
  1377. std::string lineIn;
  1378. bool valid = false; // are we in a valid output file
  1379. int line = 0; // line of the current file
  1380. cmStdString file;
  1381. while(cmSystemTools::GetLineFromStream(fin, lineIn))
  1382. {
  1383. bool startFile = false;
  1384. if(lineIn.size() > 1 && lineIn[lineIn.size()-1] == ':')
  1385. {
  1386. file = lineIn.substr(0, lineIn.size()-1);
  1387. if(coveredFileNames.find(file) != coveredFileNames.end())
  1388. {
  1389. startFile = true;
  1390. }
  1391. }
  1392. if(startFile)
  1393. {
  1394. // if we are in a valid file close it because a new one started
  1395. if(valid)
  1396. {
  1397. covLogFile << "\t\t</Report>" << std::endl
  1398. << "\t</File>" << std::endl;
  1399. }
  1400. // only allow 100 files in each log file
  1401. if ( count != 0 && count % 100 == 0 )
  1402. {
  1403. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1404. "start a new log file: "
  1405. << count
  1406. << std::endl);
  1407. this->EndCoverageLogFile(covLogFile, logFileCount);
  1408. logFileCount ++;
  1409. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  1410. {
  1411. return -1;
  1412. }
  1413. count++; // move on one
  1414. }
  1415. std::map<cmStdString, cmStdString>::iterator
  1416. i = fileMap.find(file);
  1417. // if the file should be covered write out the header for that file
  1418. if(i != fileMap.end())
  1419. {
  1420. // we have a new file so count it in the output
  1421. count++;
  1422. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1423. "Produce coverage for file: "
  1424. << file.c_str() << " " << count
  1425. << std::endl);
  1426. // start the file output
  1427. covLogFile << "\t<File Name=\""
  1428. << cmXMLSafe(i->first)
  1429. << "\" FullPath=\"" << cmXMLSafe(
  1430. this->CTest->GetShortPathToFile(
  1431. i->second.c_str())) << "\">" << std::endl
  1432. << "\t\t<Report>" << std::endl;
  1433. // write the bullseye header
  1434. line =0;
  1435. for(int k =0; bullseyeHelp[k] != 0; ++k)
  1436. {
  1437. covLogFile << "\t\t<Line Number=\"" << line << "\" Count=\"-1\">"
  1438. << cmXMLSafe(bullseyeHelp[k])
  1439. << "</Line>" << std::endl;
  1440. line++;
  1441. }
  1442. valid = true; // we are in a valid file section
  1443. }
  1444. else
  1445. {
  1446. // this is not a file that we want coverage for
  1447. valid = false;
  1448. }
  1449. }
  1450. // we are not at a start file, and we are in a valid file output the line
  1451. else if(valid)
  1452. {
  1453. covLogFile << "\t\t<Line Number=\"" << line << "\" Count=\"-1\">"
  1454. << cmXMLSafe(lineIn)
  1455. << "</Line>" << std::endl;
  1456. line++;
  1457. }
  1458. }
  1459. // if we ran out of lines a valid file then close that file
  1460. if(valid)
  1461. {
  1462. covLogFile << "\t\t</Report>" << std::endl
  1463. << "\t</File>" << std::endl;
  1464. }
  1465. this->EndCoverageLogFile(covLogFile, logFileCount);
  1466. return 1;
  1467. }
  1468. //----------------------------------------------------------------------
  1469. int cmCTestCoverageHandler::RunBullseyeCommand(
  1470. cmCTestCoverageHandlerContainer* cont,
  1471. const char* cmd,
  1472. const char* arg,
  1473. std::string& outputFile)
  1474. {
  1475. std::string program = cmSystemTools::FindProgram(cmd);
  1476. if(program.size() == 0)
  1477. {
  1478. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot find :" << cmd << "\n");
  1479. return 0;
  1480. }
  1481. if(arg)
  1482. {
  1483. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1484. "Run : " << program.c_str() << " " << arg << "\n");
  1485. }
  1486. else
  1487. {
  1488. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1489. "Run : " << program.c_str() << "\n");
  1490. }
  1491. // create a process object and start it
  1492. cmCTestRunProcess runCoverageSrc;
  1493. runCoverageSrc.SetCommand(program.c_str());
  1494. runCoverageSrc.AddArgument(arg);
  1495. std::string stdoutFile = cont->BinaryDir + "/Testing/Temporary/";
  1496. stdoutFile += this->GetCTestInstance()->GetCurrentTag();
  1497. stdoutFile += "-";
  1498. stdoutFile += cmd;
  1499. std::string stderrFile = stdoutFile;
  1500. stdoutFile += ".stdout";
  1501. stderrFile += ".stderr";
  1502. runCoverageSrc.SetStdoutFile(stdoutFile.c_str());
  1503. runCoverageSrc.SetStderrFile(stderrFile.c_str());
  1504. if(!runCoverageSrc.StartProcess())
  1505. {
  1506. cmCTestLog(this->CTest, ERROR_MESSAGE, "Could not run : "
  1507. << program.c_str() << " " << arg << "\n"
  1508. << "kwsys process state : "
  1509. << runCoverageSrc.GetProcessState());
  1510. return 0;
  1511. }
  1512. // since we set the output file names wait for it to end
  1513. runCoverageSrc.WaitForExit();
  1514. outputFile = stdoutFile;
  1515. return 1;
  1516. }
  1517. //----------------------------------------------------------------------
  1518. int cmCTestCoverageHandler::RunBullseyeSourceSummary(
  1519. cmCTestCoverageHandlerContainer* cont)
  1520. {
  1521. // Run the covsrc command and create a temp outputfile
  1522. std::string outputFile;
  1523. if(!this->RunBullseyeCommand(cont, "covsrc", "-c", outputFile))
  1524. {
  1525. cmCTestLog(this->CTest, ERROR_MESSAGE, "error running covsrc:\n");
  1526. return 0;
  1527. }
  1528. std::ostream& tmpLog = *cont->OFS;
  1529. // copen the Coverage.xml file in the Testing directory
  1530. cmGeneratedFileStream covSumFile;
  1531. if(!this->StartResultingXML(cmCTest::PartCoverage, "Coverage", covSumFile))
  1532. {
  1533. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1534. "Cannot open coverage summary file." << std::endl);
  1535. return 0;
  1536. }
  1537. this->CTest->StartXML(covSumFile, this->AppendXML);
  1538. double elapsed_time_start = cmSystemTools::GetTime();
  1539. std::string coverage_start_time = this->CTest->CurrentTime();
  1540. covSumFile << "<Coverage>" << std::endl
  1541. << "\t<StartDateTime>"
  1542. << coverage_start_time << "</StartDateTime>"
  1543. << std::endl
  1544. << "\t<StartTime>"
  1545. << static_cast<unsigned int>(cmSystemTools::GetTime())
  1546. << "</StartTime>"
  1547. << std::endl;
  1548. std::string stdline;
  1549. std::string errline;
  1550. // expected output:
  1551. // first line is:
  1552. // "Source","Function Coverage","out of","%","C/D Coverage","out of","%"
  1553. // after that data follows in that format
  1554. std::string sourceFile;
  1555. int functionsCalled = 0;
  1556. int totalFunctions = 0;
  1557. int percentFunction = 0;
  1558. int branchCovered = 0;
  1559. int totalBranches = 0;
  1560. int percentBranch = 0;
  1561. double total_tested = 0;
  1562. double total_untested = 0;
  1563. double total_functions = 0;
  1564. double percent_coverage =0;
  1565. double number_files = 0;
  1566. std::vector<std::string> coveredFiles;
  1567. std::vector<std::string> coveredFilesFullPath;
  1568. // Read and parse the summary output file
  1569. std::ifstream fin(outputFile.c_str());
  1570. if(!fin)
  1571. {
  1572. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1573. "Cannot open coverage summary file: " <<
  1574. outputFile.c_str() << std::endl);
  1575. return 0;
  1576. }
  1577. std::set<cmStdString> coveredFileNames;
  1578. while(cmSystemTools::GetLineFromStream(fin, stdline))
  1579. {
  1580. // if we have a line of output from stdout
  1581. if(stdline.size())
  1582. {
  1583. // parse the comma separated output
  1584. this->ParseBullsEyeCovsrcLine(stdline,
  1585. sourceFile,
  1586. functionsCalled,
  1587. totalFunctions,
  1588. percentFunction,
  1589. branchCovered,
  1590. totalBranches,
  1591. percentBranch);
  1592. // The first line is the header
  1593. if(sourceFile == "Source" || sourceFile == "Total")
  1594. {
  1595. continue;
  1596. }
  1597. std::string file = sourceFile;
  1598. coveredFileNames.insert(file);
  1599. if(!cmSystemTools::FileIsFullPath(sourceFile.c_str()))
  1600. {
  1601. // file will be relative to the binary dir
  1602. file = cont->BinaryDir;
  1603. file += "/";
  1604. file += sourceFile;
  1605. }
  1606. file = cmSystemTools::CollapseFullPath(file.c_str());
  1607. bool shouldIDoCoverage
  1608. = this->ShouldIDoCoverage(file.c_str(),
  1609. cont->SourceDir.c_str(),
  1610. cont->BinaryDir.c_str());
  1611. if ( !shouldIDoCoverage )
  1612. {
  1613. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1614. ".NoDartCoverage found, so skip coverage check for: "
  1615. << file.c_str()
  1616. << std::endl);
  1617. continue;
  1618. }
  1619. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1620. "Doing coverage for: "
  1621. << file.c_str()
  1622. << std::endl);
  1623. coveredFiles.push_back(sourceFile);
  1624. coveredFilesFullPath.push_back(file);
  1625. number_files++;
  1626. total_functions += totalFunctions;
  1627. total_tested += functionsCalled;
  1628. total_untested += (totalFunctions - functionsCalled);
  1629. std::string fileName = cmSystemTools::GetFilenameName(file.c_str());
  1630. std::string shortFileName =
  1631. this->CTest->GetShortPathToFile(file.c_str());
  1632. float cper = static_cast<float>(percentBranch + percentFunction);
  1633. if(totalBranches > 0)
  1634. {
  1635. cper /= 2.0f;
  1636. }
  1637. percent_coverage += cper;
  1638. float cmet = static_cast<float>(percentFunction + percentBranch);
  1639. if(totalBranches > 0)
  1640. {
  1641. cmet /= 2.0f;
  1642. }
  1643. cmet /= 100.0f;
  1644. tmpLog << stdline.c_str() << "\n";
  1645. tmpLog << fileName << "\n";
  1646. tmpLog << "functionsCalled: " << functionsCalled/100 << "\n";
  1647. tmpLog << "totalFunctions: " << totalFunctions/100 << "\n";
  1648. tmpLog << "percentFunction: " << percentFunction << "\n";
  1649. tmpLog << "branchCovered: " << branchCovered << "\n";
  1650. tmpLog << "totalBranches: " << totalBranches << "\n";
  1651. tmpLog << "percentBranch: " << percentBranch << "\n";
  1652. tmpLog << "percentCoverage: " << percent_coverage << "\n";
  1653. tmpLog << "coverage metric: " << cmet << "\n";
  1654. covSumFile << "\t<File Name=\"" << cmXMLSafe(sourceFile)
  1655. << "\" FullPath=\"" << cmXMLSafe(shortFileName)
  1656. << "\" Covered=\"" << (cmet>0?"true":"false") << "\">\n"
  1657. << "\t\t<BranchesTested>"
  1658. << branchCovered
  1659. << "</BranchesTested>\n"
  1660. << "\t\t<BranchesUnTested>"
  1661. << totalBranches - branchCovered
  1662. << "</BranchesUnTested>\n"
  1663. << "\t\t<FunctionsTested>"
  1664. << functionsCalled
  1665. << "</FunctionsTested>\n"
  1666. << "\t\t<FunctionsUnTested>"
  1667. << totalFunctions - functionsCalled
  1668. << "</FunctionsUnTested>\n"
  1669. // Hack for conversion of function to loc assume a function
  1670. // has 100 lines of code
  1671. << "\t\t<LOCTested>" << functionsCalled *100
  1672. << "</LOCTested>\n"
  1673. << "\t\t<LOCUnTested>"
  1674. << (totalFunctions - functionsCalled)*100
  1675. << "</LOCUnTested>\n"
  1676. << "\t\t<PercentCoverage>";
  1677. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  1678. covSumFile.precision(2);
  1679. covSumFile << (cper) << "</PercentCoverage>\n"
  1680. << "\t\t<CoverageMetric>";
  1681. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  1682. covSumFile.precision(2);
  1683. covSumFile << (cmet) << "</CoverageMetric>\n";
  1684. this->WriteXMLLabels(covSumFile, shortFileName);
  1685. covSumFile << "\t</File>" << std::endl;
  1686. }
  1687. }
  1688. std::string end_time = this->CTest->CurrentTime();
  1689. covSumFile << "\t<LOCTested>" << total_tested << "</LOCTested>\n"
  1690. << "\t<LOCUntested>" << total_untested << "</LOCUntested>\n"
  1691. << "\t<LOC>" << total_functions << "</LOC>\n"
  1692. << "\t<PercentCoverage>";
  1693. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  1694. covSumFile.precision(2);
  1695. covSumFile
  1696. << SAFEDIV(percent_coverage,number_files)<< "</PercentCoverage>\n"
  1697. << "\t<EndDateTime>" << end_time << "</EndDateTime>\n"
  1698. << "\t<EndTime>" << static_cast<unsigned int>(cmSystemTools::GetTime())
  1699. << "</EndTime>\n";
  1700. covSumFile
  1701. << "<ElapsedMinutes>" <<
  1702. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  1703. << "</ElapsedMinutes>"
  1704. << "</Coverage>" << std::endl;
  1705. this->CTest->EndXML(covSumFile);
  1706. // Now create the coverage information for each file
  1707. return this->RunBullseyeCoverageBranch(cont,
  1708. coveredFileNames,
  1709. coveredFiles,
  1710. coveredFilesFullPath);
  1711. }
  1712. //----------------------------------------------------------------------
  1713. int cmCTestCoverageHandler::HandleBullseyeCoverage(
  1714. cmCTestCoverageHandlerContainer* cont)
  1715. {
  1716. const char* covfile = cmSystemTools::GetEnv("COVFILE");
  1717. if(!covfile || strlen(covfile) == 0)
  1718. {
  1719. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1720. " COVFILE environment variable not found, not running "
  1721. " bullseye\n");
  1722. return 0;
  1723. }
  1724. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1725. " run covsrc with COVFILE=["
  1726. << covfile
  1727. << "]" << std::endl);
  1728. if(!this->RunBullseyeSourceSummary(cont))
  1729. {
  1730. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1731. "Error running bullseye summary.\n");
  1732. return 0;
  1733. }
  1734. cmCTestLog(this->CTest, DEBUG, "HandleBullseyeCoverage return 1 "
  1735. << std::endl);
  1736. return 1;
  1737. }
  1738. bool cmCTestCoverageHandler::GetNextInt(std::string const& inputLine,
  1739. std::string::size_type& pos,
  1740. int& value)
  1741. {
  1742. std::string::size_type start = pos;
  1743. pos = inputLine.find(',', start);
  1744. value = atoi(inputLine.substr(start, pos).c_str());
  1745. if(pos == inputLine.npos)
  1746. {
  1747. return true;
  1748. }
  1749. pos++;
  1750. return true;
  1751. }
  1752. bool cmCTestCoverageHandler::ParseBullsEyeCovsrcLine(
  1753. std::string const& inputLine,
  1754. std::string& sourceFile,
  1755. int& functionsCalled,
  1756. int& totalFunctions,
  1757. int& percentFunction,
  1758. int& branchCovered,
  1759. int& totalBranches,
  1760. int& percentBranch)
  1761. {
  1762. // find the first comma
  1763. std::string::size_type pos = inputLine.find(',');
  1764. if(pos == inputLine.npos)
  1765. {
  1766. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error parsing string : "
  1767. << inputLine.c_str() << "\n");
  1768. return false;
  1769. }
  1770. // the source file has "" around it so extract out the file name
  1771. sourceFile = inputLine.substr(1,pos-2);
  1772. pos++;
  1773. if(!this->GetNextInt(inputLine, pos, functionsCalled))
  1774. {
  1775. return false;
  1776. }
  1777. if(!this->GetNextInt(inputLine, pos, totalFunctions))
  1778. {
  1779. return false;
  1780. }
  1781. if(!this->GetNextInt(inputLine, pos, percentFunction))
  1782. {
  1783. return false;
  1784. }
  1785. if(!this->GetNextInt(inputLine, pos, branchCovered))
  1786. {
  1787. return false;
  1788. }
  1789. if(!this->GetNextInt(inputLine, pos, totalBranches))
  1790. {
  1791. return false;
  1792. }
  1793. if(!this->GetNextInt(inputLine, pos, percentBranch))
  1794. {
  1795. return false;
  1796. }
  1797. // should be at the end now
  1798. if(pos != inputLine.npos)
  1799. {
  1800. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error parsing input : "
  1801. << inputLine.c_str() << " last pos not npos = " << pos <<
  1802. "\n");
  1803. }
  1804. return true;
  1805. }
  1806. //----------------------------------------------------------------------
  1807. int cmCTestCoverageHandler::GetLabelId(std::string const& label)
  1808. {
  1809. LabelIdMapType::iterator i = this->LabelIdMap.find(label);
  1810. if(i == this->LabelIdMap.end())
  1811. {
  1812. int n = int(this->Labels.size());
  1813. this->Labels.push_back(label);
  1814. LabelIdMapType::value_type entry(label, n);
  1815. i = this->LabelIdMap.insert(entry).first;
  1816. }
  1817. return i->second;
  1818. }
  1819. //----------------------------------------------------------------------
  1820. void cmCTestCoverageHandler::LoadLabels()
  1821. {
  1822. std::string fileList = this->CTest->GetBinaryDir();
  1823. fileList += cmake::GetCMakeFilesDirectory();
  1824. fileList += "/TargetDirectories.txt";
  1825. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1826. " target directory list [" << fileList << "]\n");
  1827. std::ifstream finList(fileList.c_str());
  1828. std::string line;
  1829. while(cmSystemTools::GetLineFromStream(finList, line))
  1830. {
  1831. this->LoadLabels(line.c_str());
  1832. }
  1833. }
  1834. //----------------------------------------------------------------------
  1835. void cmCTestCoverageHandler::LoadLabels(const char* dir)
  1836. {
  1837. LabelSet& dirLabels = this->TargetDirs[dir];
  1838. std::string fname = dir;
  1839. fname += "/Labels.txt";
  1840. std::ifstream fin(fname.c_str());
  1841. if(!fin)
  1842. {
  1843. return;
  1844. }
  1845. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1846. " loading labels from [" << fname << "]\n");
  1847. bool inTarget = true;
  1848. std::string source;
  1849. std::string line;
  1850. std::vector<int> targetLabels;
  1851. while(cmSystemTools::GetLineFromStream(fin, line))
  1852. {
  1853. if(line.empty() || line[0] == '#')
  1854. {
  1855. // Ignore blank and comment lines.
  1856. continue;
  1857. }
  1858. else if(line[0] == ' ')
  1859. {
  1860. // Label lines appear indented by one space.
  1861. std::string label = line.substr(1);
  1862. int id = this->GetLabelId(label);
  1863. dirLabels.insert(id);
  1864. if(inTarget)
  1865. {
  1866. targetLabels.push_back(id);
  1867. }
  1868. else
  1869. {
  1870. this->SourceLabels[source].insert(id);
  1871. }
  1872. }
  1873. else
  1874. {
  1875. // Non-indented lines specify a source file name. The first one
  1876. // is the end of the target-wide labels.
  1877. inTarget = false;
  1878. source = this->CTest->GetShortPathToFile(line.c_str());
  1879. // Label the source with the target labels.
  1880. LabelSet& labelSet = this->SourceLabels[source];
  1881. for(std::vector<int>::const_iterator li = targetLabels.begin();
  1882. li != targetLabels.end(); ++li)
  1883. {
  1884. labelSet.insert(*li);
  1885. }
  1886. }
  1887. }
  1888. }
  1889. //----------------------------------------------------------------------
  1890. void cmCTestCoverageHandler::WriteXMLLabels(std::ofstream& os,
  1891. std::string const& source)
  1892. {
  1893. LabelMapType::const_iterator li = this->SourceLabels.find(source);
  1894. if(li != this->SourceLabels.end() && !li->second.empty())
  1895. {
  1896. os << "\t\t<Labels>\n";
  1897. for(LabelSet::const_iterator lsi = li->second.begin();
  1898. lsi != li->second.end(); ++lsi)
  1899. {
  1900. os << "\t\t\t<Label>" << cmXMLSafe(this->Labels[*lsi]) << "</Label>\n";
  1901. }
  1902. os << "\t\t</Labels>\n";
  1903. }
  1904. }
  1905. //----------------------------------------------------------------------------
  1906. void
  1907. cmCTestCoverageHandler::SetLabelFilter(std::set<cmStdString> const& labels)
  1908. {
  1909. this->LabelFilter.clear();
  1910. for(std::set<cmStdString>::const_iterator li = labels.begin();
  1911. li != labels.end(); ++li)
  1912. {
  1913. this->LabelFilter.insert(this->GetLabelId(*li));
  1914. }
  1915. }
  1916. //----------------------------------------------------------------------
  1917. bool cmCTestCoverageHandler::IntersectsFilter(LabelSet const& labels)
  1918. {
  1919. // If there is no label filter then nothing is filtered out.
  1920. if(this->LabelFilter.empty())
  1921. {
  1922. return true;
  1923. }
  1924. std::vector<int> ids;
  1925. cmsys_stl::set_intersection
  1926. (labels.begin(), labels.end(),
  1927. this->LabelFilter.begin(), this->LabelFilter.end(),
  1928. cmsys_stl::back_inserter(ids));
  1929. return !ids.empty();
  1930. }
  1931. //----------------------------------------------------------------------
  1932. bool cmCTestCoverageHandler::IsFilteredOut(std::string const& source)
  1933. {
  1934. // If there is no label filter then nothing is filtered out.
  1935. if(this->LabelFilter.empty())
  1936. {
  1937. return false;
  1938. }
  1939. // The source is filtered out if it does not have any labels in
  1940. // common with the filter set.
  1941. std::string shortSrc = this->CTest->GetShortPathToFile(source.c_str());
  1942. LabelMapType::const_iterator li = this->SourceLabels.find(shortSrc);
  1943. if(li != this->SourceLabels.end())
  1944. {
  1945. return !this->IntersectsFilter(li->second);
  1946. }
  1947. return true;
  1948. }
  1949. //----------------------------------------------------------------------
  1950. std::set<std::string> cmCTestCoverageHandler::FindUncoveredFiles(
  1951. cmCTestCoverageHandlerContainer* cont)
  1952. {
  1953. std::set<std::string> extraMatches;
  1954. for(std::vector<cmStdString>::iterator i = this->ExtraCoverageGlobs.begin();
  1955. i != this->ExtraCoverageGlobs.end(); ++i)
  1956. {
  1957. cmsys::Glob gl;
  1958. gl.RecurseOn();
  1959. gl.RecurseThroughSymlinksOff();
  1960. std::string glob = cont->SourceDir + "/" + *i;
  1961. gl.FindFiles(glob);
  1962. std::vector<std::string> files = gl.GetFiles();
  1963. for(std::vector<std::string>::iterator f = files.begin();
  1964. f != files.end(); ++f)
  1965. {
  1966. if(this->ShouldIDoCoverage(f->c_str(),
  1967. cont->SourceDir.c_str(), cont->BinaryDir.c_str()))
  1968. {
  1969. extraMatches.insert(this->CTest->GetShortPathToFile(
  1970. f->c_str()));
  1971. }
  1972. }
  1973. }
  1974. if(extraMatches.size())
  1975. {
  1976. for(cmCTestCoverageHandlerContainer::TotalCoverageMap::iterator i =
  1977. cont->TotalCoverage.begin(); i != cont->TotalCoverage.end(); ++i)
  1978. {
  1979. std::string shortPath = this->CTest->GetShortPathToFile(
  1980. i->first.c_str());
  1981. extraMatches.erase(shortPath);
  1982. }
  1983. }
  1984. return extraMatches;
  1985. }