From 39d5e80b446529e76afdc5da6d0428a8a6720f06 Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Tue, 9 Sep 2014 05:32:18 +0000 Subject: [PATCH] llvm-cov: Combine two types that were nearly identical (NFC) llvm-cov had a SourceRange type that was nearly identical to a CountedRegion except that it shaved off a couple of fields. There aren't likely to be enough of these for the minor memory savings to be worth the extra complexity here. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217417 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ProfileData/CoverageMapping.h | 20 +++++++ tools/llvm-cov/CodeCoverage.cpp | 3 +- tools/llvm-cov/SourceCoverageDataManager.cpp | 21 ++----- tools/llvm-cov/SourceCoverageDataManager.h | 45 +-------------- tools/llvm-cov/SourceCoverageView.cpp | 58 ++++++++++---------- 5 files changed, 60 insertions(+), 87 deletions(-) diff --git a/include/llvm/ProfileData/CoverageMapping.h b/include/llvm/ProfileData/CoverageMapping.h index 428b5d1328a..df33e8885db 100644 --- a/include/llvm/ProfileData/CoverageMapping.h +++ b/include/llvm/ProfileData/CoverageMapping.h @@ -171,6 +171,26 @@ struct CounterMappingRegion { return ColumnStart < Other.ColumnStart; return LineStart < Other.LineStart; } + + bool coversSameSource(const CounterMappingRegion &Other) const { + return FileID == Other.FileID && + LineStart == Other.LineStart && + ColumnStart == Other.ColumnStart && + LineEnd == Other.LineEnd && + ColumnEnd == Other.ColumnEnd; + } + + bool contains(const CounterMappingRegion &Other) const { + if (FileID != Other.FileID) + return false; + if (LineStart > Other.LineStart || + (LineStart == Other.LineStart && ColumnStart > Other.ColumnStart)) + return false; + if (LineEnd < Other.LineEnd || + (LineEnd == Other.LineEnd && ColumnEnd < Other.ColumnEnd)) + return false; + return true; + } }; /// \brief Associates a source range with an execution count. diff --git a/tools/llvm-cov/CodeCoverage.cpp b/tools/llvm-cov/CodeCoverage.cpp index 6abeef39bab..549af92f918 100644 --- a/tools/llvm-cov/CodeCoverage.cpp +++ b/tools/llvm-cov/CodeCoverage.cpp @@ -36,8 +36,9 @@ #include "llvm/Support/Path.h" #include "llvm/Support/Signals.h" #include "llvm/Support/PrettyStackTrace.h" -#include #include +#include +#include using namespace llvm; using namespace coverage; diff --git a/tools/llvm-cov/SourceCoverageDataManager.cpp b/tools/llvm-cov/SourceCoverageDataManager.cpp index 4c4d7c038d6..f64162822ab 100644 --- a/tools/llvm-cov/SourceCoverageDataManager.cpp +++ b/tools/llvm-cov/SourceCoverageDataManager.cpp @@ -18,31 +18,22 @@ using namespace llvm; using namespace coverage; void SourceCoverageDataManager::insert(const CountedRegion &CR) { - SourceRange Range(CR.LineStart, CR.ColumnStart, CR.LineEnd, CR.ColumnEnd); - if (CR.Kind == CounterMappingRegion::SkippedRegion) { - SkippedRegions.push_back(Range); - return; - } - Regions.push_back(std::make_pair(Range, CR.ExecutionCount)); + Regions.push_back(CR); + Uniqued = false; } -ArrayRef> -SourceCoverageDataManager::getSourceRegions() { +ArrayRef SourceCoverageDataManager::getSourceRegions() { if (Uniqued || Regions.size() <= 1) return Regions; // Sort. - std::sort(Regions.begin(), Regions.end(), - [](const std::pair &LHS, - const std::pair &RHS) { - return LHS.first < RHS.first; - }); + std::sort(Regions.begin(), Regions.end()); // Merge duplicate source ranges and sum their execution counts. auto Prev = Regions.begin(); for (auto I = Prev + 1, E = Regions.end(); I != E; ++I) { - if (I->first == Prev->first) { - Prev->second += I->second; + if (I->coversSameSource(*Prev)) { + Prev->ExecutionCount += I->ExecutionCount; continue; } ++Prev; diff --git a/tools/llvm-cov/SourceCoverageDataManager.h b/tools/llvm-cov/SourceCoverageDataManager.h index 148ff947ff7..1c87266e0c4 100644 --- a/tools/llvm-cov/SourceCoverageDataManager.h +++ b/tools/llvm-cov/SourceCoverageDataManager.h @@ -16,49 +16,14 @@ #include "FunctionCoverageMapping.h" #include "llvm/ProfileData/CoverageMapping.h" -#include "llvm/ADT/Hashing.h" #include -#include namespace llvm { /// \brief Partions mapping regions by their kind and sums /// the execution counts of the regions that start at the same location. class SourceCoverageDataManager { -public: - struct SourceRange { - unsigned LineStart, ColumnStart, LineEnd, ColumnEnd; - - SourceRange(unsigned LineStart, unsigned ColumnStart, unsigned LineEnd, - unsigned ColumnEnd) - : LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd), - ColumnEnd(ColumnEnd) {} - - bool operator==(const SourceRange &Other) const { - return LineStart == Other.LineStart && ColumnStart == Other.ColumnStart && - LineEnd == Other.LineEnd && ColumnEnd == Other.ColumnEnd; - } - - bool operator<(const SourceRange &Other) const { - if (LineStart == Other.LineStart) - return ColumnStart < Other.ColumnStart; - return LineStart < Other.LineStart; - } - - bool contains(const SourceRange &Other) { - if (LineStart > Other.LineStart || - (LineStart == Other.LineStart && ColumnStart > Other.ColumnStart)) - return false; - if (LineEnd < Other.LineEnd || - (LineEnd == Other.LineEnd && ColumnEnd < Other.ColumnEnd)) - return false; - return true; - } - }; - -protected: - std::vector> Regions; - std::vector SkippedRegions; + std::vector Regions; bool Uniqued; public: @@ -66,12 +31,8 @@ public: void insert(const coverage::CountedRegion &CR); - /// \brief Return the source ranges and execution counts - /// obtained from the non-skipped mapping regions. - ArrayRef> getSourceRegions(); - - /// \brief Return the source ranges obtained from the skipped mapping regions. - ArrayRef getSkippedRegions() const { return SkippedRegions; } + /// \brief Return the source regions in order of first to last occurring. + ArrayRef getSourceRegions(); }; } // namespace llvm diff --git a/tools/llvm-cov/SourceCoverageView.cpp b/tools/llvm-cov/SourceCoverageView.cpp index 7f33b774ebd..94e963aa6e8 100644 --- a/tools/llvm-cov/SourceCoverageView.cpp +++ b/tools/llvm-cov/SourceCoverageView.cpp @@ -320,51 +320,49 @@ void SourceCoverageView::render(raw_ostream &OS, unsigned Offset) { void SourceCoverageView::createLineCoverageInfo(SourceCoverageDataManager &Data) { LineStats.resize(LineCount); - for (const auto &Region : Data.getSourceRegions()) { - auto Value = Region.second; - LineStats[Region.first.LineStart - LineStart].addRegionStartCount(Value); - for (unsigned Line = Region.first.LineStart + 1; - Line <= Region.first.LineEnd; ++Line) - LineStats[Line - LineStart].addRegionCount(Value); - } - - // Reset the line stats for skipped regions. - for (const auto &Region : Data.getSkippedRegions()) { - for (unsigned Line = Region.LineStart; Line <= Region.LineEnd; ++Line) - LineStats[Line - LineStart] = LineCoverageInfo(); + for (const auto &CR : Data.getSourceRegions()) { + if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion) { + // Reset the line stats for skipped regions. + for (unsigned Line = CR.LineStart; Line <= CR.LineEnd; + ++Line) + LineStats[Line - LineStart] = LineCoverageInfo(); + continue; + } + LineStats[CR.LineStart - LineStart].addRegionStartCount(CR.ExecutionCount); + for (unsigned Line = CR.LineStart + 1; Line <= CR.LineEnd; ++Line) + LineStats[Line - LineStart].addRegionCount(CR.ExecutionCount); } } void SourceCoverageView::createHighlightRanges(SourceCoverageDataManager &Data) { - auto Regions = Data.getSourceRegions(); + auto CountedRegions = Data.getSourceRegions(); std::vector AlreadyHighlighted; - AlreadyHighlighted.resize(Regions.size(), false); + AlreadyHighlighted.resize(CountedRegions.size(), false); - for (size_t I = 0, S = Regions.size(); I < S; ++I) { - const auto &Region = Regions[I]; - auto Value = Region.second; - auto SrcRange = Region.first; - if (Value != 0) + for (size_t I = 0, S = CountedRegions.size(); I < S; ++I) { + const auto &CR = CountedRegions[I]; + if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion || + CR.ExecutionCount != 0) continue; if (AlreadyHighlighted[I]) continue; for (size_t J = 0; J < S; ++J) { - if (SrcRange.contains(Regions[J].first)) { + if (CR.contains(CountedRegions[J])) { AlreadyHighlighted[J] = true; } } - if (SrcRange.LineStart == SrcRange.LineEnd) { + if (CR.LineStart == CR.LineEnd) { HighlightRanges.push_back(HighlightRange( - SrcRange.LineStart, SrcRange.ColumnStart, SrcRange.ColumnEnd)); + CR.LineStart, CR.ColumnStart, CR.ColumnEnd)); continue; } HighlightRanges.push_back( - HighlightRange(SrcRange.LineStart, SrcRange.ColumnStart, + HighlightRange(CR.LineStart, CR.ColumnStart, std::numeric_limits::max())); HighlightRanges.push_back( - HighlightRange(SrcRange.LineEnd, 1, SrcRange.ColumnEnd)); - for (unsigned Line = SrcRange.LineStart + 1; Line < SrcRange.LineEnd; + HighlightRange(CR.LineEnd, 1, CR.ColumnEnd)); + for (unsigned Line = CR.LineStart + 1; Line < CR.LineEnd; ++Line) { HighlightRanges.push_back( HighlightRange(Line, 1, std::numeric_limits::max())); @@ -387,10 +385,12 @@ SourceCoverageView::createHighlightRanges(SourceCoverageDataManager &Data) { } void SourceCoverageView::createRegionMarkers(SourceCoverageDataManager &Data) { - for (const auto &Region : Data.getSourceRegions()) { - if (Region.first.LineStart >= LineStart) - Markers.push_back(RegionMarker(Region.first.LineStart, - Region.first.ColumnStart, Region.second)); + for (const auto &CR : Data.getSourceRegions()) { + if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion) + continue; + if (CR.LineStart >= LineStart) + Markers.push_back( + RegionMarker(CR.LineStart, CR.ColumnStart, CR.ExecutionCount)); } if (Options.Debug) { -- 2.34.1