137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//===- CoverageSummaryInfo.h - Coverage summary for function/file ---------===// 237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// 337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// The LLVM Compiler Infrastructure 437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// 537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// This file is distributed under the University of Illinois Open Source 637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// License. See LICENSE.TXT for details. 737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// 837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//===----------------------------------------------------------------------===// 937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// 1037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// These structures are used to represent code coverage metrics 1137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// for functions/files. 1237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// 1337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//===----------------------------------------------------------------------===// 1437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 1537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#ifndef LLVM_COV_COVERAGESUMMARYINFO_H 1637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#define LLVM_COV_COVERAGESUMMARYINFO_H 1737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 18de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include "llvm/ProfileData/Coverage/CoverageMapping.h" 1937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/Support/raw_ostream.h" 2037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 2137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesnamespace llvm { 2237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 2337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief Provides information about region coverage for a function/file. 2437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstruct RegionCoverageInfo { 2537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief The number of regions that were executed at least once. 2637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines size_t Covered; 2737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 2837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief The number of regions that weren't executed. 2937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines size_t NotCovered; 3037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 3137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief The total number of regions in a function/file. 3237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines size_t NumRegions; 3337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 34ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines RegionCoverageInfo() : Covered(0), NotCovered(0), NumRegions(0) {} 35ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 3637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines RegionCoverageInfo(size_t Covered, size_t NumRegions) 3737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines : Covered(Covered), NotCovered(NumRegions - Covered), 3837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines NumRegions(NumRegions) {} 3937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 40ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) { 41ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Covered += RHS.Covered; 42ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines NotCovered += RHS.NotCovered; 43ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines NumRegions += RHS.NumRegions; 44ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return *this; 45ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 46ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 4737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines bool isFullyCovered() const { return Covered == NumRegions; } 4837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 4937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines double getPercentCovered() const { 50de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar if (NumRegions == 0) 51de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar return 0.0; 5237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines return double(Covered) / double(NumRegions) * 100.0; 5337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines } 5437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}; 5537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 5637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief Provides information about line coverage for a function/file. 5737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstruct LineCoverageInfo { 5837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief The number of lines that were executed at least once. 5937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines size_t Covered; 6037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 6137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief The number of lines that weren't executed. 6237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines size_t NotCovered; 6337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 6437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief The number of lines that aren't code. 6537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines size_t NonCodeLines; 6637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 6737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief The total number of lines in a function/file. 6837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines size_t NumLines; 6937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 70ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines LineCoverageInfo() 71ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines : Covered(0), NotCovered(0), NonCodeLines(0), NumLines(0) {} 72ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 7337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines LineCoverageInfo(size_t Covered, size_t NumNonCodeLines, size_t NumLines) 7437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines : Covered(Covered), NotCovered(NumLines - NumNonCodeLines - Covered), 7537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines NonCodeLines(NumNonCodeLines), NumLines(NumLines) {} 7637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 77ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) { 78ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Covered += RHS.Covered; 79ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines NotCovered += RHS.NotCovered; 80ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines NonCodeLines += RHS.NonCodeLines; 81ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines NumLines += RHS.NumLines; 82ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return *this; 83ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 84ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 8537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines bool isFullyCovered() const { return Covered == (NumLines - NonCodeLines); } 8637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 8737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines double getPercentCovered() const { 88de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar if (NumLines - NonCodeLines == 0) 89de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar return 0.0; 9037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines return double(Covered) / double(NumLines - NonCodeLines) * 100.0; 9137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines } 9237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}; 9337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 9437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief Provides information about function coverage for a file. 9537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstruct FunctionCoverageInfo { 9637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief The number of functions that were executed. 9737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines size_t Executed; 9837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 9937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief The total number of functions in this file. 10037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines size_t NumFunctions; 10137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 102ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines FunctionCoverageInfo() : Executed(0), NumFunctions(0) {} 103ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 10437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines FunctionCoverageInfo(size_t Executed, size_t NumFunctions) 10537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines : Executed(Executed), NumFunctions(NumFunctions) {} 10637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 107ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines void addFunction(bool Covered) { 108ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (Covered) 109ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines ++Executed; 110ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines ++NumFunctions; 111ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 112ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 11337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines bool isFullyCovered() const { return Executed == NumFunctions; } 11437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 11537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines double getPercentCovered() const { 116de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar if (NumFunctions == 0) 117de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar return 0.0; 11837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines return double(Executed) / double(NumFunctions) * 100.0; 11937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines } 12037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}; 12137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 12237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief A summary of function's code coverage. 12337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstruct FunctionCoverageSummary { 12437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines StringRef Name; 12537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines uint64_t ExecutionCount; 12637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines RegionCoverageInfo RegionCoverage; 12737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines LineCoverageInfo LineCoverage; 12837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 129ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines FunctionCoverageSummary(StringRef Name) : Name(Name), ExecutionCount(0) {} 130ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 13137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines FunctionCoverageSummary(StringRef Name, uint64_t ExecutionCount, 13237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines const RegionCoverageInfo &RegionCoverage, 13337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines const LineCoverageInfo &LineCoverage) 13437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines : Name(Name), ExecutionCount(ExecutionCount), 13537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) { 13637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines } 13737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 13837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief Compute the code coverage summary for the given function coverage 13937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// mapping record. 14037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines static FunctionCoverageSummary 14137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines get(const coverage::FunctionRecord &Function); 14237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}; 14337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 14437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief A summary of file's code coverage. 14537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstruct FileCoverageSummary { 14637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines StringRef Name; 14737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines RegionCoverageInfo RegionCoverage; 14837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines LineCoverageInfo LineCoverage; 14937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines FunctionCoverageInfo FunctionCoverage; 150ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 151ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines FileCoverageSummary(StringRef Name) : Name(Name) {} 152ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 153ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines void addFunction(const FunctionCoverageSummary &Function) { 154ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines RegionCoverage += Function.RegionCoverage; 155ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines LineCoverage += Function.LineCoverage; 156ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0); 157ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 15837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}; 15937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 16037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines} // namespace llvm 16137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 16237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#endif // LLVM_COV_COVERAGESUMMARYINFO_H 163