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