[obj2yaml] - Stop triggering UB when dumping corrupted strings.
[llvm-complete.git] / unittests / ProfileData / CoverageMappingTest.cpp
blob82e7574d15c92d7940f8c89d8e215246ddcaa58a
1 //===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
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"
19 #include <ostream>
20 #include <utility>
22 using namespace llvm;
23 using namespace coverage;
25 LLVM_NODISCARD static ::testing::AssertionResult
26 ErrorEquals(coveragemap_error Expected, Error E) {
27 coveragemap_error Found;
28 std::string FoundMsg;
29 handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
30 Found = CME.get();
31 FoundMsg = CME.message();
32 });
33 if (Expected == Found)
34 return ::testing::AssertionSuccess();
35 return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
38 namespace llvm {
39 namespace coverage {
40 void PrintTo(const Counter &C, ::std::ostream *os) {
41 if (C.isZero())
42 *os << "Zero";
43 else if (C.isExpression())
44 *os << "Expression " << C.getExpressionID();
45 else
46 *os << "Counter " << C.getCounterID();
49 void PrintTo(const CoverageSegment &S, ::std::ostream *os) {
50 *os << "CoverageSegment(" << S.Line << ", " << S.Col << ", ";
51 if (S.HasCount)
52 *os << S.Count << ", ";
53 *os << (S.IsRegionEntry ? "true" : "false") << ")";
58 namespace {
60 struct OutputFunctionCoverageData {
61 StringRef Name;
62 uint64_t Hash;
63 std::vector<StringRef> Filenames;
64 std::vector<CounterMappingRegion> Regions;
66 OutputFunctionCoverageData() : Hash(0) {}
68 OutputFunctionCoverageData(OutputFunctionCoverageData &&OFCD)
69 : Name(OFCD.Name), Hash(OFCD.Hash), Filenames(std::move(OFCD.Filenames)),
70 Regions(std::move(OFCD.Regions)) {}
72 OutputFunctionCoverageData(const OutputFunctionCoverageData &) = delete;
73 OutputFunctionCoverageData &
74 operator=(const OutputFunctionCoverageData &) = delete;
75 OutputFunctionCoverageData &operator=(OutputFunctionCoverageData &&) = delete;
77 void fillCoverageMappingRecord(CoverageMappingRecord &Record) const {
78 Record.FunctionName = Name;
79 Record.FunctionHash = Hash;
80 Record.Filenames = Filenames;
81 Record.Expressions = {};
82 Record.MappingRegions = Regions;
86 struct CoverageMappingReaderMock : CoverageMappingReader {
87 ArrayRef<OutputFunctionCoverageData> Functions;
89 CoverageMappingReaderMock(ArrayRef<OutputFunctionCoverageData> Functions)
90 : Functions(Functions) {}
92 Error readNextRecord(CoverageMappingRecord &Record) override {
93 if (Functions.empty())
94 return make_error<CoverageMapError>(coveragemap_error::eof);
96 Functions.front().fillCoverageMappingRecord(Record);
97 Functions = Functions.slice(1);
99 return Error::success();
103 struct InputFunctionCoverageData {
104 // Maps the global file index from CoverageMappingTest.Files
105 // to the index of that file within this function. We can't just use
106 // global file indexes here because local indexes have to be dense.
107 // This map is used during serialization to create the virtual file mapping
108 // (from local fileId to global Index) in the head of the per-function
109 // coverage mapping data.
110 SmallDenseMap<unsigned, unsigned> ReverseVirtualFileMapping;
111 std::string Name;
112 uint64_t Hash;
113 std::vector<CounterMappingRegion> Regions;
115 InputFunctionCoverageData(std::string Name, uint64_t Hash)
116 : Name(std::move(Name)), Hash(Hash) {}
118 InputFunctionCoverageData(InputFunctionCoverageData &&IFCD)
119 : ReverseVirtualFileMapping(std::move(IFCD.ReverseVirtualFileMapping)),
120 Name(std::move(IFCD.Name)), Hash(IFCD.Hash),
121 Regions(std::move(IFCD.Regions)) {}
123 InputFunctionCoverageData(const InputFunctionCoverageData &) = delete;
124 InputFunctionCoverageData &
125 operator=(const InputFunctionCoverageData &) = delete;
126 InputFunctionCoverageData &operator=(InputFunctionCoverageData &&) = delete;
129 struct CoverageMappingTest : ::testing::TestWithParam<std::pair<bool, bool>> {
130 bool UseMultipleReaders;
131 StringMap<unsigned> Files;
132 std::vector<InputFunctionCoverageData> InputFunctions;
133 std::vector<OutputFunctionCoverageData> OutputFunctions;
135 InstrProfWriter ProfileWriter;
136 std::unique_ptr<IndexedInstrProfReader> ProfileReader;
138 std::unique_ptr<CoverageMapping> LoadedCoverage;
140 void SetUp() override {
141 ProfileWriter.setOutputSparse(GetParam().first);
142 UseMultipleReaders = GetParam().second;
145 unsigned getGlobalFileIndex(StringRef Name) {
146 auto R = Files.find(Name);
147 if (R != Files.end())
148 return R->second;
149 unsigned Index = Files.size();
150 Files.try_emplace(Name, Index);
151 return Index;
154 // Return the file index of file 'Name' for the current function.
155 // Add the file into the global map if necessary.
156 // See also InputFunctionCoverageData::ReverseVirtualFileMapping
157 // for additional comments.
158 unsigned getFileIndexForFunction(StringRef Name) {
159 unsigned GlobalIndex = getGlobalFileIndex(Name);
160 auto &CurrentFunctionFileMapping =
161 InputFunctions.back().ReverseVirtualFileMapping;
162 auto R = CurrentFunctionFileMapping.find(GlobalIndex);
163 if (R != CurrentFunctionFileMapping.end())
164 return R->second;
165 unsigned IndexInFunction = CurrentFunctionFileMapping.size();
166 CurrentFunctionFileMapping.insert(
167 std::make_pair(GlobalIndex, IndexInFunction));
168 return IndexInFunction;
171 void startFunction(StringRef FuncName, uint64_t Hash) {
172 InputFunctions.emplace_back(FuncName.str(), Hash);
175 void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
176 unsigned CE, bool Skipped = false) {
177 auto &Regions = InputFunctions.back().Regions;
178 unsigned FileID = getFileIndexForFunction(File);
179 Regions.push_back(
180 Skipped ? CounterMappingRegion::makeSkipped(FileID, LS, CS, LE, CE)
181 : CounterMappingRegion::makeRegion(C, FileID, LS, CS, LE, CE));
184 void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
185 unsigned CS, unsigned LE, unsigned CE) {
186 InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion(
187 getFileIndexForFunction(File), getFileIndexForFunction(ExpandedFile),
188 LS, CS, LE, CE));
191 std::string writeCoverageRegions(InputFunctionCoverageData &Data) {
192 SmallVector<unsigned, 8> FileIDs(Data.ReverseVirtualFileMapping.size());
193 for (const auto &E : Data.ReverseVirtualFileMapping)
194 FileIDs[E.second] = E.first;
195 std::string Coverage;
196 llvm::raw_string_ostream OS(Coverage);
197 CoverageMappingWriter(FileIDs, None, Data.Regions).write(OS);
198 return OS.str();
201 void readCoverageRegions(const std::string &Coverage,
202 OutputFunctionCoverageData &Data) {
203 SmallVector<StringRef, 8> Filenames(Files.size());
204 for (const auto &E : Files)
205 Filenames[E.getValue()] = E.getKey();
206 std::vector<CounterExpression> Expressions;
207 RawCoverageMappingReader Reader(Coverage, Filenames, Data.Filenames,
208 Expressions, Data.Regions);
209 EXPECT_THAT_ERROR(Reader.read(), Succeeded());
212 void writeAndReadCoverageRegions(bool EmitFilenames = true) {
213 OutputFunctions.resize(InputFunctions.size());
214 for (unsigned I = 0; I < InputFunctions.size(); ++I) {
215 std::string Regions = writeCoverageRegions(InputFunctions[I]);
216 readCoverageRegions(Regions, OutputFunctions[I]);
217 OutputFunctions[I].Name = InputFunctions[I].Name;
218 OutputFunctions[I].Hash = InputFunctions[I].Hash;
219 if (!EmitFilenames)
220 OutputFunctions[I].Filenames.clear();
224 void readProfCounts() {
225 auto Profile = ProfileWriter.writeBuffer();
226 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
227 EXPECT_THAT_ERROR(ReaderOrErr.takeError(), Succeeded());
228 ProfileReader = std::move(ReaderOrErr.get());
231 Expected<std::unique_ptr<CoverageMapping>> readOutputFunctions() {
232 std::vector<std::unique_ptr<CoverageMappingReader>> CoverageReaders;
233 if (UseMultipleReaders) {
234 for (const auto &OF : OutputFunctions) {
235 ArrayRef<OutputFunctionCoverageData> Funcs(OF);
236 CoverageReaders.push_back(
237 std::make_unique<CoverageMappingReaderMock>(Funcs));
239 } else {
240 ArrayRef<OutputFunctionCoverageData> Funcs(OutputFunctions);
241 CoverageReaders.push_back(
242 std::make_unique<CoverageMappingReaderMock>(Funcs));
244 return CoverageMapping::load(CoverageReaders, *ProfileReader);
247 Error loadCoverageMapping(bool EmitFilenames = true) {
248 readProfCounts();
249 writeAndReadCoverageRegions(EmitFilenames);
250 auto CoverageOrErr = readOutputFunctions();
251 if (!CoverageOrErr)
252 return CoverageOrErr.takeError();
253 LoadedCoverage = std::move(CoverageOrErr.get());
254 return Error::success();
258 TEST_P(CoverageMappingTest, basic_write_read) {
259 startFunction("func", 0x1234);
260 addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
261 addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
262 addCMR(Counter::getZero(), "foo", 3, 1, 3, 4);
263 addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
264 addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
266 writeAndReadCoverageRegions();
267 ASSERT_EQ(1u, InputFunctions.size());
268 ASSERT_EQ(1u, OutputFunctions.size());
269 InputFunctionCoverageData &Input = InputFunctions.back();
270 OutputFunctionCoverageData &Output = OutputFunctions.back();
272 size_t N = makeArrayRef(Input.Regions).size();
273 ASSERT_EQ(N, Output.Regions.size());
274 for (size_t I = 0; I < N; ++I) {
275 ASSERT_EQ(Input.Regions[I].Count, Output.Regions[I].Count);
276 ASSERT_EQ(Input.Regions[I].FileID, Output.Regions[I].FileID);
277 ASSERT_EQ(Input.Regions[I].startLoc(), Output.Regions[I].startLoc());
278 ASSERT_EQ(Input.Regions[I].endLoc(), Output.Regions[I].endLoc());
279 ASSERT_EQ(Input.Regions[I].Kind, Output.Regions[I].Kind);
283 TEST_P(CoverageMappingTest, correct_deserialize_for_more_than_two_files) {
284 const char *FileNames[] = {"bar", "baz", "foo"};
285 static const unsigned N = array_lengthof(FileNames);
287 startFunction("func", 0x1234);
288 for (unsigned I = 0; I < N; ++I)
289 // Use LineStart to hold the index of the file name
290 // in order to preserve that information during possible sorting of CMRs.
291 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
293 writeAndReadCoverageRegions();
294 ASSERT_EQ(1u, OutputFunctions.size());
295 OutputFunctionCoverageData &Output = OutputFunctions.back();
297 ASSERT_EQ(N, Output.Regions.size());
298 ASSERT_EQ(N, Output.Filenames.size());
300 for (unsigned I = 0; I < N; ++I) {
301 ASSERT_GT(N, Output.Regions[I].FileID);
302 ASSERT_GT(N, Output.Regions[I].LineStart);
303 EXPECT_EQ(FileNames[Output.Regions[I].LineStart],
304 Output.Filenames[Output.Regions[I].FileID]);
308 static const auto Err = [](Error E) { FAIL(); };
310 TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) {
311 ProfileWriter.addRecord({"func", 0x1234, {0}}, Err);
313 const char *FileNames[] = {"bar", "baz", "foo"};
314 static const unsigned N = array_lengthof(FileNames);
316 startFunction("func", 0x1234);
317 for (unsigned I = 0; I < N; ++I)
318 // Use LineStart to hold the index of the file name
319 // in order to preserve that information during possible sorting of CMRs.
320 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
322 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
324 for (unsigned I = 0; I < N; ++I) {
325 CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
326 ASSERT_TRUE(!Data.empty());
327 EXPECT_EQ(I, Data.begin()->Line);
331 TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) {
332 ProfileWriter.addRecord({"", 0x1234, {10}}, Err);
333 startFunction("", 0x1234);
334 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
335 EXPECT_TRUE(ErrorEquals(coveragemap_error::malformed, loadCoverageMapping()));
338 TEST_P(CoverageMappingTest, load_coverage_for_several_functions) {
339 ProfileWriter.addRecord({"func1", 0x1234, {10}}, Err);
340 ProfileWriter.addRecord({"func2", 0x2345, {20}}, Err);
342 startFunction("func1", 0x1234);
343 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
345 startFunction("func2", 0x2345);
346 addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
348 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
350 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
351 EXPECT_EQ(2, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
352 for (const auto &FunctionRecord : FunctionRecords) {
353 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
354 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
355 ASSERT_EQ(2U, Segments.size());
356 if (FunctionRecord.Name == "func1") {
357 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
358 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]);
359 } else {
360 ASSERT_EQ("func2", FunctionRecord.Name);
361 EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]);
362 EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]);
367 TEST_P(CoverageMappingTest, create_combined_regions) {
368 ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3}}, Err);
369 startFunction("func1", 0x1234);
371 // Given regions which start at the same location, emit a segment for the
372 // last region.
373 addCMR(Counter::getCounter(0), "file1", 1, 1, 2, 2);
374 addCMR(Counter::getCounter(1), "file1", 1, 1, 2, 2);
375 addCMR(Counter::getCounter(2), "file1", 1, 1, 2, 2);
377 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
378 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
379 const auto &FunctionRecord = *FunctionRecords.begin();
380 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
381 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
383 ASSERT_EQ(2U, Segments.size());
384 EXPECT_EQ(CoverageSegment(1, 1, 6, true), Segments[0]);
385 EXPECT_EQ(CoverageSegment(2, 2, false), Segments[1]);
388 TEST_P(CoverageMappingTest, skipped_segments_have_no_count) {
389 ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
390 startFunction("func1", 0x1234);
392 addCMR(Counter::getCounter(0), "file1", 1, 1, 5, 5);
393 addCMR(Counter::getCounter(0), "file1", 5, 1, 5, 5, /*Skipped=*/true);
395 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
396 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
397 const auto &FunctionRecord = *FunctionRecords.begin();
398 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
399 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
401 ASSERT_EQ(3U, Segments.size());
402 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
403 EXPECT_EQ(CoverageSegment(5, 1, true), Segments[1]);
404 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[2]);
407 TEST_P(CoverageMappingTest, multiple_regions_end_after_parent_ends) {
408 ProfileWriter.addRecord({"func1", 0x1234, {1, 0}}, Err);
409 startFunction("func1", 0x1234);
411 // 1| F{ a{
412 // 2|
413 // 3| a} b{ c{
414 // 4|
415 // 5| b}
416 // 6|
417 // 7| c} d{ e{
418 // 8|
419 // 9| d} e} F}
420 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); // < F
421 addCMR(Counter::getCounter(0), "file1", 1, 1, 3, 5); // < a
422 addCMR(Counter::getCounter(0), "file1", 3, 5, 5, 4); // < b
423 addCMR(Counter::getCounter(1), "file1", 3, 5, 7, 3); // < c
424 addCMR(Counter::getCounter(1), "file1", 7, 3, 9, 2); // < d
425 addCMR(Counter::getCounter(1), "file1", 7, 7, 9, 7); // < e
427 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
428 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
429 const auto &FunctionRecord = *FunctionRecords.begin();
430 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
431 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
433 // Old output (not sorted or unique):
434 // Segment at 1:1 with count 1
435 // Segment at 1:1 with count 1
436 // Segment at 3:5 with count 1
437 // Segment at 3:5 with count 0
438 // Segment at 3:5 with count 1
439 // Segment at 5:4 with count 0
440 // Segment at 7:3 with count 1
441 // Segment at 7:3 with count 0
442 // Segment at 7:7 with count 0
443 // Segment at 9:7 with count 0
444 // Segment at 9:2 with count 1
445 // Top level segment at 9:9
447 // New output (sorted and unique):
448 // Segment at 1:1 (count = 1), RegionEntry
449 // Segment at 3:5 (count = 1), RegionEntry
450 // Segment at 5:4 (count = 0)
451 // Segment at 7:3 (count = 0), RegionEntry
452 // Segment at 7:7 (count = 0), RegionEntry
453 // Segment at 9:2 (count = 0)
454 // Segment at 9:7 (count = 1)
455 // Segment at 9:9 (count = 0), Skipped
457 ASSERT_EQ(8U, Segments.size());
458 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
459 EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[1]);
460 EXPECT_EQ(CoverageSegment(5, 4, 0, false), Segments[2]);
461 EXPECT_EQ(CoverageSegment(7, 3, 0, true), Segments[3]);
462 EXPECT_EQ(CoverageSegment(7, 7, 0, true), Segments[4]);
463 EXPECT_EQ(CoverageSegment(9, 2, 0, false), Segments[5]);
464 EXPECT_EQ(CoverageSegment(9, 7, 1, false), Segments[6]);
465 EXPECT_EQ(CoverageSegment(9, 9, false), Segments[7]);
468 TEST_P(CoverageMappingTest, multiple_completed_segments_at_same_loc) {
469 ProfileWriter.addRecord({"func1", 0x1234, {0, 1, 2}}, Err);
470 startFunction("func1", 0x1234);
472 // PR35495
473 addCMR(Counter::getCounter(1), "file1", 2, 1, 18, 2);
474 addCMR(Counter::getCounter(0), "file1", 8, 10, 14, 6);
475 addCMR(Counter::getCounter(0), "file1", 8, 12, 14, 6);
476 addCMR(Counter::getCounter(1), "file1", 9, 1, 14, 6);
477 addCMR(Counter::getCounter(2), "file1", 11, 13, 11, 14);
479 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
480 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
481 const auto &FunctionRecord = *FunctionRecords.begin();
482 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
483 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
485 ASSERT_EQ(7U, Segments.size());
486 EXPECT_EQ(CoverageSegment(2, 1, 1, true), Segments[0]);
487 EXPECT_EQ(CoverageSegment(8, 10, 0, true), Segments[1]);
488 EXPECT_EQ(CoverageSegment(8, 12, 0, true), Segments[2]);
489 EXPECT_EQ(CoverageSegment(9, 1, 1, true), Segments[3]);
490 EXPECT_EQ(CoverageSegment(11, 13, 2, true), Segments[4]);
491 // Use count=1 (from 9:1 -> 14:6), not count=0 (from 8:12 -> 14:6).
492 EXPECT_EQ(CoverageSegment(11, 14, 1, false), Segments[5]);
493 EXPECT_EQ(CoverageSegment(18, 2, false), Segments[6]);
496 TEST_P(CoverageMappingTest, dont_emit_redundant_segments) {
497 ProfileWriter.addRecord({"func1", 0x1234, {1, 1}}, Err);
498 startFunction("func1", 0x1234);
500 addCMR(Counter::getCounter(0), "file1", 1, 1, 4, 4);
501 addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
502 addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 6);
504 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
505 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
506 const auto &FunctionRecord = *FunctionRecords.begin();
507 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
508 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
510 ASSERT_EQ(5U, Segments.size());
511 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
512 EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
513 EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
514 EXPECT_EQ(CoverageSegment(4, 4, 1, false), Segments[3]);
515 // A closing segment starting at 5:5 would be redundant: it would have the
516 // same count as the segment starting at 4:4, and has all the same metadata.
517 EXPECT_EQ(CoverageSegment(6, 6, false), Segments[4]);
520 TEST_P(CoverageMappingTest, dont_emit_closing_segment_at_new_region_start) {
521 ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
522 startFunction("func1", 0x1234);
524 addCMR(Counter::getCounter(0), "file1", 1, 1, 6, 5);
525 addCMR(Counter::getCounter(0), "file1", 2, 2, 6, 5);
526 addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 5);
527 addCMR(Counter::getCounter(0), "file1", 6, 5, 7, 7);
529 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
530 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
531 const auto &FunctionRecord = *FunctionRecords.begin();
532 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
533 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
535 ASSERT_EQ(5U, Segments.size());
536 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
537 EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
538 EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
539 EXPECT_EQ(CoverageSegment(6, 5, 1, true), Segments[3]);
540 // The old segment builder would get this wrong by emitting multiple segments
541 // which start at 6:5 (a few of which were skipped segments). We should just
542 // get a segment for the region entry.
543 EXPECT_EQ(CoverageSegment(7, 7, false), Segments[4]);
546 TEST_P(CoverageMappingTest, handle_consecutive_regions_with_zero_length) {
547 ProfileWriter.addRecord({"func1", 0x1234, {1, 2}}, Err);
548 startFunction("func1", 0x1234);
550 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
551 addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
552 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
553 addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
554 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
556 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
557 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
558 const auto &FunctionRecord = *FunctionRecords.begin();
559 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
560 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
562 ASSERT_EQ(1U, Segments.size());
563 EXPECT_EQ(CoverageSegment(1, 1, true), Segments[0]);
564 // We need to get a skipped segment starting at 1:1. In this case there is
565 // also a region entry at 1:1.
568 TEST_P(CoverageMappingTest, handle_sandwiched_zero_length_region) {
569 ProfileWriter.addRecord({"func1", 0x1234, {2, 1}}, Err);
570 startFunction("func1", 0x1234);
572 addCMR(Counter::getCounter(0), "file1", 1, 5, 4, 4);
573 addCMR(Counter::getCounter(1), "file1", 1, 9, 1, 50);
574 addCMR(Counter::getCounter(1), "file1", 2, 7, 2, 34);
575 addCMR(Counter::getCounter(1), "file1", 3, 5, 3, 21);
576 addCMR(Counter::getCounter(1), "file1", 3, 21, 3, 21);
577 addCMR(Counter::getCounter(1), "file1", 4, 12, 4, 17);
579 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
580 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
581 const auto &FunctionRecord = *FunctionRecords.begin();
582 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
583 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
585 ASSERT_EQ(10U, Segments.size());
586 EXPECT_EQ(CoverageSegment(1, 5, 2, true), Segments[0]);
587 EXPECT_EQ(CoverageSegment(1, 9, 1, true), Segments[1]);
588 EXPECT_EQ(CoverageSegment(1, 50, 2, false), Segments[2]);
589 EXPECT_EQ(CoverageSegment(2, 7, 1, true), Segments[3]);
590 EXPECT_EQ(CoverageSegment(2, 34, 2, false), Segments[4]);
591 EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[5]);
592 EXPECT_EQ(CoverageSegment(3, 21, 2, true), Segments[6]);
593 // Handle the zero-length region by creating a segment with its predecessor's
594 // count (i.e the count from 1:5 -> 4:4).
595 EXPECT_EQ(CoverageSegment(4, 4, false), Segments[7]);
596 // The area between 4:4 and 4:12 is skipped.
597 EXPECT_EQ(CoverageSegment(4, 12, 1, true), Segments[8]);
598 EXPECT_EQ(CoverageSegment(4, 17, false), Segments[9]);
601 TEST_P(CoverageMappingTest, handle_last_completed_region) {
602 ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3, 4}}, Err);
603 startFunction("func1", 0x1234);
605 addCMR(Counter::getCounter(0), "file1", 1, 1, 8, 8);
606 addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
607 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
608 addCMR(Counter::getCounter(3), "file1", 6, 6, 7, 7);
610 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
611 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
612 const auto &FunctionRecord = *FunctionRecords.begin();
613 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
614 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
616 ASSERT_EQ(8U, Segments.size());
617 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
618 EXPECT_EQ(CoverageSegment(2, 2, 2, true), Segments[1]);
619 EXPECT_EQ(CoverageSegment(3, 3, 3, true), Segments[2]);
620 EXPECT_EQ(CoverageSegment(4, 4, 2, false), Segments[3]);
621 EXPECT_EQ(CoverageSegment(5, 5, 1, false), Segments[4]);
622 EXPECT_EQ(CoverageSegment(6, 6, 4, true), Segments[5]);
623 EXPECT_EQ(CoverageSegment(7, 7, 1, false), Segments[6]);
624 EXPECT_EQ(CoverageSegment(8, 8, false), Segments[7]);
627 TEST_P(CoverageMappingTest, expansion_gets_first_counter) {
628 startFunction("func", 0x1234);
629 addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
630 // This starts earlier in "foo", so the expansion should get its counter.
631 addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
632 addExpansionCMR("bar", "foo", 3, 3, 3, 3);
634 writeAndReadCoverageRegions();
635 ASSERT_EQ(1u, OutputFunctions.size());
636 OutputFunctionCoverageData &Output = OutputFunctions.back();
638 ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind);
639 ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count);
640 ASSERT_EQ(3U, Output.Regions[2].LineStart);
643 TEST_P(CoverageMappingTest, basic_coverage_iteration) {
644 ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
646 startFunction("func", 0x1234);
647 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
648 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
649 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
650 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
651 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
653 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
654 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
655 ASSERT_EQ(7U, Segments.size());
656 ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments[0]);
657 ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
658 ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments[2]);
659 ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
660 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[4]);
661 ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
662 ASSERT_EQ(CoverageSegment(11, 11, false), Segments[6]);
665 TEST_P(CoverageMappingTest, test_line_coverage_iterator) {
666 ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
668 startFunction("func", 0x1234);
669 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
670 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
671 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
672 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
673 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
675 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
677 unsigned Line = 0;
678 unsigned LineCounts[] = {20, 20, 20, 20, 30, 10, 10, 10, 10, 0, 0};
679 for (const auto &LCS : getLineCoverageStats(Data)) {
680 ASSERT_EQ(Line + 1, LCS.getLine());
681 errs() << "Line: " << Line + 1 << ", count = " << LCS.getExecutionCount() << "\n";
682 ASSERT_EQ(LineCounts[Line], LCS.getExecutionCount());
683 ++Line;
685 ASSERT_EQ(11U, Line);
688 TEST_P(CoverageMappingTest, uncovered_function) {
689 startFunction("func", 0x1234);
690 addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
691 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
693 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
694 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
695 ASSERT_EQ(2U, Segments.size());
696 ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
697 ASSERT_EQ(CoverageSegment(3, 4, false), Segments[1]);
700 TEST_P(CoverageMappingTest, uncovered_function_with_mapping) {
701 startFunction("func", 0x1234);
702 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
703 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
704 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
706 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
707 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
708 ASSERT_EQ(3U, Segments.size());
709 ASSERT_EQ(CoverageSegment(1, 1, 0, true), Segments[0]);
710 ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
711 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[2]);
714 TEST_P(CoverageMappingTest, combine_regions) {
715 ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}, Err);
717 startFunction("func", 0x1234);
718 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
719 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
720 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
721 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
723 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
724 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
725 ASSERT_EQ(4U, Segments.size());
726 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
727 ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
728 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
729 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
732 TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) {
733 ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}, Err);
735 startFunction("func", 0x1234);
736 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
737 addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
738 addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
739 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
741 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
742 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
743 ASSERT_EQ(4U, Segments.size());
744 EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]);
745 EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]);
746 EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]);
747 EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]);
750 // If CodeRegions and ExpansionRegions cover the same area,
751 // only counts of CodeRegions should be used.
752 TEST_P(CoverageMappingTest, dont_combine_expansions) {
753 ProfileWriter.addRecord({"func", 0x1234, {10, 20}}, Err);
754 ProfileWriter.addRecord({"func", 0x1234, {0, 0}}, Err);
756 startFunction("func", 0x1234);
757 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
758 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
759 addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
760 addExpansionCMR("file1", "include1", 3, 3, 4, 4);
761 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
763 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
764 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
765 ASSERT_EQ(4U, Segments.size());
766 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
767 ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
768 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
769 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
772 // If an area is covered only by ExpansionRegions, they should be combinated.
773 TEST_P(CoverageMappingTest, combine_expansions) {
774 ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}, Err);
776 startFunction("func", 0x1234);
777 addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
778 addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
779 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
780 addExpansionCMR("file", "include1", 3, 1, 3, 5);
781 addExpansionCMR("file", "include2", 3, 1, 3, 5);
783 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
785 CoverageData Data = LoadedCoverage->getCoverageForFile("file");
786 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
787 ASSERT_EQ(4U, Segments.size());
788 EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]);
789 EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]);
790 EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]);
791 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]);
794 TEST_P(CoverageMappingTest, strip_filename_prefix) {
795 ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err);
797 startFunction("file1:func", 0x1234);
798 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
799 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
801 std::vector<std::string> Names;
802 for (const auto &Func : LoadedCoverage->getCoveredFunctions())
803 Names.push_back(Func.Name);
804 ASSERT_EQ(1U, Names.size());
805 ASSERT_EQ("func", Names[0]);
808 TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) {
809 ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}, Err);
811 startFunction("<unknown>:func", 0x1234);
812 addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
813 EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
815 std::vector<std::string> Names;
816 for (const auto &Func : LoadedCoverage->getCoveredFunctions())
817 Names.push_back(Func.Name);
818 ASSERT_EQ(1U, Names.size());
819 ASSERT_EQ("func", Names[0]);
822 TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
823 ProfileWriter.addRecord({"foo", 0x1234, {10}}, Err);
824 ProfileWriter.addRecord({"bar", 0x2345, {20}}, Err);
826 startFunction("foo", 0x1234);
827 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
828 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
830 startFunction("bar", 0x2345);
831 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
832 addExpansionCMR("main", "expanded", 9, 1, 9, 5);
834 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
836 std::vector<InstantiationGroup> InstantiationGroups =
837 LoadedCoverage->getInstantiationGroups("expanded");
838 for (const auto &Group : InstantiationGroups)
839 ASSERT_EQ(Group.size(), 1U);
842 TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {
843 ProfileWriter.addRecord({"func", 0x1234, {10}}, Err);
845 startFunction("func", 0x1234);
846 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
847 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
849 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
851 CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
852 std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
853 ASSERT_EQ(2U, Segments.size());
854 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
855 EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]);
858 TEST_P(CoverageMappingTest, skip_duplicate_function_record) {
859 ProfileWriter.addRecord({"func", 0x1234, {1}}, Err);
861 // This record should be loaded.
862 startFunction("func", 0x1234);
863 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
865 // This record should be loaded.
866 startFunction("func", 0x1234);
867 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
868 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
870 // This record should be skipped.
871 startFunction("func", 0x1234);
872 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
874 // This record should be loaded.
875 startFunction("func", 0x1234);
876 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
877 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
879 // This record should be skipped.
880 startFunction("func", 0x1234);
881 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
882 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
884 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
886 auto Funcs = LoadedCoverage->getCoveredFunctions();
887 unsigned NumFuncs = std::distance(Funcs.begin(), Funcs.end());
888 ASSERT_EQ(3U, NumFuncs);
891 // FIXME: Use ::testing::Combine() when llvm updates its copy of googletest.
892 INSTANTIATE_TEST_CASE_P(ParameterizedCovMapTest, CoverageMappingTest,
893 ::testing::Values(std::pair<bool, bool>({false, false}),
894 std::pair<bool, bool>({false, true}),
895 std::pair<bool, bool>({true, false}),
896 std::pair<bool, bool>({true, true})),);
898 } // end anonymous namespace