cmCTestCoverageHandler.cxx 83 KB

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