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" 18*bf42cfd7SJustin Bogner #include "llvm/ADT/Optional.h" 19ee02499aSAlex Lorenz #include "llvm/ProfileData/CoverageMapping.h" 20f2cf38e0SAlex Lorenz #include "llvm/ProfileData/CoverageMappingReader.h" 210d9593ddSChandler Carruth #include "llvm/ProfileData/CoverageMappingWriter.h" 220d9593ddSChandler Carruth #include "llvm/ProfileData/InstrProfReader.h" 23ee02499aSAlex Lorenz #include "llvm/Support/FileSystem.h" 24ee02499aSAlex Lorenz 25ee02499aSAlex Lorenz using namespace clang; 26ee02499aSAlex Lorenz using namespace CodeGen; 27ee02499aSAlex Lorenz using namespace llvm::coverage; 28ee02499aSAlex Lorenz 29ee02499aSAlex Lorenz void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range) { 30ee02499aSAlex Lorenz SkippedRanges.push_back(Range); 31ee02499aSAlex Lorenz } 32ee02499aSAlex Lorenz 33ee02499aSAlex Lorenz namespace { 34ee02499aSAlex Lorenz 35ee02499aSAlex Lorenz /// \brief A region of source code that can be mapped to a counter. 3609c7179bSJustin Bogner class SourceMappingRegion { 37ee02499aSAlex Lorenz Counter Count; 38ee02499aSAlex Lorenz 39ee02499aSAlex Lorenz /// \brief The region's starting location. 40*bf42cfd7SJustin Bogner Optional<SourceLocation> LocStart; 41ee02499aSAlex Lorenz 42ee02499aSAlex Lorenz /// \brief The region's ending location. 43*bf42cfd7SJustin Bogner Optional<SourceLocation> LocEnd; 44ee02499aSAlex Lorenz 4509c7179bSJustin Bogner public: 46*bf42cfd7SJustin Bogner SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart, 47*bf42cfd7SJustin Bogner Optional<SourceLocation> LocEnd) 48*bf42cfd7SJustin Bogner : Count(Count), LocStart(LocStart), LocEnd(LocEnd) {} 49ee02499aSAlex Lorenz 50*bf42cfd7SJustin Bogner SourceMappingRegion(SourceMappingRegion &&Region) 51*bf42cfd7SJustin Bogner : Count(std::move(Region.Count)), LocStart(std::move(Region.LocStart)), 52*bf42cfd7SJustin Bogner LocEnd(std::move(Region.LocEnd)) {} 53*bf42cfd7SJustin Bogner 54*bf42cfd7SJustin Bogner SourceMappingRegion &operator=(SourceMappingRegion &&RHS) { 55*bf42cfd7SJustin Bogner Count = std::move(RHS.Count); 56*bf42cfd7SJustin Bogner LocStart = std::move(RHS.LocStart); 57*bf42cfd7SJustin Bogner LocEnd = std::move(RHS.LocEnd); 58*bf42cfd7SJustin Bogner return *this; 59*bf42cfd7SJustin Bogner } 6009c7179bSJustin Bogner 6109c7179bSJustin Bogner const Counter &getCounter() const { return Count; } 6209c7179bSJustin Bogner 63*bf42cfd7SJustin Bogner void setCounter(Counter C) { Count = C; } 6409c7179bSJustin Bogner 65*bf42cfd7SJustin Bogner bool hasStartLoc() const { return LocStart.hasValue(); } 66*bf42cfd7SJustin Bogner 67*bf42cfd7SJustin Bogner void setStartLoc(SourceLocation Loc) { LocStart = Loc; } 68*bf42cfd7SJustin Bogner 69*bf42cfd7SJustin Bogner const SourceLocation &getStartLoc() const { 70*bf42cfd7SJustin Bogner assert(LocStart && "Region has no start location"); 71*bf42cfd7SJustin Bogner return *LocStart; 7209c7179bSJustin Bogner } 7309c7179bSJustin Bogner 74*bf42cfd7SJustin Bogner bool hasEndLoc() const { return LocEnd.hasValue(); } 75ee02499aSAlex Lorenz 76*bf42cfd7SJustin Bogner void setEndLoc(SourceLocation Loc) { LocEnd = Loc; } 77ee02499aSAlex Lorenz 78*bf42cfd7SJustin Bogner const SourceLocation &getEndLoc() const { 79*bf42cfd7SJustin Bogner assert(LocEnd && "Region has no end location"); 80*bf42cfd7SJustin Bogner return *LocEnd; 81ee02499aSAlex Lorenz } 82ee02499aSAlex Lorenz }; 83ee02499aSAlex Lorenz 84ee02499aSAlex Lorenz /// \brief Provides the common functionality for the different 85ee02499aSAlex Lorenz /// coverage mapping region builders. 86ee02499aSAlex Lorenz class CoverageMappingBuilder { 87ee02499aSAlex Lorenz public: 88ee02499aSAlex Lorenz CoverageMappingModuleGen &CVM; 89ee02499aSAlex Lorenz SourceManager &SM; 90ee02499aSAlex Lorenz const LangOptions &LangOpts; 91ee02499aSAlex Lorenz 92ee02499aSAlex Lorenz private: 93*bf42cfd7SJustin Bogner /// \brief Map of clang's FileIDs to IDs used for coverage mapping. 94*bf42cfd7SJustin Bogner llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8> 95*bf42cfd7SJustin Bogner FileIDMapping; 96ee02499aSAlex Lorenz 97ee02499aSAlex Lorenz public: 98ee02499aSAlex Lorenz /// \brief The coverage mapping regions for this function 99ee02499aSAlex Lorenz llvm::SmallVector<CounterMappingRegion, 32> MappingRegions; 100ee02499aSAlex Lorenz /// \brief The source mapping regions for this function. 101f59329b0SJustin Bogner std::vector<SourceMappingRegion> SourceRegions; 102ee02499aSAlex Lorenz 103ee02499aSAlex Lorenz CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM, 104ee02499aSAlex Lorenz const LangOptions &LangOpts) 105*bf42cfd7SJustin Bogner : CVM(CVM), SM(SM), LangOpts(LangOpts) {} 106ee02499aSAlex Lorenz 107ee02499aSAlex Lorenz /// \brief Return the precise end location for the given token. 108ee02499aSAlex Lorenz SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) { 109*bf42cfd7SJustin Bogner // We avoid getLocForEndOfToken here, because it doesn't do what we want for 110*bf42cfd7SJustin Bogner // macro locations, which we just treat as expanded files. 111*bf42cfd7SJustin Bogner unsigned TokLen = 112*bf42cfd7SJustin Bogner Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts); 113*bf42cfd7SJustin Bogner return Loc.getLocWithOffset(TokLen); 114ee02499aSAlex Lorenz } 115ee02499aSAlex Lorenz 116*bf42cfd7SJustin Bogner /// \brief Return the start location of an included file or expanded macro. 117*bf42cfd7SJustin Bogner SourceLocation getStartOfFileOrMacro(SourceLocation Loc) { 118*bf42cfd7SJustin Bogner if (Loc.isMacroID()) 119*bf42cfd7SJustin Bogner return Loc.getLocWithOffset(-SM.getFileOffset(Loc)); 120*bf42cfd7SJustin Bogner return SM.getLocForStartOfFile(SM.getFileID(Loc)); 121ee02499aSAlex Lorenz } 122ee02499aSAlex Lorenz 123*bf42cfd7SJustin Bogner /// \brief Return the end location of an included file or expanded macro. 124*bf42cfd7SJustin Bogner SourceLocation getEndOfFileOrMacro(SourceLocation Loc) { 125*bf42cfd7SJustin Bogner if (Loc.isMacroID()) 126*bf42cfd7SJustin Bogner return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) - 127*bf42cfd7SJustin Bogner SM.getFileOffset(Loc) - 1); 128*bf42cfd7SJustin Bogner return SM.getLocForEndOfFile(SM.getFileID(Loc)); 129*bf42cfd7SJustin Bogner } 130ee02499aSAlex Lorenz 131*bf42cfd7SJustin Bogner /// \brief Find out where the current file is included or macro is expanded. 132*bf42cfd7SJustin Bogner SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) { 133*bf42cfd7SJustin Bogner return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).first 134*bf42cfd7SJustin Bogner : SM.getIncludeLoc(SM.getFileID(Loc)); 135*bf42cfd7SJustin Bogner } 136*bf42cfd7SJustin Bogner 137*bf42cfd7SJustin Bogner /// \brief Get the start of \c S ignoring macro argument locations. 138*bf42cfd7SJustin Bogner SourceLocation getStart(const Stmt *S) { 139*bf42cfd7SJustin Bogner SourceLocation Loc = S->getLocStart(); 140*bf42cfd7SJustin Bogner while (SM.isMacroArgExpansion(Loc)) 141*bf42cfd7SJustin Bogner Loc = SM.getImmediateExpansionRange(Loc).first; 142*bf42cfd7SJustin Bogner return Loc; 143*bf42cfd7SJustin Bogner } 144*bf42cfd7SJustin Bogner 145*bf42cfd7SJustin Bogner /// \brief Get the end of \c S ignoring macro argument locations. 146*bf42cfd7SJustin Bogner SourceLocation getEnd(const Stmt *S) { 147*bf42cfd7SJustin Bogner SourceLocation Loc = S->getLocEnd(); 148*bf42cfd7SJustin Bogner while (SM.isMacroArgExpansion(Loc)) 149*bf42cfd7SJustin Bogner Loc = SM.getImmediateExpansionRange(Loc).first; 150*bf42cfd7SJustin Bogner return Loc; 151*bf42cfd7SJustin Bogner } 152*bf42cfd7SJustin Bogner 153*bf42cfd7SJustin Bogner /// \brief Find the set of files we have regions for and assign IDs 154*bf42cfd7SJustin Bogner /// 155*bf42cfd7SJustin Bogner /// Fills \c Mapping with the virtual file mapping needed to write out 156*bf42cfd7SJustin Bogner /// coverage and collects the necessary file information to emit source and 157*bf42cfd7SJustin Bogner /// expansion regions. 158*bf42cfd7SJustin Bogner void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) { 159*bf42cfd7SJustin Bogner FileIDMapping.clear(); 160*bf42cfd7SJustin Bogner 161*bf42cfd7SJustin Bogner SmallVector<FileID, 8> Visited; 162*bf42cfd7SJustin Bogner SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs; 163*bf42cfd7SJustin Bogner for (const auto &Region : SourceRegions) { 164*bf42cfd7SJustin Bogner SourceLocation Loc = Region.getStartLoc(); 165*bf42cfd7SJustin Bogner FileID File = SM.getFileID(Loc); 166*bf42cfd7SJustin Bogner if (std::find(Visited.begin(), Visited.end(), File) != Visited.end()) 167*bf42cfd7SJustin Bogner continue; 168*bf42cfd7SJustin Bogner Visited.push_back(File); 169*bf42cfd7SJustin Bogner 170*bf42cfd7SJustin Bogner unsigned Depth = 0; 171*bf42cfd7SJustin Bogner for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc); 172*bf42cfd7SJustin Bogner !Parent.isInvalid(); Parent = getIncludeOrExpansionLoc(Parent)) 173*bf42cfd7SJustin Bogner ++Depth; 174*bf42cfd7SJustin Bogner FileLocs.push_back(std::make_pair(Loc, Depth)); 175*bf42cfd7SJustin Bogner } 176*bf42cfd7SJustin Bogner std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second()); 177*bf42cfd7SJustin Bogner 178*bf42cfd7SJustin Bogner for (const auto &FL : FileLocs) { 179*bf42cfd7SJustin Bogner SourceLocation Loc = FL.first; 180*bf42cfd7SJustin Bogner FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first; 181ee02499aSAlex Lorenz auto Entry = SM.getFileEntryForID(SpellingFile); 182ee02499aSAlex Lorenz if (!Entry) 183*bf42cfd7SJustin Bogner continue; 184ee02499aSAlex Lorenz 185*bf42cfd7SJustin Bogner FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc); 186*bf42cfd7SJustin Bogner Mapping.push_back(CVM.getFileID(Entry)); 187*bf42cfd7SJustin Bogner } 188ee02499aSAlex Lorenz } 189ee02499aSAlex Lorenz 190*bf42cfd7SJustin Bogner /// \brief Get the coverage mapping file ID for \c Loc. 191*bf42cfd7SJustin Bogner /// 192*bf42cfd7SJustin Bogner /// If such file id doesn't exist, return None. 193*bf42cfd7SJustin Bogner Optional<unsigned> getCoverageFileID(SourceLocation Loc) { 194*bf42cfd7SJustin Bogner auto Mapping = FileIDMapping.find(SM.getFileID(Loc)); 195*bf42cfd7SJustin Bogner if (Mapping != FileIDMapping.end()) 196*bf42cfd7SJustin Bogner return Mapping->second.first; 197903678caSJustin Bogner return None; 198ee02499aSAlex Lorenz } 199ee02499aSAlex Lorenz 200ee02499aSAlex Lorenz /// \brief Return true if the given clang's file id has a corresponding 201ee02499aSAlex Lorenz /// coverage file id. 202ee02499aSAlex Lorenz bool hasExistingCoverageFileID(FileID File) const { 203ee02499aSAlex Lorenz return FileIDMapping.count(File); 204ee02499aSAlex Lorenz } 205ee02499aSAlex Lorenz 206ee02499aSAlex Lorenz /// \brief Gather all the regions that were skipped by the preprocessor 207ee02499aSAlex Lorenz /// using the constructs like #if. 208ee02499aSAlex Lorenz void gatherSkippedRegions() { 209ee02499aSAlex Lorenz /// An array of the minimum lineStarts and the maximum lineEnds 210ee02499aSAlex Lorenz /// for mapping regions from the appropriate source files. 211ee02499aSAlex Lorenz llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges; 212ee02499aSAlex Lorenz FileLineRanges.resize( 213ee02499aSAlex Lorenz FileIDMapping.size(), 214ee02499aSAlex Lorenz std::make_pair(std::numeric_limits<unsigned>::max(), 0)); 215ee02499aSAlex Lorenz for (const auto &R : MappingRegions) { 216ee02499aSAlex Lorenz FileLineRanges[R.FileID].first = 217ee02499aSAlex Lorenz std::min(FileLineRanges[R.FileID].first, R.LineStart); 218ee02499aSAlex Lorenz FileLineRanges[R.FileID].second = 219ee02499aSAlex Lorenz std::max(FileLineRanges[R.FileID].second, R.LineEnd); 220ee02499aSAlex Lorenz } 221ee02499aSAlex Lorenz 222ee02499aSAlex Lorenz auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges(); 223ee02499aSAlex Lorenz for (const auto &I : SkippedRanges) { 224ee02499aSAlex Lorenz auto LocStart = I.getBegin(); 225ee02499aSAlex Lorenz auto LocEnd = I.getEnd(); 226*bf42cfd7SJustin Bogner assert(SM.isWrittenInSameFile(LocStart, LocEnd) && 227*bf42cfd7SJustin Bogner "region spans multiple files"); 228ee02499aSAlex Lorenz 229*bf42cfd7SJustin Bogner auto CovFileID = getCoverageFileID(LocStart); 230903678caSJustin Bogner if (!CovFileID) 231ee02499aSAlex Lorenz continue; 232ee02499aSAlex Lorenz unsigned LineStart = SM.getSpellingLineNumber(LocStart); 233ee02499aSAlex Lorenz unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart); 234ee02499aSAlex Lorenz unsigned LineEnd = SM.getSpellingLineNumber(LocEnd); 235ee02499aSAlex Lorenz unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd); 236fd34280bSJustin Bogner auto Region = CounterMappingRegion::makeSkipped( 237fd34280bSJustin Bogner *CovFileID, LineStart, ColumnStart, LineEnd, ColumnEnd); 238ee02499aSAlex Lorenz // Make sure that we only collect the regions that are inside 239ee02499aSAlex Lorenz // the souce code of this function. 240903678caSJustin Bogner if (Region.LineStart >= FileLineRanges[*CovFileID].first && 241903678caSJustin Bogner Region.LineEnd <= FileLineRanges[*CovFileID].second) 242ee02499aSAlex Lorenz MappingRegions.push_back(Region); 243ee02499aSAlex Lorenz } 244ee02499aSAlex Lorenz } 245ee02499aSAlex Lorenz 246ee02499aSAlex Lorenz /// \brief Generate the coverage counter mapping regions from collected 247ee02499aSAlex Lorenz /// source regions. 248ee02499aSAlex Lorenz void emitSourceRegions() { 249*bf42cfd7SJustin Bogner for (const auto &Region : SourceRegions) { 250*bf42cfd7SJustin Bogner assert(Region.hasEndLoc() && "incomplete region"); 251ee02499aSAlex Lorenz 252*bf42cfd7SJustin Bogner SourceLocation LocStart = Region.getStartLoc(); 253*bf42cfd7SJustin Bogner assert(!SM.getFileID(LocStart).isInvalid() && "region in invalid file"); 254f59329b0SJustin Bogner 255*bf42cfd7SJustin Bogner auto CovFileID = getCoverageFileID(LocStart); 256*bf42cfd7SJustin Bogner // Ignore regions that don't have a file, such as builtin macros. 257*bf42cfd7SJustin Bogner if (!CovFileID) 258ee02499aSAlex Lorenz continue; 259ee02499aSAlex Lorenz 260*bf42cfd7SJustin Bogner SourceLocation LocEnd = getPreciseTokenLocEnd(Region.getEndLoc()); 261*bf42cfd7SJustin Bogner assert(SM.isWrittenInSameFile(LocStart, LocEnd) && 262*bf42cfd7SJustin Bogner "region spans multiple files"); 263*bf42cfd7SJustin Bogner 264f59329b0SJustin Bogner // Find the spilling locations for the mapping region. 265ee02499aSAlex Lorenz unsigned LineStart = SM.getSpellingLineNumber(LocStart); 266ee02499aSAlex Lorenz unsigned ColumnStart = SM.getSpellingColumnNumber(LocStart); 267ee02499aSAlex Lorenz unsigned LineEnd = SM.getSpellingLineNumber(LocEnd); 268ee02499aSAlex Lorenz unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd); 269ee02499aSAlex Lorenz 270*bf42cfd7SJustin Bogner assert(LineStart <= LineEnd && "region start and end out of order"); 271*bf42cfd7SJustin Bogner MappingRegions.push_back(CounterMappingRegion::makeRegion( 272*bf42cfd7SJustin Bogner Region.getCounter(), *CovFileID, LineStart, ColumnStart, LineEnd, 273*bf42cfd7SJustin Bogner ColumnEnd)); 274*bf42cfd7SJustin Bogner } 275*bf42cfd7SJustin Bogner } 276*bf42cfd7SJustin Bogner 277*bf42cfd7SJustin Bogner /// \brief Generate expansion regions for each virtual file we've seen. 278*bf42cfd7SJustin Bogner void emitExpansionRegions() { 279*bf42cfd7SJustin Bogner for (const auto &FM : FileIDMapping) { 280*bf42cfd7SJustin Bogner SourceLocation ExpandedLoc = FM.second.second; 281*bf42cfd7SJustin Bogner SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc); 282*bf42cfd7SJustin Bogner if (ParentLoc.isInvalid()) 283ee02499aSAlex Lorenz continue; 284ee02499aSAlex Lorenz 285*bf42cfd7SJustin Bogner auto ParentFileID = getCoverageFileID(ParentLoc); 286*bf42cfd7SJustin Bogner if (!ParentFileID) 287*bf42cfd7SJustin Bogner continue; 288*bf42cfd7SJustin Bogner auto ExpandedFileID = getCoverageFileID(ExpandedLoc); 289*bf42cfd7SJustin Bogner assert(ExpandedFileID && "expansion in uncovered file"); 290*bf42cfd7SJustin Bogner 291*bf42cfd7SJustin Bogner SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc); 292*bf42cfd7SJustin Bogner assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) && 293*bf42cfd7SJustin Bogner "region spans multiple files"); 294*bf42cfd7SJustin Bogner 295*bf42cfd7SJustin Bogner unsigned LineStart = SM.getSpellingLineNumber(ParentLoc); 296*bf42cfd7SJustin Bogner unsigned ColumnStart = SM.getSpellingColumnNumber(ParentLoc); 297*bf42cfd7SJustin Bogner unsigned LineEnd = SM.getSpellingLineNumber(LocEnd); 298*bf42cfd7SJustin Bogner unsigned ColumnEnd = SM.getSpellingColumnNumber(LocEnd); 299*bf42cfd7SJustin Bogner 300*bf42cfd7SJustin Bogner MappingRegions.push_back(CounterMappingRegion::makeExpansion( 301*bf42cfd7SJustin Bogner *ParentFileID, *ExpandedFileID, LineStart, ColumnStart, LineEnd, 302fd34280bSJustin Bogner ColumnEnd)); 303ee02499aSAlex Lorenz } 304ee02499aSAlex Lorenz } 305ee02499aSAlex Lorenz }; 306ee02499aSAlex Lorenz 307ee02499aSAlex Lorenz /// \brief Creates unreachable coverage regions for the functions that 308ee02499aSAlex Lorenz /// are not emitted. 309ee02499aSAlex Lorenz struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder { 310ee02499aSAlex Lorenz EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM, 311ee02499aSAlex Lorenz const LangOptions &LangOpts) 312ee02499aSAlex Lorenz : CoverageMappingBuilder(CVM, SM, LangOpts) {} 313ee02499aSAlex Lorenz 314ee02499aSAlex Lorenz void VisitDecl(const Decl *D) { 315ee02499aSAlex Lorenz if (!D->hasBody()) 316ee02499aSAlex Lorenz return; 317ee02499aSAlex Lorenz auto Body = D->getBody(); 318*bf42cfd7SJustin Bogner SourceRegions.emplace_back(Counter(), getStart(Body), getEnd(Body)); 319ee02499aSAlex Lorenz } 320ee02499aSAlex Lorenz 321ee02499aSAlex Lorenz /// \brief Write the mapping data to the output stream 322ee02499aSAlex Lorenz void write(llvm::raw_ostream &OS) { 323ee02499aSAlex Lorenz SmallVector<unsigned, 16> FileIDMapping; 324*bf42cfd7SJustin Bogner gatherFileIDs(FileIDMapping); 325*bf42cfd7SJustin Bogner emitSourceRegions(); 326ee02499aSAlex Lorenz 3275fc8fc2dSCraig Topper CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions); 328ee02499aSAlex Lorenz Writer.write(OS); 329ee02499aSAlex Lorenz } 330ee02499aSAlex Lorenz }; 331ee02499aSAlex Lorenz 332ee02499aSAlex Lorenz /// \brief A StmtVisitor that creates coverage mapping regions which map 333ee02499aSAlex Lorenz /// from the source code locations to the PGO counters. 334ee02499aSAlex Lorenz struct CounterCoverageMappingBuilder 335ee02499aSAlex Lorenz : public CoverageMappingBuilder, 336ee02499aSAlex Lorenz public ConstStmtVisitor<CounterCoverageMappingBuilder> { 337ee02499aSAlex Lorenz /// \brief The map of statements to count values. 338ee02499aSAlex Lorenz llvm::DenseMap<const Stmt *, unsigned> &CounterMap; 339ee02499aSAlex Lorenz 340*bf42cfd7SJustin Bogner /// \brief A stack of currently live regions. 341*bf42cfd7SJustin Bogner std::vector<SourceMappingRegion> RegionStack; 342ee02499aSAlex Lorenz 343ee02499aSAlex Lorenz CounterExpressionBuilder Builder; 344ee02499aSAlex Lorenz 345*bf42cfd7SJustin Bogner /// \brief A location in the most recently visited file or macro. 346*bf42cfd7SJustin Bogner /// 347*bf42cfd7SJustin Bogner /// This is used to adjust the active source regions appropriately when 348*bf42cfd7SJustin Bogner /// expressions cross file or macro boundaries. 349*bf42cfd7SJustin Bogner SourceLocation MostRecentLocation; 350*bf42cfd7SJustin Bogner 351*bf42cfd7SJustin Bogner /// \brief Return a counter for the subtraction of \c RHS from \c LHS 352ee02499aSAlex Lorenz Counter subtractCounters(Counter LHS, Counter RHS) { 353ee02499aSAlex Lorenz return Builder.subtract(LHS, RHS); 354ee02499aSAlex Lorenz } 355ee02499aSAlex Lorenz 356*bf42cfd7SJustin Bogner /// \brief Return a counter for the sum of \c LHS and \c RHS. 357ee02499aSAlex Lorenz Counter addCounters(Counter LHS, Counter RHS) { 358ee02499aSAlex Lorenz return Builder.add(LHS, RHS); 359ee02499aSAlex Lorenz } 360ee02499aSAlex Lorenz 361*bf42cfd7SJustin Bogner Counter addCounters(Counter C1, Counter C2, Counter C3) { 362*bf42cfd7SJustin Bogner return addCounters(addCounters(C1, C2), C3); 363*bf42cfd7SJustin Bogner } 364*bf42cfd7SJustin Bogner 365*bf42cfd7SJustin Bogner Counter addCounters(Counter C1, Counter C2, Counter C3, Counter C4) { 366*bf42cfd7SJustin Bogner return addCounters(addCounters(C1, C2, C3), C4); 367*bf42cfd7SJustin Bogner } 368*bf42cfd7SJustin Bogner 369ee02499aSAlex Lorenz /// \brief Return the region counter for the given statement. 370*bf42cfd7SJustin Bogner /// 371ee02499aSAlex Lorenz /// This should only be called on statements that have a dedicated counter. 372*bf42cfd7SJustin Bogner Counter getRegionCounter(const Stmt *S) { 373*bf42cfd7SJustin Bogner return Counter::getCounter(CounterMap[S]); 374ee02499aSAlex Lorenz } 375ee02499aSAlex Lorenz 376*bf42cfd7SJustin Bogner /// \brief Push a region onto the stack. 377*bf42cfd7SJustin Bogner /// 378*bf42cfd7SJustin Bogner /// Returns the index on the stack where the region was pushed. This can be 379*bf42cfd7SJustin Bogner /// used with popRegions to exit a "scope", ending the region that was pushed. 380*bf42cfd7SJustin Bogner size_t pushRegion(Counter Count, Optional<SourceLocation> StartLoc = None, 381*bf42cfd7SJustin Bogner Optional<SourceLocation> EndLoc = None) { 382*bf42cfd7SJustin Bogner if (StartLoc) 383*bf42cfd7SJustin Bogner MostRecentLocation = *StartLoc; 384*bf42cfd7SJustin Bogner RegionStack.emplace_back(Count, StartLoc, EndLoc); 385ee02499aSAlex Lorenz 386*bf42cfd7SJustin Bogner return RegionStack.size() - 1; 387ee02499aSAlex Lorenz } 388ee02499aSAlex Lorenz 389*bf42cfd7SJustin Bogner /// \brief Pop regions from the stack into the function's list of regions. 390*bf42cfd7SJustin Bogner /// 391*bf42cfd7SJustin Bogner /// Adds all regions from \c ParentIndex to the top of the stack to the 392*bf42cfd7SJustin Bogner /// function's \c SourceRegions. 393*bf42cfd7SJustin Bogner void popRegions(size_t ParentIndex) { 394*bf42cfd7SJustin Bogner assert(RegionStack.size() >= ParentIndex && "parent not in stack"); 395*bf42cfd7SJustin Bogner while (RegionStack.size() > ParentIndex) { 396*bf42cfd7SJustin Bogner SourceMappingRegion &Region = RegionStack.back(); 397*bf42cfd7SJustin Bogner if (Region.hasStartLoc()) { 398*bf42cfd7SJustin Bogner SourceLocation StartLoc = Region.getStartLoc(); 399*bf42cfd7SJustin Bogner SourceLocation EndLoc = Region.hasEndLoc() 400*bf42cfd7SJustin Bogner ? Region.getEndLoc() 401*bf42cfd7SJustin Bogner : RegionStack[ParentIndex].getEndLoc(); 402*bf42cfd7SJustin Bogner while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) { 403*bf42cfd7SJustin Bogner // The region ends in a nested file or macro expansion. Create a 404*bf42cfd7SJustin Bogner // separate region for each expansion. 405*bf42cfd7SJustin Bogner SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc); 406*bf42cfd7SJustin Bogner assert(SM.isWrittenInSameFile(NestedLoc, EndLoc)); 407*bf42cfd7SJustin Bogner 408*bf42cfd7SJustin Bogner SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc); 409*bf42cfd7SJustin Bogner 410*bf42cfd7SJustin Bogner EndLoc = getIncludeOrExpansionLoc(EndLoc); 411*bf42cfd7SJustin Bogner assert(!EndLoc.isInvalid() && 412*bf42cfd7SJustin Bogner "File exit was not handled before popRegions"); 413*bf42cfd7SJustin Bogner } 414*bf42cfd7SJustin Bogner Region.setEndLoc(EndLoc); 415*bf42cfd7SJustin Bogner 416*bf42cfd7SJustin Bogner MostRecentLocation = EndLoc; 417*bf42cfd7SJustin Bogner // If this region happens to span an entire expansion, we need to make 418*bf42cfd7SJustin Bogner // sure we don't overlap the parent region with it. 419*bf42cfd7SJustin Bogner if (StartLoc == getStartOfFileOrMacro(StartLoc) && 420*bf42cfd7SJustin Bogner EndLoc == getEndOfFileOrMacro(EndLoc)) 421*bf42cfd7SJustin Bogner MostRecentLocation = getIncludeOrExpansionLoc(EndLoc); 422*bf42cfd7SJustin Bogner 423*bf42cfd7SJustin Bogner assert(SM.isWrittenInSameFile(Region.getStartLoc(), EndLoc)); 424*bf42cfd7SJustin Bogner SourceRegions.push_back(std::move(Region)); 425*bf42cfd7SJustin Bogner } 426*bf42cfd7SJustin Bogner RegionStack.pop_back(); 427*bf42cfd7SJustin Bogner } 428ee02499aSAlex Lorenz } 429ee02499aSAlex Lorenz 430*bf42cfd7SJustin Bogner /// \brief Return the currently active region. 431*bf42cfd7SJustin Bogner SourceMappingRegion &getRegion() { 432*bf42cfd7SJustin Bogner assert(!RegionStack.empty() && "statement has no region"); 433*bf42cfd7SJustin Bogner return RegionStack.back(); 434ee02499aSAlex Lorenz } 435ee02499aSAlex Lorenz 436*bf42cfd7SJustin Bogner /// \brief Propagate counts through the children of \c S. 437*bf42cfd7SJustin Bogner Counter propagateCounts(Counter TopCount, const Stmt *S) { 438*bf42cfd7SJustin Bogner size_t Index = pushRegion(TopCount, getStart(S), getEnd(S)); 439*bf42cfd7SJustin Bogner Visit(S); 440*bf42cfd7SJustin Bogner Counter ExitCount = getRegion().getCounter(); 441*bf42cfd7SJustin Bogner popRegions(Index); 442*bf42cfd7SJustin Bogner return ExitCount; 443ee02499aSAlex Lorenz } 444ee02499aSAlex Lorenz 445*bf42cfd7SJustin Bogner /// \brief Adjust the most recently visited location to \c EndLoc. 446*bf42cfd7SJustin Bogner /// 447*bf42cfd7SJustin Bogner /// This should be used after visiting any statements in non-source order. 448*bf42cfd7SJustin Bogner void adjustForOutOfOrderTraversal(SourceLocation EndLoc) { 449*bf42cfd7SJustin Bogner MostRecentLocation = EndLoc; 450*bf42cfd7SJustin Bogner if (MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation)) 451*bf42cfd7SJustin Bogner MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation); 452ee02499aSAlex Lorenz } 453ee02499aSAlex Lorenz 454*bf42cfd7SJustin Bogner /// \brief Check whether \c Loc is included or expanded from \c Parent. 455*bf42cfd7SJustin Bogner bool isNestedIn(SourceLocation Loc, FileID Parent) { 456*bf42cfd7SJustin Bogner do { 457*bf42cfd7SJustin Bogner Loc = getIncludeOrExpansionLoc(Loc); 458*bf42cfd7SJustin Bogner if (Loc.isInvalid()) 459*bf42cfd7SJustin Bogner return false; 460*bf42cfd7SJustin Bogner } while (!SM.isInFileID(Loc, Parent)); 461*bf42cfd7SJustin Bogner return true; 462ee02499aSAlex Lorenz } 463ee02499aSAlex Lorenz 464*bf42cfd7SJustin Bogner /// \brief Adjust regions and state when \c NewLoc exits a file. 465*bf42cfd7SJustin Bogner /// 466*bf42cfd7SJustin Bogner /// If moving from our most recently tracked location to \c NewLoc exits any 467*bf42cfd7SJustin Bogner /// files, this adjusts our current region stack and creates the file regions 468*bf42cfd7SJustin Bogner /// for the exited file. 469*bf42cfd7SJustin Bogner void handleFileExit(SourceLocation NewLoc) { 470*bf42cfd7SJustin Bogner if (SM.isWrittenInSameFile(MostRecentLocation, NewLoc)) 471*bf42cfd7SJustin Bogner return; 472*bf42cfd7SJustin Bogner 473*bf42cfd7SJustin Bogner // If NewLoc is not in a file that contains MostRecentLocation, walk up to 474*bf42cfd7SJustin Bogner // find the common ancestor. 475*bf42cfd7SJustin Bogner SourceLocation LCA = NewLoc; 476*bf42cfd7SJustin Bogner FileID ParentFile = SM.getFileID(LCA); 477*bf42cfd7SJustin Bogner while (!isNestedIn(MostRecentLocation, ParentFile)) { 478*bf42cfd7SJustin Bogner LCA = getIncludeOrExpansionLoc(LCA); 479*bf42cfd7SJustin Bogner if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) { 480*bf42cfd7SJustin Bogner // Since there isn't a common ancestor, no file was exited. We just need 481*bf42cfd7SJustin Bogner // to adjust our location to the new file. 482*bf42cfd7SJustin Bogner MostRecentLocation = NewLoc; 483*bf42cfd7SJustin Bogner return; 484*bf42cfd7SJustin Bogner } 485*bf42cfd7SJustin Bogner ParentFile = SM.getFileID(LCA); 486ee02499aSAlex Lorenz } 487ee02499aSAlex Lorenz 488*bf42cfd7SJustin Bogner llvm::SmallSet<SourceLocation, 8> StartLocs; 489*bf42cfd7SJustin Bogner Optional<Counter> ParentCounter; 490*bf42cfd7SJustin Bogner for (auto I = RegionStack.rbegin(), E = RegionStack.rend(); I != E; ++I) { 491*bf42cfd7SJustin Bogner if (!I->hasStartLoc()) 492*bf42cfd7SJustin Bogner continue; 493*bf42cfd7SJustin Bogner SourceLocation Loc = I->getStartLoc(); 494*bf42cfd7SJustin Bogner if (!isNestedIn(Loc, ParentFile)) { 495*bf42cfd7SJustin Bogner ParentCounter = I->getCounter(); 496*bf42cfd7SJustin Bogner break; 497ee02499aSAlex Lorenz } 498*bf42cfd7SJustin Bogner 499*bf42cfd7SJustin Bogner while (!SM.isInFileID(Loc, ParentFile)) { 500*bf42cfd7SJustin Bogner // The most nested region for each start location is the one with the 501*bf42cfd7SJustin Bogner // correct count. We avoid creating redundant regions by stopping once 502*bf42cfd7SJustin Bogner // we've seen this region. 503*bf42cfd7SJustin Bogner if (StartLocs.insert(Loc).second) 504*bf42cfd7SJustin Bogner SourceRegions.emplace_back(I->getCounter(), Loc, 505*bf42cfd7SJustin Bogner getEndOfFileOrMacro(Loc)); 506*bf42cfd7SJustin Bogner Loc = getIncludeOrExpansionLoc(Loc); 507ee02499aSAlex Lorenz } 508*bf42cfd7SJustin Bogner I->setStartLoc(getPreciseTokenLocEnd(Loc)); 509*bf42cfd7SJustin Bogner } 510*bf42cfd7SJustin Bogner 511*bf42cfd7SJustin Bogner if (ParentCounter) { 512*bf42cfd7SJustin Bogner // If the file is contained completely by another region and doesn't 513*bf42cfd7SJustin Bogner // immediately start its own region, the whole file gets a region 514*bf42cfd7SJustin Bogner // corresponding to the parent. 515*bf42cfd7SJustin Bogner SourceLocation Loc = MostRecentLocation; 516*bf42cfd7SJustin Bogner while (isNestedIn(Loc, ParentFile)) { 517*bf42cfd7SJustin Bogner SourceLocation FileStart = getStartOfFileOrMacro(Loc); 518*bf42cfd7SJustin Bogner if (StartLocs.insert(FileStart).second) 519*bf42cfd7SJustin Bogner SourceRegions.emplace_back(*ParentCounter, FileStart, 520*bf42cfd7SJustin Bogner getEndOfFileOrMacro(Loc)); 521*bf42cfd7SJustin Bogner Loc = getIncludeOrExpansionLoc(Loc); 522*bf42cfd7SJustin Bogner } 523*bf42cfd7SJustin Bogner } 524*bf42cfd7SJustin Bogner 525*bf42cfd7SJustin Bogner MostRecentLocation = NewLoc; 526*bf42cfd7SJustin Bogner } 527*bf42cfd7SJustin Bogner 528*bf42cfd7SJustin Bogner /// \brief Ensure that \c S is included in the current region. 529*bf42cfd7SJustin Bogner void extendRegion(const Stmt *S) { 530*bf42cfd7SJustin Bogner SourceMappingRegion &Region = getRegion(); 531*bf42cfd7SJustin Bogner SourceLocation StartLoc = getStart(S); 532*bf42cfd7SJustin Bogner 533*bf42cfd7SJustin Bogner handleFileExit(StartLoc); 534*bf42cfd7SJustin Bogner if (!Region.hasStartLoc()) 535*bf42cfd7SJustin Bogner Region.setStartLoc(StartLoc); 536*bf42cfd7SJustin Bogner } 537*bf42cfd7SJustin Bogner 538*bf42cfd7SJustin Bogner /// \brief Mark \c S as a terminator, starting a zero region. 539*bf42cfd7SJustin Bogner void terminateRegion(const Stmt *S) { 540*bf42cfd7SJustin Bogner extendRegion(S); 541*bf42cfd7SJustin Bogner SourceMappingRegion &Region = getRegion(); 542*bf42cfd7SJustin Bogner if (!Region.hasEndLoc()) 543*bf42cfd7SJustin Bogner Region.setEndLoc(getEnd(S)); 544*bf42cfd7SJustin Bogner pushRegion(Counter::getZero()); 545*bf42cfd7SJustin Bogner } 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 llvm::SmallVector<unsigned, 8> VirtualFileMapping; 563*bf42cfd7SJustin Bogner gatherFileIDs(VirtualFileMapping); 564*bf42cfd7SJustin Bogner emitSourceRegions(); 565*bf42cfd7SJustin Bogner emitExpansionRegions(); 566ee02499aSAlex Lorenz gatherSkippedRegions(); 567ee02499aSAlex Lorenz 5684da909b2SJustin Bogner CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(), 5694da909b2SJustin Bogner MappingRegions); 570ee02499aSAlex Lorenz Writer.write(OS); 571ee02499aSAlex Lorenz } 572ee02499aSAlex Lorenz 573ee02499aSAlex Lorenz void VisitStmt(const Stmt *S) { 574*bf42cfd7SJustin Bogner if (!S->getLocStart().isInvalid()) 575*bf42cfd7SJustin Bogner extendRegion(S); 576ee02499aSAlex Lorenz for (Stmt::const_child_range I = S->children(); I; ++I) { 577ee02499aSAlex Lorenz if (*I) 578ee02499aSAlex Lorenz this->Visit(*I); 579ee02499aSAlex Lorenz } 580*bf42cfd7SJustin Bogner handleFileExit(getEnd(S)); 581ee02499aSAlex Lorenz } 582ee02499aSAlex Lorenz 583ee02499aSAlex Lorenz void VisitDecl(const Decl *D) { 584*bf42cfd7SJustin Bogner Stmt *Body = D->getBody(); 585*bf42cfd7SJustin Bogner propagateCounts(getRegionCounter(Body), Body); 586ee02499aSAlex Lorenz } 587ee02499aSAlex Lorenz 588ee02499aSAlex Lorenz void VisitReturnStmt(const ReturnStmt *S) { 589*bf42cfd7SJustin Bogner extendRegion(S); 590ee02499aSAlex Lorenz if (S->getRetValue()) 591ee02499aSAlex Lorenz Visit(S->getRetValue()); 592*bf42cfd7SJustin Bogner terminateRegion(S); 593ee02499aSAlex Lorenz } 594ee02499aSAlex Lorenz 595*bf42cfd7SJustin Bogner void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); } 596ee02499aSAlex Lorenz 597ee02499aSAlex Lorenz void VisitLabelStmt(const LabelStmt *S) { 598*bf42cfd7SJustin Bogner SourceLocation Start = getStart(S); 599*bf42cfd7SJustin Bogner // We can't extendRegion here or we risk overlapping with our new region. 600*bf42cfd7SJustin Bogner handleFileExit(Start); 601*bf42cfd7SJustin Bogner pushRegion(getRegionCounter(S), Start); 602ee02499aSAlex Lorenz Visit(S->getSubStmt()); 603ee02499aSAlex Lorenz } 604ee02499aSAlex Lorenz 605ee02499aSAlex Lorenz void VisitBreakStmt(const BreakStmt *S) { 606ee02499aSAlex Lorenz assert(!BreakContinueStack.empty() && "break not in a loop or switch!"); 607ee02499aSAlex Lorenz BreakContinueStack.back().BreakCount = addCounters( 608*bf42cfd7SJustin Bogner BreakContinueStack.back().BreakCount, getRegion().getCounter()); 609*bf42cfd7SJustin Bogner terminateRegion(S); 610ee02499aSAlex Lorenz } 611ee02499aSAlex Lorenz 612ee02499aSAlex Lorenz void VisitContinueStmt(const ContinueStmt *S) { 613ee02499aSAlex Lorenz assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); 614ee02499aSAlex Lorenz BreakContinueStack.back().ContinueCount = addCounters( 615*bf42cfd7SJustin Bogner BreakContinueStack.back().ContinueCount, getRegion().getCounter()); 616*bf42cfd7SJustin Bogner terminateRegion(S); 617ee02499aSAlex Lorenz } 618ee02499aSAlex Lorenz 619ee02499aSAlex Lorenz void VisitWhileStmt(const WhileStmt *S) { 620*bf42cfd7SJustin Bogner extendRegion(S); 621ee02499aSAlex Lorenz 622*bf42cfd7SJustin Bogner Counter ParentCount = getRegion().getCounter(); 623*bf42cfd7SJustin Bogner Counter BodyCount = getRegionCounter(S); 624*bf42cfd7SJustin Bogner 625*bf42cfd7SJustin Bogner // Handle the body first so that we can get the backedge count. 626*bf42cfd7SJustin Bogner BreakContinueStack.push_back(BreakContinue()); 627*bf42cfd7SJustin Bogner extendRegion(S->getBody()); 628*bf42cfd7SJustin Bogner Counter BackedgeCount = propagateCounts(BodyCount, S->getBody()); 629ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 630*bf42cfd7SJustin Bogner 631*bf42cfd7SJustin Bogner // Go back to handle the condition. 632*bf42cfd7SJustin Bogner Counter CondCount = 633*bf42cfd7SJustin Bogner addCounters(ParentCount, BackedgeCount, BC.ContinueCount); 634*bf42cfd7SJustin Bogner propagateCounts(CondCount, S->getCond()); 635*bf42cfd7SJustin Bogner adjustForOutOfOrderTraversal(getEnd(S)); 636*bf42cfd7SJustin Bogner 637*bf42cfd7SJustin Bogner Counter OutCount = 638*bf42cfd7SJustin Bogner addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount)); 639*bf42cfd7SJustin Bogner if (OutCount != ParentCount) 640*bf42cfd7SJustin Bogner pushRegion(OutCount); 641ee02499aSAlex Lorenz } 642ee02499aSAlex Lorenz 643ee02499aSAlex Lorenz void VisitDoStmt(const DoStmt *S) { 644*bf42cfd7SJustin Bogner extendRegion(S); 645ee02499aSAlex Lorenz 646*bf42cfd7SJustin Bogner Counter ParentCount = getRegion().getCounter(); 647*bf42cfd7SJustin Bogner Counter BodyCount = getRegionCounter(S); 648*bf42cfd7SJustin Bogner 649*bf42cfd7SJustin Bogner BreakContinueStack.push_back(BreakContinue()); 650*bf42cfd7SJustin Bogner extendRegion(S->getBody()); 651*bf42cfd7SJustin Bogner Counter BackedgeCount = 652*bf42cfd7SJustin Bogner propagateCounts(addCounters(ParentCount, BodyCount), S->getBody()); 653ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 654*bf42cfd7SJustin Bogner 655*bf42cfd7SJustin Bogner Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount); 656*bf42cfd7SJustin Bogner propagateCounts(CondCount, S->getCond()); 657*bf42cfd7SJustin Bogner 658*bf42cfd7SJustin Bogner Counter OutCount = 659*bf42cfd7SJustin Bogner addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount)); 660*bf42cfd7SJustin Bogner if (OutCount != ParentCount) 661*bf42cfd7SJustin Bogner pushRegion(OutCount); 662ee02499aSAlex Lorenz } 663ee02499aSAlex Lorenz 664ee02499aSAlex Lorenz void VisitForStmt(const ForStmt *S) { 665*bf42cfd7SJustin Bogner extendRegion(S); 666ee02499aSAlex Lorenz if (S->getInit()) 667ee02499aSAlex Lorenz Visit(S->getInit()); 668ee02499aSAlex Lorenz 669*bf42cfd7SJustin Bogner Counter ParentCount = getRegion().getCounter(); 670*bf42cfd7SJustin Bogner Counter BodyCount = getRegionCounter(S); 671*bf42cfd7SJustin Bogner 672*bf42cfd7SJustin Bogner // Handle the body first so that we can get the backedge count. 673ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 674*bf42cfd7SJustin Bogner extendRegion(S->getBody()); 675*bf42cfd7SJustin Bogner Counter BackedgeCount = propagateCounts(BodyCount, S->getBody()); 676*bf42cfd7SJustin Bogner BreakContinue BC = BreakContinueStack.pop_back_val(); 677ee02499aSAlex Lorenz 678ee02499aSAlex Lorenz // The increment is essentially part of the body but it needs to include 679ee02499aSAlex Lorenz // the count for all the continue statements. 680*bf42cfd7SJustin Bogner if (const Stmt *Inc = S->getInc()) 681*bf42cfd7SJustin Bogner propagateCounts(addCounters(BackedgeCount, BC.ContinueCount), Inc); 682*bf42cfd7SJustin Bogner 683*bf42cfd7SJustin Bogner // Go back to handle the condition. 684*bf42cfd7SJustin Bogner Counter CondCount = 685*bf42cfd7SJustin Bogner addCounters(ParentCount, BackedgeCount, BC.ContinueCount); 686*bf42cfd7SJustin Bogner if (const Expr *Cond = S->getCond()) { 687*bf42cfd7SJustin Bogner propagateCounts(CondCount, Cond); 688*bf42cfd7SJustin Bogner adjustForOutOfOrderTraversal(getEnd(S)); 689ee02499aSAlex Lorenz } 690ee02499aSAlex Lorenz 691*bf42cfd7SJustin Bogner Counter OutCount = 692*bf42cfd7SJustin Bogner addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount)); 693*bf42cfd7SJustin Bogner if (OutCount != ParentCount) 694*bf42cfd7SJustin Bogner pushRegion(OutCount); 695ee02499aSAlex Lorenz } 696ee02499aSAlex Lorenz 697ee02499aSAlex Lorenz void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { 698*bf42cfd7SJustin Bogner extendRegion(S); 699*bf42cfd7SJustin Bogner Visit(S->getLoopVarStmt()); 700ee02499aSAlex Lorenz Visit(S->getRangeStmt()); 701*bf42cfd7SJustin Bogner 702*bf42cfd7SJustin Bogner Counter ParentCount = getRegion().getCounter(); 703*bf42cfd7SJustin Bogner Counter BodyCount = getRegionCounter(S); 704*bf42cfd7SJustin Bogner 705ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 706*bf42cfd7SJustin Bogner extendRegion(S->getBody()); 707*bf42cfd7SJustin Bogner Counter BackedgeCount = propagateCounts(BodyCount, S->getBody()); 708ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 709*bf42cfd7SJustin Bogner 710*bf42cfd7SJustin Bogner Counter OutCount = addCounters(ParentCount, BC.BreakCount, BC.ContinueCount, 711*bf42cfd7SJustin Bogner subtractCounters(BodyCount, BackedgeCount)); 712*bf42cfd7SJustin Bogner if (OutCount != ParentCount) 713*bf42cfd7SJustin Bogner pushRegion(OutCount); 714ee02499aSAlex Lorenz } 715ee02499aSAlex Lorenz 716ee02499aSAlex Lorenz void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { 717*bf42cfd7SJustin Bogner extendRegion(S); 718ee02499aSAlex Lorenz Visit(S->getElement()); 719*bf42cfd7SJustin Bogner 720*bf42cfd7SJustin Bogner Counter ParentCount = getRegion().getCounter(); 721*bf42cfd7SJustin Bogner Counter BodyCount = getRegionCounter(S); 722*bf42cfd7SJustin Bogner 723ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 724*bf42cfd7SJustin Bogner extendRegion(S->getBody()); 725*bf42cfd7SJustin Bogner Counter BackedgeCount = propagateCounts(BodyCount, S->getBody()); 726ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 727*bf42cfd7SJustin Bogner 728*bf42cfd7SJustin Bogner Counter OutCount = addCounters(ParentCount, BC.BreakCount, BC.ContinueCount, 729*bf42cfd7SJustin Bogner subtractCounters(BodyCount, BackedgeCount)); 730*bf42cfd7SJustin Bogner if (OutCount != ParentCount) 731*bf42cfd7SJustin Bogner pushRegion(OutCount); 732ee02499aSAlex Lorenz } 733ee02499aSAlex Lorenz 734ee02499aSAlex Lorenz void VisitSwitchStmt(const SwitchStmt *S) { 735*bf42cfd7SJustin Bogner extendRegion(S); 736ee02499aSAlex Lorenz Visit(S->getCond()); 737*bf42cfd7SJustin Bogner 738ee02499aSAlex Lorenz BreakContinueStack.push_back(BreakContinue()); 739*bf42cfd7SJustin Bogner 740*bf42cfd7SJustin Bogner const Stmt *Body = S->getBody(); 741*bf42cfd7SJustin Bogner extendRegion(Body); 742*bf42cfd7SJustin Bogner if (const auto *CS = dyn_cast<CompoundStmt>(Body)) { 743*bf42cfd7SJustin Bogner if (!CS->body_empty()) { 744*bf42cfd7SJustin Bogner // The body of the switch needs a zero region so that fallthrough counts 745*bf42cfd7SJustin Bogner // behave correctly, but it would be misleading to include the braces of 746*bf42cfd7SJustin Bogner // the compound statement in the zeroed area, so we need to handle this 747*bf42cfd7SJustin Bogner // specially. 748*bf42cfd7SJustin Bogner size_t Index = 749*bf42cfd7SJustin Bogner pushRegion(Counter::getZero(), getStart(CS->body_front()), 750*bf42cfd7SJustin Bogner getEnd(CS->body_back())); 751*bf42cfd7SJustin Bogner for (const auto &Child : CS->children()) 752*bf42cfd7SJustin Bogner Visit(Child); 753*bf42cfd7SJustin Bogner popRegions(Index); 754ee02499aSAlex Lorenz } 755*bf42cfd7SJustin Bogner } else 756*bf42cfd7SJustin Bogner propagateCounts(Counter::getZero(), Body); 757ee02499aSAlex Lorenz BreakContinue BC = BreakContinueStack.pop_back_val(); 758*bf42cfd7SJustin Bogner 759ee02499aSAlex Lorenz if (!BreakContinueStack.empty()) 760ee02499aSAlex Lorenz BreakContinueStack.back().ContinueCount = addCounters( 761ee02499aSAlex Lorenz BreakContinueStack.back().ContinueCount, BC.ContinueCount); 762*bf42cfd7SJustin Bogner 763*bf42cfd7SJustin Bogner Counter ExitCount = getRegionCounter(S); 764*bf42cfd7SJustin Bogner pushRegion(ExitCount); 765ee02499aSAlex Lorenz } 766ee02499aSAlex Lorenz 767*bf42cfd7SJustin Bogner void VisitSwitchCase(const SwitchCase *S) { 768*bf42cfd7SJustin Bogner extendRegion(S); 769ee02499aSAlex Lorenz 770*bf42cfd7SJustin Bogner SourceMappingRegion &Parent = getRegion(); 771*bf42cfd7SJustin Bogner 772*bf42cfd7SJustin Bogner Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S)); 773*bf42cfd7SJustin Bogner // Reuse the existing region if it starts at our label. This is typical of 774*bf42cfd7SJustin Bogner // the first case in a switch. 775*bf42cfd7SJustin Bogner if (Parent.hasStartLoc() && Parent.getStartLoc() == getStart(S)) 776*bf42cfd7SJustin Bogner Parent.setCounter(Count); 777*bf42cfd7SJustin Bogner else 778*bf42cfd7SJustin Bogner pushRegion(Count, getStart(S)); 779*bf42cfd7SJustin Bogner 780*bf42cfd7SJustin Bogner if (const CaseStmt *CS = dyn_cast<CaseStmt>(S)) { 781*bf42cfd7SJustin Bogner Visit(CS->getLHS()); 782*bf42cfd7SJustin Bogner if (const Expr *RHS = CS->getRHS()) 783*bf42cfd7SJustin Bogner Visit(RHS); 784*bf42cfd7SJustin Bogner } 785ee02499aSAlex Lorenz Visit(S->getSubStmt()); 786ee02499aSAlex Lorenz } 787ee02499aSAlex Lorenz 788ee02499aSAlex Lorenz void VisitIfStmt(const IfStmt *S) { 789*bf42cfd7SJustin Bogner extendRegion(S); 790ee02499aSAlex Lorenz Visit(S->getCond()); 791ee02499aSAlex Lorenz 792*bf42cfd7SJustin Bogner Counter ParentCount = getRegion().getCounter(); 793*bf42cfd7SJustin Bogner Counter ThenCount = getRegionCounter(S); 794ee02499aSAlex Lorenz 795*bf42cfd7SJustin Bogner extendRegion(S->getThen()); 796*bf42cfd7SJustin Bogner Counter OutCount = propagateCounts(ThenCount, S->getThen()); 797*bf42cfd7SJustin Bogner 798*bf42cfd7SJustin Bogner Counter ElseCount = subtractCounters(ParentCount, ThenCount); 799*bf42cfd7SJustin Bogner if (const Stmt *Else = S->getElse()) { 800*bf42cfd7SJustin Bogner extendRegion(S->getElse()); 801*bf42cfd7SJustin Bogner OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else)); 802*bf42cfd7SJustin Bogner } else 803*bf42cfd7SJustin Bogner OutCount = addCounters(OutCount, ElseCount); 804*bf42cfd7SJustin Bogner 805*bf42cfd7SJustin Bogner if (OutCount != ParentCount) 806*bf42cfd7SJustin Bogner pushRegion(OutCount); 807ee02499aSAlex Lorenz } 808ee02499aSAlex Lorenz 809ee02499aSAlex Lorenz void VisitCXXTryStmt(const CXXTryStmt *S) { 810*bf42cfd7SJustin Bogner extendRegion(S); 811ee02499aSAlex Lorenz Visit(S->getTryBlock()); 812ee02499aSAlex Lorenz for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I) 813ee02499aSAlex Lorenz Visit(S->getHandler(I)); 814*bf42cfd7SJustin Bogner 815*bf42cfd7SJustin Bogner Counter ExitCount = getRegionCounter(S); 816*bf42cfd7SJustin Bogner pushRegion(ExitCount); 817ee02499aSAlex Lorenz } 818ee02499aSAlex Lorenz 819ee02499aSAlex Lorenz void VisitCXXCatchStmt(const CXXCatchStmt *S) { 820*bf42cfd7SJustin Bogner extendRegion(S); 821*bf42cfd7SJustin Bogner propagateCounts(getRegionCounter(S), S->getHandlerBlock()); 822ee02499aSAlex Lorenz } 823ee02499aSAlex Lorenz 824ee02499aSAlex Lorenz void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 825*bf42cfd7SJustin Bogner extendRegion(E); 826ee02499aSAlex Lorenz 827*bf42cfd7SJustin Bogner Counter ParentCount = getRegion().getCounter(); 828*bf42cfd7SJustin Bogner Counter TrueCount = getRegionCounter(E); 829ee02499aSAlex Lorenz 830*bf42cfd7SJustin Bogner propagateCounts(TrueCount, E->getTrueExpr()); 831*bf42cfd7SJustin Bogner propagateCounts(subtractCounters(ParentCount, TrueCount), 832*bf42cfd7SJustin Bogner E->getFalseExpr()); 833ee02499aSAlex Lorenz } 834ee02499aSAlex Lorenz 835ee02499aSAlex Lorenz void VisitBinLAnd(const BinaryOperator *E) { 836*bf42cfd7SJustin Bogner extendRegion(E); 837ee02499aSAlex Lorenz Visit(E->getLHS()); 838*bf42cfd7SJustin Bogner 839*bf42cfd7SJustin Bogner extendRegion(E->getRHS()); 840*bf42cfd7SJustin Bogner propagateCounts(getRegionCounter(E), E->getRHS()); 841ee02499aSAlex Lorenz } 842ee02499aSAlex Lorenz 843ee02499aSAlex Lorenz void VisitBinLOr(const BinaryOperator *E) { 844*bf42cfd7SJustin Bogner extendRegion(E); 845ee02499aSAlex Lorenz Visit(E->getLHS()); 846ee02499aSAlex Lorenz 847*bf42cfd7SJustin Bogner extendRegion(E->getRHS()); 848*bf42cfd7SJustin Bogner propagateCounts(getRegionCounter(E), E->getRHS()); 84901a0d062SAlex Lorenz } 850ee02499aSAlex Lorenz }; 851ee02499aSAlex Lorenz } 852ee02499aSAlex Lorenz 853ee02499aSAlex Lorenz static bool isMachO(const CodeGenModule &CGM) { 854ee02499aSAlex Lorenz return CGM.getTarget().getTriple().isOSBinFormatMachO(); 855ee02499aSAlex Lorenz } 856ee02499aSAlex Lorenz 857ee02499aSAlex Lorenz static StringRef getCoverageSection(const CodeGenModule &CGM) { 858ee02499aSAlex Lorenz return isMachO(CGM) ? "__DATA,__llvm_covmap" : "__llvm_covmap"; 859ee02499aSAlex Lorenz } 860ee02499aSAlex Lorenz 861a432d176SJustin Bogner static void dump(llvm::raw_ostream &OS, StringRef FunctionName, 862a432d176SJustin Bogner ArrayRef<CounterExpression> Expressions, 863a432d176SJustin Bogner ArrayRef<CounterMappingRegion> Regions) { 864a432d176SJustin Bogner OS << FunctionName << ":\n"; 865a432d176SJustin Bogner CounterMappingContext Ctx(Expressions); 866a432d176SJustin Bogner for (const auto &R : Regions) { 867f2cf38e0SAlex Lorenz OS.indent(2); 868f2cf38e0SAlex Lorenz switch (R.Kind) { 869f2cf38e0SAlex Lorenz case CounterMappingRegion::CodeRegion: 870f2cf38e0SAlex Lorenz break; 871f2cf38e0SAlex Lorenz case CounterMappingRegion::ExpansionRegion: 872f2cf38e0SAlex Lorenz OS << "Expansion,"; 873f2cf38e0SAlex Lorenz break; 874f2cf38e0SAlex Lorenz case CounterMappingRegion::SkippedRegion: 875f2cf38e0SAlex Lorenz OS << "Skipped,"; 876f2cf38e0SAlex Lorenz break; 877f2cf38e0SAlex Lorenz } 878f2cf38e0SAlex Lorenz 8794da909b2SJustin Bogner OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart 8804da909b2SJustin Bogner << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = "; 881f69dc349SJustin Bogner Ctx.dump(R.Count, OS); 882f2cf38e0SAlex Lorenz if (R.Kind == CounterMappingRegion::ExpansionRegion) 8834da909b2SJustin Bogner OS << " (Expanded file = " << R.ExpandedFileID << ")"; 8844da909b2SJustin Bogner OS << "\n"; 885f2cf38e0SAlex Lorenz } 886f2cf38e0SAlex Lorenz } 887f2cf38e0SAlex Lorenz 888ee02499aSAlex Lorenz void CoverageMappingModuleGen::addFunctionMappingRecord( 889f2cf38e0SAlex Lorenz llvm::GlobalVariable *FunctionName, StringRef FunctionNameValue, 8901d45c5bcSAlex Lorenz uint64_t FunctionHash, const std::string &CoverageMapping) { 891ee02499aSAlex Lorenz llvm::LLVMContext &Ctx = CGM.getLLVMContext(); 892ee02499aSAlex Lorenz auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); 8931d45c5bcSAlex Lorenz auto *Int64Ty = llvm::Type::getInt64Ty(Ctx); 894ee02499aSAlex Lorenz auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx); 895ee02499aSAlex Lorenz if (!FunctionRecordTy) { 8961d45c5bcSAlex Lorenz llvm::Type *FunctionRecordTypes[] = {Int8PtrTy, Int32Ty, Int32Ty, Int64Ty}; 897ee02499aSAlex Lorenz FunctionRecordTy = 898ee02499aSAlex Lorenz llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes)); 899ee02499aSAlex Lorenz } 900ee02499aSAlex Lorenz 901ee02499aSAlex Lorenz llvm::Constant *FunctionRecordVals[] = { 902ee02499aSAlex Lorenz llvm::ConstantExpr::getBitCast(FunctionName, Int8PtrTy), 903f2cf38e0SAlex Lorenz llvm::ConstantInt::get(Int32Ty, FunctionNameValue.size()), 9041d45c5bcSAlex Lorenz llvm::ConstantInt::get(Int32Ty, CoverageMapping.size()), 9051d45c5bcSAlex Lorenz llvm::ConstantInt::get(Int64Ty, FunctionHash)}; 906ee02499aSAlex Lorenz FunctionRecords.push_back(llvm::ConstantStruct::get( 907ee02499aSAlex Lorenz FunctionRecordTy, makeArrayRef(FunctionRecordVals))); 908ee02499aSAlex Lorenz CoverageMappings += CoverageMapping; 909f2cf38e0SAlex Lorenz 910f2cf38e0SAlex Lorenz if (CGM.getCodeGenOpts().DumpCoverageMapping) { 911f2cf38e0SAlex Lorenz // Dump the coverage mapping data for this function by decoding the 912f2cf38e0SAlex Lorenz // encoded data. This allows us to dump the mapping regions which were 913f2cf38e0SAlex Lorenz // also processed by the CoverageMappingWriter which performs 914f2cf38e0SAlex Lorenz // additional minimization operations such as reducing the number of 915f2cf38e0SAlex Lorenz // expressions. 916f2cf38e0SAlex Lorenz std::vector<StringRef> Filenames; 917f2cf38e0SAlex Lorenz std::vector<CounterExpression> Expressions; 918f2cf38e0SAlex Lorenz std::vector<CounterMappingRegion> Regions; 919f2cf38e0SAlex Lorenz llvm::SmallVector<StringRef, 16> FilenameRefs; 920f2cf38e0SAlex Lorenz FilenameRefs.resize(FileEntries.size()); 921f2cf38e0SAlex Lorenz for (const auto &Entry : FileEntries) 922f2cf38e0SAlex Lorenz FilenameRefs[Entry.second] = Entry.first->getName(); 923a432d176SJustin Bogner RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames, 924a432d176SJustin Bogner Expressions, Regions); 925a432d176SJustin Bogner if (Reader.read()) 926f2cf38e0SAlex Lorenz return; 927a432d176SJustin Bogner dump(llvm::outs(), FunctionNameValue, Expressions, Regions); 928f2cf38e0SAlex Lorenz } 929ee02499aSAlex Lorenz } 930ee02499aSAlex Lorenz 931ee02499aSAlex Lorenz void CoverageMappingModuleGen::emit() { 932ee02499aSAlex Lorenz if (FunctionRecords.empty()) 933ee02499aSAlex Lorenz return; 934ee02499aSAlex Lorenz llvm::LLVMContext &Ctx = CGM.getLLVMContext(); 935ee02499aSAlex Lorenz auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); 936ee02499aSAlex Lorenz 937ee02499aSAlex Lorenz // Create the filenames and merge them with coverage mappings 938ee02499aSAlex Lorenz llvm::SmallVector<std::string, 16> FilenameStrs; 939ee02499aSAlex Lorenz llvm::SmallVector<StringRef, 16> FilenameRefs; 940ee02499aSAlex Lorenz FilenameStrs.resize(FileEntries.size()); 941ee02499aSAlex Lorenz FilenameRefs.resize(FileEntries.size()); 942ee02499aSAlex Lorenz for (const auto &Entry : FileEntries) { 943ee02499aSAlex Lorenz llvm::SmallString<256> Path(Entry.first->getName()); 944ee02499aSAlex Lorenz llvm::sys::fs::make_absolute(Path); 945ee02499aSAlex Lorenz 946ee02499aSAlex Lorenz auto I = Entry.second; 947ee02499aSAlex Lorenz FilenameStrs[I] = std::move(std::string(Path.begin(), Path.end())); 948ee02499aSAlex Lorenz FilenameRefs[I] = FilenameStrs[I]; 949ee02499aSAlex Lorenz } 950ee02499aSAlex Lorenz 951ee02499aSAlex Lorenz std::string FilenamesAndCoverageMappings; 952ee02499aSAlex Lorenz llvm::raw_string_ostream OS(FilenamesAndCoverageMappings); 953ee02499aSAlex Lorenz CoverageFilenamesSectionWriter(FilenameRefs).write(OS); 954ee02499aSAlex Lorenz OS << CoverageMappings; 955ee02499aSAlex Lorenz size_t CoverageMappingSize = CoverageMappings.size(); 956ee02499aSAlex Lorenz size_t FilenamesSize = OS.str().size() - CoverageMappingSize; 957ee02499aSAlex Lorenz // Append extra zeroes if necessary to ensure that the size of the filenames 958ee02499aSAlex Lorenz // and coverage mappings is a multiple of 8. 959ee02499aSAlex Lorenz if (size_t Rem = OS.str().size() % 8) { 960ee02499aSAlex Lorenz CoverageMappingSize += 8 - Rem; 961ee02499aSAlex Lorenz for (size_t I = 0, S = 8 - Rem; I < S; ++I) 962ee02499aSAlex Lorenz OS << '\0'; 963ee02499aSAlex Lorenz } 964ee02499aSAlex Lorenz auto *FilenamesAndMappingsVal = 965ee02499aSAlex Lorenz llvm::ConstantDataArray::getString(Ctx, OS.str(), false); 966ee02499aSAlex Lorenz 967ee02499aSAlex Lorenz // Create the deferred function records array 968ee02499aSAlex Lorenz auto RecordsTy = 969ee02499aSAlex Lorenz llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size()); 970ee02499aSAlex Lorenz auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords); 971ee02499aSAlex Lorenz 972ee02499aSAlex Lorenz // Create the coverage data record 973ee02499aSAlex Lorenz llvm::Type *CovDataTypes[] = {Int32Ty, Int32Ty, 974ee02499aSAlex Lorenz Int32Ty, Int32Ty, 975ee02499aSAlex Lorenz RecordsTy, FilenamesAndMappingsVal->getType()}; 976ee02499aSAlex Lorenz auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes)); 977ee02499aSAlex Lorenz llvm::Constant *TUDataVals[] = { 978ee02499aSAlex Lorenz llvm::ConstantInt::get(Int32Ty, FunctionRecords.size()), 979ee02499aSAlex Lorenz llvm::ConstantInt::get(Int32Ty, FilenamesSize), 980ee02499aSAlex Lorenz llvm::ConstantInt::get(Int32Ty, CoverageMappingSize), 981ee02499aSAlex Lorenz llvm::ConstantInt::get(Int32Ty, 982ee02499aSAlex Lorenz /*Version=*/CoverageMappingVersion1), 983ee02499aSAlex Lorenz RecordsVal, FilenamesAndMappingsVal}; 984ee02499aSAlex Lorenz auto CovDataVal = 985ee02499aSAlex Lorenz llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals)); 986ee02499aSAlex Lorenz auto CovData = new llvm::GlobalVariable(CGM.getModule(), CovDataTy, true, 987ee02499aSAlex Lorenz llvm::GlobalValue::InternalLinkage, 988ee02499aSAlex Lorenz CovDataVal, 989ee02499aSAlex Lorenz "__llvm_coverage_mapping"); 990ee02499aSAlex Lorenz 991ee02499aSAlex Lorenz CovData->setSection(getCoverageSection(CGM)); 992ee02499aSAlex Lorenz CovData->setAlignment(8); 993ee02499aSAlex Lorenz 994ee02499aSAlex Lorenz // Make sure the data doesn't get deleted. 995ee02499aSAlex Lorenz CGM.addUsedGlobal(CovData); 996ee02499aSAlex Lorenz } 997ee02499aSAlex Lorenz 998ee02499aSAlex Lorenz unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) { 999ee02499aSAlex Lorenz auto It = FileEntries.find(File); 1000ee02499aSAlex Lorenz if (It != FileEntries.end()) 1001ee02499aSAlex Lorenz return It->second; 1002ee02499aSAlex Lorenz unsigned FileID = FileEntries.size(); 1003ee02499aSAlex Lorenz FileEntries.insert(std::make_pair(File, FileID)); 1004ee02499aSAlex Lorenz return FileID; 1005ee02499aSAlex Lorenz } 1006ee02499aSAlex Lorenz 1007ee02499aSAlex Lorenz void CoverageMappingGen::emitCounterMapping(const Decl *D, 1008ee02499aSAlex Lorenz llvm::raw_ostream &OS) { 1009ee02499aSAlex Lorenz assert(CounterMap); 1010e5ee6c58SJustin Bogner CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts); 1011ee02499aSAlex Lorenz Walker.VisitDecl(D); 1012ee02499aSAlex Lorenz Walker.write(OS); 1013ee02499aSAlex Lorenz } 1014ee02499aSAlex Lorenz 1015ee02499aSAlex Lorenz void CoverageMappingGen::emitEmptyMapping(const Decl *D, 1016ee02499aSAlex Lorenz llvm::raw_ostream &OS) { 1017ee02499aSAlex Lorenz EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts); 1018ee02499aSAlex Lorenz Walker.VisitDecl(D); 1019ee02499aSAlex Lorenz Walker.write(OS); 1020ee02499aSAlex Lorenz } 1021