blob: a1023473bdd33daeea9140f178a6d39722e7513f [file] [log] [blame]
Alex Lorenzee024992014-08-04 18:41:51 +00001//===--- 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"
Vedant Kumarbc6b80a2016-01-28 17:52:18 +000018#include "llvm/ADT/SmallSet.h"
Vedant Kumarca3326c2016-01-21 19:25:35 +000019#include "llvm/ADT/StringExtras.h"
Justin Bognerbf42cfd2015-02-18 21:24:51 +000020#include "llvm/ADT/Optional.h"
Easwaran Ramanb014ee42016-04-29 18:53:16 +000021#include "llvm/ProfileData/Coverage/CoverageMapping.h"
22#include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
23#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000024#include "llvm/ProfileData/InstrProfReader.h"
Alex Lorenzee024992014-08-04 18:41:51 +000025#include "llvm/Support/FileSystem.h"
Vedant Kumar14f8fb62016-07-18 21:01:27 +000026#include "llvm/Support/Path.h"
Alex Lorenzee024992014-08-04 18:41:51 +000027
28using namespace clang;
29using namespace CodeGen;
30using namespace llvm::coverage;
31
32void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range) {
33 SkippedRanges.push_back(Range);
34}
35
36namespace {
37
38/// \brief A region of source code that can be mapped to a counter.
Justin Bogner09c71792014-10-01 03:33:49 +000039class SourceMappingRegion {
Alex Lorenzee024992014-08-04 18:41:51 +000040 Counter Count;
41
Alex Lorenzee024992014-08-04 18:41:51 +000042 /// \brief The region's starting location.
Justin Bognerbf42cfd2015-02-18 21:24:51 +000043 Optional<SourceLocation> LocStart;
Alex Lorenzee024992014-08-04 18:41:51 +000044
45 /// \brief The region's ending location.
Justin Bognerbf42cfd2015-02-18 21:24:51 +000046 Optional<SourceLocation> LocEnd;
Alex Lorenzee024992014-08-04 18:41:51 +000047
Justin Bogner09c71792014-10-01 03:33:49 +000048public:
Justin Bognerbf42cfd2015-02-18 21:24:51 +000049 SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart,
50 Optional<SourceLocation> LocEnd)
51 : Count(Count), LocStart(LocStart), LocEnd(LocEnd) {}
Alex Lorenzee024992014-08-04 18:41:51 +000052
Justin Bogner09c71792014-10-01 03:33:49 +000053 const Counter &getCounter() const { return Count; }
54
Justin Bognerbf42cfd2015-02-18 21:24:51 +000055 void setCounter(Counter C) { Count = C; }
Justin Bogner09c71792014-10-01 03:33:49 +000056
Justin Bognerbf42cfd2015-02-18 21:24:51 +000057 bool hasStartLoc() const { return LocStart.hasValue(); }
58
59 void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
60
Craig Topper462c77b2015-09-26 05:10:14 +000061 SourceLocation getStartLoc() const {
Justin Bognerbf42cfd2015-02-18 21:24:51 +000062 assert(LocStart && "Region has no start location");
63 return *LocStart;
Justin Bogner09c71792014-10-01 03:33:49 +000064 }
65
Justin Bognerbf42cfd2015-02-18 21:24:51 +000066 bool hasEndLoc() const { return LocEnd.hasValue(); }
Alex Lorenzee024992014-08-04 18:41:51 +000067
Justin Bognerbf42cfd2015-02-18 21:24:51 +000068 void setEndLoc(SourceLocation Loc) { LocEnd = Loc; }
Alex Lorenzee024992014-08-04 18:41:51 +000069
Craig Topper462c77b2015-09-26 05:10:14 +000070 SourceLocation getEndLoc() const {
Justin Bognerbf42cfd2015-02-18 21:24:51 +000071 assert(LocEnd && "Region has no end location");
72 return *LocEnd;
Alex Lorenzee024992014-08-04 18:41:51 +000073 }
74};
75
Alex Lorenzee024992014-08-04 18:41:51 +000076/// \brief Provides the common functionality for the different
77/// coverage mapping region builders.
78class CoverageMappingBuilder {
79public:
80 CoverageMappingModuleGen &CVM;
81 SourceManager &SM;
82 const LangOptions &LangOpts;
83
84private:
Justin Bognerbf42cfd2015-02-18 21:24:51 +000085 /// \brief Map of clang's FileIDs to IDs used for coverage mapping.
86 llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
87 FileIDMapping;
Alex Lorenzee024992014-08-04 18:41:51 +000088
89public:
Alex Lorenzee024992014-08-04 18:41:51 +000090 /// \brief The coverage mapping regions for this function
91 llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
92 /// \brief The source mapping regions for this function.
Justin Bognerf59329b2014-10-01 03:33:52 +000093 std::vector<SourceMappingRegion> SourceRegions;
Alex Lorenzee024992014-08-04 18:41:51 +000094
Igor Kudrinfc05ee32016-08-31 07:04:16 +000095 /// \brief A set of regions which can be used as a filter.
96 ///
97 /// It is produced by emitExpansionRegions() and is used in
98 /// emitSourceRegions() to suppress producing code regions if
99 /// the same area is covered by expansion regions.
100 typedef llvm::SmallSet<std::pair<SourceLocation, SourceLocation>, 8>
101 SourceRegionFilter;
102
Alex Lorenzee024992014-08-04 18:41:51 +0000103 CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
104 const LangOptions &LangOpts)
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000105 : CVM(CVM), SM(SM), LangOpts(LangOpts) {}
Alex Lorenzee024992014-08-04 18:41:51 +0000106
107 /// \brief Return the precise end location for the given token.
108 SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000109 // We avoid getLocForEndOfToken here, because it doesn't do what we want for
110 // macro locations, which we just treat as expanded files.
111 unsigned TokLen =
112 Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
113 return Loc.getLocWithOffset(TokLen);
Alex Lorenzee024992014-08-04 18:41:51 +0000114 }
115
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000116 /// \brief Return the start location of an included file or expanded macro.
117 SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
118 if (Loc.isMacroID())
119 return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
120 return SM.getLocForStartOfFile(SM.getFileID(Loc));
Alex Lorenzee024992014-08-04 18:41:51 +0000121 }
122
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000123 /// \brief Return the end location of an included file or expanded macro.
124 SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
125 if (Loc.isMacroID())
126 return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
Justin Bognerf14b2072015-03-25 04:13:49 +0000127 SM.getFileOffset(Loc));
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000128 return SM.getLocForEndOfFile(SM.getFileID(Loc));
129 }
130
131 /// \brief Find out where the current file is included or macro is expanded.
132 SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
133 return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).first
134 : SM.getIncludeLoc(SM.getFileID(Loc));
135 }
136
Justin Bogner682bfbf2015-05-14 22:14:10 +0000137 /// \brief Return true if \c Loc is a location in a built-in macro.
138 bool isInBuiltin(SourceLocation Loc) {
Mehdi Amini99d1b292016-10-01 16:38:28 +0000139 return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>";
Justin Bogner682bfbf2015-05-14 22:14:10 +0000140 }
141
Igor Kudrind9e1a612016-06-07 10:07:51 +0000142 /// \brief Check whether \c Loc is included or expanded from \c Parent.
143 bool isNestedIn(SourceLocation Loc, FileID Parent) {
144 do {
145 Loc = getIncludeOrExpansionLoc(Loc);
146 if (Loc.isInvalid())
147 return false;
148 } while (!SM.isInFileID(Loc, Parent));
149 return true;
150 }
151
Justin Bogner682bfbf2015-05-14 22:14:10 +0000152 /// \brief Get the start of \c S ignoring macro arguments and builtin macros.
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000153 SourceLocation getStart(const Stmt *S) {
154 SourceLocation Loc = S->getLocStart();
Justin Bogner682bfbf2015-05-14 22:14:10 +0000155 while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000156 Loc = SM.getImmediateExpansionRange(Loc).first;
157 return Loc;
158 }
159
Justin Bogner682bfbf2015-05-14 22:14:10 +0000160 /// \brief Get the end of \c S ignoring macro arguments and builtin macros.
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000161 SourceLocation getEnd(const Stmt *S) {
162 SourceLocation Loc = S->getLocEnd();
Justin Bogner682bfbf2015-05-14 22:14:10 +0000163 while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000164 Loc = SM.getImmediateExpansionRange(Loc).first;
Justin Bognerf14b2072015-03-25 04:13:49 +0000165 return getPreciseTokenLocEnd(Loc);
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000166 }
167
168 /// \brief Find the set of files we have regions for and assign IDs
169 ///
170 /// Fills \c Mapping with the virtual file mapping needed to write out
171 /// coverage and collects the necessary file information to emit source and
172 /// expansion regions.
173 void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
174 FileIDMapping.clear();
175
Vedant Kumarbc6b80a2016-01-28 17:52:18 +0000176 llvm::SmallSet<FileID, 8> Visited;
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000177 SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
178 for (const auto &Region : SourceRegions) {
179 SourceLocation Loc = Region.getStartLoc();
180 FileID File = SM.getFileID(Loc);
Vedant Kumarbc6b80a2016-01-28 17:52:18 +0000181 if (!Visited.insert(File).second)
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000182 continue;
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000183
Vedant Kumar93205af2016-07-11 22:57:46 +0000184 // Do not map FileID's associated with system headers.
185 if (SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
186 continue;
187
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000188 unsigned Depth = 0;
189 for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
Yaron Kerened1fe5d2015-10-03 05:15:57 +0000190 Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent))
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000191 ++Depth;
192 FileLocs.push_back(std::make_pair(Loc, Depth));
193 }
194 std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second());
195
196 for (const auto &FL : FileLocs) {
197 SourceLocation Loc = FL.first;
198 FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
199 auto Entry = SM.getFileEntryForID(SpellingFile);
200 if (!Entry)
201 continue;
202
203 FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
204 Mapping.push_back(CVM.getFileID(Entry));
205 }
206 }
207
208 /// \brief Get the coverage mapping file ID for \c Loc.
209 ///
210 /// If such file id doesn't exist, return None.
211 Optional<unsigned> getCoverageFileID(SourceLocation Loc) {
212 auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
Justin Bogner903678c2015-01-24 20:22:32 +0000213 if (Mapping != FileIDMapping.end())
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000214 return Mapping->second.first;
215 return None;
Alex Lorenzee024992014-08-04 18:41:51 +0000216 }
217
Alex Lorenzee024992014-08-04 18:41:51 +0000218 /// \brief Gather all the regions that were skipped by the preprocessor
219 /// using the constructs like #if.
220 void gatherSkippedRegions() {
221 /// An array of the minimum lineStarts and the maximum lineEnds
222 /// for mapping regions from the appropriate source files.
223 llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
224 FileLineRanges.resize(
225 FileIDMapping.size(),
226 std::make_pair(std::numeric_limits<unsigned>::max(), 0));
227 for (const auto &R : MappingRegions) {
228 FileLineRanges[R.FileID].first =
229 std::min(FileLineRanges[R.FileID].first, R.LineStart);
230 FileLineRanges[R.FileID].second =
231 std::max(FileLineRanges[R.FileID].second, R.LineEnd);
232 }
233
234 auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
235 for (const auto &I : SkippedRanges) {
236 auto LocStart = I.getBegin();
237 auto LocEnd = I.getEnd();
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000238 assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
239 "region spans multiple files");
Alex Lorenzee024992014-08-04 18:41:51 +0000240
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000241 auto CovFileID = getCoverageFileID(LocStart);
Justin Bogner903678c2015-01-24 20:22:32 +0000242 if (!CovFileID)
Alex Lorenzee024992014-08-04 18:41:51 +0000243 continue;
244 unsigned LineStart = SM.getSpellingLineNumber(LocStart);
245 unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
246 unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
247 unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
Justin Bognerfd34280b2015-02-03 23:59:48 +0000248 auto Region = CounterMappingRegion::makeSkipped(
249 *CovFileID, LineStart, ColumnStart, LineEnd, ColumnEnd);
Alex Lorenzee024992014-08-04 18:41:51 +0000250 // Make sure that we only collect the regions that are inside
251 // the souce code of this function.
Justin Bogner903678c2015-01-24 20:22:32 +0000252 if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
253 Region.LineEnd <= FileLineRanges[*CovFileID].second)
Alex Lorenzee024992014-08-04 18:41:51 +0000254 MappingRegions.push_back(Region);
255 }
256 }
257
Alex Lorenzee024992014-08-04 18:41:51 +0000258 /// \brief Generate the coverage counter mapping regions from collected
259 /// source regions.
Igor Kudrinfc05ee32016-08-31 07:04:16 +0000260 void emitSourceRegions(const SourceRegionFilter &Filter) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000261 for (const auto &Region : SourceRegions) {
262 assert(Region.hasEndLoc() && "incomplete region");
Alex Lorenzee024992014-08-04 18:41:51 +0000263
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000264 SourceLocation LocStart = Region.getStartLoc();
Yaron Keren8b563662015-10-03 10:46:20 +0000265 assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
Justin Bognerf59329b2014-10-01 03:33:52 +0000266
Vedant Kumar93205af2016-07-11 22:57:46 +0000267 // Ignore regions from system headers.
268 if (SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
269 continue;
270
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000271 auto CovFileID = getCoverageFileID(LocStart);
272 // Ignore regions that don't have a file, such as builtin macros.
273 if (!CovFileID)
Alex Lorenzee024992014-08-04 18:41:51 +0000274 continue;
275
Justin Bognerf14b2072015-03-25 04:13:49 +0000276 SourceLocation LocEnd = Region.getEndLoc();
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000277 assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
278 "region spans multiple files");
279
Igor Kudrinfc05ee32016-08-31 07:04:16 +0000280 // Don't add code regions for the area covered by expansion regions.
281 // This not only suppresses redundant regions, but sometimes prevents
282 // creating regions with wrong counters if, for example, a statement's
283 // body ends at the end of a nested macro.
284 if (Filter.count(std::make_pair(LocStart, LocEnd)))
285 continue;
286
Justin Bognerf59329b2014-10-01 03:33:52 +0000287 // Find the spilling locations for the mapping region.
Alex Lorenzee024992014-08-04 18:41:51 +0000288 unsigned LineStart = SM.getSpellingLineNumber(LocStart);
289 unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
290 unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
291 unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
292
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000293 assert(LineStart <= LineEnd && "region start and end out of order");
294 MappingRegions.push_back(CounterMappingRegion::makeRegion(
295 Region.getCounter(), *CovFileID, LineStart, ColumnStart, LineEnd,
296 ColumnEnd));
297 }
298 }
299
300 /// \brief Generate expansion regions for each virtual file we've seen.
Igor Kudrinfc05ee32016-08-31 07:04:16 +0000301 SourceRegionFilter emitExpansionRegions() {
302 SourceRegionFilter Filter;
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000303 for (const auto &FM : FileIDMapping) {
304 SourceLocation ExpandedLoc = FM.second.second;
305 SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
306 if (ParentLoc.isInvalid())
Alex Lorenzee024992014-08-04 18:41:51 +0000307 continue;
308
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000309 auto ParentFileID = getCoverageFileID(ParentLoc);
310 if (!ParentFileID)
311 continue;
312 auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
313 assert(ExpandedFileID && "expansion in uncovered file");
314
315 SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
316 assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
317 "region spans multiple files");
Igor Kudrinfc05ee32016-08-31 07:04:16 +0000318 Filter.insert(std::make_pair(ParentLoc, LocEnd));
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000319
320 unsigned LineStart = SM.getSpellingLineNumber(ParentLoc);
321 unsigned ColumnStart = SM.getSpellingColumnNumber(ParentLoc);
322 unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
323 unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
324
325 MappingRegions.push_back(CounterMappingRegion::makeExpansion(
326 *ParentFileID, *ExpandedFileID, LineStart, ColumnStart, LineEnd,
Justin Bognerfd34280b2015-02-03 23:59:48 +0000327 ColumnEnd));
Alex Lorenzee024992014-08-04 18:41:51 +0000328 }
Igor Kudrinfc05ee32016-08-31 07:04:16 +0000329 return Filter;
Alex Lorenzee024992014-08-04 18:41:51 +0000330 }
331};
332
333/// \brief Creates unreachable coverage regions for the functions that
334/// are not emitted.
335struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
336 EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
337 const LangOptions &LangOpts)
338 : CoverageMappingBuilder(CVM, SM, LangOpts) {}
339
340 void VisitDecl(const Decl *D) {
341 if (!D->hasBody())
342 return;
343 auto Body = D->getBody();
Igor Kudrind9e1a612016-06-07 10:07:51 +0000344 SourceLocation Start = getStart(Body);
345 SourceLocation End = getEnd(Body);
346 if (!SM.isWrittenInSameFile(Start, End)) {
347 // Walk up to find the common ancestor.
348 // Correct the locations accordingly.
349 FileID StartFileID = SM.getFileID(Start);
350 FileID EndFileID = SM.getFileID(End);
351 while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) {
352 Start = getIncludeOrExpansionLoc(Start);
353 assert(Start.isValid() &&
354 "Declaration start location not nested within a known region");
355 StartFileID = SM.getFileID(Start);
356 }
357 while (StartFileID != EndFileID) {
358 End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
359 assert(End.isValid() &&
360 "Declaration end location not nested within a known region");
361 EndFileID = SM.getFileID(End);
362 }
363 }
364 SourceRegions.emplace_back(Counter(), Start, End);
Alex Lorenzee024992014-08-04 18:41:51 +0000365 }
366
367 /// \brief Write the mapping data to the output stream
368 void write(llvm::raw_ostream &OS) {
Alex Lorenzee024992014-08-04 18:41:51 +0000369 SmallVector<unsigned, 16> FileIDMapping;
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000370 gatherFileIDs(FileIDMapping);
Igor Kudrinfc05ee32016-08-31 07:04:16 +0000371 emitSourceRegions(SourceRegionFilter());
Alex Lorenzee024992014-08-04 18:41:51 +0000372
Vedant Kumarefd319a2016-07-26 00:24:59 +0000373 if (MappingRegions.empty())
374 return;
375
Craig Topper5fc8fc22014-08-27 06:28:36 +0000376 CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions);
Alex Lorenzee024992014-08-04 18:41:51 +0000377 Writer.write(OS);
378 }
379};
380
381/// \brief A StmtVisitor that creates coverage mapping regions which map
382/// from the source code locations to the PGO counters.
383struct CounterCoverageMappingBuilder
384 : public CoverageMappingBuilder,
385 public ConstStmtVisitor<CounterCoverageMappingBuilder> {
386 /// \brief The map of statements to count values.
387 llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
388
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000389 /// \brief A stack of currently live regions.
390 std::vector<SourceMappingRegion> RegionStack;
Alex Lorenzee024992014-08-04 18:41:51 +0000391
392 CounterExpressionBuilder Builder;
393
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000394 /// \brief A location in the most recently visited file or macro.
395 ///
396 /// This is used to adjust the active source regions appropriately when
397 /// expressions cross file or macro boundaries.
398 SourceLocation MostRecentLocation;
399
400 /// \brief Return a counter for the subtraction of \c RHS from \c LHS
Alex Lorenzee024992014-08-04 18:41:51 +0000401 Counter subtractCounters(Counter LHS, Counter RHS) {
402 return Builder.subtract(LHS, RHS);
403 }
404
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000405 /// \brief Return a counter for the sum of \c LHS and \c RHS.
Alex Lorenzee024992014-08-04 18:41:51 +0000406 Counter addCounters(Counter LHS, Counter RHS) {
407 return Builder.add(LHS, RHS);
408 }
409
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000410 Counter addCounters(Counter C1, Counter C2, Counter C3) {
411 return addCounters(addCounters(C1, C2), C3);
412 }
413
Alex Lorenzee024992014-08-04 18:41:51 +0000414 /// \brief Return the region counter for the given statement.
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000415 ///
Alex Lorenzee024992014-08-04 18:41:51 +0000416 /// This should only be called on statements that have a dedicated counter.
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000417 Counter getRegionCounter(const Stmt *S) {
418 return Counter::getCounter(CounterMap[S]);
Alex Lorenzee024992014-08-04 18:41:51 +0000419 }
420
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000421 /// \brief Push a region onto the stack.
422 ///
423 /// Returns the index on the stack where the region was pushed. This can be
424 /// used with popRegions to exit a "scope", ending the region that was pushed.
425 size_t pushRegion(Counter Count, Optional<SourceLocation> StartLoc = None,
426 Optional<SourceLocation> EndLoc = None) {
427 if (StartLoc)
428 MostRecentLocation = *StartLoc;
429 RegionStack.emplace_back(Count, StartLoc, EndLoc);
Alex Lorenzee024992014-08-04 18:41:51 +0000430
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000431 return RegionStack.size() - 1;
Alex Lorenzee024992014-08-04 18:41:51 +0000432 }
433
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000434 /// \brief Pop regions from the stack into the function's list of regions.
435 ///
436 /// Adds all regions from \c ParentIndex to the top of the stack to the
437 /// function's \c SourceRegions.
438 void popRegions(size_t ParentIndex) {
439 assert(RegionStack.size() >= ParentIndex && "parent not in stack");
440 while (RegionStack.size() > ParentIndex) {
441 SourceMappingRegion &Region = RegionStack.back();
442 if (Region.hasStartLoc()) {
443 SourceLocation StartLoc = Region.getStartLoc();
444 SourceLocation EndLoc = Region.hasEndLoc()
445 ? Region.getEndLoc()
446 : RegionStack[ParentIndex].getEndLoc();
447 while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
448 // The region ends in a nested file or macro expansion. Create a
449 // separate region for each expansion.
450 SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
451 assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
452
Igor Kudrin8545dae2016-08-29 11:48:50 +0000453 if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
454 SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000455
Justin Bognerf14b2072015-03-25 04:13:49 +0000456 EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
Justin Bognerdceaaad2015-07-17 23:31:21 +0000457 if (EndLoc.isInvalid())
458 llvm::report_fatal_error("File exit not handled before popRegions");
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000459 }
460 Region.setEndLoc(EndLoc);
461
462 MostRecentLocation = EndLoc;
463 // If this region happens to span an entire expansion, we need to make
464 // sure we don't overlap the parent region with it.
465 if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
466 EndLoc == getEndOfFileOrMacro(EndLoc))
467 MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
468
469 assert(SM.isWrittenInSameFile(Region.getStartLoc(), EndLoc));
Craig Topperf36a5c42015-09-26 05:10:16 +0000470 SourceRegions.push_back(Region);
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000471 }
472 RegionStack.pop_back();
473 }
Alex Lorenzee024992014-08-04 18:41:51 +0000474 }
475
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000476 /// \brief Return the currently active region.
477 SourceMappingRegion &getRegion() {
478 assert(!RegionStack.empty() && "statement has no region");
479 return RegionStack.back();
480 }
Alex Lorenzee024992014-08-04 18:41:51 +0000481
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000482 /// \brief Propagate counts through the children of \c S.
483 Counter propagateCounts(Counter TopCount, const Stmt *S) {
484 size_t Index = pushRegion(TopCount, getStart(S), getEnd(S));
485 Visit(S);
486 Counter ExitCount = getRegion().getCounter();
487 popRegions(Index);
Vedant Kumar39f01972016-02-08 19:25:45 +0000488
489 // The statement may be spanned by an expansion. Make sure we handle a file
490 // exit out of this expansion before moving to the next statement.
491 if (SM.isBeforeInTranslationUnit(getStart(S), S->getLocStart()))
492 MostRecentLocation = getEnd(S);
493
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000494 return ExitCount;
495 }
Alex Lorenzee024992014-08-04 18:41:51 +0000496
Igor Kudrin0a7c9d12016-05-04 15:38:26 +0000497 /// \brief Check whether a region with bounds \c StartLoc and \c EndLoc
498 /// is already added to \c SourceRegions.
499 bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc) {
500 return SourceRegions.rend() !=
501 std::find_if(SourceRegions.rbegin(), SourceRegions.rend(),
502 [&](const SourceMappingRegion &Region) {
503 return Region.getStartLoc() == StartLoc &&
504 Region.getEndLoc() == EndLoc;
505 });
506 }
507
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000508 /// \brief Adjust the most recently visited location to \c EndLoc.
509 ///
510 /// This should be used after visiting any statements in non-source order.
511 void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
512 MostRecentLocation = EndLoc;
Igor Kudrin0a7c9d12016-05-04 15:38:26 +0000513 // The code region for a whole macro is created in handleFileExit() when
514 // it detects exiting of the virtual file of that macro. If we visited
515 // statements in non-source order, we might already have such a region
516 // added, for example, if a body of a loop is divided among multiple
517 // macros. Avoid adding duplicate regions in such case.
Justin Bogner96ae73f2015-05-01 19:23:34 +0000518 if (getRegion().hasEndLoc() &&
Igor Kudrin0a7c9d12016-05-04 15:38:26 +0000519 MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) &&
520 isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
521 MostRecentLocation))
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000522 MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
523 }
Alex Lorenzee024992014-08-04 18:41:51 +0000524
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000525 /// \brief Adjust regions and state when \c NewLoc exits a file.
526 ///
527 /// If moving from our most recently tracked location to \c NewLoc exits any
528 /// files, this adjusts our current region stack and creates the file regions
529 /// for the exited file.
530 void handleFileExit(SourceLocation NewLoc) {
Justin Bognere44dd6d2015-06-23 20:29:09 +0000531 if (NewLoc.isInvalid() ||
532 SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000533 return;
534
535 // If NewLoc is not in a file that contains MostRecentLocation, walk up to
536 // find the common ancestor.
537 SourceLocation LCA = NewLoc;
538 FileID ParentFile = SM.getFileID(LCA);
539 while (!isNestedIn(MostRecentLocation, ParentFile)) {
540 LCA = getIncludeOrExpansionLoc(LCA);
541 if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
542 // Since there isn't a common ancestor, no file was exited. We just need
543 // to adjust our location to the new file.
544 MostRecentLocation = NewLoc;
545 return;
546 }
547 ParentFile = SM.getFileID(LCA);
Alex Lorenzee024992014-08-04 18:41:51 +0000548 }
549
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000550 llvm::SmallSet<SourceLocation, 8> StartLocs;
551 Optional<Counter> ParentCounter;
Pete Cooper57d3f142015-07-30 17:22:52 +0000552 for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
553 if (!I.hasStartLoc())
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000554 continue;
Pete Cooper57d3f142015-07-30 17:22:52 +0000555 SourceLocation Loc = I.getStartLoc();
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000556 if (!isNestedIn(Loc, ParentFile)) {
Pete Cooper57d3f142015-07-30 17:22:52 +0000557 ParentCounter = I.getCounter();
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000558 break;
559 }
Alex Lorenzee024992014-08-04 18:41:51 +0000560
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000561 while (!SM.isInFileID(Loc, ParentFile)) {
562 // The most nested region for each start location is the one with the
563 // correct count. We avoid creating redundant regions by stopping once
564 // we've seen this region.
565 if (StartLocs.insert(Loc).second)
Pete Cooper57d3f142015-07-30 17:22:52 +0000566 SourceRegions.emplace_back(I.getCounter(), Loc,
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000567 getEndOfFileOrMacro(Loc));
568 Loc = getIncludeOrExpansionLoc(Loc);
569 }
Pete Cooper57d3f142015-07-30 17:22:52 +0000570 I.setStartLoc(getPreciseTokenLocEnd(Loc));
Alex Lorenzee024992014-08-04 18:41:51 +0000571 }
572
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000573 if (ParentCounter) {
574 // If the file is contained completely by another region and doesn't
575 // immediately start its own region, the whole file gets a region
576 // corresponding to the parent.
577 SourceLocation Loc = MostRecentLocation;
578 while (isNestedIn(Loc, ParentFile)) {
579 SourceLocation FileStart = getStartOfFileOrMacro(Loc);
580 if (StartLocs.insert(FileStart).second)
581 SourceRegions.emplace_back(*ParentCounter, FileStart,
582 getEndOfFileOrMacro(Loc));
583 Loc = getIncludeOrExpansionLoc(Loc);
584 }
Alex Lorenzee024992014-08-04 18:41:51 +0000585 }
586
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000587 MostRecentLocation = NewLoc;
588 }
Alex Lorenzee024992014-08-04 18:41:51 +0000589
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000590 /// \brief Ensure that \c S is included in the current region.
591 void extendRegion(const Stmt *S) {
592 SourceMappingRegion &Region = getRegion();
593 SourceLocation StartLoc = getStart(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000594
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000595 handleFileExit(StartLoc);
596 if (!Region.hasStartLoc())
597 Region.setStartLoc(StartLoc);
598 }
599
600 /// \brief Mark \c S as a terminator, starting a zero region.
601 void terminateRegion(const Stmt *S) {
602 extendRegion(S);
603 SourceMappingRegion &Region = getRegion();
604 if (!Region.hasEndLoc())
605 Region.setEndLoc(getEnd(S));
606 pushRegion(Counter::getZero());
607 }
Alex Lorenzee024992014-08-04 18:41:51 +0000608
609 /// \brief Keep counts of breaks and continues inside loops.
610 struct BreakContinue {
611 Counter BreakCount;
612 Counter ContinueCount;
613 };
614 SmallVector<BreakContinue, 8> BreakContinueStack;
615
616 CounterCoverageMappingBuilder(
617 CoverageMappingModuleGen &CVM,
Justin Bognere5ee6c52014-10-02 16:44:01 +0000618 llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
Alex Lorenzee024992014-08-04 18:41:51 +0000619 const LangOptions &LangOpts)
Justin Bognere5ee6c52014-10-02 16:44:01 +0000620 : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap) {}
Alex Lorenzee024992014-08-04 18:41:51 +0000621
622 /// \brief Write the mapping data to the output stream
623 void write(llvm::raw_ostream &OS) {
Alex Lorenzee024992014-08-04 18:41:51 +0000624 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000625 gatherFileIDs(VirtualFileMapping);
Igor Kudrinfc05ee32016-08-31 07:04:16 +0000626 SourceRegionFilter Filter = emitExpansionRegions();
627 emitSourceRegions(Filter);
Alex Lorenzee024992014-08-04 18:41:51 +0000628 gatherSkippedRegions();
629
Vedant Kumarefd319a2016-07-26 00:24:59 +0000630 if (MappingRegions.empty())
631 return;
632
Justin Bogner4da909b2015-02-03 21:35:49 +0000633 CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
634 MappingRegions);
Alex Lorenzee024992014-08-04 18:41:51 +0000635 Writer.write(OS);
636 }
637
Alex Lorenzee024992014-08-04 18:41:51 +0000638 void VisitStmt(const Stmt *S) {
Yaron Kerened1fe5d2015-10-03 05:15:57 +0000639 if (S->getLocStart().isValid())
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000640 extendRegion(S);
Benjamin Kramer642f1732015-07-02 21:03:14 +0000641 for (const Stmt *Child : S->children())
642 if (Child)
643 this->Visit(Child);
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000644 handleFileExit(getEnd(S));
Alex Lorenzee024992014-08-04 18:41:51 +0000645 }
646
Alex Lorenzee024992014-08-04 18:41:51 +0000647 void VisitDecl(const Decl *D) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000648 Stmt *Body = D->getBody();
Vedant Kumarefd319a2016-07-26 00:24:59 +0000649
650 // Do not propagate region counts into system headers.
651 if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
652 return;
653
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000654 propagateCounts(getRegionCounter(Body), Body);
Alex Lorenzee024992014-08-04 18:41:51 +0000655 }
656
657 void VisitReturnStmt(const ReturnStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000658 extendRegion(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000659 if (S->getRetValue())
660 Visit(S->getRetValue());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000661 terminateRegion(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000662 }
663
Justin Bognerf959feb2015-04-28 06:31:55 +0000664 void VisitCXXThrowExpr(const CXXThrowExpr *E) {
665 extendRegion(E);
666 if (E->getSubExpr())
667 Visit(E->getSubExpr());
668 terminateRegion(E);
669 }
670
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000671 void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
Alex Lorenzee024992014-08-04 18:41:51 +0000672
673 void VisitLabelStmt(const LabelStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000674 SourceLocation Start = getStart(S);
675 // We can't extendRegion here or we risk overlapping with our new region.
676 handleFileExit(Start);
677 pushRegion(getRegionCounter(S), Start);
Alex Lorenzee024992014-08-04 18:41:51 +0000678 Visit(S->getSubStmt());
679 }
680
681 void VisitBreakStmt(const BreakStmt *S) {
Alex Lorenzee024992014-08-04 18:41:51 +0000682 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
683 BreakContinueStack.back().BreakCount = addCounters(
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000684 BreakContinueStack.back().BreakCount, getRegion().getCounter());
685 terminateRegion(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000686 }
687
688 void VisitContinueStmt(const ContinueStmt *S) {
Alex Lorenzee024992014-08-04 18:41:51 +0000689 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
690 BreakContinueStack.back().ContinueCount = addCounters(
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000691 BreakContinueStack.back().ContinueCount, getRegion().getCounter());
692 terminateRegion(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000693 }
694
695 void VisitWhileStmt(const WhileStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000696 extendRegion(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000697
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000698 Counter ParentCount = getRegion().getCounter();
699 Counter BodyCount = getRegionCounter(S);
700
701 // Handle the body first so that we can get the backedge count.
702 BreakContinueStack.push_back(BreakContinue());
703 extendRegion(S->getBody());
704 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
Alex Lorenzee024992014-08-04 18:41:51 +0000705 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000706
707 // Go back to handle the condition.
708 Counter CondCount =
709 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
710 propagateCounts(CondCount, S->getCond());
711 adjustForOutOfOrderTraversal(getEnd(S));
712
713 Counter OutCount =
714 addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
715 if (OutCount != ParentCount)
716 pushRegion(OutCount);
Alex Lorenzee024992014-08-04 18:41:51 +0000717 }
718
719 void VisitDoStmt(const DoStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000720 extendRegion(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000721
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000722 Counter ParentCount = getRegion().getCounter();
723 Counter BodyCount = getRegionCounter(S);
724
725 BreakContinueStack.push_back(BreakContinue());
726 extendRegion(S->getBody());
727 Counter BackedgeCount =
728 propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
Alex Lorenzee024992014-08-04 18:41:51 +0000729 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000730
731 Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
732 propagateCounts(CondCount, S->getCond());
733
734 Counter OutCount =
735 addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
736 if (OutCount != ParentCount)
737 pushRegion(OutCount);
Alex Lorenzee024992014-08-04 18:41:51 +0000738 }
739
740 void VisitForStmt(const ForStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000741 extendRegion(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000742 if (S->getInit())
743 Visit(S->getInit());
744
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000745 Counter ParentCount = getRegion().getCounter();
746 Counter BodyCount = getRegionCounter(S);
747
748 // Handle the body first so that we can get the backedge count.
Alex Lorenzee024992014-08-04 18:41:51 +0000749 BreakContinueStack.push_back(BreakContinue());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000750 extendRegion(S->getBody());
751 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
752 BreakContinue BC = BreakContinueStack.pop_back_val();
Alex Lorenzee024992014-08-04 18:41:51 +0000753
754 // The increment is essentially part of the body but it needs to include
755 // the count for all the continue statements.
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000756 if (const Stmt *Inc = S->getInc())
757 propagateCounts(addCounters(BackedgeCount, BC.ContinueCount), Inc);
758
759 // Go back to handle the condition.
760 Counter CondCount =
761 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
762 if (const Expr *Cond = S->getCond()) {
763 propagateCounts(CondCount, Cond);
764 adjustForOutOfOrderTraversal(getEnd(S));
Alex Lorenzee024992014-08-04 18:41:51 +0000765 }
766
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000767 Counter OutCount =
768 addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
769 if (OutCount != ParentCount)
770 pushRegion(OutCount);
Alex Lorenzee024992014-08-04 18:41:51 +0000771 }
772
773 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000774 extendRegion(S);
775 Visit(S->getLoopVarStmt());
Alex Lorenzee024992014-08-04 18:41:51 +0000776 Visit(S->getRangeStmt());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000777
778 Counter ParentCount = getRegion().getCounter();
779 Counter BodyCount = getRegionCounter(S);
780
Alex Lorenzee024992014-08-04 18:41:51 +0000781 BreakContinueStack.push_back(BreakContinue());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000782 extendRegion(S->getBody());
783 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
Alex Lorenzee024992014-08-04 18:41:51 +0000784 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000785
Justin Bogner15874322015-04-30 21:31:02 +0000786 Counter LoopCount =
787 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
788 Counter OutCount =
789 addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000790 if (OutCount != ParentCount)
791 pushRegion(OutCount);
Alex Lorenzee024992014-08-04 18:41:51 +0000792 }
793
794 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000795 extendRegion(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000796 Visit(S->getElement());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000797
798 Counter ParentCount = getRegion().getCounter();
799 Counter BodyCount = getRegionCounter(S);
800
Alex Lorenzee024992014-08-04 18:41:51 +0000801 BreakContinueStack.push_back(BreakContinue());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000802 extendRegion(S->getBody());
803 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
Alex Lorenzee024992014-08-04 18:41:51 +0000804 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000805
Justin Bogner15874322015-04-30 21:31:02 +0000806 Counter LoopCount =
807 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
808 Counter OutCount =
809 addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000810 if (OutCount != ParentCount)
811 pushRegion(OutCount);
Alex Lorenzee024992014-08-04 18:41:51 +0000812 }
813
814 void VisitSwitchStmt(const SwitchStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000815 extendRegion(S);
Vedant Kumarf2a6ec52016-10-14 23:38:13 +0000816 if (S->getInit())
817 Visit(S->getInit());
Alex Lorenzee024992014-08-04 18:41:51 +0000818 Visit(S->getCond());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000819
Alex Lorenzee024992014-08-04 18:41:51 +0000820 BreakContinueStack.push_back(BreakContinue());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000821
822 const Stmt *Body = S->getBody();
823 extendRegion(Body);
824 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
825 if (!CS->body_empty()) {
826 // The body of the switch needs a zero region so that fallthrough counts
827 // behave correctly, but it would be misleading to include the braces of
828 // the compound statement in the zeroed area, so we need to handle this
829 // specially.
830 size_t Index =
831 pushRegion(Counter::getZero(), getStart(CS->body_front()),
832 getEnd(CS->body_back()));
Richard Trieub5841332015-04-15 01:21:42 +0000833 for (const auto *Child : CS->children())
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000834 Visit(Child);
835 popRegions(Index);
Alex Lorenzee024992014-08-04 18:41:51 +0000836 }
Vedant Kumar87ea3b02016-05-31 20:35:12 +0000837 } else
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000838 propagateCounts(Counter::getZero(), Body);
Alex Lorenzee024992014-08-04 18:41:51 +0000839 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000840
Alex Lorenzee024992014-08-04 18:41:51 +0000841 if (!BreakContinueStack.empty())
842 BreakContinueStack.back().ContinueCount = addCounters(
843 BreakContinueStack.back().ContinueCount, BC.ContinueCount);
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000844
845 Counter ExitCount = getRegionCounter(S);
Vedant Kumar38364822016-05-31 18:06:19 +0000846 SourceLocation ExitLoc = getEnd(S);
Alex Lorenz08780522016-09-27 23:30:36 +0000847 pushRegion(ExitCount);
848
849 // Ensure that handleFileExit recognizes when the end location is located
850 // in a different file.
851 MostRecentLocation = getStart(S);
Vedant Kumar38364822016-05-31 18:06:19 +0000852 handleFileExit(ExitLoc);
Alex Lorenzee024992014-08-04 18:41:51 +0000853 }
854
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000855 void VisitSwitchCase(const SwitchCase *S) {
856 extendRegion(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000857
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000858 SourceMappingRegion &Parent = getRegion();
859
860 Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
861 // Reuse the existing region if it starts at our label. This is typical of
862 // the first case in a switch.
863 if (Parent.hasStartLoc() && Parent.getStartLoc() == getStart(S))
864 Parent.setCounter(Count);
865 else
866 pushRegion(Count, getStart(S));
867
Sanjay Patel376c06c2015-12-24 21:11:29 +0000868 if (const auto *CS = dyn_cast<CaseStmt>(S)) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000869 Visit(CS->getLHS());
870 if (const Expr *RHS = CS->getRHS())
871 Visit(RHS);
872 }
Alex Lorenzee024992014-08-04 18:41:51 +0000873 Visit(S->getSubStmt());
874 }
875
876 void VisitIfStmt(const IfStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000877 extendRegion(S);
Vedant Kumar9d2a16b2016-10-14 23:38:16 +0000878 if (S->getInit())
879 Visit(S->getInit());
880
Justin Bogner055ebc32015-06-16 06:24:15 +0000881 // Extend into the condition before we propagate through it below - this is
882 // needed to handle macros that generate the "if" but not the condition.
883 extendRegion(S->getCond());
Alex Lorenzee024992014-08-04 18:41:51 +0000884
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000885 Counter ParentCount = getRegion().getCounter();
886 Counter ThenCount = getRegionCounter(S);
Alex Lorenzee024992014-08-04 18:41:51 +0000887
Justin Bogner91f2e3c2015-02-19 03:10:30 +0000888 // Emitting a counter for the condition makes it easier to interpret the
889 // counter for the body when looking at the coverage.
890 propagateCounts(ParentCount, S->getCond());
891
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000892 extendRegion(S->getThen());
893 Counter OutCount = propagateCounts(ThenCount, S->getThen());
894
895 Counter ElseCount = subtractCounters(ParentCount, ThenCount);
896 if (const Stmt *Else = S->getElse()) {
897 extendRegion(S->getElse());
898 OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
899 } else
900 OutCount = addCounters(OutCount, ElseCount);
901
902 if (OutCount != ParentCount)
903 pushRegion(OutCount);
Alex Lorenzee024992014-08-04 18:41:51 +0000904 }
905
906 void VisitCXXTryStmt(const CXXTryStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000907 extendRegion(S);
Vedant Kumar049908b2016-06-22 19:57:58 +0000908 // Handle macros that generate the "try" but not the rest.
909 extendRegion(S->getTryBlock());
910
911 Counter ParentCount = getRegion().getCounter();
912 propagateCounts(ParentCount, S->getTryBlock());
913
Alex Lorenzee024992014-08-04 18:41:51 +0000914 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
915 Visit(S->getHandler(I));
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000916
917 Counter ExitCount = getRegionCounter(S);
918 pushRegion(ExitCount);
Alex Lorenzee024992014-08-04 18:41:51 +0000919 }
920
921 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000922 propagateCounts(getRegionCounter(S), S->getHandlerBlock());
Alex Lorenzee024992014-08-04 18:41:51 +0000923 }
924
925 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000926 extendRegion(E);
Alex Lorenzee024992014-08-04 18:41:51 +0000927
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000928 Counter ParentCount = getRegion().getCounter();
929 Counter TrueCount = getRegionCounter(E);
Alex Lorenzee024992014-08-04 18:41:51 +0000930
Justin Bognere3654ce2015-04-24 23:37:57 +0000931 Visit(E->getCond());
932
933 if (!isa<BinaryConditionalOperator>(E)) {
934 extendRegion(E->getTrueExpr());
935 propagateCounts(TrueCount, E->getTrueExpr());
936 }
937 extendRegion(E->getFalseExpr());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000938 propagateCounts(subtractCounters(ParentCount, TrueCount),
939 E->getFalseExpr());
Alex Lorenzee024992014-08-04 18:41:51 +0000940 }
941
942 void VisitBinLAnd(const BinaryOperator *E) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000943 extendRegion(E);
Alex Lorenzee024992014-08-04 18:41:51 +0000944 Visit(E->getLHS());
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000945
946 extendRegion(E->getRHS());
947 propagateCounts(getRegionCounter(E), E->getRHS());
Alex Lorenzee024992014-08-04 18:41:51 +0000948 }
949
950 void VisitBinLOr(const BinaryOperator *E) {
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000951 extendRegion(E);
Alex Lorenzee024992014-08-04 18:41:51 +0000952 Visit(E->getLHS());
Alex Lorenzee024992014-08-04 18:41:51 +0000953
Justin Bognerbf42cfd2015-02-18 21:24:51 +0000954 extendRegion(E->getRHS());
955 propagateCounts(getRegionCounter(E), E->getRHS());
Alex Lorenz01a0d062014-08-20 17:10:56 +0000956 }
Justin Bognerc1091022015-02-24 04:13:56 +0000957
958 void VisitLambdaExpr(const LambdaExpr *LE) {
959 // Lambdas are treated as their own functions for now, so we shouldn't
960 // propagate counts into them.
961 }
Alex Lorenzee024992014-08-04 18:41:51 +0000962};
Alex Lorenzee024992014-08-04 18:41:51 +0000963
Xinliang David Li1f39fcf2017-04-14 04:14:29 +0000964std::string getCoverageSection(const CodeGenModule &CGM) {
Vedant Kumar8a767a42017-04-15 00:10:05 +0000965 return llvm::getInstrProfSectionName(
966 llvm::IPSK_covmap,
967 CGM.getContext().getTargetInfo().getTriple().getObjectFormat());
Alex Lorenzee024992014-08-04 18:41:51 +0000968}
969
Vedant Kumar14f8fb62016-07-18 21:01:27 +0000970std::string normalizeFilename(StringRef Filename) {
971 llvm::SmallString<256> Path(Filename);
Vedant Kumar14f8fb62016-07-18 21:01:27 +0000972 llvm::sys::fs::make_absolute(Path);
Vedant Kumard04929d2016-07-18 22:32:02 +0000973 llvm::sys::path::remove_dots(Path, /*remove_dot_dots=*/true);
Vedant Kumar14f8fb62016-07-18 21:01:27 +0000974 return Path.str().str();
975}
976
977} // end anonymous namespace
978
Justin Bognera432d172015-02-03 00:20:24 +0000979static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
980 ArrayRef<CounterExpression> Expressions,
981 ArrayRef<CounterMappingRegion> Regions) {
982 OS << FunctionName << ":\n";
983 CounterMappingContext Ctx(Expressions);
984 for (const auto &R : Regions) {
Alex Lorenzf2cf38e2014-08-08 23:41:24 +0000985 OS.indent(2);
986 switch (R.Kind) {
987 case CounterMappingRegion::CodeRegion:
988 break;
989 case CounterMappingRegion::ExpansionRegion:
990 OS << "Expansion,";
991 break;
992 case CounterMappingRegion::SkippedRegion:
993 OS << "Skipped,";
994 break;
995 }
996
Justin Bogner4da909b2015-02-03 21:35:49 +0000997 OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
998 << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
Justin Bognerf69dc342015-01-23 23:46:13 +0000999 Ctx.dump(R.Count, OS);
Alex Lorenzf2cf38e2014-08-08 23:41:24 +00001000 if (R.Kind == CounterMappingRegion::ExpansionRegion)
Justin Bogner4da909b2015-02-03 21:35:49 +00001001 OS << " (Expanded file = " << R.ExpandedFileID << ")";
1002 OS << "\n";
Alex Lorenzf2cf38e2014-08-08 23:41:24 +00001003 }
1004}
1005
Alex Lorenzee024992014-08-04 18:41:51 +00001006void CoverageMappingModuleGen::addFunctionMappingRecord(
Xinliang David Li2129ae52016-01-07 20:05:55 +00001007 llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
Xinliang David Li848da132016-01-19 00:49:06 +00001008 const std::string &CoverageMapping, bool IsUsed) {
Alex Lorenzee024992014-08-04 18:41:51 +00001009 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
Alex Lorenzee024992014-08-04 18:41:51 +00001010 if (!FunctionRecordTy) {
Xinliang David Li2129ae52016-01-07 20:05:55 +00001011#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
Xinliang David Lia026a432015-11-05 05:46:39 +00001012 llvm::Type *FunctionRecordTypes[] = {
1013 #include "llvm/ProfileData/InstrProfData.inc"
1014 };
Alex Lorenzee024992014-08-04 18:41:51 +00001015 FunctionRecordTy =
Justin Bogner4dc5adc2015-07-02 20:47:25 +00001016 llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes),
1017 /*isPacked=*/true);
Alex Lorenzee024992014-08-04 18:41:51 +00001018 }
1019
Xinliang David Lia026a432015-11-05 05:46:39 +00001020 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
Alex Lorenzee024992014-08-04 18:41:51 +00001021 llvm::Constant *FunctionRecordVals[] = {
Xinliang David Lia026a432015-11-05 05:46:39 +00001022 #include "llvm/ProfileData/InstrProfData.inc"
1023 };
Alex Lorenzee024992014-08-04 18:41:51 +00001024 FunctionRecords.push_back(llvm::ConstantStruct::get(
1025 FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
Xinliang David Li848da132016-01-19 00:49:06 +00001026 if (!IsUsed)
Xinliang David Li2129ae52016-01-07 20:05:55 +00001027 FunctionNames.push_back(
1028 llvm::ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx)));
Vedant Kumarca3326c2016-01-21 19:25:35 +00001029 CoverageMappings.push_back(CoverageMapping);
Alex Lorenzf2cf38e2014-08-08 23:41:24 +00001030
1031 if (CGM.getCodeGenOpts().DumpCoverageMapping) {
1032 // Dump the coverage mapping data for this function by decoding the
1033 // encoded data. This allows us to dump the mapping regions which were
1034 // also processed by the CoverageMappingWriter which performs
1035 // additional minimization operations such as reducing the number of
1036 // expressions.
1037 std::vector<StringRef> Filenames;
1038 std::vector<CounterExpression> Expressions;
1039 std::vector<CounterMappingRegion> Regions;
Jordan Roseb31ee812016-11-07 17:28:04 +00001040 llvm::SmallVector<std::string, 16> FilenameStrs;
Alex Lorenzf2cf38e2014-08-08 23:41:24 +00001041 llvm::SmallVector<StringRef, 16> FilenameRefs;
Jordan Roseb31ee812016-11-07 17:28:04 +00001042 FilenameStrs.resize(FileEntries.size());
Alex Lorenzf2cf38e2014-08-08 23:41:24 +00001043 FilenameRefs.resize(FileEntries.size());
Jordan Roseb31ee812016-11-07 17:28:04 +00001044 for (const auto &Entry : FileEntries) {
1045 auto I = Entry.second;
1046 FilenameStrs[I] = normalizeFilename(Entry.first->getName());
1047 FilenameRefs[I] = FilenameStrs[I];
1048 }
Justin Bognera432d172015-02-03 00:20:24 +00001049 RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
1050 Expressions, Regions);
1051 if (Reader.read())
Alex Lorenzf2cf38e2014-08-08 23:41:24 +00001052 return;
Xinliang David Lia026a432015-11-05 05:46:39 +00001053 dump(llvm::outs(), NameValue, Expressions, Regions);
Alex Lorenzf2cf38e2014-08-08 23:41:24 +00001054 }
Alex Lorenzee024992014-08-04 18:41:51 +00001055}
1056
1057void CoverageMappingModuleGen::emit() {
1058 if (FunctionRecords.empty())
1059 return;
1060 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1061 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
1062
1063 // Create the filenames and merge them with coverage mappings
1064 llvm::SmallVector<std::string, 16> FilenameStrs;
Vedant Kumar9e324dd2016-06-29 05:33:09 +00001065 llvm::SmallVector<StringRef, 16> FilenameRefs;
Alex Lorenzee024992014-08-04 18:41:51 +00001066 FilenameStrs.resize(FileEntries.size());
Vedant Kumar9e324dd2016-06-29 05:33:09 +00001067 FilenameRefs.resize(FileEntries.size());
Alex Lorenzee024992014-08-04 18:41:51 +00001068 for (const auto &Entry : FileEntries) {
Alex Lorenzee024992014-08-04 18:41:51 +00001069 auto I = Entry.second;
Vedant Kumar14f8fb62016-07-18 21:01:27 +00001070 FilenameStrs[I] = normalizeFilename(Entry.first->getName());
Vedant Kumar9e324dd2016-06-29 05:33:09 +00001071 FilenameRefs[I] = FilenameStrs[I];
Alex Lorenzee024992014-08-04 18:41:51 +00001072 }
1073
Vedant Kumar9e324dd2016-06-29 05:33:09 +00001074 std::string FilenamesAndCoverageMappings;
1075 llvm::raw_string_ostream OS(FilenamesAndCoverageMappings);
1076 CoverageFilenamesSectionWriter(FilenameRefs).write(OS);
1077 std::string RawCoverageMappings =
1078 llvm::join(CoverageMappings.begin(), CoverageMappings.end(), "");
1079 OS << RawCoverageMappings;
1080 size_t CoverageMappingSize = RawCoverageMappings.size();
1081 size_t FilenamesSize = OS.str().size() - CoverageMappingSize;
1082 // Append extra zeroes if necessary to ensure that the size of the filenames
1083 // and coverage mappings is a multiple of 8.
1084 if (size_t Rem = OS.str().size() % 8) {
1085 CoverageMappingSize += 8 - Rem;
1086 for (size_t I = 0, S = 8 - Rem; I < S; ++I)
1087 OS << '\0';
Alex Lorenzee024992014-08-04 18:41:51 +00001088 }
1089 auto *FilenamesAndMappingsVal =
Vedant Kumar9e324dd2016-06-29 05:33:09 +00001090 llvm::ConstantDataArray::getString(Ctx, OS.str(), false);
Alex Lorenzee024992014-08-04 18:41:51 +00001091
1092 // Create the deferred function records array
1093 auto RecordsTy =
1094 llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
1095 auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
1096
Xinliang David Li20b188c2016-01-03 19:25:54 +00001097 llvm::Type *CovDataHeaderTypes[] = {
1098#define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
1099#include "llvm/ProfileData/InstrProfData.inc"
1100 };
1101 auto CovDataHeaderTy =
1102 llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes));
1103 llvm::Constant *CovDataHeaderVals[] = {
1104#define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
1105#include "llvm/ProfileData/InstrProfData.inc"
1106 };
1107 auto CovDataHeaderVal = llvm::ConstantStruct::get(
1108 CovDataHeaderTy, makeArrayRef(CovDataHeaderVals));
1109
Alex Lorenzee024992014-08-04 18:41:51 +00001110 // Create the coverage data record
Xinliang David Li20b188c2016-01-03 19:25:54 +00001111 llvm::Type *CovDataTypes[] = {CovDataHeaderTy, RecordsTy,
1112 FilenamesAndMappingsVal->getType()};
Alex Lorenzee024992014-08-04 18:41:51 +00001113 auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
Xinliang David Li20b188c2016-01-03 19:25:54 +00001114 llvm::Constant *TUDataVals[] = {CovDataHeaderVal, RecordsVal,
1115 FilenamesAndMappingsVal};
Alex Lorenzee024992014-08-04 18:41:51 +00001116 auto CovDataVal =
1117 llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
Xinliang David Li20b188c2016-01-03 19:25:54 +00001118 auto CovData = new llvm::GlobalVariable(
1119 CGM.getModule(), CovDataTy, true, llvm::GlobalValue::InternalLinkage,
1120 CovDataVal, llvm::getCoverageMappingVarName());
Alex Lorenzee024992014-08-04 18:41:51 +00001121
1122 CovData->setSection(getCoverageSection(CGM));
1123 CovData->setAlignment(8);
1124
1125 // Make sure the data doesn't get deleted.
1126 CGM.addUsedGlobal(CovData);
Xinliang David Li2129ae52016-01-07 20:05:55 +00001127 // Create the deferred function records array
1128 if (!FunctionNames.empty()) {
1129 auto NamesArrTy = llvm::ArrayType::get(llvm::Type::getInt8PtrTy(Ctx),
1130 FunctionNames.size());
1131 auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
1132 // This variable will *NOT* be emitted to the object file. It is used
1133 // to pass the list of names referenced to codegen.
1134 new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
1135 llvm::GlobalValue::InternalLinkage, NamesArrVal,
Xinliang David Li7077f0a2016-01-20 00:24:52 +00001136 llvm::getCoverageUnusedNamesVarName());
Xinliang David Li2129ae52016-01-07 20:05:55 +00001137 }
Alex Lorenzee024992014-08-04 18:41:51 +00001138}
1139
1140unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
1141 auto It = FileEntries.find(File);
1142 if (It != FileEntries.end())
1143 return It->second;
1144 unsigned FileID = FileEntries.size();
1145 FileEntries.insert(std::make_pair(File, FileID));
1146 return FileID;
1147}
1148
1149void CoverageMappingGen::emitCounterMapping(const Decl *D,
1150 llvm::raw_ostream &OS) {
1151 assert(CounterMap);
Justin Bognere5ee6c52014-10-02 16:44:01 +00001152 CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
Alex Lorenzee024992014-08-04 18:41:51 +00001153 Walker.VisitDecl(D);
1154 Walker.write(OS);
1155}
1156
1157void CoverageMappingGen::emitEmptyMapping(const Decl *D,
1158 llvm::raw_ostream &OS) {
1159 EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
1160 Walker.VisitDecl(D);
1161 Walker.write(OS);
1162}