1 //===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
11 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
12 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
13 #include "llvm/ProfileData/InstrProfReader.h"
14 #include "llvm/ProfileData/InstrProfWriter.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/Testing/Support/Error.h"
17 #include "llvm/Testing/Support/SupportHelpers.h"
18 #include "gtest/gtest.h"
24 using namespace coverage
;
26 LLVM_NODISCARD
static ::testing::AssertionResult
27 ErrorEquals(coveragemap_error Expected
, Error E
) {
28 coveragemap_error Found
;
30 handleAllErrors(std::move(E
), [&](const CoverageMapError
&CME
) {
32 FoundMsg
= CME
.message();
34 if (Expected
== Found
)
35 return ::testing::AssertionSuccess();
36 return ::testing::AssertionFailure() << "error: " << FoundMsg
<< "\n";
41 void PrintTo(const Counter
&C
, ::std::ostream
*os
) {
44 else if (C
.isExpression())
45 *os
<< "Expression " << C
.getExpressionID();
47 *os
<< "Counter " << C
.getCounterID();
50 void PrintTo(const CoverageSegment
&S
, ::std::ostream
*os
) {
51 *os
<< "CoverageSegment(" << S
.Line
<< ", " << S
.Col
<< ", ";
53 *os
<< S
.Count
<< ", ";
54 *os
<< (S
.IsRegionEntry
? "true" : "false") << ")";
61 struct OutputFunctionCoverageData
{
64 std::vector
<StringRef
> Filenames
;
65 std::vector
<CounterMappingRegion
> Regions
;
67 OutputFunctionCoverageData() : Hash(0) {}
69 OutputFunctionCoverageData(OutputFunctionCoverageData
&&OFCD
)
70 : Name(OFCD
.Name
), Hash(OFCD
.Hash
), Filenames(std::move(OFCD
.Filenames
)),
71 Regions(std::move(OFCD
.Regions
)) {}
73 OutputFunctionCoverageData(const OutputFunctionCoverageData
&) = delete;
74 OutputFunctionCoverageData
&
75 operator=(const OutputFunctionCoverageData
&) = delete;
76 OutputFunctionCoverageData
&operator=(OutputFunctionCoverageData
&&) = delete;
78 void fillCoverageMappingRecord(CoverageMappingRecord
&Record
) const {
79 Record
.FunctionName
= Name
;
80 Record
.FunctionHash
= Hash
;
81 Record
.Filenames
= Filenames
;
82 Record
.Expressions
= {};
83 Record
.MappingRegions
= Regions
;
87 struct CoverageMappingReaderMock
: CoverageMappingReader
{
88 ArrayRef
<OutputFunctionCoverageData
> Functions
;
90 CoverageMappingReaderMock(ArrayRef
<OutputFunctionCoverageData
> Functions
)
91 : Functions(Functions
) {}
93 Error
readNextRecord(CoverageMappingRecord
&Record
) override
{
94 if (Functions
.empty())
95 return make_error
<CoverageMapError
>(coveragemap_error::eof
);
97 Functions
.front().fillCoverageMappingRecord(Record
);
98 Functions
= Functions
.slice(1);
100 return Error::success();
104 struct InputFunctionCoverageData
{
105 // Maps the global file index from CoverageMappingTest.Files
106 // to the index of that file within this function. We can't just use
107 // global file indexes here because local indexes have to be dense.
108 // This map is used during serialization to create the virtual file mapping
109 // (from local fileId to global Index) in the head of the per-function
110 // coverage mapping data.
111 SmallDenseMap
<unsigned, unsigned> ReverseVirtualFileMapping
;
114 std::vector
<CounterMappingRegion
> Regions
;
116 InputFunctionCoverageData(std::string Name
, uint64_t Hash
)
117 : Name(std::move(Name
)), Hash(Hash
) {}
119 InputFunctionCoverageData(InputFunctionCoverageData
&&IFCD
)
120 : ReverseVirtualFileMapping(std::move(IFCD
.ReverseVirtualFileMapping
)),
121 Name(std::move(IFCD
.Name
)), Hash(IFCD
.Hash
),
122 Regions(std::move(IFCD
.Regions
)) {}
124 InputFunctionCoverageData(const InputFunctionCoverageData
&) = delete;
125 InputFunctionCoverageData
&
126 operator=(const InputFunctionCoverageData
&) = delete;
127 InputFunctionCoverageData
&operator=(InputFunctionCoverageData
&&) = delete;
130 struct CoverageMappingTest
: ::testing::TestWithParam
<std::pair
<bool, bool>> {
131 bool UseMultipleReaders
;
132 StringMap
<unsigned> Files
;
133 std::vector
<InputFunctionCoverageData
> InputFunctions
;
134 std::vector
<OutputFunctionCoverageData
> OutputFunctions
;
136 InstrProfWriter ProfileWriter
;
137 std::unique_ptr
<IndexedInstrProfReader
> ProfileReader
;
139 std::unique_ptr
<CoverageMapping
> LoadedCoverage
;
141 void SetUp() override
{
142 ProfileWriter
.setOutputSparse(GetParam().first
);
143 UseMultipleReaders
= GetParam().second
;
146 unsigned getGlobalFileIndex(StringRef Name
) {
147 auto R
= Files
.find(Name
);
148 if (R
!= Files
.end())
150 unsigned Index
= Files
.size();
151 Files
.try_emplace(Name
, Index
);
155 // Return the file index of file 'Name' for the current function.
156 // Add the file into the global map if necessary.
157 // See also InputFunctionCoverageData::ReverseVirtualFileMapping
158 // for additional comments.
159 unsigned getFileIndexForFunction(StringRef Name
) {
160 unsigned GlobalIndex
= getGlobalFileIndex(Name
);
161 auto &CurrentFunctionFileMapping
=
162 InputFunctions
.back().ReverseVirtualFileMapping
;
163 auto R
= CurrentFunctionFileMapping
.find(GlobalIndex
);
164 if (R
!= CurrentFunctionFileMapping
.end())
166 unsigned IndexInFunction
= CurrentFunctionFileMapping
.size();
167 CurrentFunctionFileMapping
.insert(
168 std::make_pair(GlobalIndex
, IndexInFunction
));
169 return IndexInFunction
;
172 void startFunction(StringRef FuncName
, uint64_t Hash
) {
173 InputFunctions
.emplace_back(FuncName
.str(), Hash
);
176 void addCMR(Counter C
, StringRef File
, unsigned LS
, unsigned CS
, unsigned LE
,
177 unsigned CE
, bool Skipped
= false) {
178 auto &Regions
= InputFunctions
.back().Regions
;
179 unsigned FileID
= getFileIndexForFunction(File
);
181 Skipped
? CounterMappingRegion::makeSkipped(FileID
, LS
, CS
, LE
, CE
)
182 : CounterMappingRegion::makeRegion(C
, FileID
, LS
, CS
, LE
, CE
));
185 void addExpansionCMR(StringRef File
, StringRef ExpandedFile
, unsigned LS
,
186 unsigned CS
, unsigned LE
, unsigned CE
) {
187 InputFunctions
.back().Regions
.push_back(CounterMappingRegion::makeExpansion(
188 getFileIndexForFunction(File
), getFileIndexForFunction(ExpandedFile
),
192 std::string
writeCoverageRegions(InputFunctionCoverageData
&Data
) {
193 SmallVector
<unsigned, 8> FileIDs(Data
.ReverseVirtualFileMapping
.size());
194 for (const auto &E
: Data
.ReverseVirtualFileMapping
)
195 FileIDs
[E
.second
] = E
.first
;
196 std::string Coverage
;
197 llvm::raw_string_ostream
OS(Coverage
);
198 CoverageMappingWriter(FileIDs
, None
, Data
.Regions
).write(OS
);
202 void readCoverageRegions(const std::string
&Coverage
,
203 OutputFunctionCoverageData
&Data
) {
204 SmallVector
<StringRef
, 8> Filenames(Files
.size());
205 for (const auto &E
: Files
)
206 Filenames
[E
.getValue()] = E
.getKey();
207 std::vector
<CounterExpression
> Expressions
;
208 RawCoverageMappingReader
Reader(Coverage
, Filenames
, Data
.Filenames
,
209 Expressions
, Data
.Regions
);
210 EXPECT_THAT_ERROR(Reader
.read(), Succeeded());
213 void writeAndReadCoverageRegions(bool EmitFilenames
= true) {
214 OutputFunctions
.resize(InputFunctions
.size());
215 for (unsigned I
= 0; I
< InputFunctions
.size(); ++I
) {
216 std::string Regions
= writeCoverageRegions(InputFunctions
[I
]);
217 readCoverageRegions(Regions
, OutputFunctions
[I
]);
218 OutputFunctions
[I
].Name
= InputFunctions
[I
].Name
;
219 OutputFunctions
[I
].Hash
= InputFunctions
[I
].Hash
;
221 OutputFunctions
[I
].Filenames
.clear();
225 void readProfCounts() {
226 auto Profile
= ProfileWriter
.writeBuffer();
227 auto ReaderOrErr
= IndexedInstrProfReader::create(std::move(Profile
));
228 EXPECT_THAT_ERROR(ReaderOrErr
.takeError(), Succeeded());
229 ProfileReader
= std::move(ReaderOrErr
.get());
232 Expected
<std::unique_ptr
<CoverageMapping
>> readOutputFunctions() {
233 std::vector
<std::unique_ptr
<CoverageMappingReader
>> CoverageReaders
;
234 if (UseMultipleReaders
) {
235 for (const auto &OF
: OutputFunctions
) {
236 ArrayRef
<OutputFunctionCoverageData
> Funcs(OF
);
237 CoverageReaders
.push_back(
238 make_unique
<CoverageMappingReaderMock
>(Funcs
));
241 ArrayRef
<OutputFunctionCoverageData
> Funcs(OutputFunctions
);
242 CoverageReaders
.push_back(
243 make_unique
<CoverageMappingReaderMock
>(Funcs
));
245 return CoverageMapping::load(CoverageReaders
, *ProfileReader
);
248 Error
loadCoverageMapping(bool EmitFilenames
= true) {
250 writeAndReadCoverageRegions(EmitFilenames
);
251 auto CoverageOrErr
= readOutputFunctions();
253 return CoverageOrErr
.takeError();
254 LoadedCoverage
= std::move(CoverageOrErr
.get());
255 return Error::success();
259 TEST_P(CoverageMappingTest
, basic_write_read
) {
260 startFunction("func", 0x1234);
261 addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
262 addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
263 addCMR(Counter::getZero(), "foo", 3, 1, 3, 4);
264 addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
265 addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
267 writeAndReadCoverageRegions();
268 ASSERT_EQ(1u, InputFunctions
.size());
269 ASSERT_EQ(1u, OutputFunctions
.size());
270 InputFunctionCoverageData
&Input
= InputFunctions
.back();
271 OutputFunctionCoverageData
&Output
= OutputFunctions
.back();
273 size_t N
= makeArrayRef(Input
.Regions
).size();
274 ASSERT_EQ(N
, Output
.Regions
.size());
275 for (size_t I
= 0; I
< N
; ++I
) {
276 ASSERT_EQ(Input
.Regions
[I
].Count
, Output
.Regions
[I
].Count
);
277 ASSERT_EQ(Input
.Regions
[I
].FileID
, Output
.Regions
[I
].FileID
);
278 ASSERT_EQ(Input
.Regions
[I
].startLoc(), Output
.Regions
[I
].startLoc());
279 ASSERT_EQ(Input
.Regions
[I
].endLoc(), Output
.Regions
[I
].endLoc());
280 ASSERT_EQ(Input
.Regions
[I
].Kind
, Output
.Regions
[I
].Kind
);
284 TEST_P(CoverageMappingTest
, correct_deserialize_for_more_than_two_files
) {
285 const char *FileNames
[] = {"bar", "baz", "foo"};
286 static const unsigned N
= array_lengthof(FileNames
);
288 startFunction("func", 0x1234);
289 for (unsigned I
= 0; I
< N
; ++I
)
290 // Use LineStart to hold the index of the file name
291 // in order to preserve that information during possible sorting of CMRs.
292 addCMR(Counter::getCounter(0), FileNames
[I
], I
, 1, I
, 1);
294 writeAndReadCoverageRegions();
295 ASSERT_EQ(1u, OutputFunctions
.size());
296 OutputFunctionCoverageData
&Output
= OutputFunctions
.back();
298 ASSERT_EQ(N
, Output
.Regions
.size());
299 ASSERT_EQ(N
, Output
.Filenames
.size());
301 for (unsigned I
= 0; I
< N
; ++I
) {
302 ASSERT_GT(N
, Output
.Regions
[I
].FileID
);
303 ASSERT_GT(N
, Output
.Regions
[I
].LineStart
);
304 EXPECT_EQ(FileNames
[Output
.Regions
[I
].LineStart
],
305 Output
.Filenames
[Output
.Regions
[I
].FileID
]);
309 static const auto Err
= [](Error E
) { FAIL(); };
311 TEST_P(CoverageMappingTest
, load_coverage_for_more_than_two_files
) {
312 ProfileWriter
.addRecord({"func", 0x1234, {0}}, Err
);
314 const char *FileNames
[] = {"bar", "baz", "foo"};
315 static const unsigned N
= array_lengthof(FileNames
);
317 startFunction("func", 0x1234);
318 for (unsigned I
= 0; I
< N
; ++I
)
319 // Use LineStart to hold the index of the file name
320 // in order to preserve that information during possible sorting of CMRs.
321 addCMR(Counter::getCounter(0), FileNames
[I
], I
, 1, I
, 1);
323 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
325 for (unsigned I
= 0; I
< N
; ++I
) {
326 CoverageData Data
= LoadedCoverage
->getCoverageForFile(FileNames
[I
]);
327 ASSERT_TRUE(!Data
.empty());
328 EXPECT_EQ(I
, Data
.begin()->Line
);
332 TEST_P(CoverageMappingTest
, load_coverage_with_bogus_function_name
) {
333 ProfileWriter
.addRecord({"", 0x1234, {10}}, Err
);
334 startFunction("", 0x1234);
335 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
336 EXPECT_TRUE(ErrorEquals(coveragemap_error::malformed
, loadCoverageMapping()));
339 TEST_P(CoverageMappingTest
, load_coverage_for_several_functions
) {
340 ProfileWriter
.addRecord({"func1", 0x1234, {10}}, Err
);
341 ProfileWriter
.addRecord({"func2", 0x2345, {20}}, Err
);
343 startFunction("func1", 0x1234);
344 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
346 startFunction("func2", 0x2345);
347 addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
349 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
351 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
352 EXPECT_EQ(2, std::distance(FunctionRecords
.begin(), FunctionRecords
.end()));
353 for (const auto &FunctionRecord
: FunctionRecords
) {
354 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
355 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
356 ASSERT_EQ(2U, Segments
.size());
357 if (FunctionRecord
.Name
== "func1") {
358 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments
[0]);
359 EXPECT_EQ(CoverageSegment(5, 5, false), Segments
[1]);
361 ASSERT_EQ("func2", FunctionRecord
.Name
);
362 EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments
[0]);
363 EXPECT_EQ(CoverageSegment(6, 6, false), Segments
[1]);
368 TEST_P(CoverageMappingTest
, create_combined_regions
) {
369 ProfileWriter
.addRecord({"func1", 0x1234, {1, 2, 3}}, Err
);
370 startFunction("func1", 0x1234);
372 // Given regions which start at the same location, emit a segment for the
374 addCMR(Counter::getCounter(0), "file1", 1, 1, 2, 2);
375 addCMR(Counter::getCounter(1), "file1", 1, 1, 2, 2);
376 addCMR(Counter::getCounter(2), "file1", 1, 1, 2, 2);
378 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
379 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
380 const auto &FunctionRecord
= *FunctionRecords
.begin();
381 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
382 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
384 ASSERT_EQ(2U, Segments
.size());
385 EXPECT_EQ(CoverageSegment(1, 1, 6, true), Segments
[0]);
386 EXPECT_EQ(CoverageSegment(2, 2, false), Segments
[1]);
389 TEST_P(CoverageMappingTest
, skipped_segments_have_no_count
) {
390 ProfileWriter
.addRecord({"func1", 0x1234, {1}}, Err
);
391 startFunction("func1", 0x1234);
393 addCMR(Counter::getCounter(0), "file1", 1, 1, 5, 5);
394 addCMR(Counter::getCounter(0), "file1", 5, 1, 5, 5, /*Skipped=*/true);
396 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
397 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
398 const auto &FunctionRecord
= *FunctionRecords
.begin();
399 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
400 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
402 ASSERT_EQ(3U, Segments
.size());
403 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
404 EXPECT_EQ(CoverageSegment(5, 1, true), Segments
[1]);
405 EXPECT_EQ(CoverageSegment(5, 5, false), Segments
[2]);
408 TEST_P(CoverageMappingTest
, multiple_regions_end_after_parent_ends
) {
409 ProfileWriter
.addRecord({"func1", 0x1234, {1, 0}}, Err
);
410 startFunction("func1", 0x1234);
421 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); // < F
422 addCMR(Counter::getCounter(0), "file1", 1, 1, 3, 5); // < a
423 addCMR(Counter::getCounter(0), "file1", 3, 5, 5, 4); // < b
424 addCMR(Counter::getCounter(1), "file1", 3, 5, 7, 3); // < c
425 addCMR(Counter::getCounter(1), "file1", 7, 3, 9, 2); // < d
426 addCMR(Counter::getCounter(1), "file1", 7, 7, 9, 7); // < e
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());
434 // Old output (not sorted or unique):
435 // Segment at 1:1 with count 1
436 // Segment at 1:1 with count 1
437 // Segment at 3:5 with count 1
438 // Segment at 3:5 with count 0
439 // Segment at 3:5 with count 1
440 // Segment at 5:4 with count 0
441 // Segment at 7:3 with count 1
442 // Segment at 7:3 with count 0
443 // Segment at 7:7 with count 0
444 // Segment at 9:7 with count 0
445 // Segment at 9:2 with count 1
446 // Top level segment at 9:9
448 // New output (sorted and unique):
449 // Segment at 1:1 (count = 1), RegionEntry
450 // Segment at 3:5 (count = 1), RegionEntry
451 // Segment at 5:4 (count = 0)
452 // Segment at 7:3 (count = 0), RegionEntry
453 // Segment at 7:7 (count = 0), RegionEntry
454 // Segment at 9:2 (count = 0)
455 // Segment at 9:7 (count = 1)
456 // Segment at 9:9 (count = 0), Skipped
458 ASSERT_EQ(8U, Segments
.size());
459 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
460 EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments
[1]);
461 EXPECT_EQ(CoverageSegment(5, 4, 0, false), Segments
[2]);
462 EXPECT_EQ(CoverageSegment(7, 3, 0, true), Segments
[3]);
463 EXPECT_EQ(CoverageSegment(7, 7, 0, true), Segments
[4]);
464 EXPECT_EQ(CoverageSegment(9, 2, 0, false), Segments
[5]);
465 EXPECT_EQ(CoverageSegment(9, 7, 1, false), Segments
[6]);
466 EXPECT_EQ(CoverageSegment(9, 9, false), Segments
[7]);
469 TEST_P(CoverageMappingTest
, multiple_completed_segments_at_same_loc
) {
470 ProfileWriter
.addRecord({"func1", 0x1234, {0, 1, 2}}, Err
);
471 startFunction("func1", 0x1234);
474 addCMR(Counter::getCounter(1), "file1", 2, 1, 18, 2);
475 addCMR(Counter::getCounter(0), "file1", 8, 10, 14, 6);
476 addCMR(Counter::getCounter(0), "file1", 8, 12, 14, 6);
477 addCMR(Counter::getCounter(1), "file1", 9, 1, 14, 6);
478 addCMR(Counter::getCounter(2), "file1", 11, 13, 11, 14);
480 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
481 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
482 const auto &FunctionRecord
= *FunctionRecords
.begin();
483 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
484 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
486 ASSERT_EQ(7U, Segments
.size());
487 EXPECT_EQ(CoverageSegment(2, 1, 1, true), Segments
[0]);
488 EXPECT_EQ(CoverageSegment(8, 10, 0, true), Segments
[1]);
489 EXPECT_EQ(CoverageSegment(8, 12, 0, true), Segments
[2]);
490 EXPECT_EQ(CoverageSegment(9, 1, 1, true), Segments
[3]);
491 EXPECT_EQ(CoverageSegment(11, 13, 2, true), Segments
[4]);
492 // Use count=1 (from 9:1 -> 14:6), not count=0 (from 8:12 -> 14:6).
493 EXPECT_EQ(CoverageSegment(11, 14, 1, false), Segments
[5]);
494 EXPECT_EQ(CoverageSegment(18, 2, false), Segments
[6]);
497 TEST_P(CoverageMappingTest
, dont_emit_redundant_segments
) {
498 ProfileWriter
.addRecord({"func1", 0x1234, {1, 1}}, Err
);
499 startFunction("func1", 0x1234);
501 addCMR(Counter::getCounter(0), "file1", 1, 1, 4, 4);
502 addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
503 addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 6);
505 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
506 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
507 const auto &FunctionRecord
= *FunctionRecords
.begin();
508 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
509 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
511 ASSERT_EQ(5U, Segments
.size());
512 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
513 EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments
[1]);
514 EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments
[2]);
515 EXPECT_EQ(CoverageSegment(4, 4, 1, false), Segments
[3]);
516 // A closing segment starting at 5:5 would be redundant: it would have the
517 // same count as the segment starting at 4:4, and has all the same metadata.
518 EXPECT_EQ(CoverageSegment(6, 6, false), Segments
[4]);
521 TEST_P(CoverageMappingTest
, dont_emit_closing_segment_at_new_region_start
) {
522 ProfileWriter
.addRecord({"func1", 0x1234, {1}}, Err
);
523 startFunction("func1", 0x1234);
525 addCMR(Counter::getCounter(0), "file1", 1, 1, 6, 5);
526 addCMR(Counter::getCounter(0), "file1", 2, 2, 6, 5);
527 addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 5);
528 addCMR(Counter::getCounter(0), "file1", 6, 5, 7, 7);
530 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
531 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
532 const auto &FunctionRecord
= *FunctionRecords
.begin();
533 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
534 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
536 ASSERT_EQ(5U, Segments
.size());
537 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
538 EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments
[1]);
539 EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments
[2]);
540 EXPECT_EQ(CoverageSegment(6, 5, 1, true), Segments
[3]);
541 // The old segment builder would get this wrong by emitting multiple segments
542 // which start at 6:5 (a few of which were skipped segments). We should just
543 // get a segment for the region entry.
544 EXPECT_EQ(CoverageSegment(7, 7, false), Segments
[4]);
547 TEST_P(CoverageMappingTest
, handle_consecutive_regions_with_zero_length
) {
548 ProfileWriter
.addRecord({"func1", 0x1234, {1, 2}}, Err
);
549 startFunction("func1", 0x1234);
551 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
552 addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
553 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
554 addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
555 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
557 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
558 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
559 const auto &FunctionRecord
= *FunctionRecords
.begin();
560 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
561 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
563 ASSERT_EQ(1U, Segments
.size());
564 EXPECT_EQ(CoverageSegment(1, 1, true), Segments
[0]);
565 // We need to get a skipped segment starting at 1:1. In this case there is
566 // also a region entry at 1:1.
569 TEST_P(CoverageMappingTest
, handle_sandwiched_zero_length_region
) {
570 ProfileWriter
.addRecord({"func1", 0x1234, {2, 1}}, Err
);
571 startFunction("func1", 0x1234);
573 addCMR(Counter::getCounter(0), "file1", 1, 5, 4, 4);
574 addCMR(Counter::getCounter(1), "file1", 1, 9, 1, 50);
575 addCMR(Counter::getCounter(1), "file1", 2, 7, 2, 34);
576 addCMR(Counter::getCounter(1), "file1", 3, 5, 3, 21);
577 addCMR(Counter::getCounter(1), "file1", 3, 21, 3, 21);
578 addCMR(Counter::getCounter(1), "file1", 4, 12, 4, 17);
580 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
581 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
582 const auto &FunctionRecord
= *FunctionRecords
.begin();
583 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
584 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
586 ASSERT_EQ(10U, Segments
.size());
587 EXPECT_EQ(CoverageSegment(1, 5, 2, true), Segments
[0]);
588 EXPECT_EQ(CoverageSegment(1, 9, 1, true), Segments
[1]);
589 EXPECT_EQ(CoverageSegment(1, 50, 2, false), Segments
[2]);
590 EXPECT_EQ(CoverageSegment(2, 7, 1, true), Segments
[3]);
591 EXPECT_EQ(CoverageSegment(2, 34, 2, false), Segments
[4]);
592 EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments
[5]);
593 EXPECT_EQ(CoverageSegment(3, 21, 2, true), Segments
[6]);
594 // Handle the zero-length region by creating a segment with its predecessor's
595 // count (i.e the count from 1:5 -> 4:4).
596 EXPECT_EQ(CoverageSegment(4, 4, false), Segments
[7]);
597 // The area between 4:4 and 4:12 is skipped.
598 EXPECT_EQ(CoverageSegment(4, 12, 1, true), Segments
[8]);
599 EXPECT_EQ(CoverageSegment(4, 17, false), Segments
[9]);
602 TEST_P(CoverageMappingTest
, handle_last_completed_region
) {
603 ProfileWriter
.addRecord({"func1", 0x1234, {1, 2, 3, 4}}, Err
);
604 startFunction("func1", 0x1234);
606 addCMR(Counter::getCounter(0), "file1", 1, 1, 8, 8);
607 addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
608 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
609 addCMR(Counter::getCounter(3), "file1", 6, 6, 7, 7);
611 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
612 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
613 const auto &FunctionRecord
= *FunctionRecords
.begin();
614 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
615 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
617 ASSERT_EQ(8U, Segments
.size());
618 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
619 EXPECT_EQ(CoverageSegment(2, 2, 2, true), Segments
[1]);
620 EXPECT_EQ(CoverageSegment(3, 3, 3, true), Segments
[2]);
621 EXPECT_EQ(CoverageSegment(4, 4, 2, false), Segments
[3]);
622 EXPECT_EQ(CoverageSegment(5, 5, 1, false), Segments
[4]);
623 EXPECT_EQ(CoverageSegment(6, 6, 4, true), Segments
[5]);
624 EXPECT_EQ(CoverageSegment(7, 7, 1, false), Segments
[6]);
625 EXPECT_EQ(CoverageSegment(8, 8, false), Segments
[7]);
628 TEST_P(CoverageMappingTest
, expansion_gets_first_counter
) {
629 startFunction("func", 0x1234);
630 addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
631 // This starts earlier in "foo", so the expansion should get its counter.
632 addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
633 addExpansionCMR("bar", "foo", 3, 3, 3, 3);
635 writeAndReadCoverageRegions();
636 ASSERT_EQ(1u, OutputFunctions
.size());
637 OutputFunctionCoverageData
&Output
= OutputFunctions
.back();
639 ASSERT_EQ(CounterMappingRegion::ExpansionRegion
, Output
.Regions
[2].Kind
);
640 ASSERT_EQ(Counter::getCounter(2), Output
.Regions
[2].Count
);
641 ASSERT_EQ(3U, Output
.Regions
[2].LineStart
);
644 TEST_P(CoverageMappingTest
, basic_coverage_iteration
) {
645 ProfileWriter
.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err
);
647 startFunction("func", 0x1234);
648 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
649 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
650 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
651 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
652 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
654 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
655 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
656 ASSERT_EQ(7U, Segments
.size());
657 ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments
[0]);
658 ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments
[1]);
659 ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments
[2]);
660 ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments
[3]);
661 ASSERT_EQ(CoverageSegment(9, 9, false), Segments
[4]);
662 ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments
[5]);
663 ASSERT_EQ(CoverageSegment(11, 11, false), Segments
[6]);
666 TEST_P(CoverageMappingTest
, test_line_coverage_iterator
) {
667 ProfileWriter
.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err
);
669 startFunction("func", 0x1234);
670 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
671 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
672 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
673 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
674 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
676 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
679 unsigned LineCounts
[] = {20, 20, 20, 20, 30, 10, 10, 10, 10, 0, 0};
680 for (const auto &LCS
: getLineCoverageStats(Data
)) {
681 ASSERT_EQ(Line
+ 1, LCS
.getLine());
682 errs() << "Line: " << Line
+ 1 << ", count = " << LCS
.getExecutionCount() << "\n";
683 ASSERT_EQ(LineCounts
[Line
], LCS
.getExecutionCount());
686 ASSERT_EQ(11U, Line
);
689 TEST_P(CoverageMappingTest
, uncovered_function
) {
690 startFunction("func", 0x1234);
691 addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
692 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
694 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
695 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
696 ASSERT_EQ(2U, Segments
.size());
697 ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments
[0]);
698 ASSERT_EQ(CoverageSegment(3, 4, false), Segments
[1]);
701 TEST_P(CoverageMappingTest
, uncovered_function_with_mapping
) {
702 startFunction("func", 0x1234);
703 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
704 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
705 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
707 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
708 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
709 ASSERT_EQ(3U, Segments
.size());
710 ASSERT_EQ(CoverageSegment(1, 1, 0, true), Segments
[0]);
711 ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments
[1]);
712 ASSERT_EQ(CoverageSegment(9, 9, false), Segments
[2]);
715 TEST_P(CoverageMappingTest
, combine_regions
) {
716 ProfileWriter
.addRecord({"func", 0x1234, {10, 20, 30}}, Err
);
718 startFunction("func", 0x1234);
719 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
720 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
721 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
722 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
724 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
725 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
726 ASSERT_EQ(4U, Segments
.size());
727 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments
[0]);
728 ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments
[1]);
729 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments
[2]);
730 ASSERT_EQ(CoverageSegment(9, 9, false), Segments
[3]);
733 TEST_P(CoverageMappingTest
, restore_combined_counter_after_nested_region
) {
734 ProfileWriter
.addRecord({"func", 0x1234, {10, 20, 40}}, Err
);
736 startFunction("func", 0x1234);
737 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
738 addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
739 addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
740 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
742 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
743 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
744 ASSERT_EQ(4U, Segments
.size());
745 EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments
[0]);
746 EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments
[1]);
747 EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments
[2]);
748 EXPECT_EQ(CoverageSegment(9, 9, false), Segments
[3]);
751 // If CodeRegions and ExpansionRegions cover the same area,
752 // only counts of CodeRegions should be used.
753 TEST_P(CoverageMappingTest
, dont_combine_expansions
) {
754 ProfileWriter
.addRecord({"func", 0x1234, {10, 20}}, Err
);
755 ProfileWriter
.addRecord({"func", 0x1234, {0, 0}}, Err
);
757 startFunction("func", 0x1234);
758 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
759 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
760 addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
761 addExpansionCMR("file1", "include1", 3, 3, 4, 4);
762 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
764 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
765 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
766 ASSERT_EQ(4U, Segments
.size());
767 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments
[0]);
768 ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments
[1]);
769 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments
[2]);
770 ASSERT_EQ(CoverageSegment(9, 9, false), Segments
[3]);
773 // If an area is covered only by ExpansionRegions, they should be combinated.
774 TEST_P(CoverageMappingTest
, combine_expansions
) {
775 ProfileWriter
.addRecord({"func", 0x1234, {2, 3, 7}}, Err
);
777 startFunction("func", 0x1234);
778 addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
779 addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
780 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
781 addExpansionCMR("file", "include1", 3, 1, 3, 5);
782 addExpansionCMR("file", "include2", 3, 1, 3, 5);
784 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
786 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file");
787 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
788 ASSERT_EQ(4U, Segments
.size());
789 EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments
[0]);
790 EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments
[1]);
791 EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments
[2]);
792 EXPECT_EQ(CoverageSegment(5, 5, false), Segments
[3]);
795 TEST_P(CoverageMappingTest
, strip_filename_prefix
) {
796 ProfileWriter
.addRecord({"file1:func", 0x1234, {0}}, Err
);
798 startFunction("file1:func", 0x1234);
799 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
800 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
802 std::vector
<std::string
> Names
;
803 for (const auto &Func
: LoadedCoverage
->getCoveredFunctions())
804 Names
.push_back(Func
.Name
);
805 ASSERT_EQ(1U, Names
.size());
806 ASSERT_EQ("func", Names
[0]);
809 TEST_P(CoverageMappingTest
, strip_unknown_filename_prefix
) {
810 ProfileWriter
.addRecord({"<unknown>:func", 0x1234, {0}}, Err
);
812 startFunction("<unknown>:func", 0x1234);
813 addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
814 EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
816 std::vector
<std::string
> Names
;
817 for (const auto &Func
: LoadedCoverage
->getCoveredFunctions())
818 Names
.push_back(Func
.Name
);
819 ASSERT_EQ(1U, Names
.size());
820 ASSERT_EQ("func", Names
[0]);
823 TEST_P(CoverageMappingTest
, dont_detect_false_instantiations
) {
824 ProfileWriter
.addRecord({"foo", 0x1234, {10}}, Err
);
825 ProfileWriter
.addRecord({"bar", 0x2345, {20}}, Err
);
827 startFunction("foo", 0x1234);
828 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
829 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
831 startFunction("bar", 0x2345);
832 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
833 addExpansionCMR("main", "expanded", 9, 1, 9, 5);
835 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
837 std::vector
<InstantiationGroup
> InstantiationGroups
=
838 LoadedCoverage
->getInstantiationGroups("expanded");
839 for (const auto &Group
: InstantiationGroups
)
840 ASSERT_EQ(Group
.size(), 1U);
843 TEST_P(CoverageMappingTest
, load_coverage_for_expanded_file
) {
844 ProfileWriter
.addRecord({"func", 0x1234, {10}}, Err
);
846 startFunction("func", 0x1234);
847 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
848 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
850 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
852 CoverageData Data
= LoadedCoverage
->getCoverageForFile("expanded");
853 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
854 ASSERT_EQ(2U, Segments
.size());
855 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments
[0]);
856 EXPECT_EQ(CoverageSegment(1, 10, false), Segments
[1]);
859 TEST_P(CoverageMappingTest
, skip_duplicate_function_record
) {
860 ProfileWriter
.addRecord({"func", 0x1234, {1}}, Err
);
862 // This record should be loaded.
863 startFunction("func", 0x1234);
864 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
866 // This record should be loaded.
867 startFunction("func", 0x1234);
868 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
869 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
871 // This record should be skipped.
872 startFunction("func", 0x1234);
873 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
875 // This record should be loaded.
876 startFunction("func", 0x1234);
877 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
878 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
880 // This record should be skipped.
881 startFunction("func", 0x1234);
882 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
883 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
885 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
887 auto Funcs
= LoadedCoverage
->getCoveredFunctions();
888 unsigned NumFuncs
= std::distance(Funcs
.begin(), Funcs
.end());
889 ASSERT_EQ(3U, NumFuncs
);
892 // FIXME: Use ::testing::Combine() when llvm updates its copy of googletest.
893 INSTANTIATE_TEST_CASE_P(ParameterizedCovMapTest
, CoverageMappingTest
,
894 ::testing::Values(std::pair
<bool, bool>({false, false}),
895 std::pair
<bool, bool>({false, true}),
896 std::pair
<bool, bool>({true, false}),
897 std::pair
<bool, bool>({true, true})),);
899 } // end anonymous namespace