1ee02499aSAlex Lorenz //===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- C++ -*-===// 2ee02499aSAlex Lorenz // 3ee02499aSAlex Lorenz // The LLVM Compiler Infrastructure 4ee02499aSAlex Lorenz // 5ee02499aSAlex Lorenz // This file is distributed under the University of Illinois Open Source 6ee02499aSAlex Lorenz // License. See LICENSE.TXT for details. 7ee02499aSAlex Lorenz // 8ee02499aSAlex Lorenz //===----------------------------------------------------------------------===// 9ee02499aSAlex Lorenz // 10ee02499aSAlex Lorenz // Instrumentation-based code coverage mapping generator 11ee02499aSAlex Lorenz // 12ee02499aSAlex Lorenz //===----------------------------------------------------------------------===// 13ee02499aSAlex Lorenz 14ee02499aSAlex Lorenz #include "CoverageMappingGen.h" 15ee02499aSAlex Lorenz #include "CodeGenFunction.h" 16ee02499aSAlex Lorenz #include "clang/AST/StmtVisitor.h" 17ee02499aSAlex Lorenz #include "clang/Lex/Lexer.h" 18ee02499aSAlex Lorenz #include "llvm/ProfileData/CoverageMapping.h" 19f2cf38e0SAlex Lorenz #include "llvm/ProfileData/CoverageMappingReader.h" 200d9593ddSChandler Carruth #include "llvm/ProfileData/CoverageMappingWriter.h" 210d9593ddSChandler Carruth #include "llvm/ProfileData/InstrProfReader.h" 22ee02499aSAlex Lorenz #include "llvm/Support/FileSystem.h" 23ee02499aSAlex Lorenz 24ee02499aSAlex Lorenz using namespace clang; 25ee02499aSAlex Lorenz using namespace CodeGen; 26ee02499aSAlex Lorenz using namespace llvm::coverage; 27ee02499aSAlex Lorenz 28ee02499aSAlex Lorenz void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range) { 29ee02499aSAlex Lorenz SkippedRanges.push_back(Range); 30ee02499aSAlex Lorenz } 31ee02499aSAlex Lorenz 32ee02499aSAlex Lorenz namespace { 33ee02499aSAlex Lorenz 34ee02499aSAlex Lorenz /// \brief A region of source code that can be mapped to a counter. 3509c7179bSJustin Bogner class SourceMappingRegion { 3609c7179bSJustin Bogner public: 37ee02499aSAlex Lorenz enum RegionFlags { 38ee02499aSAlex Lorenz /// \brief This region won't be emitted if it wasn't extended. 39ee02499aSAlex Lorenz /// This is useful so that we won't emit source ranges for single tokens 40ee02499aSAlex Lorenz /// that we don't really care that much about, like: 41ee02499aSAlex Lorenz /// the '(' token in #define MACRO ( 42ee02499aSAlex Lorenz IgnoreIfNotExtended = 0x0001, 43ee02499aSAlex Lorenz }; 44ee02499aSAlex Lorenz 4509c7179bSJustin Bogner private: 46ee02499aSAlex Lorenz FileID File, MacroArgumentFile; 47ee02499aSAlex Lorenz 48ee02499aSAlex Lorenz Counter Count; 49ee02499aSAlex Lorenz 50ee02499aSAlex Lorenz /// \brief A statement that initiated the count of Zero. 51ee02499aSAlex Lorenz /// 52ee02499aSAlex Lorenz /// This initiator statement is useful to prevent merging of unreachable 53ee02499aSAlex Lorenz /// regions with different statements that caused the counter to become 54ee02499aSAlex Lorenz /// unreachable. 55ee02499aSAlex Lorenz const Stmt *UnreachableInitiator; 56ee02499aSAlex Lorenz 57ee02499aSAlex Lorenz /// \brief A statement that separates certain mapping regions into groups. 58ee02499aSAlex Lorenz /// 59ee02499aSAlex Lorenz /// The group statement is sometimes useful when we are emitting the source 60ee02499aSAlex Lorenz /// regions not in their correct lexical order, e.g. the regions for the 61ee02499aSAlex Lorenz /// incrementation expression in the 'for' construct. By marking the regions 62ee02499aSAlex Lorenz /// in the incrementation expression with the group statement, we avoid the 63ee02499aSAlex Lorenz /// merging of the regions from the incrementation expression and the loop's 64ee02499aSAlex Lorenz /// body. 65ee02499aSAlex Lorenz const Stmt *Group; 66ee02499aSAlex Lorenz 67ee02499aSAlex Lorenz /// \brief The region's starting location. 68ee02499aSAlex Lorenz SourceLocation LocStart; 69ee02499aSAlex Lorenz 70ee02499aSAlex Lorenz /// \brief The region's ending location. 71ee02499aSAlex Lorenz SourceLocation LocEnd, AlternativeLocEnd; 72ee02499aSAlex Lorenz unsigned Flags; 73ee02499aSAlex Lorenz 7409c7179bSJustin Bogner public: 75ee02499aSAlex Lorenz SourceMappingRegion(FileID File, FileID MacroArgumentFile, Counter Count, 76ee02499aSAlex Lorenz const Stmt *UnreachableInitiator, const Stmt *Group, 77ee02499aSAlex Lorenz SourceLocation LocStart, SourceLocation LocEnd, 78916cca72SJustin Bogner unsigned Flags = 0) 79ee02499aSAlex Lorenz : File(File), MacroArgumentFile(MacroArgumentFile), Count(Count), 80ee02499aSAlex Lorenz UnreachableInitiator(UnreachableInitiator), Group(Group), 81ee02499aSAlex Lorenz LocStart(LocStart), LocEnd(LocEnd), AlternativeLocEnd(LocStart), 82916cca72SJustin Bogner Flags(Flags) {} 83ee02499aSAlex Lorenz 8409c7179bSJustin Bogner const FileID &getFile() const { return File; } 8509c7179bSJustin Bogner 8609c7179bSJustin Bogner const Counter &getCounter() const { return Count; } 8709c7179bSJustin Bogner 8809c7179bSJustin Bogner const SourceLocation &getStartLoc() const { return LocStart; } 8909c7179bSJustin Bogner 9009c7179bSJustin Bogner const SourceLocation &getEndLoc(const SourceManager &SM) const { 9109c7179bSJustin Bogner if (SM.getFileID(LocEnd) != File) 9209c7179bSJustin Bogner return AlternativeLocEnd; 9309c7179bSJustin Bogner return LocEnd; 9409c7179bSJustin Bogner } 9509c7179bSJustin Bogner 96ee02499aSAlex Lorenz bool hasFlag(RegionFlags Flag) const { return (Flags & Flag) != 0; } 97ee02499aSAlex Lorenz 98ee02499aSAlex Lorenz void setFlag(RegionFlags Flag) { Flags |= Flag; } 99ee02499aSAlex Lorenz 100ee02499aSAlex Lorenz void clearFlag(RegionFlags Flag) { Flags &= ~Flag; } 101ee02499aSAlex Lorenz 102ee02499aSAlex Lorenz /// \brief Return true if two regions can be merged together. 103ee02499aSAlex Lorenz bool isMergeable(SourceMappingRegion &R) { 104f59329b0SJustin Bogner // FIXME: We allow merging regions with a gap in between them. Should we? 105ee02499aSAlex Lorenz return File == R.File && MacroArgumentFile == R.MacroArgumentFile && 106ee02499aSAlex Lorenz Count == R.Count && UnreachableInitiator == R.UnreachableInitiator && 107916cca72SJustin Bogner Group == R.Group; 108ee02499aSAlex Lorenz } 109ee02499aSAlex Lorenz 110f59329b0SJustin Bogner /// \brief A comparison that sorts such that mergeable regions are adjacent. 111f59329b0SJustin Bogner friend bool operator<(const SourceMappingRegion &LHS, 112f59329b0SJustin Bogner const SourceMappingRegion &RHS) { 113f59329b0SJustin Bogner return std::tie(LHS.File, LHS.MacroArgumentFile, LHS.Count, 114f59329b0SJustin Bogner LHS.UnreachableInitiator, LHS.Group) < 115f59329b0SJustin Bogner std::tie(RHS.File, RHS.MacroArgumentFile, RHS.Count, 116f59329b0SJustin Bogner RHS.UnreachableInitiator, RHS.Group); 117ee02499aSAlex Lorenz } 118ee02499aSAlex Lorenz }; 119ee02499aSAlex Lorenz 120ee02499aSAlex Lorenz /// \brief Provides the common functionality for the different 121ee02499aSAlex Lorenz /// coverage mapping region builders. 122ee02499aSAlex Lorenz class CoverageMappingBuilder { 123ee02499aSAlex Lorenz public: 124ee02499aSAlex Lorenz CoverageMappingModuleGen &CVM; 125ee02499aSAlex Lorenz SourceManager &SM; 126ee02499aSAlex Lorenz const LangOptions &LangOpts; 127ee02499aSAlex Lorenz 128ee02499aSAlex Lorenz private: 129ee02499aSAlex Lorenz struct FileInfo { 130ee02499aSAlex Lorenz /// \brief The file id that will be used by the coverage mapping system. 131ee02499aSAlex Lorenz unsigned CovMappingFileID; 132ee02499aSAlex Lorenz const FileEntry *Entry; 133ee02499aSAlex Lorenz 134ee02499aSAlex Lorenz FileInfo(unsigned CovMappingFileID, const FileEntry *Entry) 135ee02499aSAlex Lorenz : CovMappingFileID(CovMappingFileID), Entry(Entry) {} 136ee02499aSAlex Lorenz }; 137ee02499aSAlex Lorenz 138ee02499aSAlex Lorenz /// \brief This mapping maps clang's FileIDs to file ids used 139ee02499aSAlex Lorenz /// by the coverage mapping system and clang's file entries. 140ee02499aSAlex Lorenz llvm::SmallDenseMap<FileID, FileInfo, 8> FileIDMapping; 141ee02499aSAlex Lorenz 142ee02499aSAlex Lorenz public: 143ee02499aSAlex Lorenz /// \brief The statement that corresponds to the current source group. 144ee02499aSAlex Lorenz const Stmt *CurrentSourceGroup; 145ee02499aSAlex Lorenz 146ee02499aSAlex Lorenz /// \brief The statement the initiated the current unreachable region. 147ee02499aSAlex Lorenz const Stmt *CurrentUnreachableRegionInitiator; 148ee02499aSAlex Lorenz 149ee02499aSAlex Lorenz /// \brief The coverage mapping regions for this function 150ee02499aSAlex Lorenz llvm::SmallVector<CounterMappingRegion, 32> MappingRegions; 151ee02499aSAlex Lorenz /// \brief The source mapping regions for this function. 152f59329b0SJustin Bogner std::vector<SourceMappingRegion> SourceRegions; 153ee02499aSAlex Lorenz 154ee02499aSAlex Lorenz CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM, 155ee02499aSAlex Lorenz const LangOptions &LangOpts) 156ee02499aSAlex Lorenz : CVM(CVM), SM(SM), LangOpts(LangOpts), 157ee02499aSAlex Lorenz CurrentSourceGroup(nullptr), 158ee02499aSAlex Lorenz CurrentUnreachableRegionInitiator(nullptr) {} 159ee02499aSAlex Lorenz 160ee02499aSAlex Lorenz /// \brief Return the precise end location for the given token. 161ee02499aSAlex Lorenz SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) { 162ee02499aSAlex Lorenz return Lexer::getLocForEndOfToken(SM.getSpellingLoc(Loc), 0, SM, LangOpts); 163ee02499aSAlex Lorenz } 164ee02499aSAlex Lorenz 165ee02499aSAlex Lorenz /// \brief Create the mapping that maps from the function's file ids to 166ee02499aSAlex Lorenz /// the indices for the translation unit's filenames. 167ee02499aSAlex Lorenz void createFileIDMapping(SmallVectorImpl<unsigned> &Mapping) { 168ee02499aSAlex Lorenz Mapping.resize(FileIDMapping.size(), 0); 169ee02499aSAlex Lorenz for (const auto &I : FileIDMapping) 170ee02499aSAlex Lorenz Mapping[I.second.CovMappingFileID] = CVM.getFileID(I.second.Entry); 171ee02499aSAlex Lorenz } 172ee02499aSAlex Lorenz 173ee02499aSAlex Lorenz /// \brief Get the coverage mapping file id that corresponds to the given 174ee02499aSAlex Lorenz /// clang file id. If such file id doesn't exist, it gets added to the 175ee02499aSAlex Lorenz /// mapping that maps from clang's file ids to coverage mapping file ids. 176903678caSJustin Bogner /// Returns None if there was an error getting the coverage mapping file id. 177ee02499aSAlex Lorenz /// An example of an when this function fails is when the region tries 178ee02499aSAlex Lorenz /// to get a coverage file id for a location in a built-in macro. 179903678caSJustin Bogner Optional<unsigned> getCoverageFileID(SourceLocation LocStart, FileID File, 180903678caSJustin Bogner FileID SpellingFile) { 181ee02499aSAlex Lorenz auto Mapping = FileIDMapping.find(File); 182903678caSJustin Bogner if (Mapping != FileIDMapping.end()) 183903678caSJustin Bogner return Mapping->second.CovMappingFileID; 184ee02499aSAlex Lorenz 185ee02499aSAlex Lorenz auto Entry = SM.getFileEntryForID(SpellingFile); 186ee02499aSAlex Lorenz if (!Entry) 187903678caSJustin Bogner return None; 188ee02499aSAlex Lorenz 189903678caSJustin Bogner unsigned Result = FileIDMapping.size(); 190ee02499aSAlex Lorenz FileIDMapping.insert(std::make_pair(File, FileInfo(Result, Entry))); 191ee02499aSAlex Lorenz createFileExpansionRegion(LocStart, File); 192903678caSJustin Bogner return Result; 193ee02499aSAlex Lorenz } 194ee02499aSAlex Lorenz 195ee02499aSAlex Lorenz /// \brief Get the coverage mapping file id that corresponds to the given 196ee02499aSAlex Lorenz /// clang file id. 197903678caSJustin Bogner /// Returns None if there was an error getting the coverage mapping file id. 198903678caSJustin Bogner Optional<unsigned> getExistingCoverageFileID(FileID File) { 199ee02499aSAlex Lorenz // Make sure that the file is valid. 200ee02499aSAlex Lorenz if (File.isInvalid()) 201903678caSJustin Bogner return None; 202ee02499aSAlex Lorenz auto Mapping = FileIDMapping.find(File); 203903678caSJustin Bogner if (Mapping == FileIDMapping.end()) 204903678caSJustin Bogner return None; 205903678caSJustin Bogner return Mapping->second.CovMappingFileID; 206ee02499aSAlex Lorenz } 207ee02499aSAlex Lorenz 208ee02499aSAlex Lorenz /// \brief Return true if the given clang's file id has a corresponding 209ee02499aSAlex Lorenz /// coverage file id. 210ee02499aSAlex Lorenz bool hasExistingCoverageFileID(FileID File) const { 211ee02499aSAlex Lorenz return FileIDMapping.count(File); 212ee02499aSAlex Lorenz } 213ee02499aSAlex Lorenz 214ee02499aSAlex Lorenz /// \brief Gather all the regions that were skipped by the preprocessor 215ee02499aSAlex Lorenz /// using the constructs like #if. 216ee02499aSAlex Lorenz void gatherSkippedRegions() { 217ee02499aSAlex Lorenz /// An array of the minimum lineStarts and the maximum lineEnds 218ee02499aSAlex Lorenz /// for mapping regions from the appropriate source files. 219ee02499aSAlex Lorenz llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges; 220ee02499aSAlex Lorenz FileLineRanges.resize( 221ee02499aSAlex Lorenz FileIDMapping.size(), 222ee02499aSAlex Lorenz std::make_pair(std::numeric_limits<unsigned>::max(), 0)); 223ee02499aSAlex Lorenz for (const auto &R : MappingRegions) { 224ee02499aSAlex Lorenz FileLineRanges[R.FileID].first = 225ee02499aSAlex Lorenz std::min(FileLineRanges[R.FileID].first, R.LineStart); 226ee02499aSAlex Lorenz FileLineRanges[R.FileID].second = 227ee02499aSAlex Lorenz std::max(FileLineRanges[R.FileID].second, R.LineEnd); 228ee02499aSAlex Lorenz } 229ee02499aSAlex Lorenz 230ee02499aSAlex Lorenz auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges(); 231ee02499aSAlex Lorenz for (const auto &I : SkippedRanges) { 232ee02499aSAlex Lorenz auto LocStart = I.getBegin(); 233ee02499aSAlex Lorenz auto LocEnd = I.getEnd(); 234ee02499aSAlex Lorenz auto FileStart = SM.getFileID(LocStart); 235ee02499aSAlex Lorenz if (!hasExistingCoverageFileID(FileStart)) 236ee02499aSAlex Lorenz continue; 237ee02499aSAlex Lorenz auto ActualFileStart = SM.getDecomposedSpellingLoc(LocStart).first; 238ee02499aSAlex Lorenz if (ActualFileStart != SM.getDecomposedSpellingLoc(LocEnd).first) 239ee02499aSAlex Lorenz // Ignore regions that span across multiple files. 240ee02499aSAlex Lorenz continue; 241ee02499aSAlex Lorenz 242903678caSJustin Bogner auto CovFileID = getCoverageFileID(LocStart, FileStart, ActualFileStart); 243903678caSJustin Bogner if (!CovFileID) 244ee02499aSAlex Lorenz continue; 245ee02499aSAlex Lorenz unsigned LineStart = SM.getSpellingLineNumber(LocStart); 246ee02499aSAlex Lorenz unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart); 247ee02499aSAlex Lorenz unsigned LineEnd = SM.getSpellingLineNumber(LocEnd); 248ee02499aSAlex Lorenz unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd); 249*fd34280bSJustin Bogner auto Region = CounterMappingRegion::makeSkipped( 250*fd34280bSJustin Bogner *CovFileID, LineStart, ColumnStart, LineEnd, ColumnEnd); 251ee02499aSAlex Lorenz // Make sure that we only collect the regions that are inside 252ee02499aSAlex Lorenz // the souce code of this function. 253903678caSJustin Bogner if (Region.LineStart >= FileLineRanges[*CovFileID].first && 254903678caSJustin Bogner Region.LineEnd <= FileLineRanges[*CovFileID].second) 255ee02499aSAlex Lorenz MappingRegions.push_back(Region); 256ee02499aSAlex Lorenz } 257ee02499aSAlex Lorenz } 258ee02499aSAlex Lorenz 259ee02499aSAlex Lorenz /// \brief Create a mapping region that correponds to an expansion of 260ee02499aSAlex Lorenz /// a macro or an embedded include. 261ee02499aSAlex Lorenz void createFileExpansionRegion(SourceLocation Loc, FileID ExpandedFile) { 262ee02499aSAlex Lorenz SourceLocation LocStart; 263ee02499aSAlex Lorenz if (Loc.isMacroID()) 264ee02499aSAlex Lorenz LocStart = SM.getImmediateExpansionRange(Loc).first; 265ee02499aSAlex Lorenz else { 266ee02499aSAlex Lorenz LocStart = SM.getIncludeLoc(ExpandedFile); 267ee02499aSAlex Lorenz if (LocStart.isInvalid()) 268ee02499aSAlex Lorenz return; // This file has no expansion region. 269ee02499aSAlex Lorenz } 270ee02499aSAlex Lorenz 271ee02499aSAlex Lorenz auto File = SM.getFileID(LocStart); 272ee02499aSAlex Lorenz auto SpellingFile = SM.getDecomposedSpellingLoc(LocStart).first; 273903678caSJustin Bogner auto CovFileID = getCoverageFileID(LocStart, File, SpellingFile); 274903678caSJustin Bogner auto ExpandedFileID = getExistingCoverageFileID(ExpandedFile); 275903678caSJustin Bogner if (!CovFileID || !ExpandedFileID) 276ee02499aSAlex Lorenz return; 277ee02499aSAlex Lorenz unsigned LineStart = SM.getSpellingLineNumber(LocStart); 278ee02499aSAlex Lorenz unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart); 279ee02499aSAlex Lorenz unsigned LineEnd = LineStart; 280ee02499aSAlex Lorenz // Compute the end column manually as Lexer::getLocForEndOfToken doesn't 281ee02499aSAlex Lorenz // give the correct result in all cases. 282ee02499aSAlex Lorenz unsigned ColumnEnd = 283ee02499aSAlex Lorenz ColumnStart + 284ee02499aSAlex Lorenz Lexer::MeasureTokenLength(SM.getSpellingLoc(LocStart), SM, LangOpts); 285ee02499aSAlex Lorenz 286*fd34280bSJustin Bogner MappingRegions.push_back(CounterMappingRegion::makeExpansion( 287*fd34280bSJustin Bogner *CovFileID, *ExpandedFileID, LineStart, ColumnStart, LineEnd, 288*fd34280bSJustin Bogner ColumnEnd)); 289ee02499aSAlex Lorenz } 290ee02499aSAlex Lorenz 291ee02499aSAlex Lorenz /// \brief Enter a source region group that is identified by the given 292ee02499aSAlex Lorenz /// statement. 293ee02499aSAlex Lorenz /// It's not possible to enter a group when there is already 294ee02499aSAlex Lorenz /// another group present. 295ee02499aSAlex Lorenz void beginSourceRegionGroup(const Stmt *Group) { 296ee02499aSAlex Lorenz assert(!CurrentSourceGroup); 297ee02499aSAlex Lorenz CurrentSourceGroup = Group; 298ee02499aSAlex Lorenz } 299ee02499aSAlex Lorenz 300ee02499aSAlex Lorenz /// \brief Exit the current source region group. 301ee02499aSAlex Lorenz void endSourceRegionGroup() { CurrentSourceGroup = nullptr; } 302ee02499aSAlex Lorenz 303ee02499aSAlex Lorenz /// \brief Associate a counter with a given source code range. 304ee02499aSAlex Lorenz void mapSourceCodeRange(SourceLocation LocStart, SourceLocation LocEnd, 305ee02499aSAlex Lorenz Counter Count, const Stmt *UnreachableInitiator, 306ee02499aSAlex Lorenz const Stmt *SourceGroup, unsigned Flags = 0, 307ee02499aSAlex Lorenz FileID MacroArgumentFile = FileID()) { 308ee02499aSAlex Lorenz if (SM.isMacroArgExpansion(LocStart)) { 309ee02499aSAlex Lorenz // Map the code range with the macro argument's value. 310ee02499aSAlex Lorenz mapSourceCodeRange(SM.getImmediateSpellingLoc(LocStart), 311ee02499aSAlex Lorenz SM.getImmediateSpellingLoc(LocEnd), Count, 312ee02499aSAlex Lorenz UnreachableInitiator, SourceGroup, Flags, 313ee02499aSAlex Lorenz SM.getFileID(LocStart)); 314ee02499aSAlex Lorenz // Map the code range where the macro argument is referenced. 315ee02499aSAlex Lorenz SourceLocation RefLocStart(SM.getImmediateExpansionRange(LocStart).first); 316ee02499aSAlex Lorenz SourceLocation RefLocEnd(RefLocStart); 317ee02499aSAlex Lorenz if (SM.isMacroArgExpansion(RefLocStart)) 318ee02499aSAlex Lorenz mapSourceCodeRange(RefLocStart, RefLocEnd, Count, UnreachableInitiator, 319ee02499aSAlex Lorenz SourceGroup, 0, SM.getFileID(RefLocStart)); 320ee02499aSAlex Lorenz else 321ee02499aSAlex Lorenz mapSourceCodeRange(RefLocStart, RefLocEnd, Count, UnreachableInitiator, 322ee02499aSAlex Lorenz SourceGroup); 323ee02499aSAlex Lorenz return; 324ee02499aSAlex Lorenz } 325ee02499aSAlex Lorenz auto File = SM.getFileID(LocStart); 326ee02499aSAlex Lorenz // Make sure that the file id is valid. 327ee02499aSAlex Lorenz if (File.isInvalid()) 328ee02499aSAlex Lorenz return; 329f59329b0SJustin Bogner SourceRegions.emplace_back(File, MacroArgumentFile, Count, 330f59329b0SJustin Bogner UnreachableInitiator, SourceGroup, LocStart, 331f59329b0SJustin Bogner LocEnd, Flags); 332ee02499aSAlex Lorenz } 333ee02499aSAlex Lorenz 334ee02499aSAlex Lorenz void mapSourceCodeRange(SourceLocation LocStart, SourceLocation LocEnd, 335ee02499aSAlex Lorenz Counter Count, unsigned Flags = 0) { 336ee02499aSAlex Lorenz mapSourceCodeRange(LocStart, LocEnd, Count, 337ee02499aSAlex Lorenz CurrentUnreachableRegionInitiator, CurrentSourceGroup, 338ee02499aSAlex Lorenz Flags); 339ee02499aSAlex Lorenz } 340ee02499aSAlex Lorenz 341ee02499aSAlex Lorenz /// \brief Generate the coverage counter mapping regions from collected 342ee02499aSAlex Lorenz /// source regions. 343ee02499aSAlex Lorenz void emitSourceRegions() { 344f59329b0SJustin Bogner std::sort(SourceRegions.begin(), SourceRegions.end()); 345ee02499aSAlex Lorenz 346f59329b0SJustin Bogner for (auto I = SourceRegions.begin(), E = SourceRegions.end(); I != E; ++I) { 347f59329b0SJustin Bogner // Keep the original start location of this region. 348f59329b0SJustin Bogner SourceLocation LocStart = I->getStartLoc(); 349f59329b0SJustin Bogner SourceLocation LocEnd = I->getEndLoc(SM); 350f59329b0SJustin Bogner 351f59329b0SJustin Bogner bool Ignore = I->hasFlag(SourceMappingRegion::IgnoreIfNotExtended); 352f59329b0SJustin Bogner // We need to handle mergeable regions together. 353f59329b0SJustin Bogner for (auto Next = I + 1; Next != E && Next->isMergeable(*I); ++Next) { 354f59329b0SJustin Bogner ++I; 355f59329b0SJustin Bogner LocStart = std::min(LocStart, I->getStartLoc()); 356f59329b0SJustin Bogner LocEnd = std::max(LocEnd, I->getEndLoc(SM)); 357f59329b0SJustin Bogner // FIXME: Should we && together the Ignore flag of multiple regions? 358f59329b0SJustin Bogner Ignore = false; 359f59329b0SJustin Bogner } 360f59329b0SJustin Bogner if (Ignore) 361ee02499aSAlex Lorenz continue; 362ee02499aSAlex Lorenz 363f59329b0SJustin Bogner // Find the spilling locations for the mapping region. 364ee02499aSAlex Lorenz LocEnd = getPreciseTokenLocEnd(LocEnd); 365ee02499aSAlex Lorenz unsigned LineStart = SM.getSpellingLineNumber(LocStart); 366ee02499aSAlex Lorenz unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart); 367ee02499aSAlex Lorenz unsigned LineEnd = SM.getSpellingLineNumber(LocEnd); 368ee02499aSAlex Lorenz unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd); 369ee02499aSAlex Lorenz 37009c7179bSJustin Bogner auto SpellingFile = SM.getDecomposedSpellingLoc(LocStart).first; 371903678caSJustin Bogner auto CovFileID = getCoverageFileID(LocStart, I->getFile(), SpellingFile); 372903678caSJustin Bogner if (!CovFileID) 373ee02499aSAlex Lorenz continue; 374ee02499aSAlex Lorenz 375ee02499aSAlex Lorenz assert(LineStart <= LineEnd); 376*fd34280bSJustin Bogner MappingRegions.push_back(CounterMappingRegion::makeRegion( 377903678caSJustin Bogner I->getCounter(), *CovFileID, LineStart, ColumnStart, LineEnd, 378*fd34280bSJustin Bogner ColumnEnd)); 379ee02499aSAlex Lorenz } 380ee02499aSAlex Lorenz } 381ee02499aSAlex Lorenz }; 382ee02499aSAlex Lorenz 383ee02499aSAlex Lorenz /// \brief Creates unreachable coverage regions for the functions that 384ee02499aSAlex Lorenz /// are not emitted. 385ee02499aSAlex Lorenz struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder { 386ee02499aSAlex Lorenz EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM, 387ee02499aSAlex Lorenz const LangOptions &LangOpts) 388ee02499aSAlex Lorenz : CoverageMappingBuilder(CVM, SM, LangOpts) {} 389ee02499aSAlex Lorenz 390ee02499aSAlex Lorenz void VisitDecl(const Decl *D) { 391ee02499aSAlex Lorenz if (!D->hasBody()) 392ee02499aSAlex Lorenz return; 393ee02499aSAlex Lorenz auto Body = D->getBody(); 394ee02499aSAlex Lorenz mapSourceCodeRange(Body->getLocStart(), Body->getLocEnd(), Counter()); 395ee02499aSAlex Lorenz } 396ee02499aSAlex Lorenz 397ee02499aSAlex Lorenz /// \brief Write the mapping data to the output stream 398ee02499aSAlex Lorenz void write(llvm::raw_ostream &OS) { 399ee02499aSAlex Lorenz emitSourceRegions(); 400ee02499aSAlex Lorenz SmallVector<unsigned, 16> FileIDMapping; 401ee02499aSAlex Lorenz createFileIDMapping(FileIDMapping); 402ee02499aSAlex Lorenz 4035fc8fc2dSCraig Topper CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions); 404ee02499aSAlex Lorenz Writer.write(OS); 405ee02499aSAlex Lorenz } 406ee02499aSAlex Lorenz }; 407ee02499aSAlex Lorenz 408ee02499aSAlex Lorenz /// \brief A StmtVisitor that creates coverage mapping regions which map 409ee02499aSAlex Lorenz /// from the source code locations to the PGO counters. 410ee02499aSAlex Lorenz struct CounterCoverageMappingBuilder 411ee02499aSAlex Lorenz : public CoverageMappingBuilder, 412ee02499aSAlex Lorenz public ConstStmtVisitor<CounterCoverageMappingBuilder> { 413ee02499aSAlex Lorenz /// \brief The map of statements to count values. 414ee02499aSAlex Lorenz llvm::DenseMap<const Stmt *, unsigned> &CounterMap; 415ee02499aSAlex Lorenz 416ee02499aSAlex Lorenz Counter CurrentRegionCount; 417ee02499aSAlex Lorenz 418ee02499aSAlex Lorenz CounterExpressionBuilder Builder; 419ee02499aSAlex Lorenz 420ee02499aSAlex Lorenz /// \brief Return a counter that represents the 421ee02499aSAlex Lorenz /// expression that subracts rhs from lhs. 422ee02499aSAlex Lorenz Counter subtractCounters(Counter LHS, Counter RHS) { 423ee02499aSAlex Lorenz return Builder.subtract(LHS, RHS); 424ee02499aSAlex Lorenz } 425ee02499aSAlex Lorenz 426ee02499aSAlex Lorenz /// \brief Return a counter that represents the 427ee02499aSAlex Lorenz /// the exression that adds lhs and rhs. 428ee02499aSAlex Lorenz Counter addCounters(Counter LHS, Counter RHS) { 429ee02499aSAlex Lorenz return Builder.add(LHS, RHS); 430ee02499aSAlex Lorenz } 431ee02499aSAlex Lorenz 432ee02499aSAlex Lorenz /// \brief Return the region counter for the given statement. 433ee02499aSAlex Lorenz /// This should only be called on statements that have a dedicated counter. 434ee02499aSAlex Lorenz unsigned getRegionCounter(const Stmt *S) { return CounterMap[S]; } 435ee02499aSAlex Lorenz 436ee02499aSAlex Lorenz /// \brief Return the region count for the counter at the given index. 437ee02499aSAlex Lorenz Counter getRegionCount(unsigned CounterId) { 438ee02499aSAlex Lorenz return Counter::getCounter(CounterId); 439ee02499aSAlex Lorenz } 440ee02499aSAlex Lorenz 441ee02499aSAlex Lorenz /// \brief Return the counter value of the current region. 442ee02499aSAlex Lorenz Counter getCurrentRegionCount() { return CurrentRegionCount; } 443ee02499aSAlex Lorenz 444ee02499aSAlex Lorenz /// \brief Set the counter value for the current region. 445ee02499aSAlex Lorenz /// This is used to keep track of changes to the most recent counter 446ee02499aSAlex Lorenz /// from control flow and non-local exits. 447ee02499aSAlex Lorenz void setCurrentRegionCount(Counter Count) { 448ee02499aSAlex Lorenz CurrentRegionCount = Count; 449ee02499aSAlex Lorenz CurrentUnreachableRegionInitiator = nullptr; 450ee02499aSAlex Lorenz } 451ee02499aSAlex Lorenz 452ee02499aSAlex Lorenz /// \brief Indicate that the current region is never reached, 453ee02499aSAlex Lorenz /// and thus should have a counter value of zero. 454ee02499aSAlex Lorenz /// This is important so that subsequent regions can correctly track 455ee02499aSAlex Lorenz /// their parent counts. 456ee02499aSAlex Lorenz void setCurrentRegionUnreachable(const Stmt *Initiator) { 457ee02499aSAlex Lorenz CurrentRegionCount = Counter::getZero(); 458ee02499aSAlex Lorenz CurrentUnreachableRegionInitiator = Initiator; 459ee02499aSAlex Lorenz } 460ee02499aSAlex Lorenz 461ee02499aSAlex Lorenz /// \brief A counter for a particular region. 462ee02499aSAlex Lorenz /// This is the primary interface through 463ee02499aSAlex Lorenz /// which the coverage mapping builder manages counters and their values. 464ee02499aSAlex Lorenz class RegionMapper { 465ee02499aSAlex Lorenz CounterCoverageMappingBuilder &Mapping; 466ee02499aSAlex Lorenz Counter Count; 467ee02499aSAlex Lorenz Counter ParentCount; 468ee02499aSAlex Lorenz Counter RegionCount; 469ee02499aSAlex Lorenz Counter Adjust; 470ee02499aSAlex Lorenz 471ee02499aSAlex Lorenz public: 472ee02499aSAlex Lorenz RegionMapper(CounterCoverageMappingBuilder *Mapper, const Stmt *S) 473ee02499aSAlex Lorenz : Mapping(*Mapper), 474ee02499aSAlex Lorenz Count(Mapper->getRegionCount(Mapper->getRegionCounter(S))), 475ee02499aSAlex Lorenz ParentCount(Mapper->getCurrentRegionCount()) {} 476ee02499aSAlex Lorenz 477ee02499aSAlex Lorenz /// Get the value of the counter. In most cases this is the number of times 478ee02499aSAlex Lorenz /// the region of the counter was entered, but for switch labels it's the 479ee02499aSAlex Lorenz /// number of direct jumps to that label. 480ee02499aSAlex Lorenz Counter getCount() const { return Count; } 481ee02499aSAlex Lorenz 482ee02499aSAlex Lorenz /// Get the value of the counter with adjustments applied. Adjustments occur 483ee02499aSAlex Lorenz /// when control enters or leaves the region abnormally; i.e., if there is a 484ee02499aSAlex Lorenz /// jump to a label within the region, or if the function can return from 485ee02499aSAlex Lorenz /// within the region. The adjusted count, then, is the value of the counter 486ee02499aSAlex Lorenz /// at the end of the region. 487ee02499aSAlex Lorenz Counter getAdjustedCount() const { 488ee02499aSAlex Lorenz return Mapping.addCounters(Count, Adjust); 489ee02499aSAlex Lorenz } 490ee02499aSAlex Lorenz 491ee02499aSAlex Lorenz /// Get the value of the counter in this region's parent, i.e., the region 492ee02499aSAlex Lorenz /// that was active when this region began. This is useful for deriving 493ee02499aSAlex Lorenz /// counts in implicitly counted regions, like the false case of a condition 494ee02499aSAlex Lorenz /// or the normal exits of a loop. 495ee02499aSAlex Lorenz Counter getParentCount() const { return ParentCount; } 496ee02499aSAlex Lorenz 497ee02499aSAlex Lorenz /// Activate the counter by emitting an increment and starting to track 498ee02499aSAlex Lorenz /// adjustments. If AddIncomingFallThrough is true, the current region count 499ee02499aSAlex Lorenz /// will be added to the counter for the purposes of tracking the region. 500ee02499aSAlex Lorenz void beginRegion(bool AddIncomingFallThrough = false) { 501ee02499aSAlex Lorenz RegionCount = Count; 502ee02499aSAlex Lorenz if (AddIncomingFallThrough) 503ee02499aSAlex Lorenz RegionCount = 504ee02499aSAlex Lorenz Mapping.addCounters(RegionCount, Mapping.getCurrentRegionCount()); 505ee02499aSAlex Lorenz Mapping.setCurrentRegionCount(RegionCount); 506ee02499aSAlex Lorenz } 507ee02499aSAlex Lorenz 508ee02499aSAlex Lorenz /// For counters on boolean branches, begins tracking adjustments for the 509ee02499aSAlex Lorenz /// uncounted path. 510ee02499aSAlex Lorenz void beginElseRegion() { 511ee02499aSAlex Lorenz RegionCount = Mapping.subtractCounters(ParentCount, Count); 512ee02499aSAlex Lorenz Mapping.setCurrentRegionCount(RegionCount); 513ee02499aSAlex Lorenz } 514ee02499aSAlex Lorenz 515ee02499aSAlex Lorenz /// Reset the current region count. 516ee02499aSAlex Lorenz void setCurrentRegionCount(Counter CurrentCount) { 517ee02499aSAlex Lorenz RegionCount = CurrentCount; 518ee02499aSAlex Lorenz Mapping.setCurrentRegionCount(RegionCount); 519ee02499aSAlex Lorenz } 520ee02499aSAlex Lorenz 521ee02499aSAlex Lorenz /// Adjust for non-local control flow after emitting a subexpression or 522ee02499aSAlex Lorenz /// substatement. This must be called to account for constructs such as 523ee02499aSAlex Lorenz /// gotos, 524ee02499aSAlex Lorenz /// labels, and returns, so that we can ensure that our region's count is 525ee02499aSAlex Lorenz /// correct in the code that follows. 526ee02499aSAlex Lorenz void adjustForControlFlow() { 527ee02499aSAlex Lorenz Adjust = Mapping.addCounters( 528ee02499aSAlex Lorenz Adjust, Mapping.subtractCounters(Mapping.getCurrentRegionCount(), 529ee02499aSAlex Lorenz RegionCount)); 530ee02499aSAlex Lorenz // Reset the region count in case this is called again later. 531ee02499aSAlex Lorenz RegionCount = Mapping.getCurrentRegionCount(); 532ee02499aSAlex Lorenz } 533ee02499aSAlex Lorenz 534ee02499aSAlex Lorenz /// Commit all adjustments to the current region. If the region is a loop, 535ee02499aSAlex Lorenz /// the LoopAdjust value should be the count of all the breaks and continues 536ee02499aSAlex Lorenz /// from the loop, to compensate for those counts being deducted from the 537ee02499aSAlex Lorenz /// adjustments for the body of the loop. 538ee02499aSAlex Lorenz void applyAdjustmentsToRegion() { 539ee02499aSAlex Lorenz Mapping.setCurrentRegionCount(Mapping.addCounters(ParentCount, Adjust)); 540ee02499aSAlex Lorenz } 541ee02499aSAlex Lorenz void applyAdjustmentsToRegion(Counter LoopAdjust) { 542ee02499aSAlex Lorenz Mapping.setCurrentRegionCount(Mapping.addCounters( 543ee02499aSAlex Lorenz Mapping.addCounters(ParentCount, Adjust), LoopAdjust)); 544ee02499aSAlex Lorenz } 545ee02499aSAlex Lorenz }; 546ee02499aSAlex Lorenz 547ee02499aSAlex Lorenz /// \brief Keep counts of breaks and continues inside loops. 548ee02499aSAlex Lorenz struct BreakContinue { 549ee02499aSAlex Lorenz Counter BreakCount; 550ee02499aSAlex Lorenz Counter ContinueCount; 551ee02499aSAlex Lorenz }; 552ee02499aSAlex Lorenz SmallVector<BreakContinue, 8> BreakContinueStack; 553ee02499aSAlex Lorenz 554ee02499aSAlex Lorenz CounterCoverageMappingBuilder( 555ee02499aSAlex Lorenz CoverageMappingModuleGen &CVM, 556e5ee6c58SJustin Bogner llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM, 557ee02499aSAlex Lorenz const LangOptions &LangOpts) 558e5ee6c58SJustin Bogner : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap) {} 559ee02499aSAlex Lorenz 560ee02499aSAlex Lorenz /// \brief Write the mapping data to the output stream 561ee02499aSAlex Lorenz void write(llvm::raw_ostream &OS) { 562ee02499aSAlex Lorenz emitSourceRegions(); 563ee02499aSAlex Lorenz llvm::SmallVector<unsigned, 8> VirtualFileMapping; 564ee02499aSAlex Lorenz createFileIDMapping(VirtualFileMapping); 565ee02499aSAlex Lorenz gatherSkippedRegions(); 566ee02499aSAlex Lorenz 5674da909b2SJustin Bogner CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(), 5684da909b2SJustin Bogner MappingRegions); 569ee02499aSAlex Lorenz Writer.write(OS); 570ee02499aSAlex Lorenz } 571ee02499aSAlex Lorenz 572ee02499aSAlex Lorenz /// \brief Associate the source code range with the current region count. 573ee02499aSAlex Lorenz void mapSourceCodeRange(SourceLocation LocStart, SourceLocation LocEnd, 574ee02499aSAlex Lorenz unsigned Flags = 0) { 575ee02499aSAlex Lorenz CoverageMappingBuilder::mapSourceCodeRange(LocStart, LocEnd, 576ee02499aSAlex Lorenz CurrentRegionCount, Flags); 577ee02499aSAlex Lorenz } 578ee02499aSAlex Lorenz 579ee02499aSAlex Lorenz void mapSourceCodeRange(SourceLocation LocStart) { 580ee02499aSAlex Lorenz CoverageMappingBuilder::mapSourceCodeRange(LocStart, LocStart, 581ee02499aSAlex Lorenz CurrentRegionCount); 582ee02499aSAlex Lorenz } 583ee02499aSAlex Lorenz 584ee02499aSAlex Lorenz /// \brief Associate the source range of a token with the current region 585ee02499aSAlex Lorenz /// count. 586ee02499aSAlex Lorenz /// Ignore the source range for this token if it produces a distinct 587ee02499aSAlex Lorenz /// mapping region with no other source ranges. 588ee02499aSAlex Lorenz void mapToken(SourceLocation LocStart) { 589ee02499aSAlex Lorenz CoverageMappingBuilder::mapSourceCodeRange( 590ee02499aSAlex Lorenz LocStart, LocStart, CurrentRegionCount, 591ee02499aSAlex Lorenz SourceMappingRegion::IgnoreIfNotExtended); 592ee02499aSAlex Lorenz } 593ee02499aSAlex Lorenz 594ee02499aSAlex Lorenz void VisitStmt(const Stmt *S) { 595ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 596ee02499aSAlex Lorenz for (Stmt::const_child_range I = S->children(); I; ++I) { 597ee02499aSAlex Lorenz if (*I) 598ee02499aSAlex Lorenz this->Visit(*I); 599ee02499aSAlex Lorenz } 600ee02499aSAlex Lorenz } 601ee02499aSAlex Lorenz 602ee02499aSAlex Lorenz void VisitDecl(const Decl *D) { 603ee02499aSAlex Lorenz if (!D->hasBody()) 604ee02499aSAlex Lorenz return; 605ee02499aSAlex Lorenz // Counter tracks entry to the function body. 606ee02499aSAlex Lorenz auto Body = D->getBody(); 607ee02499aSAlex Lorenz RegionMapper Cnt(this, Body); 608ee02499aSAlex Lorenz Cnt.beginRegion(); 6094cb85f36SJustin Bogner Visit(Body); 610ee02499aSAlex Lorenz } 611ee02499aSAlex Lorenz 612ee02499aSAlex Lorenz void VisitDeclStmt(const DeclStmt *S) { 613ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 614ee02499aSAlex Lorenz for (Stmt::const_child_range I = static_cast<const Stmt *>(S)->children(); 615ee02499aSAlex Lorenz I; ++I) { 616ee02499aSAlex Lorenz if (*I) 617ee02499aSAlex Lorenz this->Visit(*I); 618ee02499aSAlex Lorenz } 619ee02499aSAlex Lorenz } 620ee02499aSAlex Lorenz 621ee02499aSAlex Lorenz void VisitCompoundStmt(const CompoundStmt *S) { 622ee02499aSAlex Lorenz mapSourceCodeRange(S->getLBracLoc()); 6238e015ff4SJustin Bogner mapSourceCodeRange(S->getRBracLoc()); 624ee02499aSAlex Lorenz for (Stmt::const_child_range I = S->children(); I; ++I) { 625ee02499aSAlex Lorenz if (*I) 626ee02499aSAlex Lorenz this->Visit(*I); 627ee02499aSAlex Lorenz } 628ee02499aSAlex Lorenz } 629ee02499aSAlex Lorenz 630ee02499aSAlex Lorenz void VisitReturnStmt(const ReturnStmt *S) { 631ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 632ee02499aSAlex Lorenz if (S->getRetValue()) 633ee02499aSAlex Lorenz Visit(S->getRetValue()); 634ee02499aSAlex Lorenz setCurrentRegionUnreachable(S); 635ee02499aSAlex Lorenz } 636ee02499aSAlex Lorenz 637ee02499aSAlex Lorenz void VisitGotoStmt(const GotoStmt *S) { 638ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 639ee02499aSAlex Lorenz mapToken(S->getLabelLoc()); 640ee02499aSAlex Lorenz setCurrentRegionUnreachable(S); 641ee02499aSAlex Lorenz } 642ee02499aSAlex Lorenz 643ee02499aSAlex Lorenz void VisitLabelStmt(const LabelStmt *S) { 644ee02499aSAlex Lorenz // Counter tracks the block following the label. 645ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 646ee02499aSAlex Lorenz Cnt.beginRegion(); 647ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 648ee02499aSAlex Lorenz // Can't map the ':' token as its location isn't known. 649ee02499aSAlex Lorenz Visit(S->getSubStmt()); 650ee02499aSAlex Lorenz } 651ee02499aSAlex Lorenz 652ee02499aSAlex Lorenz void VisitBreakStmt(const BreakStmt *S) { 653ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 654ee02499aSAlex Lorenz assert(!BreakContinueStack.empty() && "break not in a loop or switch!"); 655ee02499aSAlex Lorenz BreakContinueStack.back().BreakCount = addCounters( 656ee02499aSAlex Lorenz BreakContinueStack.back().BreakCount, getCurrentRegionCount()); 657ee02499aSAlex Lorenz setCurrentRegionUnreachable(S); 658ee02499aSAlex Lorenz } 659ee02499aSAlex Lorenz 660ee02499aSAlex Lorenz void VisitContinueStmt(const ContinueStmt *S) { 661ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 662ee02499aSAlex Lorenz assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); 663ee02499aSAlex Lorenz BreakContinueStack.back().ContinueCount = addCounters( 664ee02499aSAlex Lorenz BreakContinueStack.back().ContinueCount, getCurrentRegionCount()); 665ee02499aSAlex Lorenz setCurrentRegionUnreachable(S); 666ee02499aSAlex Lorenz } 667ee02499aSAlex Lorenz 668ee02499aSAlex Lorenz void VisitWhileStmt(const WhileStmt *S) { 669ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 670ee02499aSAlex Lorenz // Counter tracks the body of the loop. 671ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 672ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 673ee02499aSAlex Lorenz // Visit the body region first so the break/continue adjustments can be 674ee02499aSAlex Lorenz // included when visiting the condition. 675ee02499aSAlex Lorenz Cnt.beginRegion(); 6764cb85f36SJustin Bogner Visit(S->getBody()); 677ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 678ee02499aSAlex Lorenz 679ee02499aSAlex Lorenz // ...then go back and propagate counts through the condition. The count 680ee02499aSAlex Lorenz // at the start of the condition is the sum of the incoming edges, 681ee02499aSAlex Lorenz // the backedge from the end of the loop body, and the edges from 682ee02499aSAlex Lorenz // continue statements. 683ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 684ee02499aSAlex Lorenz Cnt.setCurrentRegionCount( 685ee02499aSAlex Lorenz addCounters(Cnt.getParentCount(), 686ee02499aSAlex Lorenz addCounters(Cnt.getAdjustedCount(), BC.ContinueCount))); 687ee02499aSAlex Lorenz beginSourceRegionGroup(S->getCond()); 688ee02499aSAlex Lorenz Visit(S->getCond()); 689ee02499aSAlex Lorenz endSourceRegionGroup(); 690ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 691ee02499aSAlex Lorenz Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount)); 692ee02499aSAlex Lorenz } 693ee02499aSAlex Lorenz 694ee02499aSAlex Lorenz void VisitDoStmt(const DoStmt *S) { 695ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 696ee02499aSAlex Lorenz // Counter tracks the body of the loop. 697ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 698ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 699ee02499aSAlex Lorenz Cnt.beginRegion(/*AddIncomingFallThrough=*/true); 7004cb85f36SJustin Bogner Visit(S->getBody()); 701ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 702ee02499aSAlex Lorenz 703ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 704ee02499aSAlex Lorenz // The count at the start of the condition is equal to the count at the 705ee02499aSAlex Lorenz // end of the body. The adjusted count does not include either the 706ee02499aSAlex Lorenz // fall-through count coming into the loop or the continue count, so add 707ee02499aSAlex Lorenz // both of those separately. This is coincidentally the same equation as 708ee02499aSAlex Lorenz // with while loops but for different reasons. 709ee02499aSAlex Lorenz Cnt.setCurrentRegionCount( 710ee02499aSAlex Lorenz addCounters(Cnt.getParentCount(), 711ee02499aSAlex Lorenz addCounters(Cnt.getAdjustedCount(), BC.ContinueCount))); 712ee02499aSAlex Lorenz Visit(S->getCond()); 713ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 714ee02499aSAlex Lorenz Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount)); 715ee02499aSAlex Lorenz } 716ee02499aSAlex Lorenz 717ee02499aSAlex Lorenz void VisitForStmt(const ForStmt *S) { 718ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 719ee02499aSAlex Lorenz if (S->getInit()) 720ee02499aSAlex Lorenz Visit(S->getInit()); 721ee02499aSAlex Lorenz 722ee02499aSAlex Lorenz // Counter tracks the body of the loop. 723ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 724ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 725ee02499aSAlex Lorenz // Visit the body region first. (This is basically the same as a while 726ee02499aSAlex Lorenz // loop; see further comments in VisitWhileStmt.) 727ee02499aSAlex Lorenz Cnt.beginRegion(); 7284cb85f36SJustin Bogner Visit(S->getBody()); 729ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 730ee02499aSAlex Lorenz 731ee02499aSAlex Lorenz // The increment is essentially part of the body but it needs to include 732ee02499aSAlex Lorenz // the count for all the continue statements. 733ee02499aSAlex Lorenz if (S->getInc()) { 734ee02499aSAlex Lorenz Cnt.setCurrentRegionCount(addCounters( 735ee02499aSAlex Lorenz getCurrentRegionCount(), BreakContinueStack.back().ContinueCount)); 736ee02499aSAlex Lorenz beginSourceRegionGroup(S->getInc()); 737ee02499aSAlex Lorenz Visit(S->getInc()); 738ee02499aSAlex Lorenz endSourceRegionGroup(); 739ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 740ee02499aSAlex Lorenz } 741ee02499aSAlex Lorenz 742ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 743ee02499aSAlex Lorenz 744ee02499aSAlex Lorenz // ...then go back and propagate counts through the condition. 745ee02499aSAlex Lorenz if (S->getCond()) { 746ee02499aSAlex Lorenz Cnt.setCurrentRegionCount( 747ee02499aSAlex Lorenz addCounters(addCounters(Cnt.getParentCount(), Cnt.getAdjustedCount()), 748ee02499aSAlex Lorenz BC.ContinueCount)); 749ee02499aSAlex Lorenz beginSourceRegionGroup(S->getCond()); 750ee02499aSAlex Lorenz Visit(S->getCond()); 751ee02499aSAlex Lorenz endSourceRegionGroup(); 752ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 753ee02499aSAlex Lorenz } 754ee02499aSAlex Lorenz Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount)); 755ee02499aSAlex Lorenz } 756ee02499aSAlex Lorenz 757ee02499aSAlex Lorenz void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { 758ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 759ee02499aSAlex Lorenz Visit(S->getRangeStmt()); 760ee02499aSAlex Lorenz Visit(S->getBeginEndStmt()); 761ee02499aSAlex Lorenz // Counter tracks the body of the loop. 762ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 763ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 764ee02499aSAlex Lorenz // Visit the body region first. (This is basically the same as a while 765ee02499aSAlex Lorenz // loop; see further comments in VisitWhileStmt.) 766ee02499aSAlex Lorenz Cnt.beginRegion(); 7674cb85f36SJustin Bogner Visit(S->getBody()); 768ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 769ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 770ee02499aSAlex Lorenz Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount)); 771ee02499aSAlex Lorenz } 772ee02499aSAlex Lorenz 773ee02499aSAlex Lorenz void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { 774ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 775ee02499aSAlex Lorenz Visit(S->getElement()); 776ee02499aSAlex Lorenz // Counter tracks the body of the loop. 777ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 778ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 779fdd769e8SAlex Lorenz Cnt.beginRegion(); 7804cb85f36SJustin Bogner Visit(S->getBody()); 781ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 782ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 783ee02499aSAlex Lorenz Cnt.applyAdjustmentsToRegion(addCounters(BC.BreakCount, BC.ContinueCount)); 784ee02499aSAlex Lorenz } 785ee02499aSAlex Lorenz 786ee02499aSAlex Lorenz void VisitSwitchStmt(const SwitchStmt *S) { 787ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 788ee02499aSAlex Lorenz Visit(S->getCond()); 789ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 790ee02499aSAlex Lorenz // Map the '}' for the body to have the same count as the regions after 791ee02499aSAlex Lorenz // the switch. 792ee02499aSAlex Lorenz SourceLocation RBracLoc; 793ee02499aSAlex Lorenz if (const auto *CS = dyn_cast<CompoundStmt>(S->getBody())) { 794ee02499aSAlex Lorenz mapSourceCodeRange(CS->getLBracLoc()); 795ee02499aSAlex Lorenz setCurrentRegionUnreachable(S); 796ee02499aSAlex Lorenz for (Stmt::const_child_range I = CS->children(); I; ++I) { 797ee02499aSAlex Lorenz if (*I) 798ee02499aSAlex Lorenz this->Visit(*I); 799ee02499aSAlex Lorenz } 800ee02499aSAlex Lorenz RBracLoc = CS->getRBracLoc(); 801ee02499aSAlex Lorenz } else { 802ee02499aSAlex Lorenz setCurrentRegionUnreachable(S); 803ee02499aSAlex Lorenz Visit(S->getBody()); 804ee02499aSAlex Lorenz } 805ee02499aSAlex Lorenz // If the switch is inside a loop, add the continue counts. 806ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 807ee02499aSAlex Lorenz if (!BreakContinueStack.empty()) 808ee02499aSAlex Lorenz BreakContinueStack.back().ContinueCount = addCounters( 809ee02499aSAlex Lorenz BreakContinueStack.back().ContinueCount, BC.ContinueCount); 810ee02499aSAlex Lorenz // Counter tracks the exit block of the switch. 811ee02499aSAlex Lorenz RegionMapper ExitCnt(this, S); 812ee02499aSAlex Lorenz ExitCnt.beginRegion(); 813ee02499aSAlex Lorenz if (RBracLoc.isValid()) 814ee02499aSAlex Lorenz mapSourceCodeRange(RBracLoc); 815ee02499aSAlex Lorenz } 816ee02499aSAlex Lorenz 817ee02499aSAlex Lorenz void VisitCaseStmt(const CaseStmt *S) { 818ee02499aSAlex Lorenz // Counter for this particular case. This counts only jumps from the 819ee02499aSAlex Lorenz // switch header and does not include fallthrough from the case before 820ee02499aSAlex Lorenz // this one. 821ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 822ee02499aSAlex Lorenz Cnt.beginRegion(/*AddIncomingFallThrough=*/true); 823ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 824ee02499aSAlex Lorenz mapToken(S->getColonLoc()); 825ee02499aSAlex Lorenz Visit(S->getSubStmt()); 826ee02499aSAlex Lorenz } 827ee02499aSAlex Lorenz 828ee02499aSAlex Lorenz void VisitDefaultStmt(const DefaultStmt *S) { 829ee02499aSAlex Lorenz // Counter for this default case. This does not include fallthrough from 830ee02499aSAlex Lorenz // the previous case. 831ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 832ee02499aSAlex Lorenz Cnt.beginRegion(/*AddIncomingFallThrough=*/true); 833ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 834ee02499aSAlex Lorenz mapToken(S->getColonLoc()); 835ee02499aSAlex Lorenz Visit(S->getSubStmt()); 836ee02499aSAlex Lorenz } 837ee02499aSAlex Lorenz 838ee02499aSAlex Lorenz void VisitIfStmt(const IfStmt *S) { 839ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 840ee02499aSAlex Lorenz Visit(S->getCond()); 841ee02499aSAlex Lorenz mapToken(S->getElseLoc()); 842ee02499aSAlex Lorenz 843ee02499aSAlex Lorenz // Counter tracks the "then" part of an if statement. The count for 844ee02499aSAlex Lorenz // the "else" part, if it exists, will be calculated from this counter. 845ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 846ee02499aSAlex Lorenz Cnt.beginRegion(); 8474cb85f36SJustin Bogner Visit(S->getThen()); 848ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 849ee02499aSAlex Lorenz 850ee02499aSAlex Lorenz if (S->getElse()) { 851ee02499aSAlex Lorenz Cnt.beginElseRegion(); 8524cb85f36SJustin Bogner Visit(S->getElse()); 853ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 854ee02499aSAlex Lorenz } 855ee02499aSAlex Lorenz Cnt.applyAdjustmentsToRegion(); 856ee02499aSAlex Lorenz } 857ee02499aSAlex Lorenz 858ee02499aSAlex Lorenz void VisitCXXTryStmt(const CXXTryStmt *S) { 859ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 860ee02499aSAlex Lorenz Visit(S->getTryBlock()); 861ee02499aSAlex Lorenz for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I) 862ee02499aSAlex Lorenz Visit(S->getHandler(I)); 863ee02499aSAlex Lorenz // Counter tracks the continuation block of the try statement. 864ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 865ee02499aSAlex Lorenz Cnt.beginRegion(); 866ee02499aSAlex Lorenz } 867ee02499aSAlex Lorenz 868ee02499aSAlex Lorenz void VisitCXXCatchStmt(const CXXCatchStmt *S) { 869ee02499aSAlex Lorenz mapSourceCodeRange(S->getLocStart()); 870ee02499aSAlex Lorenz // Counter tracks the catch statement's handler block. 871ee02499aSAlex Lorenz RegionMapper Cnt(this, S); 872ee02499aSAlex Lorenz Cnt.beginRegion(); 8734cb85f36SJustin Bogner Visit(S->getHandlerBlock()); 874ee02499aSAlex Lorenz } 875ee02499aSAlex Lorenz 876ee02499aSAlex Lorenz void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 877ee02499aSAlex Lorenz Visit(E->getCond()); 878ee02499aSAlex Lorenz mapToken(E->getQuestionLoc()); 8798e015ff4SJustin Bogner mapToken(E->getColonLoc()); 880ee02499aSAlex Lorenz 881ee02499aSAlex Lorenz // Counter tracks the "true" part of a conditional operator. The 882ee02499aSAlex Lorenz // count in the "false" part will be calculated from this counter. 883ee02499aSAlex Lorenz RegionMapper Cnt(this, E); 884ee02499aSAlex Lorenz Cnt.beginRegion(); 885ee02499aSAlex Lorenz Visit(E->getTrueExpr()); 886ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 887ee02499aSAlex Lorenz 888ee02499aSAlex Lorenz Cnt.beginElseRegion(); 889ee02499aSAlex Lorenz Visit(E->getFalseExpr()); 890ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 891ee02499aSAlex Lorenz 892ee02499aSAlex Lorenz Cnt.applyAdjustmentsToRegion(); 893ee02499aSAlex Lorenz } 894ee02499aSAlex Lorenz 895ee02499aSAlex Lorenz void VisitBinLAnd(const BinaryOperator *E) { 896ee02499aSAlex Lorenz Visit(E->getLHS()); 897ee02499aSAlex Lorenz mapToken(E->getOperatorLoc()); 898ee02499aSAlex Lorenz // Counter tracks the right hand side of a logical and operator. 899ee02499aSAlex Lorenz RegionMapper Cnt(this, E); 900ee02499aSAlex Lorenz Cnt.beginRegion(); 901ee02499aSAlex Lorenz Visit(E->getRHS()); 902ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 903ee02499aSAlex Lorenz Cnt.applyAdjustmentsToRegion(); 904ee02499aSAlex Lorenz } 905ee02499aSAlex Lorenz 906ee02499aSAlex Lorenz void VisitBinLOr(const BinaryOperator *E) { 907ee02499aSAlex Lorenz Visit(E->getLHS()); 908ee02499aSAlex Lorenz mapToken(E->getOperatorLoc()); 909ee02499aSAlex Lorenz // Counter tracks the right hand side of a logical or operator. 910ee02499aSAlex Lorenz RegionMapper Cnt(this, E); 911ee02499aSAlex Lorenz Cnt.beginRegion(); 912ee02499aSAlex Lorenz Visit(E->getRHS()); 913ee02499aSAlex Lorenz Cnt.adjustForControlFlow(); 914ee02499aSAlex Lorenz Cnt.applyAdjustmentsToRegion(); 915ee02499aSAlex Lorenz } 916ee02499aSAlex Lorenz 917ee02499aSAlex Lorenz void VisitParenExpr(const ParenExpr *E) { 918ee02499aSAlex Lorenz mapToken(E->getLParen()); 919ee02499aSAlex Lorenz Visit(E->getSubExpr()); 920ee02499aSAlex Lorenz mapToken(E->getRParen()); 921ee02499aSAlex Lorenz } 922ee02499aSAlex Lorenz 923ee02499aSAlex Lorenz void VisitBinaryOperator(const BinaryOperator *E) { 924ee02499aSAlex Lorenz Visit(E->getLHS()); 925ee02499aSAlex Lorenz mapToken(E->getOperatorLoc()); 926ee02499aSAlex Lorenz Visit(E->getRHS()); 927ee02499aSAlex Lorenz } 928ee02499aSAlex Lorenz 929ee02499aSAlex Lorenz void VisitUnaryOperator(const UnaryOperator *E) { 930ee02499aSAlex Lorenz bool Postfix = E->isPostfix(); 931ee02499aSAlex Lorenz if (!Postfix) 932ee02499aSAlex Lorenz mapToken(E->getOperatorLoc()); 933ee02499aSAlex Lorenz Visit(E->getSubExpr()); 934ee02499aSAlex Lorenz if (Postfix) 935ee02499aSAlex Lorenz mapToken(E->getOperatorLoc()); 936ee02499aSAlex Lorenz } 937ee02499aSAlex Lorenz 938ee02499aSAlex Lorenz void VisitMemberExpr(const MemberExpr *E) { 939ee02499aSAlex Lorenz Visit(E->getBase()); 940ee02499aSAlex Lorenz mapToken(E->getMemberLoc()); 941ee02499aSAlex Lorenz } 942ee02499aSAlex Lorenz 943ee02499aSAlex Lorenz void VisitCallExpr(const CallExpr *E) { 944ee02499aSAlex Lorenz Visit(E->getCallee()); 945ee02499aSAlex Lorenz for (const auto &Arg : E->arguments()) 946ee02499aSAlex Lorenz Visit(Arg); 947ee02499aSAlex Lorenz mapToken(E->getRParenLoc()); 948ee02499aSAlex Lorenz } 949ee02499aSAlex Lorenz 950ee02499aSAlex Lorenz void VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { 951ee02499aSAlex Lorenz Visit(E->getLHS()); 952ee02499aSAlex Lorenz Visit(E->getRHS()); 953ee02499aSAlex Lorenz mapToken(E->getRBracketLoc()); 954ee02499aSAlex Lorenz } 955ee02499aSAlex Lorenz 956ee02499aSAlex Lorenz void VisitCStyleCastExpr(const CStyleCastExpr *E) { 957ee02499aSAlex Lorenz mapToken(E->getLParenLoc()); 958ee02499aSAlex Lorenz mapToken(E->getRParenLoc()); 959ee02499aSAlex Lorenz Visit(E->getSubExpr()); 960ee02499aSAlex Lorenz } 961ee02499aSAlex Lorenz 962ee02499aSAlex Lorenz // Map literals as tokens so that the macros like #define PI 3.14 963ee02499aSAlex Lorenz // won't generate coverage mapping regions. 964ee02499aSAlex Lorenz 965ee02499aSAlex Lorenz void VisitIntegerLiteral(const IntegerLiteral *E) { 966ee02499aSAlex Lorenz mapToken(E->getLocStart()); 967ee02499aSAlex Lorenz } 968ee02499aSAlex Lorenz 969ee02499aSAlex Lorenz void VisitFloatingLiteral(const FloatingLiteral *E) { 970ee02499aSAlex Lorenz mapToken(E->getLocStart()); 971ee02499aSAlex Lorenz } 972ee02499aSAlex Lorenz 973ee02499aSAlex Lorenz void VisitCharacterLiteral(const CharacterLiteral *E) { 974ee02499aSAlex Lorenz mapToken(E->getLocStart()); 975ee02499aSAlex Lorenz } 976ee02499aSAlex Lorenz 977ee02499aSAlex Lorenz void VisitStringLiteral(const StringLiteral *E) { 978ee02499aSAlex Lorenz mapToken(E->getLocStart()); 979ee02499aSAlex Lorenz } 980ee02499aSAlex Lorenz 981ee02499aSAlex Lorenz void VisitImaginaryLiteral(const ImaginaryLiteral *E) { 982ee02499aSAlex Lorenz mapToken(E->getLocStart()); 983ee02499aSAlex Lorenz } 98401a0d062SAlex Lorenz 98501a0d062SAlex Lorenz void VisitObjCMessageExpr(const ObjCMessageExpr *E) { 98601a0d062SAlex Lorenz mapToken(E->getLeftLoc()); 9874da909b2SJustin Bogner for (Stmt::const_child_range I = static_cast<const Stmt *>(E)->children(); 9884da909b2SJustin Bogner I; ++I) { 98901a0d062SAlex Lorenz if (*I) 99001a0d062SAlex Lorenz this->Visit(*I); 99101a0d062SAlex Lorenz } 99201a0d062SAlex Lorenz mapToken(E->getRightLoc()); 99301a0d062SAlex Lorenz } 994ee02499aSAlex Lorenz }; 995ee02499aSAlex Lorenz } 996ee02499aSAlex Lorenz 997ee02499aSAlex Lorenz static bool isMachO(const CodeGenModule &CGM) { 998ee02499aSAlex Lorenz return CGM.getTarget().getTriple().isOSBinFormatMachO(); 999ee02499aSAlex Lorenz } 1000ee02499aSAlex Lorenz 1001ee02499aSAlex Lorenz static StringRef getCoverageSection(const CodeGenModule &CGM) { 1002ee02499aSAlex Lorenz return isMachO(CGM) ? "__DATA,__llvm_covmap" : "__llvm_covmap"; 1003ee02499aSAlex Lorenz } 1004ee02499aSAlex Lorenz 1005a432d176SJustin Bogner static void dump(llvm::raw_ostream &OS, StringRef FunctionName, 1006a432d176SJustin Bogner ArrayRef<CounterExpression> Expressions, 1007a432d176SJustin Bogner ArrayRef<CounterMappingRegion> Regions) { 1008a432d176SJustin Bogner OS << FunctionName << ":\n"; 1009a432d176SJustin Bogner CounterMappingContext Ctx(Expressions); 1010a432d176SJustin Bogner for (const auto &R : Regions) { 1011f2cf38e0SAlex Lorenz OS.indent(2); 1012f2cf38e0SAlex Lorenz switch (R.Kind) { 1013f2cf38e0SAlex Lorenz case CounterMappingRegion::CodeRegion: 1014f2cf38e0SAlex Lorenz break; 1015f2cf38e0SAlex Lorenz case CounterMappingRegion::ExpansionRegion: 1016f2cf38e0SAlex Lorenz OS << "Expansion,"; 1017f2cf38e0SAlex Lorenz break; 1018f2cf38e0SAlex Lorenz case CounterMappingRegion::SkippedRegion: 1019f2cf38e0SAlex Lorenz OS << "Skipped,"; 1020f2cf38e0SAlex Lorenz break; 1021f2cf38e0SAlex Lorenz } 1022f2cf38e0SAlex Lorenz 10234da909b2SJustin Bogner OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart 10244da909b2SJustin Bogner << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = "; 1025f69dc349SJustin Bogner Ctx.dump(R.Count, OS); 1026f2cf38e0SAlex Lorenz if (R.Kind == CounterMappingRegion::ExpansionRegion) 10274da909b2SJustin Bogner OS << " (Expanded file = " << R.ExpandedFileID << ")"; 10284da909b2SJustin Bogner OS << "\n"; 1029f2cf38e0SAlex Lorenz } 1030f2cf38e0SAlex Lorenz } 1031f2cf38e0SAlex Lorenz 1032ee02499aSAlex Lorenz void CoverageMappingModuleGen::addFunctionMappingRecord( 1033f2cf38e0SAlex Lorenz llvm::GlobalVariable *FunctionName, StringRef FunctionNameValue, 10341d45c5bcSAlex Lorenz uint64_t FunctionHash, const std::string &CoverageMapping) { 1035ee02499aSAlex Lorenz llvm::LLVMContext &Ctx = CGM.getLLVMContext(); 1036ee02499aSAlex Lorenz auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); 10371d45c5bcSAlex Lorenz auto *Int64Ty = llvm::Type::getInt64Ty(Ctx); 1038ee02499aSAlex Lorenz auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx); 1039ee02499aSAlex Lorenz if (!FunctionRecordTy) { 10401d45c5bcSAlex Lorenz llvm::Type *FunctionRecordTypes[] = {Int8PtrTy, Int32Ty, Int32Ty, Int64Ty}; 1041ee02499aSAlex Lorenz FunctionRecordTy = 1042ee02499aSAlex Lorenz llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes)); 1043ee02499aSAlex Lorenz } 1044ee02499aSAlex Lorenz 1045ee02499aSAlex Lorenz llvm::Constant *FunctionRecordVals[] = { 1046ee02499aSAlex Lorenz llvm::ConstantExpr::getBitCast(FunctionName, Int8PtrTy), 1047f2cf38e0SAlex Lorenz llvm::ConstantInt::get(Int32Ty, FunctionNameValue.size()), 10481d45c5bcSAlex Lorenz llvm::ConstantInt::get(Int32Ty, CoverageMapping.size()), 10491d45c5bcSAlex Lorenz llvm::ConstantInt::get(Int64Ty, FunctionHash)}; 1050ee02499aSAlex Lorenz FunctionRecords.push_back(llvm::ConstantStruct::get( 1051ee02499aSAlex Lorenz FunctionRecordTy, makeArrayRef(FunctionRecordVals))); 1052ee02499aSAlex Lorenz CoverageMappings += CoverageMapping; 1053f2cf38e0SAlex Lorenz 1054f2cf38e0SAlex Lorenz if (CGM.getCodeGenOpts().DumpCoverageMapping) { 1055f2cf38e0SAlex Lorenz // Dump the coverage mapping data for this function by decoding the 1056f2cf38e0SAlex Lorenz // encoded data. This allows us to dump the mapping regions which were 1057f2cf38e0SAlex Lorenz // also processed by the CoverageMappingWriter which performs 1058f2cf38e0SAlex Lorenz // additional minimization operations such as reducing the number of 1059f2cf38e0SAlex Lorenz // expressions. 1060f2cf38e0SAlex Lorenz std::vector<StringRef> Filenames; 1061f2cf38e0SAlex Lorenz std::vector<CounterExpression> Expressions; 1062f2cf38e0SAlex Lorenz std::vector<CounterMappingRegion> Regions; 1063f2cf38e0SAlex Lorenz llvm::SmallVector<StringRef, 16> FilenameRefs; 1064f2cf38e0SAlex Lorenz FilenameRefs.resize(FileEntries.size()); 1065f2cf38e0SAlex Lorenz for (const auto &Entry : FileEntries) 1066f2cf38e0SAlex Lorenz FilenameRefs[Entry.second] = Entry.first->getName(); 1067a432d176SJustin Bogner RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames, 1068a432d176SJustin Bogner Expressions, Regions); 1069a432d176SJustin Bogner if (Reader.read()) 1070f2cf38e0SAlex Lorenz return; 1071a432d176SJustin Bogner dump(llvm::outs(), FunctionNameValue, Expressions, Regions); 1072f2cf38e0SAlex Lorenz } 1073ee02499aSAlex Lorenz } 1074ee02499aSAlex Lorenz 1075ee02499aSAlex Lorenz void CoverageMappingModuleGen::emit() { 1076ee02499aSAlex Lorenz if (FunctionRecords.empty()) 1077ee02499aSAlex Lorenz return; 1078ee02499aSAlex Lorenz llvm::LLVMContext &Ctx = CGM.getLLVMContext(); 1079ee02499aSAlex Lorenz auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); 1080ee02499aSAlex Lorenz 1081ee02499aSAlex Lorenz // Create the filenames and merge them with coverage mappings 1082ee02499aSAlex Lorenz llvm::SmallVector<std::string, 16> FilenameStrs; 1083ee02499aSAlex Lorenz llvm::SmallVector<StringRef, 16> FilenameRefs; 1084ee02499aSAlex Lorenz FilenameStrs.resize(FileEntries.size()); 1085ee02499aSAlex Lorenz FilenameRefs.resize(FileEntries.size()); 1086ee02499aSAlex Lorenz for (const auto &Entry : FileEntries) { 1087ee02499aSAlex Lorenz llvm::SmallString<256> Path(Entry.first->getName()); 1088ee02499aSAlex Lorenz llvm::sys::fs::make_absolute(Path); 1089ee02499aSAlex Lorenz 1090ee02499aSAlex Lorenz auto I = Entry.second; 1091ee02499aSAlex Lorenz FilenameStrs[I] = std::move(std::string(Path.begin(), Path.end())); 1092ee02499aSAlex Lorenz FilenameRefs[I] = FilenameStrs[I]; 1093ee02499aSAlex Lorenz } 1094ee02499aSAlex Lorenz 1095ee02499aSAlex Lorenz std::string FilenamesAndCoverageMappings; 1096ee02499aSAlex Lorenz llvm::raw_string_ostream OS(FilenamesAndCoverageMappings); 1097ee02499aSAlex Lorenz CoverageFilenamesSectionWriter(FilenameRefs).write(OS); 1098ee02499aSAlex Lorenz OS << CoverageMappings; 1099ee02499aSAlex Lorenz size_t CoverageMappingSize = CoverageMappings.size(); 1100ee02499aSAlex Lorenz size_t FilenamesSize = OS.str().size() - CoverageMappingSize; 1101ee02499aSAlex Lorenz // Append extra zeroes if necessary to ensure that the size of the filenames 1102ee02499aSAlex Lorenz // and coverage mappings is a multiple of 8. 1103ee02499aSAlex Lorenz if (size_t Rem = OS.str().size() % 8) { 1104ee02499aSAlex Lorenz CoverageMappingSize += 8 - Rem; 1105ee02499aSAlex Lorenz for (size_t I = 0, S = 8 - Rem; I < S; ++I) 1106ee02499aSAlex Lorenz OS << '\0'; 1107ee02499aSAlex Lorenz } 1108ee02499aSAlex Lorenz auto *FilenamesAndMappingsVal = 1109ee02499aSAlex Lorenz llvm::ConstantDataArray::getString(Ctx, OS.str(), false); 1110ee02499aSAlex Lorenz 1111ee02499aSAlex Lorenz // Create the deferred function records array 1112ee02499aSAlex Lorenz auto RecordsTy = 1113ee02499aSAlex Lorenz llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size()); 1114ee02499aSAlex Lorenz auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords); 1115ee02499aSAlex Lorenz 1116ee02499aSAlex Lorenz // Create the coverage data record 1117ee02499aSAlex Lorenz llvm::Type *CovDataTypes[] = {Int32Ty, Int32Ty, 1118ee02499aSAlex Lorenz Int32Ty, Int32Ty, 1119ee02499aSAlex Lorenz RecordsTy, FilenamesAndMappingsVal->getType()}; 1120ee02499aSAlex Lorenz auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes)); 1121ee02499aSAlex Lorenz llvm::Constant *TUDataVals[] = { 1122ee02499aSAlex Lorenz llvm::ConstantInt::get(Int32Ty, FunctionRecords.size()), 1123ee02499aSAlex Lorenz llvm::ConstantInt::get(Int32Ty, FilenamesSize), 1124ee02499aSAlex Lorenz llvm::ConstantInt::get(Int32Ty, CoverageMappingSize), 1125ee02499aSAlex Lorenz llvm::ConstantInt::get(Int32Ty, 1126ee02499aSAlex Lorenz /*Version=*/CoverageMappingVersion1), 1127ee02499aSAlex Lorenz RecordsVal, FilenamesAndMappingsVal}; 1128ee02499aSAlex Lorenz auto CovDataVal = 1129ee02499aSAlex Lorenz llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals)); 1130ee02499aSAlex Lorenz auto CovData = new llvm::GlobalVariable(CGM.getModule(), CovDataTy, true, 1131ee02499aSAlex Lorenz llvm::GlobalValue::InternalLinkage, 1132ee02499aSAlex Lorenz CovDataVal, 1133ee02499aSAlex Lorenz "__llvm_coverage_mapping"); 1134ee02499aSAlex Lorenz 1135ee02499aSAlex Lorenz CovData->setSection(getCoverageSection(CGM)); 1136ee02499aSAlex Lorenz CovData->setAlignment(8); 1137ee02499aSAlex Lorenz 1138ee02499aSAlex Lorenz // Make sure the data doesn't get deleted. 1139ee02499aSAlex Lorenz CGM.addUsedGlobal(CovData); 1140ee02499aSAlex Lorenz } 1141ee02499aSAlex Lorenz 1142ee02499aSAlex Lorenz unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) { 1143ee02499aSAlex Lorenz auto It = FileEntries.find(File); 1144ee02499aSAlex Lorenz if (It != FileEntries.end()) 1145ee02499aSAlex Lorenz return It->second; 1146ee02499aSAlex Lorenz unsigned FileID = FileEntries.size(); 1147ee02499aSAlex Lorenz FileEntries.insert(std::make_pair(File, FileID)); 1148ee02499aSAlex Lorenz return FileID; 1149ee02499aSAlex Lorenz } 1150ee02499aSAlex Lorenz 1151ee02499aSAlex Lorenz void CoverageMappingGen::emitCounterMapping(const Decl *D, 1152ee02499aSAlex Lorenz llvm::raw_ostream &OS) { 1153ee02499aSAlex Lorenz assert(CounterMap); 1154e5ee6c58SJustin Bogner CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts); 1155ee02499aSAlex Lorenz Walker.VisitDecl(D); 1156ee02499aSAlex Lorenz Walker.write(OS); 1157ee02499aSAlex Lorenz } 1158ee02499aSAlex Lorenz 1159ee02499aSAlex Lorenz void CoverageMappingGen::emitEmptyMapping(const Decl *D, 1160ee02499aSAlex Lorenz llvm::raw_ostream &OS) { 1161ee02499aSAlex Lorenz EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts); 1162ee02499aSAlex Lorenz Walker.VisitDecl(D); 1163ee02499aSAlex Lorenz Walker.write(OS); 1164ee02499aSAlex Lorenz } 1165