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