cmCTestCoverageHandler.cxx 62 KB

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