cmCTestCoverageHandler.cxx 81 KB

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