cmCTestCoverageHandler.h 5.1 KB

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