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