1 //===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Instrumentation-based code coverage mapping generator
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CoverageMappingGen.h"
15 #include "CodeGenFunction.h"
16 #include "clang/AST/StmtVisitor.h"
17 #include "clang/Lex/Lexer.h"
18 #include "llvm/ProfileData/CoverageMapping.h"
19 #include "llvm/ProfileData/CoverageMappingReader.h"
20 #include "llvm/ProfileData/CoverageMappingWriter.h"
21 #include "llvm/ProfileData/InstrProfReader.h"
22 #include "llvm/Support/FileSystem.h"
23 
24 using namespace clang;
25 using namespace CodeGen;
26 using namespace llvm::coverage;
27 
28 void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range) {
29   SkippedRanges.push_back(Range);
30 }
31 
32 namespace {
33 
34 /// \brief A region of source code that can be mapped to a counter.
35 class SourceMappingRegion {
36 public:
37   enum RegionFlags {
38     /// \brief This region won't be emitted if it wasn't extended.
39     /// This is useful so that we won't emit source ranges for single tokens
40     /// that we don't really care that much about, like:
41     ///   the '(' token in #define MACRO (
42     IgnoreIfNotExtended = 0x0001,
43   };
44 
45 private:
46   FileID File, MacroArgumentFile;
47 
48   Counter Count;
49 
50   /// \brief A statement that initiated the count of Zero.
51   ///
52   /// This initiator statement is useful to prevent merging of unreachable
53   /// regions with different statements that caused the counter to become
54   /// unreachable.
55   const Stmt *UnreachableInitiator;
56 
57   /// \brief A statement that separates certain mapping regions into groups.
58   ///
59   /// The group statement is sometimes useful when we are emitting the source
60   /// regions not in their correct lexical order, e.g. the regions for the
61   /// incrementation expression in the 'for' construct. By marking the regions
62   /// in the incrementation expression with the group statement, we avoid the
63   /// merging of the regions from the incrementation expression and the loop's
64   /// body.
65   const Stmt *Group;
66 
67   /// \brief The region's starting location.
68   SourceLocation LocStart;
69 
70   /// \brief The region's ending location.
71   SourceLocation LocEnd, AlternativeLocEnd;
72   unsigned Flags;
73 
74 public:
75   SourceMappingRegion(FileID File, FileID MacroArgumentFile, Counter Count,
76                       const Stmt *UnreachableInitiator, const Stmt *Group,
77                       SourceLocation LocStart, SourceLocation LocEnd,
78                       unsigned Flags = 0)
79       : File(File), MacroArgumentFile(MacroArgumentFile), Count(Count),
80         UnreachableInitiator(UnreachableInitiator), Group(Group),
81         LocStart(LocStart), LocEnd(LocEnd), AlternativeLocEnd(LocStart),
82         Flags(Flags) {}
83 
84   const FileID &getFile() const { return File; }
85 
86   const Counter &getCounter() const { return Count; }
87 
88   const SourceLocation &getStartLoc() const { return LocStart; }
89 
90   const SourceLocation &getEndLoc(const SourceManager &SM) const {
91     if (SM.getFileID(LocEnd) != File)
92       return AlternativeLocEnd;
93     return LocEnd;
94   }
95 
96   bool hasFlag(RegionFlags Flag) const { return (Flags & Flag) != 0; }
97 
98   void setFlag(RegionFlags Flag) { Flags |= Flag; }
99 
100   void clearFlag(RegionFlags Flag) { Flags &= ~Flag; }
101 
102   /// \brief Return true if two regions can be merged together.
103   bool isMergeable(SourceMappingRegion &R) {
104     // FIXME: We allow merging regions with a gap in between them. Should we?
105     return File == R.File && MacroArgumentFile == R.MacroArgumentFile &&
106            Count == R.Count && UnreachableInitiator == R.UnreachableInitiator &&
107            Group == R.Group;
108   }
109 
110   /// \brief A comparison that sorts such that mergeable regions are adjacent.
111   friend bool operator<(const SourceMappingRegion &LHS,
112                         const SourceMappingRegion &RHS) {
113     return std::tie(LHS.File, LHS.MacroArgumentFile, LHS.Count,
114                     LHS.UnreachableInitiator, LHS.Group) <
115            std::tie(RHS.File, RHS.MacroArgumentFile, RHS.Count,
116                     RHS.UnreachableInitiator, RHS.Group);
117   }
118 };
119 
120 /// \brief Provides the common functionality for the different
121 /// coverage mapping region builders.
122 class CoverageMappingBuilder {
123 public:
124   CoverageMappingModuleGen &CVM;
125   SourceManager &SM;
126   const LangOptions &LangOpts;
127 
128 private:
129   struct FileInfo {
130     /// \brief The file id that will be used by the coverage mapping system.
131     unsigned CovMappingFileID;
132     const FileEntry *Entry;
133 
134     FileInfo(unsigned CovMappingFileID, const FileEntry *Entry)
135         : CovMappingFileID(CovMappingFileID), Entry(Entry) {}
136   };
137 
138   /// \brief This mapping maps clang's FileIDs to file ids used
139   /// by the coverage mapping system and clang's file entries.
140   llvm::SmallDenseMap<FileID, FileInfo, 8> FileIDMapping;
141 
142 public:
143   /// \brief The statement that corresponds to the current source group.
144   const Stmt *CurrentSourceGroup;
145 
146   /// \brief The statement the initiated the current unreachable region.
147   const Stmt *CurrentUnreachableRegionInitiator;
148 
149   /// \brief The coverage mapping regions for this function
150   llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
151   /// \brief The source mapping regions for this function.
152   std::vector<SourceMappingRegion> SourceRegions;
153 
154   CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
155                          const LangOptions &LangOpts)
156       : CVM(CVM), SM(SM), LangOpts(LangOpts),
157         CurrentSourceGroup(nullptr),
158         CurrentUnreachableRegionInitiator(nullptr) {}
159 
160   /// \brief Return the precise end location for the given token.
161   SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
162     return Lexer::getLocForEndOfToken(SM.getSpellingLoc(Loc), 0, SM, LangOpts);
163   }
164 
165   /// \brief Create the mapping that maps from the function's file ids to
166   /// the indices for the translation unit's filenames.
167   void createFileIDMapping(SmallVectorImpl<unsigned> &Mapping) {
168     Mapping.resize(FileIDMapping.size(), 0);
169     for (const auto &I : FileIDMapping)
170       Mapping[I.second.CovMappingFileID] = CVM.getFileID(I.second.Entry);
171   }
172 
173   /// \brief Get the coverage mapping file id that corresponds to the given
174   /// clang file id. If such file id doesn't exist, it gets added to the
175   /// mapping that maps from clang's file ids to coverage mapping file ids.
176   /// Returns None if there was an error getting the coverage mapping file id.
177   /// An example of an when this function fails is when the region tries
178   /// to get a coverage file id for a location in a built-in macro.
179   Optional<unsigned> getCoverageFileID(SourceLocation LocStart, FileID File,
180                                        FileID SpellingFile) {
181     auto Mapping = FileIDMapping.find(File);
182     if (Mapping != FileIDMapping.end())
183       return Mapping->second.CovMappingFileID;
184 
185     auto Entry = SM.getFileEntryForID(SpellingFile);
186     if (!Entry)
187       return None;
188 
189     unsigned Result = FileIDMapping.size();
190     FileIDMapping.insert(std::make_pair(File, FileInfo(Result, Entry)));
191     createFileExpansionRegion(LocStart, File);
192     return Result;
193   }
194 
195   /// \brief Get the coverage mapping file id that corresponds to the given
196   /// clang file id.
197   /// Returns None if there was an error getting the coverage mapping file id.
198   Optional<unsigned> getExistingCoverageFileID(FileID File) {
199     // Make sure that the file is valid.
200     if (File.isInvalid())
201       return None;
202     auto Mapping = FileIDMapping.find(File);
203     if (Mapping == FileIDMapping.end())
204       return None;
205     return Mapping->second.CovMappingFileID;
206   }
207 
208   /// \brief Return true if the given clang's file id has a corresponding
209   /// coverage file id.
210   bool hasExistingCoverageFileID(FileID File) const {
211     return FileIDMapping.count(File);
212   }
213 
214   /// \brief Gather all the regions that were skipped by the preprocessor
215   /// using the constructs like #if.
216   void gatherSkippedRegions() {
217     /// An array of the minimum lineStarts and the maximum lineEnds
218     /// for mapping regions from the appropriate source files.
219     llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
220     FileLineRanges.resize(
221         FileIDMapping.size(),
222         std::make_pair(std::numeric_limits<unsigned>::max(), 0));
223     for (const auto &R : MappingRegions) {
224       FileLineRanges[R.FileID].first =
225           std::min(FileLineRanges[R.FileID].first, R.LineStart);
226       FileLineRanges[R.FileID].second =
227           std::max(FileLineRanges[R.FileID].second, R.LineEnd);
228     }
229 
230     auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
231     for (const auto &I : SkippedRanges) {
232       auto LocStart = I.getBegin();
233       auto LocEnd = I.getEnd();
234       auto FileStart = SM.getFileID(LocStart);
235       if (!hasExistingCoverageFileID(FileStart))
236         continue;
237       auto ActualFileStart = SM.getDecomposedSpellingLoc(LocStart).first;
238       if (ActualFileStart != SM.getDecomposedSpellingLoc(LocEnd).first)
239         // Ignore regions that span across multiple files.
240         continue;
241 
242       auto CovFileID = getCoverageFileID(LocStart, FileStart, ActualFileStart);
243       if (!CovFileID)
244         continue;
245       unsigned LineStart = SM.getSpellingLineNumber(LocStart);
246       unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
247       unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
248       unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
249       auto Region = CounterMappingRegion::makeSkipped(
250           *CovFileID, LineStart, ColumnStart, LineEnd, ColumnEnd);
251       // Make sure that we only collect the regions that are inside
252       // the souce code of this function.
253       if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
254           Region.LineEnd <= FileLineRanges[*CovFileID].second)
255         MappingRegions.push_back(Region);
256     }
257   }
258 
259   /// \brief Create a mapping region that correponds to an expansion of
260   /// a macro or an embedded include.
261   void createFileExpansionRegion(SourceLocation Loc, FileID ExpandedFile) {
262     SourceLocation LocStart;
263     if (Loc.isMacroID())
264       LocStart = SM.getImmediateExpansionRange(Loc).first;
265     else {
266       LocStart = SM.getIncludeLoc(ExpandedFile);
267       if (LocStart.isInvalid())
268         return; // This file has no expansion region.
269     }
270 
271     auto File = SM.getFileID(LocStart);
272     auto SpellingFile = SM.getDecomposedSpellingLoc(LocStart).first;
273     auto CovFileID = getCoverageFileID(LocStart, File, SpellingFile);
274     auto ExpandedFileID = getExistingCoverageFileID(ExpandedFile);
275     if (!CovFileID || !ExpandedFileID)
276       return;
277     unsigned LineStart = SM.getSpellingLineNumber(LocStart);
278     unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
279     unsigned LineEnd = LineStart;
280     // Compute the end column manually as Lexer::getLocForEndOfToken doesn't
281     // give the correct result in all cases.
282     unsigned ColumnEnd =
283         ColumnStart +
284         Lexer::MeasureTokenLength(SM.getSpellingLoc(LocStart), SM, LangOpts);
285 
286     MappingRegions.push_back(CounterMappingRegion::makeExpansion(
287         *CovFileID, *ExpandedFileID, LineStart, ColumnStart, LineEnd,
288         ColumnEnd));
289   }
290 
291   /// \brief Enter a source region group that is identified by the given
292   /// statement.
293   /// It's not possible to enter a group when there is already
294   /// another group present.
295   void beginSourceRegionGroup(const Stmt *Group) {
296     assert(!CurrentSourceGroup);
297     CurrentSourceGroup = Group;
298   }
299 
300   /// \brief Exit the current source region group.
301   void endSourceRegionGroup() { CurrentSourceGroup = nullptr; }
302 
303   /// \brief Associate a counter with a given source code range.
304   void mapSourceCodeRange(SourceLocation LocStart, SourceLocation LocEnd,
305                           Counter Count, const Stmt *UnreachableInitiator,
306                           const Stmt *SourceGroup, unsigned Flags = 0,
307                           FileID MacroArgumentFile = FileID()) {
308     if (SM.isMacroArgExpansion(LocStart)) {
309       // Map the code range with the macro argument's value.
310       mapSourceCodeRange(SM.getImmediateSpellingLoc(LocStart),
311                          SM.getImmediateSpellingLoc(LocEnd), Count,
312                          UnreachableInitiator, SourceGroup, Flags,
313                          SM.getFileID(LocStart));
314       // Map the code range where the macro argument is referenced.
315       SourceLocation RefLocStart(SM.getImmediateExpansionRange(LocStart).first);
316       SourceLocation RefLocEnd(RefLocStart);
317       if (SM.isMacroArgExpansion(RefLocStart))
318         mapSourceCodeRange(RefLocStart, RefLocEnd, Count, UnreachableInitiator,
319                            SourceGroup, 0, SM.getFileID(RefLocStart));
320       else
321         mapSourceCodeRange(RefLocStart, RefLocEnd, Count, UnreachableInitiator,
322                            SourceGroup);
323       return;
324     }
325     auto File = SM.getFileID(LocStart);
326     // Make sure that the file id is valid.
327     if (File.isInvalid())
328       return;
329     SourceRegions.emplace_back(File, MacroArgumentFile, Count,
330                                UnreachableInitiator, SourceGroup, LocStart,
331                                LocEnd, Flags);
332   }
333 
334   void mapSourceCodeRange(SourceLocation LocStart, SourceLocation LocEnd,
335                           Counter Count, unsigned Flags = 0) {
336     mapSourceCodeRange(LocStart, LocEnd, Count,
337                        CurrentUnreachableRegionInitiator, CurrentSourceGroup,
338                        Flags);
339   }
340 
341   /// \brief Generate the coverage counter mapping regions from collected
342   /// source regions.
343   void emitSourceRegions() {
344     std::sort(SourceRegions.begin(), SourceRegions.end());
345 
346     for (auto I = SourceRegions.begin(), E = SourceRegions.end(); I != E; ++I) {
347       // Keep the original start location of this region.
348       SourceLocation LocStart = I->getStartLoc();
349       SourceLocation LocEnd = I->getEndLoc(SM);
350 
351       bool Ignore = I->hasFlag(SourceMappingRegion::IgnoreIfNotExtended);
352       // We need to handle mergeable regions together.
353       for (auto Next = I + 1; Next != E && Next->isMergeable(*I); ++Next) {
354         ++I;
355         LocStart = std::min(LocStart, I->getStartLoc());
356         LocEnd = std::max(LocEnd, I->getEndLoc(SM));
357         // FIXME: Should we && together the Ignore flag of multiple regions?
358         Ignore = false;
359       }
360       if (Ignore)
361         continue;
362 
363       // Find the spilling locations for the mapping region.
364       LocEnd = getPreciseTokenLocEnd(LocEnd);
365       unsigned LineStart = SM.getSpellingLineNumber(LocStart);
366       unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
367       unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
368       unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
369 
370       auto SpellingFile = SM.getDecomposedSpellingLoc(LocStart).first;
371       auto CovFileID = getCoverageFileID(LocStart, I->getFile(), SpellingFile);
372       if (!CovFileID)
373         continue;
374 
375       assert(LineStart <= LineEnd);
376       MappingRegions.push_back(CounterMappingRegion::makeRegion(
377           I->getCounter(), *CovFileID, LineStart, ColumnStart, LineEnd,
378           ColumnEnd));
379     }
380   }
381 };
382 
383 /// \brief Creates unreachable coverage regions for the functions that
384 /// are not emitted.
385 struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
386   EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
387                               const LangOptions &LangOpts)
388       : CoverageMappingBuilder(CVM, SM, LangOpts) {}
389 
390   void VisitDecl(const Decl *D) {
391     if (!D->hasBody())
392       return;
393     auto Body = D->getBody();
394     mapSourceCodeRange(Body->getLocStart(), Body->getLocEnd(), Counter());
395   }
396 
397   /// \brief Write the mapping data to the output stream
398   void write(llvm::raw_ostream &OS) {
399     emitSourceRegions();
400     SmallVector<unsigned, 16> FileIDMapping;
401     createFileIDMapping(FileIDMapping);
402 
403     CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions);
404     Writer.write(OS);
405   }
406 };
407 
408 /// \brief A StmtVisitor that creates coverage mapping regions which map
409 /// from the source code locations to the PGO counters.
410 struct CounterCoverageMappingBuilder
411     : public CoverageMappingBuilder,
412       public ConstStmtVisitor<CounterCoverageMappingBuilder> {
413   /// \brief The map of statements to count values.
414   llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
415 
416   Counter CurrentRegionCount;
417 
418   CounterExpressionBuilder Builder;
419 
420   /// \brief Return a counter that represents the
421   /// expression that subracts rhs from lhs.
422   Counter subtractCounters(Counter LHS, Counter RHS) {
423     return Builder.subtract(LHS, RHS);
424   }
425 
426   /// \brief Return a counter that represents the
427   /// the exression that adds lhs and rhs.
428   Counter addCounters(Counter LHS, Counter RHS) {
429     return Builder.add(LHS, RHS);
430   }
431 
432   /// \brief Return the region counter for the given statement.
433   /// This should only be called on statements that have a dedicated counter.
434   unsigned getRegionCounter(const Stmt *S) { return CounterMap[S]; }
435 
436   /// \brief Return the region count for the counter at the given index.
437   Counter getRegionCount(unsigned CounterId) {
438     return Counter::getCounter(CounterId);
439   }
440 
441   /// \brief Return the counter value of the current region.
442   Counter getCurrentRegionCount() { return CurrentRegionCount; }
443 
444   /// \brief Set the counter value for the current region.
445   /// This is used to keep track of changes to the most recent counter
446   /// from control flow and non-local exits.
447   void setCurrentRegionCount(Counter Count) {
448     CurrentRegionCount = Count;
449     CurrentUnreachableRegionInitiator = nullptr;
450   }
451 
452   /// \brief Indicate that the current region is never reached,
453   /// and thus should have a counter value of zero.
454   /// This is important so that subsequent regions can correctly track
455   /// their parent counts.
456   void setCurrentRegionUnreachable(const Stmt *Initiator) {
457     CurrentRegionCount = Counter::getZero();
458     CurrentUnreachableRegionInitiator = Initiator;
459   }
460 
461   /// \brief A counter for a particular region.
462   /// This is the primary interface through
463   /// which the coverage mapping builder manages counters and their values.
464   class RegionMapper {
465     CounterCoverageMappingBuilder &Mapping;
466     Counter Count;
467     Counter ParentCount;
468     Counter RegionCount;
469     Counter Adjust;
470 
471   public:
472     RegionMapper(CounterCoverageMappingBuilder *Mapper, const Stmt *S)
473         : Mapping(*Mapper),
474           Count(Mapper->getRegionCount(Mapper->getRegionCounter(S))),
475           ParentCount(Mapper->getCurrentRegionCount()) {}
476 
477     /// Get the value of the counter. In most cases this is the number of times
478     /// the region of the counter was entered, but for switch labels it's the
479     /// number of direct jumps to that label.
480     Counter getCount() const { return Count; }
481 
482     /// Get the value of the counter with adjustments applied. Adjustments occur
483     /// when control enters or leaves the region abnormally; i.e., if there is a
484     /// jump to a label within the region, or if the function can return from
485     /// within the region. The adjusted count, then, is the value of the counter
486     /// at the end of the region.
487     Counter getAdjustedCount() const {
488       return Mapping.addCounters(Count, Adjust);
489     }
490 
491     /// Get the value of the counter in this region's parent, i.e., the region
492     /// that was active when this region began. This is useful for deriving
493     /// counts in implicitly counted regions, like the false case of a condition
494     /// or the normal exits of a loop.
495     Counter getParentCount() const { return ParentCount; }
496 
497     /// Activate the counter by emitting an increment and starting to track
498     /// adjustments. If AddIncomingFallThrough is true, the current region count
499     /// will be added to the counter for the purposes of tracking the region.
500     void beginRegion(bool AddIncomingFallThrough = false) {
501       RegionCount = Count;
502       if (AddIncomingFallThrough)
503         RegionCount =
504             Mapping.addCounters(RegionCount, Mapping.getCurrentRegionCount());
505       Mapping.setCurrentRegionCount(RegionCount);
506     }
507 
508     /// For counters on boolean branches, begins tracking adjustments for the
509     /// uncounted path.
510     void beginElseRegion() {
511       RegionCount = Mapping.subtractCounters(ParentCount, Count);
512       Mapping.setCurrentRegionCount(RegionCount);
513     }
514 
515     /// Reset the current region count.
516     void setCurrentRegionCount(Counter CurrentCount) {
517       RegionCount = CurrentCount;
518       Mapping.setCurrentRegionCount(RegionCount);
519     }
520 
521     /// Adjust for non-local control flow after emitting a subexpression or
522     /// substatement. This must be called to account for constructs such as
523     /// gotos,
524     /// labels, and returns, so that we can ensure that our region's count is
525     /// correct in the code that follows.
526     void adjustForControlFlow() {
527       Adjust = Mapping.addCounters(
528           Adjust, Mapping.subtractCounters(Mapping.getCurrentRegionCount(),
529                                            RegionCount));
530       // Reset the region count in case this is called again later.
531       RegionCount = Mapping.getCurrentRegionCount();
532     }
533 
534     /// Commit all adjustments to the current region. If the region is a loop,
535     /// the LoopAdjust value should be the count of all the breaks and continues
536     /// from the loop, to compensate for those counts being deducted from the
537     /// adjustments for the body of the loop.
538     void applyAdjustmentsToRegion() {
539       Mapping.setCurrentRegionCount(Mapping.addCounters(ParentCount, Adjust));
540     }
541     void applyAdjustmentsToRegion(Counter LoopAdjust) {
542       Mapping.setCurrentRegionCount(Mapping.addCounters(
543           Mapping.addCounters(ParentCount, Adjust), LoopAdjust));
544     }
545   };
546 
547   /// \brief Keep counts of breaks and continues inside loops.
548   struct BreakContinue {
549     Counter BreakCount;
550     Counter ContinueCount;
551   };
552   SmallVector<BreakContinue, 8> BreakContinueStack;
553 
554   CounterCoverageMappingBuilder(
555       CoverageMappingModuleGen &CVM,
556       llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
557       const LangOptions &LangOpts)
558       : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap) {}
559 
560   /// \brief Write the mapping data to the output stream
561   void write(llvm::raw_ostream &OS) {
562     emitSourceRegions();
563     llvm::SmallVector<unsigned, 8> VirtualFileMapping;
564     createFileIDMapping(VirtualFileMapping);
565     gatherSkippedRegions();
566 
567     CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
568                                  MappingRegions);
569     Writer.write(OS);
570   }
571 
572   /// \brief Associate the source code range with the current region count.
573   void mapSourceCodeRange(SourceLocation LocStart, SourceLocation LocEnd,
574                           unsigned Flags = 0) {
575     CoverageMappingBuilder::mapSourceCodeRange(LocStart, LocEnd,
576                                                CurrentRegionCount, Flags);
577   }
578 
579   void mapSourceCodeRange(SourceLocation LocStart) {
580     CoverageMappingBuilder::mapSourceCodeRange(LocStart, LocStart,
581                                                CurrentRegionCount);
582   }
583 
584   /// \brief Associate the source range of a token with the current region
585   /// count.
586   /// Ignore the source range for this token if it produces a distinct
587   /// mapping region with no other source ranges.
588   void mapToken(SourceLocation LocStart) {
589     CoverageMappingBuilder::mapSourceCodeRange(
590         LocStart, LocStart, CurrentRegionCount,
591         SourceMappingRegion::IgnoreIfNotExtended);
592   }
593 
594   void VisitStmt(const Stmt *S) {
595     mapSourceCodeRange(S->getLocStart());
596     for (Stmt::const_child_range I = S->children(); I; ++I) {
597       if (*I)
598         this->Visit(*I);
599     }
600   }
601 
602   void VisitDecl(const Decl *D) {
603     if (!D->hasBody())
604       return;
605     // Counter tracks entry to the function body.
606     auto Body = D->getBody();
607     RegionMapper Cnt(this, Body);
608     Cnt.beginRegion();
609     Visit(Body);
610   }
611 
612   void VisitDeclStmt(const DeclStmt *S) {
613     mapSourceCodeRange(S->getLocStart());
614     for (Stmt::const_child_range I = static_cast<const Stmt *>(S)->children();
615          I; ++I) {
616       if (*I)
617         this->Visit(*I);
618     }
619   }
620 
621   void VisitCompoundStmt(const CompoundStmt *S) {
622     mapSourceCodeRange(S->getLBracLoc());
623     mapSourceCodeRange(S->getRBracLoc());
624     for (Stmt::const_child_range I = S->children(); I; ++I) {
625       if (*I)
626         this->Visit(*I);
627     }
628   }
629 
630   void VisitReturnStmt(const ReturnStmt *S) {
631     mapSourceCodeRange(S->getLocStart());
632     if (S->getRetValue())
633       Visit(S->getRetValue());
634     setCurrentRegionUnreachable(S);
635   }
636 
637   void VisitGotoStmt(const GotoStmt *S) {
638     mapSourceCodeRange(S->getLocStart());
639     mapToken(S->getLabelLoc());
640     setCurrentRegionUnreachable(S);
641   }
642 
643   void VisitLabelStmt(const LabelStmt *S) {
644     // Counter tracks the block following the label.
645     RegionMapper Cnt(this, S);
646     Cnt.beginRegion();
647     mapSourceCodeRange(S->getLocStart());
648     // Can't map the ':' token as its location isn't known.
649     Visit(S->getSubStmt());
650   }
651 
652   void VisitBreakStmt(const BreakStmt *S) {
653     mapSourceCodeRange(S->getLocStart());
654     assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
655     BreakContinueStack.back().BreakCount = addCounters(
656         BreakContinueStack.back().BreakCount, getCurrentRegionCount());
657     setCurrentRegionUnreachable(S);
658   }
659 
660   void VisitContinueStmt(const ContinueStmt *S) {
661     mapSourceCodeRange(S->getLocStart());
662     assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
663     BreakContinueStack.back().ContinueCount = addCounters(
664         BreakContinueStack.back().ContinueCount, getCurrentRegionCount());
665     setCurrentRegionUnreachable(S);
666   }
667 
668   void VisitWhileStmt(const WhileStmt *S) {
669     mapSourceCodeRange(S->getLocStart());
670     // Counter tracks the body of the loop.
671     RegionMapper Cnt(this, S);
672     BreakContinueStack.push_back(BreakContinue());
673     // Visit the body region first so the break/continue adjustments can be
674     // included when visiting the condition.
675     Cnt.beginRegion();
676     Visit(S->getBody());
677     Cnt.adjustForControlFlow();
678 
679     // ...then go back and propagate counts through the condition. The count
680     // at the start of the condition is the sum of the incoming edges,
681     // the backedge from the end of the loop body, and the edges from
682     // continue statements.
683     BreakContinue BC = BreakContinueStack.pop_back_val();
684     Cnt.setCurrentRegionCount(
685         addCounters(Cnt.getParentCount(),
686                     addCounters(Cnt.getAdjustedCount(), BC.ContinueCount)));
687     beginSourceRegionGroup(S->getCond());
688     Visit(S->getCond());
689     endSourceRegionGroup();
690     Cnt.adjustForControlFlow();
691     Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount));
692   }
693 
694   void VisitDoStmt(const DoStmt *S) {
695     mapSourceCodeRange(S->getLocStart());
696     // Counter tracks the body of the loop.
697     RegionMapper Cnt(this, S);
698     BreakContinueStack.push_back(BreakContinue());
699     Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
700     Visit(S->getBody());
701     Cnt.adjustForControlFlow();
702 
703     BreakContinue BC = BreakContinueStack.pop_back_val();
704     // The count at the start of the condition is equal to the count at the
705     // end of the body. The adjusted count does not include either the
706     // fall-through count coming into the loop or the continue count, so add
707     // both of those separately. This is coincidentally the same equation as
708     // with while loops but for different reasons.
709     Cnt.setCurrentRegionCount(
710         addCounters(Cnt.getParentCount(),
711                     addCounters(Cnt.getAdjustedCount(), BC.ContinueCount)));
712     Visit(S->getCond());
713     Cnt.adjustForControlFlow();
714     Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount));
715   }
716 
717   void VisitForStmt(const ForStmt *S) {
718     mapSourceCodeRange(S->getLocStart());
719     if (S->getInit())
720       Visit(S->getInit());
721 
722     // Counter tracks the body of the loop.
723     RegionMapper Cnt(this, S);
724     BreakContinueStack.push_back(BreakContinue());
725     // Visit the body region first. (This is basically the same as a while
726     // loop; see further comments in VisitWhileStmt.)
727     Cnt.beginRegion();
728     Visit(S->getBody());
729     Cnt.adjustForControlFlow();
730 
731     // The increment is essentially part of the body but it needs to include
732     // the count for all the continue statements.
733     if (S->getInc()) {
734       Cnt.setCurrentRegionCount(addCounters(
735           getCurrentRegionCount(), BreakContinueStack.back().ContinueCount));
736       beginSourceRegionGroup(S->getInc());
737       Visit(S->getInc());
738       endSourceRegionGroup();
739       Cnt.adjustForControlFlow();
740     }
741 
742     BreakContinue BC = BreakContinueStack.pop_back_val();
743 
744     // ...then go back and propagate counts through the condition.
745     if (S->getCond()) {
746       Cnt.setCurrentRegionCount(
747           addCounters(addCounters(Cnt.getParentCount(), Cnt.getAdjustedCount()),
748                       BC.ContinueCount));
749       beginSourceRegionGroup(S->getCond());
750       Visit(S->getCond());
751       endSourceRegionGroup();
752       Cnt.adjustForControlFlow();
753     }
754     Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount));
755   }
756 
757   void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
758     mapSourceCodeRange(S->getLocStart());
759     Visit(S->getRangeStmt());
760     Visit(S->getBeginEndStmt());
761     // Counter tracks the body of the loop.
762     RegionMapper Cnt(this, S);
763     BreakContinueStack.push_back(BreakContinue());
764     // Visit the body region first. (This is basically the same as a while
765     // loop; see further comments in VisitWhileStmt.)
766     Cnt.beginRegion();
767     Visit(S->getBody());
768     Cnt.adjustForControlFlow();
769     BreakContinue BC = BreakContinueStack.pop_back_val();
770     Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount));
771   }
772 
773   void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
774     mapSourceCodeRange(S->getLocStart());
775     Visit(S->getElement());
776     // Counter tracks the body of the loop.
777     RegionMapper Cnt(this, S);
778     BreakContinueStack.push_back(BreakContinue());
779     Cnt.beginRegion();
780     Visit(S->getBody());
781     BreakContinue BC = BreakContinueStack.pop_back_val();
782     Cnt.adjustForControlFlow();
783     Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount));
784   }
785 
786   void VisitSwitchStmt(const SwitchStmt *S) {
787     mapSourceCodeRange(S->getLocStart());
788     Visit(S->getCond());
789     BreakContinueStack.push_back(BreakContinue());
790     // Map the '}' for the body to have the same count as the regions after
791     // the switch.
792     SourceLocation RBracLoc;
793     if (const auto *CS = dyn_cast<CompoundStmt>(S->getBody())) {
794       mapSourceCodeRange(CS->getLBracLoc());
795       setCurrentRegionUnreachable(S);
796       for (Stmt::const_child_range I = CS->children(); I; ++I) {
797         if (*I)
798           this->Visit(*I);
799       }
800       RBracLoc = CS->getRBracLoc();
801     } else {
802       setCurrentRegionUnreachable(S);
803       Visit(S->getBody());
804     }
805     // If the switch is inside a loop, add the continue counts.
806     BreakContinue BC = BreakContinueStack.pop_back_val();
807     if (!BreakContinueStack.empty())
808       BreakContinueStack.back().ContinueCount = addCounters(
809           BreakContinueStack.back().ContinueCount, BC.ContinueCount);
810     // Counter tracks the exit block of the switch.
811     RegionMapper ExitCnt(this, S);
812     ExitCnt.beginRegion();
813     if (RBracLoc.isValid())
814       mapSourceCodeRange(RBracLoc);
815   }
816 
817   void VisitCaseStmt(const CaseStmt *S) {
818     // Counter for this particular case. This counts only jumps from the
819     // switch header and does not include fallthrough from the case before
820     // this one.
821     RegionMapper Cnt(this, S);
822     Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
823     mapSourceCodeRange(S->getLocStart());
824     mapToken(S->getColonLoc());
825     Visit(S->getSubStmt());
826   }
827 
828   void VisitDefaultStmt(const DefaultStmt *S) {
829     // Counter for this default case. This does not include fallthrough from
830     // the previous case.
831     RegionMapper Cnt(this, S);
832     Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
833     mapSourceCodeRange(S->getLocStart());
834     mapToken(S->getColonLoc());
835     Visit(S->getSubStmt());
836   }
837 
838   void VisitIfStmt(const IfStmt *S) {
839     mapSourceCodeRange(S->getLocStart());
840     Visit(S->getCond());
841     mapToken(S->getElseLoc());
842 
843     // Counter tracks the "then" part of an if statement. The count for
844     // the "else" part, if it exists, will be calculated from this counter.
845     RegionMapper Cnt(this, S);
846     Cnt.beginRegion();
847     Visit(S->getThen());
848     Cnt.adjustForControlFlow();
849 
850     if (S->getElse()) {
851       Cnt.beginElseRegion();
852       Visit(S->getElse());
853       Cnt.adjustForControlFlow();
854     }
855     Cnt.applyAdjustmentsToRegion();
856   }
857 
858   void VisitCXXTryStmt(const CXXTryStmt *S) {
859     mapSourceCodeRange(S->getLocStart());
860     Visit(S->getTryBlock());
861     for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
862       Visit(S->getHandler(I));
863     // Counter tracks the continuation block of the try statement.
864     RegionMapper Cnt(this, S);
865     Cnt.beginRegion();
866   }
867 
868   void VisitCXXCatchStmt(const CXXCatchStmt *S) {
869     mapSourceCodeRange(S->getLocStart());
870     // Counter tracks the catch statement's handler block.
871     RegionMapper Cnt(this, S);
872     Cnt.beginRegion();
873     Visit(S->getHandlerBlock());
874   }
875 
876   void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
877     Visit(E->getCond());
878     mapToken(E->getQuestionLoc());
879     mapToken(E->getColonLoc());
880 
881     // Counter tracks the "true" part of a conditional operator. The
882     // count in the "false" part will be calculated from this counter.
883     RegionMapper Cnt(this, E);
884     Cnt.beginRegion();
885     Visit(E->getTrueExpr());
886     Cnt.adjustForControlFlow();
887 
888     Cnt.beginElseRegion();
889     Visit(E->getFalseExpr());
890     Cnt.adjustForControlFlow();
891 
892     Cnt.applyAdjustmentsToRegion();
893   }
894 
895   void VisitBinLAnd(const BinaryOperator *E) {
896     Visit(E->getLHS());
897     mapToken(E->getOperatorLoc());
898     // Counter tracks the right hand side of a logical and operator.
899     RegionMapper Cnt(this, E);
900     Cnt.beginRegion();
901     Visit(E->getRHS());
902     Cnt.adjustForControlFlow();
903     Cnt.applyAdjustmentsToRegion();
904   }
905 
906   void VisitBinLOr(const BinaryOperator *E) {
907     Visit(E->getLHS());
908     mapToken(E->getOperatorLoc());
909     // Counter tracks the right hand side of a logical or operator.
910     RegionMapper Cnt(this, E);
911     Cnt.beginRegion();
912     Visit(E->getRHS());
913     Cnt.adjustForControlFlow();
914     Cnt.applyAdjustmentsToRegion();
915   }
916 
917   void VisitParenExpr(const ParenExpr *E) {
918     mapToken(E->getLParen());
919     Visit(E->getSubExpr());
920     mapToken(E->getRParen());
921   }
922 
923   void VisitBinaryOperator(const BinaryOperator *E) {
924     Visit(E->getLHS());
925     mapToken(E->getOperatorLoc());
926     Visit(E->getRHS());
927   }
928 
929   void VisitUnaryOperator(const UnaryOperator *E) {
930     bool Postfix = E->isPostfix();
931     if (!Postfix)
932       mapToken(E->getOperatorLoc());
933     Visit(E->getSubExpr());
934     if (Postfix)
935       mapToken(E->getOperatorLoc());
936   }
937 
938   void VisitMemberExpr(const MemberExpr *E) {
939     Visit(E->getBase());
940     mapToken(E->getMemberLoc());
941   }
942 
943   void VisitCallExpr(const CallExpr *E) {
944     Visit(E->getCallee());
945     for (const auto &Arg : E->arguments())
946       Visit(Arg);
947     mapToken(E->getRParenLoc());
948   }
949 
950   void VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
951     Visit(E->getLHS());
952     Visit(E->getRHS());
953     mapToken(E->getRBracketLoc());
954   }
955 
956   void VisitCStyleCastExpr(const CStyleCastExpr *E) {
957     mapToken(E->getLParenLoc());
958     mapToken(E->getRParenLoc());
959     Visit(E->getSubExpr());
960   }
961 
962   // Map literals as tokens so that the macros like #define PI 3.14
963   // won't generate coverage mapping regions.
964 
965   void VisitIntegerLiteral(const IntegerLiteral *E) {
966     mapToken(E->getLocStart());
967   }
968 
969   void VisitFloatingLiteral(const FloatingLiteral *E) {
970     mapToken(E->getLocStart());
971   }
972 
973   void VisitCharacterLiteral(const CharacterLiteral *E) {
974     mapToken(E->getLocStart());
975   }
976 
977   void VisitStringLiteral(const StringLiteral *E) {
978     mapToken(E->getLocStart());
979   }
980 
981   void VisitImaginaryLiteral(const ImaginaryLiteral *E) {
982     mapToken(E->getLocStart());
983   }
984 
985   void VisitObjCMessageExpr(const ObjCMessageExpr *E) {
986     mapToken(E->getLeftLoc());
987     for (Stmt::const_child_range I = static_cast<const Stmt *>(E)->children();
988          I; ++I) {
989       if (*I)
990         this->Visit(*I);
991     }
992     mapToken(E->getRightLoc());
993   }
994 };
995 }
996 
997 static bool isMachO(const CodeGenModule &CGM) {
998   return CGM.getTarget().getTriple().isOSBinFormatMachO();
999 }
1000 
1001 static StringRef getCoverageSection(const CodeGenModule &CGM) {
1002   return isMachO(CGM) ? "__DATA,__llvm_covmap" : "__llvm_covmap";
1003 }
1004 
1005 static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
1006                  ArrayRef<CounterExpression> Expressions,
1007                  ArrayRef<CounterMappingRegion> Regions) {
1008   OS << FunctionName << ":\n";
1009   CounterMappingContext Ctx(Expressions);
1010   for (const auto &R : Regions) {
1011     OS.indent(2);
1012     switch (R.Kind) {
1013     case CounterMappingRegion::CodeRegion:
1014       break;
1015     case CounterMappingRegion::ExpansionRegion:
1016       OS << "Expansion,";
1017       break;
1018     case CounterMappingRegion::SkippedRegion:
1019       OS << "Skipped,";
1020       break;
1021     }
1022 
1023     OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
1024        << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
1025     Ctx.dump(R.Count, OS);
1026     if (R.Kind == CounterMappingRegion::ExpansionRegion)
1027       OS << " (Expanded file = " << R.ExpandedFileID << ")";
1028     OS << "\n";
1029   }
1030 }
1031 
1032 void CoverageMappingModuleGen::addFunctionMappingRecord(
1033     llvm::GlobalVariable *FunctionName, StringRef FunctionNameValue,
1034     uint64_t FunctionHash, const std::string &CoverageMapping) {
1035   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1036   auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
1037   auto *Int64Ty = llvm::Type::getInt64Ty(Ctx);
1038   auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
1039   if (!FunctionRecordTy) {
1040     llvm::Type *FunctionRecordTypes[] = {Int8PtrTy, Int32Ty, Int32Ty, Int64Ty};
1041     FunctionRecordTy =
1042         llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes));
1043   }
1044 
1045   llvm::Constant *FunctionRecordVals[] = {
1046       llvm::ConstantExpr::getBitCast(FunctionName, Int8PtrTy),
1047       llvm::ConstantInt::get(Int32Ty, FunctionNameValue.size()),
1048       llvm::ConstantInt::get(Int32Ty, CoverageMapping.size()),
1049       llvm::ConstantInt::get(Int64Ty, FunctionHash)};
1050   FunctionRecords.push_back(llvm::ConstantStruct::get(
1051       FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
1052   CoverageMappings += CoverageMapping;
1053 
1054   if (CGM.getCodeGenOpts().DumpCoverageMapping) {
1055     // Dump the coverage mapping data for this function by decoding the
1056     // encoded data. This allows us to dump the mapping regions which were
1057     // also processed by the CoverageMappingWriter which performs
1058     // additional minimization operations such as reducing the number of
1059     // expressions.
1060     std::vector<StringRef> Filenames;
1061     std::vector<CounterExpression> Expressions;
1062     std::vector<CounterMappingRegion> Regions;
1063     llvm::SmallVector<StringRef, 16> FilenameRefs;
1064     FilenameRefs.resize(FileEntries.size());
1065     for (const auto &Entry : FileEntries)
1066       FilenameRefs[Entry.second] = Entry.first->getName();
1067     RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
1068                                     Expressions, Regions);
1069     if (Reader.read())
1070       return;
1071     dump(llvm::outs(), FunctionNameValue, Expressions, Regions);
1072   }
1073 }
1074 
1075 void CoverageMappingModuleGen::emit() {
1076   if (FunctionRecords.empty())
1077     return;
1078   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1079   auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
1080 
1081   // Create the filenames and merge them with coverage mappings
1082   llvm::SmallVector<std::string, 16> FilenameStrs;
1083   llvm::SmallVector<StringRef, 16> FilenameRefs;
1084   FilenameStrs.resize(FileEntries.size());
1085   FilenameRefs.resize(FileEntries.size());
1086   for (const auto &Entry : FileEntries) {
1087     llvm::SmallString<256> Path(Entry.first->getName());
1088     llvm::sys::fs::make_absolute(Path);
1089 
1090     auto I = Entry.second;
1091     FilenameStrs[I] = std::move(std::string(Path.begin(), Path.end()));
1092     FilenameRefs[I] = FilenameStrs[I];
1093   }
1094 
1095   std::string FilenamesAndCoverageMappings;
1096   llvm::raw_string_ostream OS(FilenamesAndCoverageMappings);
1097   CoverageFilenamesSectionWriter(FilenameRefs).write(OS);
1098   OS << CoverageMappings;
1099   size_t CoverageMappingSize = CoverageMappings.size();
1100   size_t FilenamesSize = OS.str().size() - CoverageMappingSize;
1101   // Append extra zeroes if necessary to ensure that the size of the filenames
1102   // and coverage mappings is a multiple of 8.
1103   if (size_t Rem = OS.str().size() % 8) {
1104     CoverageMappingSize += 8 - Rem;
1105     for (size_t I = 0, S = 8 - Rem; I < S; ++I)
1106       OS << '\0';
1107   }
1108   auto *FilenamesAndMappingsVal =
1109       llvm::ConstantDataArray::getString(Ctx, OS.str(), false);
1110 
1111   // Create the deferred function records array
1112   auto RecordsTy =
1113       llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
1114   auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
1115 
1116   // Create the coverage data record
1117   llvm::Type *CovDataTypes[] = {Int32Ty,   Int32Ty,
1118                                 Int32Ty,   Int32Ty,
1119                                 RecordsTy, FilenamesAndMappingsVal->getType()};
1120   auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
1121   llvm::Constant *TUDataVals[] = {
1122       llvm::ConstantInt::get(Int32Ty, FunctionRecords.size()),
1123       llvm::ConstantInt::get(Int32Ty, FilenamesSize),
1124       llvm::ConstantInt::get(Int32Ty, CoverageMappingSize),
1125       llvm::ConstantInt::get(Int32Ty,
1126                              /*Version=*/CoverageMappingVersion1),
1127       RecordsVal, FilenamesAndMappingsVal};
1128   auto CovDataVal =
1129       llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
1130   auto CovData = new llvm::GlobalVariable(CGM.getModule(), CovDataTy, true,
1131                                           llvm::GlobalValue::InternalLinkage,
1132                                           CovDataVal,
1133                                           "__llvm_coverage_mapping");
1134 
1135   CovData->setSection(getCoverageSection(CGM));
1136   CovData->setAlignment(8);
1137 
1138   // Make sure the data doesn't get deleted.
1139   CGM.addUsedGlobal(CovData);
1140 }
1141 
1142 unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
1143   auto It = FileEntries.find(File);
1144   if (It != FileEntries.end())
1145     return It->second;
1146   unsigned FileID = FileEntries.size();
1147   FileEntries.insert(std::make_pair(File, FileID));
1148   return FileID;
1149 }
1150 
1151 void CoverageMappingGen::emitCounterMapping(const Decl *D,
1152                                             llvm::raw_ostream &OS) {
1153   assert(CounterMap);
1154   CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
1155   Walker.VisitDecl(D);
1156   Walker.write(OS);
1157 }
1158 
1159 void CoverageMappingGen::emitEmptyMapping(const Decl *D,
1160                                           llvm::raw_ostream &OS) {
1161   EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
1162   Walker.VisitDecl(D);
1163   Walker.write(OS);
1164 }
1165