2
0

CoverageMappingTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #include "llvm/ProfileData/CoverageMapping.h"
  10. #include "llvm/ProfileData/CoverageMappingReader.h"
  11. #include "llvm/ProfileData/CoverageMappingWriter.h"
  12. #include "llvm/ProfileData/InstrProfReader.h"
  13. #include "llvm/ProfileData/InstrProfWriter.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include "gtest/gtest.h"
  16. #include <sstream>
  17. using namespace llvm;
  18. using namespace coverage;
  19. static ::testing::AssertionResult NoError(std::error_code EC) {
  20. if (!EC)
  21. return ::testing::AssertionSuccess();
  22. return ::testing::AssertionFailure() << "error " << EC.value()
  23. << ": " << EC.message();
  24. }
  25. namespace llvm {
  26. namespace coverage {
  27. void PrintTo(const Counter &C, ::std::ostream *os) {
  28. if (C.isZero())
  29. *os << "Zero";
  30. else if (C.isExpression())
  31. *os << "Expression " << C.getExpressionID();
  32. else
  33. *os << "Counter " << C.getCounterID();
  34. }
  35. void PrintTo(const CoverageSegment &S, ::std::ostream *os) {
  36. *os << "CoverageSegment(" << S.Line << ", " << S.Col << ", ";
  37. if (S.HasCount)
  38. *os << S.Count << ", ";
  39. *os << (S.IsRegionEntry ? "true" : "false") << ")";
  40. }
  41. }
  42. }
  43. namespace {
  44. struct OneFunctionCoverageReader : CoverageMappingReader {
  45. StringRef Name;
  46. uint64_t Hash;
  47. std::vector<StringRef> Filenames;
  48. ArrayRef<CounterMappingRegion> Regions;
  49. bool Done;
  50. OneFunctionCoverageReader(StringRef Name, uint64_t Hash,
  51. ArrayRef<StringRef> Filenames,
  52. ArrayRef<CounterMappingRegion> Regions)
  53. : Name(Name), Hash(Hash), Filenames(Filenames), Regions(Regions),
  54. Done(false) {}
  55. std::error_code readNextRecord(CoverageMappingRecord &Record) override {
  56. if (Done)
  57. return instrprof_error::eof;
  58. Done = true;
  59. Record.FunctionName = Name;
  60. Record.FunctionHash = Hash;
  61. Record.Filenames = Filenames;
  62. Record.Expressions = {};
  63. Record.MappingRegions = Regions;
  64. return instrprof_error::success;
  65. }
  66. };
  67. struct CoverageMappingTest : ::testing::Test {
  68. StringMap<unsigned> Files;
  69. unsigned NextFile;
  70. std::vector<CounterMappingRegion> InputCMRs;
  71. std::vector<StringRef> OutputFiles;
  72. std::vector<CounterExpression> OutputExpressions;
  73. std::vector<CounterMappingRegion> OutputCMRs;
  74. InstrProfWriter ProfileWriter;
  75. std::unique_ptr<IndexedInstrProfReader> ProfileReader;
  76. std::unique_ptr<CoverageMapping> LoadedCoverage;
  77. void SetUp() override {
  78. NextFile = 0;
  79. }
  80. unsigned getFile(StringRef Name) {
  81. auto R = Files.find(Name);
  82. if (R != Files.end())
  83. return R->second;
  84. Files[Name] = NextFile;
  85. return NextFile++;
  86. }
  87. void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
  88. unsigned CE) {
  89. InputCMRs.push_back(
  90. CounterMappingRegion::makeRegion(C, getFile(File), LS, CS, LE, CE));
  91. }
  92. void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
  93. unsigned CS, unsigned LE, unsigned CE) {
  94. InputCMRs.push_back(CounterMappingRegion::makeExpansion(
  95. getFile(File), getFile(ExpandedFile), LS, CS, LE, CE));
  96. }
  97. std::string writeCoverageRegions() {
  98. SmallVector<unsigned, 8> FileIDs;
  99. for (const auto &E : Files)
  100. FileIDs.push_back(E.getValue());
  101. std::string Coverage;
  102. llvm::raw_string_ostream OS(Coverage);
  103. CoverageMappingWriter(FileIDs, None, InputCMRs).write(OS);
  104. return OS.str();
  105. }
  106. void readCoverageRegions(std::string Coverage) {
  107. SmallVector<StringRef, 8> Filenames;
  108. for (const auto &E : Files)
  109. Filenames.push_back(E.getKey());
  110. RawCoverageMappingReader Reader(Coverage, Filenames, OutputFiles,
  111. OutputExpressions, OutputCMRs);
  112. ASSERT_TRUE(NoError(Reader.read()));
  113. }
  114. void readProfCounts() {
  115. auto Profile = ProfileWriter.writeBuffer();
  116. auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
  117. ASSERT_TRUE(NoError(ReaderOrErr.getError()));
  118. ProfileReader = std::move(ReaderOrErr.get());
  119. }
  120. void loadCoverageMapping(StringRef FuncName, uint64_t Hash) {
  121. std::string Regions = writeCoverageRegions();
  122. readCoverageRegions(Regions);
  123. SmallVector<StringRef, 8> Filenames;
  124. for (const auto &E : Files)
  125. Filenames.push_back(E.getKey());
  126. OneFunctionCoverageReader CovReader(FuncName, Hash, Filenames, OutputCMRs);
  127. auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader);
  128. ASSERT_TRUE(NoError(CoverageOrErr.getError()));
  129. LoadedCoverage = std::move(CoverageOrErr.get());
  130. }
  131. };
  132. TEST_F(CoverageMappingTest, basic_write_read) {
  133. addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
  134. addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
  135. addCMR(Counter::getZero(), "foo", 3, 1, 3, 4);
  136. addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
  137. addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
  138. std::string Coverage = writeCoverageRegions();
  139. readCoverageRegions(Coverage);
  140. size_t N = makeArrayRef(InputCMRs).size();
  141. ASSERT_EQ(N, OutputCMRs.size());
  142. for (size_t I = 0; I < N; ++I) {
  143. ASSERT_EQ(InputCMRs[I].Count, OutputCMRs[I].Count);
  144. ASSERT_EQ(InputCMRs[I].FileID, OutputCMRs[I].FileID);
  145. ASSERT_EQ(InputCMRs[I].startLoc(), OutputCMRs[I].startLoc());
  146. ASSERT_EQ(InputCMRs[I].endLoc(), OutputCMRs[I].endLoc());
  147. ASSERT_EQ(InputCMRs[I].Kind, OutputCMRs[I].Kind);
  148. }
  149. }
  150. TEST_F(CoverageMappingTest, expansion_gets_first_counter) {
  151. addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
  152. // This starts earlier in "foo", so the expansion should get its counter.
  153. addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
  154. addExpansionCMR("bar", "foo", 3, 3, 3, 3);
  155. std::string Coverage = writeCoverageRegions();
  156. readCoverageRegions(Coverage);
  157. ASSERT_EQ(CounterMappingRegion::ExpansionRegion, OutputCMRs[2].Kind);
  158. ASSERT_EQ(Counter::getCounter(2), OutputCMRs[2].Count);
  159. ASSERT_EQ(3U, OutputCMRs[2].LineStart);
  160. }
  161. TEST_F(CoverageMappingTest, basic_coverage_iteration) {
  162. ProfileWriter.addFunctionCounts("func", 0x1234, {30, 20, 10, 0});
  163. readProfCounts();
  164. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  165. addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
  166. addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
  167. addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
  168. loadCoverageMapping("func", 0x1234);
  169. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  170. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  171. ASSERT_EQ(7U, Segments.size());
  172. ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments[0]);
  173. ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
  174. ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments[2]);
  175. ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
  176. ASSERT_EQ(CoverageSegment(9, 9, false), Segments[4]);
  177. ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
  178. ASSERT_EQ(CoverageSegment(11, 11, false), Segments[6]);
  179. }
  180. TEST_F(CoverageMappingTest, uncovered_function) {
  181. readProfCounts();
  182. addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
  183. loadCoverageMapping("func", 0x1234);
  184. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  185. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  186. ASSERT_EQ(2U, Segments.size());
  187. ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
  188. ASSERT_EQ(CoverageSegment(3, 4, false), Segments[1]);
  189. }
  190. TEST_F(CoverageMappingTest, uncovered_function_with_mapping) {
  191. readProfCounts();
  192. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  193. addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
  194. loadCoverageMapping("func", 0x1234);
  195. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  196. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  197. ASSERT_EQ(3U, Segments.size());
  198. ASSERT_EQ(CoverageSegment(1, 1, 0, true), Segments[0]);
  199. ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
  200. ASSERT_EQ(CoverageSegment(9, 9, false), Segments[2]);
  201. }
  202. TEST_F(CoverageMappingTest, combine_regions) {
  203. ProfileWriter.addFunctionCounts("func", 0x1234, {10, 20, 30});
  204. readProfCounts();
  205. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  206. addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
  207. addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
  208. loadCoverageMapping("func", 0x1234);
  209. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  210. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  211. ASSERT_EQ(4U, Segments.size());
  212. ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
  213. ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
  214. ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
  215. ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
  216. }
  217. TEST_F(CoverageMappingTest, dont_combine_expansions) {
  218. ProfileWriter.addFunctionCounts("func", 0x1234, {10, 20});
  219. readProfCounts();
  220. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  221. addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
  222. addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
  223. addExpansionCMR("file1", "include1", 3, 3, 4, 4);
  224. loadCoverageMapping("func", 0x1234);
  225. CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
  226. std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
  227. ASSERT_EQ(4U, Segments.size());
  228. ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
  229. ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
  230. ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
  231. ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
  232. }
  233. TEST_F(CoverageMappingTest, strip_filename_prefix) {
  234. ProfileWriter.addFunctionCounts("file1:func", 0x1234, {10});
  235. readProfCounts();
  236. addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
  237. loadCoverageMapping("file1:func", 0x1234);
  238. std::vector<std::string> Names;
  239. for (const auto &Func : LoadedCoverage->getCoveredFunctions())
  240. Names.push_back(Func.Name);
  241. ASSERT_EQ(1U, Names.size());
  242. ASSERT_EQ("func", Names[0]);
  243. }
  244. } // end anonymous namespace