cmCTestCoverageHandler.cxx 84 KB

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