Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 1 | //===- CoverageMapping.cpp - Code coverage mapping support ----------------===// |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains support for clang's and llvm's instrumentation based |
| 11 | // code coverage. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/ProfileData/Coverage/CoverageMapping.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/None.h" |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Optional.h" |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallBitVector.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
Easwaran Raman | dc70712 | 2016-04-29 18:53:05 +0000 | [diff] [blame] | 23 | #include "llvm/ProfileData/Coverage/CoverageMappingReader.h" |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 24 | #include "llvm/ProfileData/InstrProfReader.h" |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Rafael Espindola | 74f2932 | 2015-06-13 17:23:04 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Errc.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Error.h" |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ManagedStatic.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | #include <cassert> |
| 34 | #include <cstdint> |
| 35 | #include <iterator> |
| 36 | #include <memory> |
| 37 | #include <string> |
| 38 | #include <system_error> |
| 39 | #include <utility> |
| 40 | #include <vector> |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 41 | |
| 42 | using namespace llvm; |
| 43 | using namespace coverage; |
| 44 | |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 45 | #define DEBUG_TYPE "coverage-mapping" |
| 46 | |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 47 | Counter CounterExpressionBuilder::get(const CounterExpression &E) { |
Justin Bogner | ad69e64 | 2014-10-02 17:14:18 +0000 | [diff] [blame] | 48 | auto It = ExpressionIndices.find(E); |
| 49 | if (It != ExpressionIndices.end()) |
| 50 | return Counter::getExpression(It->second); |
| 51 | unsigned I = Expressions.size(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 52 | Expressions.push_back(E); |
Justin Bogner | ad69e64 | 2014-10-02 17:14:18 +0000 | [diff] [blame] | 53 | ExpressionIndices[E] = I; |
| 54 | return Counter::getExpression(I); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 57 | void CounterExpressionBuilder::extractTerms(Counter C, int Factor, |
| 58 | SmallVectorImpl<Term> &Terms) { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 59 | switch (C.getKind()) { |
| 60 | case Counter::Zero: |
| 61 | break; |
| 62 | case Counter::CounterValueReference: |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 63 | Terms.emplace_back(C.getCounterID(), Factor); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 64 | break; |
| 65 | case Counter::Expression: |
| 66 | const auto &E = Expressions[C.getExpressionID()]; |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 67 | extractTerms(E.LHS, Factor, Terms); |
| 68 | extractTerms( |
| 69 | E.RHS, E.Kind == CounterExpression::Subtract ? -Factor : Factor, Terms); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) { |
| 75 | // Gather constant terms. |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 76 | SmallVector<Term, 32> Terms; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 77 | extractTerms(ExpressionTree, +1, Terms); |
| 78 | |
| 79 | // If there are no terms, this is just a zero. The algorithm below assumes at |
| 80 | // least one term. |
| 81 | if (Terms.size() == 0) |
| 82 | return Counter::getZero(); |
| 83 | |
| 84 | // Group the terms by counter ID. |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 85 | std::sort(Terms.begin(), Terms.end(), [](const Term &LHS, const Term &RHS) { |
| 86 | return LHS.CounterID < RHS.CounterID; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 87 | }); |
| 88 | |
| 89 | // Combine terms by counter ID to eliminate counters that sum to zero. |
| 90 | auto Prev = Terms.begin(); |
| 91 | for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) { |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 92 | if (I->CounterID == Prev->CounterID) { |
| 93 | Prev->Factor += I->Factor; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 94 | continue; |
| 95 | } |
| 96 | ++Prev; |
| 97 | *Prev = *I; |
| 98 | } |
| 99 | Terms.erase(++Prev, Terms.end()); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 100 | |
| 101 | Counter C; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 102 | // Create additions. We do this before subtractions to avoid constructs like |
| 103 | // ((0 - X) + Y), as opposed to (Y - X). |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 104 | for (auto T : Terms) { |
| 105 | if (T.Factor <= 0) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 106 | continue; |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 107 | for (int I = 0; I < T.Factor; ++I) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 108 | if (C.isZero()) |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 109 | C = Counter::getCounter(T.CounterID); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 110 | else |
| 111 | C = get(CounterExpression(CounterExpression::Add, C, |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 112 | Counter::getCounter(T.CounterID))); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Create subtractions. |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 116 | for (auto T : Terms) { |
| 117 | if (T.Factor >= 0) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 118 | continue; |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 119 | for (int I = 0; I < -T.Factor; ++I) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 120 | C = get(CounterExpression(CounterExpression::Subtract, C, |
Vedant Kumar | 71b3d72 | 2017-06-26 22:33:06 +0000 | [diff] [blame] | 121 | Counter::getCounter(T.CounterID))); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 122 | } |
| 123 | return C; |
| 124 | } |
| 125 | |
| 126 | Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) { |
| 127 | return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS))); |
| 128 | } |
| 129 | |
| 130 | Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) { |
| 131 | return simplify( |
| 132 | get(CounterExpression(CounterExpression::Subtract, LHS, RHS))); |
| 133 | } |
| 134 | |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 135 | void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 136 | switch (C.getKind()) { |
| 137 | case Counter::Zero: |
| 138 | OS << '0'; |
| 139 | return; |
| 140 | case Counter::CounterValueReference: |
| 141 | OS << '#' << C.getCounterID(); |
| 142 | break; |
| 143 | case Counter::Expression: { |
| 144 | if (C.getExpressionID() >= Expressions.size()) |
| 145 | return; |
| 146 | const auto &E = Expressions[C.getExpressionID()]; |
| 147 | OS << '('; |
Alex Lorenz | a422911c | 2014-07-29 19:58:16 +0000 | [diff] [blame] | 148 | dump(E.LHS, OS); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 149 | OS << (E.Kind == CounterExpression::Subtract ? " - " : " + "); |
Alex Lorenz | a422911c | 2014-07-29 19:58:16 +0000 | [diff] [blame] | 150 | dump(E.RHS, OS); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 151 | OS << ')'; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | if (CounterValues.empty()) |
| 156 | return; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 157 | Expected<int64_t> Value = evaluate(C); |
| 158 | if (auto E = Value.takeError()) { |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 159 | consumeError(std::move(E)); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 160 | return; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 161 | } |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 162 | OS << '[' << *Value << ']'; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 165 | Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 166 | switch (C.getKind()) { |
| 167 | case Counter::Zero: |
| 168 | return 0; |
| 169 | case Counter::CounterValueReference: |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 170 | if (C.getCounterID() >= CounterValues.size()) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 171 | return errorCodeToError(errc::argument_out_of_domain); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 172 | return CounterValues[C.getCounterID()]; |
| 173 | case Counter::Expression: { |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 174 | if (C.getExpressionID() >= Expressions.size()) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 175 | return errorCodeToError(errc::argument_out_of_domain); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 176 | const auto &E = Expressions[C.getExpressionID()]; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 177 | Expected<int64_t> LHS = evaluate(E.LHS); |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 178 | if (!LHS) |
| 179 | return LHS; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 180 | Expected<int64_t> RHS = evaluate(E.RHS); |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 181 | if (!RHS) |
| 182 | return RHS; |
| 183 | return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 186 | llvm_unreachable("Unhandled CounterKind"); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 187 | } |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 188 | |
Justin Bogner | d5fca92 | 2014-11-14 01:50:32 +0000 | [diff] [blame] | 189 | void FunctionRecordIterator::skipOtherFiles() { |
| 190 | while (Current != Records.end() && !Filename.empty() && |
| 191 | Filename != Current->Filenames[0]) |
| 192 | ++Current; |
| 193 | if (Current == Records.end()) |
| 194 | *this = FunctionRecordIterator(); |
| 195 | } |
| 196 | |
Vedant Kumar | 68216d7 | 2016-10-12 22:27:45 +0000 | [diff] [blame] | 197 | Error CoverageMapping::loadFunctionRecord( |
| 198 | const CoverageMappingRecord &Record, |
| 199 | IndexedInstrProfReader &ProfileReader) { |
Vedant Kumar | 743574b | 2016-10-14 17:16:53 +0000 | [diff] [blame] | 200 | StringRef OrigFuncName = Record.FunctionName; |
Vedant Kumar | b1d331a | 2017-06-20 02:05:35 +0000 | [diff] [blame] | 201 | if (OrigFuncName.empty()) |
| 202 | return make_error<CoverageMapError>(coveragemap_error::malformed); |
| 203 | |
Vedant Kumar | 743574b | 2016-10-14 17:16:53 +0000 | [diff] [blame] | 204 | if (Record.Filenames.empty()) |
| 205 | OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName); |
| 206 | else |
| 207 | OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]); |
| 208 | |
| 209 | // Don't load records for functions we've already seen. |
| 210 | if (!FunctionNames.insert(OrigFuncName).second) |
| 211 | return Error::success(); |
| 212 | |
Vedant Kumar | 68216d7 | 2016-10-12 22:27:45 +0000 | [diff] [blame] | 213 | CounterMappingContext Ctx(Record.Expressions); |
| 214 | |
| 215 | std::vector<uint64_t> Counts; |
| 216 | if (Error E = ProfileReader.getFunctionCounts(Record.FunctionName, |
| 217 | Record.FunctionHash, Counts)) { |
| 218 | instrprof_error IPE = InstrProfError::take(std::move(E)); |
| 219 | if (IPE == instrprof_error::hash_mismatch) { |
Vedant Kumar | 18dd9e8 | 2017-09-21 01:11:30 +0000 | [diff] [blame] | 220 | FuncHashMismatches.emplace_back(Record.FunctionName, Record.FunctionHash); |
Vedant Kumar | 68216d7 | 2016-10-12 22:27:45 +0000 | [diff] [blame] | 221 | return Error::success(); |
| 222 | } else if (IPE != instrprof_error::unknown_function) |
| 223 | return make_error<InstrProfError>(IPE); |
| 224 | Counts.assign(Record.MappingRegions.size(), 0); |
| 225 | } |
| 226 | Ctx.setCounts(Counts); |
| 227 | |
| 228 | assert(!Record.MappingRegions.empty() && "Function has no regions"); |
| 229 | |
Vedant Kumar | 68216d7 | 2016-10-12 22:27:45 +0000 | [diff] [blame] | 230 | FunctionRecord Function(OrigFuncName, Record.Filenames); |
| 231 | for (const auto &Region : Record.MappingRegions) { |
| 232 | Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count); |
| 233 | if (auto E = ExecutionCount.takeError()) { |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 234 | consumeError(std::move(E)); |
Vedant Kumar | 68216d7 | 2016-10-12 22:27:45 +0000 | [diff] [blame] | 235 | return Error::success(); |
| 236 | } |
| 237 | Function.pushRegion(Region, *ExecutionCount); |
| 238 | } |
| 239 | if (Function.CountedRegions.size() != Record.MappingRegions.size()) { |
Vedant Kumar | 18dd9e8 | 2017-09-21 01:11:30 +0000 | [diff] [blame] | 240 | FuncCounterMismatches.emplace_back(Record.FunctionName, |
| 241 | Function.CountedRegions.size()); |
Vedant Kumar | 68216d7 | 2016-10-12 22:27:45 +0000 | [diff] [blame] | 242 | return Error::success(); |
| 243 | } |
| 244 | |
| 245 | Functions.push_back(std::move(Function)); |
| 246 | return Error::success(); |
| 247 | } |
| 248 | |
Vedant Kumar | 743574b | 2016-10-14 17:16:53 +0000 | [diff] [blame] | 249 | Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load( |
| 250 | ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders, |
| 251 | IndexedInstrProfReader &ProfileReader) { |
| 252 | auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping()); |
| 253 | |
Vedant Kumar | bae8397 | 2017-09-08 18:44:47 +0000 | [diff] [blame] | 254 | for (const auto &CoverageReader : CoverageReaders) { |
| 255 | for (auto RecordOrErr : *CoverageReader) { |
| 256 | if (Error E = RecordOrErr.takeError()) |
| 257 | return std::move(E); |
| 258 | const auto &Record = *RecordOrErr; |
Vedant Kumar | 743574b | 2016-10-14 17:16:53 +0000 | [diff] [blame] | 259 | if (Error E = Coverage->loadFunctionRecord(Record, ProfileReader)) |
| 260 | return std::move(E); |
Vedant Kumar | bae8397 | 2017-09-08 18:44:47 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
Vedant Kumar | 743574b | 2016-10-14 17:16:53 +0000 | [diff] [blame] | 263 | |
| 264 | return std::move(Coverage); |
| 265 | } |
| 266 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 267 | Expected<std::unique_ptr<CoverageMapping>> |
Vedant Kumar | 743574b | 2016-10-14 17:16:53 +0000 | [diff] [blame] | 268 | CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames, |
Vedant Kumar | 4b102c3 | 2017-08-01 21:23:26 +0000 | [diff] [blame] | 269 | StringRef ProfileFilename, ArrayRef<StringRef> Arches) { |
Justin Bogner | ab89ed7 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 270 | auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 271 | if (Error E = ProfileReaderOrErr.takeError()) |
| 272 | return std::move(E); |
Justin Bogner | ab89ed7 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 273 | auto ProfileReader = std::move(ProfileReaderOrErr.get()); |
Vedant Kumar | 743574b | 2016-10-14 17:16:53 +0000 | [diff] [blame] | 274 | |
| 275 | SmallVector<std::unique_ptr<CoverageMappingReader>, 4> Readers; |
| 276 | SmallVector<std::unique_ptr<MemoryBuffer>, 4> Buffers; |
Vedant Kumar | 4b102c3 | 2017-08-01 21:23:26 +0000 | [diff] [blame] | 277 | for (const auto &File : llvm::enumerate(ObjectFilenames)) { |
| 278 | auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(File.value()); |
Vedant Kumar | 743574b | 2016-10-14 17:16:53 +0000 | [diff] [blame] | 279 | if (std::error_code EC = CovMappingBufOrErr.getError()) |
| 280 | return errorCodeToError(EC); |
Vedant Kumar | 4b102c3 | 2017-08-01 21:23:26 +0000 | [diff] [blame] | 281 | StringRef Arch = Arches.empty() ? StringRef() : Arches[File.index()]; |
Vedant Kumar | 743574b | 2016-10-14 17:16:53 +0000 | [diff] [blame] | 282 | auto CoverageReaderOrErr = |
| 283 | BinaryCoverageReader::create(CovMappingBufOrErr.get(), Arch); |
| 284 | if (Error E = CoverageReaderOrErr.takeError()) |
| 285 | return std::move(E); |
| 286 | Readers.push_back(std::move(CoverageReaderOrErr.get())); |
| 287 | Buffers.push_back(std::move(CovMappingBufOrErr.get())); |
| 288 | } |
| 289 | return load(Readers, *ProfileReader); |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 292 | namespace { |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 293 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 294 | /// \brief Distributes functions into instantiation sets. |
| 295 | /// |
| 296 | /// An instantiation set is a collection of functions that have the same source |
| 297 | /// code, ie, template functions specializations. |
| 298 | class FunctionInstantiationSetCollector { |
Vedant Kumar | efcf41b | 2017-09-08 18:44:48 +0000 | [diff] [blame] | 299 | using MapT = DenseMap<LineColPair, std::vector<const FunctionRecord *>>; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 300 | MapT InstantiatedFunctions; |
| 301 | |
| 302 | public: |
| 303 | void insert(const FunctionRecord &Function, unsigned FileID) { |
| 304 | auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end(); |
| 305 | while (I != E && I->FileID != FileID) |
| 306 | ++I; |
| 307 | assert(I != E && "function does not cover the given file"); |
| 308 | auto &Functions = InstantiatedFunctions[I->startLoc()]; |
| 309 | Functions.push_back(&Function); |
| 310 | } |
| 311 | |
| 312 | MapT::iterator begin() { return InstantiatedFunctions.begin(); } |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 313 | MapT::iterator end() { return InstantiatedFunctions.end(); } |
| 314 | }; |
| 315 | |
| 316 | class SegmentBuilder { |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 317 | std::vector<CoverageSegment> &Segments; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 318 | SmallVector<const CountedRegion *, 8> ActiveRegions; |
| 319 | |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 320 | SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {} |
| 321 | |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 322 | /// Emit a segment with the count from \p Region starting at \p StartLoc. |
| 323 | // |
Vedant Kumar | ad8f637 | 2017-09-18 23:37:28 +0000 | [diff] [blame] | 324 | /// \p IsRegionEntry: The segment is at the start of a new non-gap region. |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 325 | /// \p EmitSkippedRegion: The segment must be emitted as a skipped region. |
| 326 | void startSegment(const CountedRegion &Region, LineColPair StartLoc, |
| 327 | bool IsRegionEntry, bool EmitSkippedRegion = false) { |
| 328 | bool HasCount = !EmitSkippedRegion && |
| 329 | (Region.Kind != CounterMappingRegion::SkippedRegion); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 330 | |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 331 | // If the new segment wouldn't affect coverage rendering, skip it. |
| 332 | if (!Segments.empty() && !IsRegionEntry && !EmitSkippedRegion) { |
| 333 | const auto &Last = Segments.back(); |
| 334 | if (Last.HasCount == HasCount && Last.Count == Region.ExecutionCount && |
| 335 | !Last.IsRegionEntry) |
| 336 | return; |
| 337 | } |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 338 | |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 339 | if (HasCount) |
| 340 | Segments.emplace_back(StartLoc.first, StartLoc.second, |
Vedant Kumar | ad8f637 | 2017-09-18 23:37:28 +0000 | [diff] [blame] | 341 | Region.ExecutionCount, IsRegionEntry, |
| 342 | Region.Kind == CounterMappingRegion::GapRegion); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 343 | else |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 344 | Segments.emplace_back(StartLoc.first, StartLoc.second, IsRegionEntry); |
| 345 | |
| 346 | DEBUG({ |
| 347 | const auto &Last = Segments.back(); |
| 348 | dbgs() << "Segment at " << Last.Line << ":" << Last.Col |
| 349 | << " (count = " << Last.Count << ")" |
| 350 | << (Last.IsRegionEntry ? ", RegionEntry" : "") |
Vedant Kumar | ad8f637 | 2017-09-18 23:37:28 +0000 | [diff] [blame] | 351 | << (!Last.HasCount ? ", Skipped" : "") |
| 352 | << (Last.IsGapRegion ? ", Gap" : "") << "\n"; |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 353 | }); |
| 354 | } |
| 355 | |
| 356 | /// Emit segments for active regions which end before \p Loc. |
| 357 | /// |
| 358 | /// \p Loc: The start location of the next region. If None, all active |
| 359 | /// regions are completed. |
| 360 | /// \p FirstCompletedRegion: Index of the first completed region. |
| 361 | void completeRegionsUntil(Optional<LineColPair> Loc, |
| 362 | unsigned FirstCompletedRegion) { |
| 363 | // Sort the completed regions by end location. This makes it simple to |
| 364 | // emit closing segments in sorted order. |
| 365 | auto CompletedRegionsIt = ActiveRegions.begin() + FirstCompletedRegion; |
| 366 | std::stable_sort(CompletedRegionsIt, ActiveRegions.end(), |
| 367 | [](const CountedRegion *L, const CountedRegion *R) { |
| 368 | return L->endLoc() < R->endLoc(); |
| 369 | }); |
| 370 | |
| 371 | // Emit segments for all completed regions. |
| 372 | for (unsigned I = FirstCompletedRegion + 1, E = ActiveRegions.size(); I < E; |
| 373 | ++I) { |
| 374 | const auto *CompletedRegion = ActiveRegions[I]; |
| 375 | assert((!Loc || CompletedRegion->endLoc() <= *Loc) && |
| 376 | "Completed region ends after start of new region"); |
| 377 | |
| 378 | const auto *PrevCompletedRegion = ActiveRegions[I - 1]; |
| 379 | auto CompletedSegmentLoc = PrevCompletedRegion->endLoc(); |
| 380 | |
| 381 | // Don't emit any more segments if they start where the new region begins. |
| 382 | if (Loc && CompletedSegmentLoc == *Loc) |
| 383 | break; |
| 384 | |
| 385 | // Don't emit a segment if the next completed region ends at the same |
| 386 | // location as this one. |
| 387 | if (CompletedSegmentLoc == CompletedRegion->endLoc()) |
| 388 | continue; |
| 389 | |
| 390 | startSegment(*CompletedRegion, CompletedSegmentLoc, false); |
| 391 | } |
| 392 | |
| 393 | auto Last = ActiveRegions.back(); |
| 394 | if (FirstCompletedRegion && Last->endLoc() != *Loc) { |
| 395 | // If there's a gap after the end of the last completed region and the |
| 396 | // start of the new region, use the last active region to fill the gap. |
| 397 | startSegment(*ActiveRegions[FirstCompletedRegion - 1], Last->endLoc(), |
| 398 | false); |
| 399 | } else if (!FirstCompletedRegion && (!Loc || *Loc != Last->endLoc())) { |
| 400 | // Emit a skipped segment if there are no more active regions. This |
| 401 | // ensures that gaps between functions are marked correctly. |
| 402 | startSegment(*Last, Last->endLoc(), false, true); |
| 403 | } |
| 404 | |
| 405 | // Pop the completed regions. |
| 406 | ActiveRegions.erase(CompletedRegionsIt, ActiveRegions.end()); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 409 | void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) { |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 410 | for (const auto &CR : enumerate(Regions)) { |
| 411 | auto CurStartLoc = CR.value().startLoc(); |
| 412 | |
| 413 | // Active regions which end before the current region need to be popped. |
| 414 | auto CompletedRegions = |
| 415 | std::stable_partition(ActiveRegions.begin(), ActiveRegions.end(), |
| 416 | [&](const CountedRegion *Region) { |
| 417 | return !(Region->endLoc() <= CurStartLoc); |
| 418 | }); |
| 419 | if (CompletedRegions != ActiveRegions.end()) { |
| 420 | unsigned FirstCompletedRegion = |
| 421 | std::distance(ActiveRegions.begin(), CompletedRegions); |
| 422 | completeRegionsUntil(CurStartLoc, FirstCompletedRegion); |
| 423 | } |
| 424 | |
Vedant Kumar | ad8f637 | 2017-09-18 23:37:28 +0000 | [diff] [blame] | 425 | bool GapRegion = CR.value().Kind == CounterMappingRegion::GapRegion; |
| 426 | |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 427 | // Try to emit a segment for the current region. |
| 428 | if (CurStartLoc == CR.value().endLoc()) { |
| 429 | // Avoid making zero-length regions active. If it's the last region, |
| 430 | // emit a skipped segment. Otherwise use its predecessor's count. |
| 431 | const bool Skipped = (CR.index() + 1) == Regions.size(); |
| 432 | startSegment(ActiveRegions.empty() ? CR.value() : *ActiveRegions.back(), |
Vedant Kumar | ad8f637 | 2017-09-18 23:37:28 +0000 | [diff] [blame] | 433 | CurStartLoc, !GapRegion, Skipped); |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 434 | continue; |
| 435 | } |
| 436 | if (CR.index() + 1 == Regions.size() || |
| 437 | CurStartLoc != Regions[CR.index() + 1].startLoc()) { |
| 438 | // Emit a segment if the next region doesn't start at the same location |
| 439 | // as this one. |
Vedant Kumar | ad8f637 | 2017-09-18 23:37:28 +0000 | [diff] [blame] | 440 | startSegment(CR.value(), CurStartLoc, !GapRegion); |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // This region is active (i.e not completed). |
| 444 | ActiveRegions.push_back(&CR.value()); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 445 | } |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 446 | |
| 447 | // Complete any remaining active regions. |
| 448 | if (!ActiveRegions.empty()) |
| 449 | completeRegionsUntil(None, 0); |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 452 | /// Sort a nested sequence of regions from a single file. |
| 453 | static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) { |
Igor Kudrin | 27d8dd3 | 2016-05-05 09:39:45 +0000 | [diff] [blame] | 454 | std::sort(Regions.begin(), Regions.end(), [](const CountedRegion &LHS, |
| 455 | const CountedRegion &RHS) { |
| 456 | if (LHS.startLoc() != RHS.startLoc()) |
| 457 | return LHS.startLoc() < RHS.startLoc(); |
| 458 | if (LHS.endLoc() != RHS.endLoc()) |
| 459 | // When LHS completely contains RHS, we sort LHS first. |
| 460 | return RHS.endLoc() < LHS.endLoc(); |
| 461 | // If LHS and RHS cover the same area, we need to sort them according |
| 462 | // to their kinds so that the most suitable region will become "active" |
| 463 | // in combineRegions(). Because we accumulate counter values only from |
| 464 | // regions of the same kind as the first region of the area, prefer |
| 465 | // CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion. |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 466 | static_assert(CounterMappingRegion::CodeRegion < |
| 467 | CounterMappingRegion::ExpansionRegion && |
| 468 | CounterMappingRegion::ExpansionRegion < |
| 469 | CounterMappingRegion::SkippedRegion, |
Igor Kudrin | 27d8dd3 | 2016-05-05 09:39:45 +0000 | [diff] [blame] | 470 | "Unexpected order of region kind values"); |
| 471 | return LHS.Kind < RHS.Kind; |
| 472 | }); |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /// Combine counts of regions which cover the same area. |
| 476 | static ArrayRef<CountedRegion> |
| 477 | combineRegions(MutableArrayRef<CountedRegion> Regions) { |
| 478 | if (Regions.empty()) |
| 479 | return Regions; |
| 480 | auto Active = Regions.begin(); |
| 481 | auto End = Regions.end(); |
| 482 | for (auto I = Regions.begin() + 1; I != End; ++I) { |
| 483 | if (Active->startLoc() != I->startLoc() || |
| 484 | Active->endLoc() != I->endLoc()) { |
| 485 | // Shift to the next region. |
| 486 | ++Active; |
| 487 | if (Active != I) |
| 488 | *Active = *I; |
| 489 | continue; |
| 490 | } |
| 491 | // Merge duplicate region. |
Igor Kudrin | 27d8dd3 | 2016-05-05 09:39:45 +0000 | [diff] [blame] | 492 | // If CodeRegions and ExpansionRegions cover the same area, it's probably |
| 493 | // a macro which is fully expanded to another macro. In that case, we need |
| 494 | // to accumulate counts only from CodeRegions, or else the area will be |
| 495 | // counted twice. |
| 496 | // On the other hand, a macro may have a nested macro in its body. If the |
| 497 | // outer macro is used several times, the ExpansionRegion for the nested |
| 498 | // macro will also be added several times. These ExpansionRegions cover |
| 499 | // the same source locations and have to be combined to reach the correct |
| 500 | // value for that area. |
| 501 | // We add counts of the regions of the same kind as the active region |
| 502 | // to handle the both situations. |
| 503 | if (I->Kind == Active->Kind) |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 504 | Active->ExecutionCount += I->ExecutionCount; |
| 505 | } |
| 506 | return Regions.drop_back(std::distance(++Active, End)); |
| 507 | } |
| 508 | |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 509 | public: |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 510 | /// Build a sorted list of CoverageSegments from a list of Regions. |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 511 | static std::vector<CoverageSegment> |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 512 | buildSegments(MutableArrayRef<CountedRegion> Regions) { |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 513 | std::vector<CoverageSegment> Segments; |
| 514 | SegmentBuilder Builder(Segments); |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 515 | |
| 516 | sortNestedRegions(Regions); |
| 517 | ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions); |
| 518 | |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 519 | DEBUG({ |
| 520 | dbgs() << "Combined regions:\n"; |
| 521 | for (const auto &CR : CombinedRegions) |
| 522 | dbgs() << " " << CR.LineStart << ":" << CR.ColumnStart << " -> " |
| 523 | << CR.LineEnd << ":" << CR.ColumnEnd |
| 524 | << " (count=" << CR.ExecutionCount << ")\n"; |
| 525 | }); |
| 526 | |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 527 | Builder.buildSegmentsImpl(CombinedRegions); |
Vedant Kumar | 79a1b5e | 2017-09-08 18:44:50 +0000 | [diff] [blame] | 528 | |
| 529 | #ifndef NDEBUG |
| 530 | for (unsigned I = 1, E = Segments.size(); I < E; ++I) { |
| 531 | const auto &L = Segments[I - 1]; |
| 532 | const auto &R = Segments[I]; |
| 533 | if (!(L.Line < R.Line) && !(L.Line == R.Line && L.Col < R.Col)) { |
| 534 | DEBUG(dbgs() << " ! Segment " << L.Line << ":" << L.Col |
| 535 | << " followed by " << R.Line << ":" << R.Col << "\n"); |
| 536 | assert(false && "Coverage segments not unique or sorted"); |
| 537 | } |
| 538 | } |
| 539 | #endif |
| 540 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 541 | return Segments; |
| 542 | } |
| 543 | }; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 544 | |
| 545 | } // end anonymous namespace |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 546 | |
Justin Bogner | d5fca92 | 2014-11-14 01:50:32 +0000 | [diff] [blame] | 547 | std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 548 | std::vector<StringRef> Filenames; |
| 549 | for (const auto &Function : getCoveredFunctions()) |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 550 | Filenames.insert(Filenames.end(), Function.Filenames.begin(), |
| 551 | Function.Filenames.end()); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 552 | std::sort(Filenames.begin(), Filenames.end()); |
| 553 | auto Last = std::unique(Filenames.begin(), Filenames.end()); |
| 554 | Filenames.erase(Last, Filenames.end()); |
| 555 | return Filenames; |
| 556 | } |
| 557 | |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 558 | static SmallBitVector gatherFileIDs(StringRef SourceFile, |
| 559 | const FunctionRecord &Function) { |
| 560 | SmallBitVector FilenameEquivalence(Function.Filenames.size(), false); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 561 | for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I) |
| 562 | if (SourceFile == Function.Filenames[I]) |
| 563 | FilenameEquivalence[I] = true; |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 564 | return FilenameEquivalence; |
| 565 | } |
| 566 | |
Igor Kudrin | 1c14dc4 | 2016-04-18 15:36:30 +0000 | [diff] [blame] | 567 | /// Return the ID of the file where the definition of the function is located. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 568 | static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) { |
Benjamin Kramer | 40957cc | 2015-02-12 16:30:00 +0000 | [diff] [blame] | 569 | SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 570 | for (const auto &CR : Function.CountedRegions) |
| 571 | if (CR.Kind == CounterMappingRegion::ExpansionRegion) |
Benjamin Kramer | 40957cc | 2015-02-12 16:30:00 +0000 | [diff] [blame] | 572 | IsNotExpandedFile[CR.ExpandedFileID] = false; |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 573 | int I = IsNotExpandedFile.find_first(); |
Justin Bogner | c4f5a5e | 2015-02-20 07:28:28 +0000 | [diff] [blame] | 574 | if (I == -1) |
| 575 | return None; |
| 576 | return I; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Igor Kudrin | 1c14dc4 | 2016-04-18 15:36:30 +0000 | [diff] [blame] | 579 | /// Check if SourceFile is the file that contains the definition of |
| 580 | /// the Function. Return the ID of the file in that case or None otherwise. |
| 581 | static Optional<unsigned> findMainViewFileID(StringRef SourceFile, |
| 582 | const FunctionRecord &Function) { |
| 583 | Optional<unsigned> I = findMainViewFileID(Function); |
| 584 | if (I && SourceFile == Function.Filenames[*I]) |
| 585 | return I; |
| 586 | return None; |
| 587 | } |
| 588 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 589 | static bool isExpansion(const CountedRegion &R, unsigned FileID) { |
| 590 | return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID; |
| 591 | } |
| 592 | |
Vedant Kumar | 7fcc547 | 2016-07-13 23:12:23 +0000 | [diff] [blame] | 593 | CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 594 | CoverageData FileCoverage(Filename); |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 595 | std::vector<CountedRegion> Regions; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 596 | |
| 597 | for (const auto &Function : Functions) { |
| 598 | auto MainFileID = findMainViewFileID(Filename, Function); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 599 | auto FileIDs = gatherFileIDs(Filename, Function); |
| 600 | for (const auto &CR : Function.CountedRegions) |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 601 | if (FileIDs.test(CR.FileID)) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 602 | Regions.push_back(CR); |
Igor Kudrin | 1c14dc4 | 2016-04-18 15:36:30 +0000 | [diff] [blame] | 603 | if (MainFileID && isExpansion(CR, *MainFileID)) |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 604 | FileCoverage.Expansions.emplace_back(CR, Function); |
| 605 | } |
| 606 | } |
| 607 | |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 608 | DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n"); |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 609 | FileCoverage.Segments = SegmentBuilder::buildSegments(Regions); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 610 | |
| 611 | return FileCoverage; |
| 612 | } |
| 613 | |
Vedant Kumar | dde19c5 | 2017-08-02 23:35:25 +0000 | [diff] [blame] | 614 | std::vector<InstantiationGroup> |
| 615 | CoverageMapping::getInstantiationGroups(StringRef Filename) const { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 616 | FunctionInstantiationSetCollector InstantiationSetCollector; |
| 617 | for (const auto &Function : Functions) { |
| 618 | auto MainFileID = findMainViewFileID(Filename, Function); |
| 619 | if (!MainFileID) |
| 620 | continue; |
| 621 | InstantiationSetCollector.insert(Function, *MainFileID); |
| 622 | } |
| 623 | |
Vedant Kumar | dde19c5 | 2017-08-02 23:35:25 +0000 | [diff] [blame] | 624 | std::vector<InstantiationGroup> Result; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 625 | for (const auto &InstantiationSet : InstantiationSetCollector) { |
Vedant Kumar | dde19c5 | 2017-08-02 23:35:25 +0000 | [diff] [blame] | 626 | InstantiationGroup IG{InstantiationSet.first.first, |
| 627 | InstantiationSet.first.second, |
| 628 | std::move(InstantiationSet.second)}; |
| 629 | Result.emplace_back(std::move(IG)); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 630 | } |
| 631 | return Result; |
| 632 | } |
| 633 | |
| 634 | CoverageData |
Vedant Kumar | f681e2e | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 635 | CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 636 | auto MainFileID = findMainViewFileID(Function); |
| 637 | if (!MainFileID) |
| 638 | return CoverageData(); |
| 639 | |
| 640 | CoverageData FunctionCoverage(Function.Filenames[*MainFileID]); |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 641 | std::vector<CountedRegion> Regions; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 642 | for (const auto &CR : Function.CountedRegions) |
| 643 | if (CR.FileID == *MainFileID) { |
| 644 | Regions.push_back(CR); |
| 645 | if (isExpansion(CR, *MainFileID)) |
| 646 | FunctionCoverage.Expansions.emplace_back(CR, Function); |
| 647 | } |
| 648 | |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 649 | DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n"); |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 650 | FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 651 | |
| 652 | return FunctionCoverage; |
| 653 | } |
| 654 | |
Vedant Kumar | f681e2e | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 655 | CoverageData CoverageMapping::getCoverageForExpansion( |
| 656 | const ExpansionRecord &Expansion) const { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 657 | CoverageData ExpansionCoverage( |
| 658 | Expansion.Function.Filenames[Expansion.FileID]); |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 659 | std::vector<CountedRegion> Regions; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 660 | for (const auto &CR : Expansion.Function.CountedRegions) |
| 661 | if (CR.FileID == Expansion.FileID) { |
| 662 | Regions.push_back(CR); |
| 663 | if (isExpansion(CR, Expansion.FileID)) |
| 664 | ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function); |
| 665 | } |
| 666 | |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 667 | DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID |
| 668 | << "\n"); |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 669 | ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 670 | |
| 671 | return ExpansionCoverage; |
| 672 | } |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 673 | |
Vedant Kumar | 821160d | 2017-10-18 23:58:28 +0000 | [diff] [blame] | 674 | LineCoverageStats::LineCoverageStats( |
| 675 | ArrayRef<const coverage::CoverageSegment *> LineSegments, |
| 676 | const coverage::CoverageSegment *WrappedSegment, unsigned Line) |
| 677 | : ExecutionCount(0), HasMultipleRegions(false), Mapped(false), Line(Line), |
| 678 | LineSegments(LineSegments), WrappedSegment(WrappedSegment) { |
| 679 | // Find the minimum number of regions which start in this line. |
| 680 | unsigned MinRegionCount = 0; |
| 681 | auto isStartOfRegion = [](const coverage::CoverageSegment *S) { |
| 682 | return !S->IsGapRegion && S->HasCount && S->IsRegionEntry; |
| 683 | }; |
| 684 | for (unsigned I = 0; I < LineSegments.size() && MinRegionCount < 2; ++I) |
| 685 | if (isStartOfRegion(LineSegments[I])) |
| 686 | ++MinRegionCount; |
| 687 | |
| 688 | bool StartOfSkippedRegion = !LineSegments.empty() && |
| 689 | !LineSegments.front()->HasCount && |
| 690 | LineSegments.front()->IsRegionEntry; |
| 691 | |
| 692 | HasMultipleRegions = MinRegionCount > 1; |
| 693 | Mapped = |
| 694 | !StartOfSkippedRegion && |
| 695 | ((WrappedSegment && WrappedSegment->HasCount) || (MinRegionCount > 0)); |
| 696 | |
| 697 | if (!Mapped) |
| 698 | return; |
| 699 | |
| 700 | // Pick the max count from the non-gap, region entry segments. If there |
| 701 | // aren't any, use the wrapped count. |
| 702 | if (!MinRegionCount) { |
| 703 | ExecutionCount = WrappedSegment->Count; |
| 704 | return; |
| 705 | } |
| 706 | for (const auto *LS : LineSegments) |
| 707 | if (isStartOfRegion(LS)) |
| 708 | ExecutionCount = std::max(ExecutionCount, LS->Count); |
| 709 | } |
| 710 | |
| 711 | LineCoverageIterator &LineCoverageIterator::operator++() { |
| 712 | if (Next == CD.end()) { |
| 713 | Stats = LineCoverageStats(); |
| 714 | Ended = true; |
| 715 | return *this; |
| 716 | } |
| 717 | if (Segments.size()) |
| 718 | WrappedSegment = Segments.back(); |
| 719 | Segments.clear(); |
| 720 | while (Next != CD.end() && Next->Line == Line) |
| 721 | Segments.push_back(&*Next++); |
| 722 | Stats = LineCoverageStats(Segments, WrappedSegment, Line); |
| 723 | ++Line; |
| 724 | return *this; |
| 725 | } |
| 726 | |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 727 | static std::string getCoverageMapErrString(coveragemap_error Err) { |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 728 | switch (Err) { |
| 729 | case coveragemap_error::success: |
| 730 | return "Success"; |
| 731 | case coveragemap_error::eof: |
| 732 | return "End of File"; |
| 733 | case coveragemap_error::no_data_found: |
| 734 | return "No coverage data found"; |
| 735 | case coveragemap_error::unsupported_version: |
| 736 | return "Unsupported coverage format version"; |
| 737 | case coveragemap_error::truncated: |
| 738 | return "Truncated coverage data"; |
| 739 | case coveragemap_error::malformed: |
| 740 | return "Malformed coverage data"; |
| 741 | } |
| 742 | llvm_unreachable("A value of coveragemap_error has no message."); |
| 743 | } |
| 744 | |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 745 | namespace { |
| 746 | |
Peter Collingbourne | 4718f8b | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 747 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 748 | // will be removed once this transition is complete. Clients should prefer to |
| 749 | // deal with the Error value directly, rather than converting to error_code. |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 750 | class CoverageMappingErrorCategoryType : public std::error_category { |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 751 | const char *name() const noexcept override { return "llvm.coveragemap"; } |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 752 | std::string message(int IE) const override { |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 753 | return getCoverageMapErrString(static_cast<coveragemap_error>(IE)); |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 754 | } |
| 755 | }; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 756 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 757 | } // end anonymous namespace |
| 758 | |
| 759 | std::string CoverageMapError::message() const { |
| 760 | return getCoverageMapErrString(Err); |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 761 | } |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 762 | |
| 763 | static ManagedStatic<CoverageMappingErrorCategoryType> ErrorCategory; |
| 764 | |
Xinliang David Li | 8a5bdb5 | 2016-01-10 21:56:33 +0000 | [diff] [blame] | 765 | const std::error_category &llvm::coverage::coveragemap_category() { |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 766 | return *ErrorCategory; |
| 767 | } |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 768 | |
| 769 | char CoverageMapError::ID = 0; |