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