cmCTestCoverageHandler.cxx 83 KB

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