cmCTestCoverageHandler.cxx 83 KB

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