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