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