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