cmCTestCoverageHandler.cxx 58 KB

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