cmCTestCoverageHandler.cxx 83 KB

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