cmCTestCoverageHandler.cxx 79 KB

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