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::TestWithParam<bool> { 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(GetParam()); 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 TEST_P(CoverageMappingTest, basic_write_read) { 230 startFunction("func", 0x1234); 231 addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1); 232 addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2); 233 addCMR(Counter::getZero(), "foo", 3, 1, 3, 4); 234 addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8); 235 addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4); 236 237 writeAndReadCoverageRegions(); 238 ASSERT_EQ(1u, InputFunctions.size()); 239 ASSERT_EQ(1u, OutputFunctions.size()); 240 InputFunctionCoverageData &Input = InputFunctions.back(); 241 OutputFunctionCoverageData &Output = OutputFunctions.back(); 242 243 size_t N = makeArrayRef(Input.Regions).size(); 244 ASSERT_EQ(N, Output.Regions.size()); 245 for (size_t I = 0; I < N; ++I) { 246 ASSERT_EQ(Input.Regions[I].Count, Output.Regions[I].Count); 247 ASSERT_EQ(Input.Regions[I].FileID, Output.Regions[I].FileID); 248 ASSERT_EQ(Input.Regions[I].startLoc(), Output.Regions[I].startLoc()); 249 ASSERT_EQ(Input.Regions[I].endLoc(), Output.Regions[I].endLoc()); 250 ASSERT_EQ(Input.Regions[I].Kind, Output.Regions[I].Kind); 251 } 252 } 253 254 TEST_P(CoverageMappingTest, correct_deserialize_for_more_than_two_files) { 255 const char *FileNames[] = {"bar", "baz", "foo"}; 256 static const unsigned N = array_lengthof(FileNames); 257 258 startFunction("func", 0x1234); 259 for (unsigned I = 0; I < N; ++I) 260 // Use LineStart to hold the index of the file name 261 // in order to preserve that information during possible sorting of CMRs. 262 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1); 263 264 writeAndReadCoverageRegions(); 265 ASSERT_EQ(1u, OutputFunctions.size()); 266 OutputFunctionCoverageData &Output = OutputFunctions.back(); 267 268 ASSERT_EQ(N, Output.Regions.size()); 269 ASSERT_EQ(N, Output.Filenames.size()); 270 271 for (unsigned I = 0; I < N; ++I) { 272 ASSERT_GT(N, Output.Regions[I].FileID); 273 ASSERT_GT(N, Output.Regions[I].LineStart); 274 EXPECT_EQ(FileNames[Output.Regions[I].LineStart], 275 Output.Filenames[Output.Regions[I].FileID]); 276 } 277 } 278 279 TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) { 280 InstrProfRecord Record("func", 0x1234, {0}); 281 NoError(ProfileWriter.addRecord(std::move(Record))); 282 283 const char *FileNames[] = {"bar", "baz", "foo"}; 284 static const unsigned N = array_lengthof(FileNames); 285 286 startFunction("func", 0x1234); 287 for (unsigned I = 0; I < N; ++I) 288 // Use LineStart to hold the index of the file name 289 // in order to preserve that information during possible sorting of CMRs. 290 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1); 291 292 loadCoverageMapping(); 293 294 for (unsigned I = 0; I < N; ++I) { 295 CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]); 296 ASSERT_TRUE(!Data.empty()); 297 EXPECT_EQ(I, Data.begin()->Line); 298 } 299 } 300 301 TEST_P(CoverageMappingTest, load_coverage_for_several_functions) { 302 InstrProfRecord RecordFunc1("func1", 0x1234, {10}); 303 NoError(ProfileWriter.addRecord(std::move(RecordFunc1))); 304 InstrProfRecord RecordFunc2("func2", 0x2345, {20}); 305 NoError(ProfileWriter.addRecord(std::move(RecordFunc2))); 306 307 startFunction("func1", 0x1234); 308 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5); 309 310 startFunction("func2", 0x2345); 311 addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6); 312 313 loadCoverageMapping(); 314 315 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions(); 316 EXPECT_EQ(2U, std::distance(FunctionRecords.begin(), FunctionRecords.end())); 317 for (const auto &FunctionRecord : FunctionRecords) { 318 CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord); 319 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 320 ASSERT_EQ(2U, Segments.size()); 321 if (FunctionRecord.Name == "func1") { 322 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]); 323 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]); 324 } else { 325 ASSERT_EQ("func2", FunctionRecord.Name); 326 EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]); 327 EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]); 328 } 329 } 330 } 331 332 TEST_P(CoverageMappingTest, expansion_gets_first_counter) { 333 startFunction("func", 0x1234); 334 addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2); 335 // This starts earlier in "foo", so the expansion should get its counter. 336 addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1); 337 addExpansionCMR("bar", "foo", 3, 3, 3, 3); 338 339 writeAndReadCoverageRegions(); 340 ASSERT_EQ(1u, OutputFunctions.size()); 341 OutputFunctionCoverageData &Output = OutputFunctions.back(); 342 343 ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind); 344 ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count); 345 ASSERT_EQ(3U, Output.Regions[2].LineStart); 346 } 347 348 TEST_P(CoverageMappingTest, basic_coverage_iteration) { 349 InstrProfRecord Record("func", 0x1234, {30, 20, 10, 0}); 350 NoError(ProfileWriter.addRecord(std::move(Record))); 351 352 startFunction("func", 0x1234); 353 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 354 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7); 355 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1); 356 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11); 357 loadCoverageMapping(); 358 359 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 360 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 361 ASSERT_EQ(7U, Segments.size()); 362 ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments[0]); 363 ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]); 364 ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments[2]); 365 ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]); 366 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[4]); 367 ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]); 368 ASSERT_EQ(CoverageSegment(11, 11, false), Segments[6]); 369 } 370 371 TEST_P(CoverageMappingTest, uncovered_function) { 372 startFunction("func", 0x1234); 373 addCMR(Counter::getZero(), "file1", 1, 2, 3, 4); 374 loadCoverageMapping(); 375 376 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 377 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 378 ASSERT_EQ(2U, Segments.size()); 379 ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]); 380 ASSERT_EQ(CoverageSegment(3, 4, false), Segments[1]); 381 } 382 383 TEST_P(CoverageMappingTest, uncovered_function_with_mapping) { 384 startFunction("func", 0x1234); 385 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 386 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7); 387 loadCoverageMapping(); 388 389 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 390 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 391 ASSERT_EQ(3U, Segments.size()); 392 ASSERT_EQ(CoverageSegment(1, 1, 0, true), Segments[0]); 393 ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]); 394 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[2]); 395 } 396 397 TEST_P(CoverageMappingTest, combine_regions) { 398 InstrProfRecord Record("func", 0x1234, {10, 20, 30}); 399 NoError(ProfileWriter.addRecord(std::move(Record))); 400 401 startFunction("func", 0x1234); 402 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 403 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4); 404 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4); 405 loadCoverageMapping(); 406 407 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 408 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 409 ASSERT_EQ(4U, Segments.size()); 410 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]); 411 ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]); 412 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]); 413 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]); 414 } 415 416 TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) { 417 InstrProfRecord Record("func", 0x1234, {10, 20, 40}); 418 NoError(ProfileWriter.addRecord(std::move(Record))); 419 420 startFunction("func", 0x1234); 421 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 422 addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9); 423 addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5); 424 loadCoverageMapping(); 425 426 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 427 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 428 ASSERT_EQ(4U, Segments.size()); 429 EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]); 430 EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]); 431 EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]); 432 EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]); 433 } 434 435 // If CodeRegions and ExpansionRegions cover the same area, 436 // only counts of CodeRegions should be used. 437 TEST_P(CoverageMappingTest, dont_combine_expansions) { 438 InstrProfRecord Record1("func", 0x1234, {10, 20}); 439 InstrProfRecord Record2("func", 0x1234, {0, 0}); 440 NoError(ProfileWriter.addRecord(std::move(Record1))); 441 NoError(ProfileWriter.addRecord(std::move(Record2))); 442 443 startFunction("func", 0x1234); 444 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 445 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4); 446 addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7); 447 addExpansionCMR("file1", "include1", 3, 3, 4, 4); 448 loadCoverageMapping(); 449 450 CoverageData Data = LoadedCoverage->getCoverageForFile("file1"); 451 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 452 ASSERT_EQ(4U, Segments.size()); 453 ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]); 454 ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]); 455 ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]); 456 ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]); 457 } 458 459 // If an area is covered only by ExpansionRegions, they should be combinated. 460 TEST_P(CoverageMappingTest, combine_expansions) { 461 InstrProfRecord Record("func", 0x1234, {2, 3, 7}); 462 NoError(ProfileWriter.addRecord(std::move(Record))); 463 464 startFunction("func", 0x1234); 465 addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10); 466 addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10); 467 addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5); 468 addExpansionCMR("file", "include1", 3, 1, 3, 5); 469 addExpansionCMR("file", "include2", 3, 1, 3, 5); 470 471 loadCoverageMapping(); 472 473 CoverageData Data = LoadedCoverage->getCoverageForFile("file"); 474 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 475 ASSERT_EQ(4U, Segments.size()); 476 EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]); 477 EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]); 478 EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]); 479 EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]); 480 } 481 482 TEST_P(CoverageMappingTest, strip_filename_prefix) { 483 InstrProfRecord Record("file1:func", 0x1234, {0}); 484 NoError(ProfileWriter.addRecord(std::move(Record))); 485 486 startFunction("file1:func", 0x1234); 487 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); 488 loadCoverageMapping(); 489 490 std::vector<std::string> Names; 491 for (const auto &Func : LoadedCoverage->getCoveredFunctions()) 492 Names.push_back(Func.Name); 493 ASSERT_EQ(1U, Names.size()); 494 ASSERT_EQ("func", Names[0]); 495 } 496 497 TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) { 498 InstrProfRecord Record("<unknown>:func", 0x1234, {0}); 499 NoError(ProfileWriter.addRecord(std::move(Record))); 500 501 startFunction("<unknown>:func", 0x1234); 502 addCMR(Counter::getCounter(0), "", 1, 1, 9, 9); 503 loadCoverageMapping(/*EmitFilenames=*/false); 504 505 std::vector<std::string> Names; 506 for (const auto &Func : LoadedCoverage->getCoveredFunctions()) 507 Names.push_back(Func.Name); 508 ASSERT_EQ(1U, Names.size()); 509 ASSERT_EQ("func", Names[0]); 510 } 511 512 TEST_P(CoverageMappingTest, dont_detect_false_instantiations) { 513 InstrProfRecord Record1("foo", 0x1234, {10}); 514 InstrProfRecord Record2("bar", 0x2345, {20}); 515 NoError(ProfileWriter.addRecord(std::move(Record1))); 516 NoError(ProfileWriter.addRecord(std::move(Record2))); 517 518 startFunction("foo", 0x1234); 519 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10); 520 addExpansionCMR("main", "expanded", 4, 1, 4, 5); 521 522 startFunction("bar", 0x2345); 523 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10); 524 addExpansionCMR("main", "expanded", 9, 1, 9, 5); 525 526 loadCoverageMapping(); 527 528 std::vector<const FunctionRecord *> Instantiations = 529 LoadedCoverage->getInstantiations("expanded"); 530 ASSERT_TRUE(Instantiations.empty()); 531 } 532 533 TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) { 534 InstrProfRecord Record("func", 0x1234, {10}); 535 NoError(ProfileWriter.addRecord(std::move(Record))); 536 537 startFunction("func", 0x1234); 538 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10); 539 addExpansionCMR("main", "expanded", 4, 1, 4, 5); 540 541 loadCoverageMapping(); 542 543 CoverageData Data = LoadedCoverage->getCoverageForFile("expanded"); 544 std::vector<CoverageSegment> Segments(Data.begin(), Data.end()); 545 ASSERT_EQ(2U, Segments.size()); 546 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]); 547 EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]); 548 } 549 550 INSTANTIATE_TEST_CASE_P(ParameterizedCovMapTest, CoverageMappingTest, 551 ::testing::Bool()); 552 553 } // end anonymous namespace 554