1 //===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
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
7 //===----------------------------------------------------------------------===//
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"
23 using namespace coverage
;
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
;
31 handleAllErrors(std::move(E
), [&](const CoverageMapError
&CME
) {
33 Msg
= CME
.getMessage();
34 FoundMsg
= CME
.message();
36 if (Expected_Err
== Found
&& Msg
== Expected_Msg
)
37 return ::testing::AssertionSuccess();
38 return ::testing::AssertionFailure() << "error: " << FoundMsg
<< "\n";
43 void PrintTo(const Counter
&C
, ::std::ostream
*os
) {
46 else if (C
.isExpression())
47 *os
<< "Expression " << C
.getExpressionID();
49 *os
<< "Counter " << C
.getCounterID();
52 void PrintTo(const CoverageSegment
&S
, ::std::ostream
*os
) {
53 *os
<< "CoverageSegment(" << S
.Line
<< ", " << S
.Col
<< ", ";
55 *os
<< S
.Count
<< ", ";
56 *os
<< (S
.IsRegionEntry
? "true" : "false") << ")";
63 struct OutputFunctionCoverageData
{
66 std::vector
<StringRef
> Filenames
;
67 std::vector
<CounterMappingRegion
> Regions
;
68 std::vector
<CounterExpression
> Expressions
;
70 OutputFunctionCoverageData() : Hash(0) {}
72 OutputFunctionCoverageData(OutputFunctionCoverageData
&&OFCD
)
73 : Name(OFCD
.Name
), Hash(OFCD
.Hash
), Filenames(std::move(OFCD
.Filenames
)),
74 Regions(std::move(OFCD
.Regions
)) {}
76 OutputFunctionCoverageData(const OutputFunctionCoverageData
&) = delete;
77 OutputFunctionCoverageData
&
78 operator=(const OutputFunctionCoverageData
&) = delete;
79 OutputFunctionCoverageData
&operator=(OutputFunctionCoverageData
&&) = delete;
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
;
90 struct CoverageMappingReaderMock
: CoverageMappingReader
{
91 ArrayRef
<OutputFunctionCoverageData
> Functions
;
93 CoverageMappingReaderMock(ArrayRef
<OutputFunctionCoverageData
> Functions
)
94 : Functions(Functions
) {}
96 Error
readNextRecord(CoverageMappingRecord
&Record
) override
{
97 if (Functions
.empty())
98 return make_error
<CoverageMapError
>(coveragemap_error::eof
);
100 Functions
.front().fillCoverageMappingRecord(Record
);
101 Functions
= Functions
.slice(1);
103 return Error::success();
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
;
117 std::vector
<CounterMappingRegion
> Regions
;
118 std::vector
<CounterExpression
> Expressions
;
120 InputFunctionCoverageData(std::string Name
, uint64_t Hash
)
121 : Name(std::move(Name
)), Hash(Hash
) {}
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
)) {}
128 InputFunctionCoverageData(const InputFunctionCoverageData
&) = delete;
129 InputFunctionCoverageData
&
130 operator=(const InputFunctionCoverageData
&) = delete;
131 InputFunctionCoverageData
&operator=(InputFunctionCoverageData
&&) = delete;
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
;
141 InstrProfWriter ProfileWriter
;
142 std::unique_ptr
<IndexedInstrProfReader
> ProfileReader
;
144 std::unique_ptr
<CoverageMapping
> LoadedCoverage
;
146 void SetUp() override
{
147 ProfileWriter
.setOutputSparse(std::get
<0>(GetParam()));
148 UseMultipleReaders
= std::get
<1>(GetParam());
151 unsigned getGlobalFileIndex(StringRef Name
) {
152 auto R
= Files
.find(Name
);
153 if (R
!= Files
.end())
155 unsigned Index
= Files
.size() + 1;
156 Files
.try_emplace(Name
, Index
);
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())
171 unsigned IndexInFunction
= CurrentFunctionFileMapping
.size();
172 CurrentFunctionFileMapping
.insert(
173 std::make_pair(GlobalIndex
, IndexInFunction
));
174 return IndexInFunction
;
177 void startFunction(StringRef FuncName
, uint64_t Hash
) {
178 InputFunctions
.emplace_back(FuncName
.str(), Hash
);
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
);
186 Skipped
? CounterMappingRegion::makeSkipped(FileID
, LS
, CS
, LE
, CE
)
187 : CounterMappingRegion::makeRegion(C
, FileID
, LS
, CS
, LE
, CE
));
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
),
197 void addExpression(CounterExpression CE
) {
198 InputFunctions
.back().Expressions
.push_back(CE
);
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
);
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.
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());
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
;
233 OutputFunctions
[I
].Filenames
.clear();
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());
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
));
253 ArrayRef
<OutputFunctionCoverageData
> Funcs(OutputFunctions
);
254 CoverageReaders
.push_back(
255 std::make_unique
<CoverageMappingReaderMock
>(Funcs
));
257 return CoverageMapping::load(CoverageReaders
, *ProfileReader
);
260 Error
loadCoverageMapping(bool EmitFilenames
= true) {
262 writeAndReadCoverageRegions(EmitFilenames
);
263 auto CoverageOrErr
= readOutputFunctions();
265 return CoverageOrErr
.takeError();
266 LoadedCoverage
= std::move(CoverageOrErr
.get());
267 return Error::success();
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);
279 writeAndReadCoverageRegions();
280 ASSERT_EQ(1u, InputFunctions
.size());
281 ASSERT_EQ(1u, OutputFunctions
.size());
282 InputFunctionCoverageData
&Input
= InputFunctions
.back();
283 OutputFunctionCoverageData
&Output
= OutputFunctions
.back();
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
);
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
);
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);
306 writeAndReadCoverageRegions();
307 ASSERT_EQ(1u, OutputFunctions
.size());
308 OutputFunctionCoverageData
&Output
= OutputFunctions
.back();
310 ASSERT_EQ(N
, Output
.Regions
.size());
311 ASSERT_EQ(N
, Output
.Filenames
.size());
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
]);
321 static const auto Err
= [](Error E
) { FAIL(); };
323 TEST_P(CoverageMappingTest
, load_coverage_for_more_than_two_files
) {
324 ProfileWriter
.addRecord({"func", 0x1234, {0}}, Err
);
326 const char *FileNames
[] = {"bar", "baz", "foo"};
327 static const unsigned N
= std::size(FileNames
);
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);
335 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
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
);
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"));
352 TEST_P(CoverageMappingTest
, load_coverage_for_several_functions
) {
353 ProfileWriter
.addRecord({"func1", 0x1234, {10}}, Err
);
354 ProfileWriter
.addRecord({"func2", 0x2345, {20}}, Err
);
356 startFunction("func1", 0x1234);
357 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
359 startFunction("func2", 0x2345);
360 addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
362 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
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]);
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]);
381 TEST_P(CoverageMappingTest
, create_combined_regions
) {
382 ProfileWriter
.addRecord({"func1", 0x1234, {1, 2, 3}}, Err
);
383 startFunction("func1", 0x1234);
385 // Given regions which start at the same location, emit a segment for the
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);
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());
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]);
402 TEST_P(CoverageMappingTest
, skipped_segments_have_no_count
) {
403 ProfileWriter
.addRecord({"func1", 0x1234, {1}}, Err
);
404 startFunction("func1", 0x1234);
406 addCMR(Counter::getCounter(0), "file1", 1, 1, 5, 5);
407 addCMR(Counter::getCounter(0), "file1", 5, 1, 5, 5, /*Skipped=*/true);
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());
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]);
421 TEST_P(CoverageMappingTest
, multiple_regions_end_after_parent_ends
) {
422 ProfileWriter
.addRecord({"func1", 0x1234, {1, 0}}, Err
);
423 startFunction("func1", 0x1234);
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
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());
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
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
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]);
482 TEST_P(CoverageMappingTest
, multiple_completed_segments_at_same_loc
) {
483 ProfileWriter
.addRecord({"func1", 0x1234, {0, 1, 2}}, Err
);
484 startFunction("func1", 0x1234);
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);
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());
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]);
510 TEST_P(CoverageMappingTest
, dont_emit_redundant_segments
) {
511 ProfileWriter
.addRecord({"func1", 0x1234, {1, 1}}, Err
);
512 startFunction("func1", 0x1234);
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);
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());
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]);
534 TEST_P(CoverageMappingTest
, dont_emit_closing_segment_at_new_region_start
) {
535 ProfileWriter
.addRecord({"func1", 0x1234, {1}}, Err
);
536 startFunction("func1", 0x1234);
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);
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());
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]);
560 TEST_P(CoverageMappingTest
, handle_consecutive_regions_with_zero_length
) {
561 ProfileWriter
.addRecord({"func1", 0x1234, {1, 2}}, Err
);
562 startFunction("func1", 0x1234);
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);
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());
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.
582 TEST_P(CoverageMappingTest
, handle_sandwiched_zero_length_region
) {
583 ProfileWriter
.addRecord({"func1", 0x1234, {2, 1}}, Err
);
584 startFunction("func1", 0x1234);
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);
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());
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]);
615 TEST_P(CoverageMappingTest
, handle_last_completed_region
) {
616 ProfileWriter
.addRecord({"func1", 0x1234, {1, 2, 3, 4}}, Err
);
617 startFunction("func1", 0x1234);
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);
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());
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]);
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);
648 writeAndReadCoverageRegions();
649 ASSERT_EQ(1u, OutputFunctions
.size());
650 OutputFunctionCoverageData
&Output
= OutputFunctions
.back();
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
);
657 TEST_P(CoverageMappingTest
, basic_coverage_iteration
) {
658 ProfileWriter
.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err
);
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());
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]);
679 TEST_P(CoverageMappingTest
, test_line_coverage_iterator
) {
680 ProfileWriter
.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err
);
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());
689 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
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());
699 ASSERT_EQ(11U, Line
);
701 // Check that operator->() works / compiles.
702 ASSERT_EQ(1U, LineCoverageIterator(Data
)->getLine());
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());
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]);
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());
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]);
731 TEST_P(CoverageMappingTest
, combine_regions
) {
732 ProfileWriter
.addRecord({"func", 0x1234, {10, 20, 30}}, Err
);
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());
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]);
749 TEST_P(CoverageMappingTest
, restore_combined_counter_after_nested_region
) {
750 ProfileWriter
.addRecord({"func", 0x1234, {10, 20, 40}}, Err
);
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());
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]);
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
);
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());
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]);
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
);
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);
800 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
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]);
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
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)));
821 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
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());
828 ASSERT_EQ(1U, Names
.size());
831 TEST_P(CoverageMappingTest
, strip_filename_prefix
) {
832 ProfileWriter
.addRecord({"file1:func", 0x1234, {0}}, Err
);
834 startFunction("file1:func", 0x1234);
835 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
836 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
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]);
845 TEST_P(CoverageMappingTest
, strip_unknown_filename_prefix
) {
846 ProfileWriter
.addRecord({"<unknown>:func", 0x1234, {0}}, Err
);
848 startFunction("<unknown>:func", 0x1234);
849 addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
850 EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
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]);
859 TEST_P(CoverageMappingTest
, dont_detect_false_instantiations
) {
860 ProfileWriter
.addRecord({"foo", 0x1234, {10}}, Err
);
861 ProfileWriter
.addRecord({"bar", 0x2345, {20}}, Err
);
863 startFunction("foo", 0x1234);
864 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
865 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
867 startFunction("bar", 0x2345);
868 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
869 addExpansionCMR("main", "expanded", 9, 1, 9, 5);
871 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
873 std::vector
<InstantiationGroup
> InstantiationGroups
=
874 LoadedCoverage
->getInstantiationGroups("expanded");
875 ASSERT_TRUE(InstantiationGroups
.empty());
878 TEST_P(CoverageMappingTest
, load_coverage_for_expanded_file
) {
879 ProfileWriter
.addRecord({"func", 0x1234, {10}}, Err
);
881 startFunction("func", 0x1234);
882 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
883 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
885 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
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]);
894 TEST_P(CoverageMappingTest
, skip_duplicate_function_record
) {
895 ProfileWriter
.addRecord({"func", 0x1234, {1}}, Err
);
897 // This record should be loaded.
898 startFunction("func", 0x1234);
899 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
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);
906 // This record should be skipped.
907 startFunction("func", 0x1234);
908 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
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);
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);
920 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
922 auto Funcs
= LoadedCoverage
->getCoveredFunctions();
923 unsigned NumFuncs
= std::distance(Funcs
.begin(), Funcs
.end());
924 ASSERT_EQ(3U, NumFuncs
);
927 INSTANTIATE_TEST_SUITE_P(ParameterizedCovMapTest
, CoverageMappingTest
,
928 ::testing::Combine(::testing::Bool(),
931 TEST(CoverageMappingTest
, filename_roundtrip
) {
932 std::vector
<std::string
> Paths({"dir", "a", "b", "c", "d", "e"});
934 for (bool Compress
: {false, true}) {
935 std::string EncodedFilenames
;
937 raw_string_ostream
OS(EncodedFilenames
);
938 CoverageFilenamesSectionWriter
Writer(Paths
);
939 Writer
.write(OS
, Compress
);
942 std::vector
<std::string
> ReadFilenames
;
943 RawCoverageFilenamesReader
Reader(EncodedFilenames
, ReadFilenames
);
944 EXPECT_THAT_ERROR(Reader
.read(CovMapVersion::CurrentVersion
), Succeeded());
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
);
955 TEST(CoverageMappingTest
, filename_compilation_dir
) {
956 std::vector
<std::string
> Paths({"dir", "a", "b", "c", "d", "e"});
958 for (bool Compress
: {false, true}) {
959 std::string EncodedFilenames
;
961 raw_string_ostream
OS(EncodedFilenames
);
962 CoverageFilenamesSectionWriter
Writer(Paths
);
963 Writer
.write(OS
, Compress
);
966 StringRef CompilationDir
= "out";
967 std::vector
<std::string
> ReadFilenames
;
968 RawCoverageFilenamesReader
Reader(EncodedFilenames
, ReadFilenames
,
970 EXPECT_THAT_ERROR(Reader
.read(CovMapVersion::CurrentVersion
), Succeeded());
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
);
981 } // end anonymous namespace