cmCTestCoverageHandler.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmCTestCoverageHandler_h
  14. #define cmCTestCoverageHandler_h
  15. #include "cmCTestGenericHandler.h"
  16. #include "cmListFileCache.h"
  17. #include <cmsys/RegularExpression.hxx>
  18. class cmGeneratedFileStream;
  19. class cmCTestCoverageHandlerContainer;
  20. /** \class cmCTestCoverageHandler
  21. * \brief A class that handles coverage computaiton for ctest
  22. *
  23. */
  24. class cmCTestCoverageHandler : public cmCTestGenericHandler
  25. {
  26. public:
  27. cmTypeMacro(cmCTestCoverageHandler, cmCTestGenericHandler);
  28. /*
  29. * The main entry point for this class
  30. */
  31. int ProcessHandler();
  32. cmCTestCoverageHandler();
  33. virtual void Initialize();
  34. /**
  35. * This method is called when reading CTest custom file
  36. */
  37. void PopulateCustomVectors(cmMakefile *mf);
  38. /** Report coverage only for sources with these labels. */
  39. void SetLabelFilter(std::set<cmStdString> const& labels);
  40. private:
  41. bool ShouldIDoCoverage(const char* file, const char* srcDir,
  42. const char* binDir);
  43. void CleanCoverageLogFiles(std::ostream& log);
  44. bool StartCoverageLogFile(cmGeneratedFileStream& ostr, int logFileCount);
  45. void EndCoverageLogFile(cmGeneratedFileStream& ostr, int logFileCount);
  46. //! Handle coverage using GCC's GCov
  47. int HandleGCovCoverage(cmCTestCoverageHandlerContainer* cont);
  48. void FindGCovFiles(std::vector<std::string>& files);
  49. //! Handle coverage using Bullseye
  50. int HandleBullseyeCoverage(cmCTestCoverageHandlerContainer* cont);
  51. int RunBullseyeSourceSummary(cmCTestCoverageHandlerContainer* cont);
  52. int RunBullseyeCoverageBranch(cmCTestCoverageHandlerContainer* cont,
  53. std::set<cmStdString>& coveredFileNames,
  54. std::vector<std::string>& files,
  55. std::vector<std::string>& filesFullPath);
  56. int RunBullseyeCommand(
  57. cmCTestCoverageHandlerContainer* cont,
  58. const char* cmd,
  59. const char* arg,
  60. std::string& outputFile);
  61. bool ParseBullsEyeCovsrcLine(
  62. std::string const& inputLine,
  63. std::string& sourceFile,
  64. int& functionsCalled,
  65. int& totalFunctions,
  66. int& percentFunction,
  67. int& branchCovered,
  68. int& totalBranches,
  69. int& percentBranch);
  70. bool GetNextInt(std::string const& inputLine,
  71. std::string::size_type& pos,
  72. int& value);
  73. //! Handle Python coverage using Python's Trace.py
  74. int HandleTracePyCoverage(cmCTestCoverageHandlerContainer* cont);
  75. // Find the source file based on the source and build tree. This is used for
  76. // Trace.py mode, since that one does not tell us where the source file is.
  77. std::string FindFile(cmCTestCoverageHandlerContainer* cont,
  78. std::string fileName);
  79. struct cmCTestCoverage
  80. {
  81. cmCTestCoverage()
  82. {
  83. this->AbsolutePath = "";
  84. this->FullPath = "";
  85. this->Covered = false;
  86. this->Tested = 0;
  87. this->UnTested = 0;
  88. this->Lines.clear();
  89. this->Show = false;
  90. }
  91. cmCTestCoverage(const cmCTestCoverage& rhs) :
  92. AbsolutePath(rhs.AbsolutePath),
  93. FullPath(rhs.FullPath),
  94. Covered(rhs.Covered),
  95. Tested(rhs.Tested),
  96. UnTested(rhs.UnTested),
  97. Lines(rhs.Lines),
  98. Show(rhs.Show)
  99. {
  100. }
  101. cmCTestCoverage& operator=(const cmCTestCoverage& rhs)
  102. {
  103. this->AbsolutePath = rhs.AbsolutePath;
  104. this->FullPath = rhs.FullPath;
  105. this->Covered = rhs.Covered;
  106. this->Tested = rhs.Tested;
  107. this->UnTested = rhs.UnTested;
  108. this->Lines = rhs.Lines;
  109. this->Show = rhs.Show;
  110. return *this;
  111. }
  112. std::string AbsolutePath;
  113. std::string FullPath;
  114. bool Covered;
  115. int Tested;
  116. int UnTested;
  117. std::vector<int> Lines;
  118. bool Show;
  119. };
  120. std::vector<cmStdString> CustomCoverageExclude;
  121. std::vector<cmsys::RegularExpression> CustomCoverageExcludeRegex;
  122. typedef std::map<std::string, cmCTestCoverage> CoverageMap;
  123. // Map from source file to label ids.
  124. class LabelSet: public std::set<int> {};
  125. typedef std::map<cmStdString, LabelSet> LabelMapType;
  126. LabelMapType SourceLabels;
  127. LabelMapType TargetDirs;
  128. // Map from label name to label id.
  129. typedef std::map<cmStdString, int> LabelIdMapType;
  130. LabelIdMapType LabelIdMap;
  131. std::vector<std::string> Labels;
  132. int GetLabelId(std::string const& label);
  133. // Label reading and writing methods.
  134. void LoadLabels();
  135. void LoadLabels(const char* dir);
  136. void WriteXMLLabels(std::ofstream& os, std::string const& source);
  137. // Label-based filtering.
  138. std::set<int> LabelFilter;
  139. bool IntersectsFilter(LabelSet const& labels);
  140. bool IsFilteredOut(std::string const& source);
  141. };
  142. #endif