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