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 | |
| 15 | #include "llvm/ProfileData/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" |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 19 | #include "llvm/ProfileData/CoverageMappingReader.h" |
| 20 | #include "llvm/ProfileData/InstrProfReader.h" |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace coverage; |
| 26 | |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "coverage-mapping" |
| 28 | |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 29 | Counter CounterExpressionBuilder::get(const CounterExpression &E) { |
Justin Bogner | ad69e64 | 2014-10-02 17:14:18 +0000 | [diff] [blame] | 30 | auto It = ExpressionIndices.find(E); |
| 31 | if (It != ExpressionIndices.end()) |
| 32 | return Counter::getExpression(It->second); |
| 33 | unsigned I = Expressions.size(); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 34 | Expressions.push_back(E); |
Justin Bogner | ad69e64 | 2014-10-02 17:14:18 +0000 | [diff] [blame] | 35 | ExpressionIndices[E] = I; |
| 36 | return Counter::getExpression(I); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 39 | void CounterExpressionBuilder::extractTerms( |
| 40 | Counter C, int Sign, SmallVectorImpl<std::pair<unsigned, int>> &Terms) { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 41 | switch (C.getKind()) { |
| 42 | case Counter::Zero: |
| 43 | break; |
| 44 | case Counter::CounterValueReference: |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 45 | Terms.push_back(std::make_pair(C.getCounterID(), Sign)); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 46 | break; |
| 47 | case Counter::Expression: |
| 48 | const auto &E = Expressions[C.getExpressionID()]; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 49 | extractTerms(E.LHS, Sign, Terms); |
| 50 | extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign, |
| 51 | Terms); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 52 | break; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) { |
| 57 | // Gather constant terms. |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 58 | llvm::SmallVector<std::pair<unsigned, int>, 32> Terms; |
| 59 | extractTerms(ExpressionTree, +1, Terms); |
| 60 | |
| 61 | // If there are no terms, this is just a zero. The algorithm below assumes at |
| 62 | // least one term. |
| 63 | if (Terms.size() == 0) |
| 64 | return Counter::getZero(); |
| 65 | |
| 66 | // Group the terms by counter ID. |
| 67 | std::sort(Terms.begin(), Terms.end(), |
| 68 | [](const std::pair<unsigned, int> &LHS, |
| 69 | const std::pair<unsigned, int> &RHS) { |
| 70 | return LHS.first < RHS.first; |
| 71 | }); |
| 72 | |
| 73 | // Combine terms by counter ID to eliminate counters that sum to zero. |
| 74 | auto Prev = Terms.begin(); |
| 75 | for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) { |
| 76 | if (I->first == Prev->first) { |
| 77 | Prev->second += I->second; |
| 78 | continue; |
| 79 | } |
| 80 | ++Prev; |
| 81 | *Prev = *I; |
| 82 | } |
| 83 | Terms.erase(++Prev, Terms.end()); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 84 | |
| 85 | Counter C; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 86 | // Create additions. We do this before subtractions to avoid constructs like |
| 87 | // ((0 - X) + Y), as opposed to (Y - X). |
| 88 | for (auto Term : Terms) { |
| 89 | if (Term.second <= 0) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 90 | continue; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 91 | for (int I = 0; I < Term.second; ++I) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 92 | if (C.isZero()) |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 93 | C = Counter::getCounter(Term.first); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 94 | else |
| 95 | C = get(CounterExpression(CounterExpression::Add, C, |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 96 | Counter::getCounter(Term.first))); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Create subtractions. |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 100 | for (auto Term : Terms) { |
| 101 | if (Term.second >= 0) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 102 | continue; |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 103 | for (int I = 0; I < -Term.second; ++I) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 104 | C = get(CounterExpression(CounterExpression::Subtract, C, |
Justin Bogner | f9535c4 | 2014-10-02 16:43:31 +0000 | [diff] [blame] | 105 | Counter::getCounter(Term.first))); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 106 | } |
| 107 | return C; |
| 108 | } |
| 109 | |
| 110 | Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) { |
| 111 | return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS))); |
| 112 | } |
| 113 | |
| 114 | Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) { |
| 115 | return simplify( |
| 116 | get(CounterExpression(CounterExpression::Subtract, LHS, RHS))); |
| 117 | } |
| 118 | |
| 119 | void CounterMappingContext::dump(const Counter &C, |
| 120 | llvm::raw_ostream &OS) const { |
| 121 | switch (C.getKind()) { |
| 122 | case Counter::Zero: |
| 123 | OS << '0'; |
| 124 | return; |
| 125 | case Counter::CounterValueReference: |
| 126 | OS << '#' << C.getCounterID(); |
| 127 | break; |
| 128 | case Counter::Expression: { |
| 129 | if (C.getExpressionID() >= Expressions.size()) |
| 130 | return; |
| 131 | const auto &E = Expressions[C.getExpressionID()]; |
| 132 | OS << '('; |
Alex Lorenz | a422911c | 2014-07-29 19:58:16 +0000 | [diff] [blame] | 133 | dump(E.LHS, OS); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 134 | OS << (E.Kind == CounterExpression::Subtract ? " - " : " + "); |
Alex Lorenz | a422911c | 2014-07-29 19:58:16 +0000 | [diff] [blame] | 135 | dump(E.RHS, OS); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 136 | OS << ')'; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | if (CounterValues.empty()) |
| 141 | return; |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 142 | ErrorOr<int64_t> Value = evaluate(C); |
| 143 | if (!Value) |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 144 | return; |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 145 | OS << '[' << *Value << ']'; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 148 | ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const { |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 149 | switch (C.getKind()) { |
| 150 | case Counter::Zero: |
| 151 | return 0; |
| 152 | case Counter::CounterValueReference: |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 153 | if (C.getCounterID() >= CounterValues.size()) |
Justin Bogner | 3f18834 | 2014-09-08 21:31:43 +0000 | [diff] [blame] | 154 | return std::make_error_code(std::errc::argument_out_of_domain); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 155 | return CounterValues[C.getCounterID()]; |
| 156 | case Counter::Expression: { |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 157 | if (C.getExpressionID() >= Expressions.size()) |
Justin Bogner | 3f18834 | 2014-09-08 21:31:43 +0000 | [diff] [blame] | 158 | return std::make_error_code(std::errc::argument_out_of_domain); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 159 | const auto &E = Expressions[C.getExpressionID()]; |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 160 | ErrorOr<int64_t> LHS = evaluate(E.LHS); |
| 161 | if (!LHS) |
| 162 | return LHS; |
| 163 | ErrorOr<int64_t> RHS = evaluate(E.RHS); |
| 164 | if (!RHS) |
| 165 | return RHS; |
| 166 | return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS; |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
Justin Bogner | 85b0a03 | 2014-09-08 21:04:00 +0000 | [diff] [blame] | 169 | llvm_unreachable("Unhandled CounterKind"); |
Alex Lorenz | a20a5d5 | 2014-07-24 23:57:54 +0000 | [diff] [blame] | 170 | } |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 171 | |
Justin Bogner | d5fca92 | 2014-11-14 01:50:32 +0000 | [diff] [blame] | 172 | void FunctionRecordIterator::skipOtherFiles() { |
| 173 | while (Current != Records.end() && !Filename.empty() && |
| 174 | Filename != Current->Filenames[0]) |
| 175 | ++Current; |
| 176 | if (Current == Records.end()) |
| 177 | *this = FunctionRecordIterator(); |
| 178 | } |
| 179 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 180 | ErrorOr<std::unique_ptr<CoverageMapping>> |
Justin Bogner | 1d29c08 | 2015-02-18 18:01:14 +0000 | [diff] [blame] | 181 | CoverageMapping::load(CoverageMappingReader &CoverageReader, |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 182 | IndexedInstrProfReader &ProfileReader) { |
| 183 | auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping()); |
| 184 | |
| 185 | std::vector<uint64_t> Counts; |
| 186 | for (const auto &Record : CoverageReader) { |
| 187 | Counts.clear(); |
| 188 | if (std::error_code EC = ProfileReader.getFunctionCounts( |
| 189 | Record.FunctionName, Record.FunctionHash, Counts)) { |
| 190 | if (EC != instrprof_error::hash_mismatch && |
| 191 | EC != instrprof_error::unknown_function) |
| 192 | return EC; |
| 193 | Coverage->MismatchedFunctionCount++; |
| 194 | continue; |
| 195 | } |
| 196 | |
Alex Lorenz | cb1702d | 2014-09-30 12:45:13 +0000 | [diff] [blame] | 197 | assert(Counts.size() != 0 && "Function's counts are empty"); |
| 198 | FunctionRecord Function(Record.FunctionName, Record.Filenames, |
| 199 | Counts.front()); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 200 | CounterMappingContext Ctx(Record.Expressions, Counts); |
| 201 | for (const auto &Region : Record.MappingRegions) { |
| 202 | ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count); |
| 203 | if (!ExecutionCount) |
| 204 | break; |
| 205 | Function.CountedRegions.push_back(CountedRegion(Region, *ExecutionCount)); |
| 206 | } |
| 207 | if (Function.CountedRegions.size() != Record.MappingRegions.size()) { |
| 208 | Coverage->MismatchedFunctionCount++; |
| 209 | continue; |
| 210 | } |
| 211 | |
Benjamin Kramer | e12a6ba | 2014-10-03 18:33:16 +0000 | [diff] [blame] | 212 | Coverage->Functions.push_back(std::move(Function)); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return std::move(Coverage); |
| 216 | } |
| 217 | |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 218 | ErrorOr<std::unique_ptr<CoverageMapping>> |
| 219 | CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename) { |
| 220 | auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename); |
| 221 | if (auto EC = CounterMappingBuff.getError()) |
| 222 | return EC; |
| 223 | ObjectFileCoverageMappingReader CoverageReader(CounterMappingBuff.get()); |
| 224 | if (auto EC = CoverageReader.readHeader()) |
| 225 | return EC; |
Justin Bogner | ab89ed7 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 226 | auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename); |
| 227 | if (auto EC = ProfileReaderOrErr.getError()) |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 228 | return EC; |
Justin Bogner | ab89ed7 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 229 | auto ProfileReader = std::move(ProfileReaderOrErr.get()); |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 230 | return load(CoverageReader, *ProfileReader); |
| 231 | } |
| 232 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 233 | namespace { |
| 234 | /// \brief Distributes functions into instantiation sets. |
| 235 | /// |
| 236 | /// An instantiation set is a collection of functions that have the same source |
| 237 | /// code, ie, template functions specializations. |
| 238 | class FunctionInstantiationSetCollector { |
| 239 | typedef DenseMap<std::pair<unsigned, unsigned>, |
| 240 | std::vector<const FunctionRecord *>> MapT; |
| 241 | MapT InstantiatedFunctions; |
| 242 | |
| 243 | public: |
| 244 | void insert(const FunctionRecord &Function, unsigned FileID) { |
| 245 | auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end(); |
| 246 | while (I != E && I->FileID != FileID) |
| 247 | ++I; |
| 248 | assert(I != E && "function does not cover the given file"); |
| 249 | auto &Functions = InstantiatedFunctions[I->startLoc()]; |
| 250 | Functions.push_back(&Function); |
| 251 | } |
| 252 | |
| 253 | MapT::iterator begin() { return InstantiatedFunctions.begin(); } |
| 254 | |
| 255 | MapT::iterator end() { return InstantiatedFunctions.end(); } |
| 256 | }; |
| 257 | |
| 258 | class SegmentBuilder { |
| 259 | std::vector<CoverageSegment> Segments; |
| 260 | SmallVector<const CountedRegion *, 8> ActiveRegions; |
| 261 | |
| 262 | /// Start a segment with no count specified. |
| 263 | void startSegment(unsigned Line, unsigned Col) { |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 264 | DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n"); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 265 | Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false); |
| 266 | } |
| 267 | |
| 268 | /// Start a segment with the given Region's count. |
| 269 | void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry, |
| 270 | const CountedRegion &Region) { |
| 271 | if (Segments.empty()) |
| 272 | Segments.emplace_back(Line, Col, IsRegionEntry); |
| 273 | CoverageSegment S = Segments.back(); |
| 274 | // Avoid creating empty regions. |
| 275 | if (S.Line != Line || S.Col != Col) { |
| 276 | Segments.emplace_back(Line, Col, IsRegionEntry); |
| 277 | S = Segments.back(); |
| 278 | } |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 279 | DEBUG(dbgs() << "Segment at " << Line << ":" << Col); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 280 | // Set this region's count. |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 281 | if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) { |
| 282 | DEBUG(dbgs() << " with count " << Region.ExecutionCount); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 283 | Segments.back().setCount(Region.ExecutionCount); |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 284 | } |
| 285 | DEBUG(dbgs() << "\n"); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /// Start a segment for the given region. |
| 289 | void startSegment(const CountedRegion &Region) { |
| 290 | startSegment(Region.LineStart, Region.ColumnStart, true, Region); |
| 291 | } |
| 292 | |
| 293 | /// Pop the top region off of the active stack, starting a new segment with |
| 294 | /// the containing Region's count. |
| 295 | void popRegion() { |
| 296 | const CountedRegion *Active = ActiveRegions.back(); |
| 297 | unsigned Line = Active->LineEnd, Col = Active->ColumnEnd; |
| 298 | ActiveRegions.pop_back(); |
| 299 | if (ActiveRegions.empty()) |
| 300 | startSegment(Line, Col); |
| 301 | else |
| 302 | startSegment(Line, Col, false, *ActiveRegions.back()); |
| 303 | } |
| 304 | |
| 305 | public: |
| 306 | /// Build a list of CoverageSegments from a sorted list of Regions. |
| 307 | std::vector<CoverageSegment> buildSegments(ArrayRef<CountedRegion> Regions) { |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 308 | const CountedRegion *PrevRegion = nullptr; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 309 | for (const auto &Region : Regions) { |
| 310 | // Pop any regions that end before this one starts. |
| 311 | while (!ActiveRegions.empty() && |
| 312 | ActiveRegions.back()->endLoc() <= Region.startLoc()) |
| 313 | popRegion(); |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 314 | if (PrevRegion && PrevRegion->startLoc() == Region.startLoc() && |
| 315 | PrevRegion->endLoc() == Region.endLoc()) { |
Justin Bogner | b35a72a | 2014-09-25 00:34:18 +0000 | [diff] [blame] | 316 | if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) |
| 317 | Segments.back().addCount(Region.ExecutionCount); |
| 318 | } else { |
| 319 | // Add this region to the stack. |
| 320 | ActiveRegions.push_back(&Region); |
| 321 | startSegment(Region); |
| 322 | } |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 323 | PrevRegion = &Region; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 324 | } |
| 325 | // Pop any regions that are left in the stack. |
| 326 | while (!ActiveRegions.empty()) |
| 327 | popRegion(); |
| 328 | return Segments; |
| 329 | } |
| 330 | }; |
| 331 | } |
| 332 | |
Justin Bogner | d5fca92 | 2014-11-14 01:50:32 +0000 | [diff] [blame] | 333 | std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 334 | std::vector<StringRef> Filenames; |
| 335 | for (const auto &Function : getCoveredFunctions()) |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 336 | Filenames.insert(Filenames.end(), Function.Filenames.begin(), |
| 337 | Function.Filenames.end()); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 338 | std::sort(Filenames.begin(), Filenames.end()); |
| 339 | auto Last = std::unique(Filenames.begin(), Filenames.end()); |
| 340 | Filenames.erase(Last, Filenames.end()); |
| 341 | return Filenames; |
| 342 | } |
| 343 | |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 344 | static SmallBitVector gatherFileIDs(StringRef SourceFile, |
| 345 | const FunctionRecord &Function) { |
| 346 | SmallBitVector FilenameEquivalence(Function.Filenames.size(), false); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 347 | for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I) |
| 348 | if (SourceFile == Function.Filenames[I]) |
| 349 | FilenameEquivalence[I] = true; |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 350 | return FilenameEquivalence; |
| 351 | } |
| 352 | |
| 353 | static Optional<unsigned> findMainViewFileID(StringRef SourceFile, |
| 354 | const FunctionRecord &Function) { |
| 355 | SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true); |
| 356 | SmallBitVector FilenameEquivalence = gatherFileIDs(SourceFile, Function); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 357 | for (const auto &CR : Function.CountedRegions) |
| 358 | if (CR.Kind == CounterMappingRegion::ExpansionRegion && |
| 359 | FilenameEquivalence[CR.FileID]) |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 360 | IsNotExpandedFile[CR.ExpandedFileID] = false; |
| 361 | IsNotExpandedFile &= FilenameEquivalence; |
| 362 | int I = IsNotExpandedFile.find_first(); |
| 363 | return I != -1 ? I : None; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) { |
Benjamin Kramer | 40957cc | 2015-02-12 16:30:00 +0000 | [diff] [blame] | 367 | SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 368 | for (const auto &CR : Function.CountedRegions) |
| 369 | if (CR.Kind == CounterMappingRegion::ExpansionRegion) |
Benjamin Kramer | 40957cc | 2015-02-12 16:30:00 +0000 | [diff] [blame] | 370 | IsNotExpandedFile[CR.ExpandedFileID] = false; |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 371 | int I = IsNotExpandedFile.find_first(); |
| 372 | return I != -1 ? I : None; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /// Sort a nested sequence of regions from a single file. |
| 376 | template <class It> static void sortNestedRegions(It First, It Last) { |
| 377 | std::sort(First, Last, |
| 378 | [](const CountedRegion &LHS, const CountedRegion &RHS) { |
| 379 | if (LHS.startLoc() == RHS.startLoc()) |
| 380 | // When LHS completely contains RHS, we sort LHS first. |
| 381 | return RHS.endLoc() < LHS.endLoc(); |
| 382 | return LHS.startLoc() < RHS.startLoc(); |
| 383 | }); |
| 384 | } |
| 385 | |
| 386 | static bool isExpansion(const CountedRegion &R, unsigned FileID) { |
| 387 | return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID; |
| 388 | } |
| 389 | |
| 390 | CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) { |
| 391 | CoverageData FileCoverage(Filename); |
| 392 | std::vector<coverage::CountedRegion> Regions; |
| 393 | |
| 394 | for (const auto &Function : Functions) { |
| 395 | auto MainFileID = findMainViewFileID(Filename, Function); |
| 396 | if (!MainFileID) |
| 397 | continue; |
| 398 | auto FileIDs = gatherFileIDs(Filename, Function); |
| 399 | for (const auto &CR : Function.CountedRegions) |
Benjamin Kramer | 71e1eb5 | 2015-02-12 16:18:07 +0000 | [diff] [blame] | 400 | if (FileIDs.test(CR.FileID)) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 401 | Regions.push_back(CR); |
| 402 | if (isExpansion(CR, *MainFileID)) |
| 403 | FileCoverage.Expansions.emplace_back(CR, Function); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | sortNestedRegions(Regions.begin(), Regions.end()); |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 408 | DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n"); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 409 | FileCoverage.Segments = SegmentBuilder().buildSegments(Regions); |
| 410 | |
| 411 | return FileCoverage; |
| 412 | } |
| 413 | |
| 414 | std::vector<const FunctionRecord *> |
| 415 | CoverageMapping::getInstantiations(StringRef Filename) { |
| 416 | FunctionInstantiationSetCollector InstantiationSetCollector; |
| 417 | for (const auto &Function : Functions) { |
| 418 | auto MainFileID = findMainViewFileID(Filename, Function); |
| 419 | if (!MainFileID) |
| 420 | continue; |
| 421 | InstantiationSetCollector.insert(Function, *MainFileID); |
| 422 | } |
| 423 | |
| 424 | std::vector<const FunctionRecord *> Result; |
| 425 | for (const auto &InstantiationSet : InstantiationSetCollector) { |
| 426 | if (InstantiationSet.second.size() < 2) |
| 427 | continue; |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 428 | Result.insert(Result.end(), InstantiationSet.second.begin(), |
| 429 | InstantiationSet.second.end()); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 430 | } |
| 431 | return Result; |
| 432 | } |
| 433 | |
| 434 | CoverageData |
| 435 | CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) { |
| 436 | auto MainFileID = findMainViewFileID(Function); |
| 437 | if (!MainFileID) |
| 438 | return CoverageData(); |
| 439 | |
| 440 | CoverageData FunctionCoverage(Function.Filenames[*MainFileID]); |
| 441 | std::vector<coverage::CountedRegion> Regions; |
| 442 | for (const auto &CR : Function.CountedRegions) |
| 443 | if (CR.FileID == *MainFileID) { |
| 444 | Regions.push_back(CR); |
| 445 | if (isExpansion(CR, *MainFileID)) |
| 446 | FunctionCoverage.Expansions.emplace_back(CR, Function); |
| 447 | } |
| 448 | |
| 449 | sortNestedRegions(Regions.begin(), Regions.end()); |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 450 | DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n"); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 451 | FunctionCoverage.Segments = SegmentBuilder().buildSegments(Regions); |
| 452 | |
| 453 | return FunctionCoverage; |
| 454 | } |
| 455 | |
| 456 | CoverageData |
| 457 | CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) { |
| 458 | CoverageData ExpansionCoverage( |
| 459 | Expansion.Function.Filenames[Expansion.FileID]); |
| 460 | std::vector<coverage::CountedRegion> Regions; |
| 461 | for (const auto &CR : Expansion.Function.CountedRegions) |
| 462 | if (CR.FileID == Expansion.FileID) { |
| 463 | Regions.push_back(CR); |
| 464 | if (isExpansion(CR, Expansion.FileID)) |
| 465 | ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function); |
| 466 | } |
| 467 | |
| 468 | sortNestedRegions(Regions.begin(), Regions.end()); |
Justin Bogner | 3c0f124 | 2015-01-24 20:58:52 +0000 | [diff] [blame] | 469 | DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID |
| 470 | << "\n"); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 471 | ExpansionCoverage.Segments = SegmentBuilder().buildSegments(Regions); |
| 472 | |
| 473 | return ExpansionCoverage; |
| 474 | } |