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