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