xref: /llvm-project/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp (revision a17a3e9d9a6b4baefd96e19ee5e8ce04cead8ab5)
1 //===- CoverageMapping.cpp - Code coverage mapping support ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for clang's and llvm's instrumentation based
10 // code coverage.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallBitVector.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Object/BuildID.h"
23 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
24 #include "llvm/ProfileData/InstrProfReader.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Errc.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/VirtualFileSystem.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <cmath>
35 #include <cstdint>
36 #include <iterator>
37 #include <map>
38 #include <memory>
39 #include <optional>
40 #include <string>
41 #include <system_error>
42 #include <utility>
43 #include <vector>
44 
45 using namespace llvm;
46 using namespace coverage;
47 
48 #define DEBUG_TYPE "coverage-mapping"
49 
50 Counter CounterExpressionBuilder::get(const CounterExpression &E) {
51   auto It = ExpressionIndices.find(E);
52   if (It != ExpressionIndices.end())
53     return Counter::getExpression(It->second);
54   unsigned I = Expressions.size();
55   Expressions.push_back(E);
56   ExpressionIndices[E] = I;
57   return Counter::getExpression(I);
58 }
59 
60 void CounterExpressionBuilder::extractTerms(Counter C, int Factor,
61                                             SmallVectorImpl<Term> &Terms) {
62   switch (C.getKind()) {
63   case Counter::Zero:
64     break;
65   case Counter::CounterValueReference:
66     Terms.emplace_back(C.getCounterID(), Factor);
67     break;
68   case Counter::Expression:
69     const auto &E = Expressions[C.getExpressionID()];
70     extractTerms(E.LHS, Factor, Terms);
71     extractTerms(
72         E.RHS, E.Kind == CounterExpression::Subtract ? -Factor : Factor, Terms);
73     break;
74   }
75 }
76 
77 Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
78   // Gather constant terms.
79   SmallVector<Term, 32> Terms;
80   extractTerms(ExpressionTree, +1, Terms);
81 
82   // If there are no terms, this is just a zero. The algorithm below assumes at
83   // least one term.
84   if (Terms.size() == 0)
85     return Counter::getZero();
86 
87   // Group the terms by counter ID.
88   llvm::sort(Terms, [](const Term &LHS, const Term &RHS) {
89     return LHS.CounterID < RHS.CounterID;
90   });
91 
92   // Combine terms by counter ID to eliminate counters that sum to zero.
93   auto Prev = Terms.begin();
94   for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
95     if (I->CounterID == Prev->CounterID) {
96       Prev->Factor += I->Factor;
97       continue;
98     }
99     ++Prev;
100     *Prev = *I;
101   }
102   Terms.erase(++Prev, Terms.end());
103 
104   Counter C;
105   // Create additions. We do this before subtractions to avoid constructs like
106   // ((0 - X) + Y), as opposed to (Y - X).
107   for (auto T : Terms) {
108     if (T.Factor <= 0)
109       continue;
110     for (int I = 0; I < T.Factor; ++I)
111       if (C.isZero())
112         C = Counter::getCounter(T.CounterID);
113       else
114         C = get(CounterExpression(CounterExpression::Add, C,
115                                   Counter::getCounter(T.CounterID)));
116   }
117 
118   // Create subtractions.
119   for (auto T : Terms) {
120     if (T.Factor >= 0)
121       continue;
122     for (int I = 0; I < -T.Factor; ++I)
123       C = get(CounterExpression(CounterExpression::Subtract, C,
124                                 Counter::getCounter(T.CounterID)));
125   }
126   return C;
127 }
128 
129 Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS, bool Simplify) {
130   auto Cnt = get(CounterExpression(CounterExpression::Add, LHS, RHS));
131   return Simplify ? simplify(Cnt) : Cnt;
132 }
133 
134 Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
135                                            bool Simplify) {
136   auto Cnt = get(CounterExpression(CounterExpression::Subtract, LHS, RHS));
137   return Simplify ? simplify(Cnt) : Cnt;
138 }
139 
140 void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
141   switch (C.getKind()) {
142   case Counter::Zero:
143     OS << '0';
144     return;
145   case Counter::CounterValueReference:
146     OS << '#' << C.getCounterID();
147     break;
148   case Counter::Expression: {
149     if (C.getExpressionID() >= Expressions.size())
150       return;
151     const auto &E = Expressions[C.getExpressionID()];
152     OS << '(';
153     dump(E.LHS, OS);
154     OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
155     dump(E.RHS, OS);
156     OS << ')';
157     break;
158   }
159   }
160   if (CounterValues.empty())
161     return;
162   Expected<int64_t> Value = evaluate(C);
163   if (auto E = Value.takeError()) {
164     consumeError(std::move(E));
165     return;
166   }
167   OS << '[' << *Value << ']';
168 }
169 
170 Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
171   struct StackElem {
172     Counter ICounter;
173     int64_t LHS = 0;
174     enum {
175       KNeverVisited = 0,
176       KVisitedOnce = 1,
177       KVisitedTwice = 2,
178     } VisitCount = KNeverVisited;
179   };
180 
181   std::stack<StackElem> CounterStack;
182   CounterStack.push({C});
183 
184   int64_t LastPoppedValue;
185 
186   while (!CounterStack.empty()) {
187     StackElem &Current = CounterStack.top();
188 
189     switch (Current.ICounter.getKind()) {
190     case Counter::Zero:
191       LastPoppedValue = 0;
192       CounterStack.pop();
193       break;
194     case Counter::CounterValueReference:
195       if (Current.ICounter.getCounterID() >= CounterValues.size())
196         return errorCodeToError(errc::argument_out_of_domain);
197       LastPoppedValue = CounterValues[Current.ICounter.getCounterID()];
198       CounterStack.pop();
199       break;
200     case Counter::Expression: {
201       if (Current.ICounter.getExpressionID() >= Expressions.size())
202         return errorCodeToError(errc::argument_out_of_domain);
203       const auto &E = Expressions[Current.ICounter.getExpressionID()];
204       if (Current.VisitCount == StackElem::KNeverVisited) {
205         CounterStack.push(StackElem{E.LHS});
206         Current.VisitCount = StackElem::KVisitedOnce;
207       } else if (Current.VisitCount == StackElem::KVisitedOnce) {
208         Current.LHS = LastPoppedValue;
209         CounterStack.push(StackElem{E.RHS});
210         Current.VisitCount = StackElem::KVisitedTwice;
211       } else {
212         int64_t LHS = Current.LHS;
213         int64_t RHS = LastPoppedValue;
214         LastPoppedValue =
215             E.Kind == CounterExpression::Subtract ? LHS - RHS : LHS + RHS;
216         CounterStack.pop();
217       }
218       break;
219     }
220     }
221   }
222 
223   return LastPoppedValue;
224 }
225 
226 namespace {
227 
228 class MCDCRecordProcessor {
229   /// A bitmap representing the executed test vectors for a boolean expression.
230   /// Each index of the bitmap corresponds to a possible test vector. An index
231   /// with a bit value of '1' indicates that the corresponding Test Vector
232   /// identified by that index was executed.
233   const BitVector &Bitmap;
234 
235   /// Decision Region to which the ExecutedTestVectorBitmap applies.
236   const CounterMappingRegion &Region;
237   const mcdc::DecisionParameters &DecisionParams;
238 
239   /// Array of branch regions corresponding each conditions in the boolean
240   /// expression.
241   ArrayRef<const CounterMappingRegion *> Branches;
242 
243   /// Total number of conditions in the boolean expression.
244   unsigned NumConditions;
245 
246   unsigned BitmapIdx;
247 
248   /// Mapping of a condition ID to its corresponding branch params.
249   llvm::DenseMap<unsigned, const mcdc::BranchParameters *> BranchParamsMap;
250 
251   /// Vector used to track whether a condition is constant folded.
252   MCDCRecord::BoolVector Folded;
253 
254   /// Mapping of calculated MC/DC Independence Pairs for each condition.
255   MCDCRecord::TVPairMap IndependencePairs;
256 
257   /// Actual executed Test Vectors for the boolean expression, based on
258   /// ExecutedTestVectorBitmap.
259   MCDCRecord::TestVectors ExecVectors;
260 
261 public:
262   MCDCRecordProcessor(const BitVector &Bitmap,
263                       const CounterMappingRegion &Region,
264                       ArrayRef<const CounterMappingRegion *> Branches)
265       : Bitmap(Bitmap), Region(Region),
266         DecisionParams(Region.getDecisionParams()), Branches(Branches),
267         NumConditions(DecisionParams.NumConditions),
268         BitmapIdx(DecisionParams.BitmapIdx * CHAR_BIT),
269         Folded(NumConditions, false), IndependencePairs(NumConditions) {}
270 
271 private:
272   void recordTestVector(MCDCRecord::TestVector &TV, unsigned Index,
273                         MCDCRecord::CondState Result) {
274     if (!Bitmap[BitmapIdx + Index])
275       return;
276 
277     // Copy the completed test vector to the vector of testvectors.
278     ExecVectors.push_back(TV);
279 
280     // The final value (T,F) is equal to the last non-dontcare state on the
281     // path (in a short-circuiting system).
282     ExecVectors.back().push_back(Result);
283   }
284 
285   // Walk the binary decision diagram and try assigning both false and true to
286   // each node. When a terminal node (ID == 0) is reached, fill in the value in
287   // the truth table.
288   void buildTestVector(MCDCRecord::TestVector &TV, unsigned ID,
289                        unsigned Index) {
290     auto [UnusedID, TrueID, FalseID] = *BranchParamsMap[ID];
291 
292     TV[ID - 1] = MCDCRecord::MCDC_False;
293     if (FalseID > 0)
294       buildTestVector(TV, FalseID, Index);
295     else
296       recordTestVector(TV, Index, MCDCRecord::MCDC_False);
297 
298     Index |= 1 << (ID - 1);
299     TV[ID - 1] = MCDCRecord::MCDC_True;
300     if (TrueID > 0)
301       buildTestVector(TV, TrueID, Index);
302     else
303       recordTestVector(TV, Index, MCDCRecord::MCDC_True);
304 
305     // Reset back to DontCare.
306     TV[ID - 1] = MCDCRecord::MCDC_DontCare;
307   }
308 
309   /// Walk the bits in the bitmap.  A bit set to '1' indicates that the test
310   /// vector at the corresponding index was executed during a test run.
311   void findExecutedTestVectors() {
312     // Walk the binary decision diagram to enumerate all possible test vectors.
313     // We start at the root node (ID == 1) with all values being DontCare.
314     // `Index` encodes the bitmask of true values and is initially 0.
315     MCDCRecord::TestVector TV(NumConditions, MCDCRecord::MCDC_DontCare);
316     buildTestVector(TV, 1, 0);
317   }
318 
319   // Find an independence pair for each condition:
320   // - The condition is true in one test and false in the other.
321   // - The decision outcome is true one test and false in the other.
322   // - All other conditions' values must be equal or marked as "don't care".
323   void findIndependencePairs() {
324     unsigned NumTVs = ExecVectors.size();
325     for (unsigned I = 1; I < NumTVs; ++I) {
326       const MCDCRecord::TestVector &A = ExecVectors[I];
327       for (unsigned J = 0; J < I; ++J) {
328         const MCDCRecord::TestVector &B = ExecVectors[J];
329         // Enumerate two execution vectors whose outcomes are different.
330         if (A[NumConditions] == B[NumConditions])
331           continue;
332         unsigned Flip = NumConditions, Idx;
333         for (Idx = 0; Idx < NumConditions; ++Idx) {
334           MCDCRecord::CondState ACond = A[Idx], BCond = B[Idx];
335           if (ACond == BCond || ACond == MCDCRecord::MCDC_DontCare ||
336               BCond == MCDCRecord::MCDC_DontCare)
337             continue;
338           if (Flip != NumConditions)
339             break;
340           Flip = Idx;
341         }
342         // If the two vectors differ in exactly one condition, ignoring DontCare
343         // conditions, we have found an independence pair.
344         if (Idx == NumConditions && Flip != NumConditions)
345           IndependencePairs.insert({Flip, std::make_pair(J + 1, I + 1)});
346       }
347     }
348   }
349 
350 public:
351   /// Process the MC/DC Record in order to produce a result for a boolean
352   /// expression. This process includes tracking the conditions that comprise
353   /// the decision region, calculating the list of all possible test vectors,
354   /// marking the executed test vectors, and then finding an Independence Pair
355   /// out of the executed test vectors for each condition in the boolean
356   /// expression. A condition is tracked to ensure that its ID can be mapped to
357   /// its ordinal position in the boolean expression. The condition's source
358   /// location is also tracked, as well as whether it is constant folded (in
359   /// which case it is excuded from the metric).
360   MCDCRecord processMCDCRecord() {
361     unsigned I = 0;
362     MCDCRecord::CondIDMap PosToID;
363     MCDCRecord::LineColPairMap CondLoc;
364 
365     // Walk the Record's BranchRegions (representing Conditions) in order to:
366     // - Hash the condition based on its corresponding ID. This will be used to
367     //   calculate the test vectors.
368     // - Keep a map of the condition's ordinal position (1, 2, 3, 4) to its
369     //   actual ID.  This will be used to visualize the conditions in the
370     //   correct order.
371     // - Keep track of the condition source location. This will be used to
372     //   visualize where the condition is.
373     // - Record whether the condition is constant folded so that we exclude it
374     //   from being measured.
375     for (const auto *B : Branches) {
376       const auto &BranchParams = B->getBranchParams();
377       BranchParamsMap[BranchParams.ID] = &BranchParams;
378       PosToID[I] = BranchParams.ID - 1;
379       CondLoc[I] = B->startLoc();
380       Folded[I++] = (B->Count.isZero() && B->FalseCount.isZero());
381     }
382 
383     // Using Profile Bitmap from runtime, mark the executed test vectors.
384     findExecutedTestVectors();
385 
386     // Compare executed test vectors against each other to find an independence
387     // pairs for each condition.  This processing takes the most time.
388     findIndependencePairs();
389 
390     // Record Test vectors, executed vectors, and independence pairs.
391     return MCDCRecord(Region, std::move(ExecVectors),
392                       std::move(IndependencePairs), std::move(Folded),
393                       std::move(PosToID), std::move(CondLoc));
394   }
395 };
396 
397 } // namespace
398 
399 Expected<MCDCRecord> CounterMappingContext::evaluateMCDCRegion(
400     const CounterMappingRegion &Region,
401     ArrayRef<const CounterMappingRegion *> Branches) {
402 
403   MCDCRecordProcessor MCDCProcessor(Bitmap, Region, Branches);
404   return MCDCProcessor.processMCDCRecord();
405 }
406 
407 unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
408   struct StackElem {
409     Counter ICounter;
410     int64_t LHS = 0;
411     enum {
412       KNeverVisited = 0,
413       KVisitedOnce = 1,
414       KVisitedTwice = 2,
415     } VisitCount = KNeverVisited;
416   };
417 
418   std::stack<StackElem> CounterStack;
419   CounterStack.push({C});
420 
421   int64_t LastPoppedValue;
422 
423   while (!CounterStack.empty()) {
424     StackElem &Current = CounterStack.top();
425 
426     switch (Current.ICounter.getKind()) {
427     case Counter::Zero:
428       LastPoppedValue = 0;
429       CounterStack.pop();
430       break;
431     case Counter::CounterValueReference:
432       LastPoppedValue = Current.ICounter.getCounterID();
433       CounterStack.pop();
434       break;
435     case Counter::Expression: {
436       if (Current.ICounter.getExpressionID() >= Expressions.size()) {
437         LastPoppedValue = 0;
438         CounterStack.pop();
439       } else {
440         const auto &E = Expressions[Current.ICounter.getExpressionID()];
441         if (Current.VisitCount == StackElem::KNeverVisited) {
442           CounterStack.push(StackElem{E.LHS});
443           Current.VisitCount = StackElem::KVisitedOnce;
444         } else if (Current.VisitCount == StackElem::KVisitedOnce) {
445           Current.LHS = LastPoppedValue;
446           CounterStack.push(StackElem{E.RHS});
447           Current.VisitCount = StackElem::KVisitedTwice;
448         } else {
449           int64_t LHS = Current.LHS;
450           int64_t RHS = LastPoppedValue;
451           LastPoppedValue = std::max(LHS, RHS);
452           CounterStack.pop();
453         }
454       }
455       break;
456     }
457     }
458   }
459 
460   return LastPoppedValue;
461 }
462 
463 void FunctionRecordIterator::skipOtherFiles() {
464   while (Current != Records.end() && !Filename.empty() &&
465          Filename != Current->Filenames[0])
466     ++Current;
467   if (Current == Records.end())
468     *this = FunctionRecordIterator();
469 }
470 
471 ArrayRef<unsigned> CoverageMapping::getImpreciseRecordIndicesForFilename(
472     StringRef Filename) const {
473   size_t FilenameHash = hash_value(Filename);
474   auto RecordIt = FilenameHash2RecordIndices.find(FilenameHash);
475   if (RecordIt == FilenameHash2RecordIndices.end())
476     return {};
477   return RecordIt->second;
478 }
479 
480 static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
481                                 const CoverageMappingRecord &Record) {
482   unsigned MaxCounterID = 0;
483   for (const auto &Region : Record.MappingRegions) {
484     MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
485   }
486   return MaxCounterID;
487 }
488 
489 /// Returns the bit count
490 static unsigned getMaxBitmapSize(const CounterMappingContext &Ctx,
491                                  const CoverageMappingRecord &Record) {
492   unsigned MaxBitmapIdx = 0;
493   unsigned NumConditions = 0;
494   // Scan max(BitmapIdx).
495   // Note that `<=` is used insted of `<`, because `BitmapIdx == 0` is valid
496   // and `MaxBitmapIdx is `unsigned`. `BitmapIdx` is unique in the record.
497   for (const auto &Region : reverse(Record.MappingRegions)) {
498     if (Region.Kind != CounterMappingRegion::MCDCDecisionRegion)
499       continue;
500     const auto &DecisionParams = Region.getDecisionParams();
501     if (MaxBitmapIdx <= DecisionParams.BitmapIdx) {
502       MaxBitmapIdx = DecisionParams.BitmapIdx;
503       NumConditions = DecisionParams.NumConditions;
504     }
505   }
506   unsigned SizeInBits = llvm::alignTo(uint64_t(1) << NumConditions, CHAR_BIT);
507   return MaxBitmapIdx * CHAR_BIT + SizeInBits;
508 }
509 
510 namespace {
511 
512 /// Collect Decisions, Branchs, and Expansions and associate them.
513 class MCDCDecisionRecorder {
514 private:
515   /// This holds the DecisionRegion and MCDCBranches under it.
516   /// Also traverses Expansion(s).
517   /// The Decision has the number of MCDCBranches and will complete
518   /// when it is filled with unique ConditionID of MCDCBranches.
519   struct DecisionRecord {
520     const CounterMappingRegion *DecisionRegion;
521 
522     /// They are reflected from DecisionRegion for convenience.
523     mcdc::DecisionParameters DecisionParams;
524     LineColPair DecisionStartLoc;
525     LineColPair DecisionEndLoc;
526 
527     /// This is passed to `MCDCRecordProcessor`, so this should be compatible
528     /// to`ArrayRef<const CounterMappingRegion *>`.
529     SmallVector<const CounterMappingRegion *> MCDCBranches;
530 
531     /// IDs that are stored in MCDCBranches
532     /// Complete when all IDs (1 to NumConditions) are met.
533     DenseSet<mcdc::ConditionID> ConditionIDs;
534 
535     /// Set of IDs of Expansion(s) that are relevant to DecisionRegion
536     /// and its children (via expansions).
537     /// FileID  pointed by ExpandedFileID is dedicated to the expansion, so
538     /// the location in the expansion doesn't matter.
539     DenseSet<unsigned> ExpandedFileIDs;
540 
541     DecisionRecord(const CounterMappingRegion &Decision)
542         : DecisionRegion(&Decision),
543           DecisionParams(Decision.getDecisionParams()),
544           DecisionStartLoc(Decision.startLoc()),
545           DecisionEndLoc(Decision.endLoc()) {
546       assert(Decision.Kind == CounterMappingRegion::MCDCDecisionRegion);
547     }
548 
549     /// Determine whether DecisionRecord dominates `R`.
550     bool dominates(const CounterMappingRegion &R) const {
551       // Determine whether `R` is included in `DecisionRegion`.
552       if (R.FileID == DecisionRegion->FileID &&
553           R.startLoc() >= DecisionStartLoc && R.endLoc() <= DecisionEndLoc)
554         return true;
555 
556       // Determine whether `R` is pointed by any of Expansions.
557       return ExpandedFileIDs.contains(R.FileID);
558     }
559 
560     enum Result {
561       NotProcessed = 0, /// Irrelevant to this Decision
562       Processed,        /// Added to this Decision
563       Completed,        /// Added and filled this Decision
564     };
565 
566     /// Add Branch into the Decision
567     /// \param Branch expects MCDCBranchRegion
568     /// \returns NotProcessed/Processed/Completed
569     Result addBranch(const CounterMappingRegion &Branch) {
570       assert(Branch.Kind == CounterMappingRegion::MCDCBranchRegion);
571 
572       auto ConditionID = Branch.getBranchParams().ID;
573       assert(ConditionID > 0 && "ConditionID should begin with 1");
574 
575       if (ConditionIDs.contains(ConditionID) ||
576           ConditionID > DecisionParams.NumConditions)
577         return NotProcessed;
578 
579       if (!this->dominates(Branch))
580         return NotProcessed;
581 
582       assert(MCDCBranches.size() < DecisionParams.NumConditions);
583 
584       // Put `ID=1` in front of `MCDCBranches` for convenience
585       // even if `MCDCBranches` is not topological.
586       if (ConditionID == 1)
587         MCDCBranches.insert(MCDCBranches.begin(), &Branch);
588       else
589         MCDCBranches.push_back(&Branch);
590 
591       // Mark `ID` as `assigned`.
592       ConditionIDs.insert(ConditionID);
593 
594       // `Completed` when `MCDCBranches` is full
595       return (MCDCBranches.size() == DecisionParams.NumConditions ? Completed
596                                                                   : Processed);
597     }
598 
599     /// Record Expansion if it is relevant to this Decision.
600     /// Each `Expansion` may nest.
601     /// \returns true if recorded.
602     bool recordExpansion(const CounterMappingRegion &Expansion) {
603       if (!this->dominates(Expansion))
604         return false;
605 
606       ExpandedFileIDs.insert(Expansion.ExpandedFileID);
607       return true;
608     }
609   };
610 
611 private:
612   /// Decisions in progress
613   /// DecisionRecord is added for each MCDCDecisionRegion.
614   /// DecisionRecord is removed when Decision is completed.
615   SmallVector<DecisionRecord> Decisions;
616 
617 public:
618   ~MCDCDecisionRecorder() {
619     assert(Decisions.empty() && "All Decisions have not been resolved");
620   }
621 
622   /// Register Region and start recording.
623   void registerDecision(const CounterMappingRegion &Decision) {
624     Decisions.emplace_back(Decision);
625   }
626 
627   void recordExpansion(const CounterMappingRegion &Expansion) {
628     any_of(Decisions, [&Expansion](auto &Decision) {
629       return Decision.recordExpansion(Expansion);
630     });
631   }
632 
633   using DecisionAndBranches =
634       std::pair<const CounterMappingRegion *,             /// Decision
635                 SmallVector<const CounterMappingRegion *> /// Branches
636                 >;
637 
638   /// Add MCDCBranchRegion to DecisionRecord.
639   /// \param Branch to be processed
640   /// \returns DecisionsAndBranches if DecisionRecord completed.
641   ///     Or returns nullopt.
642   std::optional<DecisionAndBranches>
643   processBranch(const CounterMappingRegion &Branch) {
644     // Seek each Decision and apply Region to it.
645     for (auto DecisionIter = Decisions.begin(), DecisionEnd = Decisions.end();
646          DecisionIter != DecisionEnd; ++DecisionIter)
647       switch (DecisionIter->addBranch(Branch)) {
648       case DecisionRecord::NotProcessed:
649         continue;
650       case DecisionRecord::Processed:
651         return std::nullopt;
652       case DecisionRecord::Completed:
653         DecisionAndBranches Result =
654             std::make_pair(DecisionIter->DecisionRegion,
655                            std::move(DecisionIter->MCDCBranches));
656         Decisions.erase(DecisionIter); // No longer used.
657         return Result;
658       }
659 
660     llvm_unreachable("Branch not found in Decisions");
661   }
662 };
663 
664 } // namespace
665 
666 Error CoverageMapping::loadFunctionRecord(
667     const CoverageMappingRecord &Record,
668     IndexedInstrProfReader &ProfileReader) {
669   StringRef OrigFuncName = Record.FunctionName;
670   if (OrigFuncName.empty())
671     return make_error<CoverageMapError>(coveragemap_error::malformed,
672                                         "record function name is empty");
673 
674   if (Record.Filenames.empty())
675     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
676   else
677     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
678 
679   CounterMappingContext Ctx(Record.Expressions);
680 
681   std::vector<uint64_t> Counts;
682   if (Error E = ProfileReader.getFunctionCounts(Record.FunctionName,
683                                                 Record.FunctionHash, Counts)) {
684     instrprof_error IPE = std::get<0>(InstrProfError::take(std::move(E)));
685     if (IPE == instrprof_error::hash_mismatch) {
686       FuncHashMismatches.emplace_back(std::string(Record.FunctionName),
687                                       Record.FunctionHash);
688       return Error::success();
689     }
690     if (IPE != instrprof_error::unknown_function)
691       return make_error<InstrProfError>(IPE);
692     Counts.assign(getMaxCounterID(Ctx, Record) + 1, 0);
693   }
694   Ctx.setCounts(Counts);
695 
696   BitVector Bitmap;
697   if (Error E = ProfileReader.getFunctionBitmap(Record.FunctionName,
698                                                 Record.FunctionHash, Bitmap)) {
699     instrprof_error IPE = std::get<0>(InstrProfError::take(std::move(E)));
700     if (IPE == instrprof_error::hash_mismatch) {
701       FuncHashMismatches.emplace_back(std::string(Record.FunctionName),
702                                       Record.FunctionHash);
703       return Error::success();
704     }
705     if (IPE != instrprof_error::unknown_function)
706       return make_error<InstrProfError>(IPE);
707     Bitmap = BitVector(getMaxBitmapSize(Ctx, Record));
708   }
709   Ctx.setBitmap(std::move(Bitmap));
710 
711   assert(!Record.MappingRegions.empty() && "Function has no regions");
712 
713   // This coverage record is a zero region for a function that's unused in
714   // some TU, but used in a different TU. Ignore it. The coverage maps from the
715   // the other TU will either be loaded (providing full region counts) or they
716   // won't (in which case we don't unintuitively report functions as uncovered
717   // when they have non-zero counts in the profile).
718   if (Record.MappingRegions.size() == 1 &&
719       Record.MappingRegions[0].Count.isZero() && Counts[0] > 0)
720     return Error::success();
721 
722   MCDCDecisionRecorder MCDCDecisions;
723   FunctionRecord Function(OrigFuncName, Record.Filenames);
724   for (const auto &Region : Record.MappingRegions) {
725     // MCDCDecisionRegion should be handled first since it overlaps with
726     // others inside.
727     if (Region.Kind == CounterMappingRegion::MCDCDecisionRegion) {
728       MCDCDecisions.registerDecision(Region);
729       continue;
730     }
731     Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
732     if (auto E = ExecutionCount.takeError()) {
733       consumeError(std::move(E));
734       return Error::success();
735     }
736     Expected<int64_t> AltExecutionCount = Ctx.evaluate(Region.FalseCount);
737     if (auto E = AltExecutionCount.takeError()) {
738       consumeError(std::move(E));
739       return Error::success();
740     }
741     Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount);
742 
743     // Record ExpansionRegion.
744     if (Region.Kind == CounterMappingRegion::ExpansionRegion) {
745       MCDCDecisions.recordExpansion(Region);
746       continue;
747     }
748 
749     // Do nothing unless MCDCBranchRegion.
750     if (Region.Kind != CounterMappingRegion::MCDCBranchRegion)
751       continue;
752 
753     auto Result = MCDCDecisions.processBranch(Region);
754     if (!Result) // Any Decision doesn't complete.
755       continue;
756 
757     auto MCDCDecision = Result->first;
758     auto &MCDCBranches = Result->second;
759 
760     // Since the bitmap identifies the executed test vectors for an MC/DC
761     // DecisionRegion, all of the information is now available to process.
762     // This is where the bulk of the MC/DC progressing takes place.
763     Expected<MCDCRecord> Record =
764         Ctx.evaluateMCDCRegion(*MCDCDecision, MCDCBranches);
765     if (auto E = Record.takeError()) {
766       consumeError(std::move(E));
767       return Error::success();
768     }
769 
770     // Save the MC/DC Record so that it can be visualized later.
771     Function.pushMCDCRecord(std::move(*Record));
772   }
773 
774   // Don't create records for (filenames, function) pairs we've already seen.
775   auto FilenamesHash = hash_combine_range(Record.Filenames.begin(),
776                                           Record.Filenames.end());
777   if (!RecordProvenance[FilenamesHash].insert(hash_value(OrigFuncName)).second)
778     return Error::success();
779 
780   Functions.push_back(std::move(Function));
781 
782   // Performance optimization: keep track of the indices of the function records
783   // which correspond to each filename. This can be used to substantially speed
784   // up queries for coverage info in a file.
785   unsigned RecordIndex = Functions.size() - 1;
786   for (StringRef Filename : Record.Filenames) {
787     auto &RecordIndices = FilenameHash2RecordIndices[hash_value(Filename)];
788     // Note that there may be duplicates in the filename set for a function
789     // record, because of e.g. macro expansions in the function in which both
790     // the macro and the function are defined in the same file.
791     if (RecordIndices.empty() || RecordIndices.back() != RecordIndex)
792       RecordIndices.push_back(RecordIndex);
793   }
794 
795   return Error::success();
796 }
797 
798 // This function is for memory optimization by shortening the lifetimes
799 // of CoverageMappingReader instances.
800 Error CoverageMapping::loadFromReaders(
801     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
802     IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
803   for (const auto &CoverageReader : CoverageReaders) {
804     for (auto RecordOrErr : *CoverageReader) {
805       if (Error E = RecordOrErr.takeError())
806         return E;
807       const auto &Record = *RecordOrErr;
808       if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader))
809         return E;
810     }
811   }
812   return Error::success();
813 }
814 
815 Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
816     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
817     IndexedInstrProfReader &ProfileReader) {
818   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
819   if (Error E = loadFromReaders(CoverageReaders, ProfileReader, *Coverage))
820     return std::move(E);
821   return std::move(Coverage);
822 }
823 
824 // If E is a no_data_found error, returns success. Otherwise returns E.
825 static Error handleMaybeNoDataFoundError(Error E) {
826   return handleErrors(
827       std::move(E), [](const CoverageMapError &CME) {
828         if (CME.get() == coveragemap_error::no_data_found)
829           return static_cast<Error>(Error::success());
830         return make_error<CoverageMapError>(CME.get(), CME.getMessage());
831       });
832 }
833 
834 Error CoverageMapping::loadFromFile(
835     StringRef Filename, StringRef Arch, StringRef CompilationDir,
836     IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage,
837     bool &DataFound, SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
838   auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(
839       Filename, /*IsText=*/false, /*RequiresNullTerminator=*/false);
840   if (std::error_code EC = CovMappingBufOrErr.getError())
841     return createFileError(Filename, errorCodeToError(EC));
842   MemoryBufferRef CovMappingBufRef =
843       CovMappingBufOrErr.get()->getMemBufferRef();
844   SmallVector<std::unique_ptr<MemoryBuffer>, 4> Buffers;
845 
846   SmallVector<object::BuildIDRef> BinaryIDs;
847   auto CoverageReadersOrErr = BinaryCoverageReader::create(
848       CovMappingBufRef, Arch, Buffers, CompilationDir,
849       FoundBinaryIDs ? &BinaryIDs : nullptr);
850   if (Error E = CoverageReadersOrErr.takeError()) {
851     E = handleMaybeNoDataFoundError(std::move(E));
852     if (E)
853       return createFileError(Filename, std::move(E));
854     return E;
855   }
856 
857   SmallVector<std::unique_ptr<CoverageMappingReader>, 4> Readers;
858   for (auto &Reader : CoverageReadersOrErr.get())
859     Readers.push_back(std::move(Reader));
860   if (FoundBinaryIDs && !Readers.empty()) {
861     llvm::append_range(*FoundBinaryIDs,
862                        llvm::map_range(BinaryIDs, [](object::BuildIDRef BID) {
863                          return object::BuildID(BID);
864                        }));
865   }
866   DataFound |= !Readers.empty();
867   if (Error E = loadFromReaders(Readers, ProfileReader, Coverage))
868     return createFileError(Filename, std::move(E));
869   return Error::success();
870 }
871 
872 Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
873     ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
874     vfs::FileSystem &FS, ArrayRef<StringRef> Arches, StringRef CompilationDir,
875     const object::BuildIDFetcher *BIDFetcher, bool CheckBinaryIDs) {
876   auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename, FS);
877   if (Error E = ProfileReaderOrErr.takeError())
878     return createFileError(ProfileFilename, std::move(E));
879   auto ProfileReader = std::move(ProfileReaderOrErr.get());
880   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
881   bool DataFound = false;
882 
883   auto GetArch = [&](size_t Idx) {
884     if (Arches.empty())
885       return StringRef();
886     if (Arches.size() == 1)
887       return Arches.front();
888     return Arches[Idx];
889   };
890 
891   SmallVector<object::BuildID> FoundBinaryIDs;
892   for (const auto &File : llvm::enumerate(ObjectFilenames)) {
893     if (Error E =
894             loadFromFile(File.value(), GetArch(File.index()), CompilationDir,
895                          *ProfileReader, *Coverage, DataFound, &FoundBinaryIDs))
896       return std::move(E);
897   }
898 
899   if (BIDFetcher) {
900     std::vector<object::BuildID> ProfileBinaryIDs;
901     if (Error E = ProfileReader->readBinaryIds(ProfileBinaryIDs))
902       return createFileError(ProfileFilename, std::move(E));
903 
904     SmallVector<object::BuildIDRef> BinaryIDsToFetch;
905     if (!ProfileBinaryIDs.empty()) {
906       const auto &Compare = [](object::BuildIDRef A, object::BuildIDRef B) {
907         return std::lexicographical_compare(A.begin(), A.end(), B.begin(),
908                                             B.end());
909       };
910       llvm::sort(FoundBinaryIDs, Compare);
911       std::set_difference(
912           ProfileBinaryIDs.begin(), ProfileBinaryIDs.end(),
913           FoundBinaryIDs.begin(), FoundBinaryIDs.end(),
914           std::inserter(BinaryIDsToFetch, BinaryIDsToFetch.end()), Compare);
915     }
916 
917     for (object::BuildIDRef BinaryID : BinaryIDsToFetch) {
918       std::optional<std::string> PathOpt = BIDFetcher->fetch(BinaryID);
919       if (PathOpt) {
920         std::string Path = std::move(*PathOpt);
921         StringRef Arch = Arches.size() == 1 ? Arches.front() : StringRef();
922         if (Error E = loadFromFile(Path, Arch, CompilationDir, *ProfileReader,
923                                   *Coverage, DataFound))
924           return std::move(E);
925       } else if (CheckBinaryIDs) {
926         return createFileError(
927             ProfileFilename,
928             createStringError(errc::no_such_file_or_directory,
929                               "Missing binary ID: " +
930                                   llvm::toHex(BinaryID, /*LowerCase=*/true)));
931       }
932     }
933   }
934 
935   if (!DataFound)
936     return createFileError(
937         join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "),
938         make_error<CoverageMapError>(coveragemap_error::no_data_found));
939   return std::move(Coverage);
940 }
941 
942 namespace {
943 
944 /// Distributes functions into instantiation sets.
945 ///
946 /// An instantiation set is a collection of functions that have the same source
947 /// code, ie, template functions specializations.
948 class FunctionInstantiationSetCollector {
949   using MapT = std::map<LineColPair, std::vector<const FunctionRecord *>>;
950   MapT InstantiatedFunctions;
951 
952 public:
953   void insert(const FunctionRecord &Function, unsigned FileID) {
954     auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
955     while (I != E && I->FileID != FileID)
956       ++I;
957     assert(I != E && "function does not cover the given file");
958     auto &Functions = InstantiatedFunctions[I->startLoc()];
959     Functions.push_back(&Function);
960   }
961 
962   MapT::iterator begin() { return InstantiatedFunctions.begin(); }
963   MapT::iterator end() { return InstantiatedFunctions.end(); }
964 };
965 
966 class SegmentBuilder {
967   std::vector<CoverageSegment> &Segments;
968   SmallVector<const CountedRegion *, 8> ActiveRegions;
969 
970   SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {}
971 
972   /// Emit a segment with the count from \p Region starting at \p StartLoc.
973   //
974   /// \p IsRegionEntry: The segment is at the start of a new non-gap region.
975   /// \p EmitSkippedRegion: The segment must be emitted as a skipped region.
976   void startSegment(const CountedRegion &Region, LineColPair StartLoc,
977                     bool IsRegionEntry, bool EmitSkippedRegion = false) {
978     bool HasCount = !EmitSkippedRegion &&
979                     (Region.Kind != CounterMappingRegion::SkippedRegion);
980 
981     // If the new segment wouldn't affect coverage rendering, skip it.
982     if (!Segments.empty() && !IsRegionEntry && !EmitSkippedRegion) {
983       const auto &Last = Segments.back();
984       if (Last.HasCount == HasCount && Last.Count == Region.ExecutionCount &&
985           !Last.IsRegionEntry)
986         return;
987     }
988 
989     if (HasCount)
990       Segments.emplace_back(StartLoc.first, StartLoc.second,
991                             Region.ExecutionCount, IsRegionEntry,
992                             Region.Kind == CounterMappingRegion::GapRegion);
993     else
994       Segments.emplace_back(StartLoc.first, StartLoc.second, IsRegionEntry);
995 
996     LLVM_DEBUG({
997       const auto &Last = Segments.back();
998       dbgs() << "Segment at " << Last.Line << ":" << Last.Col
999              << " (count = " << Last.Count << ")"
1000              << (Last.IsRegionEntry ? ", RegionEntry" : "")
1001              << (!Last.HasCount ? ", Skipped" : "")
1002              << (Last.IsGapRegion ? ", Gap" : "") << "\n";
1003     });
1004   }
1005 
1006   /// Emit segments for active regions which end before \p Loc.
1007   ///
1008   /// \p Loc: The start location of the next region. If std::nullopt, all active
1009   /// regions are completed.
1010   /// \p FirstCompletedRegion: Index of the first completed region.
1011   void completeRegionsUntil(std::optional<LineColPair> Loc,
1012                             unsigned FirstCompletedRegion) {
1013     // Sort the completed regions by end location. This makes it simple to
1014     // emit closing segments in sorted order.
1015     auto CompletedRegionsIt = ActiveRegions.begin() + FirstCompletedRegion;
1016     std::stable_sort(CompletedRegionsIt, ActiveRegions.end(),
1017                       [](const CountedRegion *L, const CountedRegion *R) {
1018                         return L->endLoc() < R->endLoc();
1019                       });
1020 
1021     // Emit segments for all completed regions.
1022     for (unsigned I = FirstCompletedRegion + 1, E = ActiveRegions.size(); I < E;
1023          ++I) {
1024       const auto *CompletedRegion = ActiveRegions[I];
1025       assert((!Loc || CompletedRegion->endLoc() <= *Loc) &&
1026              "Completed region ends after start of new region");
1027 
1028       const auto *PrevCompletedRegion = ActiveRegions[I - 1];
1029       auto CompletedSegmentLoc = PrevCompletedRegion->endLoc();
1030 
1031       // Don't emit any more segments if they start where the new region begins.
1032       if (Loc && CompletedSegmentLoc == *Loc)
1033         break;
1034 
1035       // Don't emit a segment if the next completed region ends at the same
1036       // location as this one.
1037       if (CompletedSegmentLoc == CompletedRegion->endLoc())
1038         continue;
1039 
1040       // Use the count from the last completed region which ends at this loc.
1041       for (unsigned J = I + 1; J < E; ++J)
1042         if (CompletedRegion->endLoc() == ActiveRegions[J]->endLoc())
1043           CompletedRegion = ActiveRegions[J];
1044 
1045       startSegment(*CompletedRegion, CompletedSegmentLoc, false);
1046     }
1047 
1048     auto Last = ActiveRegions.back();
1049     if (FirstCompletedRegion && Last->endLoc() != *Loc) {
1050       // If there's a gap after the end of the last completed region and the
1051       // start of the new region, use the last active region to fill the gap.
1052       startSegment(*ActiveRegions[FirstCompletedRegion - 1], Last->endLoc(),
1053                    false);
1054     } else if (!FirstCompletedRegion && (!Loc || *Loc != Last->endLoc())) {
1055       // Emit a skipped segment if there are no more active regions. This
1056       // ensures that gaps between functions are marked correctly.
1057       startSegment(*Last, Last->endLoc(), false, true);
1058     }
1059 
1060     // Pop the completed regions.
1061     ActiveRegions.erase(CompletedRegionsIt, ActiveRegions.end());
1062   }
1063 
1064   void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) {
1065     for (const auto &CR : enumerate(Regions)) {
1066       auto CurStartLoc = CR.value().startLoc();
1067 
1068       // Active regions which end before the current region need to be popped.
1069       auto CompletedRegions =
1070           std::stable_partition(ActiveRegions.begin(), ActiveRegions.end(),
1071                                 [&](const CountedRegion *Region) {
1072                                   return !(Region->endLoc() <= CurStartLoc);
1073                                 });
1074       if (CompletedRegions != ActiveRegions.end()) {
1075         unsigned FirstCompletedRegion =
1076             std::distance(ActiveRegions.begin(), CompletedRegions);
1077         completeRegionsUntil(CurStartLoc, FirstCompletedRegion);
1078       }
1079 
1080       bool GapRegion = CR.value().Kind == CounterMappingRegion::GapRegion;
1081 
1082       // Try to emit a segment for the current region.
1083       if (CurStartLoc == CR.value().endLoc()) {
1084         // Avoid making zero-length regions active. If it's the last region,
1085         // emit a skipped segment. Otherwise use its predecessor's count.
1086         const bool Skipped =
1087             (CR.index() + 1) == Regions.size() ||
1088             CR.value().Kind == CounterMappingRegion::SkippedRegion;
1089         startSegment(ActiveRegions.empty() ? CR.value() : *ActiveRegions.back(),
1090                      CurStartLoc, !GapRegion, Skipped);
1091         // If it is skipped segment, create a segment with last pushed
1092         // regions's count at CurStartLoc.
1093         if (Skipped && !ActiveRegions.empty())
1094           startSegment(*ActiveRegions.back(), CurStartLoc, false);
1095         continue;
1096       }
1097       if (CR.index() + 1 == Regions.size() ||
1098           CurStartLoc != Regions[CR.index() + 1].startLoc()) {
1099         // Emit a segment if the next region doesn't start at the same location
1100         // as this one.
1101         startSegment(CR.value(), CurStartLoc, !GapRegion);
1102       }
1103 
1104       // This region is active (i.e not completed).
1105       ActiveRegions.push_back(&CR.value());
1106     }
1107 
1108     // Complete any remaining active regions.
1109     if (!ActiveRegions.empty())
1110       completeRegionsUntil(std::nullopt, 0);
1111   }
1112 
1113   /// Sort a nested sequence of regions from a single file.
1114   static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) {
1115     llvm::sort(Regions, [](const CountedRegion &LHS, const CountedRegion &RHS) {
1116       if (LHS.startLoc() != RHS.startLoc())
1117         return LHS.startLoc() < RHS.startLoc();
1118       if (LHS.endLoc() != RHS.endLoc())
1119         // When LHS completely contains RHS, we sort LHS first.
1120         return RHS.endLoc() < LHS.endLoc();
1121       // If LHS and RHS cover the same area, we need to sort them according
1122       // to their kinds so that the most suitable region will become "active"
1123       // in combineRegions(). Because we accumulate counter values only from
1124       // regions of the same kind as the first region of the area, prefer
1125       // CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion.
1126       static_assert(CounterMappingRegion::CodeRegion <
1127                             CounterMappingRegion::ExpansionRegion &&
1128                         CounterMappingRegion::ExpansionRegion <
1129                             CounterMappingRegion::SkippedRegion,
1130                     "Unexpected order of region kind values");
1131       return LHS.Kind < RHS.Kind;
1132     });
1133   }
1134 
1135   /// Combine counts of regions which cover the same area.
1136   static ArrayRef<CountedRegion>
1137   combineRegions(MutableArrayRef<CountedRegion> Regions) {
1138     if (Regions.empty())
1139       return Regions;
1140     auto Active = Regions.begin();
1141     auto End = Regions.end();
1142     for (auto I = Regions.begin() + 1; I != End; ++I) {
1143       if (Active->startLoc() != I->startLoc() ||
1144           Active->endLoc() != I->endLoc()) {
1145         // Shift to the next region.
1146         ++Active;
1147         if (Active != I)
1148           *Active = *I;
1149         continue;
1150       }
1151       // Merge duplicate region.
1152       // If CodeRegions and ExpansionRegions cover the same area, it's probably
1153       // a macro which is fully expanded to another macro. In that case, we need
1154       // to accumulate counts only from CodeRegions, or else the area will be
1155       // counted twice.
1156       // On the other hand, a macro may have a nested macro in its body. If the
1157       // outer macro is used several times, the ExpansionRegion for the nested
1158       // macro will also be added several times. These ExpansionRegions cover
1159       // the same source locations and have to be combined to reach the correct
1160       // value for that area.
1161       // We add counts of the regions of the same kind as the active region
1162       // to handle the both situations.
1163       if (I->Kind == Active->Kind)
1164         Active->ExecutionCount += I->ExecutionCount;
1165     }
1166     return Regions.drop_back(std::distance(++Active, End));
1167   }
1168 
1169 public:
1170   /// Build a sorted list of CoverageSegments from a list of Regions.
1171   static std::vector<CoverageSegment>
1172   buildSegments(MutableArrayRef<CountedRegion> Regions) {
1173     std::vector<CoverageSegment> Segments;
1174     SegmentBuilder Builder(Segments);
1175 
1176     sortNestedRegions(Regions);
1177     ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
1178 
1179     LLVM_DEBUG({
1180       dbgs() << "Combined regions:\n";
1181       for (const auto &CR : CombinedRegions)
1182         dbgs() << "  " << CR.LineStart << ":" << CR.ColumnStart << " -> "
1183                << CR.LineEnd << ":" << CR.ColumnEnd
1184                << " (count=" << CR.ExecutionCount << ")\n";
1185     });
1186 
1187     Builder.buildSegmentsImpl(CombinedRegions);
1188 
1189 #ifndef NDEBUG
1190     for (unsigned I = 1, E = Segments.size(); I < E; ++I) {
1191       const auto &L = Segments[I - 1];
1192       const auto &R = Segments[I];
1193       if (!(L.Line < R.Line) && !(L.Line == R.Line && L.Col < R.Col)) {
1194         if (L.Line == R.Line && L.Col == R.Col && !L.HasCount)
1195           continue;
1196         LLVM_DEBUG(dbgs() << " ! Segment " << L.Line << ":" << L.Col
1197                           << " followed by " << R.Line << ":" << R.Col << "\n");
1198         assert(false && "Coverage segments not unique or sorted");
1199       }
1200     }
1201 #endif
1202 
1203     return Segments;
1204   }
1205 };
1206 
1207 } // end anonymous namespace
1208 
1209 std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
1210   std::vector<StringRef> Filenames;
1211   for (const auto &Function : getCoveredFunctions())
1212     llvm::append_range(Filenames, Function.Filenames);
1213   llvm::sort(Filenames);
1214   auto Last = std::unique(Filenames.begin(), Filenames.end());
1215   Filenames.erase(Last, Filenames.end());
1216   return Filenames;
1217 }
1218 
1219 static SmallBitVector gatherFileIDs(StringRef SourceFile,
1220                                     const FunctionRecord &Function) {
1221   SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
1222   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
1223     if (SourceFile == Function.Filenames[I])
1224       FilenameEquivalence[I] = true;
1225   return FilenameEquivalence;
1226 }
1227 
1228 /// Return the ID of the file where the definition of the function is located.
1229 static std::optional<unsigned>
1230 findMainViewFileID(const FunctionRecord &Function) {
1231   SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
1232   for (const auto &CR : Function.CountedRegions)
1233     if (CR.Kind == CounterMappingRegion::ExpansionRegion)
1234       IsNotExpandedFile[CR.ExpandedFileID] = false;
1235   int I = IsNotExpandedFile.find_first();
1236   if (I == -1)
1237     return std::nullopt;
1238   return I;
1239 }
1240 
1241 /// Check if SourceFile is the file that contains the definition of
1242 /// the Function. Return the ID of the file in that case or std::nullopt
1243 /// otherwise.
1244 static std::optional<unsigned>
1245 findMainViewFileID(StringRef SourceFile, const FunctionRecord &Function) {
1246   std::optional<unsigned> I = findMainViewFileID(Function);
1247   if (I && SourceFile == Function.Filenames[*I])
1248     return I;
1249   return std::nullopt;
1250 }
1251 
1252 static bool isExpansion(const CountedRegion &R, unsigned FileID) {
1253   return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
1254 }
1255 
1256 CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
1257   CoverageData FileCoverage(Filename);
1258   std::vector<CountedRegion> Regions;
1259 
1260   // Look up the function records in the given file. Due to hash collisions on
1261   // the filename, we may get back some records that are not in the file.
1262   ArrayRef<unsigned> RecordIndices =
1263       getImpreciseRecordIndicesForFilename(Filename);
1264   for (unsigned RecordIndex : RecordIndices) {
1265     const FunctionRecord &Function = Functions[RecordIndex];
1266     auto MainFileID = findMainViewFileID(Filename, Function);
1267     auto FileIDs = gatherFileIDs(Filename, Function);
1268     for (const auto &CR : Function.CountedRegions)
1269       if (FileIDs.test(CR.FileID)) {
1270         Regions.push_back(CR);
1271         if (MainFileID && isExpansion(CR, *MainFileID))
1272           FileCoverage.Expansions.emplace_back(CR, Function);
1273       }
1274     // Capture branch regions specific to the function (excluding expansions).
1275     for (const auto &CR : Function.CountedBranchRegions)
1276       if (FileIDs.test(CR.FileID) && (CR.FileID == CR.ExpandedFileID))
1277         FileCoverage.BranchRegions.push_back(CR);
1278     // Capture MCDC records specific to the function.
1279     for (const auto &MR : Function.MCDCRecords)
1280       if (FileIDs.test(MR.getDecisionRegion().FileID))
1281         FileCoverage.MCDCRecords.push_back(MR);
1282   }
1283 
1284   LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
1285   FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
1286 
1287   return FileCoverage;
1288 }
1289 
1290 std::vector<InstantiationGroup>
1291 CoverageMapping::getInstantiationGroups(StringRef Filename) const {
1292   FunctionInstantiationSetCollector InstantiationSetCollector;
1293   // Look up the function records in the given file. Due to hash collisions on
1294   // the filename, we may get back some records that are not in the file.
1295   ArrayRef<unsigned> RecordIndices =
1296       getImpreciseRecordIndicesForFilename(Filename);
1297   for (unsigned RecordIndex : RecordIndices) {
1298     const FunctionRecord &Function = Functions[RecordIndex];
1299     auto MainFileID = findMainViewFileID(Filename, Function);
1300     if (!MainFileID)
1301       continue;
1302     InstantiationSetCollector.insert(Function, *MainFileID);
1303   }
1304 
1305   std::vector<InstantiationGroup> Result;
1306   for (auto &InstantiationSet : InstantiationSetCollector) {
1307     InstantiationGroup IG{InstantiationSet.first.first,
1308                           InstantiationSet.first.second,
1309                           std::move(InstantiationSet.second)};
1310     Result.emplace_back(std::move(IG));
1311   }
1312   return Result;
1313 }
1314 
1315 CoverageData
1316 CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const {
1317   auto MainFileID = findMainViewFileID(Function);
1318   if (!MainFileID)
1319     return CoverageData();
1320 
1321   CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
1322   std::vector<CountedRegion> Regions;
1323   for (const auto &CR : Function.CountedRegions)
1324     if (CR.FileID == *MainFileID) {
1325       Regions.push_back(CR);
1326       if (isExpansion(CR, *MainFileID))
1327         FunctionCoverage.Expansions.emplace_back(CR, Function);
1328     }
1329   // Capture branch regions specific to the function (excluding expansions).
1330   for (const auto &CR : Function.CountedBranchRegions)
1331     if (CR.FileID == *MainFileID)
1332       FunctionCoverage.BranchRegions.push_back(CR);
1333 
1334   // Capture MCDC records specific to the function.
1335   for (const auto &MR : Function.MCDCRecords)
1336     if (MR.getDecisionRegion().FileID == *MainFileID)
1337       FunctionCoverage.MCDCRecords.push_back(MR);
1338 
1339   LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name
1340                     << "\n");
1341   FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
1342 
1343   return FunctionCoverage;
1344 }
1345 
1346 CoverageData CoverageMapping::getCoverageForExpansion(
1347     const ExpansionRecord &Expansion) const {
1348   CoverageData ExpansionCoverage(
1349       Expansion.Function.Filenames[Expansion.FileID]);
1350   std::vector<CountedRegion> Regions;
1351   for (const auto &CR : Expansion.Function.CountedRegions)
1352     if (CR.FileID == Expansion.FileID) {
1353       Regions.push_back(CR);
1354       if (isExpansion(CR, Expansion.FileID))
1355         ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
1356     }
1357   for (const auto &CR : Expansion.Function.CountedBranchRegions)
1358     // Capture branch regions that only pertain to the corresponding expansion.
1359     if (CR.FileID == Expansion.FileID)
1360       ExpansionCoverage.BranchRegions.push_back(CR);
1361 
1362   LLVM_DEBUG(dbgs() << "Emitting segments for expansion of file "
1363                     << Expansion.FileID << "\n");
1364   ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
1365 
1366   return ExpansionCoverage;
1367 }
1368 
1369 LineCoverageStats::LineCoverageStats(
1370     ArrayRef<const CoverageSegment *> LineSegments,
1371     const CoverageSegment *WrappedSegment, unsigned Line)
1372     : ExecutionCount(0), HasMultipleRegions(false), Mapped(false), Line(Line),
1373       LineSegments(LineSegments), WrappedSegment(WrappedSegment) {
1374   // Find the minimum number of regions which start in this line.
1375   unsigned MinRegionCount = 0;
1376   auto isStartOfRegion = [](const CoverageSegment *S) {
1377     return !S->IsGapRegion && S->HasCount && S->IsRegionEntry;
1378   };
1379   for (unsigned I = 0; I < LineSegments.size() && MinRegionCount < 2; ++I)
1380     if (isStartOfRegion(LineSegments[I]))
1381       ++MinRegionCount;
1382 
1383   bool StartOfSkippedRegion = !LineSegments.empty() &&
1384                               !LineSegments.front()->HasCount &&
1385                               LineSegments.front()->IsRegionEntry;
1386 
1387   HasMultipleRegions = MinRegionCount > 1;
1388   Mapped =
1389       !StartOfSkippedRegion &&
1390       ((WrappedSegment && WrappedSegment->HasCount) || (MinRegionCount > 0));
1391 
1392   // if there is any starting segment at this line with a counter, it must be
1393   // mapped
1394   Mapped |= std::any_of(
1395       LineSegments.begin(), LineSegments.end(),
1396       [](const auto *Seq) { return Seq->IsRegionEntry && Seq->HasCount; });
1397 
1398   if (!Mapped) {
1399     return;
1400   }
1401 
1402   // Pick the max count from the non-gap, region entry segments and the
1403   // wrapped count.
1404   if (WrappedSegment)
1405     ExecutionCount = WrappedSegment->Count;
1406   if (!MinRegionCount)
1407     return;
1408   for (const auto *LS : LineSegments)
1409     if (isStartOfRegion(LS))
1410       ExecutionCount = std::max(ExecutionCount, LS->Count);
1411 }
1412 
1413 LineCoverageIterator &LineCoverageIterator::operator++() {
1414   if (Next == CD.end()) {
1415     Stats = LineCoverageStats();
1416     Ended = true;
1417     return *this;
1418   }
1419   if (Segments.size())
1420     WrappedSegment = Segments.back();
1421   Segments.clear();
1422   while (Next != CD.end() && Next->Line == Line)
1423     Segments.push_back(&*Next++);
1424   Stats = LineCoverageStats(Segments, WrappedSegment, Line);
1425   ++Line;
1426   return *this;
1427 }
1428 
1429 static std::string getCoverageMapErrString(coveragemap_error Err,
1430                                            const std::string &ErrMsg = "") {
1431   std::string Msg;
1432   raw_string_ostream OS(Msg);
1433 
1434   switch (Err) {
1435   case coveragemap_error::success:
1436     OS << "success";
1437     break;
1438   case coveragemap_error::eof:
1439     OS << "end of File";
1440     break;
1441   case coveragemap_error::no_data_found:
1442     OS << "no coverage data found";
1443     break;
1444   case coveragemap_error::unsupported_version:
1445     OS << "unsupported coverage format version";
1446     break;
1447   case coveragemap_error::truncated:
1448     OS << "truncated coverage data";
1449     break;
1450   case coveragemap_error::malformed:
1451     OS << "malformed coverage data";
1452     break;
1453   case coveragemap_error::decompression_failed:
1454     OS << "failed to decompress coverage data (zlib)";
1455     break;
1456   case coveragemap_error::invalid_or_missing_arch_specifier:
1457     OS << "`-arch` specifier is invalid or missing for universal binary";
1458     break;
1459   }
1460 
1461   // If optional error message is not empty, append it to the message.
1462   if (!ErrMsg.empty())
1463     OS << ": " << ErrMsg;
1464 
1465   return Msg;
1466 }
1467 
1468 namespace {
1469 
1470 // FIXME: This class is only here to support the transition to llvm::Error. It
1471 // will be removed once this transition is complete. Clients should prefer to
1472 // deal with the Error value directly, rather than converting to error_code.
1473 class CoverageMappingErrorCategoryType : public std::error_category {
1474   const char *name() const noexcept override { return "llvm.coveragemap"; }
1475   std::string message(int IE) const override {
1476     return getCoverageMapErrString(static_cast<coveragemap_error>(IE));
1477   }
1478 };
1479 
1480 } // end anonymous namespace
1481 
1482 std::string CoverageMapError::message() const {
1483   return getCoverageMapErrString(Err, Msg);
1484 }
1485 
1486 const std::error_category &llvm::coverage::coveragemap_category() {
1487   static CoverageMappingErrorCategoryType ErrorCategory;
1488   return ErrorCategory;
1489 }
1490 
1491 char CoverageMapError::ID = 0;
1492