cmCTestCoverageHandler.cxx 69 KB

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