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