cmCTestCoverageHandler.cxx 83 KB

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