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