xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp (revision 1fd880742ace94e11fa60ee0b074f0b18e54c54f)
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/SmallSet.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
23 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
24 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
25 #include "llvm/ProfileData/InstrProfReader.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/Path.h"
28 #include <optional>
29 
30 // This selects the coverage mapping format defined when `InstrProfData.inc`
31 // is textually included.
32 #define COVMAP_V3
33 
34 static llvm::cl::opt<bool> EmptyLineCommentCoverage(
35     "emptyline-comment-coverage",
36     llvm::cl::desc("Emit emptylines and comment lines as skipped regions (only "
37                    "disable it on test)"),
38     llvm::cl::init(true), llvm::cl::Hidden);
39 
40 llvm::cl::opt<bool> SystemHeadersCoverage(
41     "system-headers-coverage",
42     llvm::cl::desc("Enable collecting coverage from system headers"),
43     llvm::cl::init(false), llvm::cl::Hidden);
44 
45 using namespace clang;
46 using namespace CodeGen;
47 using namespace llvm::coverage;
48 
49 CoverageSourceInfo *
50 CoverageMappingModuleGen::setUpCoverageCallbacks(Preprocessor &PP) {
51   CoverageSourceInfo *CoverageInfo =
52       new CoverageSourceInfo(PP.getSourceManager());
53   PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(CoverageInfo));
54   if (EmptyLineCommentCoverage) {
55     PP.addCommentHandler(CoverageInfo);
56     PP.setEmptylineHandler(CoverageInfo);
57     PP.setPreprocessToken(true);
58     PP.setTokenWatcher([CoverageInfo](clang::Token Tok) {
59       // Update previous token location.
60       CoverageInfo->PrevTokLoc = Tok.getLocation();
61       if (Tok.getKind() != clang::tok::eod)
62         CoverageInfo->updateNextTokLoc(Tok.getLocation());
63     });
64   }
65   return CoverageInfo;
66 }
67 
68 void CoverageSourceInfo::AddSkippedRange(SourceRange Range,
69                                          SkippedRange::Kind RangeKind) {
70   if (EmptyLineCommentCoverage && !SkippedRanges.empty() &&
71       PrevTokLoc == SkippedRanges.back().PrevTokLoc &&
72       SourceMgr.isWrittenInSameFile(SkippedRanges.back().Range.getEnd(),
73                                     Range.getBegin()))
74     SkippedRanges.back().Range.setEnd(Range.getEnd());
75   else
76     SkippedRanges.push_back({Range, RangeKind, PrevTokLoc});
77 }
78 
79 void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range, SourceLocation) {
80   AddSkippedRange(Range, SkippedRange::PPIfElse);
81 }
82 
83 void CoverageSourceInfo::HandleEmptyline(SourceRange Range) {
84   AddSkippedRange(Range, SkippedRange::EmptyLine);
85 }
86 
87 bool CoverageSourceInfo::HandleComment(Preprocessor &PP, SourceRange Range) {
88   AddSkippedRange(Range, SkippedRange::Comment);
89   return false;
90 }
91 
92 void CoverageSourceInfo::updateNextTokLoc(SourceLocation Loc) {
93   if (!SkippedRanges.empty() && SkippedRanges.back().NextTokLoc.isInvalid())
94     SkippedRanges.back().NextTokLoc = Loc;
95 }
96 
97 namespace {
98 using MCDCConditionID = CounterMappingRegion::MCDCConditionID;
99 using MCDCParameters = CounterMappingRegion::MCDCParameters;
100 
101 /// A region of source code that can be mapped to a counter.
102 class SourceMappingRegion {
103   /// Primary Counter that is also used for Branch Regions for "True" branches.
104   Counter Count;
105 
106   /// Secondary Counter used for Branch Regions for "False" branches.
107   std::optional<Counter> FalseCount;
108 
109   /// Parameters used for Modified Condition/Decision Coverage
110   MCDCParameters MCDCParams;
111 
112   /// The region's starting location.
113   std::optional<SourceLocation> LocStart;
114 
115   /// The region's ending location.
116   std::optional<SourceLocation> LocEnd;
117 
118   /// Whether this region is a gap region. The count from a gap region is set
119   /// as the line execution count if there are no other regions on the line.
120   bool GapRegion;
121 
122   /// Whetever this region is skipped ('if constexpr' or 'if consteval' untaken
123   /// branch, or anything skipped but not empty line / comments)
124   bool SkippedRegion;
125 
126 public:
127   SourceMappingRegion(Counter Count, std::optional<SourceLocation> LocStart,
128                       std::optional<SourceLocation> LocEnd,
129                       bool GapRegion = false)
130       : Count(Count), LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion),
131         SkippedRegion(false) {}
132 
133   SourceMappingRegion(Counter Count, std::optional<Counter> FalseCount,
134                       MCDCParameters MCDCParams,
135                       std::optional<SourceLocation> LocStart,
136                       std::optional<SourceLocation> LocEnd,
137                       bool GapRegion = false)
138       : Count(Count), FalseCount(FalseCount), MCDCParams(MCDCParams),
139         LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion),
140         SkippedRegion(false) {}
141 
142   SourceMappingRegion(MCDCParameters MCDCParams,
143                       std::optional<SourceLocation> LocStart,
144                       std::optional<SourceLocation> LocEnd)
145       : MCDCParams(MCDCParams), LocStart(LocStart), LocEnd(LocEnd),
146         GapRegion(false), SkippedRegion(false) {}
147 
148   const Counter &getCounter() const { return Count; }
149 
150   const Counter &getFalseCounter() const {
151     assert(FalseCount && "Region has no alternate counter");
152     return *FalseCount;
153   }
154 
155   void setCounter(Counter C) { Count = C; }
156 
157   bool hasStartLoc() const { return LocStart.has_value(); }
158 
159   void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
160 
161   SourceLocation getBeginLoc() const {
162     assert(LocStart && "Region has no start location");
163     return *LocStart;
164   }
165 
166   bool hasEndLoc() const { return LocEnd.has_value(); }
167 
168   void setEndLoc(SourceLocation Loc) {
169     assert(Loc.isValid() && "Setting an invalid end location");
170     LocEnd = Loc;
171   }
172 
173   SourceLocation getEndLoc() const {
174     assert(LocEnd && "Region has no end location");
175     return *LocEnd;
176   }
177 
178   bool isGap() const { return GapRegion; }
179 
180   void setGap(bool Gap) { GapRegion = Gap; }
181 
182   bool isSkipped() const { return SkippedRegion; }
183 
184   void setSkipped(bool Skipped) { SkippedRegion = Skipped; }
185 
186   bool isBranch() const { return FalseCount.has_value(); }
187 
188   bool isMCDCDecision() const { return MCDCParams.NumConditions != 0; }
189 
190   const MCDCParameters &getMCDCParams() const { return MCDCParams; }
191 };
192 
193 /// Spelling locations for the start and end of a source region.
194 struct SpellingRegion {
195   /// The line where the region starts.
196   unsigned LineStart;
197 
198   /// The column where the region starts.
199   unsigned ColumnStart;
200 
201   /// The line where the region ends.
202   unsigned LineEnd;
203 
204   /// The column where the region ends.
205   unsigned ColumnEnd;
206 
207   SpellingRegion(SourceManager &SM, SourceLocation LocStart,
208                  SourceLocation LocEnd) {
209     LineStart = SM.getSpellingLineNumber(LocStart);
210     ColumnStart = SM.getSpellingColumnNumber(LocStart);
211     LineEnd = SM.getSpellingLineNumber(LocEnd);
212     ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
213   }
214 
215   SpellingRegion(SourceManager &SM, SourceMappingRegion &R)
216       : SpellingRegion(SM, R.getBeginLoc(), R.getEndLoc()) {}
217 
218   /// Check if the start and end locations appear in source order, i.e
219   /// top->bottom, left->right.
220   bool isInSourceOrder() const {
221     return (LineStart < LineEnd) ||
222            (LineStart == LineEnd && ColumnStart <= ColumnEnd);
223   }
224 };
225 
226 /// Provides the common functionality for the different
227 /// coverage mapping region builders.
228 class CoverageMappingBuilder {
229 public:
230   CoverageMappingModuleGen &CVM;
231   SourceManager &SM;
232   const LangOptions &LangOpts;
233 
234 private:
235   /// Map of clang's FileIDs to IDs used for coverage mapping.
236   llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
237       FileIDMapping;
238 
239 public:
240   /// The coverage mapping regions for this function
241   llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
242   /// The source mapping regions for this function.
243   std::vector<SourceMappingRegion> SourceRegions;
244 
245   /// A set of regions which can be used as a filter.
246   ///
247   /// It is produced by emitExpansionRegions() and is used in
248   /// emitSourceRegions() to suppress producing code regions if
249   /// the same area is covered by expansion regions.
250   typedef llvm::SmallSet<std::pair<SourceLocation, SourceLocation>, 8>
251       SourceRegionFilter;
252 
253   CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
254                          const LangOptions &LangOpts)
255       : CVM(CVM), SM(SM), LangOpts(LangOpts) {}
256 
257   /// Return the precise end location for the given token.
258   SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
259     // We avoid getLocForEndOfToken here, because it doesn't do what we want for
260     // macro locations, which we just treat as expanded files.
261     unsigned TokLen =
262         Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
263     return Loc.getLocWithOffset(TokLen);
264   }
265 
266   /// Return the start location of an included file or expanded macro.
267   SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
268     if (Loc.isMacroID())
269       return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
270     return SM.getLocForStartOfFile(SM.getFileID(Loc));
271   }
272 
273   /// Return the end location of an included file or expanded macro.
274   SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
275     if (Loc.isMacroID())
276       return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
277                                   SM.getFileOffset(Loc));
278     return SM.getLocForEndOfFile(SM.getFileID(Loc));
279   }
280 
281   /// Find out where the current file is included or macro is expanded.
282   SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
283     return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).getBegin()
284                            : SM.getIncludeLoc(SM.getFileID(Loc));
285   }
286 
287   /// Return true if \c Loc is a location in a built-in macro.
288   bool isInBuiltin(SourceLocation Loc) {
289     return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>";
290   }
291 
292   /// Check whether \c Loc is included or expanded from \c Parent.
293   bool isNestedIn(SourceLocation Loc, FileID Parent) {
294     do {
295       Loc = getIncludeOrExpansionLoc(Loc);
296       if (Loc.isInvalid())
297         return false;
298     } while (!SM.isInFileID(Loc, Parent));
299     return true;
300   }
301 
302   /// Get the start of \c S ignoring macro arguments and builtin macros.
303   SourceLocation getStart(const Stmt *S) {
304     SourceLocation Loc = S->getBeginLoc();
305     while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
306       Loc = SM.getImmediateExpansionRange(Loc).getBegin();
307     return Loc;
308   }
309 
310   /// Get the end of \c S ignoring macro arguments and builtin macros.
311   SourceLocation getEnd(const Stmt *S) {
312     SourceLocation Loc = S->getEndLoc();
313     while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
314       Loc = SM.getImmediateExpansionRange(Loc).getBegin();
315     return getPreciseTokenLocEnd(Loc);
316   }
317 
318   /// Find the set of files we have regions for and assign IDs
319   ///
320   /// Fills \c Mapping with the virtual file mapping needed to write out
321   /// coverage and collects the necessary file information to emit source and
322   /// expansion regions.
323   void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
324     FileIDMapping.clear();
325 
326     llvm::SmallSet<FileID, 8> Visited;
327     SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
328     for (const auto &Region : SourceRegions) {
329       SourceLocation Loc = Region.getBeginLoc();
330       FileID File = SM.getFileID(Loc);
331       if (!Visited.insert(File).second)
332         continue;
333 
334       // Do not map FileID's associated with system headers unless collecting
335       // coverage from system headers is explicitly enabled.
336       if (!SystemHeadersCoverage && SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
337         continue;
338 
339       unsigned Depth = 0;
340       for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
341            Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent))
342         ++Depth;
343       FileLocs.push_back(std::make_pair(Loc, Depth));
344     }
345     llvm::stable_sort(FileLocs, llvm::less_second());
346 
347     for (const auto &FL : FileLocs) {
348       SourceLocation Loc = FL.first;
349       FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
350       auto Entry = SM.getFileEntryRefForID(SpellingFile);
351       if (!Entry)
352         continue;
353 
354       FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
355       Mapping.push_back(CVM.getFileID(*Entry));
356     }
357   }
358 
359   /// Get the coverage mapping file ID for \c Loc.
360   ///
361   /// If such file id doesn't exist, return std::nullopt.
362   std::optional<unsigned> getCoverageFileID(SourceLocation Loc) {
363     auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
364     if (Mapping != FileIDMapping.end())
365       return Mapping->second.first;
366     return std::nullopt;
367   }
368 
369   /// This shrinks the skipped range if it spans a line that contains a
370   /// non-comment token. If shrinking the skipped range would make it empty,
371   /// this returns std::nullopt.
372   /// Note this function can potentially be expensive because
373   /// getSpellingLineNumber uses getLineNumber, which is expensive.
374   std::optional<SpellingRegion> adjustSkippedRange(SourceManager &SM,
375                                                    SourceLocation LocStart,
376                                                    SourceLocation LocEnd,
377                                                    SourceLocation PrevTokLoc,
378                                                    SourceLocation NextTokLoc) {
379     SpellingRegion SR{SM, LocStart, LocEnd};
380     SR.ColumnStart = 1;
381     if (PrevTokLoc.isValid() && SM.isWrittenInSameFile(LocStart, PrevTokLoc) &&
382         SR.LineStart == SM.getSpellingLineNumber(PrevTokLoc))
383       SR.LineStart++;
384     if (NextTokLoc.isValid() && SM.isWrittenInSameFile(LocEnd, NextTokLoc) &&
385         SR.LineEnd == SM.getSpellingLineNumber(NextTokLoc)) {
386       SR.LineEnd--;
387       SR.ColumnEnd++;
388     }
389     if (SR.isInSourceOrder())
390       return SR;
391     return std::nullopt;
392   }
393 
394   /// Gather all the regions that were skipped by the preprocessor
395   /// using the constructs like #if or comments.
396   void gatherSkippedRegions() {
397     /// An array of the minimum lineStarts and the maximum lineEnds
398     /// for mapping regions from the appropriate source files.
399     llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
400     FileLineRanges.resize(
401         FileIDMapping.size(),
402         std::make_pair(std::numeric_limits<unsigned>::max(), 0));
403     for (const auto &R : MappingRegions) {
404       FileLineRanges[R.FileID].first =
405           std::min(FileLineRanges[R.FileID].first, R.LineStart);
406       FileLineRanges[R.FileID].second =
407           std::max(FileLineRanges[R.FileID].second, R.LineEnd);
408     }
409 
410     auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
411     for (auto &I : SkippedRanges) {
412       SourceRange Range = I.Range;
413       auto LocStart = Range.getBegin();
414       auto LocEnd = Range.getEnd();
415       assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
416              "region spans multiple files");
417 
418       auto CovFileID = getCoverageFileID(LocStart);
419       if (!CovFileID)
420         continue;
421       std::optional<SpellingRegion> SR;
422       if (I.isComment())
423         SR = adjustSkippedRange(SM, LocStart, LocEnd, I.PrevTokLoc,
424                                 I.NextTokLoc);
425       else if (I.isPPIfElse() || I.isEmptyLine())
426         SR = {SM, LocStart, LocEnd};
427 
428       if (!SR)
429         continue;
430       auto Region = CounterMappingRegion::makeSkipped(
431           *CovFileID, SR->LineStart, SR->ColumnStart, SR->LineEnd,
432           SR->ColumnEnd);
433       // Make sure that we only collect the regions that are inside
434       // the source code of this function.
435       if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
436           Region.LineEnd <= FileLineRanges[*CovFileID].second)
437         MappingRegions.push_back(Region);
438     }
439   }
440 
441   /// Generate the coverage counter mapping regions from collected
442   /// source regions.
443   void emitSourceRegions(const SourceRegionFilter &Filter) {
444     for (const auto &Region : SourceRegions) {
445       assert(Region.hasEndLoc() && "incomplete region");
446 
447       SourceLocation LocStart = Region.getBeginLoc();
448       assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
449 
450       // Ignore regions from system headers unless collecting coverage from
451       // system headers is explicitly enabled.
452       if (!SystemHeadersCoverage &&
453           SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
454         continue;
455 
456       auto CovFileID = getCoverageFileID(LocStart);
457       // Ignore regions that don't have a file, such as builtin macros.
458       if (!CovFileID)
459         continue;
460 
461       SourceLocation LocEnd = Region.getEndLoc();
462       assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
463              "region spans multiple files");
464 
465       // Don't add code regions for the area covered by expansion regions.
466       // This not only suppresses redundant regions, but sometimes prevents
467       // creating regions with wrong counters if, for example, a statement's
468       // body ends at the end of a nested macro.
469       if (Filter.count(std::make_pair(LocStart, LocEnd)))
470         continue;
471 
472       // Find the spelling locations for the mapping region.
473       SpellingRegion SR{SM, LocStart, LocEnd};
474       assert(SR.isInSourceOrder() && "region start and end out of order");
475 
476       if (Region.isGap()) {
477         MappingRegions.push_back(CounterMappingRegion::makeGapRegion(
478             Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
479             SR.LineEnd, SR.ColumnEnd));
480       } else if (Region.isSkipped()) {
481         MappingRegions.push_back(CounterMappingRegion::makeSkipped(
482             *CovFileID, SR.LineStart, SR.ColumnStart, SR.LineEnd,
483             SR.ColumnEnd));
484       } else if (Region.isBranch()) {
485         MappingRegions.push_back(CounterMappingRegion::makeBranchRegion(
486             Region.getCounter(), Region.getFalseCounter(),
487             Region.getMCDCParams(), *CovFileID, SR.LineStart, SR.ColumnStart,
488             SR.LineEnd, SR.ColumnEnd));
489       } else if (Region.isMCDCDecision()) {
490         MappingRegions.push_back(CounterMappingRegion::makeDecisionRegion(
491             Region.getMCDCParams(), *CovFileID, SR.LineStart, SR.ColumnStart,
492             SR.LineEnd, SR.ColumnEnd));
493       } else {
494         MappingRegions.push_back(CounterMappingRegion::makeRegion(
495             Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
496             SR.LineEnd, SR.ColumnEnd));
497       }
498     }
499   }
500 
501   /// Generate expansion regions for each virtual file we've seen.
502   SourceRegionFilter emitExpansionRegions() {
503     SourceRegionFilter Filter;
504     for (const auto &FM : FileIDMapping) {
505       SourceLocation ExpandedLoc = FM.second.second;
506       SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
507       if (ParentLoc.isInvalid())
508         continue;
509 
510       auto ParentFileID = getCoverageFileID(ParentLoc);
511       if (!ParentFileID)
512         continue;
513       auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
514       assert(ExpandedFileID && "expansion in uncovered file");
515 
516       SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
517       assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
518              "region spans multiple files");
519       Filter.insert(std::make_pair(ParentLoc, LocEnd));
520 
521       SpellingRegion SR{SM, ParentLoc, LocEnd};
522       assert(SR.isInSourceOrder() && "region start and end out of order");
523       MappingRegions.push_back(CounterMappingRegion::makeExpansion(
524           *ParentFileID, *ExpandedFileID, SR.LineStart, SR.ColumnStart,
525           SR.LineEnd, SR.ColumnEnd));
526     }
527     return Filter;
528   }
529 };
530 
531 /// Creates unreachable coverage regions for the functions that
532 /// are not emitted.
533 struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
534   EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
535                               const LangOptions &LangOpts)
536       : CoverageMappingBuilder(CVM, SM, LangOpts) {}
537 
538   void VisitDecl(const Decl *D) {
539     if (!D->hasBody())
540       return;
541     auto Body = D->getBody();
542     SourceLocation Start = getStart(Body);
543     SourceLocation End = getEnd(Body);
544     if (!SM.isWrittenInSameFile(Start, End)) {
545       // Walk up to find the common ancestor.
546       // Correct the locations accordingly.
547       FileID StartFileID = SM.getFileID(Start);
548       FileID EndFileID = SM.getFileID(End);
549       while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) {
550         Start = getIncludeOrExpansionLoc(Start);
551         assert(Start.isValid() &&
552                "Declaration start location not nested within a known region");
553         StartFileID = SM.getFileID(Start);
554       }
555       while (StartFileID != EndFileID) {
556         End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
557         assert(End.isValid() &&
558                "Declaration end location not nested within a known region");
559         EndFileID = SM.getFileID(End);
560       }
561     }
562     SourceRegions.emplace_back(Counter(), Start, End);
563   }
564 
565   /// Write the mapping data to the output stream
566   void write(llvm::raw_ostream &OS) {
567     SmallVector<unsigned, 16> FileIDMapping;
568     gatherFileIDs(FileIDMapping);
569     emitSourceRegions(SourceRegionFilter());
570 
571     if (MappingRegions.empty())
572       return;
573 
574     CoverageMappingWriter Writer(FileIDMapping, std::nullopt, MappingRegions);
575     Writer.write(OS);
576   }
577 };
578 
579 /// A wrapper object for maintaining stacks to track the resursive AST visitor
580 /// walks for the purpose of assigning IDs to leaf-level conditions measured by
581 /// MC/DC. The object is created with a reference to the MCDCBitmapMap that was
582 /// created during the initial AST walk. The presence of a bitmap associated
583 /// with a boolean expression (top-level logical operator nest) indicates that
584 /// the boolean expression qualified for MC/DC.  The resulting condition IDs
585 /// are preserved in a map reference that is also provided during object
586 /// creation.
587 struct MCDCCoverageBuilder {
588 
589   struct DecisionIDPair {
590     MCDCConditionID TrueID = 0;
591     MCDCConditionID FalseID = 0;
592   };
593 
594   /// The AST walk recursively visits nested logical-AND or logical-OR binary
595   /// operator nodes and then visits their LHS and RHS children nodes.  As this
596   /// happens, the algorithm will assign IDs to each operator's LHS and RHS side
597   /// as the walk moves deeper into the nest.  At each level of the recursive
598   /// nest, the LHS and RHS may actually correspond to larger subtrees (not
599   /// leaf-conditions). If this is the case, when that node is visited, the ID
600   /// assigned to the subtree is re-assigned to its LHS, and a new ID is given
601   /// to its RHS. At the end of the walk, all leaf-level conditions will have a
602   /// unique ID -- keep in mind that the final set of IDs may not be in
603   /// numerical order from left to right.
604   ///
605   /// Example: "x = (A && B) || (C && D) || (D && F)"
606   ///
607   ///      Visit Depth1:
608   ///              (A && B) || (C && D) || (D && F)
609   ///              ^-------LHS--------^    ^-RHS--^
610   ///                      ID=1              ID=2
611   ///
612   ///      Visit LHS-Depth2:
613   ///              (A && B) || (C && D)
614   ///              ^-LHS--^    ^-RHS--^
615   ///                ID=1        ID=3
616   ///
617   ///      Visit LHS-Depth3:
618   ///               (A && B)
619   ///               LHS   RHS
620   ///               ID=1  ID=4
621   ///
622   ///      Visit RHS-Depth3:
623   ///                         (C && D)
624   ///                         LHS   RHS
625   ///                         ID=3  ID=5
626   ///
627   ///      Visit RHS-Depth2:              (D && F)
628   ///                                     LHS   RHS
629   ///                                     ID=2  ID=6
630   ///
631   ///      Visit Depth1:
632   ///              (A && B)  || (C && D)  || (D && F)
633   ///              ID=1  ID=4   ID=3  ID=5   ID=2  ID=6
634   ///
635   /// A node ID of '0' always means MC/DC isn't being tracked.
636   ///
637   /// As the AST walk proceeds recursively, the algorithm will also use a stack
638   /// to track the IDs of logical-AND and logical-OR operations on the RHS so
639   /// that it can be determined which nodes are executed next, depending on how
640   /// a LHS or RHS of a logical-AND or logical-OR is evaluated.  This
641   /// information relies on the assigned IDs and are embedded within the
642   /// coverage region IDs of each branch region associated with a leaf-level
643   /// condition. This information helps the visualization tool reconstruct all
644   /// possible test vectors for the purposes of MC/DC analysis. If a "next" node
645   /// ID is '0', it means it's the end of the test vector. The following rules
646   /// are used:
647   ///
648   /// For logical-AND ("LHS && RHS"):
649   /// - If LHS is TRUE, execution goes to the RHS node.
650   /// - If LHS is FALSE, execution goes to the LHS node of the next logical-OR.
651   ///   If that does not exist, execution exits (ID == 0).
652   ///
653   /// - If RHS is TRUE, execution goes to LHS node of the next logical-AND.
654   ///   If that does not exist, execution exits (ID == 0).
655   /// - If RHS is FALSE, execution goes to the LHS node of the next logical-OR.
656   ///   If that does not exist, execution exits (ID == 0).
657   ///
658   /// For logical-OR ("LHS || RHS"):
659   /// - If LHS is TRUE, execution goes to the LHS node of the next logical-AND.
660   ///   If that does not exist, execution exits (ID == 0).
661   /// - If LHS is FALSE, execution goes to the RHS node.
662   ///
663   /// - If RHS is TRUE, execution goes to LHS node of the next logical-AND.
664   ///   If that does not exist, execution exits (ID == 0).
665   /// - If RHS is FALSE, execution goes to the LHS node of the next logical-OR.
666   ///   If that does not exist, execution exits (ID == 0).
667   ///
668   /// Finally, the condition IDs are also used when instrumenting the code to
669   /// indicate a unique offset into a temporary bitmap that represents the true
670   /// or false evaluation of that particular condition.
671   ///
672   /// NOTE regarding the use of CodeGenFunction::stripCond(). Even though, for
673   /// simplicity, parentheses and unary logical-NOT operators are considered
674   /// part of their underlying condition for both MC/DC and branch coverage, the
675   /// condition IDs themselves are assigned and tracked using the underlying
676   /// condition itself.  This is done solely for consistency since parentheses
677   /// and logical-NOTs are ignored when checking whether the condition is
678   /// actually an instrumentable condition. This can also make debugging a bit
679   /// easier.
680 
681 private:
682   CodeGenModule &CGM;
683 
684   llvm::SmallVector<DecisionIDPair> DecisionStack;
685   llvm::DenseMap<const Stmt *, MCDCConditionID> &CondIDs;
686   llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap;
687   MCDCConditionID NextID = 1;
688   bool NotMapped = false;
689 
690   /// Represent a sentinel value of [0,0] for the bottom of DecisionStack.
691   static constexpr DecisionIDPair DecisionStackSentinel{0, 0};
692 
693   /// Is this a logical-AND operation?
694   bool isLAnd(const BinaryOperator *E) const {
695     return E->getOpcode() == BO_LAnd;
696   }
697 
698 public:
699   MCDCCoverageBuilder(CodeGenModule &CGM,
700                       llvm::DenseMap<const Stmt *, MCDCConditionID> &CondIDMap,
701                       llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap)
702       : CGM(CGM), DecisionStack(1, DecisionStackSentinel), CondIDs(CondIDMap),
703         MCDCBitmapMap(MCDCBitmapMap) {}
704 
705   /// Return whether the build of the control flow map is at the top-level
706   /// (root) of a logical operator nest in a boolean expression prior to the
707   /// assignment of condition IDs.
708   bool isIdle() const { return (NextID == 1 && !NotMapped); }
709 
710   /// Return whether any IDs have been assigned in the build of the control
711   /// flow map, indicating that the map is being generated for this boolean
712   /// expression.
713   bool isBuilding() const { return (NextID > 1); }
714 
715   /// Set the given condition's ID.
716   void setCondID(const Expr *Cond, MCDCConditionID ID) {
717     CondIDs[CodeGenFunction::stripCond(Cond)] = ID;
718   }
719 
720   /// Return the ID of a given condition.
721   MCDCConditionID getCondID(const Expr *Cond) const {
722     auto I = CondIDs.find(CodeGenFunction::stripCond(Cond));
723     if (I == CondIDs.end())
724       return 0;
725     else
726       return I->second;
727   }
728 
729   /// Return the LHS Decision ([0,0] if not set).
730   const DecisionIDPair &back() const { return DecisionStack.back(); }
731 
732   /// Push the binary operator statement to track the nest level and assign IDs
733   /// to the operator's LHS and RHS.  The RHS may be a larger subtree that is
734   /// broken up on successive levels.
735   void pushAndAssignIDs(const BinaryOperator *E) {
736     if (!CGM.getCodeGenOpts().MCDCCoverage)
737       return;
738 
739     // If binary expression is disqualified, don't do mapping.
740     if (!isBuilding() && !MCDCBitmapMap.contains(CodeGenFunction::stripCond(E)))
741       NotMapped = true;
742 
743     // Don't go any further if we don't need to map condition IDs.
744     if (NotMapped)
745       return;
746 
747     const DecisionIDPair &ParentDecision = DecisionStack.back();
748 
749     // If the operator itself has an assigned ID, this means it represents a
750     // larger subtree.  In this case, assign that ID to its LHS node.  Its RHS
751     // will receive a new ID below. Otherwise, assign ID+1 to LHS.
752     if (CondIDs.contains(CodeGenFunction::stripCond(E)))
753       setCondID(E->getLHS(), getCondID(E));
754     else
755       setCondID(E->getLHS(), NextID++);
756 
757     // Assign a ID+1 for the RHS.
758     MCDCConditionID RHSid = NextID++;
759     setCondID(E->getRHS(), RHSid);
760 
761     // Push the LHS decision IDs onto the DecisionStack.
762     if (isLAnd(E))
763       DecisionStack.push_back({RHSid, ParentDecision.FalseID});
764     else
765       DecisionStack.push_back({ParentDecision.TrueID, RHSid});
766   }
767 
768   /// Pop and return the LHS Decision ([0,0] if not set).
769   DecisionIDPair pop() {
770     if (!CGM.getCodeGenOpts().MCDCCoverage || NotMapped)
771       return DecisionStack.front();
772 
773     assert(DecisionStack.size() > 1);
774     DecisionIDPair D = DecisionStack.back();
775     DecisionStack.pop_back();
776     return D;
777   }
778 
779   /// Return the total number of conditions and reset the state. The number of
780   /// conditions is zero if the expression isn't mapped.
781   unsigned getTotalConditionsAndReset(const BinaryOperator *E) {
782     if (!CGM.getCodeGenOpts().MCDCCoverage)
783       return 0;
784 
785     assert(!isIdle());
786     assert(DecisionStack.size() == 1);
787 
788     // Reset state if not doing mapping.
789     if (NotMapped) {
790       NotMapped = false;
791       assert(NextID == 1);
792       return 0;
793     }
794 
795     // Set number of conditions and reset.
796     unsigned TotalConds = NextID - 1;
797 
798     // Reset ID back to beginning.
799     NextID = 1;
800 
801     return TotalConds;
802   }
803 };
804 
805 /// A StmtVisitor that creates coverage mapping regions which map
806 /// from the source code locations to the PGO counters.
807 struct CounterCoverageMappingBuilder
808     : public CoverageMappingBuilder,
809       public ConstStmtVisitor<CounterCoverageMappingBuilder> {
810   /// The map of statements to count values.
811   llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
812 
813   /// The map of statements to bitmap coverage object values.
814   llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap;
815 
816   /// A stack of currently live regions.
817   llvm::SmallVector<SourceMappingRegion> RegionStack;
818 
819   /// An object to manage MCDC regions.
820   MCDCCoverageBuilder MCDCBuilder;
821 
822   CounterExpressionBuilder Builder;
823 
824   /// A location in the most recently visited file or macro.
825   ///
826   /// This is used to adjust the active source regions appropriately when
827   /// expressions cross file or macro boundaries.
828   SourceLocation MostRecentLocation;
829 
830   /// Whether the visitor at a terminate statement.
831   bool HasTerminateStmt = false;
832 
833   /// Gap region counter after terminate statement.
834   Counter GapRegionCounter;
835 
836   /// Return a counter for the subtraction of \c RHS from \c LHS
837   Counter subtractCounters(Counter LHS, Counter RHS, bool Simplify = true) {
838     return Builder.subtract(LHS, RHS, Simplify);
839   }
840 
841   /// Return a counter for the sum of \c LHS and \c RHS.
842   Counter addCounters(Counter LHS, Counter RHS, bool Simplify = true) {
843     return Builder.add(LHS, RHS, Simplify);
844   }
845 
846   Counter addCounters(Counter C1, Counter C2, Counter C3,
847                       bool Simplify = true) {
848     return addCounters(addCounters(C1, C2, Simplify), C3, Simplify);
849   }
850 
851   /// Return the region counter for the given statement.
852   ///
853   /// This should only be called on statements that have a dedicated counter.
854   Counter getRegionCounter(const Stmt *S) {
855     return Counter::getCounter(CounterMap[S]);
856   }
857 
858   unsigned getRegionBitmap(const Stmt *S) { return MCDCBitmapMap[S]; }
859 
860   /// Push a region onto the stack.
861   ///
862   /// Returns the index on the stack where the region was pushed. This can be
863   /// used with popRegions to exit a "scope", ending the region that was pushed.
864   size_t pushRegion(Counter Count,
865                     std::optional<SourceLocation> StartLoc = std::nullopt,
866                     std::optional<SourceLocation> EndLoc = std::nullopt,
867                     std::optional<Counter> FalseCount = std::nullopt,
868                     MCDCConditionID ID = 0, MCDCConditionID TrueID = 0,
869                     MCDCConditionID FalseID = 0) {
870 
871     if (StartLoc && !FalseCount) {
872       MostRecentLocation = *StartLoc;
873     }
874 
875     // If either of these locations is invalid, something elsewhere in the
876     // compiler has broken.
877     assert((!StartLoc || StartLoc->isValid()) && "Start location is not valid");
878     assert((!EndLoc || EndLoc->isValid()) && "End location is not valid");
879 
880     // However, we can still recover without crashing.
881     // If either location is invalid, set it to std::nullopt to avoid
882     // letting users of RegionStack think that region has a valid start/end
883     // location.
884     if (StartLoc && StartLoc->isInvalid())
885       StartLoc = std::nullopt;
886     if (EndLoc && EndLoc->isInvalid())
887       EndLoc = std::nullopt;
888     RegionStack.emplace_back(Count, FalseCount,
889                              MCDCParameters{0, 0, ID, TrueID, FalseID},
890                              StartLoc, EndLoc);
891 
892     return RegionStack.size() - 1;
893   }
894 
895   size_t pushRegion(unsigned BitmapIdx, unsigned Conditions,
896                     std::optional<SourceLocation> StartLoc = std::nullopt,
897                     std::optional<SourceLocation> EndLoc = std::nullopt) {
898 
899     RegionStack.emplace_back(MCDCParameters{BitmapIdx, Conditions}, StartLoc,
900                              EndLoc);
901 
902     return RegionStack.size() - 1;
903   }
904 
905   size_t locationDepth(SourceLocation Loc) {
906     size_t Depth = 0;
907     while (Loc.isValid()) {
908       Loc = getIncludeOrExpansionLoc(Loc);
909       Depth++;
910     }
911     return Depth;
912   }
913 
914   /// Pop regions from the stack into the function's list of regions.
915   ///
916   /// Adds all regions from \c ParentIndex to the top of the stack to the
917   /// function's \c SourceRegions.
918   void popRegions(size_t ParentIndex) {
919     assert(RegionStack.size() >= ParentIndex && "parent not in stack");
920     while (RegionStack.size() > ParentIndex) {
921       SourceMappingRegion &Region = RegionStack.back();
922       if (Region.hasStartLoc() &&
923           (Region.hasEndLoc() || RegionStack[ParentIndex].hasEndLoc())) {
924         SourceLocation StartLoc = Region.getBeginLoc();
925         SourceLocation EndLoc = Region.hasEndLoc()
926                                     ? Region.getEndLoc()
927                                     : RegionStack[ParentIndex].getEndLoc();
928         bool isBranch = Region.isBranch();
929         size_t StartDepth = locationDepth(StartLoc);
930         size_t EndDepth = locationDepth(EndLoc);
931         while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
932           bool UnnestStart = StartDepth >= EndDepth;
933           bool UnnestEnd = EndDepth >= StartDepth;
934           if (UnnestEnd) {
935             // The region ends in a nested file or macro expansion. If the
936             // region is not a branch region, create a separate region for each
937             // expansion, and for all regions, update the EndLoc. Branch
938             // regions should not be split in order to keep a straightforward
939             // correspondance between the region and its associated branch
940             // condition, even if the condition spans multiple depths.
941             SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
942             assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
943 
944             if (!isBranch && !isRegionAlreadyAdded(NestedLoc, EndLoc))
945               SourceRegions.emplace_back(Region.getCounter(), NestedLoc,
946                                          EndLoc);
947 
948             EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
949             if (EndLoc.isInvalid())
950               llvm::report_fatal_error(
951                   "File exit not handled before popRegions");
952             EndDepth--;
953           }
954           if (UnnestStart) {
955             // The region ends in a nested file or macro expansion. If the
956             // region is not a branch region, create a separate region for each
957             // expansion, and for all regions, update the StartLoc. Branch
958             // regions should not be split in order to keep a straightforward
959             // correspondance between the region and its associated branch
960             // condition, even if the condition spans multiple depths.
961             SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc);
962             assert(SM.isWrittenInSameFile(StartLoc, NestedLoc));
963 
964             if (!isBranch && !isRegionAlreadyAdded(StartLoc, NestedLoc))
965               SourceRegions.emplace_back(Region.getCounter(), StartLoc,
966                                          NestedLoc);
967 
968             StartLoc = getIncludeOrExpansionLoc(StartLoc);
969             if (StartLoc.isInvalid())
970               llvm::report_fatal_error(
971                   "File exit not handled before popRegions");
972             StartDepth--;
973           }
974         }
975         Region.setStartLoc(StartLoc);
976         Region.setEndLoc(EndLoc);
977 
978         if (!isBranch) {
979           MostRecentLocation = EndLoc;
980           // If this region happens to span an entire expansion, we need to
981           // make sure we don't overlap the parent region with it.
982           if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
983               EndLoc == getEndOfFileOrMacro(EndLoc))
984             MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
985         }
986 
987         assert(SM.isWrittenInSameFile(Region.getBeginLoc(), EndLoc));
988         assert(SpellingRegion(SM, Region).isInSourceOrder());
989         SourceRegions.push_back(Region);
990       }
991       RegionStack.pop_back();
992     }
993   }
994 
995   /// Return the currently active region.
996   SourceMappingRegion &getRegion() {
997     assert(!RegionStack.empty() && "statement has no region");
998     return RegionStack.back();
999   }
1000 
1001   /// Propagate counts through the children of \p S if \p VisitChildren is true.
1002   /// Otherwise, only emit a count for \p S itself.
1003   Counter propagateCounts(Counter TopCount, const Stmt *S,
1004                           bool VisitChildren = true) {
1005     SourceLocation StartLoc = getStart(S);
1006     SourceLocation EndLoc = getEnd(S);
1007     size_t Index = pushRegion(TopCount, StartLoc, EndLoc);
1008     if (VisitChildren)
1009       Visit(S);
1010     Counter ExitCount = getRegion().getCounter();
1011     popRegions(Index);
1012 
1013     // The statement may be spanned by an expansion. Make sure we handle a file
1014     // exit out of this expansion before moving to the next statement.
1015     if (SM.isBeforeInTranslationUnit(StartLoc, S->getBeginLoc()))
1016       MostRecentLocation = EndLoc;
1017 
1018     return ExitCount;
1019   }
1020 
1021   /// Determine whether the given condition can be constant folded.
1022   bool ConditionFoldsToBool(const Expr *Cond) {
1023     Expr::EvalResult Result;
1024     return (Cond->EvaluateAsInt(Result, CVM.getCodeGenModule().getContext()));
1025   }
1026 
1027   using MCDCDecisionIDPair = MCDCCoverageBuilder::DecisionIDPair;
1028 
1029   /// Create a Branch Region around an instrumentable condition for coverage
1030   /// and add it to the function's SourceRegions.  A branch region tracks a
1031   /// "True" counter and a "False" counter for boolean expressions that
1032   /// result in the generation of a branch.
1033   void
1034   createBranchRegion(const Expr *C, Counter TrueCnt, Counter FalseCnt,
1035                      const MCDCDecisionIDPair &IDPair = MCDCDecisionIDPair()) {
1036     // Check for NULL conditions.
1037     if (!C)
1038       return;
1039 
1040     // Ensure we are an instrumentable condition (i.e. no "&&" or "||").  Push
1041     // region onto RegionStack but immediately pop it (which adds it to the
1042     // function's SourceRegions) because it doesn't apply to any other source
1043     // code other than the Condition.
1044     if (CodeGenFunction::isInstrumentedCondition(C)) {
1045       MCDCConditionID ID = MCDCBuilder.getCondID(C);
1046       MCDCConditionID TrueID = IDPair.TrueID;
1047       MCDCConditionID FalseID = IDPair.FalseID;
1048 
1049       // If a condition can fold to true or false, the corresponding branch
1050       // will be removed.  Create a region with both counters hard-coded to
1051       // zero. This allows us to visualize them in a special way.
1052       // Alternatively, we can prevent any optimization done via
1053       // constant-folding by ensuring that ConstantFoldsToSimpleInteger() in
1054       // CodeGenFunction.c always returns false, but that is very heavy-handed.
1055       if (ConditionFoldsToBool(C))
1056         popRegions(pushRegion(Counter::getZero(), getStart(C), getEnd(C),
1057                               Counter::getZero(), ID, TrueID, FalseID));
1058       else
1059         // Otherwise, create a region with the True counter and False counter.
1060         popRegions(pushRegion(TrueCnt, getStart(C), getEnd(C), FalseCnt, ID,
1061                               TrueID, FalseID));
1062     }
1063   }
1064 
1065   /// Create a Decision Region with a BitmapIdx and number of Conditions. This
1066   /// type of region "contains" branch regions, one for each of the conditions.
1067   /// The visualization tool will group everything together.
1068   void createDecisionRegion(const Expr *C, unsigned BitmapIdx, unsigned Conds) {
1069     popRegions(pushRegion(BitmapIdx, Conds, getStart(C), getEnd(C)));
1070   }
1071 
1072   /// Create a Branch Region around a SwitchCase for code coverage
1073   /// and add it to the function's SourceRegions.
1074   void createSwitchCaseRegion(const SwitchCase *SC, Counter TrueCnt,
1075                               Counter FalseCnt) {
1076     // Push region onto RegionStack but immediately pop it (which adds it to
1077     // the function's SourceRegions) because it doesn't apply to any other
1078     // source other than the SwitchCase.
1079     popRegions(pushRegion(TrueCnt, getStart(SC), SC->getColonLoc(), FalseCnt));
1080   }
1081 
1082   /// Check whether a region with bounds \c StartLoc and \c EndLoc
1083   /// is already added to \c SourceRegions.
1084   bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc,
1085                             bool isBranch = false) {
1086     return llvm::any_of(
1087         llvm::reverse(SourceRegions), [&](const SourceMappingRegion &Region) {
1088           return Region.getBeginLoc() == StartLoc &&
1089                  Region.getEndLoc() == EndLoc && Region.isBranch() == isBranch;
1090         });
1091   }
1092 
1093   /// Adjust the most recently visited location to \c EndLoc.
1094   ///
1095   /// This should be used after visiting any statements in non-source order.
1096   void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
1097     MostRecentLocation = EndLoc;
1098     // The code region for a whole macro is created in handleFileExit() when
1099     // it detects exiting of the virtual file of that macro. If we visited
1100     // statements in non-source order, we might already have such a region
1101     // added, for example, if a body of a loop is divided among multiple
1102     // macros. Avoid adding duplicate regions in such case.
1103     if (getRegion().hasEndLoc() &&
1104         MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) &&
1105         isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
1106                              MostRecentLocation, getRegion().isBranch()))
1107       MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
1108   }
1109 
1110   /// Adjust regions and state when \c NewLoc exits a file.
1111   ///
1112   /// If moving from our most recently tracked location to \c NewLoc exits any
1113   /// files, this adjusts our current region stack and creates the file regions
1114   /// for the exited file.
1115   void handleFileExit(SourceLocation NewLoc) {
1116     if (NewLoc.isInvalid() ||
1117         SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
1118       return;
1119 
1120     // If NewLoc is not in a file that contains MostRecentLocation, walk up to
1121     // find the common ancestor.
1122     SourceLocation LCA = NewLoc;
1123     FileID ParentFile = SM.getFileID(LCA);
1124     while (!isNestedIn(MostRecentLocation, ParentFile)) {
1125       LCA = getIncludeOrExpansionLoc(LCA);
1126       if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
1127         // Since there isn't a common ancestor, no file was exited. We just need
1128         // to adjust our location to the new file.
1129         MostRecentLocation = NewLoc;
1130         return;
1131       }
1132       ParentFile = SM.getFileID(LCA);
1133     }
1134 
1135     llvm::SmallSet<SourceLocation, 8> StartLocs;
1136     std::optional<Counter> ParentCounter;
1137     for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
1138       if (!I.hasStartLoc())
1139         continue;
1140       SourceLocation Loc = I.getBeginLoc();
1141       if (!isNestedIn(Loc, ParentFile)) {
1142         ParentCounter = I.getCounter();
1143         break;
1144       }
1145 
1146       while (!SM.isInFileID(Loc, ParentFile)) {
1147         // The most nested region for each start location is the one with the
1148         // correct count. We avoid creating redundant regions by stopping once
1149         // we've seen this region.
1150         if (StartLocs.insert(Loc).second) {
1151           if (I.isBranch())
1152             SourceRegions.emplace_back(
1153                 I.getCounter(), I.getFalseCounter(),
1154                 MCDCParameters{0, 0, I.getMCDCParams().ID,
1155                                I.getMCDCParams().TrueID,
1156                                I.getMCDCParams().FalseID},
1157                 Loc, getEndOfFileOrMacro(Loc), I.isBranch());
1158           else
1159             SourceRegions.emplace_back(I.getCounter(), Loc,
1160                                        getEndOfFileOrMacro(Loc));
1161         }
1162         Loc = getIncludeOrExpansionLoc(Loc);
1163       }
1164       I.setStartLoc(getPreciseTokenLocEnd(Loc));
1165     }
1166 
1167     if (ParentCounter) {
1168       // If the file is contained completely by another region and doesn't
1169       // immediately start its own region, the whole file gets a region
1170       // corresponding to the parent.
1171       SourceLocation Loc = MostRecentLocation;
1172       while (isNestedIn(Loc, ParentFile)) {
1173         SourceLocation FileStart = getStartOfFileOrMacro(Loc);
1174         if (StartLocs.insert(FileStart).second) {
1175           SourceRegions.emplace_back(*ParentCounter, FileStart,
1176                                      getEndOfFileOrMacro(Loc));
1177           assert(SpellingRegion(SM, SourceRegions.back()).isInSourceOrder());
1178         }
1179         Loc = getIncludeOrExpansionLoc(Loc);
1180       }
1181     }
1182 
1183     MostRecentLocation = NewLoc;
1184   }
1185 
1186   /// Ensure that \c S is included in the current region.
1187   void extendRegion(const Stmt *S) {
1188     SourceMappingRegion &Region = getRegion();
1189     SourceLocation StartLoc = getStart(S);
1190 
1191     handleFileExit(StartLoc);
1192     if (!Region.hasStartLoc())
1193       Region.setStartLoc(StartLoc);
1194   }
1195 
1196   /// Mark \c S as a terminator, starting a zero region.
1197   void terminateRegion(const Stmt *S) {
1198     extendRegion(S);
1199     SourceMappingRegion &Region = getRegion();
1200     SourceLocation EndLoc = getEnd(S);
1201     if (!Region.hasEndLoc())
1202       Region.setEndLoc(EndLoc);
1203     pushRegion(Counter::getZero());
1204     HasTerminateStmt = true;
1205   }
1206 
1207   /// Find a valid gap range between \p AfterLoc and \p BeforeLoc.
1208   std::optional<SourceRange> findGapAreaBetween(SourceLocation AfterLoc,
1209                                                 SourceLocation BeforeLoc) {
1210     // If AfterLoc is in function-like macro, use the right parenthesis
1211     // location.
1212     if (AfterLoc.isMacroID()) {
1213       FileID FID = SM.getFileID(AfterLoc);
1214       const SrcMgr::ExpansionInfo *EI = &SM.getSLocEntry(FID).getExpansion();
1215       if (EI->isFunctionMacroExpansion())
1216         AfterLoc = EI->getExpansionLocEnd();
1217     }
1218 
1219     size_t StartDepth = locationDepth(AfterLoc);
1220     size_t EndDepth = locationDepth(BeforeLoc);
1221     while (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc)) {
1222       bool UnnestStart = StartDepth >= EndDepth;
1223       bool UnnestEnd = EndDepth >= StartDepth;
1224       if (UnnestEnd) {
1225         assert(SM.isWrittenInSameFile(getStartOfFileOrMacro(BeforeLoc),
1226                                       BeforeLoc));
1227 
1228         BeforeLoc = getIncludeOrExpansionLoc(BeforeLoc);
1229         assert(BeforeLoc.isValid());
1230         EndDepth--;
1231       }
1232       if (UnnestStart) {
1233         assert(SM.isWrittenInSameFile(AfterLoc,
1234                                       getEndOfFileOrMacro(AfterLoc)));
1235 
1236         AfterLoc = getIncludeOrExpansionLoc(AfterLoc);
1237         assert(AfterLoc.isValid());
1238         AfterLoc = getPreciseTokenLocEnd(AfterLoc);
1239         assert(AfterLoc.isValid());
1240         StartDepth--;
1241       }
1242     }
1243     AfterLoc = getPreciseTokenLocEnd(AfterLoc);
1244     // If the start and end locations of the gap are both within the same macro
1245     // file, the range may not be in source order.
1246     if (AfterLoc.isMacroID() || BeforeLoc.isMacroID())
1247       return std::nullopt;
1248     if (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc) ||
1249         !SpellingRegion(SM, AfterLoc, BeforeLoc).isInSourceOrder())
1250       return std::nullopt;
1251     return {{AfterLoc, BeforeLoc}};
1252   }
1253 
1254   /// Emit a gap region between \p StartLoc and \p EndLoc with the given count.
1255   void fillGapAreaWithCount(SourceLocation StartLoc, SourceLocation EndLoc,
1256                             Counter Count) {
1257     if (StartLoc == EndLoc)
1258       return;
1259     assert(SpellingRegion(SM, StartLoc, EndLoc).isInSourceOrder());
1260     handleFileExit(StartLoc);
1261     size_t Index = pushRegion(Count, StartLoc, EndLoc);
1262     getRegion().setGap(true);
1263     handleFileExit(EndLoc);
1264     popRegions(Index);
1265   }
1266 
1267   /// Find a valid range starting with \p StartingLoc and ending before \p
1268   /// BeforeLoc.
1269   std::optional<SourceRange> findAreaStartingFromTo(SourceLocation StartingLoc,
1270                                                     SourceLocation BeforeLoc) {
1271     // If StartingLoc is in function-like macro, use its start location.
1272     if (StartingLoc.isMacroID()) {
1273       FileID FID = SM.getFileID(StartingLoc);
1274       const SrcMgr::ExpansionInfo *EI = &SM.getSLocEntry(FID).getExpansion();
1275       if (EI->isFunctionMacroExpansion())
1276         StartingLoc = EI->getExpansionLocStart();
1277     }
1278 
1279     size_t StartDepth = locationDepth(StartingLoc);
1280     size_t EndDepth = locationDepth(BeforeLoc);
1281     while (!SM.isWrittenInSameFile(StartingLoc, BeforeLoc)) {
1282       bool UnnestStart = StartDepth >= EndDepth;
1283       bool UnnestEnd = EndDepth >= StartDepth;
1284       if (UnnestEnd) {
1285         assert(SM.isWrittenInSameFile(getStartOfFileOrMacro(BeforeLoc),
1286                                       BeforeLoc));
1287 
1288         BeforeLoc = getIncludeOrExpansionLoc(BeforeLoc);
1289         assert(BeforeLoc.isValid());
1290         EndDepth--;
1291       }
1292       if (UnnestStart) {
1293         assert(SM.isWrittenInSameFile(StartingLoc,
1294                                       getStartOfFileOrMacro(StartingLoc)));
1295 
1296         StartingLoc = getIncludeOrExpansionLoc(StartingLoc);
1297         assert(StartingLoc.isValid());
1298         StartDepth--;
1299       }
1300     }
1301     // If the start and end locations of the gap are both within the same macro
1302     // file, the range may not be in source order.
1303     if (StartingLoc.isMacroID() || BeforeLoc.isMacroID())
1304       return std::nullopt;
1305     if (!SM.isWrittenInSameFile(StartingLoc, BeforeLoc) ||
1306         !SpellingRegion(SM, StartingLoc, BeforeLoc).isInSourceOrder())
1307       return std::nullopt;
1308     return {{StartingLoc, BeforeLoc}};
1309   }
1310 
1311   void markSkipped(SourceLocation StartLoc, SourceLocation BeforeLoc) {
1312     const auto Skipped = findAreaStartingFromTo(StartLoc, BeforeLoc);
1313 
1314     if (!Skipped)
1315       return;
1316 
1317     const auto NewStartLoc = Skipped->getBegin();
1318     const auto EndLoc = Skipped->getEnd();
1319 
1320     if (NewStartLoc == EndLoc)
1321       return;
1322     assert(SpellingRegion(SM, NewStartLoc, EndLoc).isInSourceOrder());
1323     handleFileExit(NewStartLoc);
1324     size_t Index = pushRegion({}, NewStartLoc, EndLoc);
1325     getRegion().setSkipped(true);
1326     handleFileExit(EndLoc);
1327     popRegions(Index);
1328   }
1329 
1330   /// Keep counts of breaks and continues inside loops.
1331   struct BreakContinue {
1332     Counter BreakCount;
1333     Counter ContinueCount;
1334   };
1335   SmallVector<BreakContinue, 8> BreakContinueStack;
1336 
1337   CounterCoverageMappingBuilder(
1338       CoverageMappingModuleGen &CVM,
1339       llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
1340       llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap,
1341       llvm::DenseMap<const Stmt *, MCDCConditionID> &CondIDMap,
1342       SourceManager &SM, const LangOptions &LangOpts)
1343       : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
1344         MCDCBitmapMap(MCDCBitmapMap),
1345         MCDCBuilder(CVM.getCodeGenModule(), CondIDMap, MCDCBitmapMap) {}
1346 
1347   /// Write the mapping data to the output stream
1348   void write(llvm::raw_ostream &OS) {
1349     llvm::SmallVector<unsigned, 8> VirtualFileMapping;
1350     gatherFileIDs(VirtualFileMapping);
1351     SourceRegionFilter Filter = emitExpansionRegions();
1352     emitSourceRegions(Filter);
1353     gatherSkippedRegions();
1354 
1355     if (MappingRegions.empty())
1356       return;
1357 
1358     CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
1359                                  MappingRegions);
1360     Writer.write(OS);
1361   }
1362 
1363   void VisitStmt(const Stmt *S) {
1364     if (S->getBeginLoc().isValid())
1365       extendRegion(S);
1366     const Stmt *LastStmt = nullptr;
1367     bool SaveTerminateStmt = HasTerminateStmt;
1368     HasTerminateStmt = false;
1369     GapRegionCounter = Counter::getZero();
1370     for (const Stmt *Child : S->children())
1371       if (Child) {
1372         // If last statement contains terminate statements, add a gap area
1373         // between the two statements. Skipping attributed statements, because
1374         // they don't have valid start location.
1375         if (LastStmt && HasTerminateStmt && !isa<AttributedStmt>(Child)) {
1376           auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child));
1377           if (Gap)
1378             fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(),
1379                                  GapRegionCounter);
1380           SaveTerminateStmt = true;
1381           HasTerminateStmt = false;
1382         }
1383         this->Visit(Child);
1384         LastStmt = Child;
1385       }
1386     if (SaveTerminateStmt)
1387       HasTerminateStmt = true;
1388     handleFileExit(getEnd(S));
1389   }
1390 
1391   void VisitDecl(const Decl *D) {
1392     Stmt *Body = D->getBody();
1393 
1394     // Do not propagate region counts into system headers unless collecting
1395     // coverage from system headers is explicitly enabled.
1396     if (!SystemHeadersCoverage && Body &&
1397         SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
1398       return;
1399 
1400     // Do not visit the artificial children nodes of defaulted methods. The
1401     // lexer may not be able to report back precise token end locations for
1402     // these children nodes (llvm.org/PR39822), and moreover users will not be
1403     // able to see coverage for them.
1404     Counter BodyCounter = getRegionCounter(Body);
1405     bool Defaulted = false;
1406     if (auto *Method = dyn_cast<CXXMethodDecl>(D))
1407       Defaulted = Method->isDefaulted();
1408     if (auto *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1409       for (auto *Initializer : Ctor->inits()) {
1410         if (Initializer->isWritten()) {
1411           auto *Init = Initializer->getInit();
1412           if (getStart(Init).isValid() && getEnd(Init).isValid())
1413             propagateCounts(BodyCounter, Init);
1414         }
1415       }
1416     }
1417 
1418     propagateCounts(BodyCounter, Body,
1419                     /*VisitChildren=*/!Defaulted);
1420     assert(RegionStack.empty() && "Regions entered but never exited");
1421   }
1422 
1423   void VisitReturnStmt(const ReturnStmt *S) {
1424     extendRegion(S);
1425     if (S->getRetValue())
1426       Visit(S->getRetValue());
1427     terminateRegion(S);
1428   }
1429 
1430   void VisitCoroutineBodyStmt(const CoroutineBodyStmt *S) {
1431     extendRegion(S);
1432     Visit(S->getBody());
1433   }
1434 
1435   void VisitCoreturnStmt(const CoreturnStmt *S) {
1436     extendRegion(S);
1437     if (S->getOperand())
1438       Visit(S->getOperand());
1439     terminateRegion(S);
1440   }
1441 
1442   void VisitCXXThrowExpr(const CXXThrowExpr *E) {
1443     extendRegion(E);
1444     if (E->getSubExpr())
1445       Visit(E->getSubExpr());
1446     terminateRegion(E);
1447   }
1448 
1449   void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
1450 
1451   void VisitLabelStmt(const LabelStmt *S) {
1452     Counter LabelCount = getRegionCounter(S);
1453     SourceLocation Start = getStart(S);
1454     // We can't extendRegion here or we risk overlapping with our new region.
1455     handleFileExit(Start);
1456     pushRegion(LabelCount, Start);
1457     Visit(S->getSubStmt());
1458   }
1459 
1460   void VisitBreakStmt(const BreakStmt *S) {
1461     assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
1462     BreakContinueStack.back().BreakCount = addCounters(
1463         BreakContinueStack.back().BreakCount, getRegion().getCounter());
1464     // FIXME: a break in a switch should terminate regions for all preceding
1465     // case statements, not just the most recent one.
1466     terminateRegion(S);
1467   }
1468 
1469   void VisitContinueStmt(const ContinueStmt *S) {
1470     assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
1471     BreakContinueStack.back().ContinueCount = addCounters(
1472         BreakContinueStack.back().ContinueCount, getRegion().getCounter());
1473     terminateRegion(S);
1474   }
1475 
1476   void VisitCallExpr(const CallExpr *E) {
1477     VisitStmt(E);
1478 
1479     // Terminate the region when we hit a noreturn function.
1480     // (This is helpful dealing with switch statements.)
1481     QualType CalleeType = E->getCallee()->getType();
1482     if (getFunctionExtInfo(*CalleeType).getNoReturn())
1483       terminateRegion(E);
1484   }
1485 
1486   void VisitWhileStmt(const WhileStmt *S) {
1487     extendRegion(S);
1488 
1489     Counter ParentCount = getRegion().getCounter();
1490     Counter BodyCount = getRegionCounter(S);
1491 
1492     // Handle the body first so that we can get the backedge count.
1493     BreakContinueStack.push_back(BreakContinue());
1494     extendRegion(S->getBody());
1495     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1496     BreakContinue BC = BreakContinueStack.pop_back_val();
1497 
1498     bool BodyHasTerminateStmt = HasTerminateStmt;
1499     HasTerminateStmt = false;
1500 
1501     // Go back to handle the condition.
1502     Counter CondCount =
1503         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1504     propagateCounts(CondCount, S->getCond());
1505     adjustForOutOfOrderTraversal(getEnd(S));
1506 
1507     // The body count applies to the area immediately after the increment.
1508     auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1509     if (Gap)
1510       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1511 
1512     Counter OutCount =
1513         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
1514     if (OutCount != ParentCount) {
1515       pushRegion(OutCount);
1516       GapRegionCounter = OutCount;
1517       if (BodyHasTerminateStmt)
1518         HasTerminateStmt = true;
1519     }
1520 
1521     // Create Branch Region around condition.
1522     createBranchRegion(S->getCond(), BodyCount,
1523                        subtractCounters(CondCount, BodyCount));
1524   }
1525 
1526   void VisitDoStmt(const DoStmt *S) {
1527     extendRegion(S);
1528 
1529     Counter ParentCount = getRegion().getCounter();
1530     Counter BodyCount = getRegionCounter(S);
1531 
1532     BreakContinueStack.push_back(BreakContinue());
1533     extendRegion(S->getBody());
1534     Counter BackedgeCount =
1535         propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
1536     BreakContinue BC = BreakContinueStack.pop_back_val();
1537 
1538     bool BodyHasTerminateStmt = HasTerminateStmt;
1539     HasTerminateStmt = false;
1540 
1541     Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
1542     propagateCounts(CondCount, S->getCond());
1543 
1544     Counter OutCount =
1545         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
1546     if (OutCount != ParentCount) {
1547       pushRegion(OutCount);
1548       GapRegionCounter = OutCount;
1549     }
1550 
1551     // Create Branch Region around condition.
1552     createBranchRegion(S->getCond(), BodyCount,
1553                        subtractCounters(CondCount, BodyCount));
1554 
1555     if (BodyHasTerminateStmt)
1556       HasTerminateStmt = true;
1557   }
1558 
1559   void VisitForStmt(const ForStmt *S) {
1560     extendRegion(S);
1561     if (S->getInit())
1562       Visit(S->getInit());
1563 
1564     Counter ParentCount = getRegion().getCounter();
1565     Counter BodyCount = getRegionCounter(S);
1566 
1567     // The loop increment may contain a break or continue.
1568     if (S->getInc())
1569       BreakContinueStack.emplace_back();
1570 
1571     // Handle the body first so that we can get the backedge count.
1572     BreakContinueStack.emplace_back();
1573     extendRegion(S->getBody());
1574     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1575     BreakContinue BodyBC = BreakContinueStack.pop_back_val();
1576 
1577     bool BodyHasTerminateStmt = HasTerminateStmt;
1578     HasTerminateStmt = false;
1579 
1580     // The increment is essentially part of the body but it needs to include
1581     // the count for all the continue statements.
1582     BreakContinue IncrementBC;
1583     if (const Stmt *Inc = S->getInc()) {
1584       propagateCounts(addCounters(BackedgeCount, BodyBC.ContinueCount), Inc);
1585       IncrementBC = BreakContinueStack.pop_back_val();
1586     }
1587 
1588     // Go back to handle the condition.
1589     Counter CondCount = addCounters(
1590         addCounters(ParentCount, BackedgeCount, BodyBC.ContinueCount),
1591         IncrementBC.ContinueCount);
1592     if (const Expr *Cond = S->getCond()) {
1593       propagateCounts(CondCount, Cond);
1594       adjustForOutOfOrderTraversal(getEnd(S));
1595     }
1596 
1597     // The body count applies to the area immediately after the increment.
1598     auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1599     if (Gap)
1600       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1601 
1602     Counter OutCount = addCounters(BodyBC.BreakCount, IncrementBC.BreakCount,
1603                                    subtractCounters(CondCount, BodyCount));
1604     if (OutCount != ParentCount) {
1605       pushRegion(OutCount);
1606       GapRegionCounter = OutCount;
1607       if (BodyHasTerminateStmt)
1608         HasTerminateStmt = true;
1609     }
1610 
1611     // Create Branch Region around condition.
1612     createBranchRegion(S->getCond(), BodyCount,
1613                        subtractCounters(CondCount, BodyCount));
1614   }
1615 
1616   void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
1617     extendRegion(S);
1618     if (S->getInit())
1619       Visit(S->getInit());
1620     Visit(S->getLoopVarStmt());
1621     Visit(S->getRangeStmt());
1622 
1623     Counter ParentCount = getRegion().getCounter();
1624     Counter BodyCount = getRegionCounter(S);
1625 
1626     BreakContinueStack.push_back(BreakContinue());
1627     extendRegion(S->getBody());
1628     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1629     BreakContinue BC = BreakContinueStack.pop_back_val();
1630 
1631     bool BodyHasTerminateStmt = HasTerminateStmt;
1632     HasTerminateStmt = false;
1633 
1634     // The body count applies to the area immediately after the range.
1635     auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1636     if (Gap)
1637       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1638 
1639     Counter LoopCount =
1640         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1641     Counter OutCount =
1642         addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
1643     if (OutCount != ParentCount) {
1644       pushRegion(OutCount);
1645       GapRegionCounter = OutCount;
1646       if (BodyHasTerminateStmt)
1647         HasTerminateStmt = true;
1648     }
1649 
1650     // Create Branch Region around condition.
1651     createBranchRegion(S->getCond(), BodyCount,
1652                        subtractCounters(LoopCount, BodyCount));
1653   }
1654 
1655   void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
1656     extendRegion(S);
1657     Visit(S->getElement());
1658 
1659     Counter ParentCount = getRegion().getCounter();
1660     Counter BodyCount = getRegionCounter(S);
1661 
1662     BreakContinueStack.push_back(BreakContinue());
1663     extendRegion(S->getBody());
1664     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1665     BreakContinue BC = BreakContinueStack.pop_back_val();
1666 
1667     // The body count applies to the area immediately after the collection.
1668     auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1669     if (Gap)
1670       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1671 
1672     Counter LoopCount =
1673         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1674     Counter OutCount =
1675         addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
1676     if (OutCount != ParentCount) {
1677       pushRegion(OutCount);
1678       GapRegionCounter = OutCount;
1679     }
1680   }
1681 
1682   void VisitSwitchStmt(const SwitchStmt *S) {
1683     extendRegion(S);
1684     if (S->getInit())
1685       Visit(S->getInit());
1686     Visit(S->getCond());
1687 
1688     BreakContinueStack.push_back(BreakContinue());
1689 
1690     const Stmt *Body = S->getBody();
1691     extendRegion(Body);
1692     if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
1693       if (!CS->body_empty()) {
1694         // Make a region for the body of the switch.  If the body starts with
1695         // a case, that case will reuse this region; otherwise, this covers
1696         // the unreachable code at the beginning of the switch body.
1697         size_t Index = pushRegion(Counter::getZero(), getStart(CS));
1698         getRegion().setGap(true);
1699         Visit(Body);
1700 
1701         // Set the end for the body of the switch, if it isn't already set.
1702         for (size_t i = RegionStack.size(); i != Index; --i) {
1703           if (!RegionStack[i - 1].hasEndLoc())
1704             RegionStack[i - 1].setEndLoc(getEnd(CS->body_back()));
1705         }
1706 
1707         popRegions(Index);
1708       }
1709     } else
1710       propagateCounts(Counter::getZero(), Body);
1711     BreakContinue BC = BreakContinueStack.pop_back_val();
1712 
1713     if (!BreakContinueStack.empty())
1714       BreakContinueStack.back().ContinueCount = addCounters(
1715           BreakContinueStack.back().ContinueCount, BC.ContinueCount);
1716 
1717     Counter ParentCount = getRegion().getCounter();
1718     Counter ExitCount = getRegionCounter(S);
1719     SourceLocation ExitLoc = getEnd(S);
1720     pushRegion(ExitCount);
1721     GapRegionCounter = ExitCount;
1722 
1723     // Ensure that handleFileExit recognizes when the end location is located
1724     // in a different file.
1725     MostRecentLocation = getStart(S);
1726     handleFileExit(ExitLoc);
1727 
1728     // Create a Branch Region around each Case. Subtract the case's
1729     // counter from the Parent counter to track the "False" branch count.
1730     Counter CaseCountSum;
1731     bool HasDefaultCase = false;
1732     const SwitchCase *Case = S->getSwitchCaseList();
1733     for (; Case; Case = Case->getNextSwitchCase()) {
1734       HasDefaultCase = HasDefaultCase || isa<DefaultStmt>(Case);
1735       CaseCountSum =
1736           addCounters(CaseCountSum, getRegionCounter(Case), /*Simplify=*/false);
1737       createSwitchCaseRegion(
1738           Case, getRegionCounter(Case),
1739           subtractCounters(ParentCount, getRegionCounter(Case)));
1740     }
1741     // Simplify is skipped while building the counters above: it can get really
1742     // slow on top of switches with thousands of cases. Instead, trigger
1743     // simplification by adding zero to the last counter.
1744     CaseCountSum = addCounters(CaseCountSum, Counter::getZero());
1745 
1746     // If no explicit default case exists, create a branch region to represent
1747     // the hidden branch, which will be added later by the CodeGen. This region
1748     // will be associated with the switch statement's condition.
1749     if (!HasDefaultCase) {
1750       Counter DefaultTrue = subtractCounters(ParentCount, CaseCountSum);
1751       Counter DefaultFalse = subtractCounters(ParentCount, DefaultTrue);
1752       createBranchRegion(S->getCond(), DefaultTrue, DefaultFalse);
1753     }
1754   }
1755 
1756   void VisitSwitchCase(const SwitchCase *S) {
1757     extendRegion(S);
1758 
1759     SourceMappingRegion &Parent = getRegion();
1760 
1761     Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
1762     // Reuse the existing region if it starts at our label. This is typical of
1763     // the first case in a switch.
1764     if (Parent.hasStartLoc() && Parent.getBeginLoc() == getStart(S))
1765       Parent.setCounter(Count);
1766     else
1767       pushRegion(Count, getStart(S));
1768 
1769     GapRegionCounter = Count;
1770 
1771     if (const auto *CS = dyn_cast<CaseStmt>(S)) {
1772       Visit(CS->getLHS());
1773       if (const Expr *RHS = CS->getRHS())
1774         Visit(RHS);
1775     }
1776     Visit(S->getSubStmt());
1777   }
1778 
1779   void coverIfConsteval(const IfStmt *S) {
1780     assert(S->isConsteval());
1781 
1782     const auto *Then = S->getThen();
1783     const auto *Else = S->getElse();
1784 
1785     // It's better for llvm-cov to create a new region with same counter
1786     // so line-coverage can be properly calculated for lines containing
1787     // a skipped region (without it the line is marked uncovered)
1788     const Counter ParentCount = getRegion().getCounter();
1789 
1790     extendRegion(S);
1791 
1792     if (S->isNegatedConsteval()) {
1793       // ignore 'if consteval'
1794       markSkipped(S->getIfLoc(), getStart(Then));
1795       propagateCounts(ParentCount, Then);
1796 
1797       if (Else) {
1798         // ignore 'else <else>'
1799         markSkipped(getEnd(Then), getEnd(Else));
1800       }
1801     } else {
1802       assert(S->isNonNegatedConsteval());
1803       // ignore 'if consteval <then> [else]'
1804       markSkipped(S->getIfLoc(), Else ? getStart(Else) : getEnd(Then));
1805 
1806       if (Else)
1807         propagateCounts(ParentCount, Else);
1808     }
1809   }
1810 
1811   void coverIfConstexpr(const IfStmt *S) {
1812     assert(S->isConstexpr());
1813 
1814     // evaluate constant condition...
1815     const bool isTrue =
1816         S->getCond()
1817             ->EvaluateKnownConstInt(CVM.getCodeGenModule().getContext())
1818             .getBoolValue();
1819 
1820     extendRegion(S);
1821 
1822     // I'm using 'propagateCounts' later as new region is better and allows me
1823     // to properly calculate line coverage in llvm-cov utility
1824     const Counter ParentCount = getRegion().getCounter();
1825 
1826     // ignore 'if constexpr ('
1827     SourceLocation startOfSkipped = S->getIfLoc();
1828 
1829     if (const auto *Init = S->getInit()) {
1830       const auto start = getStart(Init);
1831       const auto end = getEnd(Init);
1832 
1833       // this check is to make sure typedef here which doesn't have valid source
1834       // location won't crash it
1835       if (start.isValid() && end.isValid()) {
1836         markSkipped(startOfSkipped, start);
1837         propagateCounts(ParentCount, Init);
1838         startOfSkipped = getEnd(Init);
1839       }
1840     }
1841 
1842     const auto *Then = S->getThen();
1843     const auto *Else = S->getElse();
1844 
1845     if (isTrue) {
1846       // ignore '<condition>)'
1847       markSkipped(startOfSkipped, getStart(Then));
1848       propagateCounts(ParentCount, Then);
1849 
1850       if (Else)
1851         // ignore 'else <else>'
1852         markSkipped(getEnd(Then), getEnd(Else));
1853     } else {
1854       // ignore '<condition>) <then> [else]'
1855       markSkipped(startOfSkipped, Else ? getStart(Else) : getEnd(Then));
1856 
1857       if (Else)
1858         propagateCounts(ParentCount, Else);
1859     }
1860   }
1861 
1862   void VisitIfStmt(const IfStmt *S) {
1863     // "if constexpr" and "if consteval" are not normal conditional statements,
1864     // their discarded statement should be skipped
1865     if (S->isConsteval())
1866       return coverIfConsteval(S);
1867     else if (S->isConstexpr())
1868       return coverIfConstexpr(S);
1869 
1870     extendRegion(S);
1871     if (S->getInit())
1872       Visit(S->getInit());
1873 
1874     // Extend into the condition before we propagate through it below - this is
1875     // needed to handle macros that generate the "if" but not the condition.
1876     extendRegion(S->getCond());
1877 
1878     Counter ParentCount = getRegion().getCounter();
1879     Counter ThenCount = getRegionCounter(S);
1880 
1881     // Emitting a counter for the condition makes it easier to interpret the
1882     // counter for the body when looking at the coverage.
1883     propagateCounts(ParentCount, S->getCond());
1884 
1885     // The 'then' count applies to the area immediately after the condition.
1886     std::optional<SourceRange> Gap =
1887         findGapAreaBetween(S->getRParenLoc(), getStart(S->getThen()));
1888     if (Gap)
1889       fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ThenCount);
1890 
1891     extendRegion(S->getThen());
1892     Counter OutCount = propagateCounts(ThenCount, S->getThen());
1893     Counter ElseCount = subtractCounters(ParentCount, ThenCount);
1894 
1895     if (const Stmt *Else = S->getElse()) {
1896       bool ThenHasTerminateStmt = HasTerminateStmt;
1897       HasTerminateStmt = false;
1898       // The 'else' count applies to the area immediately after the 'then'.
1899       std::optional<SourceRange> Gap =
1900           findGapAreaBetween(getEnd(S->getThen()), getStart(Else));
1901       if (Gap)
1902         fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ElseCount);
1903       extendRegion(Else);
1904       OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
1905 
1906       if (ThenHasTerminateStmt)
1907         HasTerminateStmt = true;
1908     } else
1909       OutCount = addCounters(OutCount, ElseCount);
1910 
1911     if (OutCount != ParentCount) {
1912       pushRegion(OutCount);
1913       GapRegionCounter = OutCount;
1914     }
1915 
1916     // Create Branch Region around condition.
1917     createBranchRegion(S->getCond(), ThenCount,
1918                        subtractCounters(ParentCount, ThenCount));
1919   }
1920 
1921   void VisitCXXTryStmt(const CXXTryStmt *S) {
1922     extendRegion(S);
1923     // Handle macros that generate the "try" but not the rest.
1924     extendRegion(S->getTryBlock());
1925 
1926     Counter ParentCount = getRegion().getCounter();
1927     propagateCounts(ParentCount, S->getTryBlock());
1928 
1929     for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
1930       Visit(S->getHandler(I));
1931 
1932     Counter ExitCount = getRegionCounter(S);
1933     pushRegion(ExitCount);
1934   }
1935 
1936   void VisitCXXCatchStmt(const CXXCatchStmt *S) {
1937     propagateCounts(getRegionCounter(S), S->getHandlerBlock());
1938   }
1939 
1940   void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
1941     extendRegion(E);
1942 
1943     Counter ParentCount = getRegion().getCounter();
1944     Counter TrueCount = getRegionCounter(E);
1945 
1946     propagateCounts(ParentCount, E->getCond());
1947     Counter OutCount;
1948 
1949     if (!isa<BinaryConditionalOperator>(E)) {
1950       // The 'then' count applies to the area immediately after the condition.
1951       auto Gap =
1952           findGapAreaBetween(E->getQuestionLoc(), getStart(E->getTrueExpr()));
1953       if (Gap)
1954         fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), TrueCount);
1955 
1956       extendRegion(E->getTrueExpr());
1957       OutCount = propagateCounts(TrueCount, E->getTrueExpr());
1958     }
1959 
1960     extendRegion(E->getFalseExpr());
1961     OutCount = addCounters(
1962         OutCount, propagateCounts(subtractCounters(ParentCount, TrueCount),
1963                                   E->getFalseExpr()));
1964 
1965     if (OutCount != ParentCount) {
1966       pushRegion(OutCount);
1967       GapRegionCounter = OutCount;
1968     }
1969 
1970     // Create Branch Region around condition.
1971     createBranchRegion(E->getCond(), TrueCount,
1972                        subtractCounters(ParentCount, TrueCount));
1973   }
1974 
1975   void VisitBinLAnd(const BinaryOperator *E) {
1976     bool IsRootNode = MCDCBuilder.isIdle();
1977 
1978     // Keep track of Binary Operator and assign MCDC condition IDs.
1979     MCDCBuilder.pushAndAssignIDs(E);
1980 
1981     extendRegion(E->getLHS());
1982     propagateCounts(getRegion().getCounter(), E->getLHS());
1983     handleFileExit(getEnd(E->getLHS()));
1984 
1985     // Track LHS True/False Decision.
1986     const auto DecisionLHS = MCDCBuilder.pop();
1987 
1988     // Counter tracks the right hand side of a logical and operator.
1989     extendRegion(E->getRHS());
1990     propagateCounts(getRegionCounter(E), E->getRHS());
1991 
1992     // Track RHS True/False Decision.
1993     const auto DecisionRHS = MCDCBuilder.back();
1994 
1995     // Create MCDC Decision Region if at top-level (root).
1996     unsigned NumConds = 0;
1997     if (IsRootNode && (NumConds = MCDCBuilder.getTotalConditionsAndReset(E)))
1998       createDecisionRegion(E, getRegionBitmap(E), NumConds);
1999 
2000     // Extract the RHS's Execution Counter.
2001     Counter RHSExecCnt = getRegionCounter(E);
2002 
2003     // Extract the RHS's "True" Instance Counter.
2004     Counter RHSTrueCnt = getRegionCounter(E->getRHS());
2005 
2006     // Extract the Parent Region Counter.
2007     Counter ParentCnt = getRegion().getCounter();
2008 
2009     // Create Branch Region around LHS condition.
2010     createBranchRegion(E->getLHS(), RHSExecCnt,
2011                        subtractCounters(ParentCnt, RHSExecCnt), DecisionLHS);
2012 
2013     // Create Branch Region around RHS condition.
2014     createBranchRegion(E->getRHS(), RHSTrueCnt,
2015                        subtractCounters(RHSExecCnt, RHSTrueCnt), DecisionRHS);
2016   }
2017 
2018   // Determine whether the right side of OR operation need to be visited.
2019   bool shouldVisitRHS(const Expr *LHS) {
2020     bool LHSIsTrue = false;
2021     bool LHSIsConst = false;
2022     if (!LHS->isValueDependent())
2023       LHSIsConst = LHS->EvaluateAsBooleanCondition(
2024           LHSIsTrue, CVM.getCodeGenModule().getContext());
2025     return !LHSIsConst || (LHSIsConst && !LHSIsTrue);
2026   }
2027 
2028   void VisitBinLOr(const BinaryOperator *E) {
2029     bool IsRootNode = MCDCBuilder.isIdle();
2030 
2031     // Keep track of Binary Operator and assign MCDC condition IDs.
2032     MCDCBuilder.pushAndAssignIDs(E);
2033 
2034     extendRegion(E->getLHS());
2035     Counter OutCount = propagateCounts(getRegion().getCounter(), E->getLHS());
2036     handleFileExit(getEnd(E->getLHS()));
2037 
2038     // Track LHS True/False Decision.
2039     const auto DecisionLHS = MCDCBuilder.pop();
2040 
2041     // Counter tracks the right hand side of a logical or operator.
2042     extendRegion(E->getRHS());
2043     propagateCounts(getRegionCounter(E), E->getRHS());
2044 
2045     // Track RHS True/False Decision.
2046     const auto DecisionRHS = MCDCBuilder.back();
2047 
2048     // Create MCDC Decision Region if at top-level (root).
2049     unsigned NumConds = 0;
2050     if (IsRootNode && (NumConds = MCDCBuilder.getTotalConditionsAndReset(E)))
2051       createDecisionRegion(E, getRegionBitmap(E), NumConds);
2052 
2053     // Extract the RHS's Execution Counter.
2054     Counter RHSExecCnt = getRegionCounter(E);
2055 
2056     // Extract the RHS's "False" Instance Counter.
2057     Counter RHSFalseCnt = getRegionCounter(E->getRHS());
2058 
2059     if (!shouldVisitRHS(E->getLHS())) {
2060       GapRegionCounter = OutCount;
2061     }
2062 
2063     // Extract the Parent Region Counter.
2064     Counter ParentCnt = getRegion().getCounter();
2065 
2066     // Create Branch Region around LHS condition.
2067     createBranchRegion(E->getLHS(), subtractCounters(ParentCnt, RHSExecCnt),
2068                        RHSExecCnt, DecisionLHS);
2069 
2070     // Create Branch Region around RHS condition.
2071     createBranchRegion(E->getRHS(), subtractCounters(RHSExecCnt, RHSFalseCnt),
2072                        RHSFalseCnt, DecisionRHS);
2073   }
2074 
2075   void VisitLambdaExpr(const LambdaExpr *LE) {
2076     // Lambdas are treated as their own functions for now, so we shouldn't
2077     // propagate counts into them.
2078   }
2079 
2080   void VisitPseudoObjectExpr(const PseudoObjectExpr *POE) {
2081     // Just visit syntatic expression as this is what users actually write.
2082     VisitStmt(POE->getSyntacticForm());
2083   }
2084 
2085   void VisitOpaqueValueExpr(const OpaqueValueExpr* OVE) {
2086     Visit(OVE->getSourceExpr());
2087   }
2088 };
2089 
2090 } // end anonymous namespace
2091 
2092 static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
2093                  ArrayRef<CounterExpression> Expressions,
2094                  ArrayRef<CounterMappingRegion> Regions) {
2095   OS << FunctionName << ":\n";
2096   CounterMappingContext Ctx(Expressions);
2097   for (const auto &R : Regions) {
2098     OS.indent(2);
2099     switch (R.Kind) {
2100     case CounterMappingRegion::CodeRegion:
2101       break;
2102     case CounterMappingRegion::ExpansionRegion:
2103       OS << "Expansion,";
2104       break;
2105     case CounterMappingRegion::SkippedRegion:
2106       OS << "Skipped,";
2107       break;
2108     case CounterMappingRegion::GapRegion:
2109       OS << "Gap,";
2110       break;
2111     case CounterMappingRegion::BranchRegion:
2112     case CounterMappingRegion::MCDCBranchRegion:
2113       OS << "Branch,";
2114       break;
2115     case CounterMappingRegion::MCDCDecisionRegion:
2116       OS << "Decision,";
2117       break;
2118     }
2119 
2120     OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
2121        << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
2122 
2123     if (R.Kind == CounterMappingRegion::MCDCDecisionRegion) {
2124       OS << "M:" << R.MCDCParams.BitmapIdx;
2125       OS << ", C:" << R.MCDCParams.NumConditions;
2126     } else {
2127       Ctx.dump(R.Count, OS);
2128 
2129       if (R.Kind == CounterMappingRegion::BranchRegion ||
2130           R.Kind == CounterMappingRegion::MCDCBranchRegion) {
2131         OS << ", ";
2132         Ctx.dump(R.FalseCount, OS);
2133       }
2134     }
2135 
2136     if (R.Kind == CounterMappingRegion::MCDCBranchRegion) {
2137       OS << " [" << R.MCDCParams.ID << "," << R.MCDCParams.TrueID;
2138       OS << "," << R.MCDCParams.FalseID << "] ";
2139     }
2140 
2141     if (R.Kind == CounterMappingRegion::ExpansionRegion)
2142       OS << " (Expanded file = " << R.ExpandedFileID << ")";
2143     OS << "\n";
2144   }
2145 }
2146 
2147 CoverageMappingModuleGen::CoverageMappingModuleGen(
2148     CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
2149     : CGM(CGM), SourceInfo(SourceInfo) {}
2150 
2151 std::string CoverageMappingModuleGen::getCurrentDirname() {
2152   if (!CGM.getCodeGenOpts().CoverageCompilationDir.empty())
2153     return CGM.getCodeGenOpts().CoverageCompilationDir;
2154 
2155   SmallString<256> CWD;
2156   llvm::sys::fs::current_path(CWD);
2157   return CWD.str().str();
2158 }
2159 
2160 std::string CoverageMappingModuleGen::normalizeFilename(StringRef Filename) {
2161   llvm::SmallString<256> Path(Filename);
2162   llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
2163 
2164   /// Traverse coverage prefix map in reverse order because prefix replacements
2165   /// are applied in reverse order starting from the last one when multiple
2166   /// prefix replacement options are provided.
2167   for (const auto &[From, To] :
2168        llvm::reverse(CGM.getCodeGenOpts().CoveragePrefixMap)) {
2169     if (llvm::sys::path::replace_path_prefix(Path, From, To))
2170       break;
2171   }
2172   return Path.str().str();
2173 }
2174 
2175 static std::string getInstrProfSection(const CodeGenModule &CGM,
2176                                        llvm::InstrProfSectKind SK) {
2177   return llvm::getInstrProfSectionName(
2178       SK, CGM.getContext().getTargetInfo().getTriple().getObjectFormat());
2179 }
2180 
2181 void CoverageMappingModuleGen::emitFunctionMappingRecord(
2182     const FunctionInfo &Info, uint64_t FilenamesRef) {
2183   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
2184 
2185   // Assign a name to the function record. This is used to merge duplicates.
2186   std::string FuncRecordName = "__covrec_" + llvm::utohexstr(Info.NameHash);
2187 
2188   // A dummy description for a function included-but-not-used in a TU can be
2189   // replaced by full description provided by a different TU. The two kinds of
2190   // descriptions play distinct roles: therefore, assign them different names
2191   // to prevent `linkonce_odr` merging.
2192   if (Info.IsUsed)
2193     FuncRecordName += "u";
2194 
2195   // Create the function record type.
2196   const uint64_t NameHash = Info.NameHash;
2197   const uint64_t FuncHash = Info.FuncHash;
2198   const std::string &CoverageMapping = Info.CoverageMapping;
2199 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
2200   llvm::Type *FunctionRecordTypes[] = {
2201 #include "llvm/ProfileData/InstrProfData.inc"
2202   };
2203   auto *FunctionRecordTy =
2204       llvm::StructType::get(Ctx, ArrayRef(FunctionRecordTypes),
2205                             /*isPacked=*/true);
2206 
2207   // Create the function record constant.
2208 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
2209   llvm::Constant *FunctionRecordVals[] = {
2210       #include "llvm/ProfileData/InstrProfData.inc"
2211   };
2212   auto *FuncRecordConstant =
2213       llvm::ConstantStruct::get(FunctionRecordTy, ArrayRef(FunctionRecordVals));
2214 
2215   // Create the function record global.
2216   auto *FuncRecord = new llvm::GlobalVariable(
2217       CGM.getModule(), FunctionRecordTy, /*isConstant=*/true,
2218       llvm::GlobalValue::LinkOnceODRLinkage, FuncRecordConstant,
2219       FuncRecordName);
2220   FuncRecord->setVisibility(llvm::GlobalValue::HiddenVisibility);
2221   FuncRecord->setSection(getInstrProfSection(CGM, llvm::IPSK_covfun));
2222   FuncRecord->setAlignment(llvm::Align(8));
2223   if (CGM.supportsCOMDAT())
2224     FuncRecord->setComdat(CGM.getModule().getOrInsertComdat(FuncRecordName));
2225 
2226   // Make sure the data doesn't get deleted.
2227   CGM.addUsedGlobal(FuncRecord);
2228 }
2229 
2230 void CoverageMappingModuleGen::addFunctionMappingRecord(
2231     llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
2232     const std::string &CoverageMapping, bool IsUsed) {
2233   const uint64_t NameHash = llvm::IndexedInstrProf::ComputeHash(NameValue);
2234   FunctionRecords.push_back({NameHash, FuncHash, CoverageMapping, IsUsed});
2235 
2236   if (!IsUsed)
2237     FunctionNames.push_back(NamePtr);
2238 
2239   if (CGM.getCodeGenOpts().DumpCoverageMapping) {
2240     // Dump the coverage mapping data for this function by decoding the
2241     // encoded data. This allows us to dump the mapping regions which were
2242     // also processed by the CoverageMappingWriter which performs
2243     // additional minimization operations such as reducing the number of
2244     // expressions.
2245     llvm::SmallVector<std::string, 16> FilenameStrs;
2246     std::vector<StringRef> Filenames;
2247     std::vector<CounterExpression> Expressions;
2248     std::vector<CounterMappingRegion> Regions;
2249     FilenameStrs.resize(FileEntries.size() + 1);
2250     FilenameStrs[0] = normalizeFilename(getCurrentDirname());
2251     for (const auto &Entry : FileEntries) {
2252       auto I = Entry.second;
2253       FilenameStrs[I] = normalizeFilename(Entry.first.getName());
2254     }
2255     ArrayRef<std::string> FilenameRefs = llvm::ArrayRef(FilenameStrs);
2256     RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
2257                                     Expressions, Regions);
2258     if (Reader.read())
2259       return;
2260     dump(llvm::outs(), NameValue, Expressions, Regions);
2261   }
2262 }
2263 
2264 void CoverageMappingModuleGen::emit() {
2265   if (FunctionRecords.empty())
2266     return;
2267   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
2268   auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
2269 
2270   // Create the filenames and merge them with coverage mappings
2271   llvm::SmallVector<std::string, 16> FilenameStrs;
2272   FilenameStrs.resize(FileEntries.size() + 1);
2273   // The first filename is the current working directory.
2274   FilenameStrs[0] = normalizeFilename(getCurrentDirname());
2275   for (const auto &Entry : FileEntries) {
2276     auto I = Entry.second;
2277     FilenameStrs[I] = normalizeFilename(Entry.first.getName());
2278   }
2279 
2280   std::string Filenames;
2281   {
2282     llvm::raw_string_ostream OS(Filenames);
2283     CoverageFilenamesSectionWriter(FilenameStrs).write(OS);
2284   }
2285   auto *FilenamesVal =
2286       llvm::ConstantDataArray::getString(Ctx, Filenames, false);
2287   const int64_t FilenamesRef = llvm::IndexedInstrProf::ComputeHash(Filenames);
2288 
2289   // Emit the function records.
2290   for (const FunctionInfo &Info : FunctionRecords)
2291     emitFunctionMappingRecord(Info, FilenamesRef);
2292 
2293   const unsigned NRecords = 0;
2294   const size_t FilenamesSize = Filenames.size();
2295   const unsigned CoverageMappingSize = 0;
2296   llvm::Type *CovDataHeaderTypes[] = {
2297 #define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
2298 #include "llvm/ProfileData/InstrProfData.inc"
2299   };
2300   auto CovDataHeaderTy =
2301       llvm::StructType::get(Ctx, ArrayRef(CovDataHeaderTypes));
2302   llvm::Constant *CovDataHeaderVals[] = {
2303 #define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
2304 #include "llvm/ProfileData/InstrProfData.inc"
2305   };
2306   auto CovDataHeaderVal =
2307       llvm::ConstantStruct::get(CovDataHeaderTy, ArrayRef(CovDataHeaderVals));
2308 
2309   // Create the coverage data record
2310   llvm::Type *CovDataTypes[] = {CovDataHeaderTy, FilenamesVal->getType()};
2311   auto CovDataTy = llvm::StructType::get(Ctx, ArrayRef(CovDataTypes));
2312   llvm::Constant *TUDataVals[] = {CovDataHeaderVal, FilenamesVal};
2313   auto CovDataVal = llvm::ConstantStruct::get(CovDataTy, ArrayRef(TUDataVals));
2314   auto CovData = new llvm::GlobalVariable(
2315       CGM.getModule(), CovDataTy, true, llvm::GlobalValue::PrivateLinkage,
2316       CovDataVal, llvm::getCoverageMappingVarName());
2317 
2318   CovData->setSection(getInstrProfSection(CGM, llvm::IPSK_covmap));
2319   CovData->setAlignment(llvm::Align(8));
2320 
2321   // Make sure the data doesn't get deleted.
2322   CGM.addUsedGlobal(CovData);
2323   // Create the deferred function records array
2324   if (!FunctionNames.empty()) {
2325     auto NamesArrTy = llvm::ArrayType::get(llvm::PointerType::getUnqual(Ctx),
2326                                            FunctionNames.size());
2327     auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
2328     // This variable will *NOT* be emitted to the object file. It is used
2329     // to pass the list of names referenced to codegen.
2330     new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
2331                              llvm::GlobalValue::InternalLinkage, NamesArrVal,
2332                              llvm::getCoverageUnusedNamesVarName());
2333   }
2334 }
2335 
2336 unsigned CoverageMappingModuleGen::getFileID(FileEntryRef File) {
2337   auto It = FileEntries.find(File);
2338   if (It != FileEntries.end())
2339     return It->second;
2340   unsigned FileID = FileEntries.size() + 1;
2341   FileEntries.insert(std::make_pair(File, FileID));
2342   return FileID;
2343 }
2344 
2345 void CoverageMappingGen::emitCounterMapping(const Decl *D,
2346                                             llvm::raw_ostream &OS) {
2347   assert(CounterMap && MCDCBitmapMap);
2348   CounterCoverageMappingBuilder Walker(CVM, *CounterMap, *MCDCBitmapMap,
2349                                        *CondIDMap, SM, LangOpts);
2350   Walker.VisitDecl(D);
2351   Walker.write(OS);
2352 }
2353 
2354 void CoverageMappingGen::emitEmptyMapping(const Decl *D,
2355                                           llvm::raw_ostream &OS) {
2356   EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
2357   Walker.VisitDecl(D);
2358   Walker.write(OS);
2359 }
2360