LineIterator: Provide a variant that keeps blank lines
[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 CountedRegion &CR) {
21   Regions.push_back(CR);
22   Uniqued = false;
23 }
24
25 ArrayRef<CountedRegion> SourceCoverageDataManager::getSourceRegions() {
26   if (Uniqued || Regions.size() <= 1)
27     return Regions;
28
29   // Sort the regions given that they're all in the same file at this point.
30   std::sort(Regions.begin(), Regions.end(),
31             [](const CountedRegion &LHS, const CountedRegion &RHS) {
32     return LHS.startLoc() < RHS.startLoc();
33   });
34
35   // Merge duplicate source ranges and sum their execution counts.
36   auto Prev = Regions.begin();
37   for (auto I = Prev + 1, E = Regions.end(); I != E; ++I) {
38     if (I->startLoc() == Prev->startLoc() && I->endLoc() == Prev->endLoc()) {
39       Prev->ExecutionCount += I->ExecutionCount;
40       continue;
41     }
42     ++Prev;
43     *Prev = *I;
44   }
45   ++Prev;
46   Regions.erase(Prev, Regions.end());
47
48   Uniqued = true;
49   return Regions;
50 }