xref: /llvm-project/llvm/unittests/ProfileData/CoverageMappingTest.cpp (revision cd8fe1dbcb7dd36f700681ced34b41993e844239)
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 addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
191                        unsigned CS, unsigned LE, unsigned CE) {
192     InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion(
193         getFileIndexForFunction(File), getFileIndexForFunction(ExpandedFile),
194         LS, CS, LE, CE));
195   }
196 
197   void addExpression(CounterExpression CE) {
198     InputFunctions.back().Expressions.push_back(CE);
199   }
200 
201   std::string writeCoverageRegions(InputFunctionCoverageData &Data) {
202     SmallVector<unsigned, 8> FileIDs(Data.ReverseVirtualFileMapping.size());
203     for (const auto &E : Data.ReverseVirtualFileMapping)
204       FileIDs[E.second] = E.first;
205     std::string Coverage;
206     llvm::raw_string_ostream OS(Coverage);
207     CoverageMappingWriter(FileIDs, Data.Expressions, Data.Regions).write(OS);
208     return OS.str();
209   }
210 
211   void readCoverageRegions(const std::string &Coverage,
212                            OutputFunctionCoverageData &Data) {
213     // We will re-use the StringRef in duplicate tests, clear it to avoid
214     // clobber previous ones.
215     Filenames.clear();
216     Filenames.resize(Files.size() + 1);
217     for (const auto &E : Files)
218       Filenames[E.getValue()] = E.getKey().str();
219     ArrayRef<std::string> FilenameRefs = llvm::ArrayRef(Filenames);
220     RawCoverageMappingReader Reader(Coverage, FilenameRefs, Data.Filenames,
221                                     Data.Expressions, Data.Regions);
222     EXPECT_THAT_ERROR(Reader.read(), Succeeded());
223   }
224 
225   void writeAndReadCoverageRegions(bool EmitFilenames = true) {
226     OutputFunctions.resize(InputFunctions.size());
227     for (unsigned I = 0; I < InputFunctions.size(); ++I) {
228       std::string Regions = writeCoverageRegions(InputFunctions[I]);
229       readCoverageRegions(Regions, OutputFunctions[I]);
230       OutputFunctions[I].Name = InputFunctions[I].Name;
231       OutputFunctions[I].Hash = InputFunctions[I].Hash;
232       if (!EmitFilenames)
233         OutputFunctions[I].Filenames.clear();
234     }
235   }
236 
237   void readProfCounts() {
238     auto Profile = ProfileWriter.writeBuffer();
239     auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
240     EXPECT_THAT_ERROR(ReaderOrErr.takeError(), Succeeded());
241     ProfileReader = std::move(ReaderOrErr.get());
242   }
243 
244   Expected<std::unique_ptr<CoverageMapping>> readOutputFunctions() {
245     std::vector<std::unique_ptr<CoverageMappingReader>> CoverageReaders;
246     if (UseMultipleReaders) {
247       for (const auto &OF : OutputFunctions) {
248         ArrayRef<OutputFunctionCoverageData> Funcs(OF);
249         CoverageReaders.push_back(
250             std::make_unique<CoverageMappingReaderMock>(Funcs));
251       }
252     } else {
253       ArrayRef<OutputFunctionCoverageData> Funcs(OutputFunctions);
254       CoverageReaders.push_back(
255           std::make_unique<CoverageMappingReaderMock>(Funcs));
256     }
257     return CoverageMapping::load(CoverageReaders, *ProfileReader);
258   }
259 
260   Error loadCoverageMapping(bool EmitFilenames = true) {
261     readProfCounts();
262     writeAndReadCoverageRegions(EmitFilenames);
263     auto CoverageOrErr = readOutputFunctions();
264     if (!CoverageOrErr)
265       return CoverageOrErr.takeError();
266     LoadedCoverage = std::move(CoverageOrErr.get());
267     return Error::success();
268   }
269 };
270 
271 TEST_P(CoverageMappingTest, basic_write_read) {
272   startFunction("func", 0x1234);
273   addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
274   addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
275   addCMR(Counter::getZero(),     "foo", 3, 1, 3, 4);
276   addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
277   addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
278 
279   writeAndReadCoverageRegions();
280   ASSERT_EQ(1u, InputFunctions.size());
281   ASSERT_EQ(1u, OutputFunctions.size());
282   InputFunctionCoverageData &Input = InputFunctions.back();
283   OutputFunctionCoverageData &Output = OutputFunctions.back();
284 
285   size_t N = ArrayRef(Input.Regions).size();
286   ASSERT_EQ(N, Output.Regions.size());
287   for (size_t I = 0; I < N; ++I) {
288     ASSERT_EQ(Input.Regions[I].Count, Output.Regions[I].Count);
289     ASSERT_EQ(Input.Regions[I].FileID, Output.Regions[I].FileID);
290     ASSERT_EQ(Input.Regions[I].startLoc(), Output.Regions[I].startLoc());
291     ASSERT_EQ(Input.Regions[I].endLoc(), Output.Regions[I].endLoc());
292     ASSERT_EQ(Input.Regions[I].Kind, Output.Regions[I].Kind);
293   }
294 }
295 
296 TEST_P(CoverageMappingTest, correct_deserialize_for_more_than_two_files) {
297   const char *FileNames[] = {"bar", "baz", "foo"};
298   static const unsigned N = std::size(FileNames);
299 
300   startFunction("func", 0x1234);
301   for (unsigned I = 0; I < N; ++I)
302     // Use LineStart to hold the index of the file name
303     // in order to preserve that information during possible sorting of CMRs.
304     addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
305 
306   writeAndReadCoverageRegions();
307   ASSERT_EQ(1u, OutputFunctions.size());
308   OutputFunctionCoverageData &Output = OutputFunctions.back();
309 
310   ASSERT_EQ(N, Output.Regions.size());
311   ASSERT_EQ(N, Output.Filenames.size());
312 
313   for (unsigned I = 0; I < N; ++I) {
314     ASSERT_GT(N, Output.Regions[I].FileID);
315     ASSERT_GT(N, Output.Regions[I].LineStart);
316     EXPECT_EQ(FileNames[Output.Regions[I].LineStart],
317               Output.Filenames[Output.Regions[I].FileID]);
318   }
319 }
320 
321 static const auto Err = [](Error E) { FAIL(); };
322 
323 TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) {
324   ProfileWriter.addRecord({"func", 0x1234, {0}}, Err);
325 
326   const char *FileNames[] = {"bar", "baz", "foo"};
327   static const unsigned N = std::size(FileNames);
328 
329   startFunction("func", 0x1234);
330   for (unsigned I = 0; I < N; ++I)
331     // Use LineStart to hold the index of the file name
332     // in order to preserve that information during possible sorting of CMRs.
333     addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
334 
335   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
336 
337   for (unsigned I = 0; I < N; ++I) {
338     CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
339     ASSERT_TRUE(!Data.empty());
340     EXPECT_EQ(I, Data.begin()->Line);
341   }
342 }
343 
344 TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) {
345   ProfileWriter.addRecord({"", 0x1234, {10}}, Err);
346   startFunction("", 0x1234);
347   addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
348   EXPECT_TRUE(ErrorEquals(loadCoverageMapping(), coveragemap_error::malformed,
349                           "record function name is empty"));
350 }
351 
352 TEST_P(CoverageMappingTest, load_coverage_for_several_functions) {
353   ProfileWriter.addRecord({"func1", 0x1234, {10}}, Err);
354   ProfileWriter.addRecord({"func2", 0x2345, {20}}, Err);
355 
356   startFunction("func1", 0x1234);
357   addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
358 
359   startFunction("func2", 0x2345);
360   addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
361 
362   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
363 
364   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
365   EXPECT_EQ(2, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
366   for (const auto &FunctionRecord : FunctionRecords) {
367     CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
368     std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
369     ASSERT_EQ(2U, Segments.size());
370     if (FunctionRecord.Name == "func1") {
371       EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
372       EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]);
373     } else {
374       ASSERT_EQ("func2", FunctionRecord.Name);
375       EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]);
376       EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]);
377     }
378   }
379 }
380 
381 TEST_P(CoverageMappingTest, create_combined_regions) {
382   ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3}}, Err);
383   startFunction("func1", 0x1234);
384 
385   // Given regions which start at the same location, emit a segment for the
386   // last region.
387   addCMR(Counter::getCounter(0), "file1", 1, 1, 2, 2);
388   addCMR(Counter::getCounter(1), "file1", 1, 1, 2, 2);
389   addCMR(Counter::getCounter(2), "file1", 1, 1, 2, 2);
390 
391   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
392   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
393   const auto &FunctionRecord = *FunctionRecords.begin();
394   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
395   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
396 
397   ASSERT_EQ(2U, Segments.size());
398   EXPECT_EQ(CoverageSegment(1, 1, 6, true), Segments[0]);
399   EXPECT_EQ(CoverageSegment(2, 2, false), Segments[1]);
400 }
401 
402 TEST_P(CoverageMappingTest, skipped_segments_have_no_count) {
403   ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
404   startFunction("func1", 0x1234);
405 
406   addCMR(Counter::getCounter(0), "file1", 1, 1, 5, 5);
407   addCMR(Counter::getCounter(0), "file1", 5, 1, 5, 5, /*Skipped=*/true);
408 
409   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
410   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
411   const auto &FunctionRecord = *FunctionRecords.begin();
412   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
413   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
414 
415   ASSERT_EQ(3U, Segments.size());
416   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
417   EXPECT_EQ(CoverageSegment(5, 1, true), Segments[1]);
418   EXPECT_EQ(CoverageSegment(5, 5, false), Segments[2]);
419 }
420 
421 TEST_P(CoverageMappingTest, multiple_regions_end_after_parent_ends) {
422   ProfileWriter.addRecord({"func1", 0x1234, {1, 0}}, Err);
423   startFunction("func1", 0x1234);
424 
425   // 1| F{ a{
426   // 2|
427   // 3|    a} b{ c{
428   // 4|
429   // 5|    b}
430   // 6|
431   // 7| c} d{   e{
432   // 8|
433   // 9| d}      e} F}
434   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); // < F
435   addCMR(Counter::getCounter(0), "file1", 1, 1, 3, 5); // < a
436   addCMR(Counter::getCounter(0), "file1", 3, 5, 5, 4); // < b
437   addCMR(Counter::getCounter(1), "file1", 3, 5, 7, 3); // < c
438   addCMR(Counter::getCounter(1), "file1", 7, 3, 9, 2); // < d
439   addCMR(Counter::getCounter(1), "file1", 7, 7, 9, 7); // < e
440 
441   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
442   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
443   const auto &FunctionRecord = *FunctionRecords.begin();
444   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
445   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
446 
447   // Old output (not sorted or unique):
448   //   Segment at 1:1 with count 1
449   //   Segment at 1:1 with count 1
450   //   Segment at 3:5 with count 1
451   //   Segment at 3:5 with count 0
452   //   Segment at 3:5 with count 1
453   //   Segment at 5:4 with count 0
454   //   Segment at 7:3 with count 1
455   //   Segment at 7:3 with count 0
456   //   Segment at 7:7 with count 0
457   //   Segment at 9:7 with count 0
458   //   Segment at 9:2 with count 1
459   //   Top level segment at 9:9
460 
461   // New output (sorted and unique):
462   //   Segment at 1:1 (count = 1), RegionEntry
463   //   Segment at 3:5 (count = 1), RegionEntry
464   //   Segment at 5:4 (count = 0)
465   //   Segment at 7:3 (count = 0), RegionEntry
466   //   Segment at 7:7 (count = 0), RegionEntry
467   //   Segment at 9:2 (count = 0)
468   //   Segment at 9:7 (count = 1)
469   //   Segment at 9:9 (count = 0), Skipped
470 
471   ASSERT_EQ(8U, Segments.size());
472   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
473   EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[1]);
474   EXPECT_EQ(CoverageSegment(5, 4, 0, false), Segments[2]);
475   EXPECT_EQ(CoverageSegment(7, 3, 0, true), Segments[3]);
476   EXPECT_EQ(CoverageSegment(7, 7, 0, true), Segments[4]);
477   EXPECT_EQ(CoverageSegment(9, 2, 0, false), Segments[5]);
478   EXPECT_EQ(CoverageSegment(9, 7, 1, false), Segments[6]);
479   EXPECT_EQ(CoverageSegment(9, 9, false), Segments[7]);
480 }
481 
482 TEST_P(CoverageMappingTest, multiple_completed_segments_at_same_loc) {
483   ProfileWriter.addRecord({"func1", 0x1234, {0, 1, 2}}, Err);
484   startFunction("func1", 0x1234);
485 
486   // PR35495
487   addCMR(Counter::getCounter(1), "file1", 2, 1, 18, 2);
488   addCMR(Counter::getCounter(0), "file1", 8, 10, 14, 6);
489   addCMR(Counter::getCounter(0), "file1", 8, 12, 14, 6);
490   addCMR(Counter::getCounter(1), "file1", 9, 1, 14, 6);
491   addCMR(Counter::getCounter(2), "file1", 11, 13, 11, 14);
492 
493   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
494   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
495   const auto &FunctionRecord = *FunctionRecords.begin();
496   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
497   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
498 
499   ASSERT_EQ(7U, Segments.size());
500   EXPECT_EQ(CoverageSegment(2, 1, 1, true), Segments[0]);
501   EXPECT_EQ(CoverageSegment(8, 10, 0, true), Segments[1]);
502   EXPECT_EQ(CoverageSegment(8, 12, 0, true), Segments[2]);
503   EXPECT_EQ(CoverageSegment(9, 1, 1, true), Segments[3]);
504   EXPECT_EQ(CoverageSegment(11, 13, 2, true), Segments[4]);
505   // Use count=1 (from 9:1 -> 14:6), not count=0 (from 8:12 -> 14:6).
506   EXPECT_EQ(CoverageSegment(11, 14, 1, false), Segments[5]);
507   EXPECT_EQ(CoverageSegment(18, 2, false), Segments[6]);
508 }
509 
510 TEST_P(CoverageMappingTest, dont_emit_redundant_segments) {
511   ProfileWriter.addRecord({"func1", 0x1234, {1, 1}}, Err);
512   startFunction("func1", 0x1234);
513 
514   addCMR(Counter::getCounter(0), "file1", 1, 1, 4, 4);
515   addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
516   addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 6);
517 
518   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
519   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
520   const auto &FunctionRecord = *FunctionRecords.begin();
521   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
522   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
523 
524   ASSERT_EQ(5U, Segments.size());
525   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
526   EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
527   EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
528   EXPECT_EQ(CoverageSegment(4, 4, 1, false), Segments[3]);
529   // A closing segment starting at 5:5 would be redundant: it would have the
530   // same count as the segment starting at 4:4, and has all the same metadata.
531   EXPECT_EQ(CoverageSegment(6, 6, false), Segments[4]);
532 }
533 
534 TEST_P(CoverageMappingTest, dont_emit_closing_segment_at_new_region_start) {
535   ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
536   startFunction("func1", 0x1234);
537 
538   addCMR(Counter::getCounter(0), "file1", 1, 1, 6, 5);
539   addCMR(Counter::getCounter(0), "file1", 2, 2, 6, 5);
540   addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 5);
541   addCMR(Counter::getCounter(0), "file1", 6, 5, 7, 7);
542 
543   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
544   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
545   const auto &FunctionRecord = *FunctionRecords.begin();
546   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
547   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
548 
549   ASSERT_EQ(5U, Segments.size());
550   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
551   EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
552   EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
553   EXPECT_EQ(CoverageSegment(6, 5, 1, true), Segments[3]);
554   // The old segment builder would get this wrong by emitting multiple segments
555   // which start at 6:5 (a few of which were skipped segments). We should just
556   // get a segment for the region entry.
557   EXPECT_EQ(CoverageSegment(7, 7, false), Segments[4]);
558 }
559 
560 TEST_P(CoverageMappingTest, handle_consecutive_regions_with_zero_length) {
561   ProfileWriter.addRecord({"func1", 0x1234, {1, 2}}, Err);
562   startFunction("func1", 0x1234);
563 
564   addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
565   addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
566   addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
567   addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
568   addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
569 
570   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
571   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
572   const auto &FunctionRecord = *FunctionRecords.begin();
573   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
574   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
575 
576   ASSERT_EQ(1U, Segments.size());
577   EXPECT_EQ(CoverageSegment(1, 1, true), Segments[0]);
578   // We need to get a skipped segment starting at 1:1. In this case there is
579   // also a region entry at 1:1.
580 }
581 
582 TEST_P(CoverageMappingTest, handle_sandwiched_zero_length_region) {
583   ProfileWriter.addRecord({"func1", 0x1234, {2, 1}}, Err);
584   startFunction("func1", 0x1234);
585 
586   addCMR(Counter::getCounter(0), "file1", 1, 5, 4, 4);
587   addCMR(Counter::getCounter(1), "file1", 1, 9, 1, 50);
588   addCMR(Counter::getCounter(1), "file1", 2, 7, 2, 34);
589   addCMR(Counter::getCounter(1), "file1", 3, 5, 3, 21);
590   addCMR(Counter::getCounter(1), "file1", 3, 21, 3, 21);
591   addCMR(Counter::getCounter(1), "file1", 4, 12, 4, 17);
592 
593   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
594   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
595   const auto &FunctionRecord = *FunctionRecords.begin();
596   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
597   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
598 
599   ASSERT_EQ(10U, Segments.size());
600   EXPECT_EQ(CoverageSegment(1, 5, 2, true), Segments[0]);
601   EXPECT_EQ(CoverageSegment(1, 9, 1, true), Segments[1]);
602   EXPECT_EQ(CoverageSegment(1, 50, 2, false), Segments[2]);
603   EXPECT_EQ(CoverageSegment(2, 7, 1, true), Segments[3]);
604   EXPECT_EQ(CoverageSegment(2, 34, 2, false), Segments[4]);
605   EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[5]);
606   EXPECT_EQ(CoverageSegment(3, 21, 2, true), Segments[6]);
607   // Handle the zero-length region by creating a segment with its predecessor's
608   // count (i.e the count from 1:5 -> 4:4).
609   EXPECT_EQ(CoverageSegment(4, 4, false), Segments[7]);
610   // The area between 4:4 and 4:12 is skipped.
611   EXPECT_EQ(CoverageSegment(4, 12, 1, true), Segments[8]);
612   EXPECT_EQ(CoverageSegment(4, 17, false), Segments[9]);
613 }
614 
615 TEST_P(CoverageMappingTest, handle_last_completed_region) {
616   ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3, 4}}, Err);
617   startFunction("func1", 0x1234);
618 
619   addCMR(Counter::getCounter(0), "file1", 1, 1, 8, 8);
620   addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
621   addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
622   addCMR(Counter::getCounter(3), "file1", 6, 6, 7, 7);
623 
624   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
625   const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
626   const auto &FunctionRecord = *FunctionRecords.begin();
627   CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
628   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
629 
630   ASSERT_EQ(8U, Segments.size());
631   EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
632   EXPECT_EQ(CoverageSegment(2, 2, 2, true), Segments[1]);
633   EXPECT_EQ(CoverageSegment(3, 3, 3, true), Segments[2]);
634   EXPECT_EQ(CoverageSegment(4, 4, 2, false), Segments[3]);
635   EXPECT_EQ(CoverageSegment(5, 5, 1, false), Segments[4]);
636   EXPECT_EQ(CoverageSegment(6, 6, 4, true), Segments[5]);
637   EXPECT_EQ(CoverageSegment(7, 7, 1, false), Segments[6]);
638   EXPECT_EQ(CoverageSegment(8, 8, false), Segments[7]);
639 }
640 
641 TEST_P(CoverageMappingTest, expansion_gets_first_counter) {
642   startFunction("func", 0x1234);
643   addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
644   // This starts earlier in "foo", so the expansion should get its counter.
645   addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
646   addExpansionCMR("bar", "foo", 3, 3, 3, 3);
647 
648   writeAndReadCoverageRegions();
649   ASSERT_EQ(1u, OutputFunctions.size());
650   OutputFunctionCoverageData &Output = OutputFunctions.back();
651 
652   ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind);
653   ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count);
654   ASSERT_EQ(3U, Output.Regions[2].LineStart);
655 }
656 
657 TEST_P(CoverageMappingTest, basic_coverage_iteration) {
658   ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
659 
660   startFunction("func", 0x1234);
661   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
662   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
663   addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
664   addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
665   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
666 
667   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
668   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
669   ASSERT_EQ(7U, Segments.size());
670   ASSERT_EQ(CoverageSegment(1, 1, 20, true),  Segments[0]);
671   ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
672   ASSERT_EQ(CoverageSegment(5, 8, 10, true),  Segments[2]);
673   ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
674   ASSERT_EQ(CoverageSegment(9, 9, false),     Segments[4]);
675   ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
676   ASSERT_EQ(CoverageSegment(11, 11, false),   Segments[6]);
677 }
678 
679 TEST_P(CoverageMappingTest, test_line_coverage_iterator) {
680   ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
681 
682   startFunction("func", 0x1234);
683   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
684   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
685   addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
686   addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
687   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
688 
689   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
690 
691   unsigned Line = 0;
692   unsigned LineCounts[] = {20, 20, 20, 20, 30, 10, 10, 10, 10, 0, 0};
693   for (const auto &LCS : getLineCoverageStats(Data)) {
694     ASSERT_EQ(Line + 1, LCS.getLine());
695     errs() << "Line: " << Line + 1 << ", count = " << LCS.getExecutionCount() << "\n";
696     ASSERT_EQ(LineCounts[Line], LCS.getExecutionCount());
697     ++Line;
698   }
699   ASSERT_EQ(11U, Line);
700 
701   // Check that operator->() works / compiles.
702   ASSERT_EQ(1U, LineCoverageIterator(Data)->getLine());
703 }
704 
705 TEST_P(CoverageMappingTest, uncovered_function) {
706   startFunction("func", 0x1234);
707   addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
708   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
709 
710   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
711   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
712   ASSERT_EQ(2U, Segments.size());
713   ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
714   ASSERT_EQ(CoverageSegment(3, 4, false),   Segments[1]);
715 }
716 
717 TEST_P(CoverageMappingTest, uncovered_function_with_mapping) {
718   startFunction("func", 0x1234);
719   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
720   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
721   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
722 
723   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
724   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
725   ASSERT_EQ(3U, Segments.size());
726   ASSERT_EQ(CoverageSegment(1, 1, 0, true),  Segments[0]);
727   ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
728   ASSERT_EQ(CoverageSegment(9, 9, false),    Segments[2]);
729 }
730 
731 TEST_P(CoverageMappingTest, combine_regions) {
732   ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}, Err);
733 
734   startFunction("func", 0x1234);
735   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
736   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
737   addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
738   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
739 
740   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
741   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
742   ASSERT_EQ(4U, Segments.size());
743   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
744   ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
745   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
746   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
747 }
748 
749 TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) {
750   ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}, Err);
751 
752   startFunction("func", 0x1234);
753   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
754   addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
755   addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
756   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
757 
758   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
759   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
760   ASSERT_EQ(4U, Segments.size());
761   EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]);
762   EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]);
763   EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]);
764   EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]);
765 }
766 
767 // If CodeRegions and ExpansionRegions cover the same area,
768 // only counts of CodeRegions should be used.
769 TEST_P(CoverageMappingTest, dont_combine_expansions) {
770   ProfileWriter.addRecord({"func", 0x1234, {10, 20}}, Err);
771   ProfileWriter.addRecord({"func", 0x1234, {0, 0}}, Err);
772 
773   startFunction("func", 0x1234);
774   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
775   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
776   addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
777   addExpansionCMR("file1", "include1", 3, 3, 4, 4);
778   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
779 
780   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
781   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
782   ASSERT_EQ(4U, Segments.size());
783   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
784   ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
785   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
786   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
787 }
788 
789 // If an area is covered only by ExpansionRegions, they should be combinated.
790 TEST_P(CoverageMappingTest, combine_expansions) {
791   ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}, Err);
792 
793   startFunction("func", 0x1234);
794   addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
795   addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
796   addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
797   addExpansionCMR("file", "include1", 3, 1, 3, 5);
798   addExpansionCMR("file", "include2", 3, 1, 3, 5);
799 
800   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
801 
802   CoverageData Data = LoadedCoverage->getCoverageForFile("file");
803   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
804   ASSERT_EQ(4U, Segments.size());
805   EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]);
806   EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]);
807   EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]);
808   EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]);
809 }
810 
811 // Test that counters not associated with any code regions are allowed.
812 TEST_P(CoverageMappingTest, non_code_region_counters) {
813   // No records in profdata
814 
815   startFunction("func", 0x1234);
816   addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
817   addCMR(Counter::getExpression(0), "file", 6, 1, 6, 5);
818   addExpression(CounterExpression(
819       CounterExpression::Add, Counter::getCounter(1), Counter::getCounter(2)));
820 
821   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
822 
823   std::vector<std::string> Names;
824   for (const auto &Func : LoadedCoverage->getCoveredFunctions()) {
825     Names.push_back(Func.Name);
826     ASSERT_EQ(2U, Func.CountedRegions.size());
827   }
828   ASSERT_EQ(1U, Names.size());
829 }
830 
831 TEST_P(CoverageMappingTest, strip_filename_prefix) {
832   ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err);
833 
834   startFunction("file1:func", 0x1234);
835   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
836   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
837 
838   std::vector<std::string> Names;
839   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
840     Names.push_back(Func.Name);
841   ASSERT_EQ(1U, Names.size());
842   ASSERT_EQ("func", Names[0]);
843 }
844 
845 TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) {
846   ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}, Err);
847 
848   startFunction("<unknown>:func", 0x1234);
849   addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
850   EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
851 
852   std::vector<std::string> Names;
853   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
854     Names.push_back(Func.Name);
855   ASSERT_EQ(1U, Names.size());
856   ASSERT_EQ("func", Names[0]);
857 }
858 
859 TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
860   ProfileWriter.addRecord({"foo", 0x1234, {10}}, Err);
861   ProfileWriter.addRecord({"bar", 0x2345, {20}}, Err);
862 
863   startFunction("foo", 0x1234);
864   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
865   addExpansionCMR("main", "expanded", 4, 1, 4, 5);
866 
867   startFunction("bar", 0x2345);
868   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
869   addExpansionCMR("main", "expanded", 9, 1, 9, 5);
870 
871   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
872 
873   std::vector<InstantiationGroup> InstantiationGroups =
874       LoadedCoverage->getInstantiationGroups("expanded");
875   ASSERT_TRUE(InstantiationGroups.empty());
876 }
877 
878 TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {
879   ProfileWriter.addRecord({"func", 0x1234, {10}}, Err);
880 
881   startFunction("func", 0x1234);
882   addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
883   addExpansionCMR("main", "expanded", 4, 1, 4, 5);
884 
885   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
886 
887   CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
888   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
889   ASSERT_EQ(2U, Segments.size());
890   EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
891   EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]);
892 }
893 
894 TEST_P(CoverageMappingTest, skip_duplicate_function_record) {
895   ProfileWriter.addRecord({"func", 0x1234, {1}}, Err);
896 
897   // This record should be loaded.
898   startFunction("func", 0x1234);
899   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
900 
901   // This record should be loaded.
902   startFunction("func", 0x1234);
903   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
904   addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
905 
906   // This record should be skipped.
907   startFunction("func", 0x1234);
908   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
909 
910   // This record should be loaded.
911   startFunction("func", 0x1234);
912   addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
913   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
914 
915   // This record should be skipped.
916   startFunction("func", 0x1234);
917   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
918   addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
919 
920   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
921 
922   auto Funcs = LoadedCoverage->getCoveredFunctions();
923   unsigned NumFuncs = std::distance(Funcs.begin(), Funcs.end());
924   ASSERT_EQ(3U, NumFuncs);
925 }
926 
927 INSTANTIATE_TEST_SUITE_P(ParameterizedCovMapTest, CoverageMappingTest,
928                          ::testing::Combine(::testing::Bool(),
929                                             ::testing::Bool()));
930 
931 TEST(CoverageMappingTest, filename_roundtrip) {
932   std::vector<std::string> Paths({"dir", "a", "b", "c", "d", "e"});
933 
934   for (bool Compress : {false, true}) {
935     std::string EncodedFilenames;
936     {
937       raw_string_ostream OS(EncodedFilenames);
938       CoverageFilenamesSectionWriter Writer(Paths);
939       Writer.write(OS, Compress);
940     }
941 
942     std::vector<std::string> ReadFilenames;
943     RawCoverageFilenamesReader Reader(EncodedFilenames, ReadFilenames);
944     EXPECT_THAT_ERROR(Reader.read(CovMapVersion::CurrentVersion), Succeeded());
945 
946     ASSERT_EQ(ReadFilenames.size(), Paths.size());
947     for (unsigned I = 1; I < Paths.size(); ++I) {
948       SmallString<256> P(Paths[0]);
949       llvm::sys::path::append(P, Paths[I]);
950       ASSERT_EQ(ReadFilenames[I], P);
951     }
952   }
953 }
954 
955 TEST(CoverageMappingTest, filename_compilation_dir) {
956   std::vector<std::string> Paths({"dir", "a", "b", "c", "d", "e"});
957 
958   for (bool Compress : {false, true}) {
959     std::string EncodedFilenames;
960     {
961       raw_string_ostream OS(EncodedFilenames);
962       CoverageFilenamesSectionWriter Writer(Paths);
963       Writer.write(OS, Compress);
964     }
965 
966     StringRef CompilationDir = "out";
967     std::vector<std::string> ReadFilenames;
968     RawCoverageFilenamesReader Reader(EncodedFilenames, ReadFilenames,
969                                       CompilationDir);
970     EXPECT_THAT_ERROR(Reader.read(CovMapVersion::CurrentVersion), Succeeded());
971 
972     ASSERT_EQ(ReadFilenames.size(), Paths.size());
973     for (unsigned I = 1; I < Paths.size(); ++I) {
974       SmallString<256> P(CompilationDir);
975       llvm::sys::path::append(P, Paths[I]);
976       ASSERT_EQ(ReadFilenames[I], P);
977     }
978   }
979 }
980 
981 } // end anonymous namespace
982