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