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 //===----------------------------------------------------------------------===// 8 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" 18 19 #include <ostream> 20 #include <utility> 21 22 using namespace llvm; 23 using namespace coverage; 24 25 [[nodiscard]] static ::testing::AssertionResult 26 ErrorEquals(Error E, coveragemap_error Expected_Err, 27 const std::string &Expected_Msg = std::string()) { 28 coveragemap_error Found; 29 std::string Msg; 30 std::string FoundMsg; 31 handleAllErrors(std::move(E), [&](const CoverageMapError &CME) { 32 Found = CME.get(); 33 Msg = CME.getMessage(); 34 FoundMsg = CME.message(); 35 }); 36 if (Expected_Err == Found && Msg == Expected_Msg) 37 return ::testing::AssertionSuccess(); 38 return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n"; 39 } 40 41 namespace llvm { 42 namespace coverage { 43 void PrintTo(const Counter &C, ::std::ostream *os) { 44 if (C.isZero()) 45 *os << "Zero"; 46 else if (C.isExpression()) 47 *os << "Expression " << C.getExpressionID(); 48 else 49 *os << "Counter " << C.getCounterID(); 50 } 51 52 void PrintTo(const CoverageSegment &S, ::std::ostream *os) { 53 *os << "CoverageSegment(" << S.Line << ", " << S.Col << ", "; 54 if (S.HasCount) 55 *os << S.Count << ", "; 56 *os << (S.IsRegionEntry ? "true" : "false") << ")"; 57 } 58 } 59 } 60 61 namespace { 62 63 struct OutputFunctionCoverageData { 64 StringRef Name; 65 uint64_t Hash; 66 std::vector<StringRef> Filenames; 67 std::vector<CounterMappingRegion> Regions; 68 std::vector<CounterExpression> Expressions; 69 70 OutputFunctionCoverageData() : Hash(0) {} 71 72 OutputFunctionCoverageData(OutputFunctionCoverageData &&OFCD) 73 : Name(OFCD.Name), Hash(OFCD.Hash), Filenames(std::move(OFCD.Filenames)), 74 Regions(std::move(OFCD.Regions)) {} 75 76 OutputFunctionCoverageData(const OutputFunctionCoverageData &) = delete; 77 OutputFunctionCoverageData & 78 operator=(const OutputFunctionCoverageData &) = delete; 79 OutputFunctionCoverageData &operator=(OutputFunctionCoverageData &&) = delete; 80 81 void fillCoverageMappingRecord(CoverageMappingRecord &Record) const { 82 Record.FunctionName = Name; 83 Record.FunctionHash = Hash; 84 Record.Filenames = Filenames; 85 Record.Expressions = Expressions; 86 Record.MappingRegions = Regions; 87 } 88 }; 89 90 struct CoverageMappingReaderMock : CoverageMappingReader { 91 ArrayRef<OutputFunctionCoverageData> Functions; 92 93 CoverageMappingReaderMock(ArrayRef<OutputFunctionCoverageData> Functions) 94 : Functions(Functions) {} 95 96 Error readNextRecord(CoverageMappingRecord &Record) override { 97 if (Functions.empty()) 98 return make_error<CoverageMapError>(coveragemap_error::eof); 99 100 Functions.front().fillCoverageMappingRecord(Record); 101 Functions = Functions.slice(1); 102 103 return Error::success(); 104 } 105 }; 106 107 struct InputFunctionCoverageData { 108 // Maps the global file index from CoverageMappingTest.Files 109 // to the index of that file within this function. We can't just use 110 // global file indexes here because local indexes have to be dense. 111 // This map is used during serialization to create the virtual file mapping 112 // (from local fileId to global Index) in the head of the per-function 113 // coverage mapping data. 114 SmallDenseMap<unsigned, unsigned> ReverseVirtualFileMapping; 115 std::string Name; 116 uint64_t Hash; 117 std::vector<CounterMappingRegion> Regions; 118 std::vector<CounterExpression> Expressions; 119 120 InputFunctionCoverageData(std::string Name, uint64_t Hash) 121 : Name(std::move(Name)), Hash(Hash) {} 122 123 InputFunctionCoverageData(InputFunctionCoverageData &&IFCD) 124 : ReverseVirtualFileMapping(std::move(IFCD.ReverseVirtualFileMapping)), 125 Name(std::move(IFCD.Name)), Hash(IFCD.Hash), 126 Regions(std::move(IFCD.Regions)) {} 127 128 InputFunctionCoverageData(const InputFunctionCoverageData &) = delete; 129 InputFunctionCoverageData & 130 operator=(const InputFunctionCoverageData &) = delete; 131 InputFunctionCoverageData &operator=(InputFunctionCoverageData &&) = delete; 132 }; 133 134 struct CoverageMappingTest : ::testing::TestWithParam<std::tuple<bool, bool>> { 135 bool UseMultipleReaders; 136 StringMap<unsigned> Files; 137 std::vector<std::string> Filenames; 138 std::vector<InputFunctionCoverageData> InputFunctions; 139 std::vector<OutputFunctionCoverageData> OutputFunctions; 140 141 InstrProfWriter ProfileWriter; 142 std::unique_ptr<IndexedInstrProfReader> ProfileReader; 143 144 std::unique_ptr<CoverageMapping> LoadedCoverage; 145 146 void SetUp() override { 147 ProfileWriter.setOutputSparse(std::get<0>(GetParam())); 148 UseMultipleReaders = std::get<1>(GetParam()); 149 } 150 151 unsigned getGlobalFileIndex(StringRef Name) { 152 auto R = Files.find(Name); 153 if (R != Files.end()) 154 return R->second; 155 unsigned Index = Files.size() + 1; 156 Files.try_emplace(Name, Index); 157 return Index; 158 } 159 160 // Return the file index of file 'Name' for the current function. 161 // Add the file into the global map if necessary. 162 // See also InputFunctionCoverageData::ReverseVirtualFileMapping 163 // for additional comments. 164 unsigned getFileIndexForFunction(StringRef Name) { 165 unsigned GlobalIndex = getGlobalFileIndex(Name); 166 auto &CurrentFunctionFileMapping = 167 InputFunctions.back().ReverseVirtualFileMapping; 168 auto R = CurrentFunctionFileMapping.find(GlobalIndex); 169 if (R != CurrentFunctionFileMapping.end()) 170 return R->second; 171 unsigned IndexInFunction = CurrentFunctionFileMapping.size(); 172 CurrentFunctionFileMapping.insert( 173 std::make_pair(GlobalIndex, IndexInFunction)); 174 return IndexInFunction; 175 } 176 177 void startFunction(StringRef FuncName, uint64_t Hash) { 178 InputFunctions.emplace_back(FuncName.str(), Hash); 179 } 180 181 void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE, 182 unsigned CE, bool Skipped = false) { 183 auto &Regions = InputFunctions.back().Regions; 184 unsigned FileID = getFileIndexForFunction(File); 185 Regions.push_back( 186 Skipped ? CounterMappingRegion::makeSkipped(FileID, LS, CS, LE, CE) 187 : CounterMappingRegion::makeRegion(C, FileID, LS, CS, LE, CE)); 188 } 189 190 void addSkipped(StringRef File, unsigned LS, unsigned CS, unsigned LE, 191 unsigned CE) { 192 addCMR(Counter::getZero(), File, LS, CS, LE, CE, true); 193 } 194 195 void addMCDCDecisionCMR(unsigned Mask, uint16_t NC, StringRef File, 196 unsigned LS, unsigned CS, unsigned LE, unsigned CE) { 197 auto &Regions = InputFunctions.back().Regions; 198 unsigned FileID = getFileIndexForFunction(File); 199 Regions.push_back(CounterMappingRegion::makeDecisionRegion( 200 mcdc::DecisionParameters{Mask, NC}, FileID, LS, CS, LE, CE)); 201 } 202 203 void addMCDCBranchCMR(Counter C1, Counter C2, mcdc::ConditionID ID, 204 mcdc::ConditionIDs Conds, StringRef File, unsigned LS, 205 unsigned CS, unsigned LE, unsigned CE) { 206 auto &Regions = InputFunctions.back().Regions; 207 unsigned FileID = getFileIndexForFunction(File); 208 Regions.push_back(CounterMappingRegion::makeBranchRegion( 209 C1, C2, FileID, LS, CS, LE, CE, mcdc::BranchParameters{ID, Conds})); 210 } 211 212 void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS, 213 unsigned CS, unsigned LE, unsigned CE) { 214 InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion( 215 getFileIndexForFunction(File), getFileIndexForFunction(ExpandedFile), 216 LS, CS, LE, CE)); 217 } 218 219 void addExpression(CounterExpression CE) { 220 InputFunctions.back().Expressions.push_back(CE); 221 } 222 223 std::string writeCoverageRegions(InputFunctionCoverageData &Data) { 224 SmallVector<unsigned, 8> FileIDs(Data.ReverseVirtualFileMapping.size()); 225 for (const auto &E : Data.ReverseVirtualFileMapping) 226 FileIDs[E.second] = E.first; 227 std::string Coverage; 228 llvm::raw_string_ostream OS(Coverage); 229 CoverageMappingWriter(FileIDs, Data.Expressions, Data.Regions).write(OS); 230 return OS.str(); 231 } 232 233 void readCoverageRegions(const std::string &Coverage, 234 OutputFunctionCoverageData &Data) { 235 // We will re-use the StringRef in duplicate tests, clear it to avoid 236 // clobber previous ones. 237 Filenames.clear(); 238 Filenames.resize(Files.size() + 1); 239 for (const auto &E : Files) 240 Filenames[E.getValue()] = E.getKey().str(); 241 ArrayRef<std::string> FilenameRefs = llvm::ArrayRef(Filenames); 242 RawCoverageMappingReader Reader(Coverage, FilenameRefs, Data.Filenames, 243 Data.Expressions, Data.Regions); 244 EXPECT_THAT_ERROR(Reader.read(), Succeeded()); 245 } 246 247 void writeAndReadCoverageRegions(bool EmitFilenames = true) { 248 OutputFunctions.resize(InputFunctions.size()); 249 for (unsigned I = 0; I < InputFunctions.size(); ++I) { 250 std::string Regions = writeCoverageRegions(InputFunctions[I]); 251 readCoverageRegions(Regions, OutputFunctions[I]); 252 OutputFunctions[I].Name = InputFunctions[I].Name; 253 OutputFunctions[I].Hash = InputFunctions[I].Hash; 254 if (!EmitFilenames) 255 OutputFunctions[I].Filenames.clear(); 256 } 257 } 258 259 void readProfCounts() { 260 auto Profile = ProfileWriter.writeBuffer(); 261 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); 262 EXPECT_THAT_ERROR(ReaderOrErr.takeError(), Succeeded()); 263 ProfileReader = std::move(ReaderOrErr.get()); 264 } 265 266 Expected<std::unique_ptr<CoverageMapping>> readOutputFunctions() { 267 std::vector<std::unique_ptr<CoverageMappingReader>> CoverageReaders; 268 if (UseMultipleReaders) { 269 for (const auto &OF : OutputFunctions) { 270 ArrayRef<OutputFunctionCoverageData> Funcs(OF); 271 CoverageReaders.push_back( 272 std::make_unique<CoverageMappingReaderMock>(Funcs)); 273 } 274 } else { 275 ArrayRef<OutputFunctionCoverageData> Funcs(OutputFunctions); 276 CoverageReaders.push_back( 277 std::make_unique<CoverageMappingReaderMock>(Funcs)); 278 } 279 return CoverageMapping::load(CoverageReaders, *ProfileReader); 280 } 281 282 Error loadCoverageMapping(bool EmitFilenames = true) { 283 readProfCounts(); 284 writeAndReadCoverageRegions(EmitFilenames); 285 auto CoverageOrErr = readOutputFunctions(); 286 if (!CoverageOrErr) 287 return CoverageOrErr.takeError(); 288 LoadedCoverage = std::move(CoverageOrErr.get()); 289 return Error::success(); 290 } 291 }; 292 293 TEST_P(CoverageMappingTest, basic_write_read) { 294 startFunction("func", 0x1234); 295 addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1); 296 addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2); 297 addCMR(Counter::getZero(), "foo", 3, 1, 3, 4); 298 addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8); 299 addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4); 300 301 writeAndReadCoverageRegions(); 302 ASSERT_EQ(1u, InputFunctions.size()); 303 ASSERT_EQ(1u, OutputFunctions.size()); 304 InputFunctionCoverageData &Input = InputFunctions.back(); 305 OutputFunctionCoverageData &Output = OutputFunctions.back(); 306 307 size_t N = ArrayRef(Input.Regions).size(); 308 ASSERT_EQ(N, Output.Regions.size()); 309 for (size_t I = 0; I < N; ++I) { 310 ASSERT_EQ(Input.Regions[I].Count, Output.Regions[I].Count); 311 ASSERT_EQ(Input.Regions[I].FileID, Output.Regions[I].FileID); 312 ASSERT_EQ(Input.Regions[I].startLoc(), Output.Regions[I].startLoc()); 313 ASSERT_EQ(Input.Regions[I].endLoc(), Output.Regions[I].endLoc()); 314 ASSERT_EQ(Input.Regions[I].Kind, Output.Regions[I].Kind); 315 } 316 } 317 318 TEST_P(CoverageMappingTest, correct_deserialize_for_more_than_two_files) { 319 const char *FileNames[] = {"bar", "baz", "foo"}; 320 static const unsigned N = std::size(FileNames); 321 322 startFunction("func", 0x1234); 323 for (unsigned I = 0; I < N; ++I) 324 // Use LineStart to hold the index of the file name 325 // in order to preserve that information during possible sorting of CMRs. 326 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1); 327 328 writeAndReadCoverageRegions(); 329 ASSERT_EQ(1u, OutputFunctions.size()); 330 OutputFunctionCoverageData &Output = OutputFunctions.back(); 331 332 ASSERT_EQ(N, Output.Regions.size()); 333 ASSERT_EQ(N, Output.Filenames.size()); 334 335 for (unsigned I = 0; I < N; ++I) { 336 ASSERT_GT(N, Output.Regions[I].FileID); 337 ASSERT_GT(N, Output.Regions[I].LineStart); 338 EXPECT_EQ(FileNames[Output.Regions[I].LineStart], 339 Output.Filenames[Output.Regions[I].FileID]); 340 } 341 } 342 343 static const auto Err = [](Error E) { FAIL(); }; 344 345 TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) { 346 ProfileWriter.addRecord({"func", 0x1234, {0}}, Err); 347 348 const char *FileNames[] = {"bar", "baz", "foo"}; 349 static const unsigned N = std::size(FileNames); 350 351 startFunction("func", 0x1234); 352 for (unsigned I = 0; I < N; ++I) 353 // Use LineStart to hold the index of the file name 354 // in order to preserve that information during possible sorting of CMRs. 355 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1); 356 357 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 358 359 for (unsigned I = 0; I < N; ++I) { 360 CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]); 361 ASSERT_TRUE(!Data.empty()); 362 EXPECT_EQ(I, Data.begin()->Line); 363 } 364 } 365 366 TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) { 367 ProfileWriter.addRecord({"", 0x1234, {10}}, Err); 368 startFunction("", 0x1234); 369 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5); 370 EXPECT_TRUE(ErrorEquals(loadCoverageMapping(), coveragemap_error::malformed, 371 "record function name is empty")); 372 } 373 374 TEST_P(CoverageMappingTest, load_coverage_for_several_functions) { 375 ProfileWriter.addRecord({"func1", 0x1234, {10}}, Err); 376 ProfileWriter.addRecord({"func2", 0x2345, {20}}, Err); 377 378 startFunction("func1", 0x1234); 379 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5); 380 381 startFunction("func2", 0x2345); 382 addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6); 383 384 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 385 386 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 387 EXPECT_EQ(2, std::distance(FunctionRecords.begin(), FunctionRecords.end())); 388 for (const auto &FunctionRecord : FunctionRecords) { 389 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 390 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 391 ASSERT_EQ(2U, Segments.size()); 392 if (FunctionRecord.Name == "func1") { 393 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]); 394 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]); 395 } else { 396 ASSERT_EQ("func2", FunctionRecord.Name); 397 EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]); 398 EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]); 399 } 400 } 401 } 402 403 TEST_P(CoverageMappingTest, create_combined_regions) { 404 ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3}}, Err); 405 startFunction("func1", 0x1234); 406 407 // Given regions which start at the same location, emit a segment for the 408 // last region. 409 addCMR(Counter::getCounter(0), "file1", 1, 1, 2, 2); 410 addCMR(Counter::getCounter(1), "file1", 1, 1, 2, 2); 411 addCMR(Counter::getCounter(2), "file1", 1, 1, 2, 2); 412 413 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 414 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 415 const auto &FunctionRecord = *FunctionRecords.begin(); 416 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 417 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 418 419 ASSERT_EQ(2U, Segments.size()); 420 EXPECT_EQ(CoverageSegment(1, 1, 6, true), Segments[0]); 421 EXPECT_EQ(CoverageSegment(2, 2, false), Segments[1]); 422 } 423 424 TEST_P(CoverageMappingTest, skipped_segments_have_no_count) { 425 ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err); 426 startFunction("func1", 0x1234); 427 428 addCMR(Counter::getCounter(0), "file1", 1, 1, 5, 5); 429 addCMR(Counter::getCounter(0), "file1", 5, 1, 5, 5, /*Skipped=*/true); 430 431 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 432 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 433 const auto &FunctionRecord = *FunctionRecords.begin(); 434 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 435 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 436 437 ASSERT_EQ(3U, Segments.size()); 438 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]); 439 EXPECT_EQ(CoverageSegment(5, 1, true), Segments[1]); 440 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[2]); 441 } 442 443 TEST_P(CoverageMappingTest, multiple_regions_end_after_parent_ends) { 444 ProfileWriter.addRecord({"func1", 0x1234, {1, 0}}, Err); 445 startFunction("func1", 0x1234); 446 447 // 1| F{ a{ 448 // 2| 449 // 3| a} b{ c{ 450 // 4| 451 // 5| b} 452 // 6| 453 // 7| c} d{ e{ 454 // 8| 455 // 9| d} e} F} 456 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); // < F 457 addCMR(Counter::getCounter(0), "file1", 1, 1, 3, 5); // < a 458 addCMR(Counter::getCounter(0), "file1", 3, 5, 5, 4); // < b 459 addCMR(Counter::getCounter(1), "file1", 3, 5, 7, 3); // < c 460 addCMR(Counter::getCounter(1), "file1", 7, 3, 9, 2); // < d 461 addCMR(Counter::getCounter(1), "file1", 7, 7, 9, 7); // < e 462 463 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 464 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 465 const auto &FunctionRecord = *FunctionRecords.begin(); 466 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 467 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 468 469 // Old output (not sorted or unique): 470 // Segment at 1:1 with count 1 471 // Segment at 1:1 with count 1 472 // Segment at 3:5 with count 1 473 // Segment at 3:5 with count 0 474 // Segment at 3:5 with count 1 475 // Segment at 5:4 with count 0 476 // Segment at 7:3 with count 1 477 // Segment at 7:3 with count 0 478 // Segment at 7:7 with count 0 479 // Segment at 9:7 with count 0 480 // Segment at 9:2 with count 1 481 // Top level segment at 9:9 482 483 // New output (sorted and unique): 484 // Segment at 1:1 (count = 1), RegionEntry 485 // Segment at 3:5 (count = 1), RegionEntry 486 // Segment at 5:4 (count = 0) 487 // Segment at 7:3 (count = 0), RegionEntry 488 // Segment at 7:7 (count = 0), RegionEntry 489 // Segment at 9:2 (count = 0) 490 // Segment at 9:7 (count = 1) 491 // Segment at 9:9 (count = 0), Skipped 492 493 ASSERT_EQ(8U, Segments.size()); 494 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]); 495 EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[1]); 496 EXPECT_EQ(CoverageSegment(5, 4, 0, false), Segments[2]); 497 EXPECT_EQ(CoverageSegment(7, 3, 0, true), Segments[3]); 498 EXPECT_EQ(CoverageSegment(7, 7, 0, true), Segments[4]); 499 EXPECT_EQ(CoverageSegment(9, 2, 0, false), Segments[5]); 500 EXPECT_EQ(CoverageSegment(9, 7, 1, false), Segments[6]); 501 EXPECT_EQ(CoverageSegment(9, 9, false), Segments[7]); 502 } 503 504 TEST_P(CoverageMappingTest, multiple_completed_segments_at_same_loc) { 505 ProfileWriter.addRecord({"func1", 0x1234, {0, 1, 2}}, Err); 506 startFunction("func1", 0x1234); 507 508 // PR35495 509 addCMR(Counter::getCounter(1), "file1", 2, 1, 18, 2); 510 addCMR(Counter::getCounter(0), "file1", 8, 10, 14, 6); 511 addCMR(Counter::getCounter(0), "file1", 8, 12, 14, 6); 512 addCMR(Counter::getCounter(1), "file1", 9, 1, 14, 6); 513 addCMR(Counter::getCounter(2), "file1", 11, 13, 11, 14); 514 515 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 516 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 517 const auto &FunctionRecord = *FunctionRecords.begin(); 518 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 519 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 520 521 ASSERT_EQ(7U, Segments.size()); 522 EXPECT_EQ(CoverageSegment(2, 1, 1, true), Segments[0]); 523 EXPECT_EQ(CoverageSegment(8, 10, 0, true), Segments[1]); 524 EXPECT_EQ(CoverageSegment(8, 12, 0, true), Segments[2]); 525 EXPECT_EQ(CoverageSegment(9, 1, 1, true), Segments[3]); 526 EXPECT_EQ(CoverageSegment(11, 13, 2, true), Segments[4]); 527 // Use count=1 (from 9:1 -> 14:6), not count=0 (from 8:12 -> 14:6). 528 EXPECT_EQ(CoverageSegment(11, 14, 1, false), Segments[5]); 529 EXPECT_EQ(CoverageSegment(18, 2, false), Segments[6]); 530 } 531 532 TEST_P(CoverageMappingTest, dont_emit_redundant_segments) { 533 ProfileWriter.addRecord({"func1", 0x1234, {1, 1}}, Err); 534 startFunction("func1", 0x1234); 535 536 addCMR(Counter::getCounter(0), "file1", 1, 1, 4, 4); 537 addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5); 538 addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 6); 539 540 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 541 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 542 const auto &FunctionRecord = *FunctionRecords.begin(); 543 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 544 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 545 546 ASSERT_EQ(5U, Segments.size()); 547 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]); 548 EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]); 549 EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]); 550 EXPECT_EQ(CoverageSegment(4, 4, 1, false), Segments[3]); 551 // A closing segment starting at 5:5 would be redundant: it would have the 552 // same count as the segment starting at 4:4, and has all the same metadata. 553 EXPECT_EQ(CoverageSegment(6, 6, false), Segments[4]); 554 } 555 556 TEST_P(CoverageMappingTest, dont_emit_closing_segment_at_new_region_start) { 557 ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err); 558 startFunction("func1", 0x1234); 559 560 addCMR(Counter::getCounter(0), "file1", 1, 1, 6, 5); 561 addCMR(Counter::getCounter(0), "file1", 2, 2, 6, 5); 562 addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 5); 563 addCMR(Counter::getCounter(0), "file1", 6, 5, 7, 7); 564 565 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 566 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 567 const auto &FunctionRecord = *FunctionRecords.begin(); 568 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 569 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 570 571 ASSERT_EQ(5U, Segments.size()); 572 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]); 573 EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]); 574 EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]); 575 EXPECT_EQ(CoverageSegment(6, 5, 1, true), Segments[3]); 576 // The old segment builder would get this wrong by emitting multiple segments 577 // which start at 6:5 (a few of which were skipped segments). We should just 578 // get a segment for the region entry. 579 EXPECT_EQ(CoverageSegment(7, 7, false), Segments[4]); 580 } 581 582 TEST_P(CoverageMappingTest, handle_consecutive_regions_with_zero_length) { 583 ProfileWriter.addRecord({"func1", 0x1234, {1, 2}}, Err); 584 startFunction("func1", 0x1234); 585 586 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1); 587 addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1); 588 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1); 589 addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1); 590 addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1); 591 592 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 593 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 594 const auto &FunctionRecord = *FunctionRecords.begin(); 595 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 596 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 597 598 ASSERT_EQ(1U, Segments.size()); 599 EXPECT_EQ(CoverageSegment(1, 1, true), Segments[0]); 600 // We need to get a skipped segment starting at 1:1. In this case there is 601 // also a region entry at 1:1. 602 } 603 604 TEST_P(CoverageMappingTest, handle_sandwiched_zero_length_region) { 605 ProfileWriter.addRecord({"func1", 0x1234, {2, 1}}, Err); 606 startFunction("func1", 0x1234); 607 608 addCMR(Counter::getCounter(0), "file1", 1, 5, 4, 4); 609 addCMR(Counter::getCounter(1), "file1", 1, 9, 1, 50); 610 addCMR(Counter::getCounter(1), "file1", 2, 7, 2, 34); 611 addCMR(Counter::getCounter(1), "file1", 3, 5, 3, 21); 612 addCMR(Counter::getCounter(1), "file1", 3, 21, 3, 21); 613 addCMR(Counter::getCounter(1), "file1", 4, 12, 4, 17); 614 615 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 616 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 617 const auto &FunctionRecord = *FunctionRecords.begin(); 618 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 619 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 620 621 ASSERT_EQ(10U, Segments.size()); 622 EXPECT_EQ(CoverageSegment(1, 5, 2, true), Segments[0]); 623 EXPECT_EQ(CoverageSegment(1, 9, 1, true), Segments[1]); 624 EXPECT_EQ(CoverageSegment(1, 50, 2, false), Segments[2]); 625 EXPECT_EQ(CoverageSegment(2, 7, 1, true), Segments[3]); 626 EXPECT_EQ(CoverageSegment(2, 34, 2, false), Segments[4]); 627 EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[5]); 628 EXPECT_EQ(CoverageSegment(3, 21, 2, true), Segments[6]); 629 // Handle the zero-length region by creating a segment with its predecessor's 630 // count (i.e the count from 1:5 -> 4:4). 631 EXPECT_EQ(CoverageSegment(4, 4, false), Segments[7]); 632 // The area between 4:4 and 4:12 is skipped. 633 EXPECT_EQ(CoverageSegment(4, 12, 1, true), Segments[8]); 634 EXPECT_EQ(CoverageSegment(4, 17, false), Segments[9]); 635 } 636 637 TEST_P(CoverageMappingTest, handle_last_completed_region) { 638 ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3, 4}}, Err); 639 startFunction("func1", 0x1234); 640 641 addCMR(Counter::getCounter(0), "file1", 1, 1, 8, 8); 642 addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5); 643 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4); 644 addCMR(Counter::getCounter(3), "file1", 6, 6, 7, 7); 645 646 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 647 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 648 const auto &FunctionRecord = *FunctionRecords.begin(); 649 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 650 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 651 652 ASSERT_EQ(8U, Segments.size()); 653 EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]); 654 EXPECT_EQ(CoverageSegment(2, 2, 2, true), Segments[1]); 655 EXPECT_EQ(CoverageSegment(3, 3, 3, true), Segments[2]); 656 EXPECT_EQ(CoverageSegment(4, 4, 2, false), Segments[3]); 657 EXPECT_EQ(CoverageSegment(5, 5, 1, false), Segments[4]); 658 EXPECT_EQ(CoverageSegment(6, 6, 4, true), Segments[5]); 659 EXPECT_EQ(CoverageSegment(7, 7, 1, false), Segments[6]); 660 EXPECT_EQ(CoverageSegment(8, 8, false), Segments[7]); 661 } 662 663 TEST_P(CoverageMappingTest, expansion_gets_first_counter) { 664 startFunction("func", 0x1234); 665 addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2); 666 // This starts earlier in "foo", so the expansion should get its counter. 667 addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1); 668 addExpansionCMR("bar", "foo", 3, 3, 3, 3); 669 670 writeAndReadCoverageRegions(); 671 ASSERT_EQ(1u, OutputFunctions.size()); 672 OutputFunctionCoverageData &Output = OutputFunctions.back(); 673 674 ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind); 675 ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count); 676 ASSERT_EQ(3U, Output.Regions[2].LineStart); 677 } 678 679 TEST_P(CoverageMappingTest, basic_coverage_iteration) { 680 ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err); 681 682 startFunction("func", 0x1234); 683 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 684 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7); 685 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1); 686 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11); 687 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 688 689 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 690 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 691 ASSERT_EQ(7U, Segments.size()); 692 ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments[0]); 693 ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]); 694 ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments[2]); 695 ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]); 696 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[4]); 697 ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]); 698 ASSERT_EQ(CoverageSegment(11, 11, false), Segments[6]); 699 } 700 701 TEST_P(CoverageMappingTest, test_line_coverage_iterator) { 702 ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err); 703 704 startFunction("func", 0x1234); 705 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 706 addSkipped("file1", 1, 3, 1, 8); // skipped region inside previous block 707 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7); 708 addSkipped("file1", 4, 1, 4, 20); // skipped line 709 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1); 710 addSkipped("file1", 10, 1, 12, 711 20); // skipped region which contains next region 712 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11); 713 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 714 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 715 716 unsigned Line = 0; 717 const unsigned LineCounts[] = {20, 20, 20, 0, 30, 10, 10, 10, 10, 0, 0, 0, 0}; 718 const bool MappedLines[] = {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0}; 719 ASSERT_EQ(std::size(LineCounts), std::size(MappedLines)); 720 721 for (const auto &LCS : getLineCoverageStats(Data)) { 722 ASSERT_LT(Line, std::size(LineCounts)); 723 ASSERT_LT(Line, std::size(MappedLines)); 724 725 ASSERT_EQ(Line + 1, LCS.getLine()); 726 errs() << "Line: " << Line + 1 << ", count = " << LCS.getExecutionCount() 727 << ", mapped = " << LCS.isMapped() << "\n"; 728 ASSERT_EQ(LineCounts[Line], LCS.getExecutionCount()); 729 ASSERT_EQ(MappedLines[Line], LCS.isMapped()); 730 ++Line; 731 } 732 ASSERT_EQ(12U, Line); 733 734 // Check that operator->() works / compiles. 735 ASSERT_EQ(1U, LineCoverageIterator(Data)->getLine()); 736 } 737 738 TEST_P(CoverageMappingTest, uncovered_function) { 739 startFunction("func", 0x1234); 740 addCMR(Counter::getZero(), "file1", 1, 2, 3, 4); 741 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 742 743 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 744 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 745 ASSERT_EQ(2U, Segments.size()); 746 ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]); 747 ASSERT_EQ(CoverageSegment(3, 4, false), Segments[1]); 748 } 749 750 TEST_P(CoverageMappingTest, uncovered_function_with_mapping) { 751 startFunction("func", 0x1234); 752 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 753 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7); 754 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 755 756 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 757 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 758 ASSERT_EQ(3U, Segments.size()); 759 ASSERT_EQ(CoverageSegment(1, 1, 0, true), Segments[0]); 760 ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]); 761 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[2]); 762 } 763 764 TEST_P(CoverageMappingTest, combine_regions) { 765 ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}, Err); 766 767 startFunction("func", 0x1234); 768 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 769 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4); 770 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4); 771 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 772 773 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 774 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 775 ASSERT_EQ(4U, Segments.size()); 776 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]); 777 ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]); 778 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]); 779 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]); 780 } 781 782 TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) { 783 ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}, Err); 784 785 startFunction("func", 0x1234); 786 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 787 addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9); 788 addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5); 789 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 790 791 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 792 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 793 ASSERT_EQ(4U, Segments.size()); 794 EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]); 795 EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]); 796 EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]); 797 EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]); 798 } 799 800 // If CodeRegions and ExpansionRegions cover the same area, 801 // only counts of CodeRegions should be used. 802 TEST_P(CoverageMappingTest, dont_combine_expansions) { 803 ProfileWriter.addRecord({"func", 0x1234, {10, 20}}, Err); 804 ProfileWriter.addRecord({"func", 0x1234, {0, 0}}, Err); 805 806 startFunction("func", 0x1234); 807 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 808 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4); 809 addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7); 810 addExpansionCMR("file1", "include1", 3, 3, 4, 4); 811 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 812 813 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 814 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 815 ASSERT_EQ(4U, Segments.size()); 816 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]); 817 ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]); 818 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]); 819 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]); 820 } 821 822 // If an area is covered only by ExpansionRegions, they should be combinated. 823 TEST_P(CoverageMappingTest, combine_expansions) { 824 ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}, Err); 825 826 startFunction("func", 0x1234); 827 addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10); 828 addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10); 829 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5); 830 addExpansionCMR("file", "include1", 3, 1, 3, 5); 831 addExpansionCMR("file", "include2", 3, 1, 3, 5); 832 833 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 834 835 CoverageData Data = LoadedCoverage->getCoverageForFile("file"); 836 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 837 ASSERT_EQ(4U, Segments.size()); 838 EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]); 839 EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]); 840 EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]); 841 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]); 842 } 843 844 // Test that counters not associated with any code regions are allowed. 845 TEST_P(CoverageMappingTest, non_code_region_counters) { 846 // No records in profdata 847 848 startFunction("func", 0x1234); 849 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5); 850 addCMR(Counter::getExpression(0), "file", 6, 1, 6, 5); 851 addExpression(CounterExpression( 852 CounterExpression::Add, Counter::getCounter(1), Counter::getCounter(2))); 853 854 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 855 856 std::vector<std::string> Names; 857 for (const auto &Func : LoadedCoverage->getCoveredFunctions()) { 858 Names.push_back(Func.Name); 859 ASSERT_EQ(2U, Func.CountedRegions.size()); 860 } 861 ASSERT_EQ(1U, Names.size()); 862 } 863 864 // Test that MCDC bitmasks not associated with any code regions are allowed. 865 TEST_P(CoverageMappingTest, non_code_region_bitmask) { 866 // No records in profdata 867 868 startFunction("func", 0x1234); 869 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5); 870 addCMR(Counter::getCounter(1), "file", 1, 1, 5, 5); 871 addCMR(Counter::getCounter(2), "file", 1, 1, 5, 5); 872 addCMR(Counter::getCounter(3), "file", 1, 1, 5, 5); 873 874 addMCDCDecisionCMR(0, 2, "file", 7, 1, 7, 6); 875 addMCDCBranchCMR(Counter::getCounter(0), Counter::getCounter(1), 0, {-1, 1}, 876 "file", 7, 2, 7, 3); 877 addMCDCBranchCMR(Counter::getCounter(2), Counter::getCounter(3), 1, {-1, -1}, 878 "file", 7, 4, 7, 5); 879 880 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 881 882 std::vector<std::string> Names; 883 for (const auto &Func : LoadedCoverage->getCoveredFunctions()) { 884 Names.push_back(Func.Name); 885 ASSERT_EQ(2U, Func.CountedBranchRegions.size()); 886 ASSERT_EQ(1U, Func.MCDCRecords.size()); 887 } 888 ASSERT_EQ(1U, Names.size()); 889 } 890 891 // Test the order of MCDCDecision before Expansion 892 TEST_P(CoverageMappingTest, decision_before_expansion) { 893 startFunction("foo", 0x1234); 894 addCMR(Counter::getCounter(0), "foo", 3, 23, 5, 2); 895 896 // This(4:11) was put after Expansion(4:11) before the fix 897 addMCDCDecisionCMR(0, 2, "foo", 4, 11, 4, 20); 898 899 addExpansionCMR("foo", "A", 4, 11, 4, 12); 900 addExpansionCMR("foo", "B", 4, 19, 4, 20); 901 addCMR(Counter::getCounter(0), "A", 1, 14, 1, 17); 902 addCMR(Counter::getCounter(0), "A", 1, 14, 1, 17); 903 addMCDCBranchCMR(Counter::getCounter(0), Counter::getCounter(1), 0, {-1, 1}, 904 "A", 1, 14, 1, 17); 905 addCMR(Counter::getCounter(1), "B", 1, 14, 1, 17); 906 addMCDCBranchCMR(Counter::getCounter(1), Counter::getCounter(2), 1, {-1, -1}, 907 "B", 1, 14, 1, 17); 908 909 // InputFunctionCoverageData::Regions is rewritten after the write. 910 auto InputRegions = InputFunctions.back().Regions; 911 912 writeAndReadCoverageRegions(); 913 914 const auto &OutputRegions = OutputFunctions.back().Regions; 915 916 size_t N = ArrayRef(InputRegions).size(); 917 ASSERT_EQ(N, OutputRegions.size()); 918 for (size_t I = 0; I < N; ++I) { 919 ASSERT_EQ(InputRegions[I].Kind, OutputRegions[I].Kind); 920 ASSERT_EQ(InputRegions[I].FileID, OutputRegions[I].FileID); 921 ASSERT_EQ(InputRegions[I].ExpandedFileID, OutputRegions[I].ExpandedFileID); 922 ASSERT_EQ(InputRegions[I].startLoc(), OutputRegions[I].startLoc()); 923 ASSERT_EQ(InputRegions[I].endLoc(), OutputRegions[I].endLoc()); 924 } 925 } 926 927 TEST_P(CoverageMappingTest, strip_filename_prefix) { 928 ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err); 929 930 startFunction("file1:func", 0x1234); 931 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 932 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 933 934 std::vector<std::string> Names; 935 for (const auto &Func : LoadedCoverage->getCoveredFunctions()) 936 Names.push_back(Func.Name); 937 ASSERT_EQ(1U, Names.size()); 938 ASSERT_EQ("func", Names[0]); 939 } 940 941 TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) { 942 ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}, Err); 943 944 startFunction("<unknown>:func", 0x1234); 945 addCMR(Counter::getCounter(0), "", 1, 1, 9, 9); 946 EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded()); 947 948 std::vector<std::string> Names; 949 for (const auto &Func : LoadedCoverage->getCoveredFunctions()) 950 Names.push_back(Func.Name); 951 ASSERT_EQ(1U, Names.size()); 952 ASSERT_EQ("func", Names[0]); 953 } 954 955 TEST_P(CoverageMappingTest, dont_detect_false_instantiations) { 956 ProfileWriter.addRecord({"foo", 0x1234, {10}}, Err); 957 ProfileWriter.addRecord({"bar", 0x2345, {20}}, Err); 958 959 startFunction("foo", 0x1234); 960 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10); 961 addExpansionCMR("main", "expanded", 4, 1, 4, 5); 962 963 startFunction("bar", 0x2345); 964 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10); 965 addExpansionCMR("main", "expanded", 9, 1, 9, 5); 966 967 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 968 969 std::vector<InstantiationGroup> InstantiationGroups = 970 LoadedCoverage->getInstantiationGroups("expanded"); 971 ASSERT_TRUE(InstantiationGroups.empty()); 972 } 973 974 TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) { 975 ProfileWriter.addRecord({"func", 0x1234, {10}}, Err); 976 977 startFunction("func", 0x1234); 978 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10); 979 addExpansionCMR("main", "expanded", 4, 1, 4, 5); 980 981 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 982 983 CoverageData Data = LoadedCoverage->getCoverageForFile("expanded"); 984 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 985 ASSERT_EQ(2U, Segments.size()); 986 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]); 987 EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]); 988 } 989 990 TEST_P(CoverageMappingTest, skip_duplicate_function_record) { 991 ProfileWriter.addRecord({"func", 0x1234, {1}}, Err); 992 993 // This record should be loaded. 994 startFunction("func", 0x1234); 995 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 996 997 // This record should be loaded. 998 startFunction("func", 0x1234); 999 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 1000 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9); 1001 1002 // This record should be skipped. 1003 startFunction("func", 0x1234); 1004 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 1005 1006 // This record should be loaded. 1007 startFunction("func", 0x1234); 1008 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9); 1009 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 1010 1011 // This record should be skipped. 1012 startFunction("func", 0x1234); 1013 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 1014 addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9); 1015 1016 EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); 1017 1018 auto Funcs = LoadedCoverage->getCoveredFunctions(); 1019 unsigned NumFuncs = std::distance(Funcs.begin(), Funcs.end()); 1020 ASSERT_EQ(3U, NumFuncs); 1021 } 1022 1023 INSTANTIATE_TEST_SUITE_P(ParameterizedCovMapTest, CoverageMappingTest, 1024 ::testing::Combine(::testing::Bool(), 1025 ::testing::Bool())); 1026 1027 TEST(CoverageMappingTest, filename_roundtrip) { 1028 std::vector<std::string> Paths({"dir", "a", "b", "c", "d", "e"}); 1029 1030 for (bool Compress : {false, true}) { 1031 std::string EncodedFilenames; 1032 { 1033 raw_string_ostream OS(EncodedFilenames); 1034 CoverageFilenamesSectionWriter Writer(Paths); 1035 Writer.write(OS, Compress); 1036 } 1037 1038 std::vector<std::string> ReadFilenames; 1039 RawCoverageFilenamesReader Reader(EncodedFilenames, ReadFilenames); 1040 EXPECT_THAT_ERROR(Reader.read(CovMapVersion::CurrentVersion), Succeeded()); 1041 1042 ASSERT_EQ(ReadFilenames.size(), Paths.size()); 1043 for (unsigned I = 1; I < Paths.size(); ++I) { 1044 SmallString<256> P(Paths[0]); 1045 llvm::sys::path::append(P, Paths[I]); 1046 ASSERT_EQ(ReadFilenames[I], P); 1047 } 1048 } 1049 } 1050 1051 TEST(CoverageMappingTest, filename_compilation_dir) { 1052 std::vector<std::string> Paths({"dir", "a", "b", "c", "d", "e"}); 1053 1054 for (bool Compress : {false, true}) { 1055 std::string EncodedFilenames; 1056 { 1057 raw_string_ostream OS(EncodedFilenames); 1058 CoverageFilenamesSectionWriter Writer(Paths); 1059 Writer.write(OS, Compress); 1060 } 1061 1062 StringRef CompilationDir = "out"; 1063 std::vector<std::string> ReadFilenames; 1064 RawCoverageFilenamesReader Reader(EncodedFilenames, ReadFilenames, 1065 CompilationDir); 1066 EXPECT_THAT_ERROR(Reader.read(CovMapVersion::CurrentVersion), Succeeded()); 1067 1068 ASSERT_EQ(ReadFilenames.size(), Paths.size()); 1069 for (unsigned I = 1; I < Paths.size(); ++I) { 1070 SmallString<256> P(CompilationDir); 1071 llvm::sys::path::append(P, Paths[I]); 1072 ASSERT_EQ(ReadFilenames[I], P); 1073 } 1074 } 1075 } 1076 1077 } // end anonymous namespace 1078