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