summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-cov/SourceCoverageDataManager.cpp
blob: 6f7c8e5d9855ab03b301bc16029a8dedf16bb442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//===- SourceCoverageDataManager.cpp - Manager for source file coverage
// data-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This class separates and merges mapping regions for a specific source file.
//
//===----------------------------------------------------------------------===//

#include "SourceCoverageDataManager.h"

using namespace llvm;
using namespace coverage;

void SourceCoverageDataManager::insert(const CountedRegion &CR) {
  Regions.push_back(CR);
  Uniqued = false;
}

ArrayRef<CountedRegion> SourceCoverageDataManager::getSourceRegions() {
  if (Uniqued || Regions.size() <= 1)
    return Regions;

  // Sort the regions given that they're all in the same file at this point.
  std::sort(Regions.begin(), Regions.end(),
            [](const CountedRegion &LHS, const CountedRegion &RHS) {
    return LHS.startLoc() < RHS.startLoc();
  });

  // 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->startLoc() == Prev->startLoc() && I->endLoc() == Prev->endLoc()) {
      Prev->ExecutionCount += I->ExecutionCount;
      continue;
    }
    ++Prev;
    *Prev = *I;
  }
  ++Prev;
  Regions.erase(Prev, Regions.end());

  Uniqued = true;
  return Regions;
}
OpenPOWER on IntegriCloud