blob: 55e7334acc2113f39607e633670b1497c1850b0f [file] [log] [blame]
Stephen Hines176edba2014-12-01 14:53:08 -08001//===--- 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"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070018#include "llvm/ADT/Optional.h"
Stephen Hines176edba2014-12-01 14:53:08 -080019#include "llvm/ProfileData/CoverageMapping.h"
Stephen Hines176edba2014-12-01 14:53:08 -080020#include "llvm/ProfileData/CoverageMappingReader.h"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070021#include "llvm/ProfileData/CoverageMappingWriter.h"
22#include "llvm/ProfileData/InstrProfReader.h"
Stephen Hines176edba2014-12-01 14:53:08 -080023#include "llvm/Support/FileSystem.h"
24
25using namespace clang;
26using namespace CodeGen;
27using namespace llvm::coverage;
28
29void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range) {
30 SkippedRanges.push_back(Range);
31}
32
33namespace {
34
35/// \brief A region of source code that can be mapped to a counter.
36class SourceMappingRegion {
Stephen Hines176edba2014-12-01 14:53:08 -080037 Counter Count;
38
Stephen Hines176edba2014-12-01 14:53:08 -080039 /// \brief The region's starting location.
Stephen Hines0e2c34f2015-03-23 12:09:02 -070040 Optional<SourceLocation> LocStart;
Stephen Hines176edba2014-12-01 14:53:08 -080041
42 /// \brief The region's ending location.
Stephen Hines0e2c34f2015-03-23 12:09:02 -070043 Optional<SourceLocation> LocEnd;
Stephen Hines176edba2014-12-01 14:53:08 -080044
45public:
Stephen Hines0e2c34f2015-03-23 12:09:02 -070046 SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart,
47 Optional<SourceLocation> LocEnd)
48 : Count(Count), LocStart(LocStart), LocEnd(LocEnd) {}
Stephen Hines176edba2014-12-01 14:53:08 -080049
Stephen Hines0e2c34f2015-03-23 12:09:02 -070050 SourceMappingRegion(SourceMappingRegion &&Region)
51 : Count(std::move(Region.Count)), LocStart(std::move(Region.LocStart)),
52 LocEnd(std::move(Region.LocEnd)) {}
53
54 SourceMappingRegion &operator=(SourceMappingRegion &&RHS) {
55 Count = std::move(RHS.Count);
56 LocStart = std::move(RHS.LocStart);
57 LocEnd = std::move(RHS.LocEnd);
58 return *this;
59 }
Stephen Hines176edba2014-12-01 14:53:08 -080060
61 const Counter &getCounter() const { return Count; }
62
Stephen Hines0e2c34f2015-03-23 12:09:02 -070063 void setCounter(Counter C) { Count = C; }
Stephen Hines176edba2014-12-01 14:53:08 -080064
Stephen Hines0e2c34f2015-03-23 12:09:02 -070065 bool hasStartLoc() const { return LocStart.hasValue(); }
66
67 void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
68
69 const SourceLocation &getStartLoc() const {
70 assert(LocStart && "Region has no start location");
71 return *LocStart;
Stephen Hines176edba2014-12-01 14:53:08 -080072 }
73
Stephen Hines0e2c34f2015-03-23 12:09:02 -070074 bool hasEndLoc() const { return LocEnd.hasValue(); }
Stephen Hines176edba2014-12-01 14:53:08 -080075
Stephen Hines0e2c34f2015-03-23 12:09:02 -070076 void setEndLoc(SourceLocation Loc) { LocEnd = Loc; }
Stephen Hines176edba2014-12-01 14:53:08 -080077
Stephen Hines0e2c34f2015-03-23 12:09:02 -070078 const SourceLocation &getEndLoc() const {
79 assert(LocEnd && "Region has no end location");
80 return *LocEnd;
Stephen Hines176edba2014-12-01 14:53:08 -080081 }
Stephen Hines176edba2014-12-01 14:53:08 -080082};
83
84/// \brief Provides the common functionality for the different
85/// coverage mapping region builders.
86class CoverageMappingBuilder {
87public:
88 CoverageMappingModuleGen &CVM;
89 SourceManager &SM;
90 const LangOptions &LangOpts;
91
92private:
Stephen Hines0e2c34f2015-03-23 12:09:02 -070093 /// \brief Map of clang's FileIDs to IDs used for coverage mapping.
94 llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
95 FileIDMapping;
Stephen Hines176edba2014-12-01 14:53:08 -080096
97public:
Stephen Hines176edba2014-12-01 14:53:08 -080098 /// \brief The coverage mapping regions for this function
99 llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
100 /// \brief The source mapping regions for this function.
101 std::vector<SourceMappingRegion> SourceRegions;
102
103 CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
104 const LangOptions &LangOpts)
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700105 : CVM(CVM), SM(SM), LangOpts(LangOpts) {}
Stephen Hines176edba2014-12-01 14:53:08 -0800106
107 /// \brief Return the precise end location for the given token.
108 SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700109 // 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);
Stephen Hines176edba2014-12-01 14:53:08 -0800114 }
115
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700116 /// \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));
Stephen Hines176edba2014-12-01 14:53:08 -0800121 }
122
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700123 /// \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)) -
127 SM.getFileOffset(Loc) - 1);
128 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
137 /// \brief Get the start of \c S ignoring macro argument locations.
138 SourceLocation getStart(const Stmt *S) {
139 SourceLocation Loc = S->getLocStart();
140 while (SM.isMacroArgExpansion(Loc))
141 Loc = SM.getImmediateExpansionRange(Loc).first;
142 return Loc;
143 }
144
145 /// \brief Get the end of \c S ignoring macro argument locations.
146 SourceLocation getEnd(const Stmt *S) {
147 SourceLocation Loc = S->getLocEnd();
148 while (SM.isMacroArgExpansion(Loc))
149 Loc = SM.getImmediateExpansionRange(Loc).first;
150 return Loc;
151 }
152
153 /// \brief Find the set of files we have regions for and assign IDs
154 ///
155 /// Fills \c Mapping with the virtual file mapping needed to write out
156 /// coverage and collects the necessary file information to emit source and
157 /// expansion regions.
158 void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
159 FileIDMapping.clear();
160
161 SmallVector<FileID, 8> Visited;
162 SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
163 for (const auto &Region : SourceRegions) {
164 SourceLocation Loc = Region.getStartLoc();
165 FileID File = SM.getFileID(Loc);
166 if (std::find(Visited.begin(), Visited.end(), File) != Visited.end())
167 continue;
168 Visited.push_back(File);
169
170 unsigned Depth = 0;
171 for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
172 !Parent.isInvalid(); Parent = getIncludeOrExpansionLoc(Parent))
173 ++Depth;
174 FileLocs.push_back(std::make_pair(Loc, Depth));
Stephen Hines176edba2014-12-01 14:53:08 -0800175 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700176 std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second());
Stephen Hines176edba2014-12-01 14:53:08 -0800177
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700178 for (const auto &FL : FileLocs) {
179 SourceLocation Loc = FL.first;
180 FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
181 auto Entry = SM.getFileEntryForID(SpellingFile);
182 if (!Entry)
183 continue;
Stephen Hines176edba2014-12-01 14:53:08 -0800184
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700185 FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
186 Mapping.push_back(CVM.getFileID(Entry));
187 }
Stephen Hines176edba2014-12-01 14:53:08 -0800188 }
189
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700190 /// \brief Get the coverage mapping file ID for \c Loc.
191 ///
192 /// If such file id doesn't exist, return None.
193 Optional<unsigned> getCoverageFileID(SourceLocation Loc) {
194 auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
195 if (Mapping != FileIDMapping.end())
196 return Mapping->second.first;
197 return None;
Stephen Hines176edba2014-12-01 14:53:08 -0800198 }
199
200 /// \brief Return true if the given clang's file id has a corresponding
201 /// coverage file id.
202 bool hasExistingCoverageFileID(FileID File) const {
203 return FileIDMapping.count(File);
204 }
205
206 /// \brief Gather all the regions that were skipped by the preprocessor
207 /// using the constructs like #if.
208 void gatherSkippedRegions() {
209 /// An array of the minimum lineStarts and the maximum lineEnds
210 /// for mapping regions from the appropriate source files.
211 llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
212 FileLineRanges.resize(
213 FileIDMapping.size(),
214 std::make_pair(std::numeric_limits<unsigned>::max(), 0));
215 for (const auto &R : MappingRegions) {
216 FileLineRanges[R.FileID].first =
217 std::min(FileLineRanges[R.FileID].first, R.LineStart);
218 FileLineRanges[R.FileID].second =
219 std::max(FileLineRanges[R.FileID].second, R.LineEnd);
220 }
221
222 auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
223 for (const auto &I : SkippedRanges) {
224 auto LocStart = I.getBegin();
225 auto LocEnd = I.getEnd();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700226 assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
227 "region spans multiple files");
Stephen Hines176edba2014-12-01 14:53:08 -0800228
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700229 auto CovFileID = getCoverageFileID(LocStart);
230 if (!CovFileID)
Stephen Hines176edba2014-12-01 14:53:08 -0800231 continue;
232 unsigned LineStart = SM.getSpellingLineNumber(LocStart);
233 unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
234 unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
235 unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700236 auto Region = CounterMappingRegion::makeSkipped(
237 *CovFileID, LineStart, ColumnStart, LineEnd, ColumnEnd);
Stephen Hines176edba2014-12-01 14:53:08 -0800238 // Make sure that we only collect the regions that are inside
239 // the souce code of this function.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700240 if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
241 Region.LineEnd <= FileLineRanges[*CovFileID].second)
Stephen Hines176edba2014-12-01 14:53:08 -0800242 MappingRegions.push_back(Region);
243 }
244 }
245
Stephen Hines176edba2014-12-01 14:53:08 -0800246 /// \brief Generate the coverage counter mapping regions from collected
247 /// source regions.
248 void emitSourceRegions() {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700249 for (const auto &Region : SourceRegions) {
250 assert(Region.hasEndLoc() && "incomplete region");
Stephen Hines176edba2014-12-01 14:53:08 -0800251
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700252 SourceLocation LocStart = Region.getStartLoc();
253 assert(!SM.getFileID(LocStart).isInvalid() && "region in invalid file");
Stephen Hines176edba2014-12-01 14:53:08 -0800254
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700255 auto CovFileID = getCoverageFileID(LocStart);
256 // Ignore regions that don't have a file, such as builtin macros.
257 if (!CovFileID)
Stephen Hines176edba2014-12-01 14:53:08 -0800258 continue;
259
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700260 SourceLocation LocEnd = getPreciseTokenLocEnd(Region.getEndLoc());
261 assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
262 "region spans multiple files");
263
Stephen Hines176edba2014-12-01 14:53:08 -0800264 // Find the spilling locations for the mapping region.
Stephen Hines176edba2014-12-01 14:53:08 -0800265 unsigned LineStart = SM.getSpellingLineNumber(LocStart);
266 unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
267 unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
268 unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
269
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700270 assert(LineStart <= LineEnd && "region start and end out of order");
271 MappingRegions.push_back(CounterMappingRegion::makeRegion(
272 Region.getCounter(), *CovFileID, LineStart, ColumnStart, LineEnd,
273 ColumnEnd));
274 }
275 }
276
277 /// \brief Generate expansion regions for each virtual file we've seen.
278 void emitExpansionRegions() {
279 for (const auto &FM : FileIDMapping) {
280 SourceLocation ExpandedLoc = FM.second.second;
281 SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
282 if (ParentLoc.isInvalid())
Stephen Hines176edba2014-12-01 14:53:08 -0800283 continue;
284
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700285 auto ParentFileID = getCoverageFileID(ParentLoc);
286 if (!ParentFileID)
287 continue;
288 auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
289 assert(ExpandedFileID && "expansion in uncovered file");
290
291 SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
292 assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
293 "region spans multiple files");
294
295 unsigned LineStart = SM.getSpellingLineNumber(ParentLoc);
296 unsigned ColumnStart = SM.getSpellingColumnNumber(ParentLoc);
297 unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
298 unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
299
300 MappingRegions.push_back(CounterMappingRegion::makeExpansion(
301 *ParentFileID, *ExpandedFileID, LineStart, ColumnStart, LineEnd,
302 ColumnEnd));
Stephen Hines176edba2014-12-01 14:53:08 -0800303 }
304 }
305};
306
307/// \brief Creates unreachable coverage regions for the functions that
308/// are not emitted.
309struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
310 EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
311 const LangOptions &LangOpts)
312 : CoverageMappingBuilder(CVM, SM, LangOpts) {}
313
314 void VisitDecl(const Decl *D) {
315 if (!D->hasBody())
316 return;
317 auto Body = D->getBody();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700318 SourceRegions.emplace_back(Counter(), getStart(Body), getEnd(Body));
Stephen Hines176edba2014-12-01 14:53:08 -0800319 }
320
321 /// \brief Write the mapping data to the output stream
322 void write(llvm::raw_ostream &OS) {
Stephen Hines176edba2014-12-01 14:53:08 -0800323 SmallVector<unsigned, 16> FileIDMapping;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700324 gatherFileIDs(FileIDMapping);
325 emitSourceRegions();
Stephen Hines176edba2014-12-01 14:53:08 -0800326
327 CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions);
328 Writer.write(OS);
329 }
330};
331
332/// \brief A StmtVisitor that creates coverage mapping regions which map
333/// from the source code locations to the PGO counters.
334struct CounterCoverageMappingBuilder
335 : public CoverageMappingBuilder,
336 public ConstStmtVisitor<CounterCoverageMappingBuilder> {
337 /// \brief The map of statements to count values.
338 llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
339
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700340 /// \brief A stack of currently live regions.
341 std::vector<SourceMappingRegion> RegionStack;
Stephen Hines176edba2014-12-01 14:53:08 -0800342
343 CounterExpressionBuilder Builder;
344
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700345 /// \brief A location in the most recently visited file or macro.
346 ///
347 /// This is used to adjust the active source regions appropriately when
348 /// expressions cross file or macro boundaries.
349 SourceLocation MostRecentLocation;
350
351 /// \brief Return a counter for the subtraction of \c RHS from \c LHS
Stephen Hines176edba2014-12-01 14:53:08 -0800352 Counter subtractCounters(Counter LHS, Counter RHS) {
353 return Builder.subtract(LHS, RHS);
354 }
355
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700356 /// \brief Return a counter for the sum of \c LHS and \c RHS.
Stephen Hines176edba2014-12-01 14:53:08 -0800357 Counter addCounters(Counter LHS, Counter RHS) {
358 return Builder.add(LHS, RHS);
359 }
360
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700361 Counter addCounters(Counter C1, Counter C2, Counter C3) {
362 return addCounters(addCounters(C1, C2), C3);
363 }
364
365 Counter addCounters(Counter C1, Counter C2, Counter C3, Counter C4) {
366 return addCounters(addCounters(C1, C2, C3), C4);
367 }
368
Stephen Hines176edba2014-12-01 14:53:08 -0800369 /// \brief Return the region counter for the given statement.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700370 ///
Stephen Hines176edba2014-12-01 14:53:08 -0800371 /// This should only be called on statements that have a dedicated counter.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700372 Counter getRegionCounter(const Stmt *S) {
373 return Counter::getCounter(CounterMap[S]);
Stephen Hines176edba2014-12-01 14:53:08 -0800374 }
375
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700376 /// \brief Push a region onto the stack.
377 ///
378 /// Returns the index on the stack where the region was pushed. This can be
379 /// used with popRegions to exit a "scope", ending the region that was pushed.
380 size_t pushRegion(Counter Count, Optional<SourceLocation> StartLoc = None,
381 Optional<SourceLocation> EndLoc = None) {
382 if (StartLoc)
383 MostRecentLocation = *StartLoc;
384 RegionStack.emplace_back(Count, StartLoc, EndLoc);
Stephen Hines176edba2014-12-01 14:53:08 -0800385
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700386 return RegionStack.size() - 1;
Stephen Hines176edba2014-12-01 14:53:08 -0800387 }
388
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700389 /// \brief Pop regions from the stack into the function's list of regions.
390 ///
391 /// Adds all regions from \c ParentIndex to the top of the stack to the
392 /// function's \c SourceRegions.
393 void popRegions(size_t ParentIndex) {
394 assert(RegionStack.size() >= ParentIndex && "parent not in stack");
395 while (RegionStack.size() > ParentIndex) {
396 SourceMappingRegion &Region = RegionStack.back();
397 if (Region.hasStartLoc()) {
398 SourceLocation StartLoc = Region.getStartLoc();
399 SourceLocation EndLoc = Region.hasEndLoc()
400 ? Region.getEndLoc()
401 : RegionStack[ParentIndex].getEndLoc();
402 while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
403 // The region ends in a nested file or macro expansion. Create a
404 // separate region for each expansion.
405 SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
406 assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
407
408 SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
409
410 EndLoc = getIncludeOrExpansionLoc(EndLoc);
411 assert(!EndLoc.isInvalid() &&
412 "File exit was not handled before popRegions");
413 }
414 Region.setEndLoc(EndLoc);
415
416 MostRecentLocation = EndLoc;
417 // If this region happens to span an entire expansion, we need to make
418 // sure we don't overlap the parent region with it.
419 if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
420 EndLoc == getEndOfFileOrMacro(EndLoc))
421 MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
422
423 assert(SM.isWrittenInSameFile(Region.getStartLoc(), EndLoc));
424 SourceRegions.push_back(std::move(Region));
425 }
426 RegionStack.pop_back();
427 }
Stephen Hines176edba2014-12-01 14:53:08 -0800428 }
429
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700430 /// \brief Return the currently active region.
431 SourceMappingRegion &getRegion() {
432 assert(!RegionStack.empty() && "statement has no region");
433 return RegionStack.back();
434 }
Stephen Hines176edba2014-12-01 14:53:08 -0800435
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700436 /// \brief Propagate counts through the children of \c S.
437 Counter propagateCounts(Counter TopCount, const Stmt *S) {
438 size_t Index = pushRegion(TopCount, getStart(S), getEnd(S));
439 Visit(S);
440 Counter ExitCount = getRegion().getCounter();
441 popRegions(Index);
442 return ExitCount;
443 }
Stephen Hines176edba2014-12-01 14:53:08 -0800444
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700445 /// \brief Adjust the most recently visited location to \c EndLoc.
446 ///
447 /// This should be used after visiting any statements in non-source order.
448 void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
449 MostRecentLocation = EndLoc;
450 if (MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation))
451 MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
452 }
Stephen Hines176edba2014-12-01 14:53:08 -0800453
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700454 /// \brief Check whether \c Loc is included or expanded from \c Parent.
455 bool isNestedIn(SourceLocation Loc, FileID Parent) {
456 do {
457 Loc = getIncludeOrExpansionLoc(Loc);
458 if (Loc.isInvalid())
459 return false;
460 } while (!SM.isInFileID(Loc, Parent));
461 return true;
462 }
463
464 /// \brief Adjust regions and state when \c NewLoc exits a file.
465 ///
466 /// If moving from our most recently tracked location to \c NewLoc exits any
467 /// files, this adjusts our current region stack and creates the file regions
468 /// for the exited file.
469 void handleFileExit(SourceLocation NewLoc) {
470 if (SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
471 return;
472
473 // If NewLoc is not in a file that contains MostRecentLocation, walk up to
474 // find the common ancestor.
475 SourceLocation LCA = NewLoc;
476 FileID ParentFile = SM.getFileID(LCA);
477 while (!isNestedIn(MostRecentLocation, ParentFile)) {
478 LCA = getIncludeOrExpansionLoc(LCA);
479 if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
480 // Since there isn't a common ancestor, no file was exited. We just need
481 // to adjust our location to the new file.
482 MostRecentLocation = NewLoc;
483 return;
484 }
485 ParentFile = SM.getFileID(LCA);
Stephen Hines176edba2014-12-01 14:53:08 -0800486 }
487
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700488 llvm::SmallSet<SourceLocation, 8> StartLocs;
489 Optional<Counter> ParentCounter;
490 for (auto I = RegionStack.rbegin(), E = RegionStack.rend(); I != E; ++I) {
491 if (!I->hasStartLoc())
492 continue;
493 SourceLocation Loc = I->getStartLoc();
494 if (!isNestedIn(Loc, ParentFile)) {
495 ParentCounter = I->getCounter();
496 break;
497 }
Stephen Hines176edba2014-12-01 14:53:08 -0800498
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700499 while (!SM.isInFileID(Loc, ParentFile)) {
500 // The most nested region for each start location is the one with the
501 // correct count. We avoid creating redundant regions by stopping once
502 // we've seen this region.
503 if (StartLocs.insert(Loc).second)
504 SourceRegions.emplace_back(I->getCounter(), Loc,
505 getEndOfFileOrMacro(Loc));
506 Loc = getIncludeOrExpansionLoc(Loc);
507 }
508 I->setStartLoc(getPreciseTokenLocEnd(Loc));
Stephen Hines176edba2014-12-01 14:53:08 -0800509 }
510
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700511 if (ParentCounter) {
512 // If the file is contained completely by another region and doesn't
513 // immediately start its own region, the whole file gets a region
514 // corresponding to the parent.
515 SourceLocation Loc = MostRecentLocation;
516 while (isNestedIn(Loc, ParentFile)) {
517 SourceLocation FileStart = getStartOfFileOrMacro(Loc);
518 if (StartLocs.insert(FileStart).second)
519 SourceRegions.emplace_back(*ParentCounter, FileStart,
520 getEndOfFileOrMacro(Loc));
521 Loc = getIncludeOrExpansionLoc(Loc);
522 }
Stephen Hines176edba2014-12-01 14:53:08 -0800523 }
524
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700525 MostRecentLocation = NewLoc;
526 }
Stephen Hines176edba2014-12-01 14:53:08 -0800527
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700528 /// \brief Ensure that \c S is included in the current region.
529 void extendRegion(const Stmt *S) {
530 SourceMappingRegion &Region = getRegion();
531 SourceLocation StartLoc = getStart(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800532
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700533 handleFileExit(StartLoc);
534 if (!Region.hasStartLoc())
535 Region.setStartLoc(StartLoc);
536 }
537
538 /// \brief Mark \c S as a terminator, starting a zero region.
539 void terminateRegion(const Stmt *S) {
540 extendRegion(S);
541 SourceMappingRegion &Region = getRegion();
542 if (!Region.hasEndLoc())
543 Region.setEndLoc(getEnd(S));
544 pushRegion(Counter::getZero());
545 }
Stephen Hines176edba2014-12-01 14:53:08 -0800546
547 /// \brief Keep counts of breaks and continues inside loops.
548 struct BreakContinue {
549 Counter BreakCount;
550 Counter ContinueCount;
551 };
552 SmallVector<BreakContinue, 8> BreakContinueStack;
553
554 CounterCoverageMappingBuilder(
555 CoverageMappingModuleGen &CVM,
556 llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
557 const LangOptions &LangOpts)
558 : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap) {}
559
560 /// \brief Write the mapping data to the output stream
561 void write(llvm::raw_ostream &OS) {
Stephen Hines176edba2014-12-01 14:53:08 -0800562 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700563 gatherFileIDs(VirtualFileMapping);
564 emitSourceRegions();
565 emitExpansionRegions();
Stephen Hines176edba2014-12-01 14:53:08 -0800566 gatherSkippedRegions();
567
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700568 CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
569 MappingRegions);
Stephen Hines176edba2014-12-01 14:53:08 -0800570 Writer.write(OS);
571 }
572
Stephen Hines176edba2014-12-01 14:53:08 -0800573 void VisitStmt(const Stmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700574 if (!S->getLocStart().isInvalid())
575 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800576 for (Stmt::const_child_range I = S->children(); I; ++I) {
577 if (*I)
578 this->Visit(*I);
579 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700580 handleFileExit(getEnd(S));
Stephen Hines176edba2014-12-01 14:53:08 -0800581 }
582
583 void VisitDecl(const Decl *D) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700584 Stmt *Body = D->getBody();
585 propagateCounts(getRegionCounter(Body), Body);
Stephen Hines176edba2014-12-01 14:53:08 -0800586 }
587
588 void VisitReturnStmt(const ReturnStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700589 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800590 if (S->getRetValue())
591 Visit(S->getRetValue());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700592 terminateRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800593 }
594
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700595 void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
Stephen Hines176edba2014-12-01 14:53:08 -0800596
597 void VisitLabelStmt(const LabelStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700598 SourceLocation Start = getStart(S);
599 // We can't extendRegion here or we risk overlapping with our new region.
600 handleFileExit(Start);
601 pushRegion(getRegionCounter(S), Start);
Stephen Hines176edba2014-12-01 14:53:08 -0800602 Visit(S->getSubStmt());
603 }
604
605 void VisitBreakStmt(const BreakStmt *S) {
Stephen Hines176edba2014-12-01 14:53:08 -0800606 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
607 BreakContinueStack.back().BreakCount = addCounters(
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700608 BreakContinueStack.back().BreakCount, getRegion().getCounter());
609 terminateRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800610 }
611
612 void VisitContinueStmt(const ContinueStmt *S) {
Stephen Hines176edba2014-12-01 14:53:08 -0800613 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
614 BreakContinueStack.back().ContinueCount = addCounters(
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700615 BreakContinueStack.back().ContinueCount, getRegion().getCounter());
616 terminateRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800617 }
618
619 void VisitWhileStmt(const WhileStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700620 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800621
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700622 Counter ParentCount = getRegion().getCounter();
623 Counter BodyCount = getRegionCounter(S);
624
625 // Handle the body first so that we can get the backedge count.
626 BreakContinueStack.push_back(BreakContinue());
627 extendRegion(S->getBody());
628 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
Stephen Hines176edba2014-12-01 14:53:08 -0800629 BreakContinue BC = BreakContinueStack.pop_back_val();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700630
631 // Go back to handle the condition.
632 Counter CondCount =
633 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
634 propagateCounts(CondCount, S->getCond());
635 adjustForOutOfOrderTraversal(getEnd(S));
636
637 Counter OutCount =
638 addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
639 if (OutCount != ParentCount)
640 pushRegion(OutCount);
Stephen Hines176edba2014-12-01 14:53:08 -0800641 }
642
643 void VisitDoStmt(const DoStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700644 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800645
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700646 Counter ParentCount = getRegion().getCounter();
647 Counter BodyCount = getRegionCounter(S);
648
649 BreakContinueStack.push_back(BreakContinue());
650 extendRegion(S->getBody());
651 Counter BackedgeCount =
652 propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
Stephen Hines176edba2014-12-01 14:53:08 -0800653 BreakContinue BC = BreakContinueStack.pop_back_val();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700654
655 Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
656 propagateCounts(CondCount, S->getCond());
657
658 Counter OutCount =
659 addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
660 if (OutCount != ParentCount)
661 pushRegion(OutCount);
Stephen Hines176edba2014-12-01 14:53:08 -0800662 }
663
664 void VisitForStmt(const ForStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700665 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800666 if (S->getInit())
667 Visit(S->getInit());
668
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700669 Counter ParentCount = getRegion().getCounter();
670 Counter BodyCount = getRegionCounter(S);
671
672 // Handle the body first so that we can get the backedge count.
Stephen Hines176edba2014-12-01 14:53:08 -0800673 BreakContinueStack.push_back(BreakContinue());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700674 extendRegion(S->getBody());
675 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
676 BreakContinue BC = BreakContinueStack.pop_back_val();
Stephen Hines176edba2014-12-01 14:53:08 -0800677
678 // The increment is essentially part of the body but it needs to include
679 // the count for all the continue statements.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700680 if (const Stmt *Inc = S->getInc())
681 propagateCounts(addCounters(BackedgeCount, BC.ContinueCount), Inc);
682
683 // Go back to handle the condition.
684 Counter CondCount =
685 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
686 if (const Expr *Cond = S->getCond()) {
687 propagateCounts(CondCount, Cond);
688 adjustForOutOfOrderTraversal(getEnd(S));
Stephen Hines176edba2014-12-01 14:53:08 -0800689 }
690
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700691 Counter OutCount =
692 addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
693 if (OutCount != ParentCount)
694 pushRegion(OutCount);
Stephen Hines176edba2014-12-01 14:53:08 -0800695 }
696
697 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700698 extendRegion(S);
699 Visit(S->getLoopVarStmt());
Stephen Hines176edba2014-12-01 14:53:08 -0800700 Visit(S->getRangeStmt());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700701
702 Counter ParentCount = getRegion().getCounter();
703 Counter BodyCount = getRegionCounter(S);
704
Stephen Hines176edba2014-12-01 14:53:08 -0800705 BreakContinueStack.push_back(BreakContinue());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700706 extendRegion(S->getBody());
707 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
Stephen Hines176edba2014-12-01 14:53:08 -0800708 BreakContinue BC = BreakContinueStack.pop_back_val();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700709
710 Counter OutCount = addCounters(ParentCount, BC.BreakCount, BC.ContinueCount,
711 subtractCounters(BodyCount, BackedgeCount));
712 if (OutCount != ParentCount)
713 pushRegion(OutCount);
Stephen Hines176edba2014-12-01 14:53:08 -0800714 }
715
716 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700717 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800718 Visit(S->getElement());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700719
720 Counter ParentCount = getRegion().getCounter();
721 Counter BodyCount = getRegionCounter(S);
722
Stephen Hines176edba2014-12-01 14:53:08 -0800723 BreakContinueStack.push_back(BreakContinue());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700724 extendRegion(S->getBody());
725 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
Stephen Hines176edba2014-12-01 14:53:08 -0800726 BreakContinue BC = BreakContinueStack.pop_back_val();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700727
728 Counter OutCount = addCounters(ParentCount, BC.BreakCount, BC.ContinueCount,
729 subtractCounters(BodyCount, BackedgeCount));
730 if (OutCount != ParentCount)
731 pushRegion(OutCount);
Stephen Hines176edba2014-12-01 14:53:08 -0800732 }
733
734 void VisitSwitchStmt(const SwitchStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700735 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800736 Visit(S->getCond());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700737
Stephen Hines176edba2014-12-01 14:53:08 -0800738 BreakContinueStack.push_back(BreakContinue());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700739
740 const Stmt *Body = S->getBody();
741 extendRegion(Body);
742 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
743 if (!CS->body_empty()) {
744 // The body of the switch needs a zero region so that fallthrough counts
745 // behave correctly, but it would be misleading to include the braces of
746 // the compound statement in the zeroed area, so we need to handle this
747 // specially.
748 size_t Index =
749 pushRegion(Counter::getZero(), getStart(CS->body_front()),
750 getEnd(CS->body_back()));
751 for (const auto &Child : CS->children())
752 Visit(Child);
753 popRegions(Index);
Stephen Hines176edba2014-12-01 14:53:08 -0800754 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700755 } else
756 propagateCounts(Counter::getZero(), Body);
Stephen Hines176edba2014-12-01 14:53:08 -0800757 BreakContinue BC = BreakContinueStack.pop_back_val();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700758
Stephen Hines176edba2014-12-01 14:53:08 -0800759 if (!BreakContinueStack.empty())
760 BreakContinueStack.back().ContinueCount = addCounters(
761 BreakContinueStack.back().ContinueCount, BC.ContinueCount);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700762
763 Counter ExitCount = getRegionCounter(S);
764 pushRegion(ExitCount);
Stephen Hines176edba2014-12-01 14:53:08 -0800765 }
766
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700767 void VisitSwitchCase(const SwitchCase *S) {
768 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800769
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700770 SourceMappingRegion &Parent = getRegion();
771
772 Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
773 // Reuse the existing region if it starts at our label. This is typical of
774 // the first case in a switch.
775 if (Parent.hasStartLoc() && Parent.getStartLoc() == getStart(S))
776 Parent.setCounter(Count);
777 else
778 pushRegion(Count, getStart(S));
779
780 if (const CaseStmt *CS = dyn_cast<CaseStmt>(S)) {
781 Visit(CS->getLHS());
782 if (const Expr *RHS = CS->getRHS())
783 Visit(RHS);
784 }
Stephen Hines176edba2014-12-01 14:53:08 -0800785 Visit(S->getSubStmt());
786 }
787
788 void VisitIfStmt(const IfStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700789 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800790
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700791 Counter ParentCount = getRegion().getCounter();
792 Counter ThenCount = getRegionCounter(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800793
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700794 // Emitting a counter for the condition makes it easier to interpret the
795 // counter for the body when looking at the coverage.
796 propagateCounts(ParentCount, S->getCond());
797
798 extendRegion(S->getThen());
799 Counter OutCount = propagateCounts(ThenCount, S->getThen());
800
801 Counter ElseCount = subtractCounters(ParentCount, ThenCount);
802 if (const Stmt *Else = S->getElse()) {
803 extendRegion(S->getElse());
804 OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
805 } else
806 OutCount = addCounters(OutCount, ElseCount);
807
808 if (OutCount != ParentCount)
809 pushRegion(OutCount);
Stephen Hines176edba2014-12-01 14:53:08 -0800810 }
811
812 void VisitCXXTryStmt(const CXXTryStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700813 extendRegion(S);
Stephen Hines176edba2014-12-01 14:53:08 -0800814 Visit(S->getTryBlock());
815 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
816 Visit(S->getHandler(I));
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700817
818 Counter ExitCount = getRegionCounter(S);
819 pushRegion(ExitCount);
Stephen Hines176edba2014-12-01 14:53:08 -0800820 }
821
822 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700823 extendRegion(S);
824 propagateCounts(getRegionCounter(S), S->getHandlerBlock());
Stephen Hines176edba2014-12-01 14:53:08 -0800825 }
826
827 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700828 extendRegion(E);
Stephen Hines176edba2014-12-01 14:53:08 -0800829
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700830 Counter ParentCount = getRegion().getCounter();
831 Counter TrueCount = getRegionCounter(E);
Stephen Hines176edba2014-12-01 14:53:08 -0800832
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700833 propagateCounts(TrueCount, E->getTrueExpr());
834 propagateCounts(subtractCounters(ParentCount, TrueCount),
835 E->getFalseExpr());
Stephen Hines176edba2014-12-01 14:53:08 -0800836 }
837
838 void VisitBinLAnd(const BinaryOperator *E) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700839 extendRegion(E);
Stephen Hines176edba2014-12-01 14:53:08 -0800840 Visit(E->getLHS());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700841
842 extendRegion(E->getRHS());
843 propagateCounts(getRegionCounter(E), E->getRHS());
Stephen Hines176edba2014-12-01 14:53:08 -0800844 }
845
846 void VisitBinLOr(const BinaryOperator *E) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700847 extendRegion(E);
Stephen Hines176edba2014-12-01 14:53:08 -0800848 Visit(E->getLHS());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700849
850 extendRegion(E->getRHS());
851 propagateCounts(getRegionCounter(E), E->getRHS());
Stephen Hines176edba2014-12-01 14:53:08 -0800852 }
853
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700854 void VisitLambdaExpr(const LambdaExpr *LE) {
855 // Lambdas are treated as their own functions for now, so we shouldn't
856 // propagate counts into them.
Stephen Hines176edba2014-12-01 14:53:08 -0800857 }
858};
859}
860
861static bool isMachO(const CodeGenModule &CGM) {
862 return CGM.getTarget().getTriple().isOSBinFormatMachO();
863}
864
865static StringRef getCoverageSection(const CodeGenModule &CGM) {
866 return isMachO(CGM) ? "__DATA,__llvm_covmap" : "__llvm_covmap";
867}
868
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700869static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
870 ArrayRef<CounterExpression> Expressions,
871 ArrayRef<CounterMappingRegion> Regions) {
872 OS << FunctionName << ":\n";
873 CounterMappingContext Ctx(Expressions);
874 for (const auto &R : Regions) {
Stephen Hines176edba2014-12-01 14:53:08 -0800875 OS.indent(2);
876 switch (R.Kind) {
877 case CounterMappingRegion::CodeRegion:
878 break;
879 case CounterMappingRegion::ExpansionRegion:
880 OS << "Expansion,";
881 break;
882 case CounterMappingRegion::SkippedRegion:
883 OS << "Skipped,";
884 break;
885 }
886
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700887 OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
888 << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
889 Ctx.dump(R.Count, OS);
Stephen Hines176edba2014-12-01 14:53:08 -0800890 if (R.Kind == CounterMappingRegion::ExpansionRegion)
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700891 OS << " (Expanded file = " << R.ExpandedFileID << ")";
892 OS << "\n";
Stephen Hines176edba2014-12-01 14:53:08 -0800893 }
894}
895
896void CoverageMappingModuleGen::addFunctionMappingRecord(
897 llvm::GlobalVariable *FunctionName, StringRef FunctionNameValue,
898 uint64_t FunctionHash, const std::string &CoverageMapping) {
899 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
900 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
901 auto *Int64Ty = llvm::Type::getInt64Ty(Ctx);
902 auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
903 if (!FunctionRecordTy) {
904 llvm::Type *FunctionRecordTypes[] = {Int8PtrTy, Int32Ty, Int32Ty, Int64Ty};
905 FunctionRecordTy =
906 llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes));
907 }
908
909 llvm::Constant *FunctionRecordVals[] = {
910 llvm::ConstantExpr::getBitCast(FunctionName, Int8PtrTy),
911 llvm::ConstantInt::get(Int32Ty, FunctionNameValue.size()),
912 llvm::ConstantInt::get(Int32Ty, CoverageMapping.size()),
913 llvm::ConstantInt::get(Int64Ty, FunctionHash)};
914 FunctionRecords.push_back(llvm::ConstantStruct::get(
915 FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
916 CoverageMappings += CoverageMapping;
917
918 if (CGM.getCodeGenOpts().DumpCoverageMapping) {
919 // Dump the coverage mapping data for this function by decoding the
920 // encoded data. This allows us to dump the mapping regions which were
921 // also processed by the CoverageMappingWriter which performs
922 // additional minimization operations such as reducing the number of
923 // expressions.
924 std::vector<StringRef> Filenames;
925 std::vector<CounterExpression> Expressions;
926 std::vector<CounterMappingRegion> Regions;
927 llvm::SmallVector<StringRef, 16> FilenameRefs;
928 FilenameRefs.resize(FileEntries.size());
929 for (const auto &Entry : FileEntries)
930 FilenameRefs[Entry.second] = Entry.first->getName();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700931 RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
932 Expressions, Regions);
933 if (Reader.read())
Stephen Hines176edba2014-12-01 14:53:08 -0800934 return;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700935 dump(llvm::outs(), FunctionNameValue, Expressions, Regions);
Stephen Hines176edba2014-12-01 14:53:08 -0800936 }
937}
938
939void CoverageMappingModuleGen::emit() {
940 if (FunctionRecords.empty())
941 return;
942 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
943 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
944
945 // Create the filenames and merge them with coverage mappings
946 llvm::SmallVector<std::string, 16> FilenameStrs;
947 llvm::SmallVector<StringRef, 16> FilenameRefs;
948 FilenameStrs.resize(FileEntries.size());
949 FilenameRefs.resize(FileEntries.size());
950 for (const auto &Entry : FileEntries) {
951 llvm::SmallString<256> Path(Entry.first->getName());
952 llvm::sys::fs::make_absolute(Path);
953
954 auto I = Entry.second;
955 FilenameStrs[I] = std::move(std::string(Path.begin(), Path.end()));
956 FilenameRefs[I] = FilenameStrs[I];
957 }
958
959 std::string FilenamesAndCoverageMappings;
960 llvm::raw_string_ostream OS(FilenamesAndCoverageMappings);
961 CoverageFilenamesSectionWriter(FilenameRefs).write(OS);
962 OS << CoverageMappings;
963 size_t CoverageMappingSize = CoverageMappings.size();
964 size_t FilenamesSize = OS.str().size() - CoverageMappingSize;
965 // Append extra zeroes if necessary to ensure that the size of the filenames
966 // and coverage mappings is a multiple of 8.
967 if (size_t Rem = OS.str().size() % 8) {
968 CoverageMappingSize += 8 - Rem;
969 for (size_t I = 0, S = 8 - Rem; I < S; ++I)
970 OS << '\0';
971 }
972 auto *FilenamesAndMappingsVal =
973 llvm::ConstantDataArray::getString(Ctx, OS.str(), false);
974
975 // Create the deferred function records array
976 auto RecordsTy =
977 llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
978 auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
979
980 // Create the coverage data record
981 llvm::Type *CovDataTypes[] = {Int32Ty, Int32Ty,
982 Int32Ty, Int32Ty,
983 RecordsTy, FilenamesAndMappingsVal->getType()};
984 auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
985 llvm::Constant *TUDataVals[] = {
986 llvm::ConstantInt::get(Int32Ty, FunctionRecords.size()),
987 llvm::ConstantInt::get(Int32Ty, FilenamesSize),
988 llvm::ConstantInt::get(Int32Ty, CoverageMappingSize),
989 llvm::ConstantInt::get(Int32Ty,
990 /*Version=*/CoverageMappingVersion1),
991 RecordsVal, FilenamesAndMappingsVal};
992 auto CovDataVal =
993 llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
994 auto CovData = new llvm::GlobalVariable(CGM.getModule(), CovDataTy, true,
995 llvm::GlobalValue::InternalLinkage,
996 CovDataVal,
997 "__llvm_coverage_mapping");
998
999 CovData->setSection(getCoverageSection(CGM));
1000 CovData->setAlignment(8);
1001
1002 // Make sure the data doesn't get deleted.
1003 CGM.addUsedGlobal(CovData);
1004}
1005
1006unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
1007 auto It = FileEntries.find(File);
1008 if (It != FileEntries.end())
1009 return It->second;
1010 unsigned FileID = FileEntries.size();
1011 FileEntries.insert(std::make_pair(File, FileID));
1012 return FileID;
1013}
1014
1015void CoverageMappingGen::emitCounterMapping(const Decl *D,
1016 llvm::raw_ostream &OS) {
1017 assert(CounterMap);
1018 CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
1019 Walker.VisitDecl(D);
1020 Walker.write(OS);
1021}
1022
1023void CoverageMappingGen::emitEmptyMapping(const Decl *D,
1024 llvm::raw_ostream &OS) {
1025 EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
1026 Walker.VisitDecl(D);
1027 Walker.write(OS);
1028}