cmCTestCoverageHandler.cxx 69 KB

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