cmCTestCoverageHandler.cxx 82 KB

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