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