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