cmCTestCoverageHandler.cxx 65 KB

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