xref: /minix3/external/bsd/llvm/dist/llvm/lib/ProfileData/CoverageMapping.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //=-- CoverageMapping.cpp - Code coverage mapping support ---------*- C++ -*-=//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This file contains support for clang's and llvm's instrumentation based
11*0a6a1f1dSLionel Sambuc // code coverage.
12*0a6a1f1dSLionel Sambuc //
13*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #include "llvm/ProfileData/CoverageMapping.h"
16*0a6a1f1dSLionel Sambuc #include "llvm/ADT/DenseMap.h"
17*0a6a1f1dSLionel Sambuc #include "llvm/ADT/Optional.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/ADT/SmallSet.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/ProfileData/CoverageMappingReader.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/ProfileData/InstrProfReader.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/Support/Debug.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
23*0a6a1f1dSLionel Sambuc 
24*0a6a1f1dSLionel Sambuc using namespace llvm;
25*0a6a1f1dSLionel Sambuc using namespace coverage;
26*0a6a1f1dSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "coverage-mapping"
28*0a6a1f1dSLionel Sambuc 
get(const CounterExpression & E)29*0a6a1f1dSLionel Sambuc Counter CounterExpressionBuilder::get(const CounterExpression &E) {
30*0a6a1f1dSLionel Sambuc   auto It = ExpressionIndices.find(E);
31*0a6a1f1dSLionel Sambuc   if (It != ExpressionIndices.end())
32*0a6a1f1dSLionel Sambuc     return Counter::getExpression(It->second);
33*0a6a1f1dSLionel Sambuc   unsigned I = Expressions.size();
34*0a6a1f1dSLionel Sambuc   Expressions.push_back(E);
35*0a6a1f1dSLionel Sambuc   ExpressionIndices[E] = I;
36*0a6a1f1dSLionel Sambuc   return Counter::getExpression(I);
37*0a6a1f1dSLionel Sambuc }
38*0a6a1f1dSLionel Sambuc 
extractTerms(Counter C,int Sign,SmallVectorImpl<std::pair<unsigned,int>> & Terms)39*0a6a1f1dSLionel Sambuc void CounterExpressionBuilder::extractTerms(
40*0a6a1f1dSLionel Sambuc     Counter C, int Sign, SmallVectorImpl<std::pair<unsigned, int>> &Terms) {
41*0a6a1f1dSLionel Sambuc   switch (C.getKind()) {
42*0a6a1f1dSLionel Sambuc   case Counter::Zero:
43*0a6a1f1dSLionel Sambuc     break;
44*0a6a1f1dSLionel Sambuc   case Counter::CounterValueReference:
45*0a6a1f1dSLionel Sambuc     Terms.push_back(std::make_pair(C.getCounterID(), Sign));
46*0a6a1f1dSLionel Sambuc     break;
47*0a6a1f1dSLionel Sambuc   case Counter::Expression:
48*0a6a1f1dSLionel Sambuc     const auto &E = Expressions[C.getExpressionID()];
49*0a6a1f1dSLionel Sambuc     extractTerms(E.LHS, Sign, Terms);
50*0a6a1f1dSLionel Sambuc     extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign,
51*0a6a1f1dSLionel Sambuc                  Terms);
52*0a6a1f1dSLionel Sambuc     break;
53*0a6a1f1dSLionel Sambuc   }
54*0a6a1f1dSLionel Sambuc }
55*0a6a1f1dSLionel Sambuc 
simplify(Counter ExpressionTree)56*0a6a1f1dSLionel Sambuc Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
57*0a6a1f1dSLionel Sambuc   // Gather constant terms.
58*0a6a1f1dSLionel Sambuc   llvm::SmallVector<std::pair<unsigned, int>, 32> Terms;
59*0a6a1f1dSLionel Sambuc   extractTerms(ExpressionTree, +1, Terms);
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc   // If there are no terms, this is just a zero. The algorithm below assumes at
62*0a6a1f1dSLionel Sambuc   // least one term.
63*0a6a1f1dSLionel Sambuc   if (Terms.size() == 0)
64*0a6a1f1dSLionel Sambuc     return Counter::getZero();
65*0a6a1f1dSLionel Sambuc 
66*0a6a1f1dSLionel Sambuc   // Group the terms by counter ID.
67*0a6a1f1dSLionel Sambuc   std::sort(Terms.begin(), Terms.end(),
68*0a6a1f1dSLionel Sambuc             [](const std::pair<unsigned, int> &LHS,
69*0a6a1f1dSLionel Sambuc                const std::pair<unsigned, int> &RHS) {
70*0a6a1f1dSLionel Sambuc     return LHS.first < RHS.first;
71*0a6a1f1dSLionel Sambuc   });
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc   // Combine terms by counter ID to eliminate counters that sum to zero.
74*0a6a1f1dSLionel Sambuc   auto Prev = Terms.begin();
75*0a6a1f1dSLionel Sambuc   for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
76*0a6a1f1dSLionel Sambuc     if (I->first == Prev->first) {
77*0a6a1f1dSLionel Sambuc       Prev->second += I->second;
78*0a6a1f1dSLionel Sambuc       continue;
79*0a6a1f1dSLionel Sambuc     }
80*0a6a1f1dSLionel Sambuc     ++Prev;
81*0a6a1f1dSLionel Sambuc     *Prev = *I;
82*0a6a1f1dSLionel Sambuc   }
83*0a6a1f1dSLionel Sambuc   Terms.erase(++Prev, Terms.end());
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc   Counter C;
86*0a6a1f1dSLionel Sambuc   // Create additions. We do this before subtractions to avoid constructs like
87*0a6a1f1dSLionel Sambuc   // ((0 - X) + Y), as opposed to (Y - X).
88*0a6a1f1dSLionel Sambuc   for (auto Term : Terms) {
89*0a6a1f1dSLionel Sambuc     if (Term.second <= 0)
90*0a6a1f1dSLionel Sambuc       continue;
91*0a6a1f1dSLionel Sambuc     for (int I = 0; I < Term.second; ++I)
92*0a6a1f1dSLionel Sambuc       if (C.isZero())
93*0a6a1f1dSLionel Sambuc         C = Counter::getCounter(Term.first);
94*0a6a1f1dSLionel Sambuc       else
95*0a6a1f1dSLionel Sambuc         C = get(CounterExpression(CounterExpression::Add, C,
96*0a6a1f1dSLionel Sambuc                                   Counter::getCounter(Term.first)));
97*0a6a1f1dSLionel Sambuc   }
98*0a6a1f1dSLionel Sambuc 
99*0a6a1f1dSLionel Sambuc   // Create subtractions.
100*0a6a1f1dSLionel Sambuc   for (auto Term : Terms) {
101*0a6a1f1dSLionel Sambuc     if (Term.second >= 0)
102*0a6a1f1dSLionel Sambuc       continue;
103*0a6a1f1dSLionel Sambuc     for (int I = 0; I < -Term.second; ++I)
104*0a6a1f1dSLionel Sambuc       C = get(CounterExpression(CounterExpression::Subtract, C,
105*0a6a1f1dSLionel Sambuc                                 Counter::getCounter(Term.first)));
106*0a6a1f1dSLionel Sambuc   }
107*0a6a1f1dSLionel Sambuc   return C;
108*0a6a1f1dSLionel Sambuc }
109*0a6a1f1dSLionel Sambuc 
add(Counter LHS,Counter RHS)110*0a6a1f1dSLionel Sambuc Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) {
111*0a6a1f1dSLionel Sambuc   return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS)));
112*0a6a1f1dSLionel Sambuc }
113*0a6a1f1dSLionel Sambuc 
subtract(Counter LHS,Counter RHS)114*0a6a1f1dSLionel Sambuc Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) {
115*0a6a1f1dSLionel Sambuc   return simplify(
116*0a6a1f1dSLionel Sambuc       get(CounterExpression(CounterExpression::Subtract, LHS, RHS)));
117*0a6a1f1dSLionel Sambuc }
118*0a6a1f1dSLionel Sambuc 
dump(const Counter & C,llvm::raw_ostream & OS) const119*0a6a1f1dSLionel Sambuc void CounterMappingContext::dump(const Counter &C,
120*0a6a1f1dSLionel Sambuc                                  llvm::raw_ostream &OS) const {
121*0a6a1f1dSLionel Sambuc   switch (C.getKind()) {
122*0a6a1f1dSLionel Sambuc   case Counter::Zero:
123*0a6a1f1dSLionel Sambuc     OS << '0';
124*0a6a1f1dSLionel Sambuc     return;
125*0a6a1f1dSLionel Sambuc   case Counter::CounterValueReference:
126*0a6a1f1dSLionel Sambuc     OS << '#' << C.getCounterID();
127*0a6a1f1dSLionel Sambuc     break;
128*0a6a1f1dSLionel Sambuc   case Counter::Expression: {
129*0a6a1f1dSLionel Sambuc     if (C.getExpressionID() >= Expressions.size())
130*0a6a1f1dSLionel Sambuc       return;
131*0a6a1f1dSLionel Sambuc     const auto &E = Expressions[C.getExpressionID()];
132*0a6a1f1dSLionel Sambuc     OS << '(';
133*0a6a1f1dSLionel Sambuc     dump(E.LHS, OS);
134*0a6a1f1dSLionel Sambuc     OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
135*0a6a1f1dSLionel Sambuc     dump(E.RHS, OS);
136*0a6a1f1dSLionel Sambuc     OS << ')';
137*0a6a1f1dSLionel Sambuc     break;
138*0a6a1f1dSLionel Sambuc   }
139*0a6a1f1dSLionel Sambuc   }
140*0a6a1f1dSLionel Sambuc   if (CounterValues.empty())
141*0a6a1f1dSLionel Sambuc     return;
142*0a6a1f1dSLionel Sambuc   ErrorOr<int64_t> Value = evaluate(C);
143*0a6a1f1dSLionel Sambuc   if (!Value)
144*0a6a1f1dSLionel Sambuc     return;
145*0a6a1f1dSLionel Sambuc   OS << '[' << *Value << ']';
146*0a6a1f1dSLionel Sambuc }
147*0a6a1f1dSLionel Sambuc 
evaluate(const Counter & C) const148*0a6a1f1dSLionel Sambuc ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
149*0a6a1f1dSLionel Sambuc   switch (C.getKind()) {
150*0a6a1f1dSLionel Sambuc   case Counter::Zero:
151*0a6a1f1dSLionel Sambuc     return 0;
152*0a6a1f1dSLionel Sambuc   case Counter::CounterValueReference:
153*0a6a1f1dSLionel Sambuc     if (C.getCounterID() >= CounterValues.size())
154*0a6a1f1dSLionel Sambuc       return std::make_error_code(std::errc::argument_out_of_domain);
155*0a6a1f1dSLionel Sambuc     return CounterValues[C.getCounterID()];
156*0a6a1f1dSLionel Sambuc   case Counter::Expression: {
157*0a6a1f1dSLionel Sambuc     if (C.getExpressionID() >= Expressions.size())
158*0a6a1f1dSLionel Sambuc       return std::make_error_code(std::errc::argument_out_of_domain);
159*0a6a1f1dSLionel Sambuc     const auto &E = Expressions[C.getExpressionID()];
160*0a6a1f1dSLionel Sambuc     ErrorOr<int64_t> LHS = evaluate(E.LHS);
161*0a6a1f1dSLionel Sambuc     if (!LHS)
162*0a6a1f1dSLionel Sambuc       return LHS;
163*0a6a1f1dSLionel Sambuc     ErrorOr<int64_t> RHS = evaluate(E.RHS);
164*0a6a1f1dSLionel Sambuc     if (!RHS)
165*0a6a1f1dSLionel Sambuc       return RHS;
166*0a6a1f1dSLionel Sambuc     return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
167*0a6a1f1dSLionel Sambuc   }
168*0a6a1f1dSLionel Sambuc   }
169*0a6a1f1dSLionel Sambuc   llvm_unreachable("Unhandled CounterKind");
170*0a6a1f1dSLionel Sambuc }
171*0a6a1f1dSLionel Sambuc 
skipOtherFiles()172*0a6a1f1dSLionel Sambuc void FunctionRecordIterator::skipOtherFiles() {
173*0a6a1f1dSLionel Sambuc   while (Current != Records.end() && !Filename.empty() &&
174*0a6a1f1dSLionel Sambuc          Filename != Current->Filenames[0])
175*0a6a1f1dSLionel Sambuc     ++Current;
176*0a6a1f1dSLionel Sambuc   if (Current == Records.end())
177*0a6a1f1dSLionel Sambuc     *this = FunctionRecordIterator();
178*0a6a1f1dSLionel Sambuc }
179*0a6a1f1dSLionel Sambuc 
180*0a6a1f1dSLionel Sambuc ErrorOr<std::unique_ptr<CoverageMapping>>
load(ObjectFileCoverageMappingReader & CoverageReader,IndexedInstrProfReader & ProfileReader)181*0a6a1f1dSLionel Sambuc CoverageMapping::load(ObjectFileCoverageMappingReader &CoverageReader,
182*0a6a1f1dSLionel Sambuc                       IndexedInstrProfReader &ProfileReader) {
183*0a6a1f1dSLionel Sambuc   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
184*0a6a1f1dSLionel Sambuc 
185*0a6a1f1dSLionel Sambuc   std::vector<uint64_t> Counts;
186*0a6a1f1dSLionel Sambuc   for (const auto &Record : CoverageReader) {
187*0a6a1f1dSLionel Sambuc     Counts.clear();
188*0a6a1f1dSLionel Sambuc     if (std::error_code EC = ProfileReader.getFunctionCounts(
189*0a6a1f1dSLionel Sambuc             Record.FunctionName, Record.FunctionHash, Counts)) {
190*0a6a1f1dSLionel Sambuc       if (EC != instrprof_error::hash_mismatch &&
191*0a6a1f1dSLionel Sambuc           EC != instrprof_error::unknown_function)
192*0a6a1f1dSLionel Sambuc         return EC;
193*0a6a1f1dSLionel Sambuc       Coverage->MismatchedFunctionCount++;
194*0a6a1f1dSLionel Sambuc       continue;
195*0a6a1f1dSLionel Sambuc     }
196*0a6a1f1dSLionel Sambuc 
197*0a6a1f1dSLionel Sambuc     assert(Counts.size() != 0 && "Function's counts are empty");
198*0a6a1f1dSLionel Sambuc     FunctionRecord Function(Record.FunctionName, Record.Filenames,
199*0a6a1f1dSLionel Sambuc                             Counts.front());
200*0a6a1f1dSLionel Sambuc     CounterMappingContext Ctx(Record.Expressions, Counts);
201*0a6a1f1dSLionel Sambuc     for (const auto &Region : Record.MappingRegions) {
202*0a6a1f1dSLionel Sambuc       ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
203*0a6a1f1dSLionel Sambuc       if (!ExecutionCount)
204*0a6a1f1dSLionel Sambuc         break;
205*0a6a1f1dSLionel Sambuc       Function.CountedRegions.push_back(CountedRegion(Region, *ExecutionCount));
206*0a6a1f1dSLionel Sambuc     }
207*0a6a1f1dSLionel Sambuc     if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
208*0a6a1f1dSLionel Sambuc       Coverage->MismatchedFunctionCount++;
209*0a6a1f1dSLionel Sambuc       continue;
210*0a6a1f1dSLionel Sambuc     }
211*0a6a1f1dSLionel Sambuc 
212*0a6a1f1dSLionel Sambuc     Coverage->Functions.push_back(std::move(Function));
213*0a6a1f1dSLionel Sambuc   }
214*0a6a1f1dSLionel Sambuc 
215*0a6a1f1dSLionel Sambuc   return std::move(Coverage);
216*0a6a1f1dSLionel Sambuc }
217*0a6a1f1dSLionel Sambuc 
218*0a6a1f1dSLionel Sambuc ErrorOr<std::unique_ptr<CoverageMapping>>
load(StringRef ObjectFilename,StringRef ProfileFilename)219*0a6a1f1dSLionel Sambuc CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename) {
220*0a6a1f1dSLionel Sambuc   auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
221*0a6a1f1dSLionel Sambuc   if (auto EC = CounterMappingBuff.getError())
222*0a6a1f1dSLionel Sambuc     return EC;
223*0a6a1f1dSLionel Sambuc   ObjectFileCoverageMappingReader CoverageReader(CounterMappingBuff.get());
224*0a6a1f1dSLionel Sambuc   if (auto EC = CoverageReader.readHeader())
225*0a6a1f1dSLionel Sambuc     return EC;
226*0a6a1f1dSLionel Sambuc   std::unique_ptr<IndexedInstrProfReader> ProfileReader;
227*0a6a1f1dSLionel Sambuc   if (auto EC = IndexedInstrProfReader::create(ProfileFilename, ProfileReader))
228*0a6a1f1dSLionel Sambuc     return EC;
229*0a6a1f1dSLionel Sambuc   return load(CoverageReader, *ProfileReader);
230*0a6a1f1dSLionel Sambuc }
231*0a6a1f1dSLionel Sambuc 
232*0a6a1f1dSLionel Sambuc namespace {
233*0a6a1f1dSLionel Sambuc /// \brief Distributes functions into instantiation sets.
234*0a6a1f1dSLionel Sambuc ///
235*0a6a1f1dSLionel Sambuc /// An instantiation set is a collection of functions that have the same source
236*0a6a1f1dSLionel Sambuc /// code, ie, template functions specializations.
237*0a6a1f1dSLionel Sambuc class FunctionInstantiationSetCollector {
238*0a6a1f1dSLionel Sambuc   typedef DenseMap<std::pair<unsigned, unsigned>,
239*0a6a1f1dSLionel Sambuc                    std::vector<const FunctionRecord *>> MapT;
240*0a6a1f1dSLionel Sambuc   MapT InstantiatedFunctions;
241*0a6a1f1dSLionel Sambuc 
242*0a6a1f1dSLionel Sambuc public:
insert(const FunctionRecord & Function,unsigned FileID)243*0a6a1f1dSLionel Sambuc   void insert(const FunctionRecord &Function, unsigned FileID) {
244*0a6a1f1dSLionel Sambuc     auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
245*0a6a1f1dSLionel Sambuc     while (I != E && I->FileID != FileID)
246*0a6a1f1dSLionel Sambuc       ++I;
247*0a6a1f1dSLionel Sambuc     assert(I != E && "function does not cover the given file");
248*0a6a1f1dSLionel Sambuc     auto &Functions = InstantiatedFunctions[I->startLoc()];
249*0a6a1f1dSLionel Sambuc     Functions.push_back(&Function);
250*0a6a1f1dSLionel Sambuc   }
251*0a6a1f1dSLionel Sambuc 
begin()252*0a6a1f1dSLionel Sambuc   MapT::iterator begin() { return InstantiatedFunctions.begin(); }
253*0a6a1f1dSLionel Sambuc 
end()254*0a6a1f1dSLionel Sambuc   MapT::iterator end() { return InstantiatedFunctions.end(); }
255*0a6a1f1dSLionel Sambuc };
256*0a6a1f1dSLionel Sambuc 
257*0a6a1f1dSLionel Sambuc class SegmentBuilder {
258*0a6a1f1dSLionel Sambuc   std::vector<CoverageSegment> Segments;
259*0a6a1f1dSLionel Sambuc   SmallVector<const CountedRegion *, 8> ActiveRegions;
260*0a6a1f1dSLionel Sambuc 
261*0a6a1f1dSLionel Sambuc   /// Start a segment with no count specified.
startSegment(unsigned Line,unsigned Col)262*0a6a1f1dSLionel Sambuc   void startSegment(unsigned Line, unsigned Col) {
263*0a6a1f1dSLionel Sambuc     DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n");
264*0a6a1f1dSLionel Sambuc     Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false);
265*0a6a1f1dSLionel Sambuc   }
266*0a6a1f1dSLionel Sambuc 
267*0a6a1f1dSLionel Sambuc   /// Start a segment with the given Region's count.
startSegment(unsigned Line,unsigned Col,bool IsRegionEntry,const CountedRegion & Region)268*0a6a1f1dSLionel Sambuc   void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry,
269*0a6a1f1dSLionel Sambuc                     const CountedRegion &Region) {
270*0a6a1f1dSLionel Sambuc     if (Segments.empty())
271*0a6a1f1dSLionel Sambuc       Segments.emplace_back(Line, Col, IsRegionEntry);
272*0a6a1f1dSLionel Sambuc     CoverageSegment S = Segments.back();
273*0a6a1f1dSLionel Sambuc     // Avoid creating empty regions.
274*0a6a1f1dSLionel Sambuc     if (S.Line != Line || S.Col != Col) {
275*0a6a1f1dSLionel Sambuc       Segments.emplace_back(Line, Col, IsRegionEntry);
276*0a6a1f1dSLionel Sambuc       S = Segments.back();
277*0a6a1f1dSLionel Sambuc     }
278*0a6a1f1dSLionel Sambuc     DEBUG(dbgs() << "Segment at " << Line << ":" << Col);
279*0a6a1f1dSLionel Sambuc     // Set this region's count.
280*0a6a1f1dSLionel Sambuc     if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) {
281*0a6a1f1dSLionel Sambuc       DEBUG(dbgs() << " with count " << Region.ExecutionCount);
282*0a6a1f1dSLionel Sambuc       Segments.back().setCount(Region.ExecutionCount);
283*0a6a1f1dSLionel Sambuc     }
284*0a6a1f1dSLionel Sambuc     DEBUG(dbgs() << "\n");
285*0a6a1f1dSLionel Sambuc   }
286*0a6a1f1dSLionel Sambuc 
287*0a6a1f1dSLionel Sambuc   /// Start a segment for the given region.
startSegment(const CountedRegion & Region)288*0a6a1f1dSLionel Sambuc   void startSegment(const CountedRegion &Region) {
289*0a6a1f1dSLionel Sambuc     startSegment(Region.LineStart, Region.ColumnStart, true, Region);
290*0a6a1f1dSLionel Sambuc   }
291*0a6a1f1dSLionel Sambuc 
292*0a6a1f1dSLionel Sambuc   /// Pop the top region off of the active stack, starting a new segment with
293*0a6a1f1dSLionel Sambuc   /// the containing Region's count.
popRegion()294*0a6a1f1dSLionel Sambuc   void popRegion() {
295*0a6a1f1dSLionel Sambuc     const CountedRegion *Active = ActiveRegions.back();
296*0a6a1f1dSLionel Sambuc     unsigned Line = Active->LineEnd, Col = Active->ColumnEnd;
297*0a6a1f1dSLionel Sambuc     ActiveRegions.pop_back();
298*0a6a1f1dSLionel Sambuc     if (ActiveRegions.empty())
299*0a6a1f1dSLionel Sambuc       startSegment(Line, Col);
300*0a6a1f1dSLionel Sambuc     else
301*0a6a1f1dSLionel Sambuc       startSegment(Line, Col, false, *ActiveRegions.back());
302*0a6a1f1dSLionel Sambuc   }
303*0a6a1f1dSLionel Sambuc 
304*0a6a1f1dSLionel Sambuc public:
305*0a6a1f1dSLionel Sambuc   /// Build a list of CoverageSegments from a sorted list of Regions.
buildSegments(ArrayRef<CountedRegion> Regions)306*0a6a1f1dSLionel Sambuc   std::vector<CoverageSegment> buildSegments(ArrayRef<CountedRegion> Regions) {
307*0a6a1f1dSLionel Sambuc     for (const auto &Region : Regions) {
308*0a6a1f1dSLionel Sambuc       // Pop any regions that end before this one starts.
309*0a6a1f1dSLionel Sambuc       while (!ActiveRegions.empty() &&
310*0a6a1f1dSLionel Sambuc              ActiveRegions.back()->endLoc() <= Region.startLoc())
311*0a6a1f1dSLionel Sambuc         popRegion();
312*0a6a1f1dSLionel Sambuc       if (Segments.size() && Segments.back().Line == Region.LineStart &&
313*0a6a1f1dSLionel Sambuc           Segments.back().Col == Region.ColumnStart) {
314*0a6a1f1dSLionel Sambuc         if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion)
315*0a6a1f1dSLionel Sambuc           Segments.back().addCount(Region.ExecutionCount);
316*0a6a1f1dSLionel Sambuc       } else {
317*0a6a1f1dSLionel Sambuc         // Add this region to the stack.
318*0a6a1f1dSLionel Sambuc         ActiveRegions.push_back(&Region);
319*0a6a1f1dSLionel Sambuc         startSegment(Region);
320*0a6a1f1dSLionel Sambuc       }
321*0a6a1f1dSLionel Sambuc     }
322*0a6a1f1dSLionel Sambuc     // Pop any regions that are left in the stack.
323*0a6a1f1dSLionel Sambuc     while (!ActiveRegions.empty())
324*0a6a1f1dSLionel Sambuc       popRegion();
325*0a6a1f1dSLionel Sambuc     return Segments;
326*0a6a1f1dSLionel Sambuc   }
327*0a6a1f1dSLionel Sambuc };
328*0a6a1f1dSLionel Sambuc }
329*0a6a1f1dSLionel Sambuc 
getUniqueSourceFiles() const330*0a6a1f1dSLionel Sambuc std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
331*0a6a1f1dSLionel Sambuc   std::vector<StringRef> Filenames;
332*0a6a1f1dSLionel Sambuc   for (const auto &Function : getCoveredFunctions())
333*0a6a1f1dSLionel Sambuc     for (const auto &Filename : Function.Filenames)
334*0a6a1f1dSLionel Sambuc       Filenames.push_back(Filename);
335*0a6a1f1dSLionel Sambuc   std::sort(Filenames.begin(), Filenames.end());
336*0a6a1f1dSLionel Sambuc   auto Last = std::unique(Filenames.begin(), Filenames.end());
337*0a6a1f1dSLionel Sambuc   Filenames.erase(Last, Filenames.end());
338*0a6a1f1dSLionel Sambuc   return Filenames;
339*0a6a1f1dSLionel Sambuc }
340*0a6a1f1dSLionel Sambuc 
findMainViewFileID(StringRef SourceFile,const FunctionRecord & Function)341*0a6a1f1dSLionel Sambuc static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
342*0a6a1f1dSLionel Sambuc                                              const FunctionRecord &Function) {
343*0a6a1f1dSLionel Sambuc   llvm::SmallVector<bool, 8> IsExpandedFile(Function.Filenames.size(), false);
344*0a6a1f1dSLionel Sambuc   llvm::SmallVector<bool, 8> FilenameEquivalence(Function.Filenames.size(),
345*0a6a1f1dSLionel Sambuc                                                  false);
346*0a6a1f1dSLionel Sambuc   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
347*0a6a1f1dSLionel Sambuc     if (SourceFile == Function.Filenames[I])
348*0a6a1f1dSLionel Sambuc       FilenameEquivalence[I] = true;
349*0a6a1f1dSLionel Sambuc   for (const auto &CR : Function.CountedRegions)
350*0a6a1f1dSLionel Sambuc     if (CR.Kind == CounterMappingRegion::ExpansionRegion &&
351*0a6a1f1dSLionel Sambuc         FilenameEquivalence[CR.FileID])
352*0a6a1f1dSLionel Sambuc       IsExpandedFile[CR.ExpandedFileID] = true;
353*0a6a1f1dSLionel Sambuc   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
354*0a6a1f1dSLionel Sambuc     if (FilenameEquivalence[I] && !IsExpandedFile[I])
355*0a6a1f1dSLionel Sambuc       return I;
356*0a6a1f1dSLionel Sambuc   return None;
357*0a6a1f1dSLionel Sambuc }
358*0a6a1f1dSLionel Sambuc 
findMainViewFileID(const FunctionRecord & Function)359*0a6a1f1dSLionel Sambuc static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
360*0a6a1f1dSLionel Sambuc   llvm::SmallVector<bool, 8> IsExpandedFile(Function.Filenames.size(), false);
361*0a6a1f1dSLionel Sambuc   for (const auto &CR : Function.CountedRegions)
362*0a6a1f1dSLionel Sambuc     if (CR.Kind == CounterMappingRegion::ExpansionRegion)
363*0a6a1f1dSLionel Sambuc       IsExpandedFile[CR.ExpandedFileID] = true;
364*0a6a1f1dSLionel Sambuc   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
365*0a6a1f1dSLionel Sambuc     if (!IsExpandedFile[I])
366*0a6a1f1dSLionel Sambuc       return I;
367*0a6a1f1dSLionel Sambuc   return None;
368*0a6a1f1dSLionel Sambuc }
369*0a6a1f1dSLionel Sambuc 
gatherFileIDs(StringRef SourceFile,const FunctionRecord & Function)370*0a6a1f1dSLionel Sambuc static SmallSet<unsigned, 8> gatherFileIDs(StringRef SourceFile,
371*0a6a1f1dSLionel Sambuc                                            const FunctionRecord &Function) {
372*0a6a1f1dSLionel Sambuc   SmallSet<unsigned, 8> IDs;
373*0a6a1f1dSLionel Sambuc   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
374*0a6a1f1dSLionel Sambuc     if (SourceFile == Function.Filenames[I])
375*0a6a1f1dSLionel Sambuc       IDs.insert(I);
376*0a6a1f1dSLionel Sambuc   return IDs;
377*0a6a1f1dSLionel Sambuc }
378*0a6a1f1dSLionel Sambuc 
379*0a6a1f1dSLionel Sambuc /// Sort a nested sequence of regions from a single file.
sortNestedRegions(It First,It Last)380*0a6a1f1dSLionel Sambuc template <class It> static void sortNestedRegions(It First, It Last) {
381*0a6a1f1dSLionel Sambuc   std::sort(First, Last,
382*0a6a1f1dSLionel Sambuc             [](const CountedRegion &LHS, const CountedRegion &RHS) {
383*0a6a1f1dSLionel Sambuc     if (LHS.startLoc() == RHS.startLoc())
384*0a6a1f1dSLionel Sambuc       // When LHS completely contains RHS, we sort LHS first.
385*0a6a1f1dSLionel Sambuc       return RHS.endLoc() < LHS.endLoc();
386*0a6a1f1dSLionel Sambuc     return LHS.startLoc() < RHS.startLoc();
387*0a6a1f1dSLionel Sambuc   });
388*0a6a1f1dSLionel Sambuc }
389*0a6a1f1dSLionel Sambuc 
isExpansion(const CountedRegion & R,unsigned FileID)390*0a6a1f1dSLionel Sambuc static bool isExpansion(const CountedRegion &R, unsigned FileID) {
391*0a6a1f1dSLionel Sambuc   return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
392*0a6a1f1dSLionel Sambuc }
393*0a6a1f1dSLionel Sambuc 
getCoverageForFile(StringRef Filename)394*0a6a1f1dSLionel Sambuc CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) {
395*0a6a1f1dSLionel Sambuc   CoverageData FileCoverage(Filename);
396*0a6a1f1dSLionel Sambuc   std::vector<coverage::CountedRegion> Regions;
397*0a6a1f1dSLionel Sambuc 
398*0a6a1f1dSLionel Sambuc   for (const auto &Function : Functions) {
399*0a6a1f1dSLionel Sambuc     auto MainFileID = findMainViewFileID(Filename, Function);
400*0a6a1f1dSLionel Sambuc     if (!MainFileID)
401*0a6a1f1dSLionel Sambuc       continue;
402*0a6a1f1dSLionel Sambuc     auto FileIDs = gatherFileIDs(Filename, Function);
403*0a6a1f1dSLionel Sambuc     for (const auto &CR : Function.CountedRegions)
404*0a6a1f1dSLionel Sambuc       if (FileIDs.count(CR.FileID)) {
405*0a6a1f1dSLionel Sambuc         Regions.push_back(CR);
406*0a6a1f1dSLionel Sambuc         if (isExpansion(CR, *MainFileID))
407*0a6a1f1dSLionel Sambuc           FileCoverage.Expansions.emplace_back(CR, Function);
408*0a6a1f1dSLionel Sambuc       }
409*0a6a1f1dSLionel Sambuc   }
410*0a6a1f1dSLionel Sambuc 
411*0a6a1f1dSLionel Sambuc   sortNestedRegions(Regions.begin(), Regions.end());
412*0a6a1f1dSLionel Sambuc   FileCoverage.Segments = SegmentBuilder().buildSegments(Regions);
413*0a6a1f1dSLionel Sambuc 
414*0a6a1f1dSLionel Sambuc   return FileCoverage;
415*0a6a1f1dSLionel Sambuc }
416*0a6a1f1dSLionel Sambuc 
417*0a6a1f1dSLionel Sambuc std::vector<const FunctionRecord *>
getInstantiations(StringRef Filename)418*0a6a1f1dSLionel Sambuc CoverageMapping::getInstantiations(StringRef Filename) {
419*0a6a1f1dSLionel Sambuc   FunctionInstantiationSetCollector InstantiationSetCollector;
420*0a6a1f1dSLionel Sambuc   for (const auto &Function : Functions) {
421*0a6a1f1dSLionel Sambuc     auto MainFileID = findMainViewFileID(Filename, Function);
422*0a6a1f1dSLionel Sambuc     if (!MainFileID)
423*0a6a1f1dSLionel Sambuc       continue;
424*0a6a1f1dSLionel Sambuc     InstantiationSetCollector.insert(Function, *MainFileID);
425*0a6a1f1dSLionel Sambuc   }
426*0a6a1f1dSLionel Sambuc 
427*0a6a1f1dSLionel Sambuc   std::vector<const FunctionRecord *> Result;
428*0a6a1f1dSLionel Sambuc   for (const auto &InstantiationSet : InstantiationSetCollector) {
429*0a6a1f1dSLionel Sambuc     if (InstantiationSet.second.size() < 2)
430*0a6a1f1dSLionel Sambuc       continue;
431*0a6a1f1dSLionel Sambuc     for (auto Function : InstantiationSet.second)
432*0a6a1f1dSLionel Sambuc       Result.push_back(Function);
433*0a6a1f1dSLionel Sambuc   }
434*0a6a1f1dSLionel Sambuc   return Result;
435*0a6a1f1dSLionel Sambuc }
436*0a6a1f1dSLionel Sambuc 
437*0a6a1f1dSLionel Sambuc CoverageData
getCoverageForFunction(const FunctionRecord & Function)438*0a6a1f1dSLionel Sambuc CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) {
439*0a6a1f1dSLionel Sambuc   auto MainFileID = findMainViewFileID(Function);
440*0a6a1f1dSLionel Sambuc   if (!MainFileID)
441*0a6a1f1dSLionel Sambuc     return CoverageData();
442*0a6a1f1dSLionel Sambuc 
443*0a6a1f1dSLionel Sambuc   CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
444*0a6a1f1dSLionel Sambuc   std::vector<coverage::CountedRegion> Regions;
445*0a6a1f1dSLionel Sambuc   for (const auto &CR : Function.CountedRegions)
446*0a6a1f1dSLionel Sambuc     if (CR.FileID == *MainFileID) {
447*0a6a1f1dSLionel Sambuc       Regions.push_back(CR);
448*0a6a1f1dSLionel Sambuc       if (isExpansion(CR, *MainFileID))
449*0a6a1f1dSLionel Sambuc         FunctionCoverage.Expansions.emplace_back(CR, Function);
450*0a6a1f1dSLionel Sambuc     }
451*0a6a1f1dSLionel Sambuc 
452*0a6a1f1dSLionel Sambuc   sortNestedRegions(Regions.begin(), Regions.end());
453*0a6a1f1dSLionel Sambuc   FunctionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
454*0a6a1f1dSLionel Sambuc 
455*0a6a1f1dSLionel Sambuc   return FunctionCoverage;
456*0a6a1f1dSLionel Sambuc }
457*0a6a1f1dSLionel Sambuc 
458*0a6a1f1dSLionel Sambuc CoverageData
getCoverageForExpansion(const ExpansionRecord & Expansion)459*0a6a1f1dSLionel Sambuc CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) {
460*0a6a1f1dSLionel Sambuc   CoverageData ExpansionCoverage(
461*0a6a1f1dSLionel Sambuc       Expansion.Function.Filenames[Expansion.FileID]);
462*0a6a1f1dSLionel Sambuc   std::vector<coverage::CountedRegion> Regions;
463*0a6a1f1dSLionel Sambuc   for (const auto &CR : Expansion.Function.CountedRegions)
464*0a6a1f1dSLionel Sambuc     if (CR.FileID == Expansion.FileID) {
465*0a6a1f1dSLionel Sambuc       Regions.push_back(CR);
466*0a6a1f1dSLionel Sambuc       if (isExpansion(CR, Expansion.FileID))
467*0a6a1f1dSLionel Sambuc         ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
468*0a6a1f1dSLionel Sambuc     }
469*0a6a1f1dSLionel Sambuc 
470*0a6a1f1dSLionel Sambuc   sortNestedRegions(Regions.begin(), Regions.end());
471*0a6a1f1dSLionel Sambuc   ExpansionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
472*0a6a1f1dSLionel Sambuc 
473*0a6a1f1dSLionel Sambuc   return ExpansionCoverage;
474*0a6a1f1dSLionel Sambuc }
475