blob: bbac5c26b1ebd9569126c596fc1c5571be4624bd [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"
Justin Bogner367a9f22015-05-06 23:19:35 +000023#include "llvm/Support/ManagedStatic.h"
Justin Bogner0b4c4842015-05-05 23:44:48 +000024#include "llvm/Support/Path.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000025#include "llvm/Support/raw_ostream.h"
Alex Lorenza20a5d52014-07-24 23:57:54 +000026
27using namespace llvm;
28using namespace coverage;
29
Justin Bognerb35a72a2014-09-25 00:34:18 +000030#define DEBUG_TYPE "coverage-mapping"
31
Alex Lorenza20a5d52014-07-24 23:57:54 +000032Counter CounterExpressionBuilder::get(const CounterExpression &E) {
Justin Bognerad69e642014-10-02 17:14:18 +000033 auto It = ExpressionIndices.find(E);
34 if (It != ExpressionIndices.end())
35 return Counter::getExpression(It->second);
36 unsigned I = Expressions.size();
Alex Lorenza20a5d52014-07-24 23:57:54 +000037 Expressions.push_back(E);
Justin Bognerad69e642014-10-02 17:14:18 +000038 ExpressionIndices[E] = I;
39 return Counter::getExpression(I);
Alex Lorenza20a5d52014-07-24 23:57:54 +000040}
41
Justin Bognerf9535c42014-10-02 16:43:31 +000042void CounterExpressionBuilder::extractTerms(
43 Counter C, int Sign, SmallVectorImpl<std::pair<unsigned, int>> &Terms) {
Alex Lorenza20a5d52014-07-24 23:57:54 +000044 switch (C.getKind()) {
45 case Counter::Zero:
46 break;
47 case Counter::CounterValueReference:
Justin Bognerf9535c42014-10-02 16:43:31 +000048 Terms.push_back(std::make_pair(C.getCounterID(), Sign));
Alex Lorenza20a5d52014-07-24 23:57:54 +000049 break;
50 case Counter::Expression:
51 const auto &E = Expressions[C.getExpressionID()];
Justin Bognerf9535c42014-10-02 16:43:31 +000052 extractTerms(E.LHS, Sign, Terms);
53 extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign,
54 Terms);
Alex Lorenza20a5d52014-07-24 23:57:54 +000055 break;
56 }
57}
58
59Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
60 // Gather constant terms.
Justin Bognerf9535c42014-10-02 16:43:31 +000061 llvm::SmallVector<std::pair<unsigned, int>, 32> Terms;
62 extractTerms(ExpressionTree, +1, Terms);
63
64 // If there are no terms, this is just a zero. The algorithm below assumes at
65 // least one term.
66 if (Terms.size() == 0)
67 return Counter::getZero();
68
69 // Group the terms by counter ID.
70 std::sort(Terms.begin(), Terms.end(),
71 [](const std::pair<unsigned, int> &LHS,
72 const std::pair<unsigned, int> &RHS) {
73 return LHS.first < RHS.first;
74 });
75
76 // Combine terms by counter ID to eliminate counters that sum to zero.
77 auto Prev = Terms.begin();
78 for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
79 if (I->first == Prev->first) {
80 Prev->second += I->second;
81 continue;
82 }
83 ++Prev;
84 *Prev = *I;
85 }
86 Terms.erase(++Prev, Terms.end());
Alex Lorenza20a5d52014-07-24 23:57:54 +000087
88 Counter C;
Justin Bognerf9535c42014-10-02 16:43:31 +000089 // Create additions. We do this before subtractions to avoid constructs like
90 // ((0 - X) + Y), as opposed to (Y - X).
91 for (auto Term : Terms) {
92 if (Term.second <= 0)
Alex Lorenza20a5d52014-07-24 23:57:54 +000093 continue;
Justin Bognerf9535c42014-10-02 16:43:31 +000094 for (int I = 0; I < Term.second; ++I)
Alex Lorenza20a5d52014-07-24 23:57:54 +000095 if (C.isZero())
Justin Bognerf9535c42014-10-02 16:43:31 +000096 C = Counter::getCounter(Term.first);
Alex Lorenza20a5d52014-07-24 23:57:54 +000097 else
98 C = get(CounterExpression(CounterExpression::Add, C,
Justin Bognerf9535c42014-10-02 16:43:31 +000099 Counter::getCounter(Term.first)));
Alex Lorenza20a5d52014-07-24 23:57:54 +0000100 }
101
102 // Create subtractions.
Justin Bognerf9535c42014-10-02 16:43:31 +0000103 for (auto Term : Terms) {
104 if (Term.second >= 0)
Alex Lorenza20a5d52014-07-24 23:57:54 +0000105 continue;
Justin Bognerf9535c42014-10-02 16:43:31 +0000106 for (int I = 0; I < -Term.second; ++I)
Alex Lorenza20a5d52014-07-24 23:57:54 +0000107 C = get(CounterExpression(CounterExpression::Subtract, C,
Justin Bognerf9535c42014-10-02 16:43:31 +0000108 Counter::getCounter(Term.first)));
Alex Lorenza20a5d52014-07-24 23:57:54 +0000109 }
110 return C;
111}
112
113Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) {
114 return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS)));
115}
116
117Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) {
118 return simplify(
119 get(CounterExpression(CounterExpression::Subtract, LHS, RHS)));
120}
121
122void CounterMappingContext::dump(const Counter &C,
123 llvm::raw_ostream &OS) const {
124 switch (C.getKind()) {
125 case Counter::Zero:
126 OS << '0';
127 return;
128 case Counter::CounterValueReference:
129 OS << '#' << C.getCounterID();
130 break;
131 case Counter::Expression: {
132 if (C.getExpressionID() >= Expressions.size())
133 return;
134 const auto &E = Expressions[C.getExpressionID()];
135 OS << '(';
Alex Lorenza422911c2014-07-29 19:58:16 +0000136 dump(E.LHS, OS);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000137 OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
Alex Lorenza422911c2014-07-29 19:58:16 +0000138 dump(E.RHS, OS);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000139 OS << ')';
140 break;
141 }
142 }
143 if (CounterValues.empty())
144 return;
Justin Bogner85b0a032014-09-08 21:04:00 +0000145 ErrorOr<int64_t> Value = evaluate(C);
146 if (!Value)
Alex Lorenza20a5d52014-07-24 23:57:54 +0000147 return;
Justin Bogner85b0a032014-09-08 21:04:00 +0000148 OS << '[' << *Value << ']';
Alex Lorenza20a5d52014-07-24 23:57:54 +0000149}
150
Justin Bogner85b0a032014-09-08 21:04:00 +0000151ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000152 switch (C.getKind()) {
153 case Counter::Zero:
154 return 0;
155 case Counter::CounterValueReference:
Justin Bogner85b0a032014-09-08 21:04:00 +0000156 if (C.getCounterID() >= CounterValues.size())
Justin Bogner3f188342014-09-08 21:31:43 +0000157 return std::make_error_code(std::errc::argument_out_of_domain);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000158 return CounterValues[C.getCounterID()];
159 case Counter::Expression: {
Justin Bogner85b0a032014-09-08 21:04:00 +0000160 if (C.getExpressionID() >= Expressions.size())
Justin Bogner3f188342014-09-08 21:31:43 +0000161 return std::make_error_code(std::errc::argument_out_of_domain);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000162 const auto &E = Expressions[C.getExpressionID()];
Justin Bogner85b0a032014-09-08 21:04:00 +0000163 ErrorOr<int64_t> LHS = evaluate(E.LHS);
164 if (!LHS)
165 return LHS;
166 ErrorOr<int64_t> RHS = evaluate(E.RHS);
167 if (!RHS)
168 return RHS;
169 return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000170 }
171 }
Justin Bogner85b0a032014-09-08 21:04:00 +0000172 llvm_unreachable("Unhandled CounterKind");
Alex Lorenza20a5d52014-07-24 23:57:54 +0000173}
Justin Bogner953e2402014-09-20 15:31:56 +0000174
Justin Bognerd5fca922014-11-14 01:50:32 +0000175void FunctionRecordIterator::skipOtherFiles() {
176 while (Current != Records.end() && !Filename.empty() &&
177 Filename != Current->Filenames[0])
178 ++Current;
179 if (Current == Records.end())
180 *this = FunctionRecordIterator();
181}
182
Justin Bogner0b4c4842015-05-05 23:44:48 +0000183/// Get the function name from the record, removing the filename prefix if
184/// necessary.
185static StringRef getFuncNameWithoutPrefix(const CoverageMappingRecord &Record) {
186 StringRef FunctionName = Record.FunctionName;
187 if (Record.Filenames.empty())
188 return FunctionName;
189 StringRef Filename = sys::path::filename(Record.Filenames[0]);
190 if (FunctionName.startswith(Filename))
191 FunctionName = FunctionName.drop_front(Filename.size() + 1);
192 return FunctionName;
193}
194
Justin Bogner953e2402014-09-20 15:31:56 +0000195ErrorOr<std::unique_ptr<CoverageMapping>>
Justin Bogner1d29c082015-02-18 18:01:14 +0000196CoverageMapping::load(CoverageMappingReader &CoverageReader,
Justin Bogner953e2402014-09-20 15:31:56 +0000197 IndexedInstrProfReader &ProfileReader) {
198 auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
199
200 std::vector<uint64_t> Counts;
201 for (const auto &Record : CoverageReader) {
Justin Bogner428c6052015-02-18 18:40:46 +0000202 CounterMappingContext Ctx(Record.Expressions);
203
Justin Bogner953e2402014-09-20 15:31:56 +0000204 Counts.clear();
205 if (std::error_code EC = ProfileReader.getFunctionCounts(
206 Record.FunctionName, Record.FunctionHash, Counts)) {
Justin Bogner428c6052015-02-18 18:40:46 +0000207 if (EC == instrprof_error::hash_mismatch) {
208 Coverage->MismatchedFunctionCount++;
209 continue;
210 } else if (EC != instrprof_error::unknown_function)
Justin Bogner953e2402014-09-20 15:31:56 +0000211 return EC;
Justin Bogner82a64512015-05-13 22:03:04 +0000212 Counts.assign(Record.MappingRegions.size(), 0);
213 }
214 Ctx.setCounts(Counts);
Justin Bogner953e2402014-09-20 15:31:56 +0000215
Justin Bogner428c6052015-02-18 18:40:46 +0000216 assert(!Record.MappingRegions.empty() && "Function has no regions");
Justin Bogner0b4c4842015-05-05 23:44:48 +0000217
218 FunctionRecord Function(getFuncNameWithoutPrefix(Record), Record.Filenames);
Justin Bogner953e2402014-09-20 15:31:56 +0000219 for (const auto &Region : Record.MappingRegions) {
220 ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
221 if (!ExecutionCount)
222 break;
Justin Bogner428c6052015-02-18 18:40:46 +0000223 Function.pushRegion(Region, *ExecutionCount);
Justin Bogner953e2402014-09-20 15:31:56 +0000224 }
225 if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
226 Coverage->MismatchedFunctionCount++;
227 continue;
228 }
229
Benjamin Kramere12a6ba2014-10-03 18:33:16 +0000230 Coverage->Functions.push_back(std::move(Function));
Justin Bogner953e2402014-09-20 15:31:56 +0000231 }
232
233 return std::move(Coverage);
234}
235
Justin Bogner19a93ba2014-09-20 17:19:52 +0000236ErrorOr<std::unique_ptr<CoverageMapping>>
Justin Bogner43795352015-03-11 02:30:51 +0000237CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename,
238 Triple::ArchType Arch) {
Justin Bogner19a93ba2014-09-20 17:19:52 +0000239 auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
Justin Bogner43e51632015-02-26 20:06:28 +0000240 if (std::error_code EC = CounterMappingBuff.getError())
Justin Bogner19a93ba2014-09-20 17:19:52 +0000241 return EC;
Justin Bogner43e51632015-02-26 20:06:28 +0000242 auto CoverageReaderOrErr =
Justin Bogner43795352015-03-11 02:30:51 +0000243 BinaryCoverageReader::create(CounterMappingBuff.get(), Arch);
Justin Bogner43e51632015-02-26 20:06:28 +0000244 if (std::error_code EC = CoverageReaderOrErr.getError())
Justin Bogner19a93ba2014-09-20 17:19:52 +0000245 return EC;
Justin Bogner43e51632015-02-26 20:06:28 +0000246 auto CoverageReader = std::move(CoverageReaderOrErr.get());
Justin Bognerab89ed72015-02-16 21:28:58 +0000247 auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
248 if (auto EC = ProfileReaderOrErr.getError())
Justin Bogner19a93ba2014-09-20 17:19:52 +0000249 return EC;
Justin Bognerab89ed72015-02-16 21:28:58 +0000250 auto ProfileReader = std::move(ProfileReaderOrErr.get());
Justin Bogner43e51632015-02-26 20:06:28 +0000251 return load(*CoverageReader, *ProfileReader);
Justin Bogner19a93ba2014-09-20 17:19:52 +0000252}
253
Justin Bogner953e2402014-09-20 15:31:56 +0000254namespace {
255/// \brief Distributes functions into instantiation sets.
256///
257/// An instantiation set is a collection of functions that have the same source
258/// code, ie, template functions specializations.
259class FunctionInstantiationSetCollector {
260 typedef DenseMap<std::pair<unsigned, unsigned>,
261 std::vector<const FunctionRecord *>> MapT;
262 MapT InstantiatedFunctions;
263
264public:
265 void insert(const FunctionRecord &Function, unsigned FileID) {
266 auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
267 while (I != E && I->FileID != FileID)
268 ++I;
269 assert(I != E && "function does not cover the given file");
270 auto &Functions = InstantiatedFunctions[I->startLoc()];
271 Functions.push_back(&Function);
272 }
273
274 MapT::iterator begin() { return InstantiatedFunctions.begin(); }
275
276 MapT::iterator end() { return InstantiatedFunctions.end(); }
277};
278
279class SegmentBuilder {
280 std::vector<CoverageSegment> Segments;
281 SmallVector<const CountedRegion *, 8> ActiveRegions;
282
283 /// Start a segment with no count specified.
284 void startSegment(unsigned Line, unsigned Col) {
Justin Bognerb35a72a2014-09-25 00:34:18 +0000285 DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000286 Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false);
287 }
288
289 /// Start a segment with the given Region's count.
290 void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry,
291 const CountedRegion &Region) {
292 if (Segments.empty())
293 Segments.emplace_back(Line, Col, IsRegionEntry);
294 CoverageSegment S = Segments.back();
295 // Avoid creating empty regions.
296 if (S.Line != Line || S.Col != Col) {
297 Segments.emplace_back(Line, Col, IsRegionEntry);
298 S = Segments.back();
299 }
Justin Bognerb35a72a2014-09-25 00:34:18 +0000300 DEBUG(dbgs() << "Segment at " << Line << ":" << Col);
Justin Bogner953e2402014-09-20 15:31:56 +0000301 // Set this region's count.
Justin Bognerb35a72a2014-09-25 00:34:18 +0000302 if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) {
303 DEBUG(dbgs() << " with count " << Region.ExecutionCount);
Justin Bogner953e2402014-09-20 15:31:56 +0000304 Segments.back().setCount(Region.ExecutionCount);
Justin Bognerb35a72a2014-09-25 00:34:18 +0000305 }
306 DEBUG(dbgs() << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000307 }
308
309 /// Start a segment for the given region.
310 void startSegment(const CountedRegion &Region) {
311 startSegment(Region.LineStart, Region.ColumnStart, true, Region);
312 }
313
314 /// Pop the top region off of the active stack, starting a new segment with
315 /// the containing Region's count.
316 void popRegion() {
317 const CountedRegion *Active = ActiveRegions.back();
318 unsigned Line = Active->LineEnd, Col = Active->ColumnEnd;
319 ActiveRegions.pop_back();
320 if (ActiveRegions.empty())
321 startSegment(Line, Col);
322 else
323 startSegment(Line, Col, false, *ActiveRegions.back());
324 }
325
326public:
327 /// Build a list of CoverageSegments from a sorted list of Regions.
328 std::vector<CoverageSegment> buildSegments(ArrayRef<CountedRegion> Regions) {
Justin Bogner3c0f1242015-01-24 20:58:52 +0000329 const CountedRegion *PrevRegion = nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000330 for (const auto &Region : Regions) {
331 // Pop any regions that end before this one starts.
332 while (!ActiveRegions.empty() &&
333 ActiveRegions.back()->endLoc() <= Region.startLoc())
334 popRegion();
Justin Bogner3c0f1242015-01-24 20:58:52 +0000335 if (PrevRegion && PrevRegion->startLoc() == Region.startLoc() &&
336 PrevRegion->endLoc() == Region.endLoc()) {
Justin Bogner11ae7782015-02-18 19:01:06 +0000337 if (Region.Kind == coverage::CounterMappingRegion::CodeRegion)
Justin Bognerb35a72a2014-09-25 00:34:18 +0000338 Segments.back().addCount(Region.ExecutionCount);
339 } else {
340 // Add this region to the stack.
341 ActiveRegions.push_back(&Region);
342 startSegment(Region);
343 }
Justin Bogner3c0f1242015-01-24 20:58:52 +0000344 PrevRegion = &Region;
Justin Bogner953e2402014-09-20 15:31:56 +0000345 }
346 // Pop any regions that are left in the stack.
347 while (!ActiveRegions.empty())
348 popRegion();
349 return Segments;
350 }
351};
352}
353
Justin Bognerd5fca922014-11-14 01:50:32 +0000354std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
Justin Bogner953e2402014-09-20 15:31:56 +0000355 std::vector<StringRef> Filenames;
356 for (const auto &Function : getCoveredFunctions())
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000357 Filenames.insert(Filenames.end(), Function.Filenames.begin(),
358 Function.Filenames.end());
Justin Bogner953e2402014-09-20 15:31:56 +0000359 std::sort(Filenames.begin(), Filenames.end());
360 auto Last = std::unique(Filenames.begin(), Filenames.end());
361 Filenames.erase(Last, Filenames.end());
362 return Filenames;
363}
364
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000365static SmallBitVector gatherFileIDs(StringRef SourceFile,
366 const FunctionRecord &Function) {
367 SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
Justin Bogner953e2402014-09-20 15:31:56 +0000368 for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
369 if (SourceFile == Function.Filenames[I])
370 FilenameEquivalence[I] = true;
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000371 return FilenameEquivalence;
372}
373
374static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
375 const FunctionRecord &Function) {
376 SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
377 SmallBitVector FilenameEquivalence = gatherFileIDs(SourceFile, Function);
Justin Bogner953e2402014-09-20 15:31:56 +0000378 for (const auto &CR : Function.CountedRegions)
379 if (CR.Kind == CounterMappingRegion::ExpansionRegion &&
380 FilenameEquivalence[CR.FileID])
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000381 IsNotExpandedFile[CR.ExpandedFileID] = false;
382 IsNotExpandedFile &= FilenameEquivalence;
383 int I = IsNotExpandedFile.find_first();
Justin Bognerc4f5a5e2015-02-20 07:28:28 +0000384 if (I == -1)
385 return None;
386 return I;
Justin Bogner953e2402014-09-20 15:31:56 +0000387}
388
389static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
Benjamin Kramer40957cc2015-02-12 16:30:00 +0000390 SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
Justin Bogner953e2402014-09-20 15:31:56 +0000391 for (const auto &CR : Function.CountedRegions)
392 if (CR.Kind == CounterMappingRegion::ExpansionRegion)
Benjamin Kramer40957cc2015-02-12 16:30:00 +0000393 IsNotExpandedFile[CR.ExpandedFileID] = false;
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000394 int I = IsNotExpandedFile.find_first();
Justin Bognerc4f5a5e2015-02-20 07:28:28 +0000395 if (I == -1)
396 return None;
397 return I;
Justin Bogner953e2402014-09-20 15:31:56 +0000398}
399
400/// Sort a nested sequence of regions from a single file.
401template <class It> static void sortNestedRegions(It First, It Last) {
402 std::sort(First, Last,
403 [](const CountedRegion &LHS, const CountedRegion &RHS) {
404 if (LHS.startLoc() == RHS.startLoc())
405 // When LHS completely contains RHS, we sort LHS first.
406 return RHS.endLoc() < LHS.endLoc();
407 return LHS.startLoc() < RHS.startLoc();
408 });
409}
410
411static bool isExpansion(const CountedRegion &R, unsigned FileID) {
412 return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
413}
414
415CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) {
416 CoverageData FileCoverage(Filename);
417 std::vector<coverage::CountedRegion> Regions;
418
419 for (const auto &Function : Functions) {
420 auto MainFileID = findMainViewFileID(Filename, Function);
421 if (!MainFileID)
422 continue;
423 auto FileIDs = gatherFileIDs(Filename, Function);
424 for (const auto &CR : Function.CountedRegions)
Benjamin Kramer71e1eb52015-02-12 16:18:07 +0000425 if (FileIDs.test(CR.FileID)) {
Justin Bogner953e2402014-09-20 15:31:56 +0000426 Regions.push_back(CR);
427 if (isExpansion(CR, *MainFileID))
428 FileCoverage.Expansions.emplace_back(CR, Function);
429 }
430 }
431
432 sortNestedRegions(Regions.begin(), Regions.end());
Justin Bogner3c0f1242015-01-24 20:58:52 +0000433 DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000434 FileCoverage.Segments = SegmentBuilder().buildSegments(Regions);
435
436 return FileCoverage;
437}
438
439std::vector<const FunctionRecord *>
440CoverageMapping::getInstantiations(StringRef Filename) {
441 FunctionInstantiationSetCollector InstantiationSetCollector;
442 for (const auto &Function : Functions) {
443 auto MainFileID = findMainViewFileID(Filename, Function);
444 if (!MainFileID)
445 continue;
446 InstantiationSetCollector.insert(Function, *MainFileID);
447 }
448
449 std::vector<const FunctionRecord *> Result;
450 for (const auto &InstantiationSet : InstantiationSetCollector) {
451 if (InstantiationSet.second.size() < 2)
452 continue;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000453 Result.insert(Result.end(), InstantiationSet.second.begin(),
454 InstantiationSet.second.end());
Justin Bogner953e2402014-09-20 15:31:56 +0000455 }
456 return Result;
457}
458
459CoverageData
460CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) {
461 auto MainFileID = findMainViewFileID(Function);
462 if (!MainFileID)
463 return CoverageData();
464
465 CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
466 std::vector<coverage::CountedRegion> Regions;
467 for (const auto &CR : Function.CountedRegions)
468 if (CR.FileID == *MainFileID) {
469 Regions.push_back(CR);
470 if (isExpansion(CR, *MainFileID))
471 FunctionCoverage.Expansions.emplace_back(CR, Function);
472 }
473
474 sortNestedRegions(Regions.begin(), Regions.end());
Justin Bogner3c0f1242015-01-24 20:58:52 +0000475 DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000476 FunctionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
477
478 return FunctionCoverage;
479}
480
481CoverageData
482CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) {
483 CoverageData ExpansionCoverage(
484 Expansion.Function.Filenames[Expansion.FileID]);
485 std::vector<coverage::CountedRegion> Regions;
486 for (const auto &CR : Expansion.Function.CountedRegions)
487 if (CR.FileID == Expansion.FileID) {
488 Regions.push_back(CR);
489 if (isExpansion(CR, Expansion.FileID))
490 ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
491 }
492
493 sortNestedRegions(Regions.begin(), Regions.end());
Justin Bogner3c0f1242015-01-24 20:58:52 +0000494 DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID
495 << "\n");
Justin Bogner953e2402014-09-20 15:31:56 +0000496 ExpansionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
497
498 return ExpansionCoverage;
499}
Justin Bogner367a9f22015-05-06 23:19:35 +0000500
501namespace {
502class CoverageMappingErrorCategoryType : public std::error_category {
503 const char *name() const LLVM_NOEXCEPT override { return "llvm.coveragemap"; }
504 std::string message(int IE) const override {
505 auto E = static_cast<coveragemap_error>(IE);
506 switch (E) {
507 case coveragemap_error::success:
508 return "Success";
509 case coveragemap_error::eof:
510 return "End of File";
511 case coveragemap_error::no_data_found:
512 return "No coverage data found";
513 case coveragemap_error::unsupported_version:
514 return "Unsupported coverage format version";
515 case coveragemap_error::truncated:
516 return "Truncated coverage data";
517 case coveragemap_error::malformed:
518 return "Malformed coverage data";
519 }
520 llvm_unreachable("A value of coveragemap_error has no message.");
521 }
522};
523}
524
525static ManagedStatic<CoverageMappingErrorCategoryType> ErrorCategory;
526
527const std::error_category &llvm::coveragemap_category() {
528 return *ErrorCategory;
529}