CoverageMappingTest.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. #include "llvm/ProfileData/Coverage/CoverageMapping.h"
  9. #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
  10. #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
  11. #include "llvm/ProfileData/InstrProfReader.h"
  12. #include "llvm/ProfileData/InstrProfWriter.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. #include "llvm/Testing/Support/Error.h"
  15. #include "llvm/Testing/Support/SupportHelpers.h"
  16. #include "gtest/gtest.h"
  17. #include <ostream>
  18. #include <utility>
  19. using namespace llvm;
  20. using namespace coverage;
  21. LLVM_NODISCARD static ::testing::AssertionResult
  22. ErrorEquals(coveragemap_error Expected, Error E) {
  23. coveragemap_error Found;
  24. std::string FoundMsg;
  25. handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
  26. Found = CME.get();
  27. FoundMsg = CME.message();
  28. });
  29. if (Expected == Found)
  30. return ::testing::AssertionSuccess();
  31. return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
  32. }
  33. namespace llvm {
  34. namespace coverage {
  35. void PrintTo(const Counter &C, ::std::ostream *os) {
  36. if (C.isZero())
  37. *os << "Zero";
  38. else if (C.isExpression())
  39. *os << "Expression " << C.getExpressionID();
  40. else
  41. *os << "Counter " << C.getCounterID();
  42. }
  43. void PrintTo(const CoverageSegment &S, ::std::ostream *os) {
  44. *os << "CoverageSegment(" << S.Line << ", " << S.Col << ", ";
  45. if (S.HasCount)
  46. *os << S.Count << ", ";
  47. *os << (S.IsRegionEntry ? "true" : "false") << ")";
  48. }
  49. }
  50. }
  51. namespace {
  52. struct OutputFunctionCoverageData {
  53. StringRef Name;
  54. uint64_t Hash;
  55. std::vector<StringRef> Filenames;
  56. std::vector<CounterMappingRegion> Regions;
  57. OutputFunctionCoverageData() : Hash(0) {}
  58. OutputFunctionCoverageData(OutputFunctionCoverageData &&OFCD)
  59. : Name(OFCD.Name), Hash(OFCD.Hash), Filenames(std::move(OFCD.Filenames)),
  60. Regions(std::move(OFCD.Regions)) {}
  61. OutputFunctionCoverageData(const OutputFunctionCoverageData &) = delete;
  62. OutputFunctionCoverageData &
  63. operator=(const OutputFunctionCoverageData &) = delete;
  64. OutputFunctionCoverageData &operator=(OutputFunctionCoverageData &&) = delete;
  65. void fillCoverageMappingRecord(CoverageMappingRecord &Record) const {
  66. Record.FunctionName = Name;
  67. Record.FunctionHash = Hash;
  68. Record.Filenames = Filenames;
  69. Record.Expressions = {};
  70. Record.MappingRegions = Regions;
  71. }
  72. };
  73. struct CoverageMappingReaderMock : CoverageMappingReader {
  74. ArrayRef<OutputFunctionCoverageData> Functions;
  75. CoverageMappingReaderMock(ArrayRef<OutputFunctionCoverageData> Functions)
  76. : Functions(Functions) {}
  77. Error readNextRecord(CoverageMappingRecord &Record) override {
  78. if (Functions.empty())
  79. return make_error<CoverageMapError>(coveragemap_error::eof);
  80. Functions.front().fillCoverageMappingRecord(Record);
  81. Functions = Functions.slice(1);
  82. return Error::success();
  83. }
  84. };
  85. struct InputFunctionCoverageData {
  86. // Maps the global file index from CoverageMappingTest.Files
  87. // to the index of that file within this function. We can't just use
  88. // global file indexes here because local indexes have to be dense.
  89. // This map is used during serialization to create the virtual file mapping
  90. // (from local fileId to global Index) in the head of the per-function
  91. // coverage mapping data.
  92. SmallDenseMap<unsigned, unsigned> ReverseVirtualFileMapping;
  93. std::string Name;
  94. uint64_t Hash;
  95. std::vector<CounterMappingRegion> Regions;
  96. InputFunctionCoverageData(std::string Name, uint64_t Hash)
  97. : Name(std::move(Name)), Hash(Hash) {}
  98. InputFunctionCoverageData(InputFunctionCoverageData &&IFCD)
  99. : ReverseVirtualFileMapping(std::move(IFCD.ReverseVirtualFileMapping)),
  100. Name(std::move(IFCD.Name)), Hash(IFCD.Hash),
  101. Regions(std::move(IFCD.Regions)) {}
  102. InputFunctionCoverageData(const InputFunctionCoverageData &) = delete;
  103. InputFunctionCoverageData &
  104. operator=(const InputFunctionCoverageData &) = delete;
  105. InputFunctionCoverageData &operator=(InputFunctionCoverageData &&) = delete;
  106. };
  107. struct CoverageMappingTest : ::testing::TestWithParam<std::pair<bool, bool>> {
  108. bool UseMultipleReaders;
  109. StringMap<unsigned> Files;
  110. std::vector<InputFunctionCoverageData> InputFunctions;
  111. std::vector<OutputFunctionCoverageData> OutputFunctions;
  112. InstrProfWriter ProfileWriter;
  113. std::unique_ptr<IndexedInstrProfReader> ProfileReader;
  114. std::unique_ptr<CoverageMapping> LoadedCoverage;
  115. void SetUp() override {
  116. ProfileWriter.setOutputSparse(GetParam().first);
  117. UseMultipleReaders = GetParam().second;
  118. }
  119. unsigned getGlobalFileIndex(StringRef Name) {
  120. auto R = Files.find(Name);
  121. if (R != Files.end())
  122. return R->second;
  123. unsigned Index = Files.size();
  124. Files.try_emplace(Name, Index);
  125. return Index;
  126. }
  127. // Return the file index of file 'Name' for the current function.
  128. // Add the file into the global map if necessary.
  129. // See also InputFunctionCoverageData::ReverseVirtualFileMapping
  130. // for additional comments.
  131. unsigned getFileIndexForFunction(StringRef Name) {
  132. unsigned GlobalIndex = getGlobalFileIndex(Name);
  133. auto &CurrentFunctionFileMapping =
  134. InputFunctions.back().ReverseVirtualFileMapping;
  135. auto R = CurrentFunctionFileMapping.find(GlobalIndex);
  136. if (R != CurrentFunctionFileMapping.end())
  137. return R->second;
  138. unsigned IndexInFunction = CurrentFunctionFileMapping.size();
  139. CurrentFunctionFileMapping.insert(
  140. std::make_pair(GlobalIndex, IndexInFunction));
  141. return IndexInFunction;
  142. }
  143. void startFunction(StringRef FuncName, uint64_t Hash) {
  144. InputFunctions.emplace_back(FuncName.str(), Hash);
  145. }
  146. void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
  147. unsigned CE, bool Skipped = false) {
  148. auto &Regions = InputFunctions.back().Regions;
  149. unsigned FileID = getFileIndexForFunction(File);
  150. Regions.push_back(
  151. Skipped ? CounterMappingRegion::makeSkipped(FileID, LS, CS, LE, CE)
  152. : CounterMappingRegion::makeRegion(C, FileID, LS, CS, LE, CE));
  153. }
  154. void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
  155. unsigned CS, unsigned LE, unsigned CE) {
  156. InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion(
  157. getFileIndexForFunction(File), getFileIndexForFunction(ExpandedFile),
  158. LS, CS, LE, CE));
  159. }
  160. std::string writeCoverageRegions(InputFunctionCoverageData &Data) {
  161. SmallVector<unsigned, 8> FileIDs(Data.ReverseVirtualFileMapping.size());
  162. for (const auto &E : Data.ReverseVirtualFileMapping)
  163. FileIDs[E.second] = E.first;
  164. std::string Coverage;
  165. llvm::raw_string_ostream OS(Coverage);
  166. CoverageMappingWriter(FileIDs, None, Data.Regions).write(OS);
  167. return OS.str();
  168. }
  169. void readCoverageRegions(const std::string &Coverage,
  170. OutputFunctionCoverageData &Data) {
  171. SmallVector<StringRef, 8> Filenames(Files.size());
  172. for (const auto &E : Files)
  173. Filenames[E.getValue()] = E.getKey();
  174. std::vector<CounterExpression> Expressions;
  175. RawCoverageMappingReader Reader(Coverage, Filenames, Data.Filenames,
  176. Expressions, Data.Regions);
  177. EXPECT_THAT_ERROR(Reader.read(), Succeeded());
  178. }
  179. void writeAndReadCoverageRegions(bool EmitFilenames = true) {
  180. OutputFunctions.resize(InputFunctions.size());
  181. for (unsigned I = 0; I < InputFunctions.size(); ++I) {
  182. std::string Regions = writeCoverageRegions(InputFunctions[I]);
  183. readCoverageRegions(Regions, OutputFunctions[I]);
  184. OutputFunctions[I].Name = InputFunctions[I].Name;
  185. OutputFunctions[I].Hash = InputFunctions[I].Hash;
  186. if (!EmitFilenames)
  187. OutputFunctions[I].Filenames.clear();
  188. }
  189. }
  190. void readProfCounts() {
  191. auto Profile = ProfileWriter.writeBuffer();
  192. auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
  193. EXPECT_THAT_ERROR(ReaderOrErr.takeError(), Succeeded());
  194. ProfileReader = std::move(ReaderOrErr.get());
  195. }
  196. Expected<std::unique_ptr<CoverageMapping>> readOutputFunctions() {
  197. std::vector<std::unique_ptr<CoverageMappingReader>> CoverageReaders;
  198. if (UseMultipleReaders) {
  199. for (const auto &OF : OutputFunctions) {
  200. ArrayRef<OutputFunctionCoverageData> Funcs(OF);
  201. CoverageReaders.push_back(
  202. std::make_unique<CoverageMappingReaderMock>(Funcs));
  203. }
  204. } else {
  205. ArrayRef<OutputFunctionCoverageData> Funcs(OutputFunctions);
  206. CoverageReaders.push_back(
  207. std::make_unique<CoverageMappingReaderMock>(Funcs));
  208. }
  209. return CoverageMapping::load(CoverageReaders, *ProfileReader);
  210. }
  211. Error loadCoverageMapping(bool EmitFilenames = true) {
  212. readProfCounts();
  213. writeAndReadCoverageRegions(EmitFilenames);
  214. auto CoverageOrErr = readOutputFunctions();
  215. if (!CoverageOrErr)
  216. return CoverageOrErr.takeError();
  217. LoadedCoverage = std::move(CoverageOrErr.get());
  218. return Error::success();
  219. }
  220. };
  221. TEST_P(CoverageMappingTest, basic_write_read) {
  222. startFunction("func", 0x1234);
  223. addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
  224. addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
  225. addCMR(Counter::getZero(), "foo", 3, 1, 3, 4);
  226. addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
  227. addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
  228. writeAndReadCoverageRegions();
  229. ASSERT_EQ(1u, InputFunctions.size());
  230. ASSERT_EQ(1u, OutputFunctions.size());
  231. InputFunctionCoverageData &Input = InputFunctions.back();
  232. OutputFunctionCoverageData &Output = OutputFunctions.back();
  233. size_t N = makeArrayRef(Input.Regions).size();
  234. ASSERT_EQ(N, Output.Regions.size());
  235. for (size_t I = 0; I < N; ++I) {
  236. ASSERT_EQ(Input.Regions[I].Count, Output.Regions[I].Count);
  237. ASSERT_EQ(Input.Regions[I].FileID, Output.Regions[I].FileID);
  238. ASSERT_EQ(Input.Regions[I].startLoc(), Output.Regions[I].startLoc());
  239. ASSERT_EQ(Input.Regions[I].endLoc(), Output.Regions[I].endLoc());
  240. ASSERT_EQ(Input.Regions[I].Kind, Output.Regions[I].Kind);
  241. }
  242. }
  243. TEST_P(CoverageMappingTest, correct_deserialize_for_more_than_two_files) {
  244. const char *FileNames[] = {"bar", "baz", "foo"};
  245. static const unsigned N = array_lengthof(FileNames);
  246. startFunction("func", 0x1234);
  247. for (unsigned I = 0; I < N; ++I)
  248. // Use LineStart to hold the index of the file name
  249. // in order to preserve that information during possible sorting of CMRs.
  250. addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
  251. writeAndReadCoverageRegions();
  252. ASSERT_EQ(1u, OutputFunctions.size());
  253. OutputFunctionCoverageData &Output = OutputFunctions.back();
  254. ASSERT_EQ(N, Output.Regions.size());
  255. ASSERT_EQ(N, Output.Filenames.size());
  256. for (unsigned I = 0; I < N; ++I) {
  257. ASSERT_GT(N, Output.Regions[I].FileID);
  258. ASSERT_GT(N, Output.Regions[I].LineStart);
  259. EXPECT_EQ(FileNames[Output.Regions[I].LineStart],
  260. Output.Filenames[Output.Regions[I].FileID]);
  261. }
  262. }
  263. static const auto Err = [](Error E) { FAIL(); };
  264. TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) {
  265. ProfileWriter.addRecord({"func", 0x1234, {0}}, Err);
  266. const char *FileNames[] = {"bar", "baz", "foo"};
  267. static const unsigned N = array_lengthof(FileNames);
  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. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  274. for (unsigned I = 0; I < N; ++I) {
  275. CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
  276. ASSERT_TRUE(!Data.empty());
  277. EXPECT_EQ(I, Data.begin()->Line);
  278. }
  279. }
  280. TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) {
  281. ProfileWriter.addRecord({"", 0x1234, {10}}, Err);
  282. startFunction("", 0x1234);
  283. addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
  284. EXPECT_TRUE(ErrorEquals(coveragemap_error::malformed, loadCoverageMapping()));
  285. }
  286. TEST_P(CoverageMappingTest, load_coverage_for_several_functions) {
  287. ProfileWriter.addRecord({"func1", 0x1234, {10}}, Err);
  288. ProfileWriter.addRecord({"func2", 0x2345, {20}}, Err);
  289. startFunction("func1", 0x1234);
  290. addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
  291. startFunction("func2", 0x2345);
  292. addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
  293. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  294. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  295. EXPECT_EQ(2, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
  296. for (const auto &FunctionRecord : FunctionRecords) {
  297. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  298. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  299. ASSERT_EQ(2U, Segments.size());
  300. if (FunctionRecord.Name == "func1") {
  301. EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
  302. EXPECT_EQ(CoverageSegment(5, 5, false), Segments[1]);
  303. } else {
  304. ASSERT_EQ("func2", FunctionRecord.Name);
  305. EXPECT_EQ(CoverageSegment(2, 2, 20, true), Segments[0]);
  306. EXPECT_EQ(CoverageSegment(6, 6, false), Segments[1]);
  307. }
  308. }
  309. }
  310. TEST_P(CoverageMappingTest, create_combined_regions) {
  311. ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3}}, Err);
  312. startFunction("func1", 0x1234);
  313. // Given regions which start at the same location, emit a segment for the
  314. // last region.
  315. addCMR(Counter::getCounter(0), "file1", 1, 1, 2, 2);
  316. addCMR(Counter::getCounter(1), "file1", 1, 1, 2, 2);
  317. addCMR(Counter::getCounter(2), "file1", 1, 1, 2, 2);
  318. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  319. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  320. const auto &FunctionRecord = *FunctionRecords.begin();
  321. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  322. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  323. ASSERT_EQ(2U, Segments.size());
  324. EXPECT_EQ(CoverageSegment(1, 1, 6, true), Segments[0]);
  325. EXPECT_EQ(CoverageSegment(2, 2, false), Segments[1]);
  326. }
  327. TEST_P(CoverageMappingTest, skipped_segments_have_no_count) {
  328. ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
  329. startFunction("func1", 0x1234);
  330. addCMR(Counter::getCounter(0), "file1", 1, 1, 5, 5);
  331. addCMR(Counter::getCounter(0), "file1", 5, 1, 5, 5, /*Skipped=*/true);
  332. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  333. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  334. const auto &FunctionRecord = *FunctionRecords.begin();
  335. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  336. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  337. ASSERT_EQ(3U, Segments.size());
  338. EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
  339. EXPECT_EQ(CoverageSegment(5, 1, true), Segments[1]);
  340. EXPECT_EQ(CoverageSegment(5, 5, false), Segments[2]);
  341. }
  342. TEST_P(CoverageMappingTest, multiple_regions_end_after_parent_ends) {
  343. ProfileWriter.addRecord({"func1", 0x1234, {1, 0}}, Err);
  344. startFunction("func1", 0x1234);
  345. // 1| F{ a{
  346. // 2|
  347. // 3| a} b{ c{
  348. // 4|
  349. // 5| b}
  350. // 6|
  351. // 7| c} d{ e{
  352. // 8|
  353. // 9| d} e} F}
  354. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9); // < F
  355. addCMR(Counter::getCounter(0), "file1", 1, 1, 3, 5); // < a
  356. addCMR(Counter::getCounter(0), "file1", 3, 5, 5, 4); // < b
  357. addCMR(Counter::getCounter(1), "file1", 3, 5, 7, 3); // < c
  358. addCMR(Counter::getCounter(1), "file1", 7, 3, 9, 2); // < d
  359. addCMR(Counter::getCounter(1), "file1", 7, 7, 9, 7); // < e
  360. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  361. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  362. const auto &FunctionRecord = *FunctionRecords.begin();
  363. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  364. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  365. // Old output (not sorted or unique):
  366. // Segment at 1:1 with count 1
  367. // Segment at 1:1 with count 1
  368. // Segment at 3:5 with count 1
  369. // Segment at 3:5 with count 0
  370. // Segment at 3:5 with count 1
  371. // Segment at 5:4 with count 0
  372. // Segment at 7:3 with count 1
  373. // Segment at 7:3 with count 0
  374. // Segment at 7:7 with count 0
  375. // Segment at 9:7 with count 0
  376. // Segment at 9:2 with count 1
  377. // Top level segment at 9:9
  378. // New output (sorted and unique):
  379. // Segment at 1:1 (count = 1), RegionEntry
  380. // Segment at 3:5 (count = 1), RegionEntry
  381. // Segment at 5:4 (count = 0)
  382. // Segment at 7:3 (count = 0), RegionEntry
  383. // Segment at 7:7 (count = 0), RegionEntry
  384. // Segment at 9:2 (count = 0)
  385. // Segment at 9:7 (count = 1)
  386. // Segment at 9:9 (count = 0), Skipped
  387. ASSERT_EQ(8U, Segments.size());
  388. EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
  389. EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[1]);
  390. EXPECT_EQ(CoverageSegment(5, 4, 0, false), Segments[2]);
  391. EXPECT_EQ(CoverageSegment(7, 3, 0, true), Segments[3]);
  392. EXPECT_EQ(CoverageSegment(7, 7, 0, true), Segments[4]);
  393. EXPECT_EQ(CoverageSegment(9, 2, 0, false), Segments[5]);
  394. EXPECT_EQ(CoverageSegment(9, 7, 1, false), Segments[6]);
  395. EXPECT_EQ(CoverageSegment(9, 9, false), Segments[7]);
  396. }
  397. TEST_P(CoverageMappingTest, multiple_completed_segments_at_same_loc) {
  398. ProfileWriter.addRecord({"func1", 0x1234, {0, 1, 2}}, Err);
  399. startFunction("func1", 0x1234);
  400. // PR35495
  401. addCMR(Counter::getCounter(1), "file1", 2, 1, 18, 2);
  402. addCMR(Counter::getCounter(0), "file1", 8, 10, 14, 6);
  403. addCMR(Counter::getCounter(0), "file1", 8, 12, 14, 6);
  404. addCMR(Counter::getCounter(1), "file1", 9, 1, 14, 6);
  405. addCMR(Counter::getCounter(2), "file1", 11, 13, 11, 14);
  406. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  407. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  408. const auto &FunctionRecord = *FunctionRecords.begin();
  409. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  410. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  411. ASSERT_EQ(7U, Segments.size());
  412. EXPECT_EQ(CoverageSegment(2, 1, 1, true), Segments[0]);
  413. EXPECT_EQ(CoverageSegment(8, 10, 0, true), Segments[1]);
  414. EXPECT_EQ(CoverageSegment(8, 12, 0, true), Segments[2]);
  415. EXPECT_EQ(CoverageSegment(9, 1, 1, true), Segments[3]);
  416. EXPECT_EQ(CoverageSegment(11, 13, 2, true), Segments[4]);
  417. // Use count=1 (from 9:1 -> 14:6), not count=0 (from 8:12 -> 14:6).
  418. EXPECT_EQ(CoverageSegment(11, 14, 1, false), Segments[5]);
  419. EXPECT_EQ(CoverageSegment(18, 2, false), Segments[6]);
  420. }
  421. TEST_P(CoverageMappingTest, dont_emit_redundant_segments) {
  422. ProfileWriter.addRecord({"func1", 0x1234, {1, 1}}, Err);
  423. startFunction("func1", 0x1234);
  424. addCMR(Counter::getCounter(0), "file1", 1, 1, 4, 4);
  425. addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
  426. addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 6);
  427. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  428. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  429. const auto &FunctionRecord = *FunctionRecords.begin();
  430. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  431. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  432. ASSERT_EQ(5U, Segments.size());
  433. EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
  434. EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
  435. EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
  436. EXPECT_EQ(CoverageSegment(4, 4, 1, false), Segments[3]);
  437. // A closing segment starting at 5:5 would be redundant: it would have the
  438. // same count as the segment starting at 4:4, and has all the same metadata.
  439. EXPECT_EQ(CoverageSegment(6, 6, false), Segments[4]);
  440. }
  441. TEST_P(CoverageMappingTest, dont_emit_closing_segment_at_new_region_start) {
  442. ProfileWriter.addRecord({"func1", 0x1234, {1}}, Err);
  443. startFunction("func1", 0x1234);
  444. addCMR(Counter::getCounter(0), "file1", 1, 1, 6, 5);
  445. addCMR(Counter::getCounter(0), "file1", 2, 2, 6, 5);
  446. addCMR(Counter::getCounter(0), "file1", 3, 3, 6, 5);
  447. addCMR(Counter::getCounter(0), "file1", 6, 5, 7, 7);
  448. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  449. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  450. const auto &FunctionRecord = *FunctionRecords.begin();
  451. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  452. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  453. ASSERT_EQ(5U, Segments.size());
  454. EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
  455. EXPECT_EQ(CoverageSegment(2, 2, 1, true), Segments[1]);
  456. EXPECT_EQ(CoverageSegment(3, 3, 1, true), Segments[2]);
  457. EXPECT_EQ(CoverageSegment(6, 5, 1, true), Segments[3]);
  458. // The old segment builder would get this wrong by emitting multiple segments
  459. // which start at 6:5 (a few of which were skipped segments). We should just
  460. // get a segment for the region entry.
  461. EXPECT_EQ(CoverageSegment(7, 7, false), Segments[4]);
  462. }
  463. TEST_P(CoverageMappingTest, handle_consecutive_regions_with_zero_length) {
  464. ProfileWriter.addRecord({"func1", 0x1234, {1, 2}}, Err);
  465. startFunction("func1", 0x1234);
  466. addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
  467. addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
  468. addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
  469. addCMR(Counter::getCounter(1), "file1", 1, 1, 1, 1);
  470. addCMR(Counter::getCounter(0), "file1", 1, 1, 1, 1);
  471. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  472. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  473. const auto &FunctionRecord = *FunctionRecords.begin();
  474. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  475. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  476. ASSERT_EQ(1U, Segments.size());
  477. EXPECT_EQ(CoverageSegment(1, 1, true), Segments[0]);
  478. // We need to get a skipped segment starting at 1:1. In this case there is
  479. // also a region entry at 1:1.
  480. }
  481. TEST_P(CoverageMappingTest, handle_sandwiched_zero_length_region) {
  482. ProfileWriter.addRecord({"func1", 0x1234, {2, 1}}, Err);
  483. startFunction("func1", 0x1234);
  484. addCMR(Counter::getCounter(0), "file1", 1, 5, 4, 4);
  485. addCMR(Counter::getCounter(1), "file1", 1, 9, 1, 50);
  486. addCMR(Counter::getCounter(1), "file1", 2, 7, 2, 34);
  487. addCMR(Counter::getCounter(1), "file1", 3, 5, 3, 21);
  488. addCMR(Counter::getCounter(1), "file1", 3, 21, 3, 21);
  489. addCMR(Counter::getCounter(1), "file1", 4, 12, 4, 17);
  490. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  491. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  492. const auto &FunctionRecord = *FunctionRecords.begin();
  493. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  494. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  495. ASSERT_EQ(10U, Segments.size());
  496. EXPECT_EQ(CoverageSegment(1, 5, 2, true), Segments[0]);
  497. EXPECT_EQ(CoverageSegment(1, 9, 1, true), Segments[1]);
  498. EXPECT_EQ(CoverageSegment(1, 50, 2, false), Segments[2]);
  499. EXPECT_EQ(CoverageSegment(2, 7, 1, true), Segments[3]);
  500. EXPECT_EQ(CoverageSegment(2, 34, 2, false), Segments[4]);
  501. EXPECT_EQ(CoverageSegment(3, 5, 1, true), Segments[5]);
  502. EXPECT_EQ(CoverageSegment(3, 21, 2, true), Segments[6]);
  503. // Handle the zero-length region by creating a segment with its predecessor's
  504. // count (i.e the count from 1:5 -> 4:4).
  505. EXPECT_EQ(CoverageSegment(4, 4, false), Segments[7]);
  506. // The area between 4:4 and 4:12 is skipped.
  507. EXPECT_EQ(CoverageSegment(4, 12, 1, true), Segments[8]);
  508. EXPECT_EQ(CoverageSegment(4, 17, false), Segments[9]);
  509. }
  510. TEST_P(CoverageMappingTest, handle_last_completed_region) {
  511. ProfileWriter.addRecord({"func1", 0x1234, {1, 2, 3, 4}}, Err);
  512. startFunction("func1", 0x1234);
  513. addCMR(Counter::getCounter(0), "file1", 1, 1, 8, 8);
  514. addCMR(Counter::getCounter(1), "file1", 2, 2, 5, 5);
  515. addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
  516. addCMR(Counter::getCounter(3), "file1", 6, 6, 7, 7);
  517. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  518. const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
  519. const auto &FunctionRecord = *FunctionRecords.begin();
  520. CoverageData Data = LoadedCoverage->getCoverageForFunction(FunctionRecord);
  521. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  522. ASSERT_EQ(8U, Segments.size());
  523. EXPECT_EQ(CoverageSegment(1, 1, 1, true), Segments[0]);
  524. EXPECT_EQ(CoverageSegment(2, 2, 2, true), Segments[1]);
  525. EXPECT_EQ(CoverageSegment(3, 3, 3, true), Segments[2]);
  526. EXPECT_EQ(CoverageSegment(4, 4, 2, false), Segments[3]);
  527. EXPECT_EQ(CoverageSegment(5, 5, 1, false), Segments[4]);
  528. EXPECT_EQ(CoverageSegment(6, 6, 4, true), Segments[5]);
  529. EXPECT_EQ(CoverageSegment(7, 7, 1, false), Segments[6]);
  530. EXPECT_EQ(CoverageSegment(8, 8, false), Segments[7]);
  531. }
  532. TEST_P(CoverageMappingTest, expansion_gets_first_counter) {
  533. startFunction("func", 0x1234);
  534. addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
  535. // This starts earlier in "foo", so the expansion should get its counter.
  536. addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
  537. addExpansionCMR("bar", "foo", 3, 3, 3, 3);
  538. writeAndReadCoverageRegions();
  539. ASSERT_EQ(1u, OutputFunctions.size());
  540. OutputFunctionCoverageData &Output = OutputFunctions.back();
  541. ASSERT_EQ(CounterMappingRegion::ExpansionRegion, Output.Regions[2].Kind);
  542. ASSERT_EQ(Counter::getCounter(2), Output.Regions[2].Count);
  543. ASSERT_EQ(3U, Output.Regions[2].LineStart);
  544. }
  545. TEST_P(CoverageMappingTest, basic_coverage_iteration) {
  546. ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
  547. startFunction("func", 0x1234);
  548. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  549. addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
  550. addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
  551. addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
  552. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  553. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  554. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  555. ASSERT_EQ(7U, Segments.size());
  556. ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments[0]);
  557. ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
  558. ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments[2]);
  559. ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
  560. ASSERT_EQ(CoverageSegment(9, 9, false), Segments[4]);
  561. ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
  562. ASSERT_EQ(CoverageSegment(11, 11, false), Segments[6]);
  563. }
  564. TEST_P(CoverageMappingTest, test_line_coverage_iterator) {
  565. ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);
  566. startFunction("func", 0x1234);
  567. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  568. addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
  569. addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
  570. addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
  571. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  572. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  573. unsigned Line = 0;
  574. unsigned LineCounts[] = {20, 20, 20, 20, 30, 10, 10, 10, 10, 0, 0};
  575. for (const auto &LCS : getLineCoverageStats(Data)) {
  576. ASSERT_EQ(Line + 1, LCS.getLine());
  577. errs() << "Line: " << Line + 1 << ", count = " << LCS.getExecutionCount() << "\n";
  578. ASSERT_EQ(LineCounts[Line], LCS.getExecutionCount());
  579. ++Line;
  580. }
  581. ASSERT_EQ(11U, Line);
  582. }
  583. TEST_P(CoverageMappingTest, uncovered_function) {
  584. startFunction("func", 0x1234);
  585. addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
  586. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  587. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  588. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  589. ASSERT_EQ(2U, Segments.size());
  590. ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
  591. ASSERT_EQ(CoverageSegment(3, 4, false), Segments[1]);
  592. }
  593. TEST_P(CoverageMappingTest, uncovered_function_with_mapping) {
  594. startFunction("func", 0x1234);
  595. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  596. addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
  597. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  598. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  599. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  600. ASSERT_EQ(3U, Segments.size());
  601. ASSERT_EQ(CoverageSegment(1, 1, 0, true), Segments[0]);
  602. ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
  603. ASSERT_EQ(CoverageSegment(9, 9, false), Segments[2]);
  604. }
  605. TEST_P(CoverageMappingTest, combine_regions) {
  606. ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}, Err);
  607. startFunction("func", 0x1234);
  608. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  609. addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
  610. addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
  611. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  612. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  613. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  614. ASSERT_EQ(4U, Segments.size());
  615. ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
  616. ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
  617. ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
  618. ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
  619. }
  620. TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) {
  621. ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}, Err);
  622. startFunction("func", 0x1234);
  623. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  624. addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
  625. addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
  626. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  627. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  628. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  629. ASSERT_EQ(4U, Segments.size());
  630. EXPECT_EQ(CoverageSegment(1, 1, 30, true), Segments[0]);
  631. EXPECT_EQ(CoverageSegment(3, 3, 40, true), Segments[1]);
  632. EXPECT_EQ(CoverageSegment(5, 5, 30, false), Segments[2]);
  633. EXPECT_EQ(CoverageSegment(9, 9, false), Segments[3]);
  634. }
  635. // If CodeRegions and ExpansionRegions cover the same area,
  636. // only counts of CodeRegions should be used.
  637. TEST_P(CoverageMappingTest, dont_combine_expansions) {
  638. ProfileWriter.addRecord({"func", 0x1234, {10, 20}}, Err);
  639. ProfileWriter.addRecord({"func", 0x1234, {0, 0}}, Err);
  640. startFunction("func", 0x1234);
  641. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  642. addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
  643. addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
  644. addExpansionCMR("file1", "include1", 3, 3, 4, 4);
  645. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  646. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  647. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  648. ASSERT_EQ(4U, Segments.size());
  649. ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
  650. ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
  651. ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
  652. ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
  653. }
  654. // If an area is covered only by ExpansionRegions, they should be combinated.
  655. TEST_P(CoverageMappingTest, combine_expansions) {
  656. ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}, Err);
  657. startFunction("func", 0x1234);
  658. addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
  659. addCMR(Counter::getCounter(2), "include2", 1, 1, 1, 10);
  660. addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5);
  661. addExpansionCMR("file", "include1", 3, 1, 3, 5);
  662. addExpansionCMR("file", "include2", 3, 1, 3, 5);
  663. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  664. CoverageData Data = LoadedCoverage->getCoverageForFile("file");
  665. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  666. ASSERT_EQ(4U, Segments.size());
  667. EXPECT_EQ(CoverageSegment(1, 1, 2, true), Segments[0]);
  668. EXPECT_EQ(CoverageSegment(3, 1, 10, true), Segments[1]);
  669. EXPECT_EQ(CoverageSegment(3, 5, 2, false), Segments[2]);
  670. EXPECT_EQ(CoverageSegment(5, 5, false), Segments[3]);
  671. }
  672. TEST_P(CoverageMappingTest, strip_filename_prefix) {
  673. ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err);
  674. startFunction("file1:func", 0x1234);
  675. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  676. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  677. std::vector<std::string> Names;
  678. for (const auto &Func : LoadedCoverage->getCoveredFunctions())
  679. Names.push_back(Func.Name);
  680. ASSERT_EQ(1U, Names.size());
  681. ASSERT_EQ("func", Names[0]);
  682. }
  683. TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) {
  684. ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}, Err);
  685. startFunction("<unknown>:func", 0x1234);
  686. addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
  687. EXPECT_THAT_ERROR(loadCoverageMapping(/*EmitFilenames=*/false), Succeeded());
  688. std::vector<std::string> Names;
  689. for (const auto &Func : LoadedCoverage->getCoveredFunctions())
  690. Names.push_back(Func.Name);
  691. ASSERT_EQ(1U, Names.size());
  692. ASSERT_EQ("func", Names[0]);
  693. }
  694. TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
  695. ProfileWriter.addRecord({"foo", 0x1234, {10}}, Err);
  696. ProfileWriter.addRecord({"bar", 0x2345, {20}}, Err);
  697. startFunction("foo", 0x1234);
  698. addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
  699. addExpansionCMR("main", "expanded", 4, 1, 4, 5);
  700. startFunction("bar", 0x2345);
  701. addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
  702. addExpansionCMR("main", "expanded", 9, 1, 9, 5);
  703. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  704. std::vector<InstantiationGroup> InstantiationGroups =
  705. LoadedCoverage->getInstantiationGroups("expanded");
  706. for (const auto &Group : InstantiationGroups)
  707. ASSERT_EQ(Group.size(), 1U);
  708. }
  709. TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {
  710. ProfileWriter.addRecord({"func", 0x1234, {10}}, Err);
  711. startFunction("func", 0x1234);
  712. addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
  713. addExpansionCMR("main", "expanded", 4, 1, 4, 5);
  714. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  715. CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
  716. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  717. ASSERT_EQ(2U, Segments.size());
  718. EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
  719. EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]);
  720. }
  721. TEST_P(CoverageMappingTest, skip_duplicate_function_record) {
  722. ProfileWriter.addRecord({"func", 0x1234, {1}}, Err);
  723. // This record should be loaded.
  724. startFunction("func", 0x1234);
  725. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  726. // This record should be loaded.
  727. startFunction("func", 0x1234);
  728. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  729. addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
  730. // This record should be skipped.
  731. startFunction("func", 0x1234);
  732. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  733. // This record should be loaded.
  734. startFunction("func", 0x1234);
  735. addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
  736. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  737. // This record should be skipped.
  738. startFunction("func", 0x1234);
  739. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  740. addCMR(Counter::getCounter(0), "file2", 1, 1, 9, 9);
  741. EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
  742. auto Funcs = LoadedCoverage->getCoveredFunctions();
  743. unsigned NumFuncs = std::distance(Funcs.begin(), Funcs.end());
  744. ASSERT_EQ(3U, NumFuncs);
  745. }
  746. // FIXME: Use ::testing::Combine() when llvm updates its copy of googletest.
  747. INSTANTIATE_TEST_CASE_P(ParameterizedCovMapTest, CoverageMappingTest,
  748. ::testing::Values(std::pair<bool, bool>({false, false}),
  749. std::pair<bool, bool>({false, true}),
  750. std::pair<bool, bool>({true, false}),
  751. std::pair<bool, bool>({true, true})),);
  752. } // end anonymous namespace