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