cmCTestCoverageHandler.cxx 62 KB

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