xref: /llvm-project/llvm/unittests/ProfileData/CoverageMappingTest.cpp (revision 618a22144db5e45da8c95dc22064103e1b5e5b71)
1 //===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
10 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
11 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
12 #include "llvm/ProfileData/InstrProfReader.h"
13 #include "llvm/ProfileData/InstrProfWriter.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/Testing/Support/Error.h"
16 #include "llvm/Testing/Support/SupportHelpers.h"
17 #include "gtest/gtest.h"
18 
19 #include <ostream>
20 #include <utility>
21 
22 using namespace llvm;
23 using namespace coverage;
24 
25 [[nodiscard]] static ::testing::AssertionResult
26 ErrorEquals(Error E, coveragemap_error Expected_Err,
27             const std::string &Expected_Msg = std::string()) {
28   coveragemap_error Found;
29   std::string Msg;
30   std::string FoundMsg;
31   handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
32     Found = CME.get();
33     Msg = CME.getMessage();
34     FoundMsg = CME.message();
35   });
36   if (Expected_Err == Found && Msg == Expected_Msg)
37     return ::testing::AssertionSuccess();
38   return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
39 }
40 
41 namespace llvm {
42 namespace coverage {
43 void PrintTo(const Counter &C, ::std::ostream *os) {
44   if (C.isZero())
45     *os << "Zero";
46   else if (C.isExpression())
47     *os << "Expression " << C.getExpressionID();
48   else
49     *os << "Counter " << C.getCounterID();
50 }
51 
52 void PrintTo(const CoverageSegment &S, ::std::ostream *os) {
53   *os << "CoverageSegment(" << S.Line << ", " << S.Col << ", ";
54   if (S.HasCount)
55     *os << S.Count << ", ";
56   *os << (S.IsRegionEntry ? "true" : "false") << ")";
57 }
58 }
59 }
60 
61 namespace {
62 
63 struct OutputFunctionCoverageData {
64   StringRef Name;
65   uint64_t Hash;
66   std::vector<StringRef> Filenames;
67   std::vector<CounterMappingRegion> Regions;
68   std::vector<CounterExpression> Expressions;
69 
70   OutputFunctionCoverageData() : Hash(0) {}
71 
72   OutputFunctionCoverageData(OutputFunctionCoverageData &&OFCD)
73       : Name(OFCD.Name), Hash(OFCD.Hash), Filenames(std::move(OFCD.Filenames)),
74         Regions(std::move(OFCD.Regions)) {}
75 
76   OutputFunctionCoverageData(const OutputFunctionCoverageData &) = delete;
77   OutputFunctionCoverageData &
78   operator=(const OutputFunctionCoverageData &) = delete;
79   OutputFunctionCoverageData &operator=(OutputFunctionCoverageData &&) = delete;
80 
81   void fillCoverageMappingRecord(CoverageMappingRecord &Record) const {
82     Record.FunctionName = Name;
83     Record.FunctionHash = Hash;
84     Record.Filenames = Filenames;
85     Record.Expressions = Expressions;
86     Record.MappingRegions = Regions;
87   }
88 };
89 
90 struct CoverageMappingReaderMock : CoverageMappingReader {
91   ArrayRef<OutputFunctionCoverageData> Functions;
92 
93   CoverageMappingReaderMock(ArrayRef<OutputFunctionCoverageData> Functions)
94       : Functions(Functions) {}
95 
96   Error readNextRecord(CoverageMappingRecord &Record) override {
97     if (Functions.empty())
98       return make_error<CoverageMapError>(coveragemap_error::eof);
99 
100     Functions.front().fillCoverageMappingRecord(Record);
101     Functions = Functions.slice(1);
102 
103     return Error::success();
104   }
105 };
106 
107 struct InputFunctionCoverageData {
108   // Maps the global file index from CoverageMappingTest.Files
109   // to the index of that file within this function. We can't just use
110   // global file indexes here because local indexes have to be dense.
111   // This map is used during serialization to create the virtual file mapping
112   // (from local fileId to global Index) in the head of the per-function
113   // coverage mapping data.
114   SmallDenseMap<unsigned, unsigned> ReverseVirtualFileMapping;
115   std::string Name;
116   uint64_t Hash;
117   std::vector<CounterMappingRegion> Regions;
118   std::vector<CounterExpression> Expressions;
119 
120   InputFunctionCoverageData(std::string Name, uint64_t Hash)
121       : Name(std::move(Name)), Hash(Hash) {}
122 
123   InputFunctionCoverageData(InputFunctionCoverageData &&IFCD)
124       : ReverseVirtualFileMapping(std::move(IFCD.ReverseVirtualFileMapping)),
125         Name(std::move(IFCD.Name)), Hash(IFCD.Hash),
126         Regions(std::move(IFCD.Regions)) {}
127 
128   InputFunctionCoverageData(const InputFunctionCoverageData &) = delete;
129   InputFunctionCoverageData &
130   operator=(const InputFunctionCoverageData &) = delete;
131   InputFunctionCoverageData &operator=(InputFunctionCoverageData &&) = delete;
132 };
133 
134 struct CoverageMappingTest : ::testing::TestWithParam<std::tuple<bool, bool>> {
135   bool UseMultipleReaders;
136   StringMap<unsigned> Files;
137   std::vector<std::string> Filenames;
138   std::vector<InputFunctionCoverageData> InputFunctions;
139   std::vector<OutputFunctionCoverageData> OutputFunctions;
140 
141   InstrProfWriter ProfileWriter;
142   std::unique_ptr<IndexedInstrProfReader> ProfileReader;
143 
144   std::unique_ptr<CoverageMapping> LoadedCoverage;
145 
146   void SetUp() override {
147     ProfileWriter.setOutputSparse(std::get<0>(GetParam()));
148     UseMultipleReaders = std::get<1>(GetParam());
149   }
150 
151   unsigned getGlobalFileIndex(StringRef Name) {
152     auto R = Files.find(Name);
153     if (R != Files.end())
154       return R->second;
155     unsigned Index = Files.size() + 1;
156     Files.try_emplace(Name, Index);
157     return Index;
158   }
159 
160   // Return the file index of file 'Name' for the current function.
161   // Add the file into the global map if necessary.
162   // See also InputFunctionCoverageData::ReverseVirtualFileMapping
163   // for additional comments.
164   unsigned getFileIndexForFunction(StringRef Name) {
165     unsigned GlobalIndex = getGlobalFileIndex(Name);
166     auto &CurrentFunctionFileMapping =
167         InputFunctions.back().ReverseVirtualFileMapping;
168     auto R = CurrentFunctionFileMapping.find(GlobalIndex);
169     if (R != CurrentFunctionFileMapping.end())
170       return R->second;
171     unsigned IndexInFunction = CurrentFunctionFileMapping.size();
172     CurrentFunctionFileMapping.insert(
173         std::make_pair(GlobalIndex, IndexInFunction));
174     return IndexInFunction;
175   }
176 
177   void startFunction(StringRef FuncName, uint64_t Hash) {
178     InputFunctions.emplace_back(FuncName.str(), Hash);
179   }
180 
181   void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
182               unsigned CE, bool Skipped = false) {
183     auto &Regions = InputFunctions.back().Regions;
184     unsigned FileID = getFileIndexForFunction(File);
185     Regions.push_back(
186         Skipped ? CounterMappingRegion::makeSkipped(FileID, LS, CS, LE, CE)
187                 : CounterMappingRegion::makeRegion(C, FileID, LS, CS, LE, CE));
188   }
189 
190   void addMCDCDecisionCMR(unsigned Mask, unsigned NC, StringRef File,
191                           unsigned LS, unsigned CS, unsigned LE, unsigned CE) {
192     auto &Regions = InputFunctions.back().Regions;
193     unsigned FileID = getFileIndexForFunction(File);
194     Regions.push_back(CounterMappingRegion::makeDecisionRegion(
195         CounterMappingRegion::MCDCParameters{Mask, NC}, FileID, LS, CS, LE,
196         CE));
197   }
198 
199   void addMCDCBranchCMR(Counter C1, Counter C2, unsigned ID, unsigned TrueID,
200                         unsigned FalseID, StringRef File, unsigned LS,
201                         unsigned CS, unsigned LE, unsigned CE) {
202     auto &Regions = InputFunctions.back().Regions;
203     unsigned FileID = getFileIndexForFunction(File);
204     Regions.push_back(CounterMappingRegion::makeBranchRegion(
205         C1, C2, CounterMappingRegion::MCDCParameters{0, 0, ID, TrueID, FalseID},
206         FileID, LS, CS, LE, CE));
207   }
208 
209   void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
210                        unsigned CS, unsigned LE, unsigned CE) {
211     InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion(
212         getFileIndexForFunction(File), getFileIndexForFunction(ExpandedFile),
213         LS, CS, LE, CE));
214   }
215 
216   void addExpression(CounterExpression CE) {
217     InputFunctions.back().Expressions.push_back(CE);
218   }
219 
220   std::string writeCoverageRegions(InputFunctionCoverageData &Data) {
221     SmallVector<unsigned, 8> FileIDs(Data.ReverseVirtualFileMapping.size());
222     for (const auto &E : Data.ReverseVirtualFileMapping)
223       FileIDs[E.second] = E.first;
224     std::string Coverage;
225     llvm::raw_string_ostream OS(Coverage);
226     CoverageMappingWriter(FileIDs, Data.Expressions, Data.Regions).write(OS);
227     return OS.str();
228   }
229 
230   void readCoverageRegions(const std::string &Coverage,
231                            OutputFunctionCoverageData &Data) {
232     // We will re-use the StringRef in duplicate tests, clear it to avoid
233     // clobber previous ones.
234     Filenames.clear();
235     Filenames.resize(Files.size() + 1);
236     for (const auto &E : Files)
237       Filenames[E.getValue()] = E.getKey().str();
238     ArrayRef<std::string> FilenameRefs = llvm::ArrayRef(Filenames);
239     RawCoverageMappingReader Reader(Coverage, FilenameRefs, Data.Filenames,
240                                     Data.Expressions, Data.Regions);
241     EXPECT_THAT_ERROR(Reader.read(), Succeeded());
242   }
243 
244   void writeAndReadCoverageRegions(bool EmitFilenames = true) {
245     OutputFunctions.resize(InputFunctions.size());
246     for (unsigned I = 0; I < InputFunctions.size(); ++I) {
247       std::string Regions = writeCoverageRegions(InputFunctions[I]);
248       readCoverageRegions(Regions, OutputFunctions[I]);
249       OutputFunctions[I].Name = InputFunctions[I].Name;
250       OutputFunctions[I].Hash = InputFunctions[I].Hash;
251       if (!EmitFilenames)
252         OutputFunctions[I].Filenames.clear();
253     }
254   }
255 
256   void readProfCounts() {
257     auto Profile = ProfileWriter.writeBuffer();
258     auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
259     EXPECT_THAT_ERROR(ReaderOrErr.takeError(), Succeeded());
260     ProfileReader = std::move(ReaderOrErr.get());
261   }
262 
263   Expected<std::unique_ptr<CoverageMapping>> readOutputFunctions() {
264     std::vector<std::unique_ptr<CoverageMappingReader>> CoverageReaders;
265     if (UseMultipleReaders) {
266       for (const auto &OF : OutputFunctions) {
267         ArrayRef<OutputFunctionCoverageData> Funcs(OF);
268         CoverageReaders.push_back(
269             std::make_unique<CoverageMappingReaderMock>(Funcs));
270       }
271     } else {
272       ArrayRef<OutputFunctionCoverageData> Funcs(OutputFunctions);
273       CoverageReaders.push_back(
274           std::make_unique<CoverageMappingReaderMock>(Funcs));
275     }
276     return CoverageMapping::load(CoverageReaders, *ProfileReader);
277   }
278 
279   Error loadCoverageMapping(bool EmitFilenames = true) {
280     readProfCounts();
281     writeAndReadCoverageRegions(EmitFilenames);
282     auto CoverageOrErr = readOutputFunctions();
283     if (!CoverageOrErr)
284       return CoverageOrErr.takeError();
285     LoadedCoverage = std::move(CoverageOrErr.get());
286     return Error::success();
287   }
288 };
289 
290 TEST_P(CoverageMappingTest, basic_write_read) {
291   startFunction("func", 0x1234);
292   addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
293   addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
294   addCMR(Counter::getZero(),     "foo", 3, 1, 3, 4);
295   addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
296   addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
297 
298   writeAndReadCoverageRegions();
299   ASSERT_EQ(1u, InputFunctions.size());
300   ASSERT_EQ(1u, OutputFunctions.size());
301   InputFunctionCoverageData &Input = InputFunctions.back();
302   OutputFunctionCoverageData &Output = OutputFunctions.back();
303 
304   size_t N = ArrayRef(Input.Regions).size();
305   ASSERT_EQ(N, Output.Regions.size());
306   for (size_t I = 0; I < N; ++I) {
307     ASSERT_EQ(Input.Regions[I].Count, Output.Regions[I].Count);
308     ASSERT_EQ(Input.Regions[I].FileID, Output.Regions[I].FileID);
309     ASSERT_EQ(Input.Regions[I].startLoc(), Output.Regions[I].startLoc());
310     ASSERT_EQ(Input.Regions[I].endLoc(), Output.Regions[I].endLoc());
311     ASSERT_EQ(Input.Regions[I].Kind, Output.Regions[I].Kind);
312   }
313 }
314 
315 TEST_P(CoverageMappingTest, correct_deserialize_for_more_than_two_files) {
316   const char *FileNames[] = {"bar", "baz", "foo"};
317   static const unsigned N = std::size(FileNames);
318 
319   startFunction("func", 0x1234);
320   for (unsigned I = 0; I < N; ++I)
321     // Use LineStart to hold the index of the file name
322     // in order to preserve that information during possible sorting of CMRs.
323     addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
324 
325   writeAndReadCoverageRegions();
326   ASSERT_EQ(1u, OutputFunctions.size());
327   OutputFunctionCoverageData &Output = OutputFunctions.back();
328 
329   ASSERT_EQ(N, Output.Regions.size());
330   ASSERT_EQ(N, Output.Filenames.size());
331 
332   for (unsigned I = 0; I < N; ++I) {
333     ASSERT_GT(N, Output.Regions[I].FileID);
334     ASSERT_GT(N, Output.Regions[I].LineStart);
335     EXPECT_EQ(FileNames[Output.Regions[I].LineStart],
336               Output.Filenames[Output.Regions[I].FileID]);
337   }
338 }
339 
340 static const auto Err = [](Error E) { FAIL(); };
341 
342 TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) {
343   ProfileWriter.addRecord({"func", 0x1234, {0}}, Err);
344 
345   const char *FileNames[] = {"bar", "baz", "foo"};
346   static const unsigned N = std::size(FileNames);
347 
348   startFunction("func", 0x1234);
349   for (unsigned I = 0; I < N; ++I)
350     // Use LineStart to hold the index of the file name
351     // in order to preserve that information during possible sorting of CMRs.
352     addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
353 
354   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
355 
356   for (unsigned I = 0; I < N; ++I) {
357     CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
358     ASSERT_TRUE(!Data.empty());
359     EXPECT_EQ(I, Data.begin()->Line);
360   }
361 }
362 
363 TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) {
364   ProfileWriter.addRecord({"", 0x1234, {10}}, Err);
365   startFunction("", 0x1234);
366   addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
367   EXPECT_TRUE(ErrorEquals(loadCoverageMapping(), coveragemap_error::malformed,
368                           "record function name is empty"));
369 }
370 
371 TEST_P(CoverageMappingTest, load_coverage_for_several_functions) {
372   ProfileWriter.addRecord({"func1", 0x1234, {10}}, Err);
373   ProfileWriter.addRecord({"func2", 0x2345, {20}}, Err);
374 
375   startFunction("func1", 0x1234);
376   addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
377 
378   startFunction("func2", 0x2345);
379   addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
380 
381   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
382 
383   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
384   EXPECT_EQ(2, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
385   for (const auto &FunctionRecord : FunctionRecords) {
386     CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
387     std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
388     ASSERT_EQ(2U, Segments.size());
389     if (FunctionRecord.Name == "func1") {
390       EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
391       EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]);
392     } else {
393       ASSERT_EQ("func2", FunctionRecord.Name);
394       EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]);
395       EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]);
396     }
397   }
398 }
399 
400 TEST_P(CoverageMappingTest, create_combined_regions) {
401   ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3}}, Err);
402   startFunction("func1", 0x1234);
403 
404   // Given regions which start at the same location, emit a segment for the
405   // last region.
406   addCMR(Counter::getCounter(0), "file1", 1, 1, 2, 2);
407   addCMR(Counter::getCounter(1), "file1", 1, 1, 2, 2);
408   addCMR(Counter::getCounter(2), "file1", 1, 1, 2, 2);
409 
410   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
411   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
412   const auto &FunctionRecord = *FunctionRecords.begin();
413   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
414   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
415 
416   ASSERT_EQ(2U, Segments.size());
417   EXPECT_EQ(CoverageSegment(1, 1, 6, true), Segments[0]);
418   EXPECT_EQ(CoverageSegment(2, 2, false), Segments[1]);
419 }
420 
421 TEST_P(CoverageMappingTest, skipped_segments_have_no_count) {
422   ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
423   startFunction("func1", 0x1234);
424 
425   addCMR(Counter::getCounter(0), "file1", 1, 1, 5, 5);
426   addCMR(Counter::getCounter(0), "file1", 5, 1, 5, 5, /*Skipped=*/true);
427 
428   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
429   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
430   const auto &FunctionRecord = *FunctionRecords.begin();
431   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
432   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
433 
434   ASSERT_EQ(3U, Segments.size());
435   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
436   EXPECT_EQ(CoverageSegment(5, 1, true), Segments[1]);
437   EXPECT_EQ(CoverageSegment(5, 5, false), Segments[2]);
438 }
439 
440 TEST_P(CoverageMappingTest, multiple_regions_end_after_parent_ends) {
441   ProfileWriter.addRecord({"func1", 0x1234, {1, 0}}, Err);
442   startFunction("func1", 0x1234);
443 
444   // 1| F{ a{
445   // 2|
446   // 3|    a} b{ c{
447   // 4|
448   // 5|    b}
449   // 6|
450   // 7| c} d{   e{
451   // 8|
452   // 9| d}      e} F}
453   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); // < F
454   addCMR(Counter::getCounter(0), "file1", 1, 1, 3, 5); // < a
455   addCMR(Counter::getCounter(0), "file1", 3, 5, 5, 4); // < b
456   addCMR(Counter::getCounter(1), "file1", 3, 5, 7, 3); // < c
457   addCMR(Counter::getCounter(1), "file1", 7, 3, 9, 2); // < d
458   addCMR(Counter::getCounter(1), "file1", 7, 7, 9, 7); // < e
459 
460   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
461   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
462   const auto &FunctionRecord = *FunctionRecords.begin();
463   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
464   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
465 
466   // Old output (not sorted or unique):
467   //   Segment at 1:1 with count 1
468   //   Segment at 1:1 with count 1
469   //   Segment at 3:5 with count 1
470   //   Segment at 3:5 with count 0
471   //   Segment at 3:5 with count 1
472   //   Segment at 5:4 with count 0
473   //   Segment at 7:3 with count 1
474   //   Segment at 7:3 with count 0
475   //   Segment at 7:7 with count 0
476   //   Segment at 9:7 with count 0
477   //   Segment at 9:2 with count 1
478   //   Top level segment at 9:9
479 
480   // New output (sorted and unique):
481   //   Segment at 1:1 (count = 1), RegionEntry
482   //   Segment at 3:5 (count = 1), RegionEntry
483   //   Segment at 5:4 (count = 0)
484   //   Segment at 7:3 (count = 0), RegionEntry
485   //   Segment at 7:7 (count = 0), RegionEntry
486   //   Segment at 9:2 (count = 0)
487   //   Segment at 9:7 (count = 1)
488   //   Segment at 9:9 (count = 0), Skipped
489 
490   ASSERT_EQ(8U, Segments.size());
491   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
492   EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[1]);
493   EXPECT_EQ(CoverageSegment(5, 4, 0, false), Segments[2]);
494   EXPECT_EQ(CoverageSegment(7, 3, 0, true), Segments[3]);
495   EXPECT_EQ(CoverageSegment(7, 7, 0, true), Segments[4]);
496   EXPECT_EQ(CoverageSegment(9, 2, 0, false), Segments[5]);
497   EXPECT_EQ(CoverageSegment(9, 7, 1, false), Segments[6]);
498   EXPECT_EQ(CoverageSegment(9, 9, false), Segments[7]);
499 }
500 
501 TEST_P(CoverageMappingTest, multiple_completed_segments_at_same_loc) {
502   ProfileWriter.addRecord({"func1", 0x1234, {0, 1, 2}}, Err);
503   startFunction("func1", 0x1234);
504 
505   // PR35495
506   addCMR(Counter::getCounter(1), "file1", 2, 1, 18, 2);
507   addCMR(Counter::getCounter(0), "file1", 8, 10, 14, 6);
508   addCMR(Counter::getCounter(0), "file1", 8, 12, 14, 6);
509   addCMR(Counter::getCounter(1), "file1", 9, 1, 14, 6);
510   addCMR(Counter::getCounter(2), "file1", 11, 13, 11, 14);
511 
512   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
513   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
514   const auto &FunctionRecord = *FunctionRecords.begin();
515   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
516   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
517 
518   ASSERT_EQ(7U, Segments.size());
519   EXPECT_EQ(CoverageSegment(2, 1, 1, true), Segments[0]);
520   EXPECT_EQ(CoverageSegment(8, 10, 0, true), Segments[1]);
521   EXPECT_EQ(CoverageSegment(8, 12, 0, true), Segments[2]);
522   EXPECT_EQ(CoverageSegment(9, 1, 1, true), Segments[3]);
523   EXPECT_EQ(CoverageSegment(11, 13, 2, true), Segments[4]);
524   // Use count=1 (from 9:1 -> 14:6), not count=0 (from 8:12 -> 14:6).
525   EXPECT_EQ(CoverageSegment(11, 14, 1, false), Segments[5]);
526   EXPECT_EQ(CoverageSegment(18, 2, false), Segments[6]);
527 }
528 
529 TEST_P(CoverageMappingTest, dont_emit_redundant_segments) {
530   ProfileWriter.addRecord({"func1", 0x1234, {1, 1}}, Err);
531   startFunction("func1", 0x1234);
532 
533   addCMR(Counter::getCounter(0), "file1", 1, 1, 4, 4);
534   addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
535   addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 6);
536 
537   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
538   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
539   const auto &FunctionRecord = *FunctionRecords.begin();
540   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
541   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
542 
543   ASSERT_EQ(5U, Segments.size());
544   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
545   EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
546   EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
547   EXPECT_EQ(CoverageSegment(4, 4, 1, false), Segments[3]);
548   // A closing segment starting at 5:5 would be redundant: it would have the
549   // same count as the segment starting at 4:4, and has all the same metadata.
550   EXPECT_EQ(CoverageSegment(6, 6, false), Segments[4]);
551 }
552 
553 TEST_P(CoverageMappingTest, dont_emit_closing_segment_at_new_region_start) {
554   ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
555   startFunction("func1", 0x1234);
556 
557   addCMR(Counter::getCounter(0), "file1", 1, 1, 6, 5);
558   addCMR(Counter::getCounter(0), "file1", 2, 2, 6, 5);
559   addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 5);
560   addCMR(Counter::getCounter(0), "file1", 6, 5, 7, 7);
561 
562   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
563   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
564   const auto &FunctionRecord = *FunctionRecords.begin();
565   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
566   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
567 
568   ASSERT_EQ(5U, Segments.size());
569   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
570   EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
571   EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
572   EXPECT_EQ(CoverageSegment(6, 5, 1, true), Segments[3]);
573   // The old segment builder would get this wrong by emitting multiple segments
574   // which start at 6:5 (a few of which were skipped segments). We should just
575   // get a segment for the region entry.
576   EXPECT_EQ(CoverageSegment(7, 7, false), Segments[4]);
577 }
578 
579 TEST_P(CoverageMappingTest, handle_consecutive_regions_with_zero_length) {
580   ProfileWriter.addRecord({"func1", 0x1234, {1, 2}}, Err);
581   startFunction("func1", 0x1234);
582 
583   addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
584   addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
585   addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
586   addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
587   addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
588 
589   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
590   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
591   const auto &FunctionRecord = *FunctionRecords.begin();
592   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
593   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
594 
595   ASSERT_EQ(1U, Segments.size());
596   EXPECT_EQ(CoverageSegment(1, 1, true), Segments[0]);
597   // We need to get a skipped segment starting at 1:1. In this case there is
598   // also a region entry at 1:1.
599 }
600 
601 TEST_P(CoverageMappingTest, handle_sandwiched_zero_length_region) {
602   ProfileWriter.addRecord({"func1", 0x1234, {2, 1}}, Err);
603   startFunction("func1", 0x1234);
604 
605   addCMR(Counter::getCounter(0), "file1", 1, 5, 4, 4);
606   addCMR(Counter::getCounter(1), "file1", 1, 9, 1, 50);
607   addCMR(Counter::getCounter(1), "file1", 2, 7, 2, 34);
608   addCMR(Counter::getCounter(1), "file1", 3, 5, 3, 21);
609   addCMR(Counter::getCounter(1), "file1", 3, 21, 3, 21);
610   addCMR(Counter::getCounter(1), "file1", 4, 12, 4, 17);
611 
612   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
613   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
614   const auto &FunctionRecord = *FunctionRecords.begin();
615   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
616   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
617 
618   ASSERT_EQ(10U, Segments.size());
619   EXPECT_EQ(CoverageSegment(1, 5, 2, true), Segments[0]);
620   EXPECT_EQ(CoverageSegment(1, 9, 1, true), Segments[1]);
621   EXPECT_EQ(CoverageSegment(1, 50, 2, false), Segments[2]);
622   EXPECT_EQ(CoverageSegment(2, 7, 1, true), Segments[3]);
623   EXPECT_EQ(CoverageSegment(2, 34, 2, false), Segments[4]);
624   EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[5]);
625   EXPECT_EQ(CoverageSegment(3, 21, 2, true), Segments[6]);
626   // Handle the zero-length region by creating a segment with its predecessor's
627   // count (i.e the count from 1:5 -> 4:4).
628   EXPECT_EQ(CoverageSegment(4, 4, false), Segments[7]);
629   // The area between 4:4 and 4:12 is skipped.
630   EXPECT_EQ(CoverageSegment(4, 12, 1, true), Segments[8]);
631   EXPECT_EQ(CoverageSegment(4, 17, false), Segments[9]);
632 }
633 
634 TEST_P(CoverageMappingTest, handle_last_completed_region) {
635   ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3, 4}}, Err);
636   startFunction("func1", 0x1234);
637 
638   addCMR(Counter::getCounter(0), "file1", 1, 1, 8, 8);
639   addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
640   addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
641   addCMR(Counter::getCounter(3), "file1", 6, 6, 7, 7);
642 
643   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
644   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
645   const auto &FunctionRecord = *FunctionRecords.begin();
646   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
647   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
648 
649   ASSERT_EQ(8U, Segments.size());
650   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
651   EXPECT_EQ(CoverageSegment(2, 2, 2, true), Segments[1]);
652   EXPECT_EQ(CoverageSegment(3, 3, 3, true), Segments[2]);
653   EXPECT_EQ(CoverageSegment(4, 4, 2, false), Segments[3]);
654   EXPECT_EQ(CoverageSegment(5, 5, 1, false), Segments[4]);
655   EXPECT_EQ(CoverageSegment(6, 6, 4, true), Segments[5]);
656   EXPECT_EQ(CoverageSegment(7, 7, 1, false), Segments[6]);
657   EXPECT_EQ(CoverageSegment(8, 8, false), Segments[7]);
658 }
659 
660 TEST_P(CoverageMappingTest, expansion_gets_first_counter) {
661   startFunction("func", 0x1234);
662   addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
663   // This starts earlier in "foo", so the expansion should get its counter.
664   addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
665   addExpansionCMR("bar", "foo", 3, 3, 3, 3);
666 
667   writeAndReadCoverageRegions();
668   ASSERT_EQ(1u, OutputFunctions.size());
669   OutputFunctionCoverageData &Output = OutputFunctions.back();
670 
671   ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind);
672   ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count);
673   ASSERT_EQ(3U, Output.Regions[2].LineStart);
674 }
675 
676 TEST_P(CoverageMappingTest, basic_coverage_iteration) {
677   ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
678 
679   startFunction("func", 0x1234);
680   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
681   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
682   addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
683   addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
684   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
685 
686   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
687   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
688   ASSERT_EQ(7U, Segments.size());
689   ASSERT_EQ(CoverageSegment(1, 1, 20, true),  Segments[0]);
690   ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
691   ASSERT_EQ(CoverageSegment(5, 8, 10, true),  Segments[2]);
692   ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
693   ASSERT_EQ(CoverageSegment(9, 9, false),     Segments[4]);
694   ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
695   ASSERT_EQ(CoverageSegment(11, 11, false),   Segments[6]);
696 }
697 
698 TEST_P(CoverageMappingTest, test_line_coverage_iterator) {
699   ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
700 
701   startFunction("func", 0x1234);
702   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
703   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
704   addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
705   addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
706   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
707 
708   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
709 
710   unsigned Line = 0;
711   unsigned LineCounts[] = {20, 20, 20, 20, 30, 10, 10, 10, 10, 0, 0};
712   for (const auto &LCS : getLineCoverageStats(Data)) {
713     ASSERT_EQ(Line + 1, LCS.getLine());
714     errs() << "Line: " << Line + 1 << ", count = " << LCS.getExecutionCount() << "\n";
715     ASSERT_EQ(LineCounts[Line], LCS.getExecutionCount());
716     ++Line;
717   }
718   ASSERT_EQ(11U, Line);
719 
720   // Check that operator->() works / compiles.
721   ASSERT_EQ(1U, LineCoverageIterator(Data)->getLine());
722 }
723 
724 TEST_P(CoverageMappingTest, uncovered_function) {
725   startFunction("func", 0x1234);
726   addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
727   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
728 
729   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
730   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
731   ASSERT_EQ(2U, Segments.size());
732   ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
733   ASSERT_EQ(CoverageSegment(3, 4, false),   Segments[1]);
734 }
735 
736 TEST_P(CoverageMappingTest, uncovered_function_with_mapping) {
737   startFunction("func", 0x1234);
738   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
739   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
740   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
741 
742   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
743   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
744   ASSERT_EQ(3U, Segments.size());
745   ASSERT_EQ(CoverageSegment(1, 1, 0, true),  Segments[0]);
746   ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
747   ASSERT_EQ(CoverageSegment(9, 9, false),    Segments[2]);
748 }
749 
750 TEST_P(CoverageMappingTest, combine_regions) {
751   ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}, Err);
752 
753   startFunction("func", 0x1234);
754   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
755   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
756   addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
757   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
758 
759   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
760   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
761   ASSERT_EQ(4U, Segments.size());
762   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
763   ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
764   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
765   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
766 }
767 
768 TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) {
769   ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}, Err);
770 
771   startFunction("func", 0x1234);
772   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
773   addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
774   addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
775   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
776 
777   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
778   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
779   ASSERT_EQ(4U, Segments.size());
780   EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]);
781   EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]);
782   EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]);
783   EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]);
784 }
785 
786 // If CodeRegions and ExpansionRegions cover the same area,
787 // only counts of CodeRegions should be used.
788 TEST_P(CoverageMappingTest, dont_combine_expansions) {
789   ProfileWriter.addRecord({"func", 0x1234, {10, 20}}, Err);
790   ProfileWriter.addRecord({"func", 0x1234, {0, 0}}, Err);
791 
792   startFunction("func", 0x1234);
793   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
794   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
795   addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
796   addExpansionCMR("file1", "include1", 3, 3, 4, 4);
797   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
798 
799   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
800   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
801   ASSERT_EQ(4U, Segments.size());
802   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
803   ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
804   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
805   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
806 }
807 
808 // If an area is covered only by ExpansionRegions, they should be combinated.
809 TEST_P(CoverageMappingTest, combine_expansions) {
810   ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}, Err);
811 
812   startFunction("func", 0x1234);
813   addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
814   addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
815   addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
816   addExpansionCMR("file", "include1", 3, 1, 3, 5);
817   addExpansionCMR("file", "include2", 3, 1, 3, 5);
818 
819   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
820 
821   CoverageData Data = LoadedCoverage->getCoverageForFile("file");
822   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
823   ASSERT_EQ(4U, Segments.size());
824   EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]);
825   EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]);
826   EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]);
827   EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]);
828 }
829 
830 // Test that counters not associated with any code regions are allowed.
831 TEST_P(CoverageMappingTest, non_code_region_counters) {
832   // No records in profdata
833 
834   startFunction("func", 0x1234);
835   addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
836   addCMR(Counter::getExpression(0), "file", 6, 1, 6, 5);
837   addExpression(CounterExpression(
838       CounterExpression::Add, Counter::getCounter(1), Counter::getCounter(2)));
839 
840   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
841 
842   std::vector<std::string> Names;
843   for (const auto &Func : LoadedCoverage->getCoveredFunctions()) {
844     Names.push_back(Func.Name);
845     ASSERT_EQ(2U, Func.CountedRegions.size());
846   }
847   ASSERT_EQ(1U, Names.size());
848 }
849 
850 // Test that MCDC bitmasks not associated with any code regions are allowed.
851 TEST_P(CoverageMappingTest, non_code_region_bitmask) {
852   // No records in profdata
853 
854   startFunction("func", 0x1234);
855   addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
856   addCMR(Counter::getCounter(1), "file", 1, 1, 5, 5);
857   addCMR(Counter::getCounter(2), "file", 1, 1, 5, 5);
858   addCMR(Counter::getCounter(3), "file", 1, 1, 5, 5);
859 
860   addMCDCDecisionCMR(0, 2, "file", 7, 1, 7, 6);
861   addMCDCBranchCMR(Counter::getCounter(0), Counter::getCounter(1), 1, 2, 0,
862                    "file", 7, 2, 7, 3);
863   addMCDCBranchCMR(Counter::getCounter(2), Counter::getCounter(3), 2, 0, 0,
864                    "file", 7, 4, 7, 5);
865 
866   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
867 
868   std::vector<std::string> Names;
869   for (const auto &Func : LoadedCoverage->getCoveredFunctions()) {
870     Names.push_back(Func.Name);
871     ASSERT_EQ(2U, Func.CountedBranchRegions.size());
872     ASSERT_EQ(1U, Func.MCDCRecords.size());
873   }
874   ASSERT_EQ(1U, Names.size());
875 }
876 
877 TEST_P(CoverageMappingTest, strip_filename_prefix) {
878   ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err);
879 
880   startFunction("file1:func", 0x1234);
881   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
882   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
883 
884   std::vector<std::string> Names;
885   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
886     Names.push_back(Func.Name);
887   ASSERT_EQ(1U, Names.size());
888   ASSERT_EQ("func", Names[0]);
889 }
890 
891 TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) {
892   ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}, Err);
893 
894   startFunction("<unknown>:func", 0x1234);
895   addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
896   EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
897 
898   std::vector<std::string> Names;
899   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
900     Names.push_back(Func.Name);
901   ASSERT_EQ(1U, Names.size());
902   ASSERT_EQ("func", Names[0]);
903 }
904 
905 TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
906   ProfileWriter.addRecord({"foo", 0x1234, {10}}, Err);
907   ProfileWriter.addRecord({"bar", 0x2345, {20}}, Err);
908 
909   startFunction("foo", 0x1234);
910   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
911   addExpansionCMR("main", "expanded", 4, 1, 4, 5);
912 
913   startFunction("bar", 0x2345);
914   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
915   addExpansionCMR("main", "expanded", 9, 1, 9, 5);
916 
917   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
918 
919   std::vector<InstantiationGroup> InstantiationGroups =
920       LoadedCoverage->getInstantiationGroups("expanded");
921   ASSERT_TRUE(InstantiationGroups.empty());
922 }
923 
924 TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {
925   ProfileWriter.addRecord({"func", 0x1234, {10}}, Err);
926 
927   startFunction("func", 0x1234);
928   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
929   addExpansionCMR("main", "expanded", 4, 1, 4, 5);
930 
931   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
932 
933   CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
934   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
935   ASSERT_EQ(2U, Segments.size());
936   EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
937   EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]);
938 }
939 
940 TEST_P(CoverageMappingTest, skip_duplicate_function_record) {
941   ProfileWriter.addRecord({"func", 0x1234, {1}}, Err);
942 
943   // This record should be loaded.
944   startFunction("func", 0x1234);
945   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
946 
947   // This record should be loaded.
948   startFunction("func", 0x1234);
949   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
950   addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
951 
952   // This record should be skipped.
953   startFunction("func", 0x1234);
954   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
955 
956   // This record should be loaded.
957   startFunction("func", 0x1234);
958   addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
959   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
960 
961   // This record should be skipped.
962   startFunction("func", 0x1234);
963   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
964   addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
965 
966   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
967 
968   auto Funcs = LoadedCoverage->getCoveredFunctions();
969   unsigned NumFuncs = std::distance(Funcs.begin(), Funcs.end());
970   ASSERT_EQ(3U, NumFuncs);
971 }
972 
973 INSTANTIATE_TEST_SUITE_P(ParameterizedCovMapTest, CoverageMappingTest,
974                          ::testing::Combine(::testing::Bool(),
975                                             ::testing::Bool()));
976 
977 TEST(CoverageMappingTest, filename_roundtrip) {
978   std::vector<std::string> Paths({"dir", "a", "b", "c", "d", "e"});
979 
980   for (bool Compress : {false, true}) {
981     std::string EncodedFilenames;
982     {
983       raw_string_ostream OS(EncodedFilenames);
984       CoverageFilenamesSectionWriter Writer(Paths);
985       Writer.write(OS, Compress);
986     }
987 
988     std::vector<std::string> ReadFilenames;
989     RawCoverageFilenamesReader Reader(EncodedFilenames, ReadFilenames);
990     EXPECT_THAT_ERROR(Reader.read(CovMapVersion::CurrentVersion), Succeeded());
991 
992     ASSERT_EQ(ReadFilenames.size(), Paths.size());
993     for (unsigned I = 1; I < Paths.size(); ++I) {
994       SmallString<256> P(Paths[0]);
995       llvm::sys::path::append(P, Paths[I]);
996       ASSERT_EQ(ReadFilenames[I], P);
997     }
998   }
999 }
1000 
1001 TEST(CoverageMappingTest, filename_compilation_dir) {
1002   std::vector<std::string> Paths({"dir", "a", "b", "c", "d", "e"});
1003 
1004   for (bool Compress : {false, true}) {
1005     std::string EncodedFilenames;
1006     {
1007       raw_string_ostream OS(EncodedFilenames);
1008       CoverageFilenamesSectionWriter Writer(Paths);
1009       Writer.write(OS, Compress);
1010     }
1011 
1012     StringRef CompilationDir = "out";
1013     std::vector<std::string> ReadFilenames;
1014     RawCoverageFilenamesReader Reader(EncodedFilenames, ReadFilenames,
1015                                       CompilationDir);
1016     EXPECT_THAT_ERROR(Reader.read(CovMapVersion::CurrentVersion), Succeeded());
1017 
1018     ASSERT_EQ(ReadFilenames.size(), Paths.size());
1019     for (unsigned I = 1; I < Paths.size(); ++I) {
1020       SmallString<256> P(CompilationDir);
1021       llvm::sys::path::append(P, Paths[I]);
1022       ASSERT_EQ(ReadFilenames[I], P);
1023     }
1024   }
1025 }
1026 
1027 } // end anonymous namespace
1028