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