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