blob: 31213d7fb2d9568b26efa97eaa386e5c6ac6e186 [file] [log] [blame]
Alex Lorenza20a5d52014-07-24 23:57:54 +00001//=-- 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 Bogner953e2402014-09-20 15:31:56 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/Optional.h"
Benjamin Kramer71e1eb52015-02-12 16:18:07 +000018#include "llvm/ADT/SmallBitVector.h"
Justin Bogner953e2402014-09-20 15:31:56 +000019#include "llvm/ProfileData/CoverageMappingReader.h"
20#include "llvm/ProfileData/InstrProfReader.h"
Justin Bognerb35a72a2014-09-25 00:34:18 +000021#include "llvm/Support/Debug.h"
Justin Bogner85b0a032014-09-08 21:04:00 +000022#include "llvm/Support/ErrorHandling.h"
Alex Lorenza20a5d52014-07-24 23:57:54 +000023
24using namespace llvm;
25using namespace coverage;
26
Justin Bognerb35a72a2014-09-25 00:34:18 +000027#define DEBUG_TYPE "coverage-mapping"
28
Alex Lorenza20a5d52014-07-24 23:57:54 +000029Counter CounterExpressionBuilder::get(const CounterExpression &E) {
Justin Bognerad69e642014-10-02 17:14:18 +000030 auto It = ExpressionIndices.find(E);
31 if (It != ExpressionIndices.end())
32 return Counter::getExpression(It->second);
33 unsigned I = Expressions.size();
Alex Lorenza20a5d52014-07-24 23:57:54 +000034 Expressions.push_back(E);
Justin Bognerad69e642014-10-02 17:14:18 +000035 ExpressionIndices[E] = I;
36 return Counter::getExpression(I);
Alex Lorenza20a5d52014-07-24 23:57:54 +000037}
38
Justin Bognerf9535c42014-10-02 16:43:31 +000039void CounterExpressionBuilder::extractTerms(
40 Counter C, int Sign, SmallVectorImpl<std::pair<unsigned, int>> &Terms) {
Alex Lorenza20a5d52014-07-24 23:57:54 +000041 switch (C.getKind()) {
42 case Counter::Zero:
43 break;
44 case Counter::CounterValueReference:
Justin Bognerf9535c42014-10-02 16:43:31 +000045 Terms.push_back(std::make_pair(C.getCounterID(), Sign));
Alex Lorenza20a5d52014-07-24 23:57:54 +000046 break;
47 case Counter::Expression:
48 const auto &E = Expressions[C.getExpressionID()];
Justin Bognerf9535c42014-10-02 16:43:31 +000049 extractTerms(E.LHS, Sign, Terms);
50 extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign,
51 Terms);
Alex Lorenza20a5d52014-07-24 23:57:54 +000052 break;
53 }
54}
55
56Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
57 // Gather constant terms.
Justin Bognerf9535c42014-10-02 16:43:31 +000058 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 Lorenza20a5d52014-07-24 23:57:54 +000084
85 Counter C;
Justin Bognerf9535c42014-10-02 16:43:31 +000086 // 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 Lorenza20a5d52014-07-24 23:57:54 +000090 continue;
Justin Bognerf9535c42014-10-02 16:43:31 +000091 for (int I = 0; I < Term.second; ++I)
Alex Lorenza20a5d52014-07-24 23:57:54 +000092 if (C.isZero())
Justin Bognerf9535c42014-10-02 16:43:31 +000093 C = Counter::getCounter(Term.first);
Alex Lorenza20a5d52014-07-24 23:57:54 +000094 else
95 C = get(CounterExpression(CounterExpression::Add, C,
Justin Bognerf9535c42014-10-02 16:43:31 +000096 Counter::getCounter(Term.first)));
Alex Lorenza20a5d52014-07-24 23:57:54 +000097 }
98
99 // Create subtractions.
Justin Bognerf9535c42014-10-02 16:43:31 +0000100 for (auto Term : Terms) {
101 if (Term.second >= 0)
Alex Lorenza20a5d52014-07-24 23:57:54 +0000102 continue;
Justin Bognerf9535c42014-10-02 16:43:31 +0000103 for (int I = 0; I < -Term.second; ++I)
Alex Lorenza20a5d52014-07-24 23:57:54 +0000104 C = get(CounterExpression(CounterExpression::Subtract, C,
Justin Bognerf9535c42014-10-02 16:43:31 +0000105 Counter::getCounter(Term.first)));
Alex Lorenza20a5d52014-07-24 23:57:54 +0000106 }
107 return C;
108}
109
110Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) {
111 return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS)));
112}
113
114Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) {
115 return simplify(
116 get(CounterExpression(CounterExpression::Subtract, LHS, RHS)));
117}
118
119void 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 Lorenza422911c2014-07-29 19:58:16 +0000133 dump(E.LHS, OS);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000134 OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
Alex Lorenza422911c2014-07-29 19:58:16 +0000135 dump(E.RHS, OS);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000136 OS << ')';
137 break;
138 }
139 }
140 if (CounterValues.empty())
141 return;
Justin Bogner85b0a032014-09-08 21:04:00 +0000142 ErrorOr<int64_t> Value = evaluate(C);
143 if (!Value)
Alex Lorenza20a5d52014-07-24 23:57:54 +0000144 return;
Justin Bogner85b0a032014-09-08 21:04:00 +0000145 OS << '[' << *Value << ']';
Alex Lorenza20a5d52014-07-24 23:57:54 +0000146}
147
Justin Bogner85b0a032014-09-08 21:04:00 +0000148ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000149 switch (C.getKind()) {
150 case Counter::Zero:
151 return 0;
152 case Counter::CounterValueReference:
Justin Bogner85b0a032014-09-08 21:04:00 +0000153 if (C.getCounterID() >= CounterValues.size())
Justin Bogner3f188342014-09-08 21:31:43 +0000154 return std::make_error_code(std::errc::argument_out_of_domain);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000155 return CounterValues[C.getCounterID()];
156 case Counter::Expression: {
Justin Bogner85b0a032014-09-08 21:04:00 +0000157 if (C.getExpressionID() >= Expressions.size())
Justin Bogner3f188342014-09-08 21:31:43 +0000158 return std::make_error_code(std::errc::argument_out_of_domain);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000159 const auto &E = Expressions[C.getExpressionID()];
Justin Bogner85b0a032014-09-08 21:04:00 +0000160 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 Lorenza20a5d52014-07-24 23:57:54 +0000167 }
168 }
Justin Bogner85b0a032014-09-08 21:04:00 +0000169 llvm_unreachable("Unhandled CounterKind");
Alex Lorenza20a5d52014-07-24 23:57:54 +0000170}
Justin Bogner953e2402014-09-20 15:31:56 +0000171
Justin Bognerd5fca922014-11-14 01:50:32 +0000172void 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 Bogner953e2402014-09-20 15:31:56 +0000180ErrorOr<std::unique_ptr<CoverageMapping>>
Justin Bogner1d29c082015-02-18 18:01:14 +0000181CoverageMapping::load(CoverageMappingReader &CoverageReader,
Justin Bogner953e2402014-09-20 15:31:56 +0000182 IndexedInstrProfReader &ProfileReader) {
183 auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
184
185 std::vector<uint64_t> Counts;
186 for (const auto &Record : CoverageReader) {
Justin Bogner428c6052015-02-18 18:40:46 +0000187 CounterMappingContext Ctx(Record.Expressions);
188
Justin Bogner953e2402014-09-20 15:31:56 +0000189 Counts.clear();
190 if (std::error_code EC = ProfileReader.getFunctionCounts(
191 Record.FunctionName, Record.FunctionHash, Counts)) {
Justin Bogner428c6052015-02-18 18:40:46 +0000192 if (EC == instrprof_error::hash_mismatch) {
193 Coverage->MismatchedFunctionCount++;
194 continue;
195 } else if (EC != instrprof_error::unknown_function)
Justin Bogner953e2402014-09-20 15:31:56 +0000196 return EC;
Justin Bogner428c6052015-02-18 18:40:46 +0000197 } else
198 Ctx.setCounts(Counts);
Justin Bogner953e2402014-09-20 15:31:56 +0000199
Justin Bogner428c6052015-02-18 18:40:46 +0000200 assert(!Record.MappingRegions.empty() && "Function has no regions");
201 FunctionRecord Function(Record.FunctionName, Record.Filenames);
Justin Bogner953e2402014-09-20 15:31:56 +0000202 for (const auto &Region : Record.MappingRegions) {
203 ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
204 if (!ExecutionCount)
205 break;
Justin Bogner428c6052015-02-18 18:40:46 +0000206 Function.pushRegion(Region, *ExecutionCount);
Justin Bogner953e2402014-09-20 15:31:56 +0000207 }
208 if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
209 Coverage->MismatchedFunctionCount++;
210 continue;
211 }
212
Benjamin Kramere12a6ba2014-10-03 18:33:16 +0000213 Coverage->Functions.push_back(std::move(Function));
Justin Bogner953e2402014-09-20 15:31:56 +0000214 }
215
216 return std::move(Coverage);
217}
218
Justin Bogner19a93ba2014-09-20 17:19:52 +0000219ErrorOr<std::unique_ptr<CoverageMapping>>
220CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename) {
221 auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
Justin Bogner43e51632015-02-26 20:06:28 +0000222 if (std::error_code EC = CounterMappingBuff.getError())
Justin Bogner19a93ba2014-09-20 17:19:52 +0000223 return EC;
Justin Bogner43e51632015-02-26 20:06:28 +0000224 auto CoverageReaderOrErr =
225 BinaryCoverageReader::create(CounterMappingBuff.get());
226 if (std::error_code EC = CoverageReaderOrErr.getError())
Justin Bogner19a93ba2014-09-20 17:19:52 +0000227 return EC;
Justin Bogner43e51632015-02-26 20:06:28 +0000228 auto CoverageReader = std::move(CoverageReaderOrErr.get());
Justin Bognerab89ed72015-02-16 21:28:58 +0000229 auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
230 if (auto EC = ProfileReaderOrErr.getError())
Justin Bogner19a93ba2014-09-20 17:19:52 +0000231 return EC;
Justin Bognerab89ed72015-02-16 21:28:58 +0000232 auto ProfileReader = std::move(ProfileReaderOrErr.get());
Justin Bogner43e51632015-02-26 20:06:28 +0000233 return load(*CoverageReader, *ProfileReader);
Justin Bogner19a93ba2014-09-20 17:19:52 +0000234}
235
Justin Bogner953e2402014-09-20 15:31:56 +0000236namespace {
237/// \brief Distributes functions into instantiation sets.
238///
239/// An instantiation set is a collection of functions that have the same source
240/// code, ie, template functions specializations.
241class FunctionInstantiationSetCollector {
242 typedef DenseMap<std::pair<unsigned, unsigned>,
243 std::vector<const FunctionRecord *>> MapT;
244 MapT InstantiatedFunctions;
245
246public:
247 void insert(const FunctionRecord &Function, unsigned FileID) {
248 auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
249 while (I != E && I->FileID != FileID)
250 ++I;
251 assert(I != E && "function does not cover the given file");
252 auto &Functions = InstantiatedFunctions[I->startLoc()];
253 Functions.push_back(&Function);
254 }
255
256 MapT::iterator begin() { return InstantiatedFunctions.begin(); }
257
258 MapT::iterator end() { return InstantiatedFunctions.end(); }
259};
260
261class SegmentBuilder {
262 std::vector<CoverageSegment> Segments;
263 SmallVector<const CountedRegion *, 8> ActiveRegions;
264
265 /// Start a segment with no count specified.
266 void startSegment(unsigned Line, unsigned Col) {
Justin Bognerb35a72a2014-09-25 00:34:18 +0000267 DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000268 Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false);
269 }
270
271 /// Start a segment with the given Region's count.
272 void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry,
273 const CountedRegion &Region) {
274 if (Segments.empty())
275 Segments.emplace_back(Line, Col, IsRegionEntry);
276 CoverageSegment S = Segments.back();
277 // Avoid creating empty regions.
278 if (S.Line != Line || S.Col != Col) {
279 Segments.emplace_back(Line, Col, IsRegionEntry);
280 S = Segments.back();
281 }
Justin Bognerb35a72a2014-09-25 00:34:18 +0000282 DEBUG(dbgs() << "Segment at " << Line << ":" << Col);
Justin Bogner953e2402014-09-20 15:31:56 +0000283 // Set this region's count.
Justin Bognerb35a72a2014-09-25 00:34:18 +0000284 if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) {
285 DEBUG(dbgs() << " with count " << Region.ExecutionCount);
Justin Bogner953e2402014-09-20 15:31:56 +0000286 Segments.back().setCount(Region.ExecutionCount);
Justin Bognerb35a72a2014-09-25 00:34:18 +0000287 }
288 DEBUG(dbgs() << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000289 }
290
291 /// Start a segment for the given region.
292 void startSegment(const CountedRegion &Region) {
293 startSegment(Region.LineStart, Region.ColumnStart, true, Region);
294 }
295
296 /// Pop the top region off of the active stack, starting a new segment with
297 /// the containing Region's count.
298 void popRegion() {
299 const CountedRegion *Active = ActiveRegions.back();
300 unsigned Line = Active->LineEnd, Col = Active->ColumnEnd;
301 ActiveRegions.pop_back();
302 if (ActiveRegions.empty())
303 startSegment(Line, Col);
304 else
305 startSegment(Line, Col, false, *ActiveRegions.back());
306 }
307
308public:
309 /// Build a list of CoverageSegments from a sorted list of Regions.
310 std::vector<CoverageSegment> buildSegments(ArrayRef<CountedRegion> Regions) {
Justin Bogner3c0f1242015-01-24 20:58:52 +0000311 const CountedRegion *PrevRegion = nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000312 for (const auto &Region : Regions) {
313 // Pop any regions that end before this one starts.
314 while (!ActiveRegions.empty() &&
315 ActiveRegions.back()->endLoc() <= Region.startLoc())
316 popRegion();
Justin Bogner3c0f1242015-01-24 20:58:52 +0000317 if (PrevRegion && PrevRegion->startLoc() == Region.startLoc() &&
318 PrevRegion->endLoc() == Region.endLoc()) {
Justin Bogner11ae7782015-02-18 19:01:06 +0000319 if (Region.Kind == coverage::CounterMappingRegion::CodeRegion)
Justin Bognerb35a72a2014-09-25 00:34:18 +0000320 Segments.back().addCount(Region.ExecutionCount);
321 } else {
322 // Add this region to the stack.
323 ActiveRegions.push_back(&Region);
324 startSegment(Region);
325 }
Justin Bogner3c0f1242015-01-24 20:58:52 +0000326 PrevRegion = &Region;
Justin Bogner953e2402014-09-20 15:31:56 +0000327 }
328 // Pop any regions that are left in the stack.
329 while (!ActiveRegions.empty())
330 popRegion();
331 return Segments;
332 }
333};
334}
335
Justin Bognerd5fca922014-11-14 01:50:32 +0000336std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
Justin Bogner953e2402014-09-20 15:31:56 +0000337 std::vector<StringRef> Filenames;
338 for (const auto &Function : getCoveredFunctions())
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000339 Filenames.insert(Filenames.end(), Function.Filenames.begin(),
340 Function.Filenames.end());
Justin Bogner953e2402014-09-20 15:31:56 +0000341 std::sort(Filenames.begin(), Filenames.end());
342 auto Last = std::unique(Filenames.begin(), Filenames.end());
343 Filenames.erase(Last, Filenames.end());
344 return Filenames;
345}
346
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000347static SmallBitVector gatherFileIDs(StringRef SourceFile,
348 const FunctionRecord &Function) {
349 SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
Justin Bogner953e2402014-09-20 15:31:56 +0000350 for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
351 if (SourceFile == Function.Filenames[I])
352 FilenameEquivalence[I] = true;
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000353 return FilenameEquivalence;
354}
355
356static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
357 const FunctionRecord &Function) {
358 SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
359 SmallBitVector FilenameEquivalence = gatherFileIDs(SourceFile, Function);
Justin Bogner953e2402014-09-20 15:31:56 +0000360 for (const auto &CR : Function.CountedRegions)
361 if (CR.Kind == CounterMappingRegion::ExpansionRegion &&
362 FilenameEquivalence[CR.FileID])
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000363 IsNotExpandedFile[CR.ExpandedFileID] = false;
364 IsNotExpandedFile &= FilenameEquivalence;
365 int I = IsNotExpandedFile.find_first();
Justin Bognerc4f5a5e2015-02-20 07:28:28 +0000366 if (I == -1)
367 return None;
368 return I;
Justin Bogner953e2402014-09-20 15:31:56 +0000369}
370
371static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
Benjamin Kramer40957cc2015-02-12 16:30:00 +0000372 SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
Justin Bogner953e2402014-09-20 15:31:56 +0000373 for (const auto &CR : Function.CountedRegions)
374 if (CR.Kind == CounterMappingRegion::ExpansionRegion)
Benjamin Kramer40957cc2015-02-12 16:30:00 +0000375 IsNotExpandedFile[CR.ExpandedFileID] = false;
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000376 int I = IsNotExpandedFile.find_first();
Justin Bognerc4f5a5e2015-02-20 07:28:28 +0000377 if (I == -1)
378 return None;
379 return I;
Justin Bogner953e2402014-09-20 15:31:56 +0000380}
381
382/// Sort a nested sequence of regions from a single file.
383template <class It> static void sortNestedRegions(It First, It Last) {
384 std::sort(First, Last,
385 [](const CountedRegion &LHS, const CountedRegion &RHS) {
386 if (LHS.startLoc() == RHS.startLoc())
387 // When LHS completely contains RHS, we sort LHS first.
388 return RHS.endLoc() < LHS.endLoc();
389 return LHS.startLoc() < RHS.startLoc();
390 });
391}
392
393static bool isExpansion(const CountedRegion &R, unsigned FileID) {
394 return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
395}
396
397CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) {
398 CoverageData FileCoverage(Filename);
399 std::vector<coverage::CountedRegion> Regions;
400
401 for (const auto &Function : Functions) {
402 auto MainFileID = findMainViewFileID(Filename, Function);
403 if (!MainFileID)
404 continue;
405 auto FileIDs = gatherFileIDs(Filename, Function);
406 for (const auto &CR : Function.CountedRegions)
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000407 if (FileIDs.test(CR.FileID)) {
Justin Bogner953e2402014-09-20 15:31:56 +0000408 Regions.push_back(CR);
409 if (isExpansion(CR, *MainFileID))
410 FileCoverage.Expansions.emplace_back(CR, Function);
411 }
412 }
413
414 sortNestedRegions(Regions.begin(), Regions.end());
Justin Bogner3c0f1242015-01-24 20:58:52 +0000415 DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000416 FileCoverage.Segments = SegmentBuilder().buildSegments(Regions);
417
418 return FileCoverage;
419}
420
421std::vector<const FunctionRecord *>
422CoverageMapping::getInstantiations(StringRef Filename) {
423 FunctionInstantiationSetCollector InstantiationSetCollector;
424 for (const auto &Function : Functions) {
425 auto MainFileID = findMainViewFileID(Filename, Function);
426 if (!MainFileID)
427 continue;
428 InstantiationSetCollector.insert(Function, *MainFileID);
429 }
430
431 std::vector<const FunctionRecord *> Result;
432 for (const auto &InstantiationSet : InstantiationSetCollector) {
433 if (InstantiationSet.second.size() < 2)
434 continue;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000435 Result.insert(Result.end(), InstantiationSet.second.begin(),
436 InstantiationSet.second.end());
Justin Bogner953e2402014-09-20 15:31:56 +0000437 }
438 return Result;
439}
440
441CoverageData
442CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) {
443 auto MainFileID = findMainViewFileID(Function);
444 if (!MainFileID)
445 return CoverageData();
446
447 CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
448 std::vector<coverage::CountedRegion> Regions;
449 for (const auto &CR : Function.CountedRegions)
450 if (CR.FileID == *MainFileID) {
451 Regions.push_back(CR);
452 if (isExpansion(CR, *MainFileID))
453 FunctionCoverage.Expansions.emplace_back(CR, Function);
454 }
455
456 sortNestedRegions(Regions.begin(), Regions.end());
Justin Bogner3c0f1242015-01-24 20:58:52 +0000457 DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000458 FunctionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
459
460 return FunctionCoverage;
461}
462
463CoverageData
464CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) {
465 CoverageData ExpansionCoverage(
466 Expansion.Function.Filenames[Expansion.FileID]);
467 std::vector<coverage::CountedRegion> Regions;
468 for (const auto &CR : Expansion.Function.CountedRegions)
469 if (CR.FileID == Expansion.FileID) {
470 Regions.push_back(CR);
471 if (isExpansion(CR, Expansion.FileID))
472 ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
473 }
474
475 sortNestedRegions(Regions.begin(), Regions.end());
Justin Bogner3c0f1242015-01-24 20:58:52 +0000476 DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID
477 << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000478 ExpansionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
479
480 return ExpansionCoverage;
481}