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
This commit is contained in:
Justin Bogner 2014-09-09 05:32:18 +00:00
parent 74a60259a5
commit 39d5e80b44
5 changed files with 60 additions and 87 deletions

View File

@ -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.

View File

@ -36,8 +36,9 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/PrettyStackTrace.h"
#include <system_error>
#include <functional>
#include <system_error>
#include <unordered_map>
using namespace llvm;
using namespace coverage;

View File

@ -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<std::pair<SourceCoverageDataManager::SourceRange, uint64_t>>
SourceCoverageDataManager::getSourceRegions() {
ArrayRef<CountedRegion> SourceCoverageDataManager::getSourceRegions() {
if (Uniqued || Regions.size() <= 1)
return Regions;
// Sort.
std::sort(Regions.begin(), Regions.end(),
[](const std::pair<SourceRange, uint64_t> &LHS,
const std::pair<SourceRange, uint64_t> &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;

View File

@ -16,49 +16,14 @@
#include "FunctionCoverageMapping.h"
#include "llvm/ProfileData/CoverageMapping.h"
#include "llvm/ADT/Hashing.h"
#include <vector>
#include <unordered_map>
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<std::pair<SourceRange, uint64_t>> Regions;
std::vector<SourceRange> SkippedRegions;
std::vector<coverage::CountedRegion> 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<std::pair<SourceRange, uint64_t>> getSourceRegions();
/// \brief Return the source ranges obtained from the skipped mapping regions.
ArrayRef<SourceRange> getSkippedRegions() const { return SkippedRegions; }
/// \brief Return the source regions in order of first to last occurring.
ArrayRef<coverage::CountedRegion> getSourceRegions();
};
} // namespace llvm

View File

@ -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<bool> 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<unsigned>::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<unsigned>::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) {