1 //===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Instrumentation-based code coverage mapping generator
12 //===----------------------------------------------------------------------===//
14 #include "CoverageMappingGen.h"
15 #include "CodeGenFunction.h"
16 #include "clang/AST/StmtVisitor.h"
17 #include "clang/Lex/Lexer.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
22 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
23 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
24 #include "llvm/ProfileData/InstrProfReader.h"
25 #include "llvm/Support/FileSystem.h"
27 using namespace clang;
28 using namespace CodeGen;
29 using namespace llvm::coverage;
31 void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range) {
32 SkippedRanges.push_back(Range);
37 /// \brief A region of source code that can be mapped to a counter.
38 class SourceMappingRegion {
41 /// \brief The region's starting location.
42 Optional<SourceLocation> LocStart;
44 /// \brief The region's ending location.
45 Optional<SourceLocation> LocEnd;
48 SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart,
49 Optional<SourceLocation> LocEnd)
50 : Count(Count), LocStart(LocStart), LocEnd(LocEnd) {}
52 const Counter &getCounter() const { return Count; }
54 void setCounter(Counter C) { Count = C; }
56 bool hasStartLoc() const { return LocStart.hasValue(); }
58 void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
60 SourceLocation getStartLoc() const {
61 assert(LocStart && "Region has no start location");
65 bool hasEndLoc() const { return LocEnd.hasValue(); }
67 void setEndLoc(SourceLocation Loc) { LocEnd = Loc; }
69 SourceLocation getEndLoc() const {
70 assert(LocEnd && "Region has no end location");
75 /// \brief Provides the common functionality for the different
76 /// coverage mapping region builders.
77 class CoverageMappingBuilder {
79 CoverageMappingModuleGen &CVM;
81 const LangOptions &LangOpts;
84 /// \brief Map of clang's FileIDs to IDs used for coverage mapping.
85 llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
89 /// \brief The coverage mapping regions for this function
90 llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
91 /// \brief The source mapping regions for this function.
92 std::vector<SourceMappingRegion> SourceRegions;
94 CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
95 const LangOptions &LangOpts)
96 : CVM(CVM), SM(SM), LangOpts(LangOpts) {}
98 /// \brief Return the precise end location for the given token.
99 SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
100 // We avoid getLocForEndOfToken here, because it doesn't do what we want for
101 // macro locations, which we just treat as expanded files.
103 Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
104 return Loc.getLocWithOffset(TokLen);
107 /// \brief Return the start location of an included file or expanded macro.
108 SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
110 return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
111 return SM.getLocForStartOfFile(SM.getFileID(Loc));
114 /// \brief Return the end location of an included file or expanded macro.
115 SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
117 return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
118 SM.getFileOffset(Loc));
119 return SM.getLocForEndOfFile(SM.getFileID(Loc));
122 /// \brief Find out where the current file is included or macro is expanded.
123 SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
124 return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).first
125 : SM.getIncludeLoc(SM.getFileID(Loc));
128 /// \brief Return true if \c Loc is a location in a built-in macro.
129 bool isInBuiltin(SourceLocation Loc) {
130 return strcmp(SM.getBufferName(SM.getSpellingLoc(Loc)), "<built-in>") == 0;
133 /// \brief Check whether \c Loc is included or expanded from \c Parent.
134 bool isNestedIn(SourceLocation Loc, FileID Parent) {
136 Loc = getIncludeOrExpansionLoc(Loc);
139 } while (!SM.isInFileID(Loc, Parent));
143 /// \brief Get the start of \c S ignoring macro arguments and builtin macros.
144 SourceLocation getStart(const Stmt *S) {
145 SourceLocation Loc = S->getLocStart();
146 while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
147 Loc = SM.getImmediateExpansionRange(Loc).first;
151 /// \brief Get the end of \c S ignoring macro arguments and builtin macros.
152 SourceLocation getEnd(const Stmt *S) {
153 SourceLocation Loc = S->getLocEnd();
154 while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
155 Loc = SM.getImmediateExpansionRange(Loc).first;
156 return getPreciseTokenLocEnd(Loc);
159 /// \brief Find the set of files we have regions for and assign IDs
161 /// Fills \c Mapping with the virtual file mapping needed to write out
162 /// coverage and collects the necessary file information to emit source and
163 /// expansion regions.
164 void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
165 FileIDMapping.clear();
167 llvm::SmallSet<FileID, 8> Visited;
168 SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
169 for (const auto &Region : SourceRegions) {
170 SourceLocation Loc = Region.getStartLoc();
171 FileID File = SM.getFileID(Loc);
172 if (!Visited.insert(File).second)
175 // Do not map FileID's associated with system headers.
176 if (SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
180 for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
181 Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent))
183 FileLocs.push_back(std::make_pair(Loc, Depth));
185 std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second());
187 for (const auto &FL : FileLocs) {
188 SourceLocation Loc = FL.first;
189 FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
190 auto Entry = SM.getFileEntryForID(SpellingFile);
194 FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
195 Mapping.push_back(CVM.getFileID(Entry));
199 /// \brief Get the coverage mapping file ID for \c Loc.
201 /// If such file id doesn't exist, return None.
202 Optional<unsigned> getCoverageFileID(SourceLocation Loc) {
203 auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
204 if (Mapping != FileIDMapping.end())
205 return Mapping->second.first;
209 /// \brief Gather all the regions that were skipped by the preprocessor
210 /// using the constructs like #if.
211 void gatherSkippedRegions() {
212 /// An array of the minimum lineStarts and the maximum lineEnds
213 /// for mapping regions from the appropriate source files.
214 llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
215 FileLineRanges.resize(
216 FileIDMapping.size(),
217 std::make_pair(std::numeric_limits<unsigned>::max(), 0));
218 for (const auto &R : MappingRegions) {
219 FileLineRanges[R.FileID].first =
220 std::min(FileLineRanges[R.FileID].first, R.LineStart);
221 FileLineRanges[R.FileID].second =
222 std::max(FileLineRanges[R.FileID].second, R.LineEnd);
225 auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
226 for (const auto &I : SkippedRanges) {
227 auto LocStart = I.getBegin();
228 auto LocEnd = I.getEnd();
229 assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
230 "region spans multiple files");
232 auto CovFileID = getCoverageFileID(LocStart);
235 unsigned LineStart = SM.getSpellingLineNumber(LocStart);
236 unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
237 unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
238 unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
239 auto Region = CounterMappingRegion::makeSkipped(
240 *CovFileID, LineStart, ColumnStart, LineEnd, ColumnEnd);
241 // Make sure that we only collect the regions that are inside
242 // the souce code of this function.
243 if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
244 Region.LineEnd <= FileLineRanges[*CovFileID].second)
245 MappingRegions.push_back(Region);
249 /// \brief Generate the coverage counter mapping regions from collected
251 void emitSourceRegions() {
252 for (const auto &Region : SourceRegions) {
253 assert(Region.hasEndLoc() && "incomplete region");
255 SourceLocation LocStart = Region.getStartLoc();
256 assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
258 // Ignore regions from system headers.
259 if (SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
262 auto CovFileID = getCoverageFileID(LocStart);
263 // Ignore regions that don't have a file, such as builtin macros.
267 SourceLocation LocEnd = Region.getEndLoc();
268 assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
269 "region spans multiple files");
271 // Find the spilling locations for the mapping region.
272 unsigned LineStart = SM.getSpellingLineNumber(LocStart);
273 unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart);
274 unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
275 unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
277 assert(LineStart <= LineEnd && "region start and end out of order");
278 MappingRegions.push_back(CounterMappingRegion::makeRegion(
279 Region.getCounter(), *CovFileID, LineStart, ColumnStart, LineEnd,
284 /// \brief Generate expansion regions for each virtual file we've seen.
285 void emitExpansionRegions() {
286 for (const auto &FM : FileIDMapping) {
287 SourceLocation ExpandedLoc = FM.second.second;
288 SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
289 if (ParentLoc.isInvalid())
292 auto ParentFileID = getCoverageFileID(ParentLoc);
295 auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
296 assert(ExpandedFileID && "expansion in uncovered file");
298 SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
299 assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
300 "region spans multiple files");
302 unsigned LineStart = SM.getSpellingLineNumber(ParentLoc);
303 unsigned ColumnStart = SM.getSpellingColumnNumber(ParentLoc);
304 unsigned LineEnd = SM.getSpellingLineNumber(LocEnd);
305 unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
307 MappingRegions.push_back(CounterMappingRegion::makeExpansion(
308 *ParentFileID, *ExpandedFileID, LineStart, ColumnStart, LineEnd,
314 /// \brief Creates unreachable coverage regions for the functions that
316 struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
317 EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
318 const LangOptions &LangOpts)
319 : CoverageMappingBuilder(CVM, SM, LangOpts) {}
321 void VisitDecl(const Decl *D) {
324 auto Body = D->getBody();
325 SourceLocation Start = getStart(Body);
326 SourceLocation End = getEnd(Body);
327 if (!SM.isWrittenInSameFile(Start, End)) {
328 // Walk up to find the common ancestor.
329 // Correct the locations accordingly.
330 FileID StartFileID = SM.getFileID(Start);
331 FileID EndFileID = SM.getFileID(End);
332 while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) {
333 Start = getIncludeOrExpansionLoc(Start);
334 assert(Start.isValid() &&
335 "Declaration start location not nested within a known region");
336 StartFileID = SM.getFileID(Start);
338 while (StartFileID != EndFileID) {
339 End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
340 assert(End.isValid() &&
341 "Declaration end location not nested within a known region");
342 EndFileID = SM.getFileID(End);
345 SourceRegions.emplace_back(Counter(), Start, End);
348 /// \brief Write the mapping data to the output stream
349 void write(llvm::raw_ostream &OS) {
350 SmallVector<unsigned, 16> FileIDMapping;
351 gatherFileIDs(FileIDMapping);
354 if (MappingRegions.empty())
357 CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions);
362 /// \brief A StmtVisitor that creates coverage mapping regions which map
363 /// from the source code locations to the PGO counters.
364 struct CounterCoverageMappingBuilder
365 : public CoverageMappingBuilder,
366 public ConstStmtVisitor<CounterCoverageMappingBuilder> {
367 /// \brief The map of statements to count values.
368 llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
370 /// \brief A stack of currently live regions.
371 std::vector<SourceMappingRegion> RegionStack;
373 CounterExpressionBuilder Builder;
375 /// \brief A location in the most recently visited file or macro.
377 /// This is used to adjust the active source regions appropriately when
378 /// expressions cross file or macro boundaries.
379 SourceLocation MostRecentLocation;
381 /// \brief Return a counter for the subtraction of \c RHS from \c LHS
382 Counter subtractCounters(Counter LHS, Counter RHS) {
383 return Builder.subtract(LHS, RHS);
386 /// \brief Return a counter for the sum of \c LHS and \c RHS.
387 Counter addCounters(Counter LHS, Counter RHS) {
388 return Builder.add(LHS, RHS);
391 Counter addCounters(Counter C1, Counter C2, Counter C3) {
392 return addCounters(addCounters(C1, C2), C3);
395 /// \brief Return the region counter for the given statement.
397 /// This should only be called on statements that have a dedicated counter.
398 Counter getRegionCounter(const Stmt *S) {
399 return Counter::getCounter(CounterMap[S]);
402 /// \brief Push a region onto the stack.
404 /// Returns the index on the stack where the region was pushed. This can be
405 /// used with popRegions to exit a "scope", ending the region that was pushed.
406 size_t pushRegion(Counter Count, Optional<SourceLocation> StartLoc = None,
407 Optional<SourceLocation> EndLoc = None) {
409 MostRecentLocation = *StartLoc;
410 RegionStack.emplace_back(Count, StartLoc, EndLoc);
412 return RegionStack.size() - 1;
415 /// \brief Pop regions from the stack into the function's list of regions.
417 /// Adds all regions from \c ParentIndex to the top of the stack to the
418 /// function's \c SourceRegions.
419 void popRegions(size_t ParentIndex) {
420 assert(RegionStack.size() >= ParentIndex && "parent not in stack");
421 while (RegionStack.size() > ParentIndex) {
422 SourceMappingRegion &Region = RegionStack.back();
423 if (Region.hasStartLoc()) {
424 SourceLocation StartLoc = Region.getStartLoc();
425 SourceLocation EndLoc = Region.hasEndLoc()
427 : RegionStack[ParentIndex].getEndLoc();
428 while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
429 // The region ends in a nested file or macro expansion. Create a
430 // separate region for each expansion.
431 SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
432 assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
434 SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
436 EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
437 if (EndLoc.isInvalid())
438 llvm::report_fatal_error("File exit not handled before popRegions");
440 Region.setEndLoc(EndLoc);
442 MostRecentLocation = EndLoc;
443 // If this region happens to span an entire expansion, we need to make
444 // sure we don't overlap the parent region with it.
445 if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
446 EndLoc == getEndOfFileOrMacro(EndLoc))
447 MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
449 assert(SM.isWrittenInSameFile(Region.getStartLoc(), EndLoc));
450 SourceRegions.push_back(Region);
452 RegionStack.pop_back();
456 /// \brief Return the currently active region.
457 SourceMappingRegion &getRegion() {
458 assert(!RegionStack.empty() && "statement has no region");
459 return RegionStack.back();
462 /// \brief Propagate counts through the children of \c S.
463 Counter propagateCounts(Counter TopCount, const Stmt *S) {
464 size_t Index = pushRegion(TopCount, getStart(S), getEnd(S));
466 Counter ExitCount = getRegion().getCounter();
469 // The statement may be spanned by an expansion. Make sure we handle a file
470 // exit out of this expansion before moving to the next statement.
471 if (SM.isBeforeInTranslationUnit(getStart(S), S->getLocStart()))
472 MostRecentLocation = getEnd(S);
477 /// \brief Check whether a region with bounds \c StartLoc and \c EndLoc
478 /// is already added to \c SourceRegions.
479 bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc) {
480 return SourceRegions.rend() !=
481 std::find_if(SourceRegions.rbegin(), SourceRegions.rend(),
482 [&](const SourceMappingRegion &Region) {
483 return Region.getStartLoc() == StartLoc &&
484 Region.getEndLoc() == EndLoc;
488 /// \brief Adjust the most recently visited location to \c EndLoc.
490 /// This should be used after visiting any statements in non-source order.
491 void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
492 MostRecentLocation = EndLoc;
493 // The code region for a whole macro is created in handleFileExit() when
494 // it detects exiting of the virtual file of that macro. If we visited
495 // statements in non-source order, we might already have such a region
496 // added, for example, if a body of a loop is divided among multiple
497 // macros. Avoid adding duplicate regions in such case.
498 if (getRegion().hasEndLoc() &&
499 MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) &&
500 isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
502 MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
505 /// \brief Adjust regions and state when \c NewLoc exits a file.
507 /// If moving from our most recently tracked location to \c NewLoc exits any
508 /// files, this adjusts our current region stack and creates the file regions
509 /// for the exited file.
510 void handleFileExit(SourceLocation NewLoc) {
511 if (NewLoc.isInvalid() ||
512 SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
515 // If NewLoc is not in a file that contains MostRecentLocation, walk up to
516 // find the common ancestor.
517 SourceLocation LCA = NewLoc;
518 FileID ParentFile = SM.getFileID(LCA);
519 while (!isNestedIn(MostRecentLocation, ParentFile)) {
520 LCA = getIncludeOrExpansionLoc(LCA);
521 if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
522 // Since there isn't a common ancestor, no file was exited. We just need
523 // to adjust our location to the new file.
524 MostRecentLocation = NewLoc;
527 ParentFile = SM.getFileID(LCA);
530 llvm::SmallSet<SourceLocation, 8> StartLocs;
531 Optional<Counter> ParentCounter;
532 for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
533 if (!I.hasStartLoc())
535 SourceLocation Loc = I.getStartLoc();
536 if (!isNestedIn(Loc, ParentFile)) {
537 ParentCounter = I.getCounter();
541 while (!SM.isInFileID(Loc, ParentFile)) {
542 // The most nested region for each start location is the one with the
543 // correct count. We avoid creating redundant regions by stopping once
544 // we've seen this region.
545 if (StartLocs.insert(Loc).second)
546 SourceRegions.emplace_back(I.getCounter(), Loc,
547 getEndOfFileOrMacro(Loc));
548 Loc = getIncludeOrExpansionLoc(Loc);
550 I.setStartLoc(getPreciseTokenLocEnd(Loc));
554 // If the file is contained completely by another region and doesn't
555 // immediately start its own region, the whole file gets a region
556 // corresponding to the parent.
557 SourceLocation Loc = MostRecentLocation;
558 while (isNestedIn(Loc, ParentFile)) {
559 SourceLocation FileStart = getStartOfFileOrMacro(Loc);
560 if (StartLocs.insert(FileStart).second)
561 SourceRegions.emplace_back(*ParentCounter, FileStart,
562 getEndOfFileOrMacro(Loc));
563 Loc = getIncludeOrExpansionLoc(Loc);
567 MostRecentLocation = NewLoc;
570 /// \brief Ensure that \c S is included in the current region.
571 void extendRegion(const Stmt *S) {
572 SourceMappingRegion &Region = getRegion();
573 SourceLocation StartLoc = getStart(S);
575 handleFileExit(StartLoc);
576 if (!Region.hasStartLoc())
577 Region.setStartLoc(StartLoc);
580 /// \brief Mark \c S as a terminator, starting a zero region.
581 void terminateRegion(const Stmt *S) {
583 SourceMappingRegion &Region = getRegion();
584 if (!Region.hasEndLoc())
585 Region.setEndLoc(getEnd(S));
586 pushRegion(Counter::getZero());
589 /// \brief Keep counts of breaks and continues inside loops.
590 struct BreakContinue {
592 Counter ContinueCount;
594 SmallVector<BreakContinue, 8> BreakContinueStack;
596 CounterCoverageMappingBuilder(
597 CoverageMappingModuleGen &CVM,
598 llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
599 const LangOptions &LangOpts)
600 : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap) {}
602 /// \brief Write the mapping data to the output stream
603 void write(llvm::raw_ostream &OS) {
604 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
605 gatherFileIDs(VirtualFileMapping);
607 emitExpansionRegions();
608 gatherSkippedRegions();
610 if (MappingRegions.empty())
613 CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
618 void VisitStmt(const Stmt *S) {
619 if (S->getLocStart().isValid())
621 for (const Stmt *Child : S->children())
624 handleFileExit(getEnd(S));
627 void VisitDecl(const Decl *D) {
628 Stmt *Body = D->getBody();
630 // Do not propagate region counts into system headers.
631 if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
634 propagateCounts(getRegionCounter(Body), Body);
637 void VisitReturnStmt(const ReturnStmt *S) {
639 if (S->getRetValue())
640 Visit(S->getRetValue());
644 void VisitCXXThrowExpr(const CXXThrowExpr *E) {
647 Visit(E->getSubExpr());
651 void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
653 void VisitLabelStmt(const LabelStmt *S) {
654 SourceLocation Start = getStart(S);
655 // We can't extendRegion here or we risk overlapping with our new region.
656 handleFileExit(Start);
657 pushRegion(getRegionCounter(S), Start);
658 Visit(S->getSubStmt());
661 void VisitBreakStmt(const BreakStmt *S) {
662 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
663 BreakContinueStack.back().BreakCount = addCounters(
664 BreakContinueStack.back().BreakCount, getRegion().getCounter());
668 void VisitContinueStmt(const ContinueStmt *S) {
669 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
670 BreakContinueStack.back().ContinueCount = addCounters(
671 BreakContinueStack.back().ContinueCount, getRegion().getCounter());
675 void VisitWhileStmt(const WhileStmt *S) {
678 Counter ParentCount = getRegion().getCounter();
679 Counter BodyCount = getRegionCounter(S);
681 // Handle the body first so that we can get the backedge count.
682 BreakContinueStack.push_back(BreakContinue());
683 extendRegion(S->getBody());
684 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
685 BreakContinue BC = BreakContinueStack.pop_back_val();
687 // Go back to handle the condition.
689 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
690 propagateCounts(CondCount, S->getCond());
691 adjustForOutOfOrderTraversal(getEnd(S));
694 addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
695 if (OutCount != ParentCount)
696 pushRegion(OutCount);
699 void VisitDoStmt(const DoStmt *S) {
702 Counter ParentCount = getRegion().getCounter();
703 Counter BodyCount = getRegionCounter(S);
705 BreakContinueStack.push_back(BreakContinue());
706 extendRegion(S->getBody());
707 Counter BackedgeCount =
708 propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
709 BreakContinue BC = BreakContinueStack.pop_back_val();
711 Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
712 propagateCounts(CondCount, S->getCond());
715 addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
716 if (OutCount != ParentCount)
717 pushRegion(OutCount);
720 void VisitForStmt(const ForStmt *S) {
725 Counter ParentCount = getRegion().getCounter();
726 Counter BodyCount = getRegionCounter(S);
728 // Handle the body first so that we can get the backedge count.
729 BreakContinueStack.push_back(BreakContinue());
730 extendRegion(S->getBody());
731 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
732 BreakContinue BC = BreakContinueStack.pop_back_val();
734 // The increment is essentially part of the body but it needs to include
735 // the count for all the continue statements.
736 if (const Stmt *Inc = S->getInc())
737 propagateCounts(addCounters(BackedgeCount, BC.ContinueCount), Inc);
739 // Go back to handle the condition.
741 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
742 if (const Expr *Cond = S->getCond()) {
743 propagateCounts(CondCount, Cond);
744 adjustForOutOfOrderTraversal(getEnd(S));
748 addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
749 if (OutCount != ParentCount)
750 pushRegion(OutCount);
753 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
755 Visit(S->getLoopVarStmt());
756 Visit(S->getRangeStmt());
758 Counter ParentCount = getRegion().getCounter();
759 Counter BodyCount = getRegionCounter(S);
761 BreakContinueStack.push_back(BreakContinue());
762 extendRegion(S->getBody());
763 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
764 BreakContinue BC = BreakContinueStack.pop_back_val();
767 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
769 addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
770 if (OutCount != ParentCount)
771 pushRegion(OutCount);
774 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
776 Visit(S->getElement());
778 Counter ParentCount = getRegion().getCounter();
779 Counter BodyCount = getRegionCounter(S);
781 BreakContinueStack.push_back(BreakContinue());
782 extendRegion(S->getBody());
783 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
784 BreakContinue BC = BreakContinueStack.pop_back_val();
787 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
789 addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
790 if (OutCount != ParentCount)
791 pushRegion(OutCount);
794 void VisitSwitchStmt(const SwitchStmt *S) {
798 BreakContinueStack.push_back(BreakContinue());
800 const Stmt *Body = S->getBody();
802 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
803 if (!CS->body_empty()) {
804 // The body of the switch needs a zero region so that fallthrough counts
805 // behave correctly, but it would be misleading to include the braces of
806 // the compound statement in the zeroed area, so we need to handle this
809 pushRegion(Counter::getZero(), getStart(CS->body_front()),
810 getEnd(CS->body_back()));
811 for (const auto *Child : CS->children())
816 propagateCounts(Counter::getZero(), Body);
817 BreakContinue BC = BreakContinueStack.pop_back_val();
819 if (!BreakContinueStack.empty())
820 BreakContinueStack.back().ContinueCount = addCounters(
821 BreakContinueStack.back().ContinueCount, BC.ContinueCount);
823 Counter ExitCount = getRegionCounter(S);
824 SourceLocation ExitLoc = getEnd(S);
825 pushRegion(ExitCount, getStart(S), ExitLoc);
826 handleFileExit(ExitLoc);
829 void VisitSwitchCase(const SwitchCase *S) {
832 SourceMappingRegion &Parent = getRegion();
834 Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
835 // Reuse the existing region if it starts at our label. This is typical of
836 // the first case in a switch.
837 if (Parent.hasStartLoc() && Parent.getStartLoc() == getStart(S))
838 Parent.setCounter(Count);
840 pushRegion(Count, getStart(S));
842 if (const auto *CS = dyn_cast<CaseStmt>(S)) {
844 if (const Expr *RHS = CS->getRHS())
847 Visit(S->getSubStmt());
850 void VisitIfStmt(const IfStmt *S) {
852 // Extend into the condition before we propagate through it below - this is
853 // needed to handle macros that generate the "if" but not the condition.
854 extendRegion(S->getCond());
856 Counter ParentCount = getRegion().getCounter();
857 Counter ThenCount = getRegionCounter(S);
859 // Emitting a counter for the condition makes it easier to interpret the
860 // counter for the body when looking at the coverage.
861 propagateCounts(ParentCount, S->getCond());
863 extendRegion(S->getThen());
864 Counter OutCount = propagateCounts(ThenCount, S->getThen());
866 Counter ElseCount = subtractCounters(ParentCount, ThenCount);
867 if (const Stmt *Else = S->getElse()) {
868 extendRegion(S->getElse());
869 OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
871 OutCount = addCounters(OutCount, ElseCount);
873 if (OutCount != ParentCount)
874 pushRegion(OutCount);
877 void VisitCXXTryStmt(const CXXTryStmt *S) {
879 // Handle macros that generate the "try" but not the rest.
880 extendRegion(S->getTryBlock());
882 Counter ParentCount = getRegion().getCounter();
883 propagateCounts(ParentCount, S->getTryBlock());
885 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
886 Visit(S->getHandler(I));
888 Counter ExitCount = getRegionCounter(S);
889 pushRegion(ExitCount);
892 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
893 propagateCounts(getRegionCounter(S), S->getHandlerBlock());
896 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
899 Counter ParentCount = getRegion().getCounter();
900 Counter TrueCount = getRegionCounter(E);
904 if (!isa<BinaryConditionalOperator>(E)) {
905 extendRegion(E->getTrueExpr());
906 propagateCounts(TrueCount, E->getTrueExpr());
908 extendRegion(E->getFalseExpr());
909 propagateCounts(subtractCounters(ParentCount, TrueCount),
913 void VisitBinLAnd(const BinaryOperator *E) {
917 extendRegion(E->getRHS());
918 propagateCounts(getRegionCounter(E), E->getRHS());
921 void VisitBinLOr(const BinaryOperator *E) {
925 extendRegion(E->getRHS());
926 propagateCounts(getRegionCounter(E), E->getRHS());
929 void VisitLambdaExpr(const LambdaExpr *LE) {
930 // Lambdas are treated as their own functions for now, so we shouldn't
931 // propagate counts into them.
936 static bool isMachO(const CodeGenModule &CGM) {
937 return CGM.getTarget().getTriple().isOSBinFormatMachO();
940 static StringRef getCoverageSection(const CodeGenModule &CGM) {
941 return llvm::getInstrProfCoverageSectionName(isMachO(CGM));
944 static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
945 ArrayRef<CounterExpression> Expressions,
946 ArrayRef<CounterMappingRegion> Regions) {
947 OS << FunctionName << ":\n";
948 CounterMappingContext Ctx(Expressions);
949 for (const auto &R : Regions) {
952 case CounterMappingRegion::CodeRegion:
954 case CounterMappingRegion::ExpansionRegion:
957 case CounterMappingRegion::SkippedRegion:
962 OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
963 << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
964 Ctx.dump(R.Count, OS);
965 if (R.Kind == CounterMappingRegion::ExpansionRegion)
966 OS << " (Expanded file = " << R.ExpandedFileID << ")";
971 void CoverageMappingModuleGen::addFunctionMappingRecord(
972 llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
973 const std::string &CoverageMapping, bool IsUsed) {
974 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
975 if (!FunctionRecordTy) {
976 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
977 llvm::Type *FunctionRecordTypes[] = {
978 #include "llvm/ProfileData/InstrProfData.inc"
981 llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes),
985 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
986 llvm::Constant *FunctionRecordVals[] = {
987 #include "llvm/ProfileData/InstrProfData.inc"
989 FunctionRecords.push_back(llvm::ConstantStruct::get(
990 FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
992 FunctionNames.push_back(
993 llvm::ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx)));
994 CoverageMappings.push_back(CoverageMapping);
996 if (CGM.getCodeGenOpts().DumpCoverageMapping) {
997 // Dump the coverage mapping data for this function by decoding the
998 // encoded data. This allows us to dump the mapping regions which were
999 // also processed by the CoverageMappingWriter which performs
1000 // additional minimization operations such as reducing the number of
1002 std::vector<StringRef> Filenames;
1003 std::vector<CounterExpression> Expressions;
1004 std::vector<CounterMappingRegion> Regions;
1005 llvm::SmallVector<StringRef, 16> FilenameRefs;
1006 FilenameRefs.resize(FileEntries.size());
1007 for (const auto &Entry : FileEntries)
1008 FilenameRefs[Entry.second] = Entry.first->getName();
1009 RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
1010 Expressions, Regions);
1013 dump(llvm::outs(), NameValue, Expressions, Regions);
1017 void CoverageMappingModuleGen::emit() {
1018 if (FunctionRecords.empty())
1020 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1021 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
1023 // Create the filenames and merge them with coverage mappings
1024 llvm::SmallVector<std::string, 16> FilenameStrs;
1025 llvm::SmallVector<StringRef, 16> FilenameRefs;
1026 FilenameStrs.resize(FileEntries.size());
1027 FilenameRefs.resize(FileEntries.size());
1028 for (const auto &Entry : FileEntries) {
1029 llvm::SmallString<256> Path(Entry.first->getName());
1030 llvm::sys::fs::make_absolute(Path);
1032 auto I = Entry.second;
1033 FilenameStrs[I] = std::string(Path.begin(), Path.end());
1034 FilenameRefs[I] = FilenameStrs[I];
1037 std::string FilenamesAndCoverageMappings;
1038 llvm::raw_string_ostream OS(FilenamesAndCoverageMappings);
1039 CoverageFilenamesSectionWriter(FilenameRefs).write(OS);
1040 std::string RawCoverageMappings =
1041 llvm::join(CoverageMappings.begin(), CoverageMappings.end(), "");
1042 OS << RawCoverageMappings;
1043 size_t CoverageMappingSize = RawCoverageMappings.size();
1044 size_t FilenamesSize = OS.str().size() - CoverageMappingSize;
1045 // Append extra zeroes if necessary to ensure that the size of the filenames
1046 // and coverage mappings is a multiple of 8.
1047 if (size_t Rem = OS.str().size() % 8) {
1048 CoverageMappingSize += 8 - Rem;
1049 for (size_t I = 0, S = 8 - Rem; I < S; ++I)
1052 auto *FilenamesAndMappingsVal =
1053 llvm::ConstantDataArray::getString(Ctx, OS.str(), false);
1055 // Create the deferred function records array
1057 llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
1058 auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
1060 llvm::Type *CovDataHeaderTypes[] = {
1061 #define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
1062 #include "llvm/ProfileData/InstrProfData.inc"
1064 auto CovDataHeaderTy =
1065 llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes));
1066 llvm::Constant *CovDataHeaderVals[] = {
1067 #define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
1068 #include "llvm/ProfileData/InstrProfData.inc"
1070 auto CovDataHeaderVal = llvm::ConstantStruct::get(
1071 CovDataHeaderTy, makeArrayRef(CovDataHeaderVals));
1073 // Create the coverage data record
1074 llvm::Type *CovDataTypes[] = {CovDataHeaderTy, RecordsTy,
1075 FilenamesAndMappingsVal->getType()};
1076 auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
1077 llvm::Constant *TUDataVals[] = {CovDataHeaderVal, RecordsVal,
1078 FilenamesAndMappingsVal};
1080 llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
1081 auto CovData = new llvm::GlobalVariable(
1082 CGM.getModule(), CovDataTy, true, llvm::GlobalValue::InternalLinkage,
1083 CovDataVal, llvm::getCoverageMappingVarName());
1085 CovData->setSection(getCoverageSection(CGM));
1086 CovData->setAlignment(8);
1088 // Make sure the data doesn't get deleted.
1089 CGM.addUsedGlobal(CovData);
1090 // Create the deferred function records array
1091 if (!FunctionNames.empty()) {
1092 auto NamesArrTy = llvm::ArrayType::get(llvm::Type::getInt8PtrTy(Ctx),
1093 FunctionNames.size());
1094 auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
1095 // This variable will *NOT* be emitted to the object file. It is used
1096 // to pass the list of names referenced to codegen.
1097 new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
1098 llvm::GlobalValue::InternalLinkage, NamesArrVal,
1099 llvm::getCoverageUnusedNamesVarName());
1103 unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
1104 auto It = FileEntries.find(File);
1105 if (It != FileEntries.end())
1107 unsigned FileID = FileEntries.size();
1108 FileEntries.insert(std::make_pair(File, FileID));
1112 void CoverageMappingGen::emitCounterMapping(const Decl *D,
1113 llvm::raw_ostream &OS) {
1115 CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
1116 Walker.VisitDecl(D);
1120 void CoverageMappingGen::emitEmptyMapping(const Decl *D,
1121 llvm::raw_ostream &OS) {
1122 EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
1123 Walker.VisitDecl(D);