llvm-cov: add code coverage tool that's based on coverage mapping format and clang...
[oota-llvm.git] / tools / llvm-cov / SourceCoverageDataManager.cpp
1 //===- SourceCoverageDataManager.cpp - Manager for source file coverage
2 // data-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This class separates and merges mapping regions for a specific source file.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "SourceCoverageDataManager.h"
16
17 using namespace llvm;
18 using namespace coverage;
19
20 void SourceCoverageDataManager::insert(const MappingRegion &Region) {
21   SourceRange Range(Region.LineStart, Region.ColumnStart, Region.LineEnd,
22                     Region.ColumnEnd);
23   if (Region.Kind == CounterMappingRegion::SkippedRegion) {
24     SkippedRegions.push_back(Range);
25     return;
26   }
27   Regions.push_back(std::make_pair(Range, Region.ExecutionCount));
28 }
29
30 ArrayRef<std::pair<SourceCoverageDataManager::SourceRange, uint64_t>>
31 SourceCoverageDataManager::getSourceRegions() {
32   if (Uniqued || Regions.size() <= 1)
33     return Regions;
34
35   // Sort.
36   std::sort(Regions.begin(), Regions.end(),
37             [](const std::pair<SourceRange, uint64_t> &LHS,
38                const std::pair<SourceRange, uint64_t> &RHS) {
39     return LHS.first < RHS.first;
40   });
41
42   // Merge duplicate source ranges and sum their execution counts.
43   auto Prev = Regions.begin();
44   for (auto I = Prev + 1, E = Regions.end(); I != E; ++I) {
45     if (I->first == Prev->first) {
46       Prev->second += I->second;
47       continue;
48     }
49     ++Prev;
50     *Prev = *I;
51   }
52   ++Prev;
53   Regions.erase(Prev, Regions.end());
54
55   Uniqued = true;
56   return Regions;
57 }