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