cmCTestCoverageHandler.cxx 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193
  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<std::string>::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<std::string>::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. if (gcovCommand.empty())
  803. {
  804. cmCTestLog(this->CTest, ERROR_MESSAGE,
  805. "Could not find gcov." << std::endl);
  806. return 0;
  807. }
  808. std::string gcovExtraFlags
  809. = this->CTest->GetCTestConfiguration("CoverageExtraFlags");
  810. // Style 1
  811. std::string st1gcovOutputRex1
  812. = "[0-9]+\\.[0-9]+% of [0-9]+ (source |)lines executed in file (.*)$";
  813. std::string st1gcovOutputRex2 = "^Creating (.*\\.gcov)\\.";
  814. cmsys::RegularExpression st1re1(st1gcovOutputRex1.c_str());
  815. cmsys::RegularExpression st1re2(st1gcovOutputRex2.c_str());
  816. // Style 2
  817. std::string st2gcovOutputRex1 = "^File *[`'](.*)'$";
  818. std::string st2gcovOutputRex2
  819. = "Lines executed: *[0-9]+\\.[0-9]+% of [0-9]+$";
  820. std::string st2gcovOutputRex3 = "^(.*)reating [`'](.*\\.gcov)'";
  821. std::string st2gcovOutputRex4 = "^(.*):unexpected EOF *$";
  822. std::string st2gcovOutputRex5 = "^(.*):cannot open source file*$";
  823. std::string st2gcovOutputRex6
  824. = "^(.*):source file is newer than graph file `(.*)'$";
  825. cmsys::RegularExpression st2re1(st2gcovOutputRex1.c_str());
  826. cmsys::RegularExpression st2re2(st2gcovOutputRex2.c_str());
  827. cmsys::RegularExpression st2re3(st2gcovOutputRex3.c_str());
  828. cmsys::RegularExpression st2re4(st2gcovOutputRex4.c_str());
  829. cmsys::RegularExpression st2re5(st2gcovOutputRex5.c_str());
  830. cmsys::RegularExpression st2re6(st2gcovOutputRex6.c_str());
  831. std::vector<std::string> files;
  832. this->FindGCovFiles(files);
  833. std::vector<std::string>::iterator it;
  834. if ( files.size() == 0 )
  835. {
  836. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  837. " Cannot find any GCov coverage files."
  838. << std::endl);
  839. // No coverage files is a valid thing, so the exit code is 0
  840. return 0;
  841. }
  842. std::string testingDir = this->CTest->GetBinaryDir() + "/Testing";
  843. std::string tempDir = testingDir + "/CoverageInfo";
  844. std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
  845. cmSystemTools::MakeDirectory(tempDir.c_str());
  846. cmSystemTools::ChangeDirectory(tempDir.c_str());
  847. int gcovStyle = 0;
  848. std::set<std::string> missingFiles;
  849. std::string actualSourceFile = "";
  850. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  851. " Processing coverage (each . represents one file):" << std::endl);
  852. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  853. int file_count = 0;
  854. // make sure output from gcov is in English!
  855. cmCTestCoverageHandlerLocale locale_C;
  856. static_cast<void>(locale_C);
  857. // files is a list of *.da and *.gcda files with coverage data in them.
  858. // These are binary files that you give as input to gcov so that it will
  859. // give us text output we can analyze to summarize coverage.
  860. //
  861. for ( it = files.begin(); it != files.end(); ++ it )
  862. {
  863. cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);
  864. // Call gcov to get coverage data for this *.gcda file:
  865. //
  866. std::string fileDir = cmSystemTools::GetFilenamePath(it->c_str());
  867. std::string command = "\"" + gcovCommand + "\" " +
  868. gcovExtraFlags + " " +
  869. "-o \"" + fileDir + "\" " +
  870. "\"" + *it + "\"";
  871. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, command.c_str()
  872. << std::endl);
  873. std::string output = "";
  874. std::string errors = "";
  875. int retVal = 0;
  876. *cont->OFS << "* Run coverage for: " << fileDir.c_str() << std::endl;
  877. *cont->OFS << " Command: " << command.c_str() << std::endl;
  878. int res = this->CTest->RunCommand(command.c_str(), &output, &errors,
  879. &retVal, tempDir.c_str(), 0 /*this->TimeOut*/);
  880. *cont->OFS << " Output: " << output.c_str() << std::endl;
  881. *cont->OFS << " Errors: " << errors.c_str() << std::endl;
  882. if ( ! res )
  883. {
  884. cmCTestLog(this->CTest, ERROR_MESSAGE,
  885. "Problem running coverage on file: " << it->c_str() << std::endl);
  886. cmCTestLog(this->CTest, ERROR_MESSAGE,
  887. "Command produced error: " << errors << std::endl);
  888. cont->Error ++;
  889. continue;
  890. }
  891. if ( retVal != 0 )
  892. {
  893. cmCTestLog(this->CTest, ERROR_MESSAGE, "Coverage command returned: "
  894. << retVal << " while processing: " << it->c_str() << std::endl);
  895. cmCTestLog(this->CTest, ERROR_MESSAGE,
  896. "Command produced error: " << cont->Error << std::endl);
  897. }
  898. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  899. "--------------------------------------------------------------"
  900. << std::endl
  901. << output << std::endl
  902. << "--------------------------------------------------------------"
  903. << std::endl);
  904. std::vector<std::string> lines;
  905. std::vector<std::string>::iterator line;
  906. cmSystemTools::Split(output.c_str(), lines);
  907. for ( line = lines.begin(); line != lines.end(); ++line)
  908. {
  909. std::string sourceFile;
  910. std::string gcovFile;
  911. cmCTestLog(this->CTest, DEBUG, "Line: [" << line->c_str() << "]"
  912. << std::endl);
  913. if ( line->size() == 0 )
  914. {
  915. // Ignore empty line; probably style 2
  916. }
  917. else if ( st1re1.find(line->c_str()) )
  918. {
  919. if ( gcovStyle == 0 )
  920. {
  921. gcovStyle = 1;
  922. }
  923. if ( gcovStyle != 1 )
  924. {
  925. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e1"
  926. << std::endl);
  927. cont->Error ++;
  928. break;
  929. }
  930. actualSourceFile = "";
  931. sourceFile = st1re1.match(2);
  932. }
  933. else if ( st1re2.find(line->c_str() ) )
  934. {
  935. if ( gcovStyle == 0 )
  936. {
  937. gcovStyle = 1;
  938. }
  939. if ( gcovStyle != 1 )
  940. {
  941. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e2"
  942. << std::endl);
  943. cont->Error ++;
  944. break;
  945. }
  946. gcovFile = st1re2.match(1);
  947. }
  948. else if ( st2re1.find(line->c_str() ) )
  949. {
  950. if ( gcovStyle == 0 )
  951. {
  952. gcovStyle = 2;
  953. }
  954. if ( gcovStyle != 2 )
  955. {
  956. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e3"
  957. << std::endl);
  958. cont->Error ++;
  959. break;
  960. }
  961. actualSourceFile = "";
  962. sourceFile = st2re1.match(1);
  963. }
  964. else if ( st2re2.find(line->c_str() ) )
  965. {
  966. if ( gcovStyle == 0 )
  967. {
  968. gcovStyle = 2;
  969. }
  970. if ( gcovStyle != 2 )
  971. {
  972. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e4"
  973. << std::endl);
  974. cont->Error ++;
  975. break;
  976. }
  977. }
  978. else if ( st2re3.find(line->c_str() ) )
  979. {
  980. if ( gcovStyle == 0 )
  981. {
  982. gcovStyle = 2;
  983. }
  984. if ( gcovStyle != 2 )
  985. {
  986. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e5"
  987. << std::endl);
  988. cont->Error ++;
  989. break;
  990. }
  991. gcovFile = st2re3.match(2);
  992. }
  993. else if ( st2re4.find(line->c_str() ) )
  994. {
  995. if ( gcovStyle == 0 )
  996. {
  997. gcovStyle = 2;
  998. }
  999. if ( gcovStyle != 2 )
  1000. {
  1001. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e6"
  1002. << std::endl);
  1003. cont->Error ++;
  1004. break;
  1005. }
  1006. cmCTestLog(this->CTest, WARNING, "Warning: " << st2re4.match(1)
  1007. << " had unexpected EOF" << std::endl);
  1008. }
  1009. else if ( st2re5.find(line->c_str() ) )
  1010. {
  1011. if ( gcovStyle == 0 )
  1012. {
  1013. gcovStyle = 2;
  1014. }
  1015. if ( gcovStyle != 2 )
  1016. {
  1017. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e7"
  1018. << std::endl);
  1019. cont->Error ++;
  1020. break;
  1021. }
  1022. cmCTestLog(this->CTest, WARNING, "Warning: Cannot open file: "
  1023. << st2re5.match(1) << std::endl);
  1024. }
  1025. else if ( st2re6.find(line->c_str() ) )
  1026. {
  1027. if ( gcovStyle == 0 )
  1028. {
  1029. gcovStyle = 2;
  1030. }
  1031. if ( gcovStyle != 2 )
  1032. {
  1033. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e8"
  1034. << std::endl);
  1035. cont->Error ++;
  1036. break;
  1037. }
  1038. cmCTestLog(this->CTest, WARNING, "Warning: File: " << st2re6.match(1)
  1039. << " is newer than " << st2re6.match(2) << std::endl);
  1040. }
  1041. else
  1042. {
  1043. // gcov 4.7 can have output lines saying "No executable lines" and
  1044. // "Removing 'filename.gcov'"... Don't log those as "errors."
  1045. if(*line != "No executable lines" &&
  1046. !cmSystemTools::StringStartsWith(line->c_str(), "Removing "))
  1047. {
  1048. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1049. "Unknown gcov output line: [" << line->c_str() << "]"
  1050. << std::endl);
  1051. cont->Error ++;
  1052. //abort();
  1053. }
  1054. }
  1055. // If the last line of gcov output gave us a valid value for gcovFile,
  1056. // and we have an actualSourceFile, then insert a (or add to existing)
  1057. // SingleFileCoverageVector for actualSourceFile:
  1058. //
  1059. if ( !gcovFile.empty() && !actualSourceFile.empty() )
  1060. {
  1061. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& vec
  1062. = cont->TotalCoverage[actualSourceFile];
  1063. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " in gcovFile: "
  1064. << gcovFile << std::endl);
  1065. cmsys::ifstream ifile(gcovFile.c_str());
  1066. if ( ! ifile )
  1067. {
  1068. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open file: "
  1069. << gcovFile << std::endl);
  1070. }
  1071. else
  1072. {
  1073. long cnt = -1;
  1074. std::string nl;
  1075. while ( cmSystemTools::GetLineFromStream(ifile, nl) )
  1076. {
  1077. cnt ++;
  1078. //TODO: Handle gcov 3.0 non-coverage lines
  1079. // Skip empty lines
  1080. if ( !nl.size() )
  1081. {
  1082. continue;
  1083. }
  1084. // Skip unused lines
  1085. if ( nl.size() < 12 )
  1086. {
  1087. continue;
  1088. }
  1089. // Read the coverage count from the beginning of the gcov output
  1090. // line
  1091. std::string prefix = nl.substr(0, 12);
  1092. int cov = atoi(prefix.c_str());
  1093. // Read the line number starting at the 10th character of the gcov
  1094. // output line
  1095. std::string lineNumber = nl.substr(10, 5);
  1096. int lineIdx = atoi(lineNumber.c_str())-1;
  1097. if ( lineIdx >= 0 )
  1098. {
  1099. while ( vec.size() <= static_cast<size_t>(lineIdx) )
  1100. {
  1101. vec.push_back(-1);
  1102. }
  1103. // Initially all entries are -1 (not used). If we get coverage
  1104. // information, increment it to 0 first.
  1105. if ( vec[lineIdx] < 0 )
  1106. {
  1107. if ( cov > 0 || prefix.find("#") != prefix.npos )
  1108. {
  1109. vec[lineIdx] = 0;
  1110. }
  1111. }
  1112. vec[lineIdx] += cov;
  1113. }
  1114. }
  1115. }
  1116. actualSourceFile = "";
  1117. }
  1118. if ( !sourceFile.empty() && actualSourceFile.empty() )
  1119. {
  1120. gcovFile = "";
  1121. // Is it in the source dir or the binary dir?
  1122. //
  1123. if ( IsFileInDir(sourceFile, cont->SourceDir) )
  1124. {
  1125. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " produced s: "
  1126. << sourceFile.c_str() << std::endl);
  1127. *cont->OFS << " produced in source dir: " << sourceFile.c_str()
  1128. << std::endl;
  1129. actualSourceFile
  1130. = cmSystemTools::CollapseFullPath(sourceFile.c_str());
  1131. }
  1132. else if ( IsFileInDir(sourceFile, cont->BinaryDir) )
  1133. {
  1134. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " produced b: "
  1135. << sourceFile.c_str() << std::endl);
  1136. *cont->OFS << " produced in binary dir: " << sourceFile.c_str()
  1137. << std::endl;
  1138. actualSourceFile
  1139. = cmSystemTools::CollapseFullPath(sourceFile.c_str());
  1140. }
  1141. if ( actualSourceFile.empty() )
  1142. {
  1143. if ( missingFiles.find(sourceFile) == missingFiles.end() )
  1144. {
  1145. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1146. "Something went wrong" << std::endl);
  1147. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1148. "Cannot find file: ["
  1149. << sourceFile.c_str() << "]" << std::endl);
  1150. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1151. " in source dir: ["
  1152. << cont->SourceDir.c_str() << "]"
  1153. << std::endl);
  1154. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1155. " or binary dir: ["
  1156. << cont->BinaryDir.size() << "]"
  1157. << std::endl);
  1158. *cont->OFS << " Something went wrong. Cannot find file: "
  1159. << sourceFile.c_str()
  1160. << " in source dir: " << cont->SourceDir.c_str()
  1161. << " or binary dir: " << cont->BinaryDir.c_str() << std::endl;
  1162. missingFiles.insert(sourceFile);
  1163. }
  1164. }
  1165. }
  1166. }
  1167. file_count++;
  1168. if ( file_count % 50 == 0 )
  1169. {
  1170. cmCTestLog(this->CTest, HANDLER_OUTPUT, " processed: " << file_count
  1171. << " out of " << files.size() << std::endl);
  1172. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  1173. }
  1174. }
  1175. cmSystemTools::ChangeDirectory(currentDirectory.c_str());
  1176. return file_count;
  1177. }
  1178. //----------------------------------------------------------------------------
  1179. void cmCTestCoverageHandler::FindGCovFiles(std::vector<std::string>& files)
  1180. {
  1181. cmsys::Glob gl;
  1182. gl.RecurseOn();
  1183. gl.RecurseThroughSymlinksOff();
  1184. for(LabelMapType::const_iterator lmi = this->TargetDirs.begin();
  1185. lmi != this->TargetDirs.end(); ++lmi)
  1186. {
  1187. // Skip targets containing no interesting labels.
  1188. if(!this->IntersectsFilter(lmi->second))
  1189. {
  1190. continue;
  1191. }
  1192. // Coverage files appear next to their object files in the target
  1193. // support directory.
  1194. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1195. " globbing for coverage in: " << lmi->first << std::endl);
  1196. std::string daGlob = lmi->first;
  1197. daGlob += "/*.da";
  1198. gl.FindFiles(daGlob);
  1199. files.insert(files.end(), gl.GetFiles().begin(), gl.GetFiles().end());
  1200. daGlob = lmi->first;
  1201. daGlob += "/*.gcda";
  1202. gl.FindFiles(daGlob);
  1203. files.insert(files.end(), gl.GetFiles().begin(), gl.GetFiles().end());
  1204. }
  1205. }
  1206. //----------------------------------------------------------------------
  1207. int cmCTestCoverageHandler::HandleTracePyCoverage(
  1208. cmCTestCoverageHandlerContainer* cont)
  1209. {
  1210. cmsys::Glob gl;
  1211. gl.RecurseOn();
  1212. gl.RecurseThroughSymlinksOff();
  1213. std::string daGlob = cont->BinaryDir + "/*.cover";
  1214. gl.FindFiles(daGlob);
  1215. std::vector<std::string> files = gl.GetFiles();
  1216. if ( files.size() == 0 )
  1217. {
  1218. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1219. " Cannot find any Python Trace.py coverage files."
  1220. << std::endl);
  1221. // No coverage files is a valid thing, so the exit code is 0
  1222. return 0;
  1223. }
  1224. std::string testingDir = this->CTest->GetBinaryDir() + "/Testing";
  1225. std::string tempDir = testingDir + "/CoverageInfo";
  1226. std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
  1227. cmSystemTools::MakeDirectory(tempDir.c_str());
  1228. cmSystemTools::ChangeDirectory(tempDir.c_str());
  1229. cmSystemTools::ChangeDirectory(currentDirectory.c_str());
  1230. std::vector<std::string>::iterator fileIt;
  1231. int file_count = 0;
  1232. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  1233. {
  1234. std::string fileName = this->FindFile(cont, *fileIt);
  1235. if ( fileName.empty() )
  1236. {
  1237. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1238. "Cannot find source Python file corresponding to: "
  1239. << fileIt->c_str() << std::endl);
  1240. continue;
  1241. }
  1242. std::string actualSourceFile
  1243. = cmSystemTools::CollapseFullPath(fileName.c_str());
  1244. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1245. " Check coverage for file: " << actualSourceFile.c_str()
  1246. << std::endl);
  1247. cmCTestCoverageHandlerContainer::SingleFileCoverageVector* vec
  1248. = &cont->TotalCoverage[actualSourceFile];
  1249. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1250. " in file: " << fileIt->c_str() << std::endl);
  1251. cmsys::ifstream ifile(fileIt->c_str());
  1252. if ( ! ifile )
  1253. {
  1254. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open file: "
  1255. << fileIt->c_str() << std::endl);
  1256. }
  1257. else
  1258. {
  1259. long cnt = -1;
  1260. std::string nl;
  1261. while ( cmSystemTools::GetLineFromStream(ifile, nl) )
  1262. {
  1263. cnt ++;
  1264. // Skip empty lines
  1265. if ( !nl.size() )
  1266. {
  1267. continue;
  1268. }
  1269. // Skip unused lines
  1270. if ( nl.size() < 12 )
  1271. {
  1272. continue;
  1273. }
  1274. // Read the coverage count from the beginning of the Trace.py output
  1275. // line
  1276. std::string prefix = nl.substr(0, 6);
  1277. if ( prefix[5] != ' ' && prefix[5] != ':' )
  1278. {
  1279. // This is a hack. We should really do something more elaborate
  1280. prefix = nl.substr(0, 7);
  1281. if ( prefix[6] != ' ' && prefix[6] != ':' )
  1282. {
  1283. prefix = nl.substr(0, 8);
  1284. if ( prefix[7] != ' ' && prefix[7] != ':' )
  1285. {
  1286. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1287. "Currently the limit is maximum coverage of 999999"
  1288. << std::endl);
  1289. }
  1290. }
  1291. }
  1292. int cov = atoi(prefix.c_str());
  1293. if ( prefix[prefix.size()-1] != ':' )
  1294. {
  1295. // This line does not have ':' so no coverage here. That said,
  1296. // Trace.py does not handle not covered lines versus comments etc.
  1297. // So, this will be set to 0.
  1298. cov = 0;
  1299. }
  1300. cmCTestLog(this->CTest, DEBUG, "Prefix: " << prefix.c_str()
  1301. << " cov: " << cov
  1302. << std::endl);
  1303. // Read the line number starting at the 10th character of the gcov
  1304. // output line
  1305. long lineIdx = cnt;
  1306. if ( lineIdx >= 0 )
  1307. {
  1308. while ( vec->size() <=
  1309. static_cast<size_t>(lineIdx) )
  1310. {
  1311. vec->push_back(-1);
  1312. }
  1313. // Initially all entries are -1 (not used). If we get coverage
  1314. // information, increment it to 0 first.
  1315. if ( (*vec)[lineIdx] < 0 )
  1316. {
  1317. if ( cov >= 0 )
  1318. {
  1319. (*vec)[lineIdx] = 0;
  1320. }
  1321. }
  1322. (*vec)[lineIdx] += cov;
  1323. }
  1324. }
  1325. }
  1326. ++ file_count;
  1327. }
  1328. cmSystemTools::ChangeDirectory(currentDirectory.c_str());
  1329. return file_count;
  1330. }
  1331. //----------------------------------------------------------------------
  1332. std::string cmCTestCoverageHandler::FindFile(
  1333. cmCTestCoverageHandlerContainer* cont,
  1334. std::string fileName)
  1335. {
  1336. std::string fileNameNoE
  1337. = cmSystemTools::GetFilenameWithoutLastExtension(fileName);
  1338. // First check in source and binary directory
  1339. std::string fullName = cont->SourceDir + "/" + fileNameNoE + ".py";
  1340. if ( cmSystemTools::FileExists(fullName.c_str()) )
  1341. {
  1342. return fullName;
  1343. }
  1344. fullName = cont->BinaryDir + "/" + fileNameNoE + ".py";
  1345. if ( cmSystemTools::FileExists(fullName.c_str()) )
  1346. {
  1347. return fullName;
  1348. }
  1349. return "";
  1350. }
  1351. // This is a header put on each marked up source file
  1352. namespace
  1353. {
  1354. const char* bullseyeHelp[] =
  1355. {" Coverage produced by bullseye covbr tool: ",
  1356. " www.bullseye.com/help/ref_covbr.html",
  1357. " * An arrow --> indicates incomplete coverage.",
  1358. " * An X indicates a function that was invoked, a switch label that ",
  1359. " was exercised, a try-block that finished, or an exception handler ",
  1360. " that was invoked.",
  1361. " * A T or F indicates a boolean decision that evaluated true or false,",
  1362. " respectively.",
  1363. " * A t or f indicates a boolean condition within a decision if the ",
  1364. " condition evaluated true or false, respectively.",
  1365. " * A k indicates a constant decision or condition.",
  1366. " * The slash / means this probe is excluded from summary results. ",
  1367. 0};
  1368. }
  1369. //----------------------------------------------------------------------
  1370. int cmCTestCoverageHandler::RunBullseyeCoverageBranch(
  1371. cmCTestCoverageHandlerContainer* cont,
  1372. std::set<std::string>& coveredFileNames,
  1373. std::vector<std::string>& files,
  1374. std::vector<std::string>& filesFullPath)
  1375. {
  1376. if(files.size() != filesFullPath.size())
  1377. {
  1378. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1379. "Files and full path files not the same size?:\n");
  1380. return 0;
  1381. }
  1382. // create the output stream for the CoverageLog-N.xml file
  1383. cmGeneratedFileStream covLogFile;
  1384. int logFileCount = 0;
  1385. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  1386. {
  1387. return -1;
  1388. }
  1389. // for each file run covbr on that file to get the coverage
  1390. // information for that file
  1391. std::string outputFile;
  1392. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1393. "run covbr: "
  1394. << std::endl);
  1395. if(!this->RunBullseyeCommand(cont, "covbr", 0, outputFile))
  1396. {
  1397. cmCTestLog(this->CTest, ERROR_MESSAGE, "error running covbr for." << "\n");
  1398. return -1;
  1399. }
  1400. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1401. "covbr output in " << outputFile
  1402. << std::endl);
  1403. // open the output file
  1404. cmsys::ifstream fin(outputFile.c_str());
  1405. if(!fin)
  1406. {
  1407. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1408. "Cannot open coverage file: " <<
  1409. outputFile.c_str() << std::endl);
  1410. return 0;
  1411. }
  1412. std::map<std::string, std::string> fileMap;
  1413. std::vector<std::string>::iterator fp = filesFullPath.begin();
  1414. for(std::vector<std::string>::iterator f = files.begin();
  1415. f != files.end(); ++f, ++fp)
  1416. {
  1417. fileMap[*f] = *fp;
  1418. }
  1419. int count =0; // keep count of the number of files
  1420. // Now parse each line from the bullseye cov log file
  1421. std::string lineIn;
  1422. bool valid = false; // are we in a valid output file
  1423. int line = 0; // line of the current file
  1424. std::string file;
  1425. while(cmSystemTools::GetLineFromStream(fin, lineIn))
  1426. {
  1427. bool startFile = false;
  1428. if(lineIn.size() > 1 && lineIn[lineIn.size()-1] == ':')
  1429. {
  1430. file = lineIn.substr(0, lineIn.size()-1);
  1431. if(coveredFileNames.find(file) != coveredFileNames.end())
  1432. {
  1433. startFile = true;
  1434. }
  1435. }
  1436. if(startFile)
  1437. {
  1438. // if we are in a valid file close it because a new one started
  1439. if(valid)
  1440. {
  1441. covLogFile << "\t\t</Report>" << std::endl
  1442. << "\t</File>" << std::endl;
  1443. }
  1444. // only allow 100 files in each log file
  1445. if ( count != 0 && count % 100 == 0 )
  1446. {
  1447. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1448. "start a new log file: "
  1449. << count
  1450. << std::endl);
  1451. this->EndCoverageLogFile(covLogFile, logFileCount);
  1452. logFileCount ++;
  1453. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  1454. {
  1455. return -1;
  1456. }
  1457. count++; // move on one
  1458. }
  1459. std::map<std::string, std::string>::iterator
  1460. i = fileMap.find(file);
  1461. // if the file should be covered write out the header for that file
  1462. if(i != fileMap.end())
  1463. {
  1464. // we have a new file so count it in the output
  1465. count++;
  1466. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1467. "Produce coverage for file: "
  1468. << file.c_str() << " " << count
  1469. << std::endl);
  1470. // start the file output
  1471. covLogFile << "\t<File Name=\""
  1472. << cmXMLSafe(i->first)
  1473. << "\" FullPath=\"" << cmXMLSafe(
  1474. this->CTest->GetShortPathToFile(
  1475. i->second.c_str())) << "\">" << std::endl
  1476. << "\t\t<Report>" << std::endl;
  1477. // write the bullseye header
  1478. line =0;
  1479. for(int k =0; bullseyeHelp[k] != 0; ++k)
  1480. {
  1481. covLogFile << "\t\t<Line Number=\"" << line << "\" Count=\"-1\">"
  1482. << cmXMLSafe(bullseyeHelp[k])
  1483. << "</Line>" << std::endl;
  1484. line++;
  1485. }
  1486. valid = true; // we are in a valid file section
  1487. }
  1488. else
  1489. {
  1490. // this is not a file that we want coverage for
  1491. valid = false;
  1492. }
  1493. }
  1494. // we are not at a start file, and we are in a valid file output the line
  1495. else if(valid)
  1496. {
  1497. covLogFile << "\t\t<Line Number=\"" << line << "\" Count=\"-1\">"
  1498. << cmXMLSafe(lineIn)
  1499. << "</Line>" << std::endl;
  1500. line++;
  1501. }
  1502. }
  1503. // if we ran out of lines a valid file then close that file
  1504. if(valid)
  1505. {
  1506. covLogFile << "\t\t</Report>" << std::endl
  1507. << "\t</File>" << std::endl;
  1508. }
  1509. this->EndCoverageLogFile(covLogFile, logFileCount);
  1510. return 1;
  1511. }
  1512. //----------------------------------------------------------------------
  1513. int cmCTestCoverageHandler::RunBullseyeCommand(
  1514. cmCTestCoverageHandlerContainer* cont,
  1515. const char* cmd,
  1516. const char* arg,
  1517. std::string& outputFile)
  1518. {
  1519. std::string program = cmSystemTools::FindProgram(cmd);
  1520. if(program.size() == 0)
  1521. {
  1522. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot find :" << cmd << "\n");
  1523. return 0;
  1524. }
  1525. if(arg)
  1526. {
  1527. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1528. "Run : " << program.c_str() << " " << arg << "\n");
  1529. }
  1530. else
  1531. {
  1532. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1533. "Run : " << program.c_str() << "\n");
  1534. }
  1535. // create a process object and start it
  1536. cmCTestRunProcess runCoverageSrc;
  1537. runCoverageSrc.SetCommand(program.c_str());
  1538. runCoverageSrc.AddArgument(arg);
  1539. std::string stdoutFile = cont->BinaryDir + "/Testing/Temporary/";
  1540. stdoutFile += this->GetCTestInstance()->GetCurrentTag();
  1541. stdoutFile += "-";
  1542. stdoutFile += cmd;
  1543. std::string stderrFile = stdoutFile;
  1544. stdoutFile += ".stdout";
  1545. stderrFile += ".stderr";
  1546. runCoverageSrc.SetStdoutFile(stdoutFile.c_str());
  1547. runCoverageSrc.SetStderrFile(stderrFile.c_str());
  1548. if(!runCoverageSrc.StartProcess())
  1549. {
  1550. cmCTestLog(this->CTest, ERROR_MESSAGE, "Could not run : "
  1551. << program.c_str() << " " << arg << "\n"
  1552. << "kwsys process state : "
  1553. << runCoverageSrc.GetProcessState());
  1554. return 0;
  1555. }
  1556. // since we set the output file names wait for it to end
  1557. runCoverageSrc.WaitForExit();
  1558. outputFile = stdoutFile;
  1559. return 1;
  1560. }
  1561. //----------------------------------------------------------------------
  1562. int cmCTestCoverageHandler::RunBullseyeSourceSummary(
  1563. cmCTestCoverageHandlerContainer* cont)
  1564. {
  1565. // Run the covsrc command and create a temp outputfile
  1566. std::string outputFile;
  1567. if(!this->RunBullseyeCommand(cont, "covsrc", "-c", outputFile))
  1568. {
  1569. cmCTestLog(this->CTest, ERROR_MESSAGE, "error running covsrc:\n");
  1570. return 0;
  1571. }
  1572. std::ostream& tmpLog = *cont->OFS;
  1573. // copen the Coverage.xml file in the Testing directory
  1574. cmGeneratedFileStream covSumFile;
  1575. if(!this->StartResultingXML(cmCTest::PartCoverage, "Coverage", covSumFile))
  1576. {
  1577. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1578. "Cannot open coverage summary file." << std::endl);
  1579. return 0;
  1580. }
  1581. this->CTest->StartXML(covSumFile, this->AppendXML);
  1582. double elapsed_time_start = cmSystemTools::GetTime();
  1583. std::string coverage_start_time = this->CTest->CurrentTime();
  1584. covSumFile << "<Coverage>" << std::endl
  1585. << "\t<StartDateTime>"
  1586. << coverage_start_time << "</StartDateTime>"
  1587. << std::endl
  1588. << "\t<StartTime>"
  1589. << static_cast<unsigned int>(cmSystemTools::GetTime())
  1590. << "</StartTime>"
  1591. << std::endl;
  1592. std::string stdline;
  1593. std::string errline;
  1594. // expected output:
  1595. // first line is:
  1596. // "Source","Function Coverage","out of","%","C/D Coverage","out of","%"
  1597. // after that data follows in that format
  1598. std::string sourceFile;
  1599. int functionsCalled = 0;
  1600. int totalFunctions = 0;
  1601. int percentFunction = 0;
  1602. int branchCovered = 0;
  1603. int totalBranches = 0;
  1604. int percentBranch = 0;
  1605. double total_tested = 0;
  1606. double total_untested = 0;
  1607. double total_functions = 0;
  1608. double percent_coverage =0;
  1609. double number_files = 0;
  1610. std::vector<std::string> coveredFiles;
  1611. std::vector<std::string> coveredFilesFullPath;
  1612. // Read and parse the summary output file
  1613. cmsys::ifstream fin(outputFile.c_str());
  1614. if(!fin)
  1615. {
  1616. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1617. "Cannot open coverage summary file: " <<
  1618. outputFile.c_str() << std::endl);
  1619. return 0;
  1620. }
  1621. std::set<std::string> coveredFileNames;
  1622. while(cmSystemTools::GetLineFromStream(fin, stdline))
  1623. {
  1624. // if we have a line of output from stdout
  1625. if(stdline.size())
  1626. {
  1627. // parse the comma separated output
  1628. this->ParseBullsEyeCovsrcLine(stdline,
  1629. sourceFile,
  1630. functionsCalled,
  1631. totalFunctions,
  1632. percentFunction,
  1633. branchCovered,
  1634. totalBranches,
  1635. percentBranch);
  1636. // The first line is the header
  1637. if(sourceFile == "Source" || sourceFile == "Total")
  1638. {
  1639. continue;
  1640. }
  1641. std::string file = sourceFile;
  1642. coveredFileNames.insert(file);
  1643. if(!cmSystemTools::FileIsFullPath(sourceFile.c_str()))
  1644. {
  1645. // file will be relative to the binary dir
  1646. file = cont->BinaryDir;
  1647. file += "/";
  1648. file += sourceFile;
  1649. }
  1650. file = cmSystemTools::CollapseFullPath(file.c_str());
  1651. bool shouldIDoCoverage
  1652. = this->ShouldIDoCoverage(file.c_str(),
  1653. cont->SourceDir.c_str(),
  1654. cont->BinaryDir.c_str());
  1655. if ( !shouldIDoCoverage )
  1656. {
  1657. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1658. ".NoDartCoverage found, so skip coverage check for: "
  1659. << file.c_str()
  1660. << std::endl);
  1661. continue;
  1662. }
  1663. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1664. "Doing coverage for: "
  1665. << file.c_str()
  1666. << std::endl);
  1667. coveredFiles.push_back(sourceFile);
  1668. coveredFilesFullPath.push_back(file);
  1669. number_files++;
  1670. total_functions += totalFunctions;
  1671. total_tested += functionsCalled;
  1672. total_untested += (totalFunctions - functionsCalled);
  1673. std::string fileName = cmSystemTools::GetFilenameName(file.c_str());
  1674. std::string shortFileName =
  1675. this->CTest->GetShortPathToFile(file.c_str());
  1676. float cper = static_cast<float>(percentBranch + percentFunction);
  1677. if(totalBranches > 0)
  1678. {
  1679. cper /= 2.0f;
  1680. }
  1681. percent_coverage += cper;
  1682. float cmet = static_cast<float>(percentFunction + percentBranch);
  1683. if(totalBranches > 0)
  1684. {
  1685. cmet /= 2.0f;
  1686. }
  1687. cmet /= 100.0f;
  1688. tmpLog << stdline.c_str() << "\n";
  1689. tmpLog << fileName << "\n";
  1690. tmpLog << "functionsCalled: " << functionsCalled/100 << "\n";
  1691. tmpLog << "totalFunctions: " << totalFunctions/100 << "\n";
  1692. tmpLog << "percentFunction: " << percentFunction << "\n";
  1693. tmpLog << "branchCovered: " << branchCovered << "\n";
  1694. tmpLog << "totalBranches: " << totalBranches << "\n";
  1695. tmpLog << "percentBranch: " << percentBranch << "\n";
  1696. tmpLog << "percentCoverage: " << percent_coverage << "\n";
  1697. tmpLog << "coverage metric: " << cmet << "\n";
  1698. covSumFile << "\t<File Name=\"" << cmXMLSafe(sourceFile)
  1699. << "\" FullPath=\"" << cmXMLSafe(shortFileName)
  1700. << "\" Covered=\"" << (cmet>0?"true":"false") << "\">\n"
  1701. << "\t\t<BranchesTested>"
  1702. << branchCovered
  1703. << "</BranchesTested>\n"
  1704. << "\t\t<BranchesUnTested>"
  1705. << totalBranches - branchCovered
  1706. << "</BranchesUnTested>\n"
  1707. << "\t\t<FunctionsTested>"
  1708. << functionsCalled
  1709. << "</FunctionsTested>\n"
  1710. << "\t\t<FunctionsUnTested>"
  1711. << totalFunctions - functionsCalled
  1712. << "</FunctionsUnTested>\n"
  1713. // Hack for conversion of function to loc assume a function
  1714. // has 100 lines of code
  1715. << "\t\t<LOCTested>" << functionsCalled *100
  1716. << "</LOCTested>\n"
  1717. << "\t\t<LOCUnTested>"
  1718. << (totalFunctions - functionsCalled)*100
  1719. << "</LOCUnTested>\n"
  1720. << "\t\t<PercentCoverage>";
  1721. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  1722. covSumFile.precision(2);
  1723. covSumFile << (cper) << "</PercentCoverage>\n"
  1724. << "\t\t<CoverageMetric>";
  1725. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  1726. covSumFile.precision(2);
  1727. covSumFile << (cmet) << "</CoverageMetric>\n";
  1728. this->WriteXMLLabels(covSumFile, shortFileName);
  1729. covSumFile << "\t</File>" << std::endl;
  1730. }
  1731. }
  1732. std::string end_time = this->CTest->CurrentTime();
  1733. covSumFile << "\t<LOCTested>" << total_tested << "</LOCTested>\n"
  1734. << "\t<LOCUntested>" << total_untested << "</LOCUntested>\n"
  1735. << "\t<LOC>" << total_functions << "</LOC>\n"
  1736. << "\t<PercentCoverage>";
  1737. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  1738. covSumFile.precision(2);
  1739. covSumFile
  1740. << SAFEDIV(percent_coverage,number_files)<< "</PercentCoverage>\n"
  1741. << "\t<EndDateTime>" << end_time << "</EndDateTime>\n"
  1742. << "\t<EndTime>" << static_cast<unsigned int>(cmSystemTools::GetTime())
  1743. << "</EndTime>\n";
  1744. covSumFile
  1745. << "<ElapsedMinutes>" <<
  1746. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  1747. << "</ElapsedMinutes>"
  1748. << "</Coverage>" << std::endl;
  1749. this->CTest->EndXML(covSumFile);
  1750. // Now create the coverage information for each file
  1751. return this->RunBullseyeCoverageBranch(cont,
  1752. coveredFileNames,
  1753. coveredFiles,
  1754. coveredFilesFullPath);
  1755. }
  1756. //----------------------------------------------------------------------
  1757. int cmCTestCoverageHandler::HandleBullseyeCoverage(
  1758. cmCTestCoverageHandlerContainer* cont)
  1759. {
  1760. const char* covfile = cmSystemTools::GetEnv("COVFILE");
  1761. if(!covfile || strlen(covfile) == 0)
  1762. {
  1763. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1764. " COVFILE environment variable not found, not running "
  1765. " bullseye\n");
  1766. return 0;
  1767. }
  1768. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1769. " run covsrc with COVFILE=["
  1770. << covfile
  1771. << "]" << std::endl);
  1772. if(!this->RunBullseyeSourceSummary(cont))
  1773. {
  1774. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1775. "Error running bullseye summary.\n");
  1776. return 0;
  1777. }
  1778. cmCTestLog(this->CTest, DEBUG, "HandleBullseyeCoverage return 1 "
  1779. << std::endl);
  1780. return 1;
  1781. }
  1782. bool cmCTestCoverageHandler::GetNextInt(std::string const& inputLine,
  1783. std::string::size_type& pos,
  1784. int& value)
  1785. {
  1786. std::string::size_type start = pos;
  1787. pos = inputLine.find(',', start);
  1788. value = atoi(inputLine.substr(start, pos).c_str());
  1789. if(pos == inputLine.npos)
  1790. {
  1791. return true;
  1792. }
  1793. pos++;
  1794. return true;
  1795. }
  1796. bool cmCTestCoverageHandler::ParseBullsEyeCovsrcLine(
  1797. std::string const& inputLine,
  1798. std::string& sourceFile,
  1799. int& functionsCalled,
  1800. int& totalFunctions,
  1801. int& percentFunction,
  1802. int& branchCovered,
  1803. int& totalBranches,
  1804. int& percentBranch)
  1805. {
  1806. // find the first comma
  1807. std::string::size_type pos = inputLine.find(',');
  1808. if(pos == inputLine.npos)
  1809. {
  1810. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error parsing string : "
  1811. << inputLine.c_str() << "\n");
  1812. return false;
  1813. }
  1814. // the source file has "" around it so extract out the file name
  1815. sourceFile = inputLine.substr(1,pos-2);
  1816. pos++;
  1817. if(!this->GetNextInt(inputLine, pos, functionsCalled))
  1818. {
  1819. return false;
  1820. }
  1821. if(!this->GetNextInt(inputLine, pos, totalFunctions))
  1822. {
  1823. return false;
  1824. }
  1825. if(!this->GetNextInt(inputLine, pos, percentFunction))
  1826. {
  1827. return false;
  1828. }
  1829. if(!this->GetNextInt(inputLine, pos, branchCovered))
  1830. {
  1831. return false;
  1832. }
  1833. if(!this->GetNextInt(inputLine, pos, totalBranches))
  1834. {
  1835. return false;
  1836. }
  1837. if(!this->GetNextInt(inputLine, pos, percentBranch))
  1838. {
  1839. return false;
  1840. }
  1841. // should be at the end now
  1842. if(pos != inputLine.npos)
  1843. {
  1844. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error parsing input : "
  1845. << inputLine.c_str() << " last pos not npos = " << pos <<
  1846. "\n");
  1847. }
  1848. return true;
  1849. }
  1850. //----------------------------------------------------------------------
  1851. int cmCTestCoverageHandler::GetLabelId(std::string const& label)
  1852. {
  1853. LabelIdMapType::iterator i = this->LabelIdMap.find(label);
  1854. if(i == this->LabelIdMap.end())
  1855. {
  1856. int n = int(this->Labels.size());
  1857. this->Labels.push_back(label);
  1858. LabelIdMapType::value_type entry(label, n);
  1859. i = this->LabelIdMap.insert(entry).first;
  1860. }
  1861. return i->second;
  1862. }
  1863. //----------------------------------------------------------------------
  1864. void cmCTestCoverageHandler::LoadLabels()
  1865. {
  1866. std::string fileList = this->CTest->GetBinaryDir();
  1867. fileList += cmake::GetCMakeFilesDirectory();
  1868. fileList += "/TargetDirectories.txt";
  1869. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1870. " target directory list [" << fileList << "]\n");
  1871. cmsys::ifstream finList(fileList.c_str());
  1872. std::string line;
  1873. while(cmSystemTools::GetLineFromStream(finList, line))
  1874. {
  1875. this->LoadLabels(line.c_str());
  1876. }
  1877. }
  1878. //----------------------------------------------------------------------
  1879. void cmCTestCoverageHandler::LoadLabels(const char* dir)
  1880. {
  1881. LabelSet& dirLabels = this->TargetDirs[dir];
  1882. std::string fname = dir;
  1883. fname += "/Labels.txt";
  1884. cmsys::ifstream fin(fname.c_str());
  1885. if(!fin)
  1886. {
  1887. return;
  1888. }
  1889. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1890. " loading labels from [" << fname << "]\n");
  1891. bool inTarget = true;
  1892. std::string source;
  1893. std::string line;
  1894. std::vector<int> targetLabels;
  1895. while(cmSystemTools::GetLineFromStream(fin, line))
  1896. {
  1897. if(line.empty() || line[0] == '#')
  1898. {
  1899. // Ignore blank and comment lines.
  1900. continue;
  1901. }
  1902. else if(line[0] == ' ')
  1903. {
  1904. // Label lines appear indented by one space.
  1905. std::string label = line.substr(1);
  1906. int id = this->GetLabelId(label);
  1907. dirLabels.insert(id);
  1908. if(inTarget)
  1909. {
  1910. targetLabels.push_back(id);
  1911. }
  1912. else
  1913. {
  1914. this->SourceLabels[source].insert(id);
  1915. }
  1916. }
  1917. else
  1918. {
  1919. // Non-indented lines specify a source file name. The first one
  1920. // is the end of the target-wide labels.
  1921. inTarget = false;
  1922. source = this->CTest->GetShortPathToFile(line.c_str());
  1923. // Label the source with the target labels.
  1924. LabelSet& labelSet = this->SourceLabels[source];
  1925. for(std::vector<int>::const_iterator li = targetLabels.begin();
  1926. li != targetLabels.end(); ++li)
  1927. {
  1928. labelSet.insert(*li);
  1929. }
  1930. }
  1931. }
  1932. }
  1933. //----------------------------------------------------------------------
  1934. void cmCTestCoverageHandler::WriteXMLLabels(std::ostream& os,
  1935. std::string const& source)
  1936. {
  1937. LabelMapType::const_iterator li = this->SourceLabels.find(source);
  1938. if(li != this->SourceLabels.end() && !li->second.empty())
  1939. {
  1940. os << "\t\t<Labels>\n";
  1941. for(LabelSet::const_iterator lsi = li->second.begin();
  1942. lsi != li->second.end(); ++lsi)
  1943. {
  1944. os << "\t\t\t<Label>" << cmXMLSafe(this->Labels[*lsi]) << "</Label>\n";
  1945. }
  1946. os << "\t\t</Labels>\n";
  1947. }
  1948. }
  1949. //----------------------------------------------------------------------------
  1950. void
  1951. cmCTestCoverageHandler::SetLabelFilter(std::set<std::string> const& labels)
  1952. {
  1953. this->LabelFilter.clear();
  1954. for(std::set<std::string>::const_iterator li = labels.begin();
  1955. li != labels.end(); ++li)
  1956. {
  1957. this->LabelFilter.insert(this->GetLabelId(*li));
  1958. }
  1959. }
  1960. //----------------------------------------------------------------------
  1961. bool cmCTestCoverageHandler::IntersectsFilter(LabelSet const& labels)
  1962. {
  1963. // If there is no label filter then nothing is filtered out.
  1964. if(this->LabelFilter.empty())
  1965. {
  1966. return true;
  1967. }
  1968. std::vector<int> ids;
  1969. cmsys_stl::set_intersection
  1970. (labels.begin(), labels.end(),
  1971. this->LabelFilter.begin(), this->LabelFilter.end(),
  1972. cmsys_stl::back_inserter(ids));
  1973. return !ids.empty();
  1974. }
  1975. //----------------------------------------------------------------------
  1976. bool cmCTestCoverageHandler::IsFilteredOut(std::string const& source)
  1977. {
  1978. // If there is no label filter then nothing is filtered out.
  1979. if(this->LabelFilter.empty())
  1980. {
  1981. return false;
  1982. }
  1983. // The source is filtered out if it does not have any labels in
  1984. // common with the filter set.
  1985. std::string shortSrc = this->CTest->GetShortPathToFile(source.c_str());
  1986. LabelMapType::const_iterator li = this->SourceLabels.find(shortSrc);
  1987. if(li != this->SourceLabels.end())
  1988. {
  1989. return !this->IntersectsFilter(li->second);
  1990. }
  1991. return true;
  1992. }
  1993. //----------------------------------------------------------------------
  1994. std::set<std::string> cmCTestCoverageHandler::FindUncoveredFiles(
  1995. cmCTestCoverageHandlerContainer* cont)
  1996. {
  1997. std::set<std::string> extraMatches;
  1998. for(std::vector<std::string>::iterator i = this->ExtraCoverageGlobs.begin();
  1999. i != this->ExtraCoverageGlobs.end(); ++i)
  2000. {
  2001. cmsys::Glob gl;
  2002. gl.RecurseOn();
  2003. gl.RecurseThroughSymlinksOff();
  2004. std::string glob = cont->SourceDir + "/" + *i;
  2005. gl.FindFiles(glob);
  2006. std::vector<std::string> files = gl.GetFiles();
  2007. for(std::vector<std::string>::iterator f = files.begin();
  2008. f != files.end(); ++f)
  2009. {
  2010. if(this->ShouldIDoCoverage(f->c_str(),
  2011. cont->SourceDir.c_str(), cont->BinaryDir.c_str()))
  2012. {
  2013. extraMatches.insert(this->CTest->GetShortPathToFile(
  2014. f->c_str()));
  2015. }
  2016. }
  2017. }
  2018. if(extraMatches.size())
  2019. {
  2020. for(cmCTestCoverageHandlerContainer::TotalCoverageMap::iterator i =
  2021. cont->TotalCoverage.begin(); i != cont->TotalCoverage.end(); ++i)
  2022. {
  2023. std::string shortPath = this->CTest->GetShortPathToFile(
  2024. i->first.c_str());
  2025. extraMatches.erase(shortPath);
  2026. }
  2027. }
  2028. return extraMatches;
  2029. }