cmCTestCoverageHandler.cxx 83 KB

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