1 //===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Instrumentation-based code coverage mapping generator
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CoverageMappingGen.h"
15 #include "CodeGenFunction.h"
16 #include "clang/AST/StmtVisitor.h"
17 #include "clang/Lex/Lexer.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
22 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
23 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
24 #include "llvm/ProfileData/InstrProfReader.h"
25 #include "llvm/Support/FileSystem.h"
26 
27 using namespace clang;
28 using namespace CodeGen;
29 using namespace llvm::coverage;
30 
31 void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range) {
32   SkippedRanges.push_back(Range);
33 }
34 
35 namespace {
36 
37 /// \brief A region of source code that can be mapped to a counter.
38 class SourceMappingRegion {
39   Counter Count;
40 
41   /// \brief The region's starting location.
42   Optional<SourceLocation> LocStart;
43 
44   /// \brief The region's ending location.
45   Optional<SourceLocation> LocEnd;
46 
47 public:
48   SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart,
49                       Optional<SourceLocation> LocEnd)
50       : Count(Count), LocStart(LocStart), LocEnd(LocEnd) {}
51 
52   const Counter &getCounter() const { return Count; }
53 
54   void setCounter(Counter C) { Count = C; }
55 
56   bool hasStartLoc() const { return LocStart.hasValue(); }
57 
58   void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
59 
60   SourceLocation getStartLoc() const {
61     assert(LocStart && "Region has no start location");
62     return *LocStart;
63   }
64 
65   bool hasEndLoc() const { return LocEnd.hasValue(); }
66 
67   void setEndLoc(SourceLocation Loc) { LocEnd = Loc; }
68 
69   SourceLocation getEndLoc() const {
70     assert(LocEnd && "Region has no end location");
71     return *LocEnd;
72   }
73 };
74 
75 /// \brief Provides the common functionality for the different
76 /// coverage mapping region builders.
77 class CoverageMappingBuilder {
78 public:
79   CoverageMappingModuleGen &CVM;
80   SourceManager &SM;
81   const LangOptions &LangOpts;
82 
83 private:
84   /// \brief Map of clang's FileIDs to IDs used for coverage mapping.
85   llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
86       FileIDMapping;
87 
88 public:
89   /// \brief The coverage mapping regions for this function
90   llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
91   /// \brief The source mapping regions for this function.
92   std::vector<SourceMappingRegion> SourceRegions;
93 
94   CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
95                          const LangOptions &LangOpts)
96       : CVM(CVM), SM(SM), LangOpts(LangOpts) {}
97 
98   /// \brief Return the precise end location for the given token.
99   SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
100     // We avoid getLocForEndOfToken here, because it doesn't do what we want for
101     // macro locations, which we just treat as expanded files.
102     unsigned TokLen =
103         Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
104     return Loc.getLocWithOffset(TokLen);
105   }
106 
107   /// \brief Return the start location of an included file or expanded macro.
108   SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
109     if (Loc.isMacroID())
110       return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
111     return SM.getLocForStartOfFile(SM.getFileID(Loc));
112   }
113 
114   /// \brief Return the end location of an included file or expanded macro.
115   SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
116     if (Loc.isMacroID())
117       return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
118                                   SM.getFileOffset(Loc));
119     return SM.getLocForEndOfFile(SM.getFileID(Loc));
120   }
121 
122   /// \brief Find out where the current file is included or macro is expanded.
123   SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
124     return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).first
125                            : SM.getIncludeLoc(SM.getFileID(Loc));
126   }
127 
128   /// \brief Return true if \c Loc is a location in a built-in macro.
129   bool isInBuiltin(SourceLocation Loc) {
130     return strcmp(SM.getBufferName(SM.getSpellingLoc(Loc)), "<built-in>") == 0;
131   }
132 
133   /// \brief Check whether \c Loc is included or expanded from \c Parent.
134   bool isNestedIn(SourceLocation Loc, FileID Parent) {
135     do {
136       Loc = getIncludeOrExpansionLoc(Loc);
137       if (Loc.isInvalid())
138         return false;
139     } while (!SM.isInFileID(Loc, Parent));
140     return true;
141   }
142 
143   /// \brief Get the start of \c S ignoring macro arguments and builtin macros.
144   SourceLocation getStart(const Stmt *S) {
145     SourceLocation Loc = S->getLocStart();
146     while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
147       Loc = SM.getImmediateExpansionRange(Loc).first;
148     return Loc;
149   }
150 
151   /// \brief Get the end of \c S ignoring macro arguments and builtin macros.
152   SourceLocation getEnd(const Stmt *S) {
153     SourceLocation Loc = S->getLocEnd();
154     while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
155       Loc = SM.getImmediateExpansionRange(Loc).first;
156     return getPreciseTokenLocEnd(Loc);
157   }
158 
159   /// \brief Find the set of files we have regions for and assign IDs
160   ///
161   /// Fills \c Mapping with the virtual file mapping needed to write out
162   /// coverage and collects the necessary file information to emit source and
163   /// expansion regions.
164   void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
165     FileIDMapping.clear();
166 
167     llvm::SmallSet<FileID, 8> Visited;
168     SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
169     for (const auto &Region : SourceRegions) {
170       SourceLocation Loc = Region.getStartLoc();
171       FileID File = SM.getFileID(Loc);
172       if (!Visited.insert(File).second)
173         continue;
174 
175       unsigned Depth = 0;
176       for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
177            Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent))
178         ++Depth;
179       FileLocs.push_back(std::make_pair(Loc, Depth));
180     }
181     std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second());
182 
183     for (const auto &FL : FileLocs) {
184       SourceLocation Loc = FL.first;
185       FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
186       auto Entry = SM.getFileEntryForID(SpellingFile);
187       if (!Entry)
188         continue;
189 
190       FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
191       Mapping.push_back(CVM.getFileID(Entry));
192     }
193   }
194 
195   /// \brief Get the coverage mapping file ID for \c Loc.
196   ///
197   /// If such file id doesn't exist, return None.
198   Optional<unsigned> getCoverageFileID(SourceLocation Loc) {
199     auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
200     if (Mapping != FileIDMapping.end())
201       return Mapping->second.first;
202     return None;
203   }
204 
205   /// \brief Return true if the given clang's file id has a corresponding
206   /// coverage file id.
207   bool hasExistingCoverageFileID(FileID File) const {
208     return FileIDMapping.count(File);
209   }
210 
211   /// \brief Gather all the regions that were skipped by the preprocessor
212   /// using the constructs like #if.
213   void gatherSkippedRegions() {
214     /// An array of the minimum lineStarts and the maximum lineEnds
215     /// for mapping regions from the appropriate source files.
216     llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
217     FileLineRanges.resize(
218         FileIDMapping.size(),
219         std::make_pair(std::numeric_limits<unsigned>::max(), 0));
220     for (const auto &R : MappingRegions) {
221       FileLineRanges[R.FileID].first =
222           std::min(FileLineRanges[R.FileID].first, R.LineStart);
223       FileLineRanges[R.FileID].second =
224           std::max(FileLineRanges[R.FileID].second, R.LineEnd);
225     }
226 
227     auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
228     for (const auto &I : SkippedRanges) {
229       auto LocStart = I.getBegin();
230       auto LocEnd = I.getEnd();
231       assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
232              "region spans multiple files");
233 
234       auto CovFileID = getCoverageFileID(LocStart);
235       if (!CovFileID)
236         continue;
237       unsigned LineStart = SM.getSpellingLineNumber(LocStart);
238       unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
239       unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
240       unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
241       auto Region = CounterMappingRegion::makeSkipped(
242           *CovFileID, LineStart, ColumnStart, LineEnd, ColumnEnd);
243       // Make sure that we only collect the regions that are inside
244       // the souce code of this function.
245       if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
246           Region.LineEnd <= FileLineRanges[*CovFileID].second)
247         MappingRegions.push_back(Region);
248     }
249   }
250 
251   /// \brief Generate the coverage counter mapping regions from collected
252   /// source regions.
253   void emitSourceRegions() {
254     for (const auto &Region : SourceRegions) {
255       assert(Region.hasEndLoc() && "incomplete region");
256 
257       SourceLocation LocStart = Region.getStartLoc();
258       assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
259 
260       auto CovFileID = getCoverageFileID(LocStart);
261       // Ignore regions that don't have a file, such as builtin macros.
262       if (!CovFileID)
263         continue;
264 
265       SourceLocation LocEnd = Region.getEndLoc();
266       assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
267              "region spans multiple files");
268 
269       // Find the spilling locations for the mapping region.
270       unsigned LineStart = SM.getSpellingLineNumber(LocStart);
271       unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
272       unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
273       unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
274 
275       assert(LineStart <= LineEnd && "region start and end out of order");
276       MappingRegions.push_back(CounterMappingRegion::makeRegion(
277           Region.getCounter(), *CovFileID, LineStart, ColumnStart, LineEnd,
278           ColumnEnd));
279     }
280   }
281 
282   /// \brief Generate expansion regions for each virtual file we've seen.
283   void emitExpansionRegions() {
284     for (const auto &FM : FileIDMapping) {
285       SourceLocation ExpandedLoc = FM.second.second;
286       SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
287       if (ParentLoc.isInvalid())
288         continue;
289 
290       auto ParentFileID = getCoverageFileID(ParentLoc);
291       if (!ParentFileID)
292         continue;
293       auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
294       assert(ExpandedFileID && "expansion in uncovered file");
295 
296       SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
297       assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
298              "region spans multiple files");
299 
300       unsigned LineStart = SM.getSpellingLineNumber(ParentLoc);
301       unsigned ColumnStart = SM.getSpellingColumnNumber(ParentLoc);
302       unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
303       unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
304 
305       MappingRegions.push_back(CounterMappingRegion::makeExpansion(
306           *ParentFileID, *ExpandedFileID, LineStart, ColumnStart, LineEnd,
307           ColumnEnd));
308     }
309   }
310 };
311 
312 /// \brief Creates unreachable coverage regions for the functions that
313 /// are not emitted.
314 struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
315   EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
316                               const LangOptions &LangOpts)
317       : CoverageMappingBuilder(CVM, SM, LangOpts) {}
318 
319   void VisitDecl(const Decl *D) {
320     if (!D->hasBody())
321       return;
322     auto Body = D->getBody();
323     SourceLocation Start = getStart(Body);
324     SourceLocation End = getEnd(Body);
325     if (!SM.isWrittenInSameFile(Start, End)) {
326       // Walk up to find the common ancestor.
327       // Correct the locations accordingly.
328       FileID StartFileID = SM.getFileID(Start);
329       FileID EndFileID = SM.getFileID(End);
330       while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) {
331         Start = getIncludeOrExpansionLoc(Start);
332         assert(Start.isValid() &&
333                "Declaration start location not nested within a known region");
334         StartFileID = SM.getFileID(Start);
335       }
336       while (StartFileID != EndFileID) {
337         End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
338         assert(End.isValid() &&
339                "Declaration end location not nested within a known region");
340         EndFileID = SM.getFileID(End);
341       }
342     }
343     SourceRegions.emplace_back(Counter(), Start, End);
344   }
345 
346   /// \brief Write the mapping data to the output stream
347   void write(llvm::raw_ostream &OS) {
348     SmallVector<unsigned, 16> FileIDMapping;
349     gatherFileIDs(FileIDMapping);
350     emitSourceRegions();
351 
352     CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions);
353     Writer.write(OS);
354   }
355 };
356 
357 /// \brief A StmtVisitor that creates coverage mapping regions which map
358 /// from the source code locations to the PGO counters.
359 struct CounterCoverageMappingBuilder
360     : public CoverageMappingBuilder,
361       public ConstStmtVisitor<CounterCoverageMappingBuilder> {
362   /// \brief The map of statements to count values.
363   llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
364 
365   /// \brief A stack of currently live regions.
366   std::vector<SourceMappingRegion> RegionStack;
367 
368   CounterExpressionBuilder Builder;
369 
370   /// \brief A location in the most recently visited file or macro.
371   ///
372   /// This is used to adjust the active source regions appropriately when
373   /// expressions cross file or macro boundaries.
374   SourceLocation MostRecentLocation;
375 
376   /// \brief Return a counter for the subtraction of \c RHS from \c LHS
377   Counter subtractCounters(Counter LHS, Counter RHS) {
378     return Builder.subtract(LHS, RHS);
379   }
380 
381   /// \brief Return a counter for the sum of \c LHS and \c RHS.
382   Counter addCounters(Counter LHS, Counter RHS) {
383     return Builder.add(LHS, RHS);
384   }
385 
386   Counter addCounters(Counter C1, Counter C2, Counter C3) {
387     return addCounters(addCounters(C1, C2), C3);
388   }
389 
390   Counter addCounters(Counter C1, Counter C2, Counter C3, Counter C4) {
391     return addCounters(addCounters(C1, C2, C3), C4);
392   }
393 
394   /// \brief Return the region counter for the given statement.
395   ///
396   /// This should only be called on statements that have a dedicated counter.
397   Counter getRegionCounter(const Stmt *S) {
398     return Counter::getCounter(CounterMap[S]);
399   }
400 
401   /// \brief Push a region onto the stack.
402   ///
403   /// Returns the index on the stack where the region was pushed. This can be
404   /// used with popRegions to exit a "scope", ending the region that was pushed.
405   size_t pushRegion(Counter Count, Optional<SourceLocation> StartLoc = None,
406                     Optional<SourceLocation> EndLoc = None) {
407     if (StartLoc)
408       MostRecentLocation = *StartLoc;
409     RegionStack.emplace_back(Count, StartLoc, EndLoc);
410 
411     return RegionStack.size() - 1;
412   }
413 
414   /// \brief Pop regions from the stack into the function's list of regions.
415   ///
416   /// Adds all regions from \c ParentIndex to the top of the stack to the
417   /// function's \c SourceRegions.
418   void popRegions(size_t ParentIndex) {
419     assert(RegionStack.size() >= ParentIndex && "parent not in stack");
420     while (RegionStack.size() > ParentIndex) {
421       SourceMappingRegion &Region = RegionStack.back();
422       if (Region.hasStartLoc()) {
423         SourceLocation StartLoc = Region.getStartLoc();
424         SourceLocation EndLoc = Region.hasEndLoc()
425                                     ? Region.getEndLoc()
426                                     : RegionStack[ParentIndex].getEndLoc();
427         while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
428           // The region ends in a nested file or macro expansion. Create a
429           // separate region for each expansion.
430           SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
431           assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
432 
433           SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
434 
435           EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
436           if (EndLoc.isInvalid())
437             llvm::report_fatal_error("File exit not handled before popRegions");
438         }
439         Region.setEndLoc(EndLoc);
440 
441         MostRecentLocation = EndLoc;
442         // If this region happens to span an entire expansion, we need to make
443         // sure we don't overlap the parent region with it.
444         if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
445             EndLoc == getEndOfFileOrMacro(EndLoc))
446           MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
447 
448         assert(SM.isWrittenInSameFile(Region.getStartLoc(), EndLoc));
449         SourceRegions.push_back(Region);
450       }
451       RegionStack.pop_back();
452     }
453   }
454 
455   /// \brief Return the currently active region.
456   SourceMappingRegion &getRegion() {
457     assert(!RegionStack.empty() && "statement has no region");
458     return RegionStack.back();
459   }
460 
461   /// \brief Propagate counts through the children of \c S.
462   Counter propagateCounts(Counter TopCount, const Stmt *S) {
463     size_t Index = pushRegion(TopCount, getStart(S), getEnd(S));
464     Visit(S);
465     Counter ExitCount = getRegion().getCounter();
466     popRegions(Index);
467 
468     // The statement may be spanned by an expansion. Make sure we handle a file
469     // exit out of this expansion before moving to the next statement.
470     if (SM.isBeforeInTranslationUnit(getStart(S), S->getLocStart()))
471       MostRecentLocation = getEnd(S);
472 
473     return ExitCount;
474   }
475 
476   /// \brief Check whether a region with bounds \c StartLoc and \c EndLoc
477   /// is already added to \c SourceRegions.
478   bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc) {
479     return SourceRegions.rend() !=
480            std::find_if(SourceRegions.rbegin(), SourceRegions.rend(),
481                         [&](const SourceMappingRegion &Region) {
482                           return Region.getStartLoc() == StartLoc &&
483                                  Region.getEndLoc() == EndLoc;
484                         });
485   }
486 
487   /// \brief Adjust the most recently visited location to \c EndLoc.
488   ///
489   /// This should be used after visiting any statements in non-source order.
490   void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
491     MostRecentLocation = EndLoc;
492     // The code region for a whole macro is created in handleFileExit() when
493     // it detects exiting of the virtual file of that macro. If we visited
494     // statements in non-source order, we might already have such a region
495     // added, for example, if a body of a loop is divided among multiple
496     // macros. Avoid adding duplicate regions in such case.
497     if (getRegion().hasEndLoc() &&
498         MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) &&
499         isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
500                              MostRecentLocation))
501       MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
502   }
503 
504   /// \brief Adjust regions and state when \c NewLoc exits a file.
505   ///
506   /// If moving from our most recently tracked location to \c NewLoc exits any
507   /// files, this adjusts our current region stack and creates the file regions
508   /// for the exited file.
509   void handleFileExit(SourceLocation NewLoc) {
510     if (NewLoc.isInvalid() ||
511         SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
512       return;
513 
514     // If NewLoc is not in a file that contains MostRecentLocation, walk up to
515     // find the common ancestor.
516     SourceLocation LCA = NewLoc;
517     FileID ParentFile = SM.getFileID(LCA);
518     while (!isNestedIn(MostRecentLocation, ParentFile)) {
519       LCA = getIncludeOrExpansionLoc(LCA);
520       if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
521         // Since there isn't a common ancestor, no file was exited. We just need
522         // to adjust our location to the new file.
523         MostRecentLocation = NewLoc;
524         return;
525       }
526       ParentFile = SM.getFileID(LCA);
527     }
528 
529     llvm::SmallSet<SourceLocation, 8> StartLocs;
530     Optional<Counter> ParentCounter;
531     for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
532       if (!I.hasStartLoc())
533         continue;
534       SourceLocation Loc = I.getStartLoc();
535       if (!isNestedIn(Loc, ParentFile)) {
536         ParentCounter = I.getCounter();
537         break;
538       }
539 
540       while (!SM.isInFileID(Loc, ParentFile)) {
541         // The most nested region for each start location is the one with the
542         // correct count. We avoid creating redundant regions by stopping once
543         // we've seen this region.
544         if (StartLocs.insert(Loc).second)
545           SourceRegions.emplace_back(I.getCounter(), Loc,
546                                      getEndOfFileOrMacro(Loc));
547         Loc = getIncludeOrExpansionLoc(Loc);
548       }
549       I.setStartLoc(getPreciseTokenLocEnd(Loc));
550     }
551 
552     if (ParentCounter) {
553       // If the file is contained completely by another region and doesn't
554       // immediately start its own region, the whole file gets a region
555       // corresponding to the parent.
556       SourceLocation Loc = MostRecentLocation;
557       while (isNestedIn(Loc, ParentFile)) {
558         SourceLocation FileStart = getStartOfFileOrMacro(Loc);
559         if (StartLocs.insert(FileStart).second)
560           SourceRegions.emplace_back(*ParentCounter, FileStart,
561                                      getEndOfFileOrMacro(Loc));
562         Loc = getIncludeOrExpansionLoc(Loc);
563       }
564     }
565 
566     MostRecentLocation = NewLoc;
567   }
568 
569   /// \brief Ensure that \c S is included in the current region.
570   void extendRegion(const Stmt *S) {
571     SourceMappingRegion &Region = getRegion();
572     SourceLocation StartLoc = getStart(S);
573 
574     handleFileExit(StartLoc);
575     if (!Region.hasStartLoc())
576       Region.setStartLoc(StartLoc);
577   }
578 
579   /// \brief Mark \c S as a terminator, starting a zero region.
580   void terminateRegion(const Stmt *S) {
581     extendRegion(S);
582     SourceMappingRegion &Region = getRegion();
583     if (!Region.hasEndLoc())
584       Region.setEndLoc(getEnd(S));
585     pushRegion(Counter::getZero());
586   }
587 
588   /// \brief Keep counts of breaks and continues inside loops.
589   struct BreakContinue {
590     Counter BreakCount;
591     Counter ContinueCount;
592   };
593   SmallVector<BreakContinue, 8> BreakContinueStack;
594 
595   CounterCoverageMappingBuilder(
596       CoverageMappingModuleGen &CVM,
597       llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
598       const LangOptions &LangOpts)
599       : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap) {}
600 
601   /// \brief Write the mapping data to the output stream
602   void write(llvm::raw_ostream &OS) {
603     llvm::SmallVector<unsigned, 8> VirtualFileMapping;
604     gatherFileIDs(VirtualFileMapping);
605     emitSourceRegions();
606     emitExpansionRegions();
607     gatherSkippedRegions();
608 
609     CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
610                                  MappingRegions);
611     Writer.write(OS);
612   }
613 
614   void VisitStmt(const Stmt *S) {
615     if (S->getLocStart().isValid())
616       extendRegion(S);
617     for (const Stmt *Child : S->children())
618       if (Child)
619         this->Visit(Child);
620     handleFileExit(getEnd(S));
621   }
622 
623   void VisitDecl(const Decl *D) {
624     Stmt *Body = D->getBody();
625     propagateCounts(getRegionCounter(Body), Body);
626   }
627 
628   void VisitReturnStmt(const ReturnStmt *S) {
629     extendRegion(S);
630     if (S->getRetValue())
631       Visit(S->getRetValue());
632     terminateRegion(S);
633   }
634 
635   void VisitCXXThrowExpr(const CXXThrowExpr *E) {
636     extendRegion(E);
637     if (E->getSubExpr())
638       Visit(E->getSubExpr());
639     terminateRegion(E);
640   }
641 
642   void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
643 
644   void VisitLabelStmt(const LabelStmt *S) {
645     SourceLocation Start = getStart(S);
646     // We can't extendRegion here or we risk overlapping with our new region.
647     handleFileExit(Start);
648     pushRegion(getRegionCounter(S), Start);
649     Visit(S->getSubStmt());
650   }
651 
652   void VisitBreakStmt(const BreakStmt *S) {
653     assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
654     BreakContinueStack.back().BreakCount = addCounters(
655         BreakContinueStack.back().BreakCount, getRegion().getCounter());
656     terminateRegion(S);
657   }
658 
659   void VisitContinueStmt(const ContinueStmt *S) {
660     assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
661     BreakContinueStack.back().ContinueCount = addCounters(
662         BreakContinueStack.back().ContinueCount, getRegion().getCounter());
663     terminateRegion(S);
664   }
665 
666   void VisitWhileStmt(const WhileStmt *S) {
667     extendRegion(S);
668 
669     Counter ParentCount = getRegion().getCounter();
670     Counter BodyCount = getRegionCounter(S);
671 
672     // Handle the body first so that we can get the backedge count.
673     BreakContinueStack.push_back(BreakContinue());
674     extendRegion(S->getBody());
675     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
676     BreakContinue BC = BreakContinueStack.pop_back_val();
677 
678     // Go back to handle the condition.
679     Counter CondCount =
680         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
681     propagateCounts(CondCount, S->getCond());
682     adjustForOutOfOrderTraversal(getEnd(S));
683 
684     Counter OutCount =
685         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
686     if (OutCount != ParentCount)
687       pushRegion(OutCount);
688   }
689 
690   void VisitDoStmt(const DoStmt *S) {
691     extendRegion(S);
692 
693     Counter ParentCount = getRegion().getCounter();
694     Counter BodyCount = getRegionCounter(S);
695 
696     BreakContinueStack.push_back(BreakContinue());
697     extendRegion(S->getBody());
698     Counter BackedgeCount =
699         propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
700     BreakContinue BC = BreakContinueStack.pop_back_val();
701 
702     Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
703     propagateCounts(CondCount, S->getCond());
704 
705     Counter OutCount =
706         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
707     if (OutCount != ParentCount)
708       pushRegion(OutCount);
709   }
710 
711   void VisitForStmt(const ForStmt *S) {
712     extendRegion(S);
713     if (S->getInit())
714       Visit(S->getInit());
715 
716     Counter ParentCount = getRegion().getCounter();
717     Counter BodyCount = getRegionCounter(S);
718 
719     // Handle the body first so that we can get the backedge count.
720     BreakContinueStack.push_back(BreakContinue());
721     extendRegion(S->getBody());
722     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
723     BreakContinue BC = BreakContinueStack.pop_back_val();
724 
725     // The increment is essentially part of the body but it needs to include
726     // the count for all the continue statements.
727     if (const Stmt *Inc = S->getInc())
728       propagateCounts(addCounters(BackedgeCount, BC.ContinueCount), Inc);
729 
730     // Go back to handle the condition.
731     Counter CondCount =
732         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
733     if (const Expr *Cond = S->getCond()) {
734       propagateCounts(CondCount, Cond);
735       adjustForOutOfOrderTraversal(getEnd(S));
736     }
737 
738     Counter OutCount =
739         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
740     if (OutCount != ParentCount)
741       pushRegion(OutCount);
742   }
743 
744   void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
745     extendRegion(S);
746     Visit(S->getLoopVarStmt());
747     Visit(S->getRangeStmt());
748 
749     Counter ParentCount = getRegion().getCounter();
750     Counter BodyCount = getRegionCounter(S);
751 
752     BreakContinueStack.push_back(BreakContinue());
753     extendRegion(S->getBody());
754     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
755     BreakContinue BC = BreakContinueStack.pop_back_val();
756 
757     Counter LoopCount =
758         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
759     Counter OutCount =
760         addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
761     if (OutCount != ParentCount)
762       pushRegion(OutCount);
763   }
764 
765   void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
766     extendRegion(S);
767     Visit(S->getElement());
768 
769     Counter ParentCount = getRegion().getCounter();
770     Counter BodyCount = getRegionCounter(S);
771 
772     BreakContinueStack.push_back(BreakContinue());
773     extendRegion(S->getBody());
774     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
775     BreakContinue BC = BreakContinueStack.pop_back_val();
776 
777     Counter LoopCount =
778         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
779     Counter OutCount =
780         addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
781     if (OutCount != ParentCount)
782       pushRegion(OutCount);
783   }
784 
785   void VisitSwitchStmt(const SwitchStmt *S) {
786     extendRegion(S);
787     Visit(S->getCond());
788 
789     BreakContinueStack.push_back(BreakContinue());
790 
791     const Stmt *Body = S->getBody();
792     extendRegion(Body);
793     if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
794       if (!CS->body_empty()) {
795         // The body of the switch needs a zero region so that fallthrough counts
796         // behave correctly, but it would be misleading to include the braces of
797         // the compound statement in the zeroed area, so we need to handle this
798         // specially.
799         size_t Index =
800             pushRegion(Counter::getZero(), getStart(CS->body_front()),
801                        getEnd(CS->body_back()));
802         for (const auto *Child : CS->children())
803           Visit(Child);
804         popRegions(Index);
805       }
806     } else
807       propagateCounts(Counter::getZero(), Body);
808     BreakContinue BC = BreakContinueStack.pop_back_val();
809 
810     if (!BreakContinueStack.empty())
811       BreakContinueStack.back().ContinueCount = addCounters(
812           BreakContinueStack.back().ContinueCount, BC.ContinueCount);
813 
814     Counter ExitCount = getRegionCounter(S);
815     SourceLocation ExitLoc = getEnd(S);
816     pushRegion(ExitCount, getStart(S), ExitLoc);
817     handleFileExit(ExitLoc);
818   }
819 
820   void VisitSwitchCase(const SwitchCase *S) {
821     extendRegion(S);
822 
823     SourceMappingRegion &Parent = getRegion();
824 
825     Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
826     // Reuse the existing region if it starts at our label. This is typical of
827     // the first case in a switch.
828     if (Parent.hasStartLoc() && Parent.getStartLoc() == getStart(S))
829       Parent.setCounter(Count);
830     else
831       pushRegion(Count, getStart(S));
832 
833     if (const auto *CS = dyn_cast<CaseStmt>(S)) {
834       Visit(CS->getLHS());
835       if (const Expr *RHS = CS->getRHS())
836         Visit(RHS);
837     }
838     Visit(S->getSubStmt());
839   }
840 
841   void VisitIfStmt(const IfStmt *S) {
842     extendRegion(S);
843     // Extend into the condition before we propagate through it below - this is
844     // needed to handle macros that generate the "if" but not the condition.
845     extendRegion(S->getCond());
846 
847     Counter ParentCount = getRegion().getCounter();
848     Counter ThenCount = getRegionCounter(S);
849 
850     // Emitting a counter for the condition makes it easier to interpret the
851     // counter for the body when looking at the coverage.
852     propagateCounts(ParentCount, S->getCond());
853 
854     extendRegion(S->getThen());
855     Counter OutCount = propagateCounts(ThenCount, S->getThen());
856 
857     Counter ElseCount = subtractCounters(ParentCount, ThenCount);
858     if (const Stmt *Else = S->getElse()) {
859       extendRegion(S->getElse());
860       OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
861     } else
862       OutCount = addCounters(OutCount, ElseCount);
863 
864     if (OutCount != ParentCount)
865       pushRegion(OutCount);
866   }
867 
868   void VisitCXXTryStmt(const CXXTryStmt *S) {
869     extendRegion(S);
870     Visit(S->getTryBlock());
871     for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
872       Visit(S->getHandler(I));
873 
874     Counter ExitCount = getRegionCounter(S);
875     pushRegion(ExitCount);
876   }
877 
878   void VisitCXXCatchStmt(const CXXCatchStmt *S) {
879     propagateCounts(getRegionCounter(S), S->getHandlerBlock());
880   }
881 
882   void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
883     extendRegion(E);
884 
885     Counter ParentCount = getRegion().getCounter();
886     Counter TrueCount = getRegionCounter(E);
887 
888     Visit(E->getCond());
889 
890     if (!isa<BinaryConditionalOperator>(E)) {
891       extendRegion(E->getTrueExpr());
892       propagateCounts(TrueCount, E->getTrueExpr());
893     }
894     extendRegion(E->getFalseExpr());
895     propagateCounts(subtractCounters(ParentCount, TrueCount),
896                     E->getFalseExpr());
897   }
898 
899   void VisitBinLAnd(const BinaryOperator *E) {
900     extendRegion(E);
901     Visit(E->getLHS());
902 
903     extendRegion(E->getRHS());
904     propagateCounts(getRegionCounter(E), E->getRHS());
905   }
906 
907   void VisitBinLOr(const BinaryOperator *E) {
908     extendRegion(E);
909     Visit(E->getLHS());
910 
911     extendRegion(E->getRHS());
912     propagateCounts(getRegionCounter(E), E->getRHS());
913   }
914 
915   void VisitLambdaExpr(const LambdaExpr *LE) {
916     // Lambdas are treated as their own functions for now, so we shouldn't
917     // propagate counts into them.
918   }
919 };
920 }
921 
922 static bool isMachO(const CodeGenModule &CGM) {
923   return CGM.getTarget().getTriple().isOSBinFormatMachO();
924 }
925 
926 static StringRef getCoverageSection(const CodeGenModule &CGM) {
927   return llvm::getInstrProfCoverageSectionName(isMachO(CGM));
928 }
929 
930 static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
931                  ArrayRef<CounterExpression> Expressions,
932                  ArrayRef<CounterMappingRegion> Regions) {
933   OS << FunctionName << ":\n";
934   CounterMappingContext Ctx(Expressions);
935   for (const auto &R : Regions) {
936     OS.indent(2);
937     switch (R.Kind) {
938     case CounterMappingRegion::CodeRegion:
939       break;
940     case CounterMappingRegion::ExpansionRegion:
941       OS << "Expansion,";
942       break;
943     case CounterMappingRegion::SkippedRegion:
944       OS << "Skipped,";
945       break;
946     }
947 
948     OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
949        << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
950     Ctx.dump(R.Count, OS);
951     if (R.Kind == CounterMappingRegion::ExpansionRegion)
952       OS << " (Expanded file = " << R.ExpandedFileID << ")";
953     OS << "\n";
954   }
955 }
956 
957 void CoverageMappingModuleGen::addFunctionMappingRecord(
958     llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
959     const std::string &CoverageMapping, bool IsUsed) {
960   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
961   if (!FunctionRecordTy) {
962 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
963     llvm::Type *FunctionRecordTypes[] = {
964       #include "llvm/ProfileData/InstrProfData.inc"
965     };
966     FunctionRecordTy =
967         llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes),
968                               /*isPacked=*/true);
969   }
970 
971   #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
972   llvm::Constant *FunctionRecordVals[] = {
973       #include "llvm/ProfileData/InstrProfData.inc"
974   };
975   FunctionRecords.push_back(llvm::ConstantStruct::get(
976       FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
977   if (!IsUsed)
978     FunctionNames.push_back(
979         llvm::ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx)));
980   CoverageMappings.push_back(CoverageMapping);
981 
982   if (CGM.getCodeGenOpts().DumpCoverageMapping) {
983     // Dump the coverage mapping data for this function by decoding the
984     // encoded data. This allows us to dump the mapping regions which were
985     // also processed by the CoverageMappingWriter which performs
986     // additional minimization operations such as reducing the number of
987     // expressions.
988     std::vector<StringRef> Filenames;
989     std::vector<CounterExpression> Expressions;
990     std::vector<CounterMappingRegion> Regions;
991     llvm::SmallVector<StringRef, 16> FilenameRefs;
992     FilenameRefs.resize(FileEntries.size());
993     for (const auto &Entry : FileEntries)
994       FilenameRefs[Entry.second] = Entry.first->getName();
995     RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
996                                     Expressions, Regions);
997     if (Reader.read())
998       return;
999     dump(llvm::outs(), NameValue, Expressions, Regions);
1000   }
1001 }
1002 
1003 void CoverageMappingModuleGen::emit() {
1004   if (FunctionRecords.empty())
1005     return;
1006   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1007   auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
1008 
1009   // Create the filenames and merge them with coverage mappings
1010   llvm::SmallVector<std::string, 16> FilenameStrs;
1011   FilenameStrs.resize(FileEntries.size());
1012   for (const auto &Entry : FileEntries) {
1013     llvm::SmallString<256> Path(Entry.first->getName());
1014     llvm::sys::fs::make_absolute(Path);
1015 
1016     auto I = Entry.second;
1017     FilenameStrs[I] = std::string(Path.begin(), Path.end());
1018   }
1019 
1020   size_t FilenamesSize;
1021   size_t CoverageMappingSize;
1022   llvm::Expected<std::string> CoverageDataOrErr = encodeFilenamesAndRawMappings(
1023       FilenameStrs, CoverageMappings, FilenamesSize, CoverageMappingSize);
1024   if (llvm::Error E = CoverageDataOrErr.takeError()) {
1025     llvm::handleAllErrors(std::move(E), [](llvm::ErrorInfoBase &EI) {
1026       llvm::report_fatal_error(EI.message());
1027     });
1028   }
1029   std::string CoverageData = std::move(CoverageDataOrErr.get());
1030   auto *FilenamesAndMappingsVal =
1031       llvm::ConstantDataArray::getString(Ctx, CoverageData, false);
1032 
1033   // Create the deferred function records array
1034   auto RecordsTy =
1035       llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
1036   auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
1037 
1038   llvm::Type *CovDataHeaderTypes[] = {
1039 #define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
1040 #include "llvm/ProfileData/InstrProfData.inc"
1041   };
1042   auto CovDataHeaderTy =
1043       llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes));
1044   llvm::Constant *CovDataHeaderVals[] = {
1045 #define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
1046 #include "llvm/ProfileData/InstrProfData.inc"
1047   };
1048   auto CovDataHeaderVal = llvm::ConstantStruct::get(
1049       CovDataHeaderTy, makeArrayRef(CovDataHeaderVals));
1050 
1051   // Create the coverage data record
1052   llvm::Type *CovDataTypes[] = {CovDataHeaderTy, RecordsTy,
1053                                 FilenamesAndMappingsVal->getType()};
1054   auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
1055   llvm::Constant *TUDataVals[] = {CovDataHeaderVal, RecordsVal,
1056                                   FilenamesAndMappingsVal};
1057   auto CovDataVal =
1058       llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
1059   auto CovData = new llvm::GlobalVariable(
1060       CGM.getModule(), CovDataTy, true, llvm::GlobalValue::InternalLinkage,
1061       CovDataVal, llvm::getCoverageMappingVarName());
1062 
1063   CovData->setSection(getCoverageSection(CGM));
1064   CovData->setAlignment(8);
1065 
1066   // Make sure the data doesn't get deleted.
1067   CGM.addUsedGlobal(CovData);
1068   // Create the deferred function records array
1069   if (!FunctionNames.empty()) {
1070     auto NamesArrTy = llvm::ArrayType::get(llvm::Type::getInt8PtrTy(Ctx),
1071                                            FunctionNames.size());
1072     auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
1073     // This variable will *NOT* be emitted to the object file. It is used
1074     // to pass the list of names referenced to codegen.
1075     new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
1076                              llvm::GlobalValue::InternalLinkage, NamesArrVal,
1077                              llvm::getCoverageUnusedNamesVarName());
1078   }
1079 }
1080 
1081 unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
1082   auto It = FileEntries.find(File);
1083   if (It != FileEntries.end())
1084     return It->second;
1085   unsigned FileID = FileEntries.size();
1086   FileEntries.insert(std::make_pair(File, FileID));
1087   return FileID;
1088 }
1089 
1090 void CoverageMappingGen::emitCounterMapping(const Decl *D,
1091                                             llvm::raw_ostream &OS) {
1092   assert(CounterMap);
1093   CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
1094   Walker.VisitDecl(D);
1095   Walker.write(OS);
1096 }
1097 
1098 void CoverageMappingGen::emitEmptyMapping(const Decl *D,
1099                                           llvm::raw_ostream &OS) {
1100   EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
1101   Walker.VisitDecl(D);
1102   Walker.write(OS);
1103 }
1104