1 //===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Instrumentation-based code coverage mapping generator
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CoverageMappingGen.h"
15 #include "CodeGenFunction.h"
16 #include "clang/AST/StmtVisitor.h"
17 #include "clang/Lex/Lexer.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
22 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
23 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
24 #include "llvm/ProfileData/InstrProfReader.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 
28 using namespace clang;
29 using namespace CodeGen;
30 using namespace llvm::coverage;
31 
32 void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range, SourceLocation) {
33   SkippedRanges.push_back(Range);
34 }
35 
36 namespace {
37 
38 /// A region of source code that can be mapped to a counter.
39 class SourceMappingRegion {
40   Counter Count;
41 
42   /// The region's starting location.
43   Optional<SourceLocation> LocStart;
44 
45   /// The region's ending location.
46   Optional<SourceLocation> LocEnd;
47 
48   /// Whether this region should be emitted after its parent is emitted.
49   bool DeferRegion;
50 
51   /// Whether this region is a gap region. The count from a gap region is set
52   /// as the line execution count if there are no other regions on the line.
53   bool GapRegion;
54 
55 public:
56   SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart,
57                       Optional<SourceLocation> LocEnd, bool DeferRegion = false,
58                       bool GapRegion = false)
59       : Count(Count), LocStart(LocStart), LocEnd(LocEnd),
60         DeferRegion(DeferRegion), GapRegion(GapRegion) {}
61 
62   const Counter &getCounter() const { return Count; }
63 
64   void setCounter(Counter C) { Count = C; }
65 
66   bool hasStartLoc() const { return LocStart.hasValue(); }
67 
68   void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
69 
70   SourceLocation getBeginLoc() const {
71     assert(LocStart && "Region has no start location");
72     return *LocStart;
73   }
74 
75   bool hasEndLoc() const { return LocEnd.hasValue(); }
76 
77   void setEndLoc(SourceLocation Loc) {
78     assert(Loc.isValid() && "Setting an invalid end location");
79     LocEnd = Loc;
80   }
81 
82   SourceLocation getEndLoc() const {
83     assert(LocEnd && "Region has no end location");
84     return *LocEnd;
85   }
86 
87   bool isDeferred() const { return DeferRegion; }
88 
89   void setDeferred(bool Deferred) { DeferRegion = Deferred; }
90 
91   bool isGap() const { return GapRegion; }
92 
93   void setGap(bool Gap) { GapRegion = Gap; }
94 };
95 
96 /// Spelling locations for the start and end of a source region.
97 struct SpellingRegion {
98   /// The line where the region starts.
99   unsigned LineStart;
100 
101   /// The column where the region starts.
102   unsigned ColumnStart;
103 
104   /// The line where the region ends.
105   unsigned LineEnd;
106 
107   /// The column where the region ends.
108   unsigned ColumnEnd;
109 
110   SpellingRegion(SourceManager &SM, SourceLocation LocStart,
111                  SourceLocation LocEnd) {
112     LineStart = SM.getSpellingLineNumber(LocStart);
113     ColumnStart = SM.getSpellingColumnNumber(LocStart);
114     LineEnd = SM.getSpellingLineNumber(LocEnd);
115     ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
116   }
117 
118   SpellingRegion(SourceManager &SM, SourceMappingRegion &R)
119       : SpellingRegion(SM, R.getBeginLoc(), R.getEndLoc()) {}
120 
121   /// Check if the start and end locations appear in source order, i.e
122   /// top->bottom, left->right.
123   bool isInSourceOrder() const {
124     return (LineStart < LineEnd) ||
125            (LineStart == LineEnd && ColumnStart <= ColumnEnd);
126   }
127 };
128 
129 /// Provides the common functionality for the different
130 /// coverage mapping region builders.
131 class CoverageMappingBuilder {
132 public:
133   CoverageMappingModuleGen &CVM;
134   SourceManager &SM;
135   const LangOptions &LangOpts;
136 
137 private:
138   /// Map of clang's FileIDs to IDs used for coverage mapping.
139   llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
140       FileIDMapping;
141 
142 public:
143   /// The coverage mapping regions for this function
144   llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
145   /// The source mapping regions for this function.
146   std::vector<SourceMappingRegion> SourceRegions;
147 
148   /// A set of regions which can be used as a filter.
149   ///
150   /// It is produced by emitExpansionRegions() and is used in
151   /// emitSourceRegions() to suppress producing code regions if
152   /// the same area is covered by expansion regions.
153   typedef llvm::SmallSet<std::pair<SourceLocation, SourceLocation>, 8>
154       SourceRegionFilter;
155 
156   CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
157                          const LangOptions &LangOpts)
158       : CVM(CVM), SM(SM), LangOpts(LangOpts) {}
159 
160   /// Return the precise end location for the given token.
161   SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
162     // We avoid getLocForEndOfToken here, because it doesn't do what we want for
163     // macro locations, which we just treat as expanded files.
164     unsigned TokLen =
165         Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
166     return Loc.getLocWithOffset(TokLen);
167   }
168 
169   /// Return the start location of an included file or expanded macro.
170   SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
171     if (Loc.isMacroID())
172       return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
173     return SM.getLocForStartOfFile(SM.getFileID(Loc));
174   }
175 
176   /// Return the end location of an included file or expanded macro.
177   SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
178     if (Loc.isMacroID())
179       return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
180                                   SM.getFileOffset(Loc));
181     return SM.getLocForEndOfFile(SM.getFileID(Loc));
182   }
183 
184   /// Find out where the current file is included or macro is expanded.
185   SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
186     return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).getBegin()
187                            : SM.getIncludeLoc(SM.getFileID(Loc));
188   }
189 
190   /// Return true if \c Loc is a location in a built-in macro.
191   bool isInBuiltin(SourceLocation Loc) {
192     return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>";
193   }
194 
195   /// Check whether \c Loc is included or expanded from \c Parent.
196   bool isNestedIn(SourceLocation Loc, FileID Parent) {
197     do {
198       Loc = getIncludeOrExpansionLoc(Loc);
199       if (Loc.isInvalid())
200         return false;
201     } while (!SM.isInFileID(Loc, Parent));
202     return true;
203   }
204 
205   /// Get the start of \c S ignoring macro arguments and builtin macros.
206   SourceLocation getStart(const Stmt *S) {
207     SourceLocation Loc = S->getBeginLoc();
208     while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
209       Loc = SM.getImmediateExpansionRange(Loc).getBegin();
210     return Loc;
211   }
212 
213   /// Get the end of \c S ignoring macro arguments and builtin macros.
214   SourceLocation getEnd(const Stmt *S) {
215     SourceLocation Loc = S->getEndLoc();
216     while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
217       Loc = SM.getImmediateExpansionRange(Loc).getBegin();
218     return getPreciseTokenLocEnd(Loc);
219   }
220 
221   /// Find the set of files we have regions for and assign IDs
222   ///
223   /// Fills \c Mapping with the virtual file mapping needed to write out
224   /// coverage and collects the necessary file information to emit source and
225   /// expansion regions.
226   void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
227     FileIDMapping.clear();
228 
229     llvm::SmallSet<FileID, 8> Visited;
230     SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
231     for (const auto &Region : SourceRegions) {
232       SourceLocation Loc = Region.getBeginLoc();
233       FileID File = SM.getFileID(Loc);
234       if (!Visited.insert(File).second)
235         continue;
236 
237       // Do not map FileID's associated with system headers.
238       if (SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
239         continue;
240 
241       unsigned Depth = 0;
242       for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
243            Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent))
244         ++Depth;
245       FileLocs.push_back(std::make_pair(Loc, Depth));
246     }
247     std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second());
248 
249     for (const auto &FL : FileLocs) {
250       SourceLocation Loc = FL.first;
251       FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
252       auto Entry = SM.getFileEntryForID(SpellingFile);
253       if (!Entry)
254         continue;
255 
256       FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
257       Mapping.push_back(CVM.getFileID(Entry));
258     }
259   }
260 
261   /// Get the coverage mapping file ID for \c Loc.
262   ///
263   /// If such file id doesn't exist, return None.
264   Optional<unsigned> getCoverageFileID(SourceLocation Loc) {
265     auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
266     if (Mapping != FileIDMapping.end())
267       return Mapping->second.first;
268     return None;
269   }
270 
271   /// Gather all the regions that were skipped by the preprocessor
272   /// using the constructs like #if.
273   void gatherSkippedRegions() {
274     /// An array of the minimum lineStarts and the maximum lineEnds
275     /// for mapping regions from the appropriate source files.
276     llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
277     FileLineRanges.resize(
278         FileIDMapping.size(),
279         std::make_pair(std::numeric_limits<unsigned>::max(), 0));
280     for (const auto &R : MappingRegions) {
281       FileLineRanges[R.FileID].first =
282           std::min(FileLineRanges[R.FileID].first, R.LineStart);
283       FileLineRanges[R.FileID].second =
284           std::max(FileLineRanges[R.FileID].second, R.LineEnd);
285     }
286 
287     auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
288     for (const auto &I : SkippedRanges) {
289       auto LocStart = I.getBegin();
290       auto LocEnd = I.getEnd();
291       assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
292              "region spans multiple files");
293 
294       auto CovFileID = getCoverageFileID(LocStart);
295       if (!CovFileID)
296         continue;
297       SpellingRegion SR{SM, LocStart, LocEnd};
298       auto Region = CounterMappingRegion::makeSkipped(
299           *CovFileID, SR.LineStart, SR.ColumnStart, SR.LineEnd, SR.ColumnEnd);
300       // Make sure that we only collect the regions that are inside
301       // the source code of this function.
302       if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
303           Region.LineEnd <= FileLineRanges[*CovFileID].second)
304         MappingRegions.push_back(Region);
305     }
306   }
307 
308   /// Generate the coverage counter mapping regions from collected
309   /// source regions.
310   void emitSourceRegions(const SourceRegionFilter &Filter) {
311     for (const auto &Region : SourceRegions) {
312       assert(Region.hasEndLoc() && "incomplete region");
313 
314       SourceLocation LocStart = Region.getBeginLoc();
315       assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
316 
317       // Ignore regions from system headers.
318       if (SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
319         continue;
320 
321       auto CovFileID = getCoverageFileID(LocStart);
322       // Ignore regions that don't have a file, such as builtin macros.
323       if (!CovFileID)
324         continue;
325 
326       SourceLocation LocEnd = Region.getEndLoc();
327       assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
328              "region spans multiple files");
329 
330       // Don't add code regions for the area covered by expansion regions.
331       // This not only suppresses redundant regions, but sometimes prevents
332       // creating regions with wrong counters if, for example, a statement's
333       // body ends at the end of a nested macro.
334       if (Filter.count(std::make_pair(LocStart, LocEnd)))
335         continue;
336 
337       // Find the spelling locations for the mapping region.
338       SpellingRegion SR{SM, LocStart, LocEnd};
339       assert(SR.isInSourceOrder() && "region start and end out of order");
340 
341       if (Region.isGap()) {
342         MappingRegions.push_back(CounterMappingRegion::makeGapRegion(
343             Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
344             SR.LineEnd, SR.ColumnEnd));
345       } else {
346         MappingRegions.push_back(CounterMappingRegion::makeRegion(
347             Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
348             SR.LineEnd, SR.ColumnEnd));
349       }
350     }
351   }
352 
353   /// Generate expansion regions for each virtual file we've seen.
354   SourceRegionFilter emitExpansionRegions() {
355     SourceRegionFilter Filter;
356     for (const auto &FM : FileIDMapping) {
357       SourceLocation ExpandedLoc = FM.second.second;
358       SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
359       if (ParentLoc.isInvalid())
360         continue;
361 
362       auto ParentFileID = getCoverageFileID(ParentLoc);
363       if (!ParentFileID)
364         continue;
365       auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
366       assert(ExpandedFileID && "expansion in uncovered file");
367 
368       SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
369       assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
370              "region spans multiple files");
371       Filter.insert(std::make_pair(ParentLoc, LocEnd));
372 
373       SpellingRegion SR{SM, ParentLoc, LocEnd};
374       assert(SR.isInSourceOrder() && "region start and end out of order");
375       MappingRegions.push_back(CounterMappingRegion::makeExpansion(
376           *ParentFileID, *ExpandedFileID, SR.LineStart, SR.ColumnStart,
377           SR.LineEnd, SR.ColumnEnd));
378     }
379     return Filter;
380   }
381 };
382 
383 /// Creates unreachable coverage regions for the functions that
384 /// are not emitted.
385 struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
386   EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
387                               const LangOptions &LangOpts)
388       : CoverageMappingBuilder(CVM, SM, LangOpts) {}
389 
390   void VisitDecl(const Decl *D) {
391     if (!D->hasBody())
392       return;
393     auto Body = D->getBody();
394     SourceLocation Start = getStart(Body);
395     SourceLocation End = getEnd(Body);
396     if (!SM.isWrittenInSameFile(Start, End)) {
397       // Walk up to find the common ancestor.
398       // Correct the locations accordingly.
399       FileID StartFileID = SM.getFileID(Start);
400       FileID EndFileID = SM.getFileID(End);
401       while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) {
402         Start = getIncludeOrExpansionLoc(Start);
403         assert(Start.isValid() &&
404                "Declaration start location not nested within a known region");
405         StartFileID = SM.getFileID(Start);
406       }
407       while (StartFileID != EndFileID) {
408         End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
409         assert(End.isValid() &&
410                "Declaration end location not nested within a known region");
411         EndFileID = SM.getFileID(End);
412       }
413     }
414     SourceRegions.emplace_back(Counter(), Start, End);
415   }
416 
417   /// Write the mapping data to the output stream
418   void write(llvm::raw_ostream &OS) {
419     SmallVector<unsigned, 16> FileIDMapping;
420     gatherFileIDs(FileIDMapping);
421     emitSourceRegions(SourceRegionFilter());
422 
423     if (MappingRegions.empty())
424       return;
425 
426     CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions);
427     Writer.write(OS);
428   }
429 };
430 
431 /// A StmtVisitor that creates coverage mapping regions which map
432 /// from the source code locations to the PGO counters.
433 struct CounterCoverageMappingBuilder
434     : public CoverageMappingBuilder,
435       public ConstStmtVisitor<CounterCoverageMappingBuilder> {
436   /// The map of statements to count values.
437   llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
438 
439   /// A stack of currently live regions.
440   std::vector<SourceMappingRegion> RegionStack;
441 
442   /// The currently deferred region: its end location and count can be set once
443   /// its parent has been popped from the region stack.
444   Optional<SourceMappingRegion> DeferredRegion;
445 
446   CounterExpressionBuilder Builder;
447 
448   /// A location in the most recently visited file or macro.
449   ///
450   /// This is used to adjust the active source regions appropriately when
451   /// expressions cross file or macro boundaries.
452   SourceLocation MostRecentLocation;
453 
454   /// Location of the last terminated region.
455   Optional<std::pair<SourceLocation, size_t>> LastTerminatedRegion;
456 
457   /// Return a counter for the subtraction of \c RHS from \c LHS
458   Counter subtractCounters(Counter LHS, Counter RHS) {
459     return Builder.subtract(LHS, RHS);
460   }
461 
462   /// Return a counter for the sum of \c LHS and \c RHS.
463   Counter addCounters(Counter LHS, Counter RHS) {
464     return Builder.add(LHS, RHS);
465   }
466 
467   Counter addCounters(Counter C1, Counter C2, Counter C3) {
468     return addCounters(addCounters(C1, C2), C3);
469   }
470 
471   /// Return the region counter for the given statement.
472   ///
473   /// This should only be called on statements that have a dedicated counter.
474   Counter getRegionCounter(const Stmt *S) {
475     return Counter::getCounter(CounterMap[S]);
476   }
477 
478   /// Push a region onto the stack.
479   ///
480   /// Returns the index on the stack where the region was pushed. This can be
481   /// used with popRegions to exit a "scope", ending the region that was pushed.
482   size_t pushRegion(Counter Count, Optional<SourceLocation> StartLoc = None,
483                     Optional<SourceLocation> EndLoc = None) {
484     if (StartLoc) {
485       MostRecentLocation = *StartLoc;
486       completeDeferred(Count, MostRecentLocation);
487     }
488     RegionStack.emplace_back(Count, StartLoc, EndLoc);
489 
490     return RegionStack.size() - 1;
491   }
492 
493   /// Complete any pending deferred region by setting its end location and
494   /// count, and then pushing it onto the region stack.
495   size_t completeDeferred(Counter Count, SourceLocation DeferredEndLoc) {
496     size_t Index = RegionStack.size();
497     if (!DeferredRegion)
498       return Index;
499 
500     // Consume the pending region.
501     SourceMappingRegion DR = DeferredRegion.getValue();
502     DeferredRegion = None;
503 
504     // If the region ends in an expansion, find the expansion site.
505     FileID StartFile = SM.getFileID(DR.getBeginLoc());
506     if (SM.getFileID(DeferredEndLoc) != StartFile) {
507       if (isNestedIn(DeferredEndLoc, StartFile)) {
508         do {
509           DeferredEndLoc = getIncludeOrExpansionLoc(DeferredEndLoc);
510         } while (StartFile != SM.getFileID(DeferredEndLoc));
511       } else {
512         return Index;
513       }
514     }
515 
516     // The parent of this deferred region ends where the containing decl ends,
517     // so the region isn't useful.
518     if (DR.getBeginLoc() == DeferredEndLoc)
519       return Index;
520 
521     // If we're visiting statements in non-source order (e.g switch cases or
522     // a loop condition) we can't construct a sensible deferred region.
523     if (!SpellingRegion(SM, DR.getBeginLoc(), DeferredEndLoc).isInSourceOrder())
524       return Index;
525 
526     DR.setGap(true);
527     DR.setCounter(Count);
528     DR.setEndLoc(DeferredEndLoc);
529     handleFileExit(DeferredEndLoc);
530     RegionStack.push_back(DR);
531     return Index;
532   }
533 
534   /// Complete a deferred region created after a terminated region at the
535   /// top-level.
536   void completeTopLevelDeferredRegion(Counter Count,
537                                       SourceLocation DeferredEndLoc) {
538     if (DeferredRegion || !LastTerminatedRegion)
539       return;
540 
541     if (LastTerminatedRegion->second != RegionStack.size())
542       return;
543 
544     SourceLocation Start = LastTerminatedRegion->first;
545     if (SM.getFileID(Start) != SM.getMainFileID())
546       return;
547 
548     SourceMappingRegion DR = RegionStack.back();
549     DR.setStartLoc(Start);
550     DR.setDeferred(false);
551     DeferredRegion = DR;
552     completeDeferred(Count, DeferredEndLoc);
553   }
554 
555   size_t locationDepth(SourceLocation Loc) {
556     size_t Depth = 0;
557     while (Loc.isValid()) {
558       Loc = getIncludeOrExpansionLoc(Loc);
559       Depth++;
560     }
561     return Depth;
562   }
563 
564   /// Pop regions from the stack into the function's list of regions.
565   ///
566   /// Adds all regions from \c ParentIndex to the top of the stack to the
567   /// function's \c SourceRegions.
568   void popRegions(size_t ParentIndex) {
569     assert(RegionStack.size() >= ParentIndex && "parent not in stack");
570     bool ParentOfDeferredRegion = false;
571     while (RegionStack.size() > ParentIndex) {
572       SourceMappingRegion &Region = RegionStack.back();
573       if (Region.hasStartLoc()) {
574         SourceLocation StartLoc = Region.getBeginLoc();
575         SourceLocation EndLoc = Region.hasEndLoc()
576                                     ? Region.getEndLoc()
577                                     : RegionStack[ParentIndex].getEndLoc();
578         size_t StartDepth = locationDepth(StartLoc);
579         size_t EndDepth = locationDepth(EndLoc);
580         while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
581           bool UnnestStart = StartDepth >= EndDepth;
582           bool UnnestEnd = EndDepth >= StartDepth;
583           if (UnnestEnd) {
584             // The region ends in a nested file or macro expansion. Create a
585             // separate region for each expansion.
586             SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
587             assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
588 
589             if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
590               SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
591 
592             EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
593             if (EndLoc.isInvalid())
594               llvm::report_fatal_error("File exit not handled before popRegions");
595             EndDepth--;
596           }
597           if (UnnestStart) {
598             // The region begins in a nested file or macro expansion. Create a
599             // separate region for each expansion.
600             SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc);
601             assert(SM.isWrittenInSameFile(StartLoc, NestedLoc));
602 
603             if (!isRegionAlreadyAdded(StartLoc, NestedLoc))
604               SourceRegions.emplace_back(Region.getCounter(), StartLoc, NestedLoc);
605 
606             StartLoc = getIncludeOrExpansionLoc(StartLoc);
607             if (StartLoc.isInvalid())
608               llvm::report_fatal_error("File exit not handled before popRegions");
609             StartDepth--;
610           }
611         }
612         Region.setStartLoc(StartLoc);
613         Region.setEndLoc(EndLoc);
614 
615         MostRecentLocation = EndLoc;
616         // If this region happens to span an entire expansion, we need to make
617         // sure we don't overlap the parent region with it.
618         if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
619             EndLoc == getEndOfFileOrMacro(EndLoc))
620           MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
621 
622         assert(SM.isWrittenInSameFile(Region.getBeginLoc(), EndLoc));
623         assert(SpellingRegion(SM, Region).isInSourceOrder());
624         SourceRegions.push_back(Region);
625 
626         if (ParentOfDeferredRegion) {
627           ParentOfDeferredRegion = false;
628 
629           // If there's an existing deferred region, keep the old one, because
630           // it means there are two consecutive returns (or a similar pattern).
631           if (!DeferredRegion.hasValue() &&
632               // File IDs aren't gathered within macro expansions, so it isn't
633               // useful to try and create a deferred region inside of one.
634               !EndLoc.isMacroID())
635             DeferredRegion =
636                 SourceMappingRegion(Counter::getZero(), EndLoc, None);
637         }
638       } else if (Region.isDeferred()) {
639         assert(!ParentOfDeferredRegion && "Consecutive deferred regions");
640         ParentOfDeferredRegion = true;
641       }
642       RegionStack.pop_back();
643 
644       // If the zero region pushed after the last terminated region no longer
645       // exists, clear its cached information.
646       if (LastTerminatedRegion &&
647           RegionStack.size() < LastTerminatedRegion->second)
648         LastTerminatedRegion = None;
649     }
650     assert(!ParentOfDeferredRegion && "Deferred region with no parent");
651   }
652 
653   /// Return the currently active region.
654   SourceMappingRegion &getRegion() {
655     assert(!RegionStack.empty() && "statement has no region");
656     return RegionStack.back();
657   }
658 
659   /// Propagate counts through the children of \c S.
660   Counter propagateCounts(Counter TopCount, const Stmt *S) {
661     SourceLocation StartLoc = getStart(S);
662     SourceLocation EndLoc = getEnd(S);
663     size_t Index = pushRegion(TopCount, StartLoc, EndLoc);
664     Visit(S);
665     Counter ExitCount = getRegion().getCounter();
666     popRegions(Index);
667 
668     // The statement may be spanned by an expansion. Make sure we handle a file
669     // exit out of this expansion before moving to the next statement.
670     if (SM.isBeforeInTranslationUnit(StartLoc, S->getBeginLoc()))
671       MostRecentLocation = EndLoc;
672 
673     return ExitCount;
674   }
675 
676   /// Check whether a region with bounds \c StartLoc and \c EndLoc
677   /// is already added to \c SourceRegions.
678   bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc) {
679     return SourceRegions.rend() !=
680            std::find_if(SourceRegions.rbegin(), SourceRegions.rend(),
681                         [&](const SourceMappingRegion &Region) {
682                           return Region.getBeginLoc() == StartLoc &&
683                                  Region.getEndLoc() == EndLoc;
684                         });
685   }
686 
687   /// Adjust the most recently visited location to \c EndLoc.
688   ///
689   /// This should be used after visiting any statements in non-source order.
690   void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
691     MostRecentLocation = EndLoc;
692     // The code region for a whole macro is created in handleFileExit() when
693     // it detects exiting of the virtual file of that macro. If we visited
694     // statements in non-source order, we might already have such a region
695     // added, for example, if a body of a loop is divided among multiple
696     // macros. Avoid adding duplicate regions in such case.
697     if (getRegion().hasEndLoc() &&
698         MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) &&
699         isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
700                              MostRecentLocation))
701       MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
702   }
703 
704   /// Adjust regions and state when \c NewLoc exits a file.
705   ///
706   /// If moving from our most recently tracked location to \c NewLoc exits any
707   /// files, this adjusts our current region stack and creates the file regions
708   /// for the exited file.
709   void handleFileExit(SourceLocation NewLoc) {
710     if (NewLoc.isInvalid() ||
711         SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
712       return;
713 
714     // If NewLoc is not in a file that contains MostRecentLocation, walk up to
715     // find the common ancestor.
716     SourceLocation LCA = NewLoc;
717     FileID ParentFile = SM.getFileID(LCA);
718     while (!isNestedIn(MostRecentLocation, ParentFile)) {
719       LCA = getIncludeOrExpansionLoc(LCA);
720       if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
721         // Since there isn't a common ancestor, no file was exited. We just need
722         // to adjust our location to the new file.
723         MostRecentLocation = NewLoc;
724         return;
725       }
726       ParentFile = SM.getFileID(LCA);
727     }
728 
729     llvm::SmallSet<SourceLocation, 8> StartLocs;
730     Optional<Counter> ParentCounter;
731     for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
732       if (!I.hasStartLoc())
733         continue;
734       SourceLocation Loc = I.getBeginLoc();
735       if (!isNestedIn(Loc, ParentFile)) {
736         ParentCounter = I.getCounter();
737         break;
738       }
739 
740       while (!SM.isInFileID(Loc, ParentFile)) {
741         // The most nested region for each start location is the one with the
742         // correct count. We avoid creating redundant regions by stopping once
743         // we've seen this region.
744         if (StartLocs.insert(Loc).second)
745           SourceRegions.emplace_back(I.getCounter(), Loc,
746                                      getEndOfFileOrMacro(Loc));
747         Loc = getIncludeOrExpansionLoc(Loc);
748       }
749       I.setStartLoc(getPreciseTokenLocEnd(Loc));
750     }
751 
752     if (ParentCounter) {
753       // If the file is contained completely by another region and doesn't
754       // immediately start its own region, the whole file gets a region
755       // corresponding to the parent.
756       SourceLocation Loc = MostRecentLocation;
757       while (isNestedIn(Loc, ParentFile)) {
758         SourceLocation FileStart = getStartOfFileOrMacro(Loc);
759         if (StartLocs.insert(FileStart).second) {
760           SourceRegions.emplace_back(*ParentCounter, FileStart,
761                                      getEndOfFileOrMacro(Loc));
762           assert(SpellingRegion(SM, SourceRegions.back()).isInSourceOrder());
763         }
764         Loc = getIncludeOrExpansionLoc(Loc);
765       }
766     }
767 
768     MostRecentLocation = NewLoc;
769   }
770 
771   /// Ensure that \c S is included in the current region.
772   void extendRegion(const Stmt *S) {
773     SourceMappingRegion &Region = getRegion();
774     SourceLocation StartLoc = getStart(S);
775 
776     handleFileExit(StartLoc);
777     if (!Region.hasStartLoc())
778       Region.setStartLoc(StartLoc);
779 
780     completeDeferred(Region.getCounter(), StartLoc);
781   }
782 
783   /// Mark \c S as a terminator, starting a zero region.
784   void terminateRegion(const Stmt *S) {
785     extendRegion(S);
786     SourceMappingRegion &Region = getRegion();
787     SourceLocation EndLoc = getEnd(S);
788     if (!Region.hasEndLoc())
789       Region.setEndLoc(EndLoc);
790     pushRegion(Counter::getZero());
791     auto &ZeroRegion = getRegion();
792     ZeroRegion.setDeferred(true);
793     LastTerminatedRegion = {EndLoc, RegionStack.size()};
794   }
795 
796   /// Find a valid gap range between \p AfterLoc and \p BeforeLoc.
797   Optional<SourceRange> findGapAreaBetween(SourceLocation AfterLoc,
798                                            SourceLocation BeforeLoc) {
799     // If the start and end locations of the gap are both within the same macro
800     // file, the range may not be in source order.
801     if (AfterLoc.isMacroID() || BeforeLoc.isMacroID())
802       return None;
803     if (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc))
804       return None;
805     return {{AfterLoc, BeforeLoc}};
806   }
807 
808   /// Find the source range after \p AfterStmt and before \p BeforeStmt.
809   Optional<SourceRange> findGapAreaBetween(const Stmt *AfterStmt,
810                                            const Stmt *BeforeStmt) {
811     return findGapAreaBetween(getPreciseTokenLocEnd(getEnd(AfterStmt)),
812                               getStart(BeforeStmt));
813   }
814 
815   /// Emit a gap region between \p StartLoc and \p EndLoc with the given count.
816   void fillGapAreaWithCount(SourceLocation StartLoc, SourceLocation EndLoc,
817                             Counter Count) {
818     if (StartLoc == EndLoc)
819       return;
820     assert(SpellingRegion(SM, StartLoc, EndLoc).isInSourceOrder());
821     handleFileExit(StartLoc);
822     size_t Index = pushRegion(Count, StartLoc, EndLoc);
823     getRegion().setGap(true);
824     handleFileExit(EndLoc);
825     popRegions(Index);
826   }
827 
828   /// Keep counts of breaks and continues inside loops.
829   struct BreakContinue {
830     Counter BreakCount;
831     Counter ContinueCount;
832   };
833   SmallVector<BreakContinue, 8> BreakContinueStack;
834 
835   CounterCoverageMappingBuilder(
836       CoverageMappingModuleGen &CVM,
837       llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
838       const LangOptions &LangOpts)
839       : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
840         DeferredRegion(None) {}
841 
842   /// Write the mapping data to the output stream
843   void write(llvm::raw_ostream &OS) {
844     llvm::SmallVector<unsigned, 8> VirtualFileMapping;
845     gatherFileIDs(VirtualFileMapping);
846     SourceRegionFilter Filter = emitExpansionRegions();
847     assert(!DeferredRegion && "Deferred region never completed");
848     emitSourceRegions(Filter);
849     gatherSkippedRegions();
850 
851     if (MappingRegions.empty())
852       return;
853 
854     CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
855                                  MappingRegions);
856     Writer.write(OS);
857   }
858 
859   void VisitStmt(const Stmt *S) {
860     if (S->getBeginLoc().isValid())
861       extendRegion(S);
862     for (const Stmt *Child : S->children())
863       if (Child)
864         this->Visit(Child);
865     handleFileExit(getEnd(S));
866   }
867 
868   void VisitDecl(const Decl *D) {
869     assert(!DeferredRegion && "Deferred region never completed");
870 
871     Stmt *Body = D->getBody();
872 
873     // Do not propagate region counts into system headers.
874     if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
875       return;
876 
877     propagateCounts(getRegionCounter(Body), Body);
878     assert(RegionStack.empty() && "Regions entered but never exited");
879 
880     // Discard the last uncompleted deferred region in a decl, if one exists.
881     // This prevents lines at the end of a function containing only whitespace
882     // or closing braces from being marked as uncovered.
883     DeferredRegion = None;
884   }
885 
886   void VisitReturnStmt(const ReturnStmt *S) {
887     extendRegion(S);
888     if (S->getRetValue())
889       Visit(S->getRetValue());
890     terminateRegion(S);
891   }
892 
893   void VisitCXXThrowExpr(const CXXThrowExpr *E) {
894     extendRegion(E);
895     if (E->getSubExpr())
896       Visit(E->getSubExpr());
897     terminateRegion(E);
898   }
899 
900   void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
901 
902   void VisitLabelStmt(const LabelStmt *S) {
903     Counter LabelCount = getRegionCounter(S);
904     SourceLocation Start = getStart(S);
905     completeTopLevelDeferredRegion(LabelCount, Start);
906     completeDeferred(LabelCount, Start);
907     // We can't extendRegion here or we risk overlapping with our new region.
908     handleFileExit(Start);
909     pushRegion(LabelCount, Start);
910     Visit(S->getSubStmt());
911   }
912 
913   void VisitBreakStmt(const BreakStmt *S) {
914     assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
915     BreakContinueStack.back().BreakCount = addCounters(
916         BreakContinueStack.back().BreakCount, getRegion().getCounter());
917     // FIXME: a break in a switch should terminate regions for all preceding
918     // case statements, not just the most recent one.
919     terminateRegion(S);
920   }
921 
922   void VisitContinueStmt(const ContinueStmt *S) {
923     assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
924     BreakContinueStack.back().ContinueCount = addCounters(
925         BreakContinueStack.back().ContinueCount, getRegion().getCounter());
926     terminateRegion(S);
927   }
928 
929   void VisitCallExpr(const CallExpr *E) {
930     VisitStmt(E);
931 
932     // Terminate the region when we hit a noreturn function.
933     // (This is helpful dealing with switch statements.)
934     QualType CalleeType = E->getCallee()->getType();
935     if (getFunctionExtInfo(*CalleeType).getNoReturn())
936       terminateRegion(E);
937   }
938 
939   void VisitWhileStmt(const WhileStmt *S) {
940     extendRegion(S);
941 
942     Counter ParentCount = getRegion().getCounter();
943     Counter BodyCount = getRegionCounter(S);
944 
945     // Handle the body first so that we can get the backedge count.
946     BreakContinueStack.push_back(BreakContinue());
947     extendRegion(S->getBody());
948     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
949     BreakContinue BC = BreakContinueStack.pop_back_val();
950 
951     // Go back to handle the condition.
952     Counter CondCount =
953         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
954     propagateCounts(CondCount, S->getCond());
955     adjustForOutOfOrderTraversal(getEnd(S));
956 
957     // The body count applies to the area immediately after the increment.
958     auto Gap = findGapAreaBetween(S->getCond(), S->getBody());
959     if (Gap)
960       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
961 
962     Counter OutCount =
963         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
964     if (OutCount != ParentCount)
965       pushRegion(OutCount);
966   }
967 
968   void VisitDoStmt(const DoStmt *S) {
969     extendRegion(S);
970 
971     Counter ParentCount = getRegion().getCounter();
972     Counter BodyCount = getRegionCounter(S);
973 
974     BreakContinueStack.push_back(BreakContinue());
975     extendRegion(S->getBody());
976     Counter BackedgeCount =
977         propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
978     BreakContinue BC = BreakContinueStack.pop_back_val();
979 
980     Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
981     propagateCounts(CondCount, S->getCond());
982 
983     Counter OutCount =
984         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
985     if (OutCount != ParentCount)
986       pushRegion(OutCount);
987   }
988 
989   void VisitForStmt(const ForStmt *S) {
990     extendRegion(S);
991     if (S->getInit())
992       Visit(S->getInit());
993 
994     Counter ParentCount = getRegion().getCounter();
995     Counter BodyCount = getRegionCounter(S);
996 
997     // The loop increment may contain a break or continue.
998     if (S->getInc())
999       BreakContinueStack.emplace_back();
1000 
1001     // Handle the body first so that we can get the backedge count.
1002     BreakContinueStack.emplace_back();
1003     extendRegion(S->getBody());
1004     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1005     BreakContinue BodyBC = BreakContinueStack.pop_back_val();
1006 
1007     // The increment is essentially part of the body but it needs to include
1008     // the count for all the continue statements.
1009     BreakContinue IncrementBC;
1010     if (const Stmt *Inc = S->getInc()) {
1011       propagateCounts(addCounters(BackedgeCount, BodyBC.ContinueCount), Inc);
1012       IncrementBC = BreakContinueStack.pop_back_val();
1013     }
1014 
1015     // Go back to handle the condition.
1016     Counter CondCount = addCounters(
1017         addCounters(ParentCount, BackedgeCount, BodyBC.ContinueCount),
1018         IncrementBC.ContinueCount);
1019     if (const Expr *Cond = S->getCond()) {
1020       propagateCounts(CondCount, Cond);
1021       adjustForOutOfOrderTraversal(getEnd(S));
1022     }
1023 
1024     // The body count applies to the area immediately after the increment.
1025     auto Gap = findGapAreaBetween(getPreciseTokenLocEnd(S->getRParenLoc()),
1026                                   getStart(S->getBody()));
1027     if (Gap)
1028       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1029 
1030     Counter OutCount = addCounters(BodyBC.BreakCount, IncrementBC.BreakCount,
1031                                    subtractCounters(CondCount, BodyCount));
1032     if (OutCount != ParentCount)
1033       pushRegion(OutCount);
1034   }
1035 
1036   void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
1037     extendRegion(S);
1038     if (S->getInit())
1039       Visit(S->getInit());
1040     Visit(S->getLoopVarStmt());
1041     Visit(S->getRangeStmt());
1042 
1043     Counter ParentCount = getRegion().getCounter();
1044     Counter BodyCount = getRegionCounter(S);
1045 
1046     BreakContinueStack.push_back(BreakContinue());
1047     extendRegion(S->getBody());
1048     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1049     BreakContinue BC = BreakContinueStack.pop_back_val();
1050 
1051     // The body count applies to the area immediately after the range.
1052     auto Gap = findGapAreaBetween(getPreciseTokenLocEnd(S->getRParenLoc()),
1053                                   getStart(S->getBody()));
1054     if (Gap)
1055       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1056 
1057     Counter LoopCount =
1058         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1059     Counter OutCount =
1060         addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
1061     if (OutCount != ParentCount)
1062       pushRegion(OutCount);
1063   }
1064 
1065   void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
1066     extendRegion(S);
1067     Visit(S->getElement());
1068 
1069     Counter ParentCount = getRegion().getCounter();
1070     Counter BodyCount = getRegionCounter(S);
1071 
1072     BreakContinueStack.push_back(BreakContinue());
1073     extendRegion(S->getBody());
1074     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1075     BreakContinue BC = BreakContinueStack.pop_back_val();
1076 
1077     // The body count applies to the area immediately after the collection.
1078     auto Gap = findGapAreaBetween(getPreciseTokenLocEnd(S->getRParenLoc()),
1079                                   getStart(S->getBody()));
1080     if (Gap)
1081       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1082 
1083     Counter LoopCount =
1084         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1085     Counter OutCount =
1086         addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
1087     if (OutCount != ParentCount)
1088       pushRegion(OutCount);
1089   }
1090 
1091   void VisitSwitchStmt(const SwitchStmt *S) {
1092     extendRegion(S);
1093     if (S->getInit())
1094       Visit(S->getInit());
1095     Visit(S->getCond());
1096 
1097     BreakContinueStack.push_back(BreakContinue());
1098 
1099     const Stmt *Body = S->getBody();
1100     extendRegion(Body);
1101     if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
1102       if (!CS->body_empty()) {
1103         // Make a region for the body of the switch.  If the body starts with
1104         // a case, that case will reuse this region; otherwise, this covers
1105         // the unreachable code at the beginning of the switch body.
1106         size_t Index =
1107             pushRegion(Counter::getZero(), getStart(CS->body_front()));
1108         for (const auto *Child : CS->children())
1109           Visit(Child);
1110 
1111         // Set the end for the body of the switch, if it isn't already set.
1112         for (size_t i = RegionStack.size(); i != Index; --i) {
1113           if (!RegionStack[i - 1].hasEndLoc())
1114             RegionStack[i - 1].setEndLoc(getEnd(CS->body_back()));
1115         }
1116 
1117         popRegions(Index);
1118       }
1119     } else
1120       propagateCounts(Counter::getZero(), Body);
1121     BreakContinue BC = BreakContinueStack.pop_back_val();
1122 
1123     if (!BreakContinueStack.empty())
1124       BreakContinueStack.back().ContinueCount = addCounters(
1125           BreakContinueStack.back().ContinueCount, BC.ContinueCount);
1126 
1127     Counter ExitCount = getRegionCounter(S);
1128     SourceLocation ExitLoc = getEnd(S);
1129     pushRegion(ExitCount);
1130 
1131     // Ensure that handleFileExit recognizes when the end location is located
1132     // in a different file.
1133     MostRecentLocation = getStart(S);
1134     handleFileExit(ExitLoc);
1135   }
1136 
1137   void VisitSwitchCase(const SwitchCase *S) {
1138     extendRegion(S);
1139 
1140     SourceMappingRegion &Parent = getRegion();
1141 
1142     Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
1143     // Reuse the existing region if it starts at our label. This is typical of
1144     // the first case in a switch.
1145     if (Parent.hasStartLoc() && Parent.getBeginLoc() == getStart(S))
1146       Parent.setCounter(Count);
1147     else
1148       pushRegion(Count, getStart(S));
1149 
1150     if (const auto *CS = dyn_cast<CaseStmt>(S)) {
1151       Visit(CS->getLHS());
1152       if (const Expr *RHS = CS->getRHS())
1153         Visit(RHS);
1154     }
1155     Visit(S->getSubStmt());
1156   }
1157 
1158   void VisitIfStmt(const IfStmt *S) {
1159     extendRegion(S);
1160     if (S->getInit())
1161       Visit(S->getInit());
1162 
1163     // Extend into the condition before we propagate through it below - this is
1164     // needed to handle macros that generate the "if" but not the condition.
1165     extendRegion(S->getCond());
1166 
1167     Counter ParentCount = getRegion().getCounter();
1168     Counter ThenCount = getRegionCounter(S);
1169 
1170     // Emitting a counter for the condition makes it easier to interpret the
1171     // counter for the body when looking at the coverage.
1172     propagateCounts(ParentCount, S->getCond());
1173 
1174     // The 'then' count applies to the area immediately after the condition.
1175     auto Gap = findGapAreaBetween(S->getCond(), S->getThen());
1176     if (Gap)
1177       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ThenCount);
1178 
1179     extendRegion(S->getThen());
1180     Counter OutCount = propagateCounts(ThenCount, S->getThen());
1181 
1182     Counter ElseCount = subtractCounters(ParentCount, ThenCount);
1183     if (const Stmt *Else = S->getElse()) {
1184       // The 'else' count applies to the area immediately after the 'then'.
1185       Gap = findGapAreaBetween(S->getThen(), Else);
1186       if (Gap)
1187         fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ElseCount);
1188       extendRegion(Else);
1189       OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
1190     } else
1191       OutCount = addCounters(OutCount, ElseCount);
1192 
1193     if (OutCount != ParentCount)
1194       pushRegion(OutCount);
1195   }
1196 
1197   void VisitCXXTryStmt(const CXXTryStmt *S) {
1198     extendRegion(S);
1199     // Handle macros that generate the "try" but not the rest.
1200     extendRegion(S->getTryBlock());
1201 
1202     Counter ParentCount = getRegion().getCounter();
1203     propagateCounts(ParentCount, S->getTryBlock());
1204 
1205     for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
1206       Visit(S->getHandler(I));
1207 
1208     Counter ExitCount = getRegionCounter(S);
1209     pushRegion(ExitCount);
1210   }
1211 
1212   void VisitCXXCatchStmt(const CXXCatchStmt *S) {
1213     propagateCounts(getRegionCounter(S), S->getHandlerBlock());
1214   }
1215 
1216   void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
1217     extendRegion(E);
1218 
1219     Counter ParentCount = getRegion().getCounter();
1220     Counter TrueCount = getRegionCounter(E);
1221 
1222     Visit(E->getCond());
1223 
1224     if (!isa<BinaryConditionalOperator>(E)) {
1225       // The 'then' count applies to the area immediately after the condition.
1226       auto Gap =
1227           findGapAreaBetween(E->getQuestionLoc(), getStart(E->getTrueExpr()));
1228       if (Gap)
1229         fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), TrueCount);
1230 
1231       extendRegion(E->getTrueExpr());
1232       propagateCounts(TrueCount, E->getTrueExpr());
1233     }
1234 
1235     extendRegion(E->getFalseExpr());
1236     propagateCounts(subtractCounters(ParentCount, TrueCount),
1237                     E->getFalseExpr());
1238   }
1239 
1240   void VisitBinLAnd(const BinaryOperator *E) {
1241     extendRegion(E->getLHS());
1242     propagateCounts(getRegion().getCounter(), E->getLHS());
1243     handleFileExit(getEnd(E->getLHS()));
1244 
1245     extendRegion(E->getRHS());
1246     propagateCounts(getRegionCounter(E), E->getRHS());
1247   }
1248 
1249   void VisitBinLOr(const BinaryOperator *E) {
1250     extendRegion(E->getLHS());
1251     propagateCounts(getRegion().getCounter(), E->getLHS());
1252     handleFileExit(getEnd(E->getLHS()));
1253 
1254     extendRegion(E->getRHS());
1255     propagateCounts(getRegionCounter(E), E->getRHS());
1256   }
1257 
1258   void VisitLambdaExpr(const LambdaExpr *LE) {
1259     // Lambdas are treated as their own functions for now, so we shouldn't
1260     // propagate counts into them.
1261   }
1262 };
1263 
1264 std::string getCoverageSection(const CodeGenModule &CGM) {
1265   return llvm::getInstrProfSectionName(
1266       llvm::IPSK_covmap,
1267       CGM.getContext().getTargetInfo().getTriple().getObjectFormat());
1268 }
1269 
1270 std::string normalizeFilename(StringRef Filename) {
1271   llvm::SmallString<256> Path(Filename);
1272   llvm::sys::fs::make_absolute(Path);
1273   llvm::sys::path::remove_dots(Path, /*remove_dot_dots=*/true);
1274   return Path.str().str();
1275 }
1276 
1277 } // end anonymous namespace
1278 
1279 static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
1280                  ArrayRef<CounterExpression> Expressions,
1281                  ArrayRef<CounterMappingRegion> Regions) {
1282   OS << FunctionName << ":\n";
1283   CounterMappingContext Ctx(Expressions);
1284   for (const auto &R : Regions) {
1285     OS.indent(2);
1286     switch (R.Kind) {
1287     case CounterMappingRegion::CodeRegion:
1288       break;
1289     case CounterMappingRegion::ExpansionRegion:
1290       OS << "Expansion,";
1291       break;
1292     case CounterMappingRegion::SkippedRegion:
1293       OS << "Skipped,";
1294       break;
1295     case CounterMappingRegion::GapRegion:
1296       OS << "Gap,";
1297       break;
1298     }
1299 
1300     OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
1301        << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
1302     Ctx.dump(R.Count, OS);
1303     if (R.Kind == CounterMappingRegion::ExpansionRegion)
1304       OS << " (Expanded file = " << R.ExpandedFileID << ")";
1305     OS << "\n";
1306   }
1307 }
1308 
1309 void CoverageMappingModuleGen::addFunctionMappingRecord(
1310     llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
1311     const std::string &CoverageMapping, bool IsUsed) {
1312   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1313   if (!FunctionRecordTy) {
1314 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
1315     llvm::Type *FunctionRecordTypes[] = {
1316       #include "llvm/ProfileData/InstrProfData.inc"
1317     };
1318     FunctionRecordTy =
1319         llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes),
1320                               /*isPacked=*/true);
1321   }
1322 
1323   #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
1324   llvm::Constant *FunctionRecordVals[] = {
1325       #include "llvm/ProfileData/InstrProfData.inc"
1326   };
1327   FunctionRecords.push_back(llvm::ConstantStruct::get(
1328       FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
1329   if (!IsUsed)
1330     FunctionNames.push_back(
1331         llvm::ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx)));
1332   CoverageMappings.push_back(CoverageMapping);
1333 
1334   if (CGM.getCodeGenOpts().DumpCoverageMapping) {
1335     // Dump the coverage mapping data for this function by decoding the
1336     // encoded data. This allows us to dump the mapping regions which were
1337     // also processed by the CoverageMappingWriter which performs
1338     // additional minimization operations such as reducing the number of
1339     // expressions.
1340     std::vector<StringRef> Filenames;
1341     std::vector<CounterExpression> Expressions;
1342     std::vector<CounterMappingRegion> Regions;
1343     llvm::SmallVector<std::string, 16> FilenameStrs;
1344     llvm::SmallVector<StringRef, 16> FilenameRefs;
1345     FilenameStrs.resize(FileEntries.size());
1346     FilenameRefs.resize(FileEntries.size());
1347     for (const auto &Entry : FileEntries) {
1348       auto I = Entry.second;
1349       FilenameStrs[I] = normalizeFilename(Entry.first->getName());
1350       FilenameRefs[I] = FilenameStrs[I];
1351     }
1352     RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
1353                                     Expressions, Regions);
1354     if (Reader.read())
1355       return;
1356     dump(llvm::outs(), NameValue, Expressions, Regions);
1357   }
1358 }
1359 
1360 void CoverageMappingModuleGen::emit() {
1361   if (FunctionRecords.empty())
1362     return;
1363   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1364   auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
1365 
1366   // Create the filenames and merge them with coverage mappings
1367   llvm::SmallVector<std::string, 16> FilenameStrs;
1368   llvm::SmallVector<StringRef, 16> FilenameRefs;
1369   FilenameStrs.resize(FileEntries.size());
1370   FilenameRefs.resize(FileEntries.size());
1371   for (const auto &Entry : FileEntries) {
1372     auto I = Entry.second;
1373     FilenameStrs[I] = normalizeFilename(Entry.first->getName());
1374     FilenameRefs[I] = FilenameStrs[I];
1375   }
1376 
1377   std::string FilenamesAndCoverageMappings;
1378   llvm::raw_string_ostream OS(FilenamesAndCoverageMappings);
1379   CoverageFilenamesSectionWriter(FilenameRefs).write(OS);
1380   std::string RawCoverageMappings =
1381       llvm::join(CoverageMappings.begin(), CoverageMappings.end(), "");
1382   OS << RawCoverageMappings;
1383   size_t CoverageMappingSize = RawCoverageMappings.size();
1384   size_t FilenamesSize = OS.str().size() - CoverageMappingSize;
1385   // Append extra zeroes if necessary to ensure that the size of the filenames
1386   // and coverage mappings is a multiple of 8.
1387   if (size_t Rem = OS.str().size() % 8) {
1388     CoverageMappingSize += 8 - Rem;
1389     OS.write_zeros(8 - Rem);
1390   }
1391   auto *FilenamesAndMappingsVal =
1392       llvm::ConstantDataArray::getString(Ctx, OS.str(), false);
1393 
1394   // Create the deferred function records array
1395   auto RecordsTy =
1396       llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
1397   auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
1398 
1399   llvm::Type *CovDataHeaderTypes[] = {
1400 #define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
1401 #include "llvm/ProfileData/InstrProfData.inc"
1402   };
1403   auto CovDataHeaderTy =
1404       llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes));
1405   llvm::Constant *CovDataHeaderVals[] = {
1406 #define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
1407 #include "llvm/ProfileData/InstrProfData.inc"
1408   };
1409   auto CovDataHeaderVal = llvm::ConstantStruct::get(
1410       CovDataHeaderTy, makeArrayRef(CovDataHeaderVals));
1411 
1412   // Create the coverage data record
1413   llvm::Type *CovDataTypes[] = {CovDataHeaderTy, RecordsTy,
1414                                 FilenamesAndMappingsVal->getType()};
1415   auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
1416   llvm::Constant *TUDataVals[] = {CovDataHeaderVal, RecordsVal,
1417                                   FilenamesAndMappingsVal};
1418   auto CovDataVal =
1419       llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
1420   auto CovData = new llvm::GlobalVariable(
1421       CGM.getModule(), CovDataTy, true, llvm::GlobalValue::InternalLinkage,
1422       CovDataVal, llvm::getCoverageMappingVarName());
1423 
1424   CovData->setSection(getCoverageSection(CGM));
1425   CovData->setAlignment(8);
1426 
1427   // Make sure the data doesn't get deleted.
1428   CGM.addUsedGlobal(CovData);
1429   // Create the deferred function records array
1430   if (!FunctionNames.empty()) {
1431     auto NamesArrTy = llvm::ArrayType::get(llvm::Type::getInt8PtrTy(Ctx),
1432                                            FunctionNames.size());
1433     auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
1434     // This variable will *NOT* be emitted to the object file. It is used
1435     // to pass the list of names referenced to codegen.
1436     new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
1437                              llvm::GlobalValue::InternalLinkage, NamesArrVal,
1438                              llvm::getCoverageUnusedNamesVarName());
1439   }
1440 }
1441 
1442 unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
1443   auto It = FileEntries.find(File);
1444   if (It != FileEntries.end())
1445     return It->second;
1446   unsigned FileID = FileEntries.size();
1447   FileEntries.insert(std::make_pair(File, FileID));
1448   return FileID;
1449 }
1450 
1451 void CoverageMappingGen::emitCounterMapping(const Decl *D,
1452                                             llvm::raw_ostream &OS) {
1453   assert(CounterMap);
1454   CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
1455   Walker.VisitDecl(D);
1456   Walker.write(OS);
1457 }
1458 
1459 void CoverageMappingGen::emitEmptyMapping(const Decl *D,
1460                                           llvm::raw_ostream &OS) {
1461   EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
1462   Walker.VisitDecl(D);
1463   Walker.write(OS);
1464 }
1465