cmCTestCoverageHandler.cxx 65 KB

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