xref: /llvm-project/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp (revision b53e0d1b341f142e6d27e53058141d283837fbbd)
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/SmallBitVector.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
21 #include "llvm/ProfileData/InstrProfReader.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/Errc.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <algorithm>
29 #include <cassert>
30 #include <cstdint>
31 #include <iterator>
32 #include <map>
33 #include <memory>
34 #include <optional>
35 #include <string>
36 #include <system_error>
37 #include <utility>
38 #include <vector>
39 
40 using namespace llvm;
41 using namespace coverage;
42 
43 #define DEBUG_TYPE "coverage-mapping"
44 
45 Counter CounterExpressionBuilder::get(const CounterExpression &E) {
46   auto It = ExpressionIndices.find(E);
47   if (It != ExpressionIndices.end())
48     return Counter::getExpression(It->second);
49   unsigned I = Expressions.size();
50   Expressions.push_back(E);
51   ExpressionIndices[E] = I;
52   return Counter::getExpression(I);
53 }
54 
55 void CounterExpressionBuilder::extractTerms(Counter C, int Factor,
56                                             SmallVectorImpl<Term> &Terms) {
57   switch (C.getKind()) {
58   case Counter::Zero:
59     break;
60   case Counter::CounterValueReference:
61     Terms.emplace_back(C.getCounterID(), Factor);
62     break;
63   case Counter::Expression:
64     const auto &E = Expressions[C.getExpressionID()];
65     extractTerms(E.LHS, Factor, Terms);
66     extractTerms(
67         E.RHS, E.Kind == CounterExpression::Subtract ? -Factor : Factor, Terms);
68     break;
69   }
70 }
71 
72 Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
73   // Gather constant terms.
74   SmallVector<Term, 32> Terms;
75   extractTerms(ExpressionTree, +1, Terms);
76 
77   // If there are no terms, this is just a zero. The algorithm below assumes at
78   // least one term.
79   if (Terms.size() == 0)
80     return Counter::getZero();
81 
82   // Group the terms by counter ID.
83   llvm::sort(Terms, [](const Term &LHS, const Term &RHS) {
84     return LHS.CounterID < RHS.CounterID;
85   });
86 
87   // Combine terms by counter ID to eliminate counters that sum to zero.
88   auto Prev = Terms.begin();
89   for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
90     if (I->CounterID == Prev->CounterID) {
91       Prev->Factor += I->Factor;
92       continue;
93     }
94     ++Prev;
95     *Prev = *I;
96   }
97   Terms.erase(++Prev, Terms.end());
98 
99   Counter C;
100   // Create additions. We do this before subtractions to avoid constructs like
101   // ((0 - X) + Y), as opposed to (Y - X).
102   for (auto T : Terms) {
103     if (T.Factor <= 0)
104       continue;
105     for (int I = 0; I < T.Factor; ++I)
106       if (C.isZero())
107         C = Counter::getCounter(T.CounterID);
108       else
109         C = get(CounterExpression(CounterExpression::Add, C,
110                                   Counter::getCounter(T.CounterID)));
111   }
112 
113   // Create subtractions.
114   for (auto T : Terms) {
115     if (T.Factor >= 0)
116       continue;
117     for (int I = 0; I < -T.Factor; ++I)
118       C = get(CounterExpression(CounterExpression::Subtract, C,
119                                 Counter::getCounter(T.CounterID)));
120   }
121   return C;
122 }
123 
124 Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS, bool Simplify) {
125   auto Cnt = get(CounterExpression(CounterExpression::Add, LHS, RHS));
126   return Simplify ? simplify(Cnt) : Cnt;
127 }
128 
129 Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
130                                            bool Simplify) {
131   auto Cnt = get(CounterExpression(CounterExpression::Subtract, LHS, RHS));
132   return Simplify ? simplify(Cnt) : Cnt;
133 }
134 
135 void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
136   switch (C.getKind()) {
137   case Counter::Zero:
138     OS << '0';
139     return;
140   case Counter::CounterValueReference:
141     OS << '#' << C.getCounterID();
142     break;
143   case Counter::Expression: {
144     if (C.getExpressionID() >= Expressions.size())
145       return;
146     const auto &E = Expressions[C.getExpressionID()];
147     OS << '(';
148     dump(E.LHS, OS);
149     OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
150     dump(E.RHS, OS);
151     OS << ')';
152     break;
153   }
154   }
155   if (CounterValues.empty())
156     return;
157   Expected<int64_t> Value = evaluate(C);
158   if (auto E = Value.takeError()) {
159     consumeError(std::move(E));
160     return;
161   }
162   OS << '[' << *Value << ']';
163 }
164 
165 Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
166   switch (C.getKind()) {
167   case Counter::Zero:
168     return 0;
169   case Counter::CounterValueReference:
170     if (C.getCounterID() >= CounterValues.size())
171       return errorCodeToError(errc::argument_out_of_domain);
172     return CounterValues[C.getCounterID()];
173   case Counter::Expression: {
174     if (C.getExpressionID() >= Expressions.size())
175       return errorCodeToError(errc::argument_out_of_domain);
176     const auto &E = Expressions[C.getExpressionID()];
177     Expected<int64_t> LHS = evaluate(E.LHS);
178     if (!LHS)
179       return LHS;
180     Expected<int64_t> RHS = evaluate(E.RHS);
181     if (!RHS)
182       return RHS;
183     return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
184   }
185   }
186   llvm_unreachable("Unhandled CounterKind");
187 }
188 
189 unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
190   switch (C.getKind()) {
191   case Counter::Zero:
192     return 0;
193   case Counter::CounterValueReference:
194     return C.getCounterID();
195   case Counter::Expression: {
196     if (C.getExpressionID() >= Expressions.size())
197       return 0;
198     const auto &E = Expressions[C.getExpressionID()];
199     return std::max(getMaxCounterID(E.LHS), getMaxCounterID(E.RHS));
200   }
201   }
202   llvm_unreachable("Unhandled CounterKind");
203 }
204 
205 void FunctionRecordIterator::skipOtherFiles() {
206   while (Current != Records.end() && !Filename.empty() &&
207          Filename != Current->Filenames[0])
208     ++Current;
209   if (Current == Records.end())
210     *this = FunctionRecordIterator();
211 }
212 
213 ArrayRef<unsigned> CoverageMapping::getImpreciseRecordIndicesForFilename(
214     StringRef Filename) const {
215   size_t FilenameHash = hash_value(Filename);
216   auto RecordIt = FilenameHash2RecordIndices.find(FilenameHash);
217   if (RecordIt == FilenameHash2RecordIndices.end())
218     return {};
219   return RecordIt->second;
220 }
221 
222 static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
223                                 const CoverageMappingRecord &Record) {
224   unsigned MaxCounterID = 0;
225   for (const auto &Region : Record.MappingRegions) {
226     MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
227   }
228   return MaxCounterID;
229 }
230 
231 Error CoverageMapping::loadFunctionRecord(
232     const CoverageMappingRecord &Record,
233     IndexedInstrProfReader &ProfileReader) {
234   StringRef OrigFuncName = Record.FunctionName;
235   if (OrigFuncName.empty())
236     return make_error<CoverageMapError>(coveragemap_error::malformed);
237 
238   if (Record.Filenames.empty())
239     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
240   else
241     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
242 
243   CounterMappingContext Ctx(Record.Expressions);
244 
245   std::vector<uint64_t> Counts;
246   if (Error E = ProfileReader.getFunctionCounts(Record.FunctionName,
247                                                 Record.FunctionHash, Counts)) {
248     instrprof_error IPE = InstrProfError::take(std::move(E));
249     if (IPE == instrprof_error::hash_mismatch) {
250       FuncHashMismatches.emplace_back(std::string(Record.FunctionName),
251                                       Record.FunctionHash);
252       return Error::success();
253     } else if (IPE != instrprof_error::unknown_function)
254       return make_error<InstrProfError>(IPE);
255     Counts.assign(getMaxCounterID(Ctx, Record) + 1, 0);
256   }
257   Ctx.setCounts(Counts);
258 
259   assert(!Record.MappingRegions.empty() && "Function has no regions");
260 
261   // This coverage record is a zero region for a function that's unused in
262   // some TU, but used in a different TU. Ignore it. The coverage maps from the
263   // the other TU will either be loaded (providing full region counts) or they
264   // won't (in which case we don't unintuitively report functions as uncovered
265   // when they have non-zero counts in the profile).
266   if (Record.MappingRegions.size() == 1 &&
267       Record.MappingRegions[0].Count.isZero() && Counts[0] > 0)
268     return Error::success();
269 
270   FunctionRecord Function(OrigFuncName, Record.Filenames);
271   for (const auto &Region : Record.MappingRegions) {
272     Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
273     if (auto E = ExecutionCount.takeError()) {
274       consumeError(std::move(E));
275       return Error::success();
276     }
277     Expected<int64_t> AltExecutionCount = Ctx.evaluate(Region.FalseCount);
278     if (auto E = AltExecutionCount.takeError()) {
279       consumeError(std::move(E));
280       return Error::success();
281     }
282     Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount);
283   }
284 
285   // Don't create records for (filenames, function) pairs we've already seen.
286   auto FilenamesHash = hash_combine_range(Record.Filenames.begin(),
287                                           Record.Filenames.end());
288   if (!RecordProvenance[FilenamesHash].insert(hash_value(OrigFuncName)).second)
289     return Error::success();
290 
291   Functions.push_back(std::move(Function));
292 
293   // Performance optimization: keep track of the indices of the function records
294   // which correspond to each filename. This can be used to substantially speed
295   // up queries for coverage info in a file.
296   unsigned RecordIndex = Functions.size() - 1;
297   for (StringRef Filename : Record.Filenames) {
298     auto &RecordIndices = FilenameHash2RecordIndices[hash_value(Filename)];
299     // Note that there may be duplicates in the filename set for a function
300     // record, because of e.g. macro expansions in the function in which both
301     // the macro and the function are defined in the same file.
302     if (RecordIndices.empty() || RecordIndices.back() != RecordIndex)
303       RecordIndices.push_back(RecordIndex);
304   }
305 
306   return Error::success();
307 }
308 
309 // This function is for memory optimization by shortening the lifetimes
310 // of CoverageMappingReader instances.
311 Error CoverageMapping::loadFromReaders(
312     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
313     IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
314   for (const auto &CoverageReader : CoverageReaders) {
315     for (auto RecordOrErr : *CoverageReader) {
316       if (Error E = RecordOrErr.takeError())
317         return E;
318       const auto &Record = *RecordOrErr;
319       if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader))
320         return E;
321     }
322   }
323   return Error::success();
324 }
325 
326 Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
327     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
328     IndexedInstrProfReader &ProfileReader) {
329   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
330   if (Error E = loadFromReaders(CoverageReaders, ProfileReader, *Coverage))
331     return std::move(E);
332   return std::move(Coverage);
333 }
334 
335 // If E is a no_data_found error, returns success. Otherwise returns E.
336 static Error handleMaybeNoDataFoundError(Error E) {
337   return handleErrors(
338       std::move(E), [](const CoverageMapError &CME) {
339         if (CME.get() == coveragemap_error::no_data_found)
340           return static_cast<Error>(Error::success());
341         return make_error<CoverageMapError>(CME.get());
342       });
343 }
344 
345 Expected<std::unique_ptr<CoverageMapping>>
346 CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
347                       StringRef ProfileFilename, ArrayRef<StringRef> Arches,
348                       StringRef CompilationDir) {
349   auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
350   if (Error E = ProfileReaderOrErr.takeError())
351     return createFileError(ProfileFilename, std::move(E));
352   auto ProfileReader = std::move(ProfileReaderOrErr.get());
353   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
354   bool DataFound = false;
355 
356   for (const auto &File : llvm::enumerate(ObjectFilenames)) {
357     auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(
358         File.value(), /*IsText=*/false, /*RequiresNullTerminator=*/false);
359     if (std::error_code EC = CovMappingBufOrErr.getError())
360       return createFileError(File.value(), errorCodeToError(EC));
361     StringRef Arch = Arches.empty() ? StringRef() : Arches[File.index()];
362     MemoryBufferRef CovMappingBufRef =
363         CovMappingBufOrErr.get()->getMemBufferRef();
364     SmallVector<std::unique_ptr<MemoryBuffer>, 4> Buffers;
365     auto CoverageReadersOrErr = BinaryCoverageReader::create(
366         CovMappingBufRef, Arch, Buffers, CompilationDir);
367     if (Error E = CoverageReadersOrErr.takeError()) {
368       E = handleMaybeNoDataFoundError(std::move(E));
369       if (E)
370         return createFileError(File.value(), std::move(E));
371       // E == success (originally a no_data_found error).
372       continue;
373     }
374 
375     SmallVector<std::unique_ptr<CoverageMappingReader>, 4> Readers;
376     for (auto &Reader : CoverageReadersOrErr.get())
377       Readers.push_back(std::move(Reader));
378     DataFound |= !Readers.empty();
379     if (Error E = loadFromReaders(Readers, *ProfileReader, *Coverage))
380       return createFileError(File.value(), std::move(E));
381   }
382   // If no readers were created, either no objects were provided or none of them
383   // had coverage data. Return an error in the latter case.
384   if (!DataFound && !ObjectFilenames.empty())
385     return createFileError(
386         join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "),
387         make_error<CoverageMapError>(coveragemap_error::no_data_found));
388   return std::move(Coverage);
389 }
390 
391 namespace {
392 
393 /// Distributes functions into instantiation sets.
394 ///
395 /// An instantiation set is a collection of functions that have the same source
396 /// code, ie, template functions specializations.
397 class FunctionInstantiationSetCollector {
398   using MapT = std::map<LineColPair, std::vector<const FunctionRecord *>>;
399   MapT InstantiatedFunctions;
400 
401 public:
402   void insert(const FunctionRecord &Function, unsigned FileID) {
403     auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
404     while (I != E && I->FileID != FileID)
405       ++I;
406     assert(I != E && "function does not cover the given file");
407     auto &Functions = InstantiatedFunctions[I->startLoc()];
408     Functions.push_back(&Function);
409   }
410 
411   MapT::iterator begin() { return InstantiatedFunctions.begin(); }
412   MapT::iterator end() { return InstantiatedFunctions.end(); }
413 };
414 
415 class SegmentBuilder {
416   std::vector<CoverageSegment> &Segments;
417   SmallVector<const CountedRegion *, 8> ActiveRegions;
418 
419   SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {}
420 
421   /// Emit a segment with the count from \p Region starting at \p StartLoc.
422   //
423   /// \p IsRegionEntry: The segment is at the start of a new non-gap region.
424   /// \p EmitSkippedRegion: The segment must be emitted as a skipped region.
425   void startSegment(const CountedRegion &Region, LineColPair StartLoc,
426                     bool IsRegionEntry, bool EmitSkippedRegion = false) {
427     bool HasCount = !EmitSkippedRegion &&
428                     (Region.Kind != CounterMappingRegion::SkippedRegion);
429 
430     // If the new segment wouldn't affect coverage rendering, skip it.
431     if (!Segments.empty() && !IsRegionEntry && !EmitSkippedRegion) {
432       const auto &Last = Segments.back();
433       if (Last.HasCount == HasCount && Last.Count == Region.ExecutionCount &&
434           !Last.IsRegionEntry)
435         return;
436     }
437 
438     if (HasCount)
439       Segments.emplace_back(StartLoc.first, StartLoc.second,
440                             Region.ExecutionCount, IsRegionEntry,
441                             Region.Kind == CounterMappingRegion::GapRegion);
442     else
443       Segments.emplace_back(StartLoc.first, StartLoc.second, IsRegionEntry);
444 
445     LLVM_DEBUG({
446       const auto &Last = Segments.back();
447       dbgs() << "Segment at " << Last.Line << ":" << Last.Col
448              << " (count = " << Last.Count << ")"
449              << (Last.IsRegionEntry ? ", RegionEntry" : "")
450              << (!Last.HasCount ? ", Skipped" : "")
451              << (Last.IsGapRegion ? ", Gap" : "") << "\n";
452     });
453   }
454 
455   /// Emit segments for active regions which end before \p Loc.
456   ///
457   /// \p Loc: The start location of the next region. If std::nullopt, all active
458   /// regions are completed.
459   /// \p FirstCompletedRegion: Index of the first completed region.
460   void completeRegionsUntil(std::optional<LineColPair> Loc,
461                             unsigned FirstCompletedRegion) {
462     // Sort the completed regions by end location. This makes it simple to
463     // emit closing segments in sorted order.
464     auto CompletedRegionsIt = ActiveRegions.begin() + FirstCompletedRegion;
465     std::stable_sort(CompletedRegionsIt, ActiveRegions.end(),
466                       [](const CountedRegion *L, const CountedRegion *R) {
467                         return L->endLoc() < R->endLoc();
468                       });
469 
470     // Emit segments for all completed regions.
471     for (unsigned I = FirstCompletedRegion + 1, E = ActiveRegions.size(); I < E;
472          ++I) {
473       const auto *CompletedRegion = ActiveRegions[I];
474       assert((!Loc || CompletedRegion->endLoc() <= *Loc) &&
475              "Completed region ends after start of new region");
476 
477       const auto *PrevCompletedRegion = ActiveRegions[I - 1];
478       auto CompletedSegmentLoc = PrevCompletedRegion->endLoc();
479 
480       // Don't emit any more segments if they start where the new region begins.
481       if (Loc && CompletedSegmentLoc == *Loc)
482         break;
483 
484       // Don't emit a segment if the next completed region ends at the same
485       // location as this one.
486       if (CompletedSegmentLoc == CompletedRegion->endLoc())
487         continue;
488 
489       // Use the count from the last completed region which ends at this loc.
490       for (unsigned J = I + 1; J < E; ++J)
491         if (CompletedRegion->endLoc() == ActiveRegions[J]->endLoc())
492           CompletedRegion = ActiveRegions[J];
493 
494       startSegment(*CompletedRegion, CompletedSegmentLoc, false);
495     }
496 
497     auto Last = ActiveRegions.back();
498     if (FirstCompletedRegion && Last->endLoc() != *Loc) {
499       // If there's a gap after the end of the last completed region and the
500       // start of the new region, use the last active region to fill the gap.
501       startSegment(*ActiveRegions[FirstCompletedRegion - 1], Last->endLoc(),
502                    false);
503     } else if (!FirstCompletedRegion && (!Loc || *Loc != Last->endLoc())) {
504       // Emit a skipped segment if there are no more active regions. This
505       // ensures that gaps between functions are marked correctly.
506       startSegment(*Last, Last->endLoc(), false, true);
507     }
508 
509     // Pop the completed regions.
510     ActiveRegions.erase(CompletedRegionsIt, ActiveRegions.end());
511   }
512 
513   void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) {
514     for (const auto &CR : enumerate(Regions)) {
515       auto CurStartLoc = CR.value().startLoc();
516 
517       // Active regions which end before the current region need to be popped.
518       auto CompletedRegions =
519           std::stable_partition(ActiveRegions.begin(), ActiveRegions.end(),
520                                 [&](const CountedRegion *Region) {
521                                   return !(Region->endLoc() <= CurStartLoc);
522                                 });
523       if (CompletedRegions != ActiveRegions.end()) {
524         unsigned FirstCompletedRegion =
525             std::distance(ActiveRegions.begin(), CompletedRegions);
526         completeRegionsUntil(CurStartLoc, FirstCompletedRegion);
527       }
528 
529       bool GapRegion = CR.value().Kind == CounterMappingRegion::GapRegion;
530 
531       // Try to emit a segment for the current region.
532       if (CurStartLoc == CR.value().endLoc()) {
533         // Avoid making zero-length regions active. If it's the last region,
534         // emit a skipped segment. Otherwise use its predecessor's count.
535         const bool Skipped =
536             (CR.index() + 1) == Regions.size() ||
537             CR.value().Kind == CounterMappingRegion::SkippedRegion;
538         startSegment(ActiveRegions.empty() ? CR.value() : *ActiveRegions.back(),
539                      CurStartLoc, !GapRegion, Skipped);
540         // If it is skipped segment, create a segment with last pushed
541         // regions's count at CurStartLoc.
542         if (Skipped && !ActiveRegions.empty())
543           startSegment(*ActiveRegions.back(), CurStartLoc, false);
544         continue;
545       }
546       if (CR.index() + 1 == Regions.size() ||
547           CurStartLoc != Regions[CR.index() + 1].startLoc()) {
548         // Emit a segment if the next region doesn't start at the same location
549         // as this one.
550         startSegment(CR.value(), CurStartLoc, !GapRegion);
551       }
552 
553       // This region is active (i.e not completed).
554       ActiveRegions.push_back(&CR.value());
555     }
556 
557     // Complete any remaining active regions.
558     if (!ActiveRegions.empty())
559       completeRegionsUntil(std::nullopt, 0);
560   }
561 
562   /// Sort a nested sequence of regions from a single file.
563   static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) {
564     llvm::sort(Regions, [](const CountedRegion &LHS, const CountedRegion &RHS) {
565       if (LHS.startLoc() != RHS.startLoc())
566         return LHS.startLoc() < RHS.startLoc();
567       if (LHS.endLoc() != RHS.endLoc())
568         // When LHS completely contains RHS, we sort LHS first.
569         return RHS.endLoc() < LHS.endLoc();
570       // If LHS and RHS cover the same area, we need to sort them according
571       // to their kinds so that the most suitable region will become "active"
572       // in combineRegions(). Because we accumulate counter values only from
573       // regions of the same kind as the first region of the area, prefer
574       // CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion.
575       static_assert(CounterMappingRegion::CodeRegion <
576                             CounterMappingRegion::ExpansionRegion &&
577                         CounterMappingRegion::ExpansionRegion <
578                             CounterMappingRegion::SkippedRegion,
579                     "Unexpected order of region kind values");
580       return LHS.Kind < RHS.Kind;
581     });
582   }
583 
584   /// Combine counts of regions which cover the same area.
585   static ArrayRef<CountedRegion>
586   combineRegions(MutableArrayRef<CountedRegion> Regions) {
587     if (Regions.empty())
588       return Regions;
589     auto Active = Regions.begin();
590     auto End = Regions.end();
591     for (auto I = Regions.begin() + 1; I != End; ++I) {
592       if (Active->startLoc() != I->startLoc() ||
593           Active->endLoc() != I->endLoc()) {
594         // Shift to the next region.
595         ++Active;
596         if (Active != I)
597           *Active = *I;
598         continue;
599       }
600       // Merge duplicate region.
601       // If CodeRegions and ExpansionRegions cover the same area, it's probably
602       // a macro which is fully expanded to another macro. In that case, we need
603       // to accumulate counts only from CodeRegions, or else the area will be
604       // counted twice.
605       // On the other hand, a macro may have a nested macro in its body. If the
606       // outer macro is used several times, the ExpansionRegion for the nested
607       // macro will also be added several times. These ExpansionRegions cover
608       // the same source locations and have to be combined to reach the correct
609       // value for that area.
610       // We add counts of the regions of the same kind as the active region
611       // to handle the both situations.
612       if (I->Kind == Active->Kind)
613         Active->ExecutionCount += I->ExecutionCount;
614     }
615     return Regions.drop_back(std::distance(++Active, End));
616   }
617 
618 public:
619   /// Build a sorted list of CoverageSegments from a list of Regions.
620   static std::vector<CoverageSegment>
621   buildSegments(MutableArrayRef<CountedRegion> Regions) {
622     std::vector<CoverageSegment> Segments;
623     SegmentBuilder Builder(Segments);
624 
625     sortNestedRegions(Regions);
626     ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
627 
628     LLVM_DEBUG({
629       dbgs() << "Combined regions:\n";
630       for (const auto &CR : CombinedRegions)
631         dbgs() << "  " << CR.LineStart << ":" << CR.ColumnStart << " -> "
632                << CR.LineEnd << ":" << CR.ColumnEnd
633                << " (count=" << CR.ExecutionCount << ")\n";
634     });
635 
636     Builder.buildSegmentsImpl(CombinedRegions);
637 
638 #ifndef NDEBUG
639     for (unsigned I = 1, E = Segments.size(); I < E; ++I) {
640       const auto &L = Segments[I - 1];
641       const auto &R = Segments[I];
642       if (!(L.Line < R.Line) && !(L.Line == R.Line && L.Col < R.Col)) {
643         if (L.Line == R.Line && L.Col == R.Col && !L.HasCount)
644           continue;
645         LLVM_DEBUG(dbgs() << " ! Segment " << L.Line << ":" << L.Col
646                           << " followed by " << R.Line << ":" << R.Col << "\n");
647         assert(false && "Coverage segments not unique or sorted");
648       }
649     }
650 #endif
651 
652     return Segments;
653   }
654 };
655 
656 } // end anonymous namespace
657 
658 std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
659   std::vector<StringRef> Filenames;
660   for (const auto &Function : getCoveredFunctions())
661     llvm::append_range(Filenames, Function.Filenames);
662   llvm::sort(Filenames);
663   auto Last = std::unique(Filenames.begin(), Filenames.end());
664   Filenames.erase(Last, Filenames.end());
665   return Filenames;
666 }
667 
668 static SmallBitVector gatherFileIDs(StringRef SourceFile,
669                                     const FunctionRecord &Function) {
670   SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
671   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
672     if (SourceFile == Function.Filenames[I])
673       FilenameEquivalence[I] = true;
674   return FilenameEquivalence;
675 }
676 
677 /// Return the ID of the file where the definition of the function is located.
678 static std::optional<unsigned>
679 findMainViewFileID(const FunctionRecord &Function) {
680   SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
681   for (const auto &CR : Function.CountedRegions)
682     if (CR.Kind == CounterMappingRegion::ExpansionRegion)
683       IsNotExpandedFile[CR.ExpandedFileID] = false;
684   int I = IsNotExpandedFile.find_first();
685   if (I == -1)
686     return std::nullopt;
687   return I;
688 }
689 
690 /// Check if SourceFile is the file that contains the definition of
691 /// the Function. Return the ID of the file in that case or std::nullopt
692 /// otherwise.
693 static std::optional<unsigned>
694 findMainViewFileID(StringRef SourceFile, const FunctionRecord &Function) {
695   std::optional<unsigned> I = findMainViewFileID(Function);
696   if (I && SourceFile == Function.Filenames[*I])
697     return I;
698   return std::nullopt;
699 }
700 
701 static bool isExpansion(const CountedRegion &R, unsigned FileID) {
702   return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
703 }
704 
705 CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
706   CoverageData FileCoverage(Filename);
707   std::vector<CountedRegion> Regions;
708 
709   // Look up the function records in the given file. Due to hash collisions on
710   // the filename, we may get back some records that are not in the file.
711   ArrayRef<unsigned> RecordIndices =
712       getImpreciseRecordIndicesForFilename(Filename);
713   for (unsigned RecordIndex : RecordIndices) {
714     const FunctionRecord &Function = Functions[RecordIndex];
715     auto MainFileID = findMainViewFileID(Filename, Function);
716     auto FileIDs = gatherFileIDs(Filename, Function);
717     for (const auto &CR : Function.CountedRegions)
718       if (FileIDs.test(CR.FileID)) {
719         Regions.push_back(CR);
720         if (MainFileID && isExpansion(CR, *MainFileID))
721           FileCoverage.Expansions.emplace_back(CR, Function);
722       }
723     // Capture branch regions specific to the function (excluding expansions).
724     for (const auto &CR : Function.CountedBranchRegions)
725       if (FileIDs.test(CR.FileID) && (CR.FileID == CR.ExpandedFileID))
726         FileCoverage.BranchRegions.push_back(CR);
727   }
728 
729   LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
730   FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
731 
732   return FileCoverage;
733 }
734 
735 std::vector<InstantiationGroup>
736 CoverageMapping::getInstantiationGroups(StringRef Filename) const {
737   FunctionInstantiationSetCollector InstantiationSetCollector;
738   // Look up the function records in the given file. Due to hash collisions on
739   // the filename, we may get back some records that are not in the file.
740   ArrayRef<unsigned> RecordIndices =
741       getImpreciseRecordIndicesForFilename(Filename);
742   for (unsigned RecordIndex : RecordIndices) {
743     const FunctionRecord &Function = Functions[RecordIndex];
744     auto MainFileID = findMainViewFileID(Filename, Function);
745     if (!MainFileID)
746       continue;
747     InstantiationSetCollector.insert(Function, *MainFileID);
748   }
749 
750   std::vector<InstantiationGroup> Result;
751   for (auto &InstantiationSet : InstantiationSetCollector) {
752     InstantiationGroup IG{InstantiationSet.first.first,
753                           InstantiationSet.first.second,
754                           std::move(InstantiationSet.second)};
755     Result.emplace_back(std::move(IG));
756   }
757   return Result;
758 }
759 
760 CoverageData
761 CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const {
762   auto MainFileID = findMainViewFileID(Function);
763   if (!MainFileID)
764     return CoverageData();
765 
766   CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
767   std::vector<CountedRegion> Regions;
768   for (const auto &CR : Function.CountedRegions)
769     if (CR.FileID == *MainFileID) {
770       Regions.push_back(CR);
771       if (isExpansion(CR, *MainFileID))
772         FunctionCoverage.Expansions.emplace_back(CR, Function);
773     }
774   // Capture branch regions specific to the function (excluding expansions).
775   for (const auto &CR : Function.CountedBranchRegions)
776     if (CR.FileID == *MainFileID)
777       FunctionCoverage.BranchRegions.push_back(CR);
778 
779   LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name
780                     << "\n");
781   FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
782 
783   return FunctionCoverage;
784 }
785 
786 CoverageData CoverageMapping::getCoverageForExpansion(
787     const ExpansionRecord &Expansion) const {
788   CoverageData ExpansionCoverage(
789       Expansion.Function.Filenames[Expansion.FileID]);
790   std::vector<CountedRegion> Regions;
791   for (const auto &CR : Expansion.Function.CountedRegions)
792     if (CR.FileID == Expansion.FileID) {
793       Regions.push_back(CR);
794       if (isExpansion(CR, Expansion.FileID))
795         ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
796     }
797   for (const auto &CR : Expansion.Function.CountedBranchRegions)
798     // Capture branch regions that only pertain to the corresponding expansion.
799     if (CR.FileID == Expansion.FileID)
800       ExpansionCoverage.BranchRegions.push_back(CR);
801 
802   LLVM_DEBUG(dbgs() << "Emitting segments for expansion of file "
803                     << Expansion.FileID << "\n");
804   ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
805 
806   return ExpansionCoverage;
807 }
808 
809 LineCoverageStats::LineCoverageStats(
810     ArrayRef<const CoverageSegment *> LineSegments,
811     const CoverageSegment *WrappedSegment, unsigned Line)
812     : ExecutionCount(0), HasMultipleRegions(false), Mapped(false), Line(Line),
813       LineSegments(LineSegments), WrappedSegment(WrappedSegment) {
814   // Find the minimum number of regions which start in this line.
815   unsigned MinRegionCount = 0;
816   auto isStartOfRegion = [](const CoverageSegment *S) {
817     return !S->IsGapRegion && S->HasCount && S->IsRegionEntry;
818   };
819   for (unsigned I = 0; I < LineSegments.size() && MinRegionCount < 2; ++I)
820     if (isStartOfRegion(LineSegments[I]))
821       ++MinRegionCount;
822 
823   bool StartOfSkippedRegion = !LineSegments.empty() &&
824                               !LineSegments.front()->HasCount &&
825                               LineSegments.front()->IsRegionEntry;
826 
827   HasMultipleRegions = MinRegionCount > 1;
828   Mapped =
829       !StartOfSkippedRegion &&
830       ((WrappedSegment && WrappedSegment->HasCount) || (MinRegionCount > 0));
831 
832   if (!Mapped)
833     return;
834 
835   // Pick the max count from the non-gap, region entry segments and the
836   // wrapped count.
837   if (WrappedSegment)
838     ExecutionCount = WrappedSegment->Count;
839   if (!MinRegionCount)
840     return;
841   for (const auto *LS : LineSegments)
842     if (isStartOfRegion(LS))
843       ExecutionCount = std::max(ExecutionCount, LS->Count);
844 }
845 
846 LineCoverageIterator &LineCoverageIterator::operator++() {
847   if (Next == CD.end()) {
848     Stats = LineCoverageStats();
849     Ended = true;
850     return *this;
851   }
852   if (Segments.size())
853     WrappedSegment = Segments.back();
854   Segments.clear();
855   while (Next != CD.end() && Next->Line == Line)
856     Segments.push_back(&*Next++);
857   Stats = LineCoverageStats(Segments, WrappedSegment, Line);
858   ++Line;
859   return *this;
860 }
861 
862 static std::string getCoverageMapErrString(coveragemap_error Err) {
863   switch (Err) {
864   case coveragemap_error::success:
865     return "Success";
866   case coveragemap_error::eof:
867     return "End of File";
868   case coveragemap_error::no_data_found:
869     return "No coverage data found";
870   case coveragemap_error::unsupported_version:
871     return "Unsupported coverage format version";
872   case coveragemap_error::truncated:
873     return "Truncated coverage data";
874   case coveragemap_error::malformed:
875     return "Malformed coverage data";
876   case coveragemap_error::decompression_failed:
877     return "Failed to decompress coverage data (zlib)";
878   case coveragemap_error::invalid_or_missing_arch_specifier:
879     return "`-arch` specifier is invalid or missing for universal binary";
880   }
881   llvm_unreachable("A value of coveragemap_error has no message.");
882 }
883 
884 namespace {
885 
886 // FIXME: This class is only here to support the transition to llvm::Error. It
887 // will be removed once this transition is complete. Clients should prefer to
888 // deal with the Error value directly, rather than converting to error_code.
889 class CoverageMappingErrorCategoryType : public std::error_category {
890   const char *name() const noexcept override { return "llvm.coveragemap"; }
891   std::string message(int IE) const override {
892     return getCoverageMapErrString(static_cast<coveragemap_error>(IE));
893   }
894 };
895 
896 } // end anonymous namespace
897 
898 std::string CoverageMapError::message() const {
899   return getCoverageMapErrString(Err);
900 }
901 
902 const std::error_category &llvm::coverage::coveragemap_category() {
903   static CoverageMappingErrorCategoryType ErrorCategory;
904   return ErrorCategory;
905 }
906 
907 char CoverageMapError::ID = 0;
908