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 TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) {
307   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {0}}),
308                     Succeeded());
309 
310   const char *FileNames[] = {"bar", "baz", "foo"};
311   static const unsigned N = array_lengthof(FileNames);
312 
313   startFunction("func", 0x1234);
314   for (unsigned I = 0; I < N; ++I)
315     // Use LineStart to hold the index of the file name
316     // in order to preserve that information during possible sorting of CMRs.
317     addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
318 
319   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
320 
321   for (unsigned I = 0; I < N; ++I) {
322     CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
323     ASSERT_TRUE(!Data.empty());
324     EXPECT_EQ(I, Data.begin()->Line);
325   }
326 }
327 
328 TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) {
329   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"", 0x1234, {10}}), Succeeded());
330   startFunction("", 0x1234);
331   addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
332   EXPECT_TRUE(ErrorEquals(coveragemap_error::malformed, loadCoverageMapping()));
333 }
334 
335 TEST_P(CoverageMappingTest, load_coverage_for_several_functions) {
336   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func1", 0x1234, {10}}),
337                     Succeeded());
338   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func2", 0x2345, {20}}),
339                     Succeeded());
340 
341   startFunction("func1", 0x1234);
342   addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
343 
344   startFunction("func2", 0x2345);
345   addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
346 
347   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
348 
349   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
350   EXPECT_EQ(2, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
351   for (const auto &FunctionRecord : FunctionRecords) {
352     CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
353     std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
354     ASSERT_EQ(2U, Segments.size());
355     if (FunctionRecord.Name == "func1") {
356       EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
357       EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]);
358     } else {
359       ASSERT_EQ("func2", FunctionRecord.Name);
360       EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]);
361       EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]);
362     }
363   }
364 }
365 
366 TEST_P(CoverageMappingTest, expansion_gets_first_counter) {
367   startFunction("func", 0x1234);
368   addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
369   // This starts earlier in "foo", so the expansion should get its counter.
370   addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
371   addExpansionCMR("bar", "foo", 3, 3, 3, 3);
372 
373   writeAndReadCoverageRegions();
374   ASSERT_EQ(1u, OutputFunctions.size());
375   OutputFunctionCoverageData &Output = OutputFunctions.back();
376 
377   ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind);
378   ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count);
379   ASSERT_EQ(3U, Output.Regions[2].LineStart);
380 }
381 
382 TEST_P(CoverageMappingTest, basic_coverage_iteration) {
383   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}),
384                     Succeeded());
385 
386   startFunction("func", 0x1234);
387   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
388   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
389   addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
390   addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
391   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
392 
393   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
394   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
395   ASSERT_EQ(7U, Segments.size());
396   ASSERT_EQ(CoverageSegment(1, 1, 20, true),  Segments[0]);
397   ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
398   ASSERT_EQ(CoverageSegment(5, 8, 10, true),  Segments[2]);
399   ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
400   ASSERT_EQ(CoverageSegment(9, 9, false),     Segments[4]);
401   ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
402   ASSERT_EQ(CoverageSegment(11, 11, false),   Segments[6]);
403 }
404 
405 TEST_P(CoverageMappingTest, uncovered_function) {
406   startFunction("func", 0x1234);
407   addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
408   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
409 
410   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
411   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
412   ASSERT_EQ(2U, Segments.size());
413   ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
414   ASSERT_EQ(CoverageSegment(3, 4, false),   Segments[1]);
415 }
416 
417 TEST_P(CoverageMappingTest, uncovered_function_with_mapping) {
418   startFunction("func", 0x1234);
419   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
420   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
421   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
422 
423   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
424   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
425   ASSERT_EQ(3U, Segments.size());
426   ASSERT_EQ(CoverageSegment(1, 1, 0, true),  Segments[0]);
427   ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
428   ASSERT_EQ(CoverageSegment(9, 9, false),    Segments[2]);
429 }
430 
431 TEST_P(CoverageMappingTest, combine_regions) {
432   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}),
433                     Succeeded());
434 
435   startFunction("func", 0x1234);
436   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
437   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
438   addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
439   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
440 
441   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
442   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
443   ASSERT_EQ(4U, Segments.size());
444   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
445   ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
446   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
447   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
448 }
449 
450 TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) {
451   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}),
452                     Succeeded());
453 
454   startFunction("func", 0x1234);
455   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
456   addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
457   addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
458   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
459 
460   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
461   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
462   ASSERT_EQ(4U, Segments.size());
463   EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]);
464   EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]);
465   EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]);
466   EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]);
467 }
468 
469 // If CodeRegions and ExpansionRegions cover the same area,
470 // only counts of CodeRegions should be used.
471 TEST_P(CoverageMappingTest, dont_combine_expansions) {
472   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {10, 20}}),
473                     Succeeded());
474   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {0, 0}}),
475                     Succeeded());
476 
477   startFunction("func", 0x1234);
478   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
479   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
480   addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
481   addExpansionCMR("file1", "include1", 3, 3, 4, 4);
482   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
483 
484   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
485   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
486   ASSERT_EQ(4U, Segments.size());
487   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
488   ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
489   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
490   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
491 }
492 
493 // If an area is covered only by ExpansionRegions, they should be combinated.
494 TEST_P(CoverageMappingTest, combine_expansions) {
495   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}),
496                     Succeeded());
497 
498   startFunction("func", 0x1234);
499   addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
500   addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
501   addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
502   addExpansionCMR("file", "include1", 3, 1, 3, 5);
503   addExpansionCMR("file", "include2", 3, 1, 3, 5);
504 
505   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
506 
507   CoverageData Data = LoadedCoverage->getCoverageForFile("file");
508   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
509   ASSERT_EQ(4U, Segments.size());
510   EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]);
511   EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]);
512   EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]);
513   EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]);
514 }
515 
516 TEST_P(CoverageMappingTest, strip_filename_prefix) {
517   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"file1:func", 0x1234, {0}}),
518                     Succeeded());
519 
520   startFunction("file1:func", 0x1234);
521   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
522   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
523 
524   std::vector<std::string> Names;
525   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
526     Names.push_back(Func.Name);
527   ASSERT_EQ(1U, Names.size());
528   ASSERT_EQ("func", Names[0]);
529 }
530 
531 TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) {
532   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}),
533                     Succeeded());
534 
535   startFunction("<unknown>:func", 0x1234);
536   addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
537   EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
538 
539   std::vector<std::string> Names;
540   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
541     Names.push_back(Func.Name);
542   ASSERT_EQ(1U, Names.size());
543   ASSERT_EQ("func", Names[0]);
544 }
545 
546 TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
547   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"foo", 0x1234, {10}}),
548                     Succeeded());
549   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"bar", 0x2345, {20}}),
550                     Succeeded());
551 
552   startFunction("foo", 0x1234);
553   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
554   addExpansionCMR("main", "expanded", 4, 1, 4, 5);
555 
556   startFunction("bar", 0x2345);
557   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
558   addExpansionCMR("main", "expanded", 9, 1, 9, 5);
559 
560   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
561 
562   std::vector<const FunctionRecord *> Instantiations =
563       LoadedCoverage->getInstantiations("expanded");
564   ASSERT_TRUE(Instantiations.empty());
565 }
566 
567 TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {
568   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {10}}),
569                     Succeeded());
570 
571   startFunction("func", 0x1234);
572   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
573   addExpansionCMR("main", "expanded", 4, 1, 4, 5);
574 
575   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
576 
577   CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
578   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
579   ASSERT_EQ(2U, Segments.size());
580   EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
581   EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]);
582 }
583 
584 TEST_P(CoverageMappingTest, skip_duplicate_function_record) {
585   EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {1}}),
586                     Succeeded());
587 
588   startFunction("func", 0x1234);
589   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
590 
591   startFunction("func", 0x1234);
592   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
593 
594   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
595 
596   auto Funcs = LoadedCoverage->getCoveredFunctions();
597   unsigned NumFuncs = std::distance(Funcs.begin(), Funcs.end());
598   ASSERT_EQ(1U, NumFuncs);
599 }
600 
601 // FIXME: Use ::testing::Combine() when llvm updates its copy of googletest.
602 INSTANTIATE_TEST_CASE_P(ParameterizedCovMapTest, CoverageMappingTest,
603                         ::testing::Values(std::pair<bool, bool>({false, false}),
604                                           std::pair<bool, bool>({false, true}),
605                                           std::pair<bool, bool>({true, false}),
606                                           std::pair<bool, bool>({true, true})),);
607 
608 } // end anonymous namespace
609