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