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