cmCTestCoverageHandler.cxx 80 KB

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