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