Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 1 | //=-- CoverageMapping.cpp - Code coverage mapping support ---------*- C++ -*-=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains support for clang's and llvm's instrumentation based |
| 11 | // code coverage. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Easwaran Raman | dc70712 | 2016-04-29 18:53:05 +0000 | [diff] [blame] | 15 | #include "llvm/ProfileData/Coverage/CoverageMapping.h" |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | #include "llvm/ADT/Optional.h" |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallBitVector.h" |
Easwaran Raman | dc70712 | 2016-04-29 18:53:05 +0000 | [diff] [blame] | 19 | #include "llvm/ProfileData/Coverage/CoverageMappingReader.h" |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 20 | #include "llvm/ProfileData/InstrProfReader.h" |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Rafael Espindola | 74f2932 | 2015-06-13 17:23:04 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Errc.h" |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ManagedStatic.h" |
Justin Bogner | 0b4c484 | 2015-05-05 23:44:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Path.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace coverage; |
| 30 | |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "coverage-mapping" |
| 32 | |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 33 | Counter CounterExpressionBuilder::get(const CounterExpression &E) { |
Justin Bogner | ad69e64 | 2014-10-02 17:14:18 +0000 | [diff] [blame] | 34 | auto It = ExpressionIndices.find(E); |
| 35 | if (It != ExpressionIndices.end()) |
| 36 | return Counter::getExpression(It->second); |
| 37 | unsigned I = Expressions.size(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 38 | Expressions.push_back(E); |
Justin Bogner | ad69e64 | 2014-10-02 17:14:18 +0000 | [diff] [blame] | 39 | ExpressionIndices[E] = I; |
| 40 | return Counter::getExpression(I); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 43 | void CounterExpressionBuilder::extractTerms( |
| 44 | Counter C, int Sign, SmallVectorImpl<std::pair<unsigned, int>> &Terms) { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 45 | switch (C.getKind()) { |
| 46 | case Counter::Zero: |
| 47 | break; |
| 48 | case Counter::CounterValueReference: |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 49 | Terms.push_back(std::make_pair(C.getCounterID(), Sign)); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 50 | break; |
| 51 | case Counter::Expression: |
| 52 | const auto &E = Expressions[C.getExpressionID()]; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 53 | extractTerms(E.LHS, Sign, Terms); |
| 54 | extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign, |
| 55 | Terms); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) { |
| 61 | // Gather constant terms. |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 62 | llvm::SmallVector<std::pair<unsigned, int>, 32> Terms; |
| 63 | extractTerms(ExpressionTree, +1, Terms); |
| 64 | |
| 65 | // If there are no terms, this is just a zero. The algorithm below assumes at |
| 66 | // least one term. |
| 67 | if (Terms.size() == 0) |
| 68 | return Counter::getZero(); |
| 69 | |
| 70 | // Group the terms by counter ID. |
| 71 | std::sort(Terms.begin(), Terms.end(), |
| 72 | [](const std::pair<unsigned, int> &LHS, |
| 73 | const std::pair<unsigned, int> &RHS) { |
| 74 | return LHS.first < RHS.first; |
| 75 | }); |
| 76 | |
| 77 | // Combine terms by counter ID to eliminate counters that sum to zero. |
| 78 | auto Prev = Terms.begin(); |
| 79 | for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) { |
| 80 | if (I->first == Prev->first) { |
| 81 | Prev->second += I->second; |
| 82 | continue; |
| 83 | } |
| 84 | ++Prev; |
| 85 | *Prev = *I; |
| 86 | } |
| 87 | Terms.erase(++Prev, Terms.end()); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 88 | |
| 89 | Counter C; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 90 | // Create additions. We do this before subtractions to avoid constructs like |
| 91 | // ((0 - X) + Y), as opposed to (Y - X). |
| 92 | for (auto Term : Terms) { |
| 93 | if (Term.second <= 0) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 94 | continue; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 95 | for (int I = 0; I < Term.second; ++I) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 96 | if (C.isZero()) |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 97 | C = Counter::getCounter(Term.first); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 98 | else |
| 99 | C = get(CounterExpression(CounterExpression::Add, C, |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 100 | Counter::getCounter(Term.first))); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Create subtractions. |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 104 | for (auto Term : Terms) { |
| 105 | if (Term.second >= 0) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 106 | continue; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 107 | for (int I = 0; I < -Term.second; ++I) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 108 | C = get(CounterExpression(CounterExpression::Subtract, C, |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 109 | Counter::getCounter(Term.first))); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 110 | } |
| 111 | return C; |
| 112 | } |
| 113 | |
| 114 | Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) { |
| 115 | return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS))); |
| 116 | } |
| 117 | |
| 118 | Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) { |
| 119 | return simplify( |
| 120 | get(CounterExpression(CounterExpression::Subtract, LHS, RHS))); |
| 121 | } |
| 122 | |
| 123 | void CounterMappingContext::dump(const Counter &C, |
| 124 | llvm::raw_ostream &OS) const { |
| 125 | switch (C.getKind()) { |
| 126 | case Counter::Zero: |
| 127 | OS << '0'; |
| 128 | return; |
| 129 | case Counter::CounterValueReference: |
| 130 | OS << '#' << C.getCounterID(); |
| 131 | break; |
| 132 | case Counter::Expression: { |
| 133 | if (C.getExpressionID() >= Expressions.size()) |
| 134 | return; |
| 135 | const auto &E = Expressions[C.getExpressionID()]; |
| 136 | OS << '('; |
Alex Lorenz | a422911c | 2014-07-29 19:58:16 +0000 | [diff] [blame] | 137 | dump(E.LHS, OS); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 138 | OS << (E.Kind == CounterExpression::Subtract ? " - " : " + "); |
Alex Lorenz | a422911c | 2014-07-29 19:58:16 +0000 | [diff] [blame] | 139 | dump(E.RHS, OS); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 140 | OS << ')'; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | if (CounterValues.empty()) |
| 145 | return; |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 146 | ErrorOr<int64_t> Value = evaluate(C); |
| 147 | if (!Value) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 148 | return; |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 149 | OS << '[' << *Value << ']'; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 152 | ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 153 | switch (C.getKind()) { |
| 154 | case Counter::Zero: |
| 155 | return 0; |
| 156 | case Counter::CounterValueReference: |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 157 | if (C.getCounterID() >= CounterValues.size()) |
Rafael Espindola | 74f2932 | 2015-06-13 17:23:04 +0000 | [diff] [blame] | 158 | return make_error_code(errc::argument_out_of_domain); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 159 | return CounterValues[C.getCounterID()]; |
| 160 | case Counter::Expression: { |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 161 | if (C.getExpressionID() >= Expressions.size()) |
Rafael Espindola | 74f2932 | 2015-06-13 17:23:04 +0000 | [diff] [blame] | 162 | return make_error_code(errc::argument_out_of_domain); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 163 | const auto &E = Expressions[C.getExpressionID()]; |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 164 | ErrorOr<int64_t> LHS = evaluate(E.LHS); |
| 165 | if (!LHS) |
| 166 | return LHS; |
| 167 | ErrorOr<int64_t> RHS = evaluate(E.RHS); |
| 168 | if (!RHS) |
| 169 | return RHS; |
| 170 | return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 173 | llvm_unreachable("Unhandled CounterKind"); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 174 | } |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 175 | |
Justin Bogner | d5fca92 | 2014-11-14 01:50:32 +0000 | [diff] [blame] | 176 | void FunctionRecordIterator::skipOtherFiles() { |
| 177 | while (Current != Records.end() && !Filename.empty() && |
| 178 | Filename != Current->Filenames[0]) |
| 179 | ++Current; |
| 180 | if (Current == Records.end()) |
| 181 | *this = FunctionRecordIterator(); |
| 182 | } |
| 183 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 184 | ErrorOr<std::unique_ptr<CoverageMapping>> |
Justin Bogner | 1d29c08 | 2015-02-18 18:01:14 +0000 | [diff] [blame] | 185 | CoverageMapping::load(CoverageMappingReader &CoverageReader, |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 186 | IndexedInstrProfReader &ProfileReader) { |
| 187 | auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping()); |
| 188 | |
| 189 | std::vector<uint64_t> Counts; |
| 190 | for (const auto &Record : CoverageReader) { |
Justin Bogner | 428c605 | 2015-02-18 18:40:46 +0000 | [diff] [blame] | 191 | CounterMappingContext Ctx(Record.Expressions); |
| 192 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 193 | Counts.clear(); |
| 194 | if (std::error_code EC = ProfileReader.getFunctionCounts( |
| 195 | Record.FunctionName, Record.FunctionHash, Counts)) { |
Justin Bogner | 428c605 | 2015-02-18 18:40:46 +0000 | [diff] [blame] | 196 | if (EC == instrprof_error::hash_mismatch) { |
| 197 | Coverage->MismatchedFunctionCount++; |
| 198 | continue; |
| 199 | } else if (EC != instrprof_error::unknown_function) |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 200 | return EC; |
Justin Bogner | 82a6451 | 2015-05-13 22:03:04 +0000 | [diff] [blame] | 201 | Counts.assign(Record.MappingRegions.size(), 0); |
| 202 | } |
| 203 | Ctx.setCounts(Counts); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 204 | |
Justin Bogner | 428c605 | 2015-02-18 18:40:46 +0000 | [diff] [blame] | 205 | assert(!Record.MappingRegions.empty() && "Function has no regions"); |
Justin Bogner | 0b4c484 | 2015-05-05 23:44:48 +0000 | [diff] [blame] | 206 | |
Xinliang David Li | 4ec4014 | 2015-12-15 19:44:45 +0000 | [diff] [blame] | 207 | StringRef OrigFuncName = Record.FunctionName; |
Vedant Kumar | 43a8565 | 2016-03-28 15:49:08 +0000 | [diff] [blame] | 208 | if (Record.Filenames.empty()) |
| 209 | OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName); |
| 210 | else |
Xinliang David Li | 4ec4014 | 2015-12-15 19:44:45 +0000 | [diff] [blame] | 211 | OrigFuncName = |
| 212 | getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]); |
| 213 | FunctionRecord Function(OrigFuncName, Record.Filenames); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 214 | for (const auto &Region : Record.MappingRegions) { |
| 215 | ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count); |
| 216 | if (!ExecutionCount) |
| 217 | break; |
Justin Bogner | 428c605 | 2015-02-18 18:40:46 +0000 | [diff] [blame] | 218 | Function.pushRegion(Region, *ExecutionCount); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 219 | } |
| 220 | if (Function.CountedRegions.size() != Record.MappingRegions.size()) { |
| 221 | Coverage->MismatchedFunctionCount++; |
| 222 | continue; |
| 223 | } |
| 224 | |
Benjamin Kramer | e12a6ba | 2014-10-03 18:33:16 +0000 | [diff] [blame] | 225 | Coverage->Functions.push_back(std::move(Function)); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return std::move(Coverage); |
| 229 | } |
| 230 | |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 231 | ErrorOr<std::unique_ptr<CoverageMapping>> |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 232 | CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename, |
Frederic Riss | ebc162a | 2015-06-22 21:33:24 +0000 | [diff] [blame] | 233 | StringRef Arch) { |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 234 | auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename); |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 235 | if (std::error_code EC = CounterMappingBuff.getError()) |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 236 | return EC; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 237 | auto CoverageReaderOrErr = |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 238 | BinaryCoverageReader::create(CounterMappingBuff.get(), Arch); |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 239 | if (std::error_code EC = CoverageReaderOrErr.getError()) |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 240 | return EC; |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 241 | auto CoverageReader = std::move(CoverageReaderOrErr.get()); |
Justin Bogner | ab89ed7 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 242 | auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename); |
| 243 | if (auto EC = ProfileReaderOrErr.getError()) |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 244 | return EC; |
Justin Bogner | ab89ed7 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 245 | auto ProfileReader = std::move(ProfileReaderOrErr.get()); |
Justin Bogner | 43e5163 | 2015-02-26 20:06:28 +0000 | [diff] [blame] | 246 | return load(*CoverageReader, *ProfileReader); |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 249 | namespace { |
| 250 | /// \brief Distributes functions into instantiation sets. |
| 251 | /// |
| 252 | /// An instantiation set is a collection of functions that have the same source |
| 253 | /// code, ie, template functions specializations. |
| 254 | class FunctionInstantiationSetCollector { |
| 255 | typedef DenseMap<std::pair<unsigned, unsigned>, |
| 256 | std::vector<const FunctionRecord *>> MapT; |
| 257 | MapT InstantiatedFunctions; |
| 258 | |
| 259 | public: |
| 260 | void insert(const FunctionRecord &Function, unsigned FileID) { |
| 261 | auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end(); |
| 262 | while (I != E && I->FileID != FileID) |
| 263 | ++I; |
| 264 | assert(I != E && "function does not cover the given file"); |
| 265 | auto &Functions = InstantiatedFunctions[I->startLoc()]; |
| 266 | Functions.push_back(&Function); |
| 267 | } |
| 268 | |
| 269 | MapT::iterator begin() { return InstantiatedFunctions.begin(); } |
| 270 | |
| 271 | MapT::iterator end() { return InstantiatedFunctions.end(); } |
| 272 | }; |
| 273 | |
| 274 | class SegmentBuilder { |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 275 | std::vector<CoverageSegment> &Segments; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 276 | SmallVector<const CountedRegion *, 8> ActiveRegions; |
| 277 | |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 278 | SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {} |
| 279 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 280 | /// Start a segment with no count specified. |
| 281 | void startSegment(unsigned Line, unsigned Col) { |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 282 | DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n"); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 283 | Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false); |
| 284 | } |
| 285 | |
| 286 | /// Start a segment with the given Region's count. |
| 287 | void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry, |
| 288 | const CountedRegion &Region) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 289 | // Avoid creating empty regions. |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 290 | if (!Segments.empty() && Segments.back().Line == Line && |
| 291 | Segments.back().Col == Col) |
| 292 | Segments.pop_back(); |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 293 | DEBUG(dbgs() << "Segment at " << Line << ":" << Col); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 294 | // Set this region's count. |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 295 | if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) { |
| 296 | DEBUG(dbgs() << " with count " << Region.ExecutionCount); |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 297 | Segments.emplace_back(Line, Col, Region.ExecutionCount, IsRegionEntry); |
| 298 | } else |
| 299 | Segments.emplace_back(Line, Col, IsRegionEntry); |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 300 | DEBUG(dbgs() << "\n"); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /// Start a segment for the given region. |
| 304 | void startSegment(const CountedRegion &Region) { |
| 305 | startSegment(Region.LineStart, Region.ColumnStart, true, Region); |
| 306 | } |
| 307 | |
| 308 | /// Pop the top region off of the active stack, starting a new segment with |
| 309 | /// the containing Region's count. |
| 310 | void popRegion() { |
| 311 | const CountedRegion *Active = ActiveRegions.back(); |
| 312 | unsigned Line = Active->LineEnd, Col = Active->ColumnEnd; |
| 313 | ActiveRegions.pop_back(); |
| 314 | if (ActiveRegions.empty()) |
| 315 | startSegment(Line, Col); |
| 316 | else |
| 317 | startSegment(Line, Col, false, *ActiveRegions.back()); |
| 318 | } |
| 319 | |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 320 | void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 321 | for (const auto &Region : Regions) { |
| 322 | // Pop any regions that end before this one starts. |
| 323 | while (!ActiveRegions.empty() && |
| 324 | ActiveRegions.back()->endLoc() <= Region.startLoc()) |
| 325 | popRegion(); |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 326 | // Add this region to the stack. |
| 327 | ActiveRegions.push_back(&Region); |
| 328 | startSegment(Region); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 329 | } |
| 330 | // Pop any regions that are left in the stack. |
| 331 | while (!ActiveRegions.empty()) |
| 332 | popRegion(); |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 335 | /// Sort a nested sequence of regions from a single file. |
| 336 | static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) { |
| 337 | std::sort(Regions.begin(), Regions.end(), |
| 338 | [](const CountedRegion &LHS, const CountedRegion &RHS) { |
| 339 | if (LHS.startLoc() == RHS.startLoc()) |
| 340 | // When LHS completely contains RHS, we sort LHS first. |
| 341 | return RHS.endLoc() < LHS.endLoc(); |
| 342 | return LHS.startLoc() < RHS.startLoc(); |
| 343 | }); |
| 344 | } |
| 345 | |
| 346 | /// Combine counts of regions which cover the same area. |
| 347 | static ArrayRef<CountedRegion> |
| 348 | combineRegions(MutableArrayRef<CountedRegion> Regions) { |
| 349 | if (Regions.empty()) |
| 350 | return Regions; |
| 351 | auto Active = Regions.begin(); |
| 352 | auto End = Regions.end(); |
| 353 | for (auto I = Regions.begin() + 1; I != End; ++I) { |
| 354 | if (Active->startLoc() != I->startLoc() || |
| 355 | Active->endLoc() != I->endLoc()) { |
| 356 | // Shift to the next region. |
| 357 | ++Active; |
| 358 | if (Active != I) |
| 359 | *Active = *I; |
| 360 | continue; |
| 361 | } |
| 362 | // Merge duplicate region. |
| 363 | if (I->Kind != coverage::CounterMappingRegion::CodeRegion) |
| 364 | // Add counts only from CodeRegions. |
| 365 | continue; |
| 366 | if (Active->Kind == coverage::CounterMappingRegion::SkippedRegion) |
| 367 | // We have to overwrite SkippedRegions because of special handling |
| 368 | // of them in startSegment(). |
| 369 | *Active = *I; |
| 370 | else |
| 371 | // Otherwise, just append the count. |
| 372 | Active->ExecutionCount += I->ExecutionCount; |
| 373 | } |
| 374 | return Regions.drop_back(std::distance(++Active, End)); |
| 375 | } |
| 376 | |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 377 | public: |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 378 | /// Build a list of CoverageSegments from a list of Regions. |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 379 | static std::vector<CoverageSegment> |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 380 | buildSegments(MutableArrayRef<CountedRegion> Regions) { |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 381 | std::vector<CoverageSegment> Segments; |
| 382 | SegmentBuilder Builder(Segments); |
Igor Kudrin | ed99a96 | 2016-04-25 09:43:37 +0000 | [diff] [blame] | 383 | |
| 384 | sortNestedRegions(Regions); |
| 385 | ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions); |
| 386 | |
| 387 | Builder.buildSegmentsImpl(CombinedRegions); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 388 | return Segments; |
| 389 | } |
| 390 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 391 | } |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 392 | |
Justin Bogner | d5fca92 | 2014-11-14 01:50:32 +0000 | [diff] [blame] | 393 | std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 394 | std::vector<StringRef> Filenames; |
| 395 | for (const auto &Function : getCoveredFunctions()) |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 396 | Filenames.insert(Filenames.end(), Function.Filenames.begin(), |
| 397 | Function.Filenames.end()); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 398 | std::sort(Filenames.begin(), Filenames.end()); |
| 399 | auto Last = std::unique(Filenames.begin(), Filenames.end()); |
| 400 | Filenames.erase(Last, Filenames.end()); |
| 401 | return Filenames; |
| 402 | } |
| 403 | |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 404 | static SmallBitVector gatherFileIDs(StringRef SourceFile, |
| 405 | const FunctionRecord &Function) { |
| 406 | SmallBitVector FilenameEquivalence(Function.Filenames.size(), false); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 407 | for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I) |
| 408 | if (SourceFile == Function.Filenames[I]) |
| 409 | FilenameEquivalence[I] = true; |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 410 | return FilenameEquivalence; |
| 411 | } |
| 412 | |
Igor Kudrin | 1c14dc4 | 2016-04-18 15:36:30 +0000 | [diff] [blame] | 413 | /// 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] | 414 | static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) { |
Benjamin Kramer | 40957cc | 2015-02-12 16:30:00 +0000 | [diff] [blame] | 415 | SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 416 | for (const auto &CR : Function.CountedRegions) |
| 417 | if (CR.Kind == CounterMappingRegion::ExpansionRegion) |
Benjamin Kramer | 40957cc | 2015-02-12 16:30:00 +0000 | [diff] [blame] | 418 | IsNotExpandedFile[CR.ExpandedFileID] = false; |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 419 | int I = IsNotExpandedFile.find_first(); |
Justin Bogner | c4f5a5e | 2015-02-20 07:28:28 +0000 | [diff] [blame] | 420 | if (I == -1) |
| 421 | return None; |
| 422 | return I; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Igor Kudrin | 1c14dc4 | 2016-04-18 15:36:30 +0000 | [diff] [blame] | 425 | /// Check if SourceFile is the file that contains the definition of |
| 426 | /// the Function. Return the ID of the file in that case or None otherwise. |
| 427 | static Optional<unsigned> findMainViewFileID(StringRef SourceFile, |
| 428 | const FunctionRecord &Function) { |
| 429 | Optional<unsigned> I = findMainViewFileID(Function); |
| 430 | if (I && SourceFile == Function.Filenames[*I]) |
| 431 | return I; |
| 432 | return None; |
| 433 | } |
| 434 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 435 | static bool isExpansion(const CountedRegion &R, unsigned FileID) { |
| 436 | return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID; |
| 437 | } |
| 438 | |
| 439 | CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) { |
| 440 | CoverageData FileCoverage(Filename); |
| 441 | std::vector<coverage::CountedRegion> Regions; |
| 442 | |
| 443 | for (const auto &Function : Functions) { |
| 444 | auto MainFileID = findMainViewFileID(Filename, Function); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 445 | auto FileIDs = gatherFileIDs(Filename, Function); |
| 446 | for (const auto &CR : Function.CountedRegions) |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 447 | if (FileIDs.test(CR.FileID)) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 448 | Regions.push_back(CR); |
Igor Kudrin | 1c14dc4 | 2016-04-18 15:36:30 +0000 | [diff] [blame] | 449 | if (MainFileID && isExpansion(CR, *MainFileID)) |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 450 | FileCoverage.Expansions.emplace_back(CR, Function); |
| 451 | } |
| 452 | } |
| 453 | |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 454 | DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n"); |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 455 | FileCoverage.Segments = SegmentBuilder::buildSegments(Regions); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 456 | |
| 457 | return FileCoverage; |
| 458 | } |
| 459 | |
| 460 | std::vector<const FunctionRecord *> |
| 461 | CoverageMapping::getInstantiations(StringRef Filename) { |
| 462 | FunctionInstantiationSetCollector InstantiationSetCollector; |
| 463 | for (const auto &Function : Functions) { |
| 464 | auto MainFileID = findMainViewFileID(Filename, Function); |
| 465 | if (!MainFileID) |
| 466 | continue; |
| 467 | InstantiationSetCollector.insert(Function, *MainFileID); |
| 468 | } |
| 469 | |
| 470 | std::vector<const FunctionRecord *> Result; |
| 471 | for (const auto &InstantiationSet : InstantiationSetCollector) { |
| 472 | if (InstantiationSet.second.size() < 2) |
| 473 | continue; |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 474 | Result.insert(Result.end(), InstantiationSet.second.begin(), |
| 475 | InstantiationSet.second.end()); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 476 | } |
| 477 | return Result; |
| 478 | } |
| 479 | |
| 480 | CoverageData |
| 481 | CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) { |
| 482 | auto MainFileID = findMainViewFileID(Function); |
| 483 | if (!MainFileID) |
| 484 | return CoverageData(); |
| 485 | |
| 486 | CoverageData FunctionCoverage(Function.Filenames[*MainFileID]); |
| 487 | std::vector<coverage::CountedRegion> Regions; |
| 488 | for (const auto &CR : Function.CountedRegions) |
| 489 | if (CR.FileID == *MainFileID) { |
| 490 | Regions.push_back(CR); |
| 491 | if (isExpansion(CR, *MainFileID)) |
| 492 | FunctionCoverage.Expansions.emplace_back(CR, Function); |
| 493 | } |
| 494 | |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 495 | DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n"); |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 496 | FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 497 | |
| 498 | return FunctionCoverage; |
| 499 | } |
| 500 | |
| 501 | CoverageData |
| 502 | CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) { |
| 503 | CoverageData ExpansionCoverage( |
| 504 | Expansion.Function.Filenames[Expansion.FileID]); |
| 505 | std::vector<coverage::CountedRegion> Regions; |
| 506 | for (const auto &CR : Expansion.Function.CountedRegions) |
| 507 | if (CR.FileID == Expansion.FileID) { |
| 508 | Regions.push_back(CR); |
| 509 | if (isExpansion(CR, Expansion.FileID)) |
| 510 | ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function); |
| 511 | } |
| 512 | |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 513 | DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID |
| 514 | << "\n"); |
Igor Kudrin | c0774e6 | 2016-04-14 09:10:00 +0000 | [diff] [blame] | 515 | ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 516 | |
| 517 | return ExpansionCoverage; |
| 518 | } |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 519 | |
| 520 | namespace { |
| 521 | class CoverageMappingErrorCategoryType : public std::error_category { |
| 522 | const char *name() const LLVM_NOEXCEPT override { return "llvm.coveragemap"; } |
| 523 | std::string message(int IE) const override { |
| 524 | auto E = static_cast<coveragemap_error>(IE); |
| 525 | switch (E) { |
| 526 | case coveragemap_error::success: |
| 527 | return "Success"; |
| 528 | case coveragemap_error::eof: |
| 529 | return "End of File"; |
| 530 | case coveragemap_error::no_data_found: |
| 531 | return "No coverage data found"; |
| 532 | case coveragemap_error::unsupported_version: |
| 533 | return "Unsupported coverage format version"; |
| 534 | case coveragemap_error::truncated: |
| 535 | return "Truncated coverage data"; |
| 536 | case coveragemap_error::malformed: |
| 537 | return "Malformed coverage data"; |
| 538 | } |
| 539 | llvm_unreachable("A value of coveragemap_error has no message."); |
| 540 | } |
| 541 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 542 | } |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 543 | |
| 544 | static ManagedStatic<CoverageMappingErrorCategoryType> ErrorCategory; |
| 545 | |
Xinliang David Li | 8a5bdb5 | 2016-01-10 21:56:33 +0000 | [diff] [blame] | 546 | const std::error_category &llvm::coverage::coveragemap_category() { |
Justin Bogner | 367a9f2 | 2015-05-06 23:19:35 +0000 | [diff] [blame] | 547 | return *ErrorCategory; |
| 548 | } |