1 //===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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 "gtest/gtest.h"
17
18 #include <ostream>
19
20 using namespace llvm;
21 using namespace coverage;
22
NoError(Error E)23 static ::testing::AssertionResult NoError(Error E) {
24 if (!E)
25 return ::testing::AssertionSuccess();
26 return ::testing::AssertionFailure() << "error: " << toString(std::move(E))
27 << "\n";
28 }
29
30 namespace llvm {
31 namespace coverage {
PrintTo(const Counter & C,::std::ostream * os)32 void PrintTo(const Counter &C, ::std::ostream *os) {
33 if (C.isZero())
34 *os << "Zero";
35 else if (C.isExpression())
36 *os << "Expression " << C.getExpressionID();
37 else
38 *os << "Counter " << C.getCounterID();
39 }
40
PrintTo(const CoverageSegment & S,::std::ostream * os)41 void PrintTo(const CoverageSegment &S, ::std::ostream *os) {
42 *os << "CoverageSegment(" << S.Line << ", " << S.Col << ", ";
43 if (S.HasCount)
44 *os << S.Count << ", ";
45 *os << (S.IsRegionEntry ? "true" : "false") << ")";
46 }
47 }
48 }
49
50 namespace {
51
52 struct OutputFunctionCoverageData {
53 StringRef Name;
54 uint64_t Hash;
55 std::vector<StringRef> Filenames;
56 std::vector<CounterMappingRegion> Regions;
57
fillCoverageMappingRecord__anon747248e00111::OutputFunctionCoverageData58 void fillCoverageMappingRecord(CoverageMappingRecord &Record) const {
59 Record.FunctionName = Name;
60 Record.FunctionHash = Hash;
61 Record.Filenames = Filenames;
62 Record.Expressions = {};
63 Record.MappingRegions = Regions;
64 }
65 };
66
67 struct CoverageMappingReaderMock : CoverageMappingReader {
68 ArrayRef<OutputFunctionCoverageData> Functions;
69
CoverageMappingReaderMock__anon747248e00111::CoverageMappingReaderMock70 CoverageMappingReaderMock(ArrayRef<OutputFunctionCoverageData> Functions)
71 : Functions(Functions) {}
72
readNextRecord__anon747248e00111::CoverageMappingReaderMock73 Error readNextRecord(CoverageMappingRecord &Record) override {
74 if (Functions.empty())
75 return make_error<CoverageMapError>(coveragemap_error::eof);
76
77 Functions.front().fillCoverageMappingRecord(Record);
78 Functions = Functions.slice(1);
79
80 return Error::success();
81 }
82 };
83
84 struct InputFunctionCoverageData {
85 // Maps the global file index from CoverageMappingTest.Files
86 // to the index of that file within this function. We can't just use
87 // global file indexes here because local indexes have to be dense.
88 // This map is used during serialization to create the virtual file mapping
89 // (from local fileId to global Index) in the head of the per-function
90 // coverage mapping data.
91 SmallDenseMap<unsigned, unsigned> ReverseVirtualFileMapping;
92 std::string Name;
93 uint64_t Hash;
94 std::vector<CounterMappingRegion> Regions;
95
InputFunctionCoverageData__anon747248e00111::InputFunctionCoverageData96 InputFunctionCoverageData(std::string Name, uint64_t Hash)
97 : Name(std::move(Name)), Hash(Hash) {}
98 };
99
100 struct CoverageMappingTest : ::testing::Test {
101 StringMap<unsigned> Files;
102 std::vector<InputFunctionCoverageData> InputFunctions;
103 std::vector<OutputFunctionCoverageData> OutputFunctions;
104
105 InstrProfWriter ProfileWriter;
106 std::unique_ptr<IndexedInstrProfReader> ProfileReader;
107
108 std::unique_ptr<CoverageMapping> LoadedCoverage;
109
SetUp__anon747248e00111::CoverageMappingTest110 void SetUp() override {
111 ProfileWriter.setOutputSparse(false);
112 }
113
getGlobalFileIndex__anon747248e00111::CoverageMappingTest114 unsigned getGlobalFileIndex(StringRef Name) {
115 auto R = Files.find(Name);
116 if (R != Files.end())
117 return R->second;
118 unsigned Index = Files.size();
119 Files.emplace_second(Name, Index);
120 return Index;
121 }
122
123 // Return the file index of file 'Name' for the current function.
124 // Add the file into the global map if necesary.
125 // See also InputFunctionCoverageData::ReverseVirtualFileMapping
126 // for additional comments.
getFileIndexForFunction__anon747248e00111::CoverageMappingTest127 unsigned getFileIndexForFunction(StringRef Name) {
128 unsigned GlobalIndex = getGlobalFileIndex(Name);
129 auto &CurrentFunctionFileMapping =
130 InputFunctions.back().ReverseVirtualFileMapping;
131 auto R = CurrentFunctionFileMapping.find(GlobalIndex);
132 if (R != CurrentFunctionFileMapping.end())
133 return R->second;
134 unsigned IndexInFunction = CurrentFunctionFileMapping.size();
135 CurrentFunctionFileMapping.insert(
136 std::make_pair(GlobalIndex, IndexInFunction));
137 return IndexInFunction;
138 }
139
startFunction__anon747248e00111::CoverageMappingTest140 void startFunction(StringRef FuncName, uint64_t Hash) {
141 InputFunctions.emplace_back(FuncName.str(), Hash);
142 }
143
addCMR__anon747248e00111::CoverageMappingTest144 void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
145 unsigned CE) {
146 InputFunctions.back().Regions.push_back(CounterMappingRegion::makeRegion(
147 C, getFileIndexForFunction(File), LS, CS, LE, CE));
148 }
149
addExpansionCMR__anon747248e00111::CoverageMappingTest150 void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
151 unsigned CS, unsigned LE, unsigned CE) {
152 InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion(
153 getFileIndexForFunction(File), getFileIndexForFunction(ExpandedFile),
154 LS, CS, LE, CE));
155 }
156
writeCoverageRegions__anon747248e00111::CoverageMappingTest157 std::string writeCoverageRegions(InputFunctionCoverageData &Data) {
158 SmallVector<unsigned, 8> FileIDs(Data.ReverseVirtualFileMapping.size());
159 for (const auto &E : Data.ReverseVirtualFileMapping)
160 FileIDs[E.second] = E.first;
161 std::string Coverage;
162 llvm::raw_string_ostream OS(Coverage);
163 CoverageMappingWriter(FileIDs, None, Data.Regions).write(OS);
164 return OS.str();
165 }
166
readCoverageRegions__anon747248e00111::CoverageMappingTest167 void readCoverageRegions(std::string Coverage,
168 OutputFunctionCoverageData &Data) {
169 SmallVector<StringRef, 8> Filenames(Files.size());
170 for (const auto &E : Files)
171 Filenames[E.getValue()] = E.getKey();
172 std::vector<CounterExpression> Expressions;
173 RawCoverageMappingReader Reader(Coverage, Filenames, Data.Filenames,
174 Expressions, Data.Regions);
175 ASSERT_TRUE(NoError(Reader.read()));
176 }
177
writeAndReadCoverageRegions__anon747248e00111::CoverageMappingTest178 void writeAndReadCoverageRegions(bool EmitFilenames = true) {
179 OutputFunctions.resize(InputFunctions.size());
180 for (unsigned I = 0; I < InputFunctions.size(); ++I) {
181 std::string Regions = writeCoverageRegions(InputFunctions[I]);
182 readCoverageRegions(Regions, OutputFunctions[I]);
183 OutputFunctions[I].Name = InputFunctions[I].Name;
184 OutputFunctions[I].Hash = InputFunctions[I].Hash;
185 if (!EmitFilenames)
186 OutputFunctions[I].Filenames.clear();
187 }
188 }
189
readProfCounts__anon747248e00111::CoverageMappingTest190 void readProfCounts() {
191 auto Profile = ProfileWriter.writeBuffer();
192 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
193 ASSERT_TRUE(NoError(ReaderOrErr.takeError()));
194 ProfileReader = std::move(ReaderOrErr.get());
195 }
196
loadCoverageMapping__anon747248e00111::CoverageMappingTest197 void loadCoverageMapping(bool EmitFilenames = true) {
198 readProfCounts();
199 writeAndReadCoverageRegions(EmitFilenames);
200
201 CoverageMappingReaderMock CovReader(OutputFunctions);
202 auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader);
203 ASSERT_TRUE(NoError(CoverageOrErr.takeError()));
204 LoadedCoverage = std::move(CoverageOrErr.get());
205 }
206 };
207
208 struct MaybeSparseCoverageMappingTest
209 : public CoverageMappingTest,
210 public ::testing::WithParamInterface<bool> {
SetUp__anon747248e00111::MaybeSparseCoverageMappingTest211 void SetUp() {
212 CoverageMappingTest::SetUp();
213 ProfileWriter.setOutputSparse(GetParam());
214 }
215 };
216
TEST_P(MaybeSparseCoverageMappingTest,basic_write_read)217 TEST_P(MaybeSparseCoverageMappingTest, basic_write_read) {
218 startFunction("func", 0x1234);
219 addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
220 addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
221 addCMR(Counter::getZero(), "foo", 3, 1, 3, 4);
222 addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
223 addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
224
225 writeAndReadCoverageRegions();
226 ASSERT_EQ(1u, InputFunctions.size());
227 ASSERT_EQ(1u, OutputFunctions.size());
228 InputFunctionCoverageData &Input = InputFunctions.back();
229 OutputFunctionCoverageData &Output = OutputFunctions.back();
230
231 size_t N = makeArrayRef(Input.Regions).size();
232 ASSERT_EQ(N, Output.Regions.size());
233 for (size_t I = 0; I < N; ++I) {
234 ASSERT_EQ(Input.Regions[I].Count, Output.Regions[I].Count);
235 ASSERT_EQ(Input.Regions[I].FileID, Output.Regions[I].FileID);
236 ASSERT_EQ(Input.Regions[I].startLoc(), Output.Regions[I].startLoc());
237 ASSERT_EQ(Input.Regions[I].endLoc(), Output.Regions[I].endLoc());
238 ASSERT_EQ(Input.Regions[I].Kind, Output.Regions[I].Kind);
239 }
240 }
241
TEST_P(MaybeSparseCoverageMappingTest,correct_deserialize_for_more_than_two_files)242 TEST_P(MaybeSparseCoverageMappingTest,
243 correct_deserialize_for_more_than_two_files) {
244 const char *FileNames[] = {"bar", "baz", "foo"};
245 static const unsigned N = array_lengthof(FileNames);
246
247 startFunction("func", 0x1234);
248 for (unsigned I = 0; I < N; ++I)
249 // Use LineStart to hold the index of the file name
250 // in order to preserve that information during possible sorting of CMRs.
251 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
252
253 writeAndReadCoverageRegions();
254 ASSERT_EQ(1u, OutputFunctions.size());
255 OutputFunctionCoverageData &Output = OutputFunctions.back();
256
257 ASSERT_EQ(N, Output.Regions.size());
258 ASSERT_EQ(N, Output.Filenames.size());
259
260 for (unsigned I = 0; I < N; ++I) {
261 ASSERT_GT(N, Output.Regions[I].FileID);
262 ASSERT_GT(N, Output.Regions[I].LineStart);
263 EXPECT_EQ(FileNames[Output.Regions[I].LineStart],
264 Output.Filenames[Output.Regions[I].FileID]);
265 }
266 }
267
TEST_P(MaybeSparseCoverageMappingTest,load_coverage_for_more_than_two_files)268 TEST_P(MaybeSparseCoverageMappingTest, load_coverage_for_more_than_two_files) {
269 InstrProfRecord Record("func", 0x1234, {0});
270 NoError(ProfileWriter.addRecord(std::move(Record)));
271
272 const char *FileNames[] = {"bar", "baz", "foo"};
273 static const unsigned N = array_lengthof(FileNames);
274
275 startFunction("func", 0x1234);
276 for (unsigned I = 0; I < N; ++I)
277 // Use LineStart to hold the index of the file name
278 // in order to preserve that information during possible sorting of CMRs.
279 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
280
281 loadCoverageMapping();
282
283 for (unsigned I = 0; I < N; ++I) {
284 CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
285 ASSERT_TRUE(!Data.empty());
286 EXPECT_EQ(I, Data.begin()->Line);
287 }
288 }
289
TEST_P(MaybeSparseCoverageMappingTest,load_coverage_for_several_functions)290 TEST_P(MaybeSparseCoverageMappingTest, load_coverage_for_several_functions) {
291 InstrProfRecord RecordFunc1("func1", 0x1234, {10});
292 NoError(ProfileWriter.addRecord(std::move(RecordFunc1)));
293 InstrProfRecord RecordFunc2("func2", 0x2345, {20});
294 NoError(ProfileWriter.addRecord(std::move(RecordFunc2)));
295
296 startFunction("func1", 0x1234);
297 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
298
299 startFunction("func2", 0x2345);
300 addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
301
302 loadCoverageMapping();
303
304 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
305 EXPECT_EQ(2U, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
306 for (const auto &FunctionRecord : FunctionRecords) {
307 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
308 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
309 ASSERT_EQ(2U, Segments.size());
310 if (FunctionRecord.Name == "func1") {
311 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
312 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]);
313 } else {
314 ASSERT_EQ("func2", FunctionRecord.Name);
315 EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]);
316 EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]);
317 }
318 }
319 }
320
TEST_P(MaybeSparseCoverageMappingTest,expansion_gets_first_counter)321 TEST_P(MaybeSparseCoverageMappingTest, expansion_gets_first_counter) {
322 startFunction("func", 0x1234);
323 addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
324 // This starts earlier in "foo", so the expansion should get its counter.
325 addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
326 addExpansionCMR("bar", "foo", 3, 3, 3, 3);
327
328 writeAndReadCoverageRegions();
329 ASSERT_EQ(1u, OutputFunctions.size());
330 OutputFunctionCoverageData &Output = OutputFunctions.back();
331
332 ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind);
333 ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count);
334 ASSERT_EQ(3U, Output.Regions[2].LineStart);
335 }
336
TEST_P(MaybeSparseCoverageMappingTest,basic_coverage_iteration)337 TEST_P(MaybeSparseCoverageMappingTest, basic_coverage_iteration) {
338 InstrProfRecord Record("func", 0x1234, {30, 20, 10, 0});
339 NoError(ProfileWriter.addRecord(std::move(Record)));
340
341 startFunction("func", 0x1234);
342 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
343 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
344 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
345 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
346 loadCoverageMapping();
347
348 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
349 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
350 ASSERT_EQ(7U, Segments.size());
351 ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments[0]);
352 ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
353 ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments[2]);
354 ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
355 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[4]);
356 ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
357 ASSERT_EQ(CoverageSegment(11, 11, false), Segments[6]);
358 }
359
TEST_P(MaybeSparseCoverageMappingTest,uncovered_function)360 TEST_P(MaybeSparseCoverageMappingTest, uncovered_function) {
361 startFunction("func", 0x1234);
362 addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
363 loadCoverageMapping();
364
365 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
366 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
367 ASSERT_EQ(2U, Segments.size());
368 ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
369 ASSERT_EQ(CoverageSegment(3, 4, false), Segments[1]);
370 }
371
TEST_P(MaybeSparseCoverageMappingTest,uncovered_function_with_mapping)372 TEST_P(MaybeSparseCoverageMappingTest, uncovered_function_with_mapping) {
373 startFunction("func", 0x1234);
374 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
375 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
376 loadCoverageMapping();
377
378 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
379 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
380 ASSERT_EQ(3U, Segments.size());
381 ASSERT_EQ(CoverageSegment(1, 1, 0, true), Segments[0]);
382 ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
383 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[2]);
384 }
385
TEST_P(MaybeSparseCoverageMappingTest,combine_regions)386 TEST_P(MaybeSparseCoverageMappingTest, combine_regions) {
387 InstrProfRecord Record("func", 0x1234, {10, 20, 30});
388 NoError(ProfileWriter.addRecord(std::move(Record)));
389
390 startFunction("func", 0x1234);
391 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
392 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
393 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
394 loadCoverageMapping();
395
396 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
397 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
398 ASSERT_EQ(4U, Segments.size());
399 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
400 ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
401 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
402 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
403 }
404
TEST_P(MaybeSparseCoverageMappingTest,restore_combined_counter_after_nested_region)405 TEST_P(MaybeSparseCoverageMappingTest,
406 restore_combined_counter_after_nested_region) {
407 InstrProfRecord Record("func", 0x1234, {10, 20, 40});
408 NoError(ProfileWriter.addRecord(std::move(Record)));
409
410 startFunction("func", 0x1234);
411 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
412 addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
413 addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
414 loadCoverageMapping();
415
416 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
417 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
418 ASSERT_EQ(4U, Segments.size());
419 EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]);
420 EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]);
421 EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]);
422 EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]);
423 }
424
425 // If CodeRegions and ExpansionRegions cover the same area,
426 // only counts of CodeRegions should be used.
TEST_P(MaybeSparseCoverageMappingTest,dont_combine_expansions)427 TEST_P(MaybeSparseCoverageMappingTest, dont_combine_expansions) {
428 InstrProfRecord Record1("func", 0x1234, {10, 20});
429 InstrProfRecord Record2("func", 0x1234, {0, 0});
430 NoError(ProfileWriter.addRecord(std::move(Record1)));
431 NoError(ProfileWriter.addRecord(std::move(Record2)));
432
433 startFunction("func", 0x1234);
434 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
435 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
436 addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
437 addExpansionCMR("file1", "include1", 3, 3, 4, 4);
438 loadCoverageMapping();
439
440 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
441 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
442 ASSERT_EQ(4U, Segments.size());
443 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
444 ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
445 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
446 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
447 }
448
449 // If an area is covered only by ExpansionRegions, they should be combinated.
TEST_P(MaybeSparseCoverageMappingTest,combine_expansions)450 TEST_P(MaybeSparseCoverageMappingTest, combine_expansions) {
451 InstrProfRecord Record("func", 0x1234, {2, 3, 7});
452 NoError(ProfileWriter.addRecord(std::move(Record)));
453
454 startFunction("func", 0x1234);
455 addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
456 addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
457 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
458 addExpansionCMR("file", "include1", 3, 1, 3, 5);
459 addExpansionCMR("file", "include2", 3, 1, 3, 5);
460
461 loadCoverageMapping();
462
463 CoverageData Data = LoadedCoverage->getCoverageForFile("file");
464 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
465 ASSERT_EQ(4U, Segments.size());
466 EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]);
467 EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]);
468 EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]);
469 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]);
470 }
471
TEST_P(MaybeSparseCoverageMappingTest,strip_filename_prefix)472 TEST_P(MaybeSparseCoverageMappingTest, strip_filename_prefix) {
473 InstrProfRecord Record("file1:func", 0x1234, {0});
474 NoError(ProfileWriter.addRecord(std::move(Record)));
475
476 startFunction("file1:func", 0x1234);
477 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
478 loadCoverageMapping();
479
480 std::vector<std::string> Names;
481 for (const auto &Func : LoadedCoverage->getCoveredFunctions())
482 Names.push_back(Func.Name);
483 ASSERT_EQ(1U, Names.size());
484 ASSERT_EQ("func", Names[0]);
485 }
486
TEST_P(MaybeSparseCoverageMappingTest,strip_unknown_filename_prefix)487 TEST_P(MaybeSparseCoverageMappingTest, strip_unknown_filename_prefix) {
488 InstrProfRecord Record("<unknown>:func", 0x1234, {0});
489 NoError(ProfileWriter.addRecord(std::move(Record)));
490
491 startFunction("<unknown>:func", 0x1234);
492 addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
493 loadCoverageMapping(/*EmitFilenames=*/false);
494
495 std::vector<std::string> Names;
496 for (const auto &Func : LoadedCoverage->getCoveredFunctions())
497 Names.push_back(Func.Name);
498 ASSERT_EQ(1U, Names.size());
499 ASSERT_EQ("func", Names[0]);
500 }
501
TEST_P(MaybeSparseCoverageMappingTest,dont_detect_false_instantiations)502 TEST_P(MaybeSparseCoverageMappingTest, dont_detect_false_instantiations) {
503 InstrProfRecord Record1("foo", 0x1234, {10});
504 InstrProfRecord Record2("bar", 0x2345, {20});
505 NoError(ProfileWriter.addRecord(std::move(Record1)));
506 NoError(ProfileWriter.addRecord(std::move(Record2)));
507
508 startFunction("foo", 0x1234);
509 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
510 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
511
512 startFunction("bar", 0x2345);
513 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
514 addExpansionCMR("main", "expanded", 9, 1, 9, 5);
515
516 loadCoverageMapping();
517
518 std::vector<const FunctionRecord *> Instantiations =
519 LoadedCoverage->getInstantiations("expanded");
520 ASSERT_TRUE(Instantiations.empty());
521 }
522
TEST_P(MaybeSparseCoverageMappingTest,load_coverage_for_expanded_file)523 TEST_P(MaybeSparseCoverageMappingTest, load_coverage_for_expanded_file) {
524 InstrProfRecord Record("func", 0x1234, {10});
525 NoError(ProfileWriter.addRecord(std::move(Record)));
526
527 startFunction("func", 0x1234);
528 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
529 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
530
531 loadCoverageMapping();
532
533 CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
534 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
535 ASSERT_EQ(2U, Segments.size());
536 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
537 EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]);
538 }
539
540 INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseCoverageMappingTest,
541 ::testing::Bool());
542
543 } // end anonymous namespace
544