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