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"
24 using namespace coverage
;
26 [[nodiscard
]] static ::testing::AssertionResult
27 ErrorEquals(Error E
, coveragemap_error Expected_Err
,
28 const std::string
&Expected_Msg
= std::string()) {
29 coveragemap_error Found
;
32 handleAllErrors(std::move(E
), [&](const CoverageMapError
&CME
) {
34 Msg
= CME
.getMessage();
35 FoundMsg
= CME
.message();
37 if (Expected_Err
== Found
&& Msg
== Expected_Msg
)
38 return ::testing::AssertionSuccess();
39 return ::testing::AssertionFailure() << "error: " << FoundMsg
<< "\n";
44 void PrintTo(const Counter
&C
, ::std::ostream
*os
) {
47 else if (C
.isExpression())
48 *os
<< "Expression " << C
.getExpressionID();
50 *os
<< "Counter " << C
.getCounterID();
53 void PrintTo(const CoverageSegment
&S
, ::std::ostream
*os
) {
54 *os
<< "CoverageSegment(" << S
.Line
<< ", " << S
.Col
<< ", ";
56 *os
<< S
.Count
<< ", ";
57 *os
<< (S
.IsRegionEntry
? "true" : "false") << ")";
64 struct OutputFunctionCoverageData
{
67 std::vector
<StringRef
> Filenames
;
68 std::vector
<CounterMappingRegion
> Regions
;
69 std::vector
<CounterExpression
> Expressions
;
71 OutputFunctionCoverageData() : Hash(0) {}
73 OutputFunctionCoverageData(OutputFunctionCoverageData
&&OFCD
)
74 : Name(OFCD
.Name
), Hash(OFCD
.Hash
), Filenames(std::move(OFCD
.Filenames
)),
75 Regions(std::move(OFCD
.Regions
)) {}
77 OutputFunctionCoverageData(const OutputFunctionCoverageData
&) = delete;
78 OutputFunctionCoverageData
&
79 operator=(const OutputFunctionCoverageData
&) = delete;
80 OutputFunctionCoverageData
&operator=(OutputFunctionCoverageData
&&) = delete;
82 void fillCoverageMappingRecord(CoverageMappingRecord
&Record
) const {
83 Record
.FunctionName
= Name
;
84 Record
.FunctionHash
= Hash
;
85 Record
.Filenames
= Filenames
;
86 Record
.Expressions
= Expressions
;
87 Record
.MappingRegions
= Regions
;
91 struct CoverageMappingReaderMock
: CoverageMappingReader
{
92 ArrayRef
<OutputFunctionCoverageData
> Functions
;
94 CoverageMappingReaderMock(ArrayRef
<OutputFunctionCoverageData
> Functions
)
95 : Functions(Functions
) {}
97 Error
readNextRecord(CoverageMappingRecord
&Record
) override
{
98 if (Functions
.empty())
99 return make_error
<CoverageMapError
>(coveragemap_error::eof
);
101 Functions
.front().fillCoverageMappingRecord(Record
);
102 Functions
= Functions
.slice(1);
104 return Error::success();
108 struct InputFunctionCoverageData
{
109 // Maps the global file index from CoverageMappingTest.Files
110 // to the index of that file within this function. We can't just use
111 // global file indexes here because local indexes have to be dense.
112 // This map is used during serialization to create the virtual file mapping
113 // (from local fileId to global Index) in the head of the per-function
114 // coverage mapping data.
115 SmallDenseMap
<unsigned, unsigned> ReverseVirtualFileMapping
;
118 std::vector
<CounterMappingRegion
> Regions
;
119 std::vector
<CounterExpression
> Expressions
;
121 InputFunctionCoverageData(std::string Name
, uint64_t Hash
)
122 : Name(std::move(Name
)), Hash(Hash
) {}
124 InputFunctionCoverageData(InputFunctionCoverageData
&&IFCD
)
125 : ReverseVirtualFileMapping(std::move(IFCD
.ReverseVirtualFileMapping
)),
126 Name(std::move(IFCD
.Name
)), Hash(IFCD
.Hash
),
127 Regions(std::move(IFCD
.Regions
)) {}
129 InputFunctionCoverageData(const InputFunctionCoverageData
&) = delete;
130 InputFunctionCoverageData
&
131 operator=(const InputFunctionCoverageData
&) = delete;
132 InputFunctionCoverageData
&operator=(InputFunctionCoverageData
&&) = delete;
135 struct CoverageMappingTest
: ::testing::TestWithParam
<std::tuple
<bool, bool>> {
136 bool UseMultipleReaders
;
137 StringMap
<unsigned> Files
;
138 std::vector
<std::string
> Filenames
;
139 std::vector
<InputFunctionCoverageData
> InputFunctions
;
140 std::vector
<OutputFunctionCoverageData
> OutputFunctions
;
142 InstrProfWriter ProfileWriter
;
143 std::unique_ptr
<IndexedInstrProfReader
> ProfileReader
;
145 std::unique_ptr
<CoverageMapping
> LoadedCoverage
;
147 void SetUp() override
{
148 ProfileWriter
.setOutputSparse(std::get
<0>(GetParam()));
149 UseMultipleReaders
= std::get
<1>(GetParam());
152 unsigned getGlobalFileIndex(StringRef Name
) {
153 auto R
= Files
.find(Name
);
154 if (R
!= Files
.end())
156 unsigned Index
= Files
.size() + 1;
157 Files
.try_emplace(Name
, Index
);
161 // Return the file index of file 'Name' for the current function.
162 // Add the file into the global map if necessary.
163 // See also InputFunctionCoverageData::ReverseVirtualFileMapping
164 // for additional comments.
165 unsigned getFileIndexForFunction(StringRef Name
) {
166 unsigned GlobalIndex
= getGlobalFileIndex(Name
);
167 auto &CurrentFunctionFileMapping
=
168 InputFunctions
.back().ReverseVirtualFileMapping
;
169 auto R
= CurrentFunctionFileMapping
.find(GlobalIndex
);
170 if (R
!= CurrentFunctionFileMapping
.end())
172 unsigned IndexInFunction
= CurrentFunctionFileMapping
.size();
173 CurrentFunctionFileMapping
.insert(
174 std::make_pair(GlobalIndex
, IndexInFunction
));
175 return IndexInFunction
;
178 void startFunction(StringRef FuncName
, uint64_t Hash
) {
179 InputFunctions
.emplace_back(FuncName
.str(), Hash
);
182 void addCMR(Counter C
, StringRef File
, unsigned LS
, unsigned CS
, unsigned LE
,
183 unsigned CE
, bool Skipped
= false) {
184 auto &Regions
= InputFunctions
.back().Regions
;
185 unsigned FileID
= getFileIndexForFunction(File
);
187 Skipped
? CounterMappingRegion::makeSkipped(FileID
, LS
, CS
, LE
, CE
)
188 : CounterMappingRegion::makeRegion(C
, FileID
, LS
, CS
, LE
, CE
));
191 void addSkipped(StringRef File
, unsigned LS
, unsigned CS
, unsigned LE
,
193 addCMR(Counter::getZero(), File
, LS
, CS
, LE
, CE
, true);
196 void addMCDCDecisionCMR(unsigned Mask
, uint16_t NC
, StringRef File
,
197 unsigned LS
, unsigned CS
, unsigned LE
, unsigned CE
) {
198 auto &Regions
= InputFunctions
.back().Regions
;
199 unsigned FileID
= getFileIndexForFunction(File
);
200 Regions
.push_back(CounterMappingRegion::makeDecisionRegion(
201 mcdc::DecisionParameters
{Mask
, NC
}, FileID
, LS
, CS
, LE
, CE
));
204 void addMCDCBranchCMR(Counter C1
, Counter C2
, mcdc::ConditionID ID
,
205 mcdc::ConditionIDs Conds
, StringRef File
, unsigned LS
,
206 unsigned CS
, unsigned LE
, unsigned CE
) {
207 auto &Regions
= InputFunctions
.back().Regions
;
208 unsigned FileID
= getFileIndexForFunction(File
);
209 Regions
.push_back(CounterMappingRegion::makeBranchRegion(
210 C1
, C2
, FileID
, LS
, CS
, LE
, CE
, mcdc::BranchParameters
{ID
, Conds
}));
213 void addExpansionCMR(StringRef File
, StringRef ExpandedFile
, unsigned LS
,
214 unsigned CS
, unsigned LE
, unsigned CE
) {
215 InputFunctions
.back().Regions
.push_back(CounterMappingRegion::makeExpansion(
216 getFileIndexForFunction(File
), getFileIndexForFunction(ExpandedFile
),
220 void addExpression(CounterExpression CE
) {
221 InputFunctions
.back().Expressions
.push_back(CE
);
224 std::string
writeCoverageRegions(InputFunctionCoverageData
&Data
) {
225 SmallVector
<unsigned, 8> FileIDs(Data
.ReverseVirtualFileMapping
.size());
226 for (const auto &E
: Data
.ReverseVirtualFileMapping
)
227 FileIDs
[E
.second
] = E
.first
;
228 std::string Coverage
;
229 llvm::raw_string_ostream
OS(Coverage
);
230 CoverageMappingWriter(FileIDs
, Data
.Expressions
, Data
.Regions
).write(OS
);
234 void readCoverageRegions(const std::string
&Coverage
,
235 OutputFunctionCoverageData
&Data
) {
236 // We will re-use the StringRef in duplicate tests, clear it to avoid
237 // clobber previous ones.
239 Filenames
.resize(Files
.size() + 1);
240 for (const auto &E
: Files
)
241 Filenames
[E
.getValue()] = E
.getKey().str();
242 ArrayRef
<std::string
> FilenameRefs
= llvm::ArrayRef(Filenames
);
243 RawCoverageMappingReader
Reader(Coverage
, FilenameRefs
, Data
.Filenames
,
244 Data
.Expressions
, Data
.Regions
);
245 EXPECT_THAT_ERROR(Reader
.read(), Succeeded());
248 void writeAndReadCoverageRegions(bool EmitFilenames
= true) {
249 OutputFunctions
.resize(InputFunctions
.size());
250 for (unsigned I
= 0; I
< InputFunctions
.size(); ++I
) {
251 std::string Regions
= writeCoverageRegions(InputFunctions
[I
]);
252 readCoverageRegions(Regions
, OutputFunctions
[I
]);
253 OutputFunctions
[I
].Name
= InputFunctions
[I
].Name
;
254 OutputFunctions
[I
].Hash
= InputFunctions
[I
].Hash
;
256 OutputFunctions
[I
].Filenames
.clear();
260 void readProfCounts() {
261 auto Profile
= ProfileWriter
.writeBuffer();
262 auto ReaderOrErr
= IndexedInstrProfReader::create(std::move(Profile
));
263 EXPECT_THAT_ERROR(ReaderOrErr
.takeError(), Succeeded());
264 ProfileReader
= std::move(ReaderOrErr
.get());
267 Expected
<std::unique_ptr
<CoverageMapping
>> readOutputFunctions() {
268 std::vector
<std::unique_ptr
<CoverageMappingReader
>> CoverageReaders
;
269 if (UseMultipleReaders
) {
270 for (const auto &OF
: OutputFunctions
) {
271 ArrayRef
<OutputFunctionCoverageData
> Funcs(OF
);
272 CoverageReaders
.push_back(
273 std::make_unique
<CoverageMappingReaderMock
>(Funcs
));
276 ArrayRef
<OutputFunctionCoverageData
> Funcs(OutputFunctions
);
277 CoverageReaders
.push_back(
278 std::make_unique
<CoverageMappingReaderMock
>(Funcs
));
280 return CoverageMapping::load(CoverageReaders
, *ProfileReader
);
283 Error
loadCoverageMapping(bool EmitFilenames
= true) {
285 writeAndReadCoverageRegions(EmitFilenames
);
286 auto CoverageOrErr
= readOutputFunctions();
288 return CoverageOrErr
.takeError();
289 LoadedCoverage
= std::move(CoverageOrErr
.get());
290 return Error::success();
294 TEST_P(CoverageMappingTest
, basic_write_read
) {
295 startFunction("func", 0x1234);
296 addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
297 addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
298 addCMR(Counter::getZero(), "foo", 3, 1, 3, 4);
299 addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
300 addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
302 writeAndReadCoverageRegions();
303 ASSERT_EQ(1u, InputFunctions
.size());
304 ASSERT_EQ(1u, OutputFunctions
.size());
305 InputFunctionCoverageData
&Input
= InputFunctions
.back();
306 OutputFunctionCoverageData
&Output
= OutputFunctions
.back();
308 size_t N
= ArrayRef(Input
.Regions
).size();
309 ASSERT_EQ(N
, Output
.Regions
.size());
310 for (size_t I
= 0; I
< N
; ++I
) {
311 ASSERT_EQ(Input
.Regions
[I
].Count
, Output
.Regions
[I
].Count
);
312 ASSERT_EQ(Input
.Regions
[I
].FileID
, Output
.Regions
[I
].FileID
);
313 ASSERT_EQ(Input
.Regions
[I
].startLoc(), Output
.Regions
[I
].startLoc());
314 ASSERT_EQ(Input
.Regions
[I
].endLoc(), Output
.Regions
[I
].endLoc());
315 ASSERT_EQ(Input
.Regions
[I
].Kind
, Output
.Regions
[I
].Kind
);
319 TEST_P(CoverageMappingTest
, correct_deserialize_for_more_than_two_files
) {
320 const char *FileNames
[] = {"bar", "baz", "foo"};
321 static const unsigned N
= std::size(FileNames
);
323 startFunction("func", 0x1234);
324 for (unsigned I
= 0; I
< N
; ++I
)
325 // Use LineStart to hold the index of the file name
326 // in order to preserve that information during possible sorting of CMRs.
327 addCMR(Counter::getCounter(0), FileNames
[I
], I
, 1, I
, 1);
329 writeAndReadCoverageRegions();
330 ASSERT_EQ(1u, OutputFunctions
.size());
331 OutputFunctionCoverageData
&Output
= OutputFunctions
.back();
333 ASSERT_EQ(N
, Output
.Regions
.size());
334 ASSERT_EQ(N
, Output
.Filenames
.size());
336 for (unsigned I
= 0; I
< N
; ++I
) {
337 ASSERT_GT(N
, Output
.Regions
[I
].FileID
);
338 ASSERT_GT(N
, Output
.Regions
[I
].LineStart
);
339 EXPECT_EQ(FileNames
[Output
.Regions
[I
].LineStart
],
340 Output
.Filenames
[Output
.Regions
[I
].FileID
]);
344 static const auto Err
= [](Error E
) { FAIL(); };
346 TEST_P(CoverageMappingTest
, load_coverage_for_more_than_two_files
) {
347 ProfileWriter
.addRecord({"func", 0x1234, {0}}, Err
);
349 const char *FileNames
[] = {"bar", "baz", "foo"};
350 static const unsigned N
= std::size(FileNames
);
352 startFunction("func", 0x1234);
353 for (unsigned I
= 0; I
< N
; ++I
)
354 // Use LineStart to hold the index of the file name
355 // in order to preserve that information during possible sorting of CMRs.
356 addCMR(Counter::getCounter(0), FileNames
[I
], I
, 1, I
, 1);
358 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
360 for (unsigned I
= 0; I
< N
; ++I
) {
361 CoverageData Data
= LoadedCoverage
->getCoverageForFile(FileNames
[I
]);
362 ASSERT_TRUE(!Data
.empty());
363 EXPECT_EQ(I
, Data
.begin()->Line
);
367 TEST_P(CoverageMappingTest
, load_coverage_with_bogus_function_name
) {
368 ProfileWriter
.addRecord({"", 0x1234, {10}}, Err
);
369 startFunction("", 0x1234);
370 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
371 EXPECT_TRUE(ErrorEquals(loadCoverageMapping(), coveragemap_error::malformed
,
372 "record function name is empty"));
375 TEST_P(CoverageMappingTest
, load_coverage_for_several_functions
) {
376 ProfileWriter
.addRecord({"func1", 0x1234, {10}}, Err
);
377 ProfileWriter
.addRecord({"func2", 0x2345, {20}}, Err
);
379 startFunction("func1", 0x1234);
380 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
382 startFunction("func2", 0x2345);
383 addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
385 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
387 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
388 EXPECT_EQ(2, std::distance(FunctionRecords
.begin(), FunctionRecords
.end()));
389 for (const auto &FunctionRecord
: FunctionRecords
) {
390 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
391 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
392 ASSERT_EQ(2U, Segments
.size());
393 if (FunctionRecord
.Name
== "func1") {
394 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments
[0]);
395 EXPECT_EQ(CoverageSegment(5, 5, false), Segments
[1]);
397 ASSERT_EQ("func2", FunctionRecord
.Name
);
398 EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments
[0]);
399 EXPECT_EQ(CoverageSegment(6, 6, false), Segments
[1]);
404 TEST_P(CoverageMappingTest
, create_combined_regions
) {
405 ProfileWriter
.addRecord({"func1", 0x1234, {1, 2, 3}}, Err
);
406 startFunction("func1", 0x1234);
408 // Given regions which start at the same location, emit a segment for the
410 addCMR(Counter::getCounter(0), "file1", 1, 1, 2, 2);
411 addCMR(Counter::getCounter(1), "file1", 1, 1, 2, 2);
412 addCMR(Counter::getCounter(2), "file1", 1, 1, 2, 2);
414 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
415 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
416 const auto &FunctionRecord
= *FunctionRecords
.begin();
417 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
418 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
420 ASSERT_EQ(2U, Segments
.size());
421 EXPECT_EQ(CoverageSegment(1, 1, 6, true), Segments
[0]);
422 EXPECT_EQ(CoverageSegment(2, 2, false), Segments
[1]);
425 TEST_P(CoverageMappingTest
, skipped_segments_have_no_count
) {
426 ProfileWriter
.addRecord({"func1", 0x1234, {1}}, Err
);
427 startFunction("func1", 0x1234);
429 addCMR(Counter::getCounter(0), "file1", 1, 1, 5, 5);
430 addCMR(Counter::getCounter(0), "file1", 5, 1, 5, 5, /*Skipped=*/true);
432 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
433 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
434 const auto &FunctionRecord
= *FunctionRecords
.begin();
435 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
436 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
438 ASSERT_EQ(3U, Segments
.size());
439 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
440 EXPECT_EQ(CoverageSegment(5, 1, true), Segments
[1]);
441 EXPECT_EQ(CoverageSegment(5, 5, false), Segments
[2]);
444 TEST_P(CoverageMappingTest
, multiple_regions_end_after_parent_ends
) {
445 ProfileWriter
.addRecord({"func1", 0x1234, {1, 0}}, Err
);
446 startFunction("func1", 0x1234);
457 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); // < F
458 addCMR(Counter::getCounter(0), "file1", 1, 1, 3, 5); // < a
459 addCMR(Counter::getCounter(0), "file1", 3, 5, 5, 4); // < b
460 addCMR(Counter::getCounter(1), "file1", 3, 5, 7, 3); // < c
461 addCMR(Counter::getCounter(1), "file1", 7, 3, 9, 2); // < d
462 addCMR(Counter::getCounter(1), "file1", 7, 7, 9, 7); // < e
464 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
465 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
466 const auto &FunctionRecord
= *FunctionRecords
.begin();
467 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
468 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
470 // Old output (not sorted or unique):
471 // Segment at 1:1 with count 1
472 // Segment at 1:1 with count 1
473 // Segment at 3:5 with count 1
474 // Segment at 3:5 with count 0
475 // Segment at 3:5 with count 1
476 // Segment at 5:4 with count 0
477 // Segment at 7:3 with count 1
478 // Segment at 7:3 with count 0
479 // Segment at 7:7 with count 0
480 // Segment at 9:7 with count 0
481 // Segment at 9:2 with count 1
482 // Top level segment at 9:9
484 // New output (sorted and unique):
485 // Segment at 1:1 (count = 1), RegionEntry
486 // Segment at 3:5 (count = 1), RegionEntry
487 // Segment at 5:4 (count = 0)
488 // Segment at 7:3 (count = 0), RegionEntry
489 // Segment at 7:7 (count = 0), RegionEntry
490 // Segment at 9:2 (count = 0)
491 // Segment at 9:7 (count = 1)
492 // Segment at 9:9 (count = 0), Skipped
494 ASSERT_EQ(8U, Segments
.size());
495 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
496 EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments
[1]);
497 EXPECT_EQ(CoverageSegment(5, 4, 0, false), Segments
[2]);
498 EXPECT_EQ(CoverageSegment(7, 3, 0, true), Segments
[3]);
499 EXPECT_EQ(CoverageSegment(7, 7, 0, true), Segments
[4]);
500 EXPECT_EQ(CoverageSegment(9, 2, 0, false), Segments
[5]);
501 EXPECT_EQ(CoverageSegment(9, 7, 1, false), Segments
[6]);
502 EXPECT_EQ(CoverageSegment(9, 9, false), Segments
[7]);
505 TEST_P(CoverageMappingTest
, multiple_completed_segments_at_same_loc
) {
506 ProfileWriter
.addRecord({"func1", 0x1234, {0, 1, 2}}, Err
);
507 startFunction("func1", 0x1234);
510 addCMR(Counter::getCounter(1), "file1", 2, 1, 18, 2);
511 addCMR(Counter::getCounter(0), "file1", 8, 10, 14, 6);
512 addCMR(Counter::getCounter(0), "file1", 8, 12, 14, 6);
513 addCMR(Counter::getCounter(1), "file1", 9, 1, 14, 6);
514 addCMR(Counter::getCounter(2), "file1", 11, 13, 11, 14);
516 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
517 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
518 const auto &FunctionRecord
= *FunctionRecords
.begin();
519 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
520 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
522 ASSERT_EQ(7U, Segments
.size());
523 EXPECT_EQ(CoverageSegment(2, 1, 1, true), Segments
[0]);
524 EXPECT_EQ(CoverageSegment(8, 10, 0, true), Segments
[1]);
525 EXPECT_EQ(CoverageSegment(8, 12, 0, true), Segments
[2]);
526 EXPECT_EQ(CoverageSegment(9, 1, 1, true), Segments
[3]);
527 EXPECT_EQ(CoverageSegment(11, 13, 2, true), Segments
[4]);
528 // Use count=1 (from 9:1 -> 14:6), not count=0 (from 8:12 -> 14:6).
529 EXPECT_EQ(CoverageSegment(11, 14, 1, false), Segments
[5]);
530 EXPECT_EQ(CoverageSegment(18, 2, false), Segments
[6]);
533 TEST_P(CoverageMappingTest
, dont_emit_redundant_segments
) {
534 ProfileWriter
.addRecord({"func1", 0x1234, {1, 1}}, Err
);
535 startFunction("func1", 0x1234);
537 addCMR(Counter::getCounter(0), "file1", 1, 1, 4, 4);
538 addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
539 addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 6);
541 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
542 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
543 const auto &FunctionRecord
= *FunctionRecords
.begin();
544 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
545 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
547 ASSERT_EQ(5U, Segments
.size());
548 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
549 EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments
[1]);
550 EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments
[2]);
551 EXPECT_EQ(CoverageSegment(4, 4, 1, false), Segments
[3]);
552 // A closing segment starting at 5:5 would be redundant: it would have the
553 // same count as the segment starting at 4:4, and has all the same metadata.
554 EXPECT_EQ(CoverageSegment(6, 6, false), Segments
[4]);
557 TEST_P(CoverageMappingTest
, dont_emit_closing_segment_at_new_region_start
) {
558 ProfileWriter
.addRecord({"func1", 0x1234, {1}}, Err
);
559 startFunction("func1", 0x1234);
561 addCMR(Counter::getCounter(0), "file1", 1, 1, 6, 5);
562 addCMR(Counter::getCounter(0), "file1", 2, 2, 6, 5);
563 addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 5);
564 addCMR(Counter::getCounter(0), "file1", 6, 5, 7, 7);
566 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
567 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
568 const auto &FunctionRecord
= *FunctionRecords
.begin();
569 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
570 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
572 ASSERT_EQ(5U, Segments
.size());
573 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
574 EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments
[1]);
575 EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments
[2]);
576 EXPECT_EQ(CoverageSegment(6, 5, 1, true), Segments
[3]);
577 // The old segment builder would get this wrong by emitting multiple segments
578 // which start at 6:5 (a few of which were skipped segments). We should just
579 // get a segment for the region entry.
580 EXPECT_EQ(CoverageSegment(7, 7, false), Segments
[4]);
583 TEST_P(CoverageMappingTest
, handle_consecutive_regions_with_zero_length
) {
584 ProfileWriter
.addRecord({"func1", 0x1234, {1, 2}}, Err
);
585 startFunction("func1", 0x1234);
587 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
588 addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
589 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
590 addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
591 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
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(1U, Segments
.size());
600 EXPECT_EQ(CoverageSegment(1, 1, true), Segments
[0]);
601 // We need to get a skipped segment starting at 1:1. In this case there is
602 // also a region entry at 1:1.
605 TEST_P(CoverageMappingTest
, handle_sandwiched_zero_length_region
) {
606 ProfileWriter
.addRecord({"func1", 0x1234, {2, 1}}, Err
);
607 startFunction("func1", 0x1234);
609 addCMR(Counter::getCounter(0), "file1", 1, 5, 4, 4);
610 addCMR(Counter::getCounter(1), "file1", 1, 9, 1, 50);
611 addCMR(Counter::getCounter(1), "file1", 2, 7, 2, 34);
612 addCMR(Counter::getCounter(1), "file1", 3, 5, 3, 21);
613 addCMR(Counter::getCounter(1), "file1", 3, 21, 3, 21);
614 addCMR(Counter::getCounter(1), "file1", 4, 12, 4, 17);
616 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
617 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
618 const auto &FunctionRecord
= *FunctionRecords
.begin();
619 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
620 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
622 ASSERT_EQ(10U, Segments
.size());
623 EXPECT_EQ(CoverageSegment(1, 5, 2, true), Segments
[0]);
624 EXPECT_EQ(CoverageSegment(1, 9, 1, true), Segments
[1]);
625 EXPECT_EQ(CoverageSegment(1, 50, 2, false), Segments
[2]);
626 EXPECT_EQ(CoverageSegment(2, 7, 1, true), Segments
[3]);
627 EXPECT_EQ(CoverageSegment(2, 34, 2, false), Segments
[4]);
628 EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments
[5]);
629 EXPECT_EQ(CoverageSegment(3, 21, 2, true), Segments
[6]);
630 // Handle the zero-length region by creating a segment with its predecessor's
631 // count (i.e the count from 1:5 -> 4:4).
632 EXPECT_EQ(CoverageSegment(4, 4, false), Segments
[7]);
633 // The area between 4:4 and 4:12 is skipped.
634 EXPECT_EQ(CoverageSegment(4, 12, 1, true), Segments
[8]);
635 EXPECT_EQ(CoverageSegment(4, 17, false), Segments
[9]);
638 TEST_P(CoverageMappingTest
, handle_last_completed_region
) {
639 ProfileWriter
.addRecord({"func1", 0x1234, {1, 2, 3, 4}}, Err
);
640 startFunction("func1", 0x1234);
642 addCMR(Counter::getCounter(0), "file1", 1, 1, 8, 8);
643 addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
644 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
645 addCMR(Counter::getCounter(3), "file1", 6, 6, 7, 7);
647 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
648 const auto FunctionRecords
= LoadedCoverage
->getCoveredFunctions();
649 const auto &FunctionRecord
= *FunctionRecords
.begin();
650 CoverageData Data
= LoadedCoverage
->getCoverageForFunction(FunctionRecord
);
651 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
653 ASSERT_EQ(8U, Segments
.size());
654 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments
[0]);
655 EXPECT_EQ(CoverageSegment(2, 2, 2, true), Segments
[1]);
656 EXPECT_EQ(CoverageSegment(3, 3, 3, true), Segments
[2]);
657 EXPECT_EQ(CoverageSegment(4, 4, 2, false), Segments
[3]);
658 EXPECT_EQ(CoverageSegment(5, 5, 1, false), Segments
[4]);
659 EXPECT_EQ(CoverageSegment(6, 6, 4, true), Segments
[5]);
660 EXPECT_EQ(CoverageSegment(7, 7, 1, false), Segments
[6]);
661 EXPECT_EQ(CoverageSegment(8, 8, false), Segments
[7]);
664 TEST_P(CoverageMappingTest
, expansion_gets_first_counter
) {
665 startFunction("func", 0x1234);
666 addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
667 // This starts earlier in "foo", so the expansion should get its counter.
668 addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
669 addExpansionCMR("bar", "foo", 3, 3, 3, 3);
671 writeAndReadCoverageRegions();
672 ASSERT_EQ(1u, OutputFunctions
.size());
673 OutputFunctionCoverageData
&Output
= OutputFunctions
.back();
675 ASSERT_EQ(CounterMappingRegion::ExpansionRegion
, Output
.Regions
[2].Kind
);
676 ASSERT_EQ(Counter::getCounter(2), Output
.Regions
[2].Count
);
677 ASSERT_EQ(3U, Output
.Regions
[2].LineStart
);
680 TEST_P(CoverageMappingTest
, basic_coverage_iteration
) {
681 ProfileWriter
.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err
);
683 startFunction("func", 0x1234);
684 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
685 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
686 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
687 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
688 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
690 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
691 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
692 ASSERT_EQ(7U, Segments
.size());
693 ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments
[0]);
694 ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments
[1]);
695 ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments
[2]);
696 ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments
[3]);
697 ASSERT_EQ(CoverageSegment(9, 9, false), Segments
[4]);
698 ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments
[5]);
699 ASSERT_EQ(CoverageSegment(11, 11, false), Segments
[6]);
702 TEST_P(CoverageMappingTest
, test_line_coverage_iterator
) {
703 ProfileWriter
.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err
);
705 startFunction("func", 0x1234);
706 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
707 addSkipped("file1", 1, 3, 1, 8); // skipped region inside previous block
708 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
709 addSkipped("file1", 4, 1, 4, 20); // skipped line
710 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
711 addSkipped("file1", 10, 1, 12,
712 20); // skipped region which contains next region
713 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
714 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
715 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
718 const unsigned LineCounts
[] = {20, 20, 20, 0, 30, 10, 10, 10, 10, 0, 0, 0, 0};
719 const bool MappedLines
[] = {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0};
720 ASSERT_EQ(std::size(LineCounts
), std::size(MappedLines
));
722 for (const auto &LCS
: getLineCoverageStats(Data
)) {
723 ASSERT_LT(Line
, std::size(LineCounts
));
724 ASSERT_LT(Line
, std::size(MappedLines
));
726 ASSERT_EQ(Line
+ 1, LCS
.getLine());
727 errs() << "Line: " << Line
+ 1 << ", count = " << LCS
.getExecutionCount()
728 << ", mapped = " << LCS
.isMapped() << "\n";
729 ASSERT_EQ(LineCounts
[Line
], LCS
.getExecutionCount());
730 ASSERT_EQ(MappedLines
[Line
], LCS
.isMapped());
733 ASSERT_EQ(12U, Line
);
735 // Check that operator->() works / compiles.
736 ASSERT_EQ(1U, LineCoverageIterator(Data
)->getLine());
739 TEST_P(CoverageMappingTest
, uncovered_function
) {
740 startFunction("func", 0x1234);
741 addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
742 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
744 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
745 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
746 ASSERT_EQ(2U, Segments
.size());
747 ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments
[0]);
748 ASSERT_EQ(CoverageSegment(3, 4, false), Segments
[1]);
751 TEST_P(CoverageMappingTest
, uncovered_function_with_mapping
) {
752 startFunction("func", 0x1234);
753 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
754 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
755 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
757 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
758 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
759 ASSERT_EQ(3U, Segments
.size());
760 ASSERT_EQ(CoverageSegment(1, 1, 0, true), Segments
[0]);
761 ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments
[1]);
762 ASSERT_EQ(CoverageSegment(9, 9, false), Segments
[2]);
765 TEST_P(CoverageMappingTest
, combine_regions
) {
766 ProfileWriter
.addRecord({"func", 0x1234, {10, 20, 30}}, Err
);
768 startFunction("func", 0x1234);
769 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
770 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
771 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
772 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
774 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
775 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
776 ASSERT_EQ(4U, Segments
.size());
777 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments
[0]);
778 ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments
[1]);
779 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments
[2]);
780 ASSERT_EQ(CoverageSegment(9, 9, false), Segments
[3]);
783 TEST_P(CoverageMappingTest
, restore_combined_counter_after_nested_region
) {
784 ProfileWriter
.addRecord({"func", 0x1234, {10, 20, 40}}, Err
);
786 startFunction("func", 0x1234);
787 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
788 addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
789 addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
790 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
792 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
793 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
794 ASSERT_EQ(4U, Segments
.size());
795 EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments
[0]);
796 EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments
[1]);
797 EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments
[2]);
798 EXPECT_EQ(CoverageSegment(9, 9, false), Segments
[3]);
801 // If CodeRegions and ExpansionRegions cover the same area,
802 // only counts of CodeRegions should be used.
803 TEST_P(CoverageMappingTest
, dont_combine_expansions
) {
804 ProfileWriter
.addRecord({"func", 0x1234, {10, 20}}, Err
);
805 ProfileWriter
.addRecord({"func", 0x1234, {0, 0}}, Err
);
807 startFunction("func", 0x1234);
808 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
809 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
810 addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
811 addExpansionCMR("file1", "include1", 3, 3, 4, 4);
812 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
814 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file1");
815 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
816 ASSERT_EQ(4U, Segments
.size());
817 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments
[0]);
818 ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments
[1]);
819 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments
[2]);
820 ASSERT_EQ(CoverageSegment(9, 9, false), Segments
[3]);
823 // If an area is covered only by ExpansionRegions, they should be combinated.
824 TEST_P(CoverageMappingTest
, combine_expansions
) {
825 ProfileWriter
.addRecord({"func", 0x1234, {2, 3, 7}}, Err
);
827 startFunction("func", 0x1234);
828 addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
829 addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
830 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
831 addExpansionCMR("file", "include1", 3, 1, 3, 5);
832 addExpansionCMR("file", "include2", 3, 1, 3, 5);
834 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
836 CoverageData Data
= LoadedCoverage
->getCoverageForFile("file");
837 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
838 ASSERT_EQ(4U, Segments
.size());
839 EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments
[0]);
840 EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments
[1]);
841 EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments
[2]);
842 EXPECT_EQ(CoverageSegment(5, 5, false), Segments
[3]);
845 // Test that counters not associated with any code regions are allowed.
846 TEST_P(CoverageMappingTest
, non_code_region_counters
) {
847 // No records in profdata
849 startFunction("func", 0x1234);
850 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
851 addCMR(Counter::getExpression(0), "file", 6, 1, 6, 5);
852 addExpression(CounterExpression(
853 CounterExpression::Add
, Counter::getCounter(1), Counter::getCounter(2)));
855 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
857 std::vector
<std::string
> Names
;
858 for (const auto &Func
: LoadedCoverage
->getCoveredFunctions()) {
859 Names
.push_back(Func
.Name
);
860 ASSERT_EQ(2U, Func
.CountedRegions
.size());
862 ASSERT_EQ(1U, Names
.size());
865 // Test that MCDC bitmasks not associated with any code regions are allowed.
866 TEST_P(CoverageMappingTest
, non_code_region_bitmask
) {
867 // No records in profdata
869 startFunction("func", 0x1234);
870 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
871 addCMR(Counter::getCounter(1), "file", 1, 1, 5, 5);
872 addCMR(Counter::getCounter(2), "file", 1, 1, 5, 5);
873 addCMR(Counter::getCounter(3), "file", 1, 1, 5, 5);
875 addMCDCDecisionCMR(3, 2, "file", 7, 1, 7, 6);
876 addMCDCBranchCMR(Counter::getCounter(0), Counter::getCounter(1), 0, {-1, 1},
878 addMCDCBranchCMR(Counter::getCounter(2), Counter::getCounter(3), 1, {-1, -1},
881 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
883 std::vector
<std::string
> Names
;
884 for (const auto &Func
: LoadedCoverage
->getCoveredFunctions()) {
885 Names
.push_back(Func
.Name
);
886 ASSERT_EQ(2U, Func
.CountedBranchRegions
.size());
887 ASSERT_EQ(1U, Func
.MCDCRecords
.size());
889 ASSERT_EQ(1U, Names
.size());
892 // Test the order of MCDCDecision before Expansion
893 TEST_P(CoverageMappingTest
, decision_before_expansion
) {
894 startFunction("foo", 0x1234);
895 addCMR(Counter::getCounter(0), "foo", 3, 23, 5, 2);
897 // This(4:11) was put after Expansion(4:11) before the fix
898 addMCDCDecisionCMR(3, 2, "foo", 4, 11, 4, 20);
900 addExpansionCMR("foo", "A", 4, 11, 4, 12);
901 addExpansionCMR("foo", "B", 4, 19, 4, 20);
902 addCMR(Counter::getCounter(0), "A", 1, 14, 1, 17);
903 addCMR(Counter::getCounter(0), "A", 1, 14, 1, 17);
904 addMCDCBranchCMR(Counter::getCounter(0), Counter::getCounter(1), 0, {-1, 1},
906 addCMR(Counter::getCounter(1), "B", 1, 14, 1, 17);
907 addMCDCBranchCMR(Counter::getCounter(1), Counter::getCounter(2), 1, {-1, -1},
910 // InputFunctionCoverageData::Regions is rewritten after the write.
911 auto InputRegions
= InputFunctions
.back().Regions
;
913 writeAndReadCoverageRegions();
915 const auto &OutputRegions
= OutputFunctions
.back().Regions
;
917 size_t N
= ArrayRef(InputRegions
).size();
918 ASSERT_EQ(N
, OutputRegions
.size());
919 for (size_t I
= 0; I
< N
; ++I
) {
920 ASSERT_EQ(InputRegions
[I
].Kind
, OutputRegions
[I
].Kind
);
921 ASSERT_EQ(InputRegions
[I
].FileID
, OutputRegions
[I
].FileID
);
922 ASSERT_EQ(InputRegions
[I
].ExpandedFileID
, OutputRegions
[I
].ExpandedFileID
);
923 ASSERT_EQ(InputRegions
[I
].startLoc(), OutputRegions
[I
].startLoc());
924 ASSERT_EQ(InputRegions
[I
].endLoc(), OutputRegions
[I
].endLoc());
928 TEST_P(CoverageMappingTest
, strip_filename_prefix
) {
929 ProfileWriter
.addRecord({"file1:func", 0x1234, {0}}, Err
);
931 startFunction("file1:func", 0x1234);
932 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
933 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
935 std::vector
<std::string
> Names
;
936 for (const auto &Func
: LoadedCoverage
->getCoveredFunctions())
937 Names
.push_back(Func
.Name
);
938 ASSERT_EQ(1U, Names
.size());
939 ASSERT_EQ("func", Names
[0]);
942 TEST_P(CoverageMappingTest
, strip_unknown_filename_prefix
) {
943 ProfileWriter
.addRecord({"<unknown>:func", 0x1234, {0}}, Err
);
945 startFunction("<unknown>:func", 0x1234);
946 addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
947 EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
949 std::vector
<std::string
> Names
;
950 for (const auto &Func
: LoadedCoverage
->getCoveredFunctions())
951 Names
.push_back(Func
.Name
);
952 ASSERT_EQ(1U, Names
.size());
953 ASSERT_EQ("func", Names
[0]);
956 TEST_P(CoverageMappingTest
, dont_detect_false_instantiations
) {
957 ProfileWriter
.addRecord({"foo", 0x1234, {10}}, Err
);
958 ProfileWriter
.addRecord({"bar", 0x2345, {20}}, Err
);
960 startFunction("foo", 0x1234);
961 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
962 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
964 startFunction("bar", 0x2345);
965 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
966 addExpansionCMR("main", "expanded", 9, 1, 9, 5);
968 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
970 std::vector
<InstantiationGroup
> InstantiationGroups
=
971 LoadedCoverage
->getInstantiationGroups("expanded");
972 ASSERT_TRUE(InstantiationGroups
.empty());
975 TEST_P(CoverageMappingTest
, load_coverage_for_expanded_file
) {
976 ProfileWriter
.addRecord({"func", 0x1234, {10}}, Err
);
978 startFunction("func", 0x1234);
979 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
980 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
982 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
984 CoverageData Data
= LoadedCoverage
->getCoverageForFile("expanded");
985 std::vector
<CoverageSegment
> Segments(Data
.begin(), Data
.end());
986 ASSERT_EQ(2U, Segments
.size());
987 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments
[0]);
988 EXPECT_EQ(CoverageSegment(1, 10, false), Segments
[1]);
991 TEST_P(CoverageMappingTest
, skip_duplicate_function_record
) {
992 ProfileWriter
.addRecord({"func", 0x1234, {1}}, Err
);
994 // This record should be loaded.
995 startFunction("func", 0x1234);
996 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
998 // This record should be loaded.
999 startFunction("func", 0x1234);
1000 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
1001 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
1003 // This record should be skipped.
1004 startFunction("func", 0x1234);
1005 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
1007 // This record should be loaded.
1008 startFunction("func", 0x1234);
1009 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
1010 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
1012 // This record should be skipped.
1013 startFunction("func", 0x1234);
1014 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
1015 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
1017 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
1019 auto Funcs
= LoadedCoverage
->getCoveredFunctions();
1020 unsigned NumFuncs
= std::distance(Funcs
.begin(), Funcs
.end());
1021 ASSERT_EQ(3U, NumFuncs
);
1024 INSTANTIATE_TEST_SUITE_P(ParameterizedCovMapTest
, CoverageMappingTest
,
1025 ::testing::Combine(::testing::Bool(),
1026 ::testing::Bool()));
1028 TEST(CoverageMappingTest
, filename_roundtrip
) {
1029 std::vector
<std::string
> Paths({"dir", "a", "b", "c", "d", "e"});
1031 for (bool Compress
: {false, true}) {
1032 std::string EncodedFilenames
;
1034 raw_string_ostream
OS(EncodedFilenames
);
1035 CoverageFilenamesSectionWriter
Writer(Paths
);
1036 Writer
.write(OS
, Compress
);
1039 std::vector
<std::string
> ReadFilenames
;
1040 RawCoverageFilenamesReader
Reader(EncodedFilenames
, ReadFilenames
);
1041 EXPECT_THAT_ERROR(Reader
.read(CovMapVersion::CurrentVersion
), Succeeded());
1043 ASSERT_EQ(ReadFilenames
.size(), Paths
.size());
1044 for (unsigned I
= 1; I
< Paths
.size(); ++I
) {
1045 SmallString
<256> P(Paths
[0]);
1046 llvm::sys::path::append(P
, Paths
[I
]);
1047 ASSERT_EQ(ReadFilenames
[I
], P
);
1052 TEST(CoverageMappingTest
, filename_compilation_dir
) {
1053 std::vector
<std::string
> Paths({"dir", "a", "b", "c", "d", "e"});
1055 for (bool Compress
: {false, true}) {
1056 std::string EncodedFilenames
;
1058 raw_string_ostream
OS(EncodedFilenames
);
1059 CoverageFilenamesSectionWriter
Writer(Paths
);
1060 Writer
.write(OS
, Compress
);
1063 StringRef CompilationDir
= "out";
1064 std::vector
<std::string
> ReadFilenames
;
1065 RawCoverageFilenamesReader
Reader(EncodedFilenames
, ReadFilenames
,
1067 EXPECT_THAT_ERROR(Reader
.read(CovMapVersion::CurrentVersion
), Succeeded());
1069 ASSERT_EQ(ReadFilenames
.size(), Paths
.size());
1070 for (unsigned I
= 1; I
< Paths
.size(); ++I
) {
1071 SmallString
<256> P(CompilationDir
);
1072 llvm::sys::path::append(P
, Paths
[I
]);
1073 ASSERT_EQ(ReadFilenames
[I
], P
);
1078 TEST(CoverageMappingTest
, TVIdxBuilder
) {
1079 // ((n0 && n3) || (n2 && n4) || (n1 && n5))
1080 static const std::array
<mcdc::ConditionIDs
, 6> Branches
= {{
1089 auto TheBuilder
= mcdc::TVIdxBuilder(
1090 SmallVector
<mcdc::ConditionIDs
>(ArrayRef(Branches
)), Offset
);
1091 EXPECT_TRUE(TheBuilder
.NumTestVectors
< TheBuilder
.HardMaxTVs
);
1092 EXPECT_EQ(TheBuilder
.Indices
.size(), 6u);
1093 EXPECT_EQ(TheBuilder
.NumTestVectors
, 15);
1095 std::map
<int, int> Decisions
;
1096 for (unsigned I
= 0; I
< TheBuilder
.Indices
.size(); ++I
) {
1099 std::array
<int, 2> Indices
;
1101 static const std::array
<Rec
, 6> IndicesRefs
= {{
1109 EXPECT_EQ(TheBuilder
.Indices
[I
], IndicesRefs
[I
].Indices
);
1112 const auto &Node
= TheBuilder
.SavedNodes
[I
];
1113 EXPECT_EQ(Node
.Width
, IndicesRefs
[I
].Width
);
1114 for (int C
= 0; C
< 2; ++C
) {
1115 auto Index
= TheBuilder
.Indices
[I
][C
];
1116 if (Node
.NextIDs
[C
] < 0)
1117 EXPECT_TRUE(Decisions
.insert({Index
, Node
.Width
}).second
);
1123 int NextIdx
= Offset
;
1124 for (const auto [Index
, Width
] : Decisions
) {
1125 EXPECT_EQ(Index
, NextIdx
);
1128 // The sum of Width(s) is NumTVs.
1129 EXPECT_EQ(NextIdx
, Offset
+ TheBuilder
.NumTestVectors
);
1133 } // end anonymous namespace