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