cmCTestCoverageHandler.cxx 68 KB

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