Home | History | Annotate | Download | only in llvm-cov
      1 //===- CoverageSummaryInfo.h - Coverage summary for function/file ---------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // These structures are used to represent code coverage metrics
     11 // for functions/files.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_COV_COVERAGESUMMARYINFO_H
     16 #define LLVM_COV_COVERAGESUMMARYINFO_H
     17 
     18 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
     19 #include "llvm/Support/raw_ostream.h"
     20 
     21 namespace llvm {
     22 
     23 /// \brief Provides information about region coverage for a function/file.
     24 struct RegionCoverageInfo {
     25   /// \brief The number of regions that were executed at least once.
     26   size_t Covered;
     27 
     28   /// \brief The number of regions that weren't executed.
     29   size_t NotCovered;
     30 
     31   /// \brief The total number of regions in a function/file.
     32   size_t NumRegions;
     33 
     34   RegionCoverageInfo() : Covered(0), NotCovered(0), NumRegions(0) {}
     35 
     36   RegionCoverageInfo(size_t Covered, size_t NumRegions)
     37       : Covered(Covered), NotCovered(NumRegions - Covered),
     38         NumRegions(NumRegions) {}
     39 
     40   RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
     41     Covered += RHS.Covered;
     42     NotCovered += RHS.NotCovered;
     43     NumRegions += RHS.NumRegions;
     44     return *this;
     45   }
     46 
     47   bool isFullyCovered() const { return Covered == NumRegions; }
     48 
     49   double getPercentCovered() const {
     50     if (NumRegions == 0)
     51       return 0.0;
     52     return double(Covered) / double(NumRegions) * 100.0;
     53   }
     54 };
     55 
     56 /// \brief Provides information about line coverage for a function/file.
     57 struct LineCoverageInfo {
     58   /// \brief The number of lines that were executed at least once.
     59   size_t Covered;
     60 
     61   /// \brief The number of lines that weren't executed.
     62   size_t NotCovered;
     63 
     64   /// \brief The number of lines that aren't code.
     65   size_t NonCodeLines;
     66 
     67   /// \brief The total number of lines in a function/file.
     68   size_t NumLines;
     69 
     70   LineCoverageInfo()
     71       : Covered(0), NotCovered(0), NonCodeLines(0), NumLines(0) {}
     72 
     73   LineCoverageInfo(size_t Covered, size_t NumNonCodeLines, size_t NumLines)
     74       : Covered(Covered), NotCovered(NumLines - NumNonCodeLines - Covered),
     75         NonCodeLines(NumNonCodeLines), NumLines(NumLines) {}
     76 
     77   LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) {
     78     Covered += RHS.Covered;
     79     NotCovered += RHS.NotCovered;
     80     NonCodeLines += RHS.NonCodeLines;
     81     NumLines += RHS.NumLines;
     82     return *this;
     83   }
     84 
     85   bool isFullyCovered() const { return Covered == (NumLines - NonCodeLines); }
     86 
     87   double getPercentCovered() const {
     88     if (NumLines - NonCodeLines == 0)
     89       return 0.0;
     90     return double(Covered) / double(NumLines - NonCodeLines) * 100.0;
     91   }
     92 };
     93 
     94 /// \brief Provides information about function coverage for a file.
     95 struct FunctionCoverageInfo {
     96   /// \brief The number of functions that were executed.
     97   size_t Executed;
     98 
     99   /// \brief The total number of functions in this file.
    100   size_t NumFunctions;
    101 
    102   FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
    103 
    104   FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
    105       : Executed(Executed), NumFunctions(NumFunctions) {}
    106 
    107   void addFunction(bool Covered) {
    108     if (Covered)
    109       ++Executed;
    110     ++NumFunctions;
    111   }
    112 
    113   bool isFullyCovered() const { return Executed == NumFunctions; }
    114 
    115   double getPercentCovered() const {
    116     if (NumFunctions == 0)
    117       return 0.0;
    118     return double(Executed) / double(NumFunctions) * 100.0;
    119   }
    120 };
    121 
    122 /// \brief A summary of function's code coverage.
    123 struct FunctionCoverageSummary {
    124   StringRef Name;
    125   uint64_t ExecutionCount;
    126   RegionCoverageInfo RegionCoverage;
    127   LineCoverageInfo LineCoverage;
    128 
    129   FunctionCoverageSummary(StringRef Name) : Name(Name), ExecutionCount(0) {}
    130 
    131   FunctionCoverageSummary(StringRef Name, uint64_t ExecutionCount,
    132                           const RegionCoverageInfo &RegionCoverage,
    133                           const LineCoverageInfo &LineCoverage)
    134       : Name(Name), ExecutionCount(ExecutionCount),
    135         RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) {
    136   }
    137 
    138   /// \brief Compute the code coverage summary for the given function coverage
    139   /// mapping record.
    140   static FunctionCoverageSummary
    141   get(const coverage::FunctionRecord &Function);
    142 };
    143 
    144 /// \brief A summary of file's code coverage.
    145 struct FileCoverageSummary {
    146   StringRef Name;
    147   RegionCoverageInfo RegionCoverage;
    148   LineCoverageInfo LineCoverage;
    149   FunctionCoverageInfo FunctionCoverage;
    150 
    151   FileCoverageSummary(StringRef Name) : Name(Name) {}
    152 
    153   void addFunction(const FunctionCoverageSummary &Function) {
    154     RegionCoverage += Function.RegionCoverage;
    155     LineCoverage += Function.LineCoverage;
    156     FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
    157   }
    158 };
    159 
    160 } // namespace llvm
    161 
    162 #endif // LLVM_COV_COVERAGESUMMARYINFO_H
    163