diff options
author | Vedant Kumar <vsk@apple.com> | 2017-10-18 23:58:28 +0000 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2017-10-18 23:58:28 +0000 |
commit | 2ce33a5dffc95368fb7d6faa5e6f4b9b5471c3b9 (patch) | |
tree | aa81d0acdac08bbc5f80bc1245cea4ecca0d32a0 /tools/llvm-cov | |
parent | 34ea0deeb123fc72aabf286a7b9743679d3d58c1 (diff) |
[llvm-cov] Move LineCoverageIterator to libCoverage. NFC.
LineCoverageIterator makes it easy for clients of coverage data to
determine line execution counts for a file or function. The coverage
iteration logic is tricky enough that it really pays not to have
multiple copies of it. Hopefully having just one implementation in LLVM
will make the iteration logic easier to test, reuse, and update.
This commit is NFC but I've added a unit test to go along with it just
because it's easy to do now.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316141 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-cov')
-rw-r--r-- | tools/llvm-cov/CoverageSummaryInfo.cpp | 53 | ||||
-rw-r--r-- | tools/llvm-cov/CoverageSummaryInfo.h | 89 |
2 files changed, 0 insertions, 142 deletions
diff --git a/tools/llvm-cov/CoverageSummaryInfo.cpp b/tools/llvm-cov/CoverageSummaryInfo.cpp index d1fcef4299d..7847a2abf48 100644 --- a/tools/llvm-cov/CoverageSummaryInfo.cpp +++ b/tools/llvm-cov/CoverageSummaryInfo.cpp @@ -17,59 +17,6 @@ using namespace llvm; using namespace coverage; -LineCoverageStats::LineCoverageStats( - ArrayRef<const coverage::CoverageSegment *> LineSegments, - const coverage::CoverageSegment *WrappedSegment, unsigned Line) - : ExecutionCount(0), HasMultipleRegions(false), Mapped(false), Line(Line), - LineSegments(LineSegments), WrappedSegment(WrappedSegment) { - // Find the minimum number of regions which start in this line. - unsigned MinRegionCount = 0; - auto isStartOfRegion = [](const coverage::CoverageSegment *S) { - return !S->IsGapRegion && S->HasCount && S->IsRegionEntry; - }; - for (unsigned I = 0; I < LineSegments.size() && MinRegionCount < 2; ++I) - if (isStartOfRegion(LineSegments[I])) - ++MinRegionCount; - - bool StartOfSkippedRegion = !LineSegments.empty() && - !LineSegments.front()->HasCount && - LineSegments.front()->IsRegionEntry; - - HasMultipleRegions = MinRegionCount > 1; - Mapped = - !StartOfSkippedRegion && - ((WrappedSegment && WrappedSegment->HasCount) || (MinRegionCount > 0)); - - if (!Mapped) - return; - - // Pick the max count from the non-gap, region entry segments. If there - // aren't any, use the wrapped count. - if (!MinRegionCount) { - ExecutionCount = WrappedSegment->Count; - return; - } - for (const auto *LS : LineSegments) - if (isStartOfRegion(LS)) - ExecutionCount = std::max(ExecutionCount, LS->Count); -} - -LineCoverageIterator &LineCoverageIterator::operator++() { - if (Next == CD.end()) { - Stats = LineCoverageStats(); - Ended = true; - return *this; - } - if (Segments.size()) - WrappedSegment = Segments.back(); - Segments.clear(); - while (Next != CD.end() && Next->Line == Line) - Segments.push_back(&*Next++); - Stats = LineCoverageStats(Segments, WrappedSegment, Line); - ++Line; - return *this; -} - FunctionCoverageSummary FunctionCoverageSummary::get(const CoverageMapping &CM, const coverage::FunctionRecord &Function) { diff --git a/tools/llvm-cov/CoverageSummaryInfo.h b/tools/llvm-cov/CoverageSummaryInfo.h index d3f43d19104..8eae0b7fec9 100644 --- a/tools/llvm-cov/CoverageSummaryInfo.h +++ b/tools/llvm-cov/CoverageSummaryInfo.h @@ -15,8 +15,6 @@ #ifndef LLVM_COV_COVERAGESUMMARYINFO_H #define LLVM_COV_COVERAGESUMMARYINFO_H -#include "llvm/ADT/iterator.h" -#include "llvm/ADT/iterator_range.h" #include "llvm/ProfileData/Coverage/CoverageMapping.h" #include "llvm/Support/raw_ostream.h" @@ -138,93 +136,6 @@ public: } }; -/// \brief Coverage statistics for a single line. -class LineCoverageStats { - uint64_t ExecutionCount; - bool HasMultipleRegions; - bool Mapped; - unsigned Line; - ArrayRef<const coverage::CoverageSegment *> LineSegments; - const coverage::CoverageSegment *WrappedSegment; - - friend class LineCoverageIterator; - LineCoverageStats() = default; - -public: - LineCoverageStats(ArrayRef<const coverage::CoverageSegment *> LineSegments, - const coverage::CoverageSegment *WrappedSegment, - unsigned Line); - - uint64_t getExecutionCount() const { return ExecutionCount; } - - bool hasMultipleRegions() const { return HasMultipleRegions; } - - bool isMapped() const { return Mapped; } - - unsigned getLine() const { return Line; } - - ArrayRef<const coverage::CoverageSegment *> getLineSegments() const { - return LineSegments; - } - - const coverage::CoverageSegment *getWrappedSegment() const { - return WrappedSegment; - } -}; - -/// Iterates over LineCoverageStats for each line described by a CoverageData -/// object. -class LineCoverageIterator - : public iterator_facade_base< - LineCoverageIterator, std::forward_iterator_tag, LineCoverageStats> { -public: - LineCoverageIterator(const coverage::CoverageData &CD) - : LineCoverageIterator(CD, CD.begin()->Line) {} - - LineCoverageIterator(const coverage::CoverageData &CD, unsigned Line) - : CD(CD), WrappedSegment(nullptr), Next(CD.begin()), Ended(false), - Line(Line), Segments(), Stats() { - this->operator++(); - } - - LineCoverageIterator &operator=(const LineCoverageIterator &R) = default; - - bool operator==(const LineCoverageIterator &R) const { - return &CD == &R.CD && Next == R.Next && Ended == R.Ended; - } - - const LineCoverageStats &operator*() const { return Stats; } - - LineCoverageStats &operator*() { return Stats; } - - LineCoverageIterator &operator++(); - - LineCoverageIterator getEnd() const { - auto EndIt = *this; - EndIt.Next = CD.end(); - EndIt.Ended = true; - return EndIt; - } - -private: - const coverage::CoverageData &CD; - const coverage::CoverageSegment *WrappedSegment; - std::vector<coverage::CoverageSegment>::const_iterator Next; - bool Ended; - unsigned Line; - SmallVector<const coverage::CoverageSegment *, 4> Segments; - LineCoverageStats Stats; -}; - -/// Get a range of LineCoverageStats for each line described by a CoverageData -/// object. -static inline iterator_range<LineCoverageIterator> -getLineCoverageStats(const coverage::CoverageData &CD) { - auto Begin = LineCoverageIterator(CD); - auto End = Begin.getEnd(); - return make_range(Begin, End); -} - /// \brief A summary of function's code coverage. struct FunctionCoverageSummary { std::string Name; |