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/CoverageMapping.h"
22 #include "llvm/ProfileData/CoverageMappingReader.h"
23 #include "llvm/ProfileData/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 Adjust the most recently visited location to \c EndLoc.
447   ///
448   /// This should be used after visiting any statements in non-source order.
449   void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
450     MostRecentLocation = EndLoc;
451     // Avoid adding duplicate regions if we have a completed region on the top
452     // of the stack and are adjusting to the end of a virtual file.
453     if (getRegion().hasEndLoc() &&
454         MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation))
455       MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
456   }
457 
458   /// \brief Check whether \c Loc is included or expanded from \c Parent.
459   bool isNestedIn(SourceLocation Loc, FileID Parent) {
460     do {
461       Loc = getIncludeOrExpansionLoc(Loc);
462       if (Loc.isInvalid())
463         return false;
464     } while (!SM.isInFileID(Loc, Parent));
465     return true;
466   }
467 
468   /// \brief Adjust regions and state when \c NewLoc exits a file.
469   ///
470   /// If moving from our most recently tracked location to \c NewLoc exits any
471   /// files, this adjusts our current region stack and creates the file regions
472   /// for the exited file.
473   void handleFileExit(SourceLocation NewLoc) {
474     if (NewLoc.isInvalid() ||
475         SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
476       return;
477 
478     // If NewLoc is not in a file that contains MostRecentLocation, walk up to
479     // find the common ancestor.
480     SourceLocation LCA = NewLoc;
481     FileID ParentFile = SM.getFileID(LCA);
482     while (!isNestedIn(MostRecentLocation, ParentFile)) {
483       LCA = getIncludeOrExpansionLoc(LCA);
484       if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
485         // Since there isn't a common ancestor, no file was exited. We just need
486         // to adjust our location to the new file.
487         MostRecentLocation = NewLoc;
488         return;
489       }
490       ParentFile = SM.getFileID(LCA);
491     }
492 
493     llvm::SmallSet<SourceLocation, 8> StartLocs;
494     Optional<Counter> ParentCounter;
495     for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
496       if (!I.hasStartLoc())
497         continue;
498       SourceLocation Loc = I.getStartLoc();
499       if (!isNestedIn(Loc, ParentFile)) {
500         ParentCounter = I.getCounter();
501         break;
502       }
503 
504       while (!SM.isInFileID(Loc, ParentFile)) {
505         // The most nested region for each start location is the one with the
506         // correct count. We avoid creating redundant regions by stopping once
507         // we've seen this region.
508         if (StartLocs.insert(Loc).second)
509           SourceRegions.emplace_back(I.getCounter(), Loc,
510                                      getEndOfFileOrMacro(Loc));
511         Loc = getIncludeOrExpansionLoc(Loc);
512       }
513       I.setStartLoc(getPreciseTokenLocEnd(Loc));
514     }
515 
516     if (ParentCounter) {
517       // If the file is contained completely by another region and doesn't
518       // immediately start its own region, the whole file gets a region
519       // corresponding to the parent.
520       SourceLocation Loc = MostRecentLocation;
521       while (isNestedIn(Loc, ParentFile)) {
522         SourceLocation FileStart = getStartOfFileOrMacro(Loc);
523         if (StartLocs.insert(FileStart).second)
524           SourceRegions.emplace_back(*ParentCounter, FileStart,
525                                      getEndOfFileOrMacro(Loc));
526         Loc = getIncludeOrExpansionLoc(Loc);
527       }
528     }
529 
530     MostRecentLocation = NewLoc;
531   }
532 
533   /// \brief Ensure that \c S is included in the current region.
534   void extendRegion(const Stmt *S) {
535     SourceMappingRegion &Region = getRegion();
536     SourceLocation StartLoc = getStart(S);
537 
538     handleFileExit(StartLoc);
539     if (!Region.hasStartLoc())
540       Region.setStartLoc(StartLoc);
541   }
542 
543   /// \brief Mark \c S as a terminator, starting a zero region.
544   void terminateRegion(const Stmt *S) {
545     extendRegion(S);
546     SourceMappingRegion &Region = getRegion();
547     if (!Region.hasEndLoc())
548       Region.setEndLoc(getEnd(S));
549     pushRegion(Counter::getZero());
550   }
551 
552   /// \brief Keep counts of breaks and continues inside loops.
553   struct BreakContinue {
554     Counter BreakCount;
555     Counter ContinueCount;
556   };
557   SmallVector<BreakContinue, 8> BreakContinueStack;
558 
559   CounterCoverageMappingBuilder(
560       CoverageMappingModuleGen &CVM,
561       llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
562       const LangOptions &LangOpts)
563       : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap) {}
564 
565   /// \brief Write the mapping data to the output stream
566   void write(llvm::raw_ostream &OS) {
567     llvm::SmallVector<unsigned, 8> VirtualFileMapping;
568     gatherFileIDs(VirtualFileMapping);
569     emitSourceRegions();
570     emitExpansionRegions();
571     gatherSkippedRegions();
572 
573     CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
574                                  MappingRegions);
575     Writer.write(OS);
576   }
577 
578   void VisitStmt(const Stmt *S) {
579     if (S->getLocStart().isValid())
580       extendRegion(S);
581     for (const Stmt *Child : S->children())
582       if (Child)
583         this->Visit(Child);
584     handleFileExit(getEnd(S));
585   }
586 
587   void VisitDecl(const Decl *D) {
588     Stmt *Body = D->getBody();
589     propagateCounts(getRegionCounter(Body), Body);
590   }
591 
592   void VisitReturnStmt(const ReturnStmt *S) {
593     extendRegion(S);
594     if (S->getRetValue())
595       Visit(S->getRetValue());
596     terminateRegion(S);
597   }
598 
599   void VisitCXXThrowExpr(const CXXThrowExpr *E) {
600     extendRegion(E);
601     if (E->getSubExpr())
602       Visit(E->getSubExpr());
603     terminateRegion(E);
604   }
605 
606   void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
607 
608   void VisitLabelStmt(const LabelStmt *S) {
609     SourceLocation Start = getStart(S);
610     // We can't extendRegion here or we risk overlapping with our new region.
611     handleFileExit(Start);
612     pushRegion(getRegionCounter(S), Start);
613     Visit(S->getSubStmt());
614   }
615 
616   void VisitBreakStmt(const BreakStmt *S) {
617     assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
618     BreakContinueStack.back().BreakCount = addCounters(
619         BreakContinueStack.back().BreakCount, getRegion().getCounter());
620     terminateRegion(S);
621   }
622 
623   void VisitContinueStmt(const ContinueStmt *S) {
624     assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
625     BreakContinueStack.back().ContinueCount = addCounters(
626         BreakContinueStack.back().ContinueCount, getRegion().getCounter());
627     terminateRegion(S);
628   }
629 
630   void VisitWhileStmt(const WhileStmt *S) {
631     extendRegion(S);
632 
633     Counter ParentCount = getRegion().getCounter();
634     Counter BodyCount = getRegionCounter(S);
635 
636     // Handle the body first so that we can get the backedge count.
637     BreakContinueStack.push_back(BreakContinue());
638     extendRegion(S->getBody());
639     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
640     BreakContinue BC = BreakContinueStack.pop_back_val();
641 
642     // Go back to handle the condition.
643     Counter CondCount =
644         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
645     propagateCounts(CondCount, S->getCond());
646     adjustForOutOfOrderTraversal(getEnd(S));
647 
648     Counter OutCount =
649         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
650     if (OutCount != ParentCount)
651       pushRegion(OutCount);
652   }
653 
654   void VisitDoStmt(const DoStmt *S) {
655     extendRegion(S);
656 
657     Counter ParentCount = getRegion().getCounter();
658     Counter BodyCount = getRegionCounter(S);
659 
660     BreakContinueStack.push_back(BreakContinue());
661     extendRegion(S->getBody());
662     Counter BackedgeCount =
663         propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
664     BreakContinue BC = BreakContinueStack.pop_back_val();
665 
666     Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
667     propagateCounts(CondCount, S->getCond());
668 
669     Counter OutCount =
670         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
671     if (OutCount != ParentCount)
672       pushRegion(OutCount);
673   }
674 
675   void VisitForStmt(const ForStmt *S) {
676     extendRegion(S);
677     if (S->getInit())
678       Visit(S->getInit());
679 
680     Counter ParentCount = getRegion().getCounter();
681     Counter BodyCount = getRegionCounter(S);
682 
683     // Handle the body first so that we can get the backedge count.
684     BreakContinueStack.push_back(BreakContinue());
685     extendRegion(S->getBody());
686     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
687     BreakContinue BC = BreakContinueStack.pop_back_val();
688 
689     // The increment is essentially part of the body but it needs to include
690     // the count for all the continue statements.
691     if (const Stmt *Inc = S->getInc())
692       propagateCounts(addCounters(BackedgeCount, BC.ContinueCount), Inc);
693 
694     // Go back to handle the condition.
695     Counter CondCount =
696         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
697     if (const Expr *Cond = S->getCond()) {
698       propagateCounts(CondCount, Cond);
699       adjustForOutOfOrderTraversal(getEnd(S));
700     }
701 
702     Counter OutCount =
703         addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
704     if (OutCount != ParentCount)
705       pushRegion(OutCount);
706   }
707 
708   void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
709     extendRegion(S);
710     Visit(S->getLoopVarStmt());
711     Visit(S->getRangeStmt());
712 
713     Counter ParentCount = getRegion().getCounter();
714     Counter BodyCount = getRegionCounter(S);
715 
716     BreakContinueStack.push_back(BreakContinue());
717     extendRegion(S->getBody());
718     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
719     BreakContinue BC = BreakContinueStack.pop_back_val();
720 
721     Counter LoopCount =
722         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
723     Counter OutCount =
724         addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
725     if (OutCount != ParentCount)
726       pushRegion(OutCount);
727   }
728 
729   void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
730     extendRegion(S);
731     Visit(S->getElement());
732 
733     Counter ParentCount = getRegion().getCounter();
734     Counter BodyCount = getRegionCounter(S);
735 
736     BreakContinueStack.push_back(BreakContinue());
737     extendRegion(S->getBody());
738     Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
739     BreakContinue BC = BreakContinueStack.pop_back_val();
740 
741     Counter LoopCount =
742         addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
743     Counter OutCount =
744         addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
745     if (OutCount != ParentCount)
746       pushRegion(OutCount);
747   }
748 
749   void VisitSwitchStmt(const SwitchStmt *S) {
750     extendRegion(S);
751     Visit(S->getCond());
752 
753     BreakContinueStack.push_back(BreakContinue());
754 
755     const Stmt *Body = S->getBody();
756     extendRegion(Body);
757     if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
758       if (!CS->body_empty()) {
759         // The body of the switch needs a zero region so that fallthrough counts
760         // behave correctly, but it would be misleading to include the braces of
761         // the compound statement in the zeroed area, so we need to handle this
762         // specially.
763         size_t Index =
764             pushRegion(Counter::getZero(), getStart(CS->body_front()),
765                        getEnd(CS->body_back()));
766         for (const auto *Child : CS->children())
767           Visit(Child);
768         popRegions(Index);
769       }
770     } else
771       propagateCounts(Counter::getZero(), Body);
772     BreakContinue BC = BreakContinueStack.pop_back_val();
773 
774     if (!BreakContinueStack.empty())
775       BreakContinueStack.back().ContinueCount = addCounters(
776           BreakContinueStack.back().ContinueCount, BC.ContinueCount);
777 
778     Counter ExitCount = getRegionCounter(S);
779     pushRegion(ExitCount, getStart(S), getEnd(S));
780   }
781 
782   void VisitSwitchCase(const SwitchCase *S) {
783     extendRegion(S);
784 
785     SourceMappingRegion &Parent = getRegion();
786 
787     Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
788     // Reuse the existing region if it starts at our label. This is typical of
789     // the first case in a switch.
790     if (Parent.hasStartLoc() && Parent.getStartLoc() == getStart(S))
791       Parent.setCounter(Count);
792     else
793       pushRegion(Count, getStart(S));
794 
795     if (const auto *CS = dyn_cast<CaseStmt>(S)) {
796       Visit(CS->getLHS());
797       if (const Expr *RHS = CS->getRHS())
798         Visit(RHS);
799     }
800     Visit(S->getSubStmt());
801   }
802 
803   void VisitIfStmt(const IfStmt *S) {
804     extendRegion(S);
805     // Extend into the condition before we propagate through it below - this is
806     // needed to handle macros that generate the "if" but not the condition.
807     extendRegion(S->getCond());
808 
809     Counter ParentCount = getRegion().getCounter();
810     Counter ThenCount = getRegionCounter(S);
811 
812     // Emitting a counter for the condition makes it easier to interpret the
813     // counter for the body when looking at the coverage.
814     propagateCounts(ParentCount, S->getCond());
815 
816     extendRegion(S->getThen());
817     Counter OutCount = propagateCounts(ThenCount, S->getThen());
818 
819     Counter ElseCount = subtractCounters(ParentCount, ThenCount);
820     if (const Stmt *Else = S->getElse()) {
821       extendRegion(S->getElse());
822       OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
823     } else
824       OutCount = addCounters(OutCount, ElseCount);
825 
826     if (OutCount != ParentCount)
827       pushRegion(OutCount);
828   }
829 
830   void VisitCXXTryStmt(const CXXTryStmt *S) {
831     extendRegion(S);
832     Visit(S->getTryBlock());
833     for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
834       Visit(S->getHandler(I));
835 
836     Counter ExitCount = getRegionCounter(S);
837     pushRegion(ExitCount);
838   }
839 
840   void VisitCXXCatchStmt(const CXXCatchStmt *S) {
841     propagateCounts(getRegionCounter(S), S->getHandlerBlock());
842   }
843 
844   void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
845     extendRegion(E);
846 
847     Counter ParentCount = getRegion().getCounter();
848     Counter TrueCount = getRegionCounter(E);
849 
850     Visit(E->getCond());
851 
852     if (!isa<BinaryConditionalOperator>(E)) {
853       extendRegion(E->getTrueExpr());
854       propagateCounts(TrueCount, E->getTrueExpr());
855     }
856     extendRegion(E->getFalseExpr());
857     propagateCounts(subtractCounters(ParentCount, TrueCount),
858                     E->getFalseExpr());
859   }
860 
861   void VisitBinLAnd(const BinaryOperator *E) {
862     extendRegion(E);
863     Visit(E->getLHS());
864 
865     extendRegion(E->getRHS());
866     propagateCounts(getRegionCounter(E), E->getRHS());
867   }
868 
869   void VisitBinLOr(const BinaryOperator *E) {
870     extendRegion(E);
871     Visit(E->getLHS());
872 
873     extendRegion(E->getRHS());
874     propagateCounts(getRegionCounter(E), E->getRHS());
875   }
876 
877   void VisitLambdaExpr(const LambdaExpr *LE) {
878     // Lambdas are treated as their own functions for now, so we shouldn't
879     // propagate counts into them.
880   }
881 };
882 }
883 
884 static bool isMachO(const CodeGenModule &CGM) {
885   return CGM.getTarget().getTriple().isOSBinFormatMachO();
886 }
887 
888 static StringRef getCoverageSection(const CodeGenModule &CGM) {
889   return llvm::getInstrProfCoverageSectionName(isMachO(CGM));
890 }
891 
892 static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
893                  ArrayRef<CounterExpression> Expressions,
894                  ArrayRef<CounterMappingRegion> Regions) {
895   OS << FunctionName << ":\n";
896   CounterMappingContext Ctx(Expressions);
897   for (const auto &R : Regions) {
898     OS.indent(2);
899     switch (R.Kind) {
900     case CounterMappingRegion::CodeRegion:
901       break;
902     case CounterMappingRegion::ExpansionRegion:
903       OS << "Expansion,";
904       break;
905     case CounterMappingRegion::SkippedRegion:
906       OS << "Skipped,";
907       break;
908     }
909 
910     OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
911        << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
912     Ctx.dump(R.Count, OS);
913     if (R.Kind == CounterMappingRegion::ExpansionRegion)
914       OS << " (Expanded file = " << R.ExpandedFileID << ")";
915     OS << "\n";
916   }
917 }
918 
919 void CoverageMappingModuleGen::addFunctionMappingRecord(
920     llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
921     const std::string &CoverageMapping, bool IsUsed) {
922   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
923   if (!FunctionRecordTy) {
924 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
925     llvm::Type *FunctionRecordTypes[] = {
926       #include "llvm/ProfileData/InstrProfData.inc"
927     };
928     FunctionRecordTy =
929         llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes),
930                               /*isPacked=*/true);
931   }
932 
933   #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
934   llvm::Constant *FunctionRecordVals[] = {
935       #include "llvm/ProfileData/InstrProfData.inc"
936   };
937   FunctionRecords.push_back(llvm::ConstantStruct::get(
938       FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
939   if (!IsUsed)
940     FunctionNames.push_back(
941         llvm::ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx)));
942   CoverageMappings.push_back(CoverageMapping);
943 
944   if (CGM.getCodeGenOpts().DumpCoverageMapping) {
945     // Dump the coverage mapping data for this function by decoding the
946     // encoded data. This allows us to dump the mapping regions which were
947     // also processed by the CoverageMappingWriter which performs
948     // additional minimization operations such as reducing the number of
949     // expressions.
950     std::vector<StringRef> Filenames;
951     std::vector<CounterExpression> Expressions;
952     std::vector<CounterMappingRegion> Regions;
953     llvm::SmallVector<StringRef, 16> FilenameRefs;
954     FilenameRefs.resize(FileEntries.size());
955     for (const auto &Entry : FileEntries)
956       FilenameRefs[Entry.second] = Entry.first->getName();
957     RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
958                                     Expressions, Regions);
959     if (Reader.read())
960       return;
961     dump(llvm::outs(), NameValue, Expressions, Regions);
962   }
963 }
964 
965 void CoverageMappingModuleGen::emit() {
966   if (FunctionRecords.empty())
967     return;
968   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
969   auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
970 
971   // Create the filenames and merge them with coverage mappings
972   llvm::SmallVector<std::string, 16> FilenameStrs;
973   llvm::SmallVector<StringRef, 16> FilenameRefs;
974   FilenameStrs.resize(FileEntries.size());
975   FilenameRefs.resize(FileEntries.size());
976   for (const auto &Entry : FileEntries) {
977     llvm::SmallString<256> Path(Entry.first->getName());
978     llvm::sys::fs::make_absolute(Path);
979 
980     auto I = Entry.second;
981     FilenameStrs[I] = std::string(Path.begin(), Path.end());
982     FilenameRefs[I] = FilenameStrs[I];
983   }
984 
985   std::string FilenamesAndCoverageMappings;
986   llvm::raw_string_ostream OS(FilenamesAndCoverageMappings);
987   CoverageFilenamesSectionWriter(FilenameRefs).write(OS);
988   std::string RawCoverageMappings =
989       llvm::join(CoverageMappings.begin(), CoverageMappings.end(), "");
990   OS << RawCoverageMappings;
991   size_t CoverageMappingSize = RawCoverageMappings.size();
992   size_t FilenamesSize = OS.str().size() - CoverageMappingSize;
993   // Append extra zeroes if necessary to ensure that the size of the filenames
994   // and coverage mappings is a multiple of 8.
995   if (size_t Rem = OS.str().size() % 8) {
996     CoverageMappingSize += 8 - Rem;
997     for (size_t I = 0, S = 8 - Rem; I < S; ++I)
998       OS << '\0';
999   }
1000   auto *FilenamesAndMappingsVal =
1001       llvm::ConstantDataArray::getString(Ctx, OS.str(), false);
1002 
1003   // Create the deferred function records array
1004   auto RecordsTy =
1005       llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
1006   auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
1007 
1008   llvm::Type *CovDataHeaderTypes[] = {
1009 #define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
1010 #include "llvm/ProfileData/InstrProfData.inc"
1011   };
1012   auto CovDataHeaderTy =
1013       llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes));
1014   llvm::Constant *CovDataHeaderVals[] = {
1015 #define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
1016 #include "llvm/ProfileData/InstrProfData.inc"
1017   };
1018   auto CovDataHeaderVal = llvm::ConstantStruct::get(
1019       CovDataHeaderTy, makeArrayRef(CovDataHeaderVals));
1020 
1021   // Create the coverage data record
1022   llvm::Type *CovDataTypes[] = {CovDataHeaderTy, RecordsTy,
1023                                 FilenamesAndMappingsVal->getType()};
1024   auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
1025   llvm::Constant *TUDataVals[] = {CovDataHeaderVal, RecordsVal,
1026                                   FilenamesAndMappingsVal};
1027   auto CovDataVal =
1028       llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
1029   auto CovData = new llvm::GlobalVariable(
1030       CGM.getModule(), CovDataTy, true, llvm::GlobalValue::InternalLinkage,
1031       CovDataVal, llvm::getCoverageMappingVarName());
1032 
1033   CovData->setSection(getCoverageSection(CGM));
1034   CovData->setAlignment(8);
1035 
1036   // Make sure the data doesn't get deleted.
1037   CGM.addUsedGlobal(CovData);
1038   // Create the deferred function records array
1039   if (!FunctionNames.empty()) {
1040     auto NamesArrTy = llvm::ArrayType::get(llvm::Type::getInt8PtrTy(Ctx),
1041                                            FunctionNames.size());
1042     auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
1043     // This variable will *NOT* be emitted to the object file. It is used
1044     // to pass the list of names referenced to codegen.
1045     new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
1046                              llvm::GlobalValue::InternalLinkage, NamesArrVal,
1047                              llvm::getCoverageUnusedNamesVarName());
1048   }
1049 }
1050 
1051 unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
1052   auto It = FileEntries.find(File);
1053   if (It != FileEntries.end())
1054     return It->second;
1055   unsigned FileID = FileEntries.size();
1056   FileEntries.insert(std::make_pair(File, FileID));
1057   return FileID;
1058 }
1059 
1060 void CoverageMappingGen::emitCounterMapping(const Decl *D,
1061                                             llvm::raw_ostream &OS) {
1062   assert(CounterMap);
1063   CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
1064   Walker.VisitDecl(D);
1065   Walker.write(OS);
1066 }
1067 
1068 void CoverageMappingGen::emitEmptyMapping(const Decl *D,
1069                                           llvm::raw_ostream &OS) {
1070   EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
1071   Walker.VisitDecl(D);
1072   Walker.write(OS);
1073 }
1074