1 //===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
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 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
11 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
12 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
13 #include "llvm/ProfileData/InstrProfReader.h"
14 #include "llvm/ProfileData/InstrProfWriter.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/Testing/Support/Error.h"
17 #include "llvm/Testing/Support/SupportHelpers.h"
18 #include "gtest/gtest.h"
19 
20 #include <ostream>
21 #include <utility>
22 
23 using namespace llvm;
24 using namespace coverage;
25 
26 LLVM_NODISCARD static ::testing::AssertionResult
27 ErrorEquals(coveragemap_error Expected, Error E) {
28   coveragemap_error Found;
29   std::string FoundMsg;
30   handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
31     Found = CME.get();
32     FoundMsg = CME.message();
33   });
34   if (Expected == Found)
35     return ::testing::AssertionSuccess();
36   return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
37 }
38 
39 namespace llvm {
40 namespace coverage {
41 void PrintTo(const Counter &C, ::std::ostream *os) {
42   if (C.isZero())
43     *os << "Zero";
44   else if (C.isExpression())
45     *os << "Expression " << C.getExpressionID();
46   else
47     *os << "Counter " << C.getCounterID();
48 }
49 
50 void PrintTo(const CoverageSegment &S, ::std::ostream *os) {
51   *os << "CoverageSegment(" << S.Line << ", " << S.Col << ", ";
52   if (S.HasCount)
53     *os << S.Count << ", ";
54   *os << (S.IsRegionEntry ? "true" : "false") << ")";
55 }
56 }
57 }
58 
59 namespace {
60 
61 struct OutputFunctionCoverageData {
62   StringRef Name;
63   uint64_t Hash;
64   std::vector<StringRef> Filenames;
65   std::vector<CounterMappingRegion> Regions;
66 
67   OutputFunctionCoverageData() : Hash(0) {}
68 
69   OutputFunctionCoverageData(OutputFunctionCoverageData &&OFCD)
70       : Name(OFCD.Name), Hash(OFCD.Hash), Filenames(std::move(OFCD.Filenames)),
71         Regions(std::move(OFCD.Regions)) {}
72 
73   OutputFunctionCoverageData(const OutputFunctionCoverageData &) = delete;
74   OutputFunctionCoverageData &
75   operator=(const OutputFunctionCoverageData &) = delete;
76   OutputFunctionCoverageData &operator=(OutputFunctionCoverageData &&) = delete;
77 
78   void fillCoverageMappingRecord(CoverageMappingRecord &Record) const {
79     Record.FunctionName = Name;
80     Record.FunctionHash = Hash;
81     Record.Filenames = Filenames;
82     Record.Expressions = {};
83     Record.MappingRegions = Regions;
84   }
85 };
86 
87 struct CoverageMappingReaderMock : CoverageMappingReader {
88   ArrayRef<OutputFunctionCoverageData> Functions;
89 
90   CoverageMappingReaderMock(ArrayRef<OutputFunctionCoverageData> Functions)
91       : Functions(Functions) {}
92 
93   Error readNextRecord(CoverageMappingRecord &Record) override {
94     if (Functions.empty())
95       return make_error<CoverageMapError>(coveragemap_error::eof);
96 
97     Functions.front().fillCoverageMappingRecord(Record);
98     Functions = Functions.slice(1);
99 
100     return Error::success();
101   }
102 };
103 
104 struct InputFunctionCoverageData {
105   // Maps the global file index from CoverageMappingTest.Files
106   // to the index of that file within this function. We can't just use
107   // global file indexes here because local indexes have to be dense.
108   // This map is used during serialization to create the virtual file mapping
109   // (from local fileId to global Index) in the head of the per-function
110   // coverage mapping data.
111   SmallDenseMap<unsigned, unsigned> ReverseVirtualFileMapping;
112   std::string Name;
113   uint64_t Hash;
114   std::vector<CounterMappingRegion> Regions;
115 
116   InputFunctionCoverageData(std::string Name, uint64_t Hash)
117       : Name(std::move(Name)), Hash(Hash) {}
118 
119   InputFunctionCoverageData(InputFunctionCoverageData &&IFCD)
120       : ReverseVirtualFileMapping(std::move(IFCD.ReverseVirtualFileMapping)),
121         Name(std::move(IFCD.Name)), Hash(IFCD.Hash),
122         Regions(std::move(IFCD.Regions)) {}
123 
124   InputFunctionCoverageData(const InputFunctionCoverageData &) = delete;
125   InputFunctionCoverageData &
126   operator=(const InputFunctionCoverageData &) = delete;
127   InputFunctionCoverageData &operator=(InputFunctionCoverageData &&) = delete;
128 };
129 
130 struct CoverageMappingTest : ::testing::TestWithParam<std::pair<bool, bool>> {
131   bool UseMultipleReaders;
132   StringMap<unsigned> Files;
133   std::vector<InputFunctionCoverageData> InputFunctions;
134   std::vector<OutputFunctionCoverageData> OutputFunctions;
135 
136   InstrProfWriter ProfileWriter;
137   std::unique_ptr<IndexedInstrProfReader> ProfileReader;
138 
139   std::unique_ptr<CoverageMapping> LoadedCoverage;
140 
141   void SetUp() override {
142     ProfileWriter.setOutputSparse(GetParam().first);
143     UseMultipleReaders = GetParam().second;
144   }
145 
146   unsigned getGlobalFileIndex(StringRef Name) {
147     auto R = Files.find(Name);
148     if (R != Files.end())
149       return R->second;
150     unsigned Index = Files.size();
151     Files.try_emplace(Name, Index);
152     return Index;
153   }
154 
155   // Return the file index of file 'Name' for the current function.
156   // Add the file into the global map if necessary.
157   // See also InputFunctionCoverageData::ReverseVirtualFileMapping
158   // for additional comments.
159   unsigned getFileIndexForFunction(StringRef Name) {
160     unsigned GlobalIndex = getGlobalFileIndex(Name);
161     auto &CurrentFunctionFileMapping =
162         InputFunctions.back().ReverseVirtualFileMapping;
163     auto R = CurrentFunctionFileMapping.find(GlobalIndex);
164     if (R != CurrentFunctionFileMapping.end())
165       return R->second;
166     unsigned IndexInFunction = CurrentFunctionFileMapping.size();
167     CurrentFunctionFileMapping.insert(
168         std::make_pair(GlobalIndex, IndexInFunction));
169     return IndexInFunction;
170   }
171 
172   void startFunction(StringRef FuncName, uint64_t Hash) {
173     InputFunctions.emplace_back(FuncName.str(), Hash);
174   }
175 
176   void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
177               unsigned CE) {
178     InputFunctions.back().Regions.push_back(CounterMappingRegion::makeRegion(
179         C, getFileIndexForFunction(File), LS, CS, LE, CE));
180   }
181 
182   void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
183                        unsigned CS, unsigned LE, unsigned CE) {
184     InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion(
185         getFileIndexForFunction(File), getFileIndexForFunction(ExpandedFile),
186         LS, CS, LE, CE));
187   }
188 
189   std::string writeCoverageRegions(InputFunctionCoverageData &Data) {
190     SmallVector<unsigned, 8> FileIDs(Data.ReverseVirtualFileMapping.size());
191     for (const auto &E : Data.ReverseVirtualFileMapping)
192       FileIDs[E.second] = E.first;
193     std::string Coverage;
194     llvm::raw_string_ostream OS(Coverage);
195     CoverageMappingWriter(FileIDs, None, Data.Regions).write(OS);
196     return OS.str();
197   }
198 
199   void readCoverageRegions(const std::string &Coverage,
200                            OutputFunctionCoverageData &Data) {
201     SmallVector<StringRef, 8> Filenames(Files.size());
202     for (const auto &E : Files)
203       Filenames[E.getValue()] = E.getKey();
204     std::vector<CounterExpression> Expressions;
205     RawCoverageMappingReader Reader(Coverage, Filenames, Data.Filenames,
206                                     Expressions, Data.Regions);
207     EXPECT_THAT_ERROR(Reader.read(), Succeeded());
208   }
209 
210   void writeAndReadCoverageRegions(bool EmitFilenames = true) {
211     OutputFunctions.resize(InputFunctions.size());
212     for (unsigned I = 0; I < InputFunctions.size(); ++I) {
213       std::string Regions = writeCoverageRegions(InputFunctions[I]);
214       readCoverageRegions(Regions, OutputFunctions[I]);
215       OutputFunctions[I].Name = InputFunctions[I].Name;
216       OutputFunctions[I].Hash = InputFunctions[I].Hash;
217       if (!EmitFilenames)
218         OutputFunctions[I].Filenames.clear();
219     }
220   }
221 
222   void readProfCounts() {
223     auto Profile = ProfileWriter.writeBuffer();
224     auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
225     EXPECT_THAT_ERROR(ReaderOrErr.takeError(), Succeeded());
226     ProfileReader = std::move(ReaderOrErr.get());
227   }
228 
229   Expected<std::unique_ptr<CoverageMapping>> readOutputFunctions() {
230     std::vector<std::unique_ptr<CoverageMappingReader>> CoverageReaders;
231     if (UseMultipleReaders) {
232       for (const auto &OF : OutputFunctions) {
233         ArrayRef<OutputFunctionCoverageData> Funcs(OF);
234         CoverageReaders.push_back(
235             make_unique<CoverageMappingReaderMock>(Funcs));
236       }
237     } else {
238       ArrayRef<OutputFunctionCoverageData> Funcs(OutputFunctions);
239       CoverageReaders.push_back(
240           make_unique<CoverageMappingReaderMock>(Funcs));
241     }
242     return CoverageMapping::load(CoverageReaders, *ProfileReader);
243   }
244 
245   Error loadCoverageMapping(bool EmitFilenames = true) {
246     readProfCounts();
247     writeAndReadCoverageRegions(EmitFilenames);
248     auto CoverageOrErr = readOutputFunctions();
249     if (!CoverageOrErr)
250       return CoverageOrErr.takeError();
251     LoadedCoverage = std::move(CoverageOrErr.get());
252     return Error::success();
253   }
254 };
255 
256 TEST_P(CoverageMappingTest, basic_write_read) {
257   startFunction("func", 0x1234);
258   addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
259   addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
260   addCMR(Counter::getZero(),     "foo", 3, 1, 3, 4);
261   addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
262   addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
263 
264   writeAndReadCoverageRegions();
265   ASSERT_EQ(1u, InputFunctions.size());
266   ASSERT_EQ(1u, OutputFunctions.size());
267   InputFunctionCoverageData &Input = InputFunctions.back();
268   OutputFunctionCoverageData &Output = OutputFunctions.back();
269 
270   size_t N = makeArrayRef(Input.Regions).size();
271   ASSERT_EQ(N, Output.Regions.size());
272   for (size_t I = 0; I < N; ++I) {
273     ASSERT_EQ(Input.Regions[I].Count, Output.Regions[I].Count);
274     ASSERT_EQ(Input.Regions[I].FileID, Output.Regions[I].FileID);
275     ASSERT_EQ(Input.Regions[I].startLoc(), Output.Regions[I].startLoc());
276     ASSERT_EQ(Input.Regions[I].endLoc(), Output.Regions[I].endLoc());
277     ASSERT_EQ(Input.Regions[I].Kind, Output.Regions[I].Kind);
278   }
279 }
280 
281 TEST_P(CoverageMappingTest, correct_deserialize_for_more_than_two_files) {
282   const char *FileNames[] = {"bar", "baz", "foo"};
283   static const unsigned N = array_lengthof(FileNames);
284 
285   startFunction("func", 0x1234);
286   for (unsigned I = 0; I < N; ++I)
287     // Use LineStart to hold the index of the file name
288     // in order to preserve that information during possible sorting of CMRs.
289     addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
290 
291   writeAndReadCoverageRegions();
292   ASSERT_EQ(1u, OutputFunctions.size());
293   OutputFunctionCoverageData &Output = OutputFunctions.back();
294 
295   ASSERT_EQ(N, Output.Regions.size());
296   ASSERT_EQ(N, Output.Filenames.size());
297 
298   for (unsigned I = 0; I < N; ++I) {
299     ASSERT_GT(N, Output.Regions[I].FileID);
300     ASSERT_GT(N, Output.Regions[I].LineStart);
301     EXPECT_EQ(FileNames[Output.Regions[I].LineStart],
302               Output.Filenames[Output.Regions[I].FileID]);
303   }
304 }
305 
306 static const auto Err = [](Error E) { FAIL(); };
307 
308 TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) {
309   ProfileWriter.addRecord({"func", 0x1234, {0}}, Err);
310 
311   const char *FileNames[] = {"bar", "baz", "foo"};
312   static const unsigned N = array_lengthof(FileNames);
313 
314   startFunction("func", 0x1234);
315   for (unsigned I = 0; I < N; ++I)
316     // Use LineStart to hold the index of the file name
317     // in order to preserve that information during possible sorting of CMRs.
318     addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
319 
320   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
321 
322   for (unsigned I = 0; I < N; ++I) {
323     CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
324     ASSERT_TRUE(!Data.empty());
325     EXPECT_EQ(I, Data.begin()->Line);
326   }
327 }
328 
329 TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) {
330   ProfileWriter.addRecord({"", 0x1234, {10}}, Err);
331   startFunction("", 0x1234);
332   addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
333   EXPECT_TRUE(ErrorEquals(coveragemap_error::malformed, loadCoverageMapping()));
334 }
335 
336 TEST_P(CoverageMappingTest, load_coverage_for_several_functions) {
337   ProfileWriter.addRecord({"func1", 0x1234, {10}}, Err);
338   ProfileWriter.addRecord({"func2", 0x2345, {20}}, Err);
339 
340   startFunction("func1", 0x1234);
341   addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
342 
343   startFunction("func2", 0x2345);
344   addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
345 
346   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
347 
348   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
349   EXPECT_EQ(2, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
350   for (const auto &FunctionRecord : FunctionRecords) {
351     CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
352     std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
353     ASSERT_EQ(2U, Segments.size());
354     if (FunctionRecord.Name == "func1") {
355       EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
356       EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]);
357     } else {
358       ASSERT_EQ("func2", FunctionRecord.Name);
359       EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]);
360       EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]);
361     }
362   }
363 }
364 
365 TEST_P(CoverageMappingTest, expansion_gets_first_counter) {
366   startFunction("func", 0x1234);
367   addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
368   // This starts earlier in "foo", so the expansion should get its counter.
369   addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
370   addExpansionCMR("bar", "foo", 3, 3, 3, 3);
371 
372   writeAndReadCoverageRegions();
373   ASSERT_EQ(1u, OutputFunctions.size());
374   OutputFunctionCoverageData &Output = OutputFunctions.back();
375 
376   ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind);
377   ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count);
378   ASSERT_EQ(3U, Output.Regions[2].LineStart);
379 }
380 
381 TEST_P(CoverageMappingTest, basic_coverage_iteration) {
382   ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
383 
384   startFunction("func", 0x1234);
385   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
386   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
387   addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
388   addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
389   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
390 
391   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
392   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
393   ASSERT_EQ(7U, Segments.size());
394   ASSERT_EQ(CoverageSegment(1, 1, 20, true),  Segments[0]);
395   ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
396   ASSERT_EQ(CoverageSegment(5, 8, 10, true),  Segments[2]);
397   ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
398   ASSERT_EQ(CoverageSegment(9, 9, false),     Segments[4]);
399   ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
400   ASSERT_EQ(CoverageSegment(11, 11, false),   Segments[6]);
401 }
402 
403 TEST_P(CoverageMappingTest, uncovered_function) {
404   startFunction("func", 0x1234);
405   addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
406   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
407 
408   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
409   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
410   ASSERT_EQ(2U, Segments.size());
411   ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
412   ASSERT_EQ(CoverageSegment(3, 4, false),   Segments[1]);
413 }
414 
415 TEST_P(CoverageMappingTest, uncovered_function_with_mapping) {
416   startFunction("func", 0x1234);
417   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
418   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
419   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
420 
421   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
422   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
423   ASSERT_EQ(3U, Segments.size());
424   ASSERT_EQ(CoverageSegment(1, 1, 0, true),  Segments[0]);
425   ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
426   ASSERT_EQ(CoverageSegment(9, 9, false),    Segments[2]);
427 }
428 
429 TEST_P(CoverageMappingTest, combine_regions) {
430   ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}, Err);
431 
432   startFunction("func", 0x1234);
433   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
434   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
435   addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
436   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
437 
438   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
439   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
440   ASSERT_EQ(4U, Segments.size());
441   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
442   ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
443   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
444   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
445 }
446 
447 TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) {
448   ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}, Err);
449 
450   startFunction("func", 0x1234);
451   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
452   addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
453   addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
454   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
455 
456   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
457   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
458   ASSERT_EQ(4U, Segments.size());
459   EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]);
460   EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]);
461   EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]);
462   EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]);
463 }
464 
465 // If CodeRegions and ExpansionRegions cover the same area,
466 // only counts of CodeRegions should be used.
467 TEST_P(CoverageMappingTest, dont_combine_expansions) {
468   ProfileWriter.addRecord({"func", 0x1234, {10, 20}}, Err);
469   ProfileWriter.addRecord({"func", 0x1234, {0, 0}}, Err);
470 
471   startFunction("func", 0x1234);
472   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
473   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
474   addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
475   addExpansionCMR("file1", "include1", 3, 3, 4, 4);
476   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
477 
478   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
479   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
480   ASSERT_EQ(4U, Segments.size());
481   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
482   ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
483   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
484   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
485 }
486 
487 // If an area is covered only by ExpansionRegions, they should be combinated.
488 TEST_P(CoverageMappingTest, combine_expansions) {
489   ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}, Err);
490 
491   startFunction("func", 0x1234);
492   addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
493   addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
494   addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
495   addExpansionCMR("file", "include1", 3, 1, 3, 5);
496   addExpansionCMR("file", "include2", 3, 1, 3, 5);
497 
498   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
499 
500   CoverageData Data = LoadedCoverage->getCoverageForFile("file");
501   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
502   ASSERT_EQ(4U, Segments.size());
503   EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]);
504   EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]);
505   EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]);
506   EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]);
507 }
508 
509 TEST_P(CoverageMappingTest, strip_filename_prefix) {
510   ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err);
511 
512   startFunction("file1:func", 0x1234);
513   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
514   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
515 
516   std::vector<std::string> Names;
517   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
518     Names.push_back(Func.Name);
519   ASSERT_EQ(1U, Names.size());
520   ASSERT_EQ("func", Names[0]);
521 }
522 
523 TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) {
524   ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}, Err);
525 
526   startFunction("<unknown>:func", 0x1234);
527   addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
528   EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
529 
530   std::vector<std::string> Names;
531   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
532     Names.push_back(Func.Name);
533   ASSERT_EQ(1U, Names.size());
534   ASSERT_EQ("func", Names[0]);
535 }
536 
537 TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
538   ProfileWriter.addRecord({"foo", 0x1234, {10}}, Err);
539   ProfileWriter.addRecord({"bar", 0x2345, {20}}, Err);
540 
541   startFunction("foo", 0x1234);
542   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
543   addExpansionCMR("main", "expanded", 4, 1, 4, 5);
544 
545   startFunction("bar", 0x2345);
546   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
547   addExpansionCMR("main", "expanded", 9, 1, 9, 5);
548 
549   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
550 
551   std::vector<const FunctionRecord *> Instantiations =
552       LoadedCoverage->getInstantiations("expanded");
553   ASSERT_TRUE(Instantiations.empty());
554 }
555 
556 TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {
557   ProfileWriter.addRecord({"func", 0x1234, {10}}, Err);
558 
559   startFunction("func", 0x1234);
560   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
561   addExpansionCMR("main", "expanded", 4, 1, 4, 5);
562 
563   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
564 
565   CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
566   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
567   ASSERT_EQ(2U, Segments.size());
568   EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
569   EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]);
570 }
571 
572 TEST_P(CoverageMappingTest, skip_duplicate_function_record) {
573   ProfileWriter.addRecord({"func", 0x1234, {1}}, Err);
574 
575   startFunction("func", 0x1234);
576   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
577 
578   startFunction("func", 0x1234);
579   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
580 
581   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
582 
583   auto Funcs = LoadedCoverage->getCoveredFunctions();
584   unsigned NumFuncs = std::distance(Funcs.begin(), Funcs.end());
585   ASSERT_EQ(1U, NumFuncs);
586 }
587 
588 // FIXME: Use ::testing::Combine() when llvm updates its copy of googletest.
589 INSTANTIATE_TEST_CASE_P(ParameterizedCovMapTest, CoverageMappingTest,
590                         ::testing::Values(std::pair<bool, bool>({false, false}),
591                                           std::pair<bool, bool>({false, true}),
592                                           std::pair<bool, bool>({true, false}),
593                                           std::pair<bool, bool>({true, true})),);
594 
595 } // end anonymous namespace
596