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/CoverageMapping.h"
11 #include "llvm/ProfileData/CoverageMappingReader.h"
12 #include "llvm/ProfileData/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 <sstream>
19 
20 using namespace llvm;
21 using namespace coverage;
22 
23 static ::testing::AssertionResult NoError(std::error_code EC) {
24   if (!EC)
25     return ::testing::AssertionSuccess();
26   return ::testing::AssertionFailure() << "error " << EC.value()
27                                        << ": " << EC.message();
28 }
29 
30 namespace llvm {
31 namespace coverage {
32 void PrintTo(const Counter &C, ::std::ostream *os) {
33   if (C.isZero())
34     *os << "Zero";
35   else if (C.isExpression())
36     *os << "Expression " << C.getExpressionID();
37   else
38     *os << "Counter " << C.getCounterID();
39 }
40 
41 void PrintTo(const CoverageSegment &S, ::std::ostream *os) {
42   *os << "CoverageSegment(" << S.Line << ", " << S.Col << ", ";
43   if (S.HasCount)
44     *os << S.Count << ", ";
45   *os << (S.IsRegionEntry ? "true" : "false") << ")";
46 }
47 }
48 }
49 
50 namespace {
51 
52 struct OneFunctionCoverageReader : CoverageMappingReader {
53   StringRef Name;
54   uint64_t Hash;
55   std::vector<StringRef> Filenames;
56   ArrayRef<CounterMappingRegion> Regions;
57   bool Done;
58 
59   OneFunctionCoverageReader(StringRef Name, uint64_t Hash,
60                             ArrayRef<StringRef> Filenames,
61                             ArrayRef<CounterMappingRegion> Regions)
62       : Name(Name), Hash(Hash), Filenames(Filenames), Regions(Regions),
63         Done(false) {}
64 
65   std::error_code readNextRecord(CoverageMappingRecord &Record) override {
66     if (Done)
67       return instrprof_error::eof;
68     Done = true;
69 
70     Record.FunctionName = Name;
71     Record.FunctionHash = Hash;
72     Record.Filenames = Filenames;
73     Record.Expressions = {};
74     Record.MappingRegions = Regions;
75     return instrprof_error::success;
76   }
77 };
78 
79 struct CoverageMappingTest : ::testing::Test {
80   StringMap<unsigned> Files;
81   std::vector<CounterMappingRegion> InputCMRs;
82 
83   std::vector<StringRef> OutputFiles;
84   std::vector<CounterExpression> OutputExpressions;
85   std::vector<CounterMappingRegion> OutputCMRs;
86 
87   InstrProfWriter ProfileWriter;
88   std::unique_ptr<IndexedInstrProfReader> ProfileReader;
89 
90   std::unique_ptr<CoverageMapping> LoadedCoverage;
91 
92   void SetUp() override {
93     ProfileWriter.setOutputSparse(false);
94   }
95 
96   unsigned getFile(StringRef Name) {
97     auto R = Files.find(Name);
98     if (R != Files.end())
99       return R->second;
100     unsigned Index = Files.size();
101     Files.emplace_second(Name, Index);
102     return Index;
103   }
104 
105   void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
106               unsigned CE) {
107     InputCMRs.push_back(
108         CounterMappingRegion::makeRegion(C, getFile(File), LS, CS, LE, CE));
109   }
110 
111   void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
112                        unsigned CS, unsigned LE, unsigned CE) {
113     InputCMRs.push_back(CounterMappingRegion::makeExpansion(
114         getFile(File), getFile(ExpandedFile), LS, CS, LE, CE));
115   }
116 
117   std::string writeCoverageRegions() {
118     SmallVector<unsigned, 8> FileIDs(Files.size());
119     for (unsigned I = 0; I < FileIDs.size(); ++I)
120       FileIDs[I] = I;
121     std::string Coverage;
122     llvm::raw_string_ostream OS(Coverage);
123     CoverageMappingWriter(FileIDs, None, InputCMRs).write(OS);
124     return OS.str();
125   }
126 
127   void readCoverageRegions(std::string Coverage) {
128     SmallVector<StringRef, 8> Filenames(Files.size());
129     for (const auto &E : Files)
130       Filenames[E.getValue()] = E.getKey();
131     RawCoverageMappingReader Reader(Coverage, Filenames, OutputFiles,
132                                     OutputExpressions, OutputCMRs);
133     ASSERT_TRUE(NoError(Reader.read()));
134   }
135 
136   void readProfCounts() {
137     auto Profile = ProfileWriter.writeBuffer();
138     auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
139     ASSERT_TRUE(NoError(ReaderOrErr.getError()));
140     ProfileReader = std::move(ReaderOrErr.get());
141   }
142 
143   void loadCoverageMapping(StringRef FuncName, uint64_t Hash,
144                            bool EmitFilenames = true) {
145     std::string Regions = writeCoverageRegions();
146     readCoverageRegions(Regions);
147 
148     SmallVector<StringRef, 8> Filenames;
149     if (EmitFilenames) {
150       Filenames.resize(Files.size());
151       for (const auto &E : Files)
152         Filenames[E.getValue()] = E.getKey();
153     }
154     OneFunctionCoverageReader CovReader(FuncName, Hash, Filenames, OutputCMRs);
155     auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader);
156     ASSERT_TRUE(NoError(CoverageOrErr.getError()));
157     LoadedCoverage = std::move(CoverageOrErr.get());
158   }
159 };
160 
161 struct MaybeSparseCoverageMappingTest
162     : public CoverageMappingTest,
163       public ::testing::WithParamInterface<bool> {
164   void SetUp() {
165     CoverageMappingTest::SetUp();
166     ProfileWriter.setOutputSparse(GetParam());
167   }
168 };
169 
170 TEST_P(MaybeSparseCoverageMappingTest, basic_write_read) {
171   addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
172   addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
173   addCMR(Counter::getZero(),     "foo", 3, 1, 3, 4);
174   addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
175   addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
176   std::string Coverage = writeCoverageRegions();
177   readCoverageRegions(Coverage);
178 
179   size_t N = makeArrayRef(InputCMRs).size();
180   ASSERT_EQ(N, OutputCMRs.size());
181   for (size_t I = 0; I < N; ++I) {
182     ASSERT_EQ(InputCMRs[I].Count,      OutputCMRs[I].Count);
183     ASSERT_EQ(InputCMRs[I].FileID,     OutputCMRs[I].FileID);
184     ASSERT_EQ(InputCMRs[I].startLoc(), OutputCMRs[I].startLoc());
185     ASSERT_EQ(InputCMRs[I].endLoc(),   OutputCMRs[I].endLoc());
186     ASSERT_EQ(InputCMRs[I].Kind,       OutputCMRs[I].Kind);
187   }
188 }
189 
190 TEST_P(MaybeSparseCoverageMappingTest,
191        correct_deserialize_for_more_than_two_files) {
192   const char *FileNames[] = {"bar", "baz", "foo"};
193   static const unsigned N = array_lengthof(FileNames);
194 
195   for (unsigned I = 0; I < N; ++I)
196     // Use LineStart to hold the index of the file name
197     // in order to preserve that information during possible sorting of CMRs.
198     addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
199 
200   std::string Coverage = writeCoverageRegions();
201   readCoverageRegions(Coverage);
202 
203   ASSERT_EQ(N, OutputCMRs.size());
204   ASSERT_EQ(N, OutputFiles.size());
205 
206   for (unsigned I = 0; I < N; ++I) {
207     ASSERT_GT(N, OutputCMRs[I].FileID);
208     ASSERT_GT(N, OutputCMRs[I].LineStart);
209     EXPECT_EQ(FileNames[OutputCMRs[I].LineStart],
210               OutputFiles[OutputCMRs[I].FileID]);
211   }
212 }
213 
214 TEST_P(MaybeSparseCoverageMappingTest, load_coverage_for_more_than_two_files) {
215   InstrProfRecord Record("func", 0x1234, {0});
216   ProfileWriter.addRecord(std::move(Record));
217   readProfCounts();
218 
219   const char *FileNames[] = {"bar", "baz", "foo"};
220   static const unsigned N = array_lengthof(FileNames);
221 
222   for (unsigned I = 0; I < N; ++I)
223     // Use LineStart to hold the index of the file name
224     // in order to preserve that information during possible sorting of CMRs.
225     addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
226 
227   loadCoverageMapping("func", 0x1234);
228 
229   for (unsigned I = 0; I < N; ++I) {
230     CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
231     ASSERT_TRUE(!Data.empty());
232     EXPECT_EQ(I, Data.begin()->Line);
233   }
234 }
235 
236 TEST_P(MaybeSparseCoverageMappingTest, expansion_gets_first_counter) {
237   addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
238   // This starts earlier in "foo", so the expansion should get its counter.
239   addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
240   addExpansionCMR("bar", "foo", 3, 3, 3, 3);
241   std::string Coverage = writeCoverageRegions();
242   readCoverageRegions(Coverage);
243 
244   ASSERT_EQ(CounterMappingRegion::ExpansionRegion, OutputCMRs[2].Kind);
245   ASSERT_EQ(Counter::getCounter(2), OutputCMRs[2].Count);
246   ASSERT_EQ(3U, OutputCMRs[2].LineStart);
247 }
248 
249 TEST_P(MaybeSparseCoverageMappingTest, basic_coverage_iteration) {
250   InstrProfRecord Record("func", 0x1234, {30, 20, 10, 0});
251   ProfileWriter.addRecord(std::move(Record));
252   readProfCounts();
253 
254   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
255   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
256   addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
257   addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
258   loadCoverageMapping("func", 0x1234);
259 
260   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
261   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
262   ASSERT_EQ(7U, Segments.size());
263   ASSERT_EQ(CoverageSegment(1, 1, 20, true),  Segments[0]);
264   ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
265   ASSERT_EQ(CoverageSegment(5, 8, 10, true),  Segments[2]);
266   ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
267   ASSERT_EQ(CoverageSegment(9, 9, false),     Segments[4]);
268   ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
269   ASSERT_EQ(CoverageSegment(11, 11, false),   Segments[6]);
270 }
271 
272 TEST_P(MaybeSparseCoverageMappingTest, uncovered_function) {
273   readProfCounts();
274 
275   addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
276   loadCoverageMapping("func", 0x1234);
277 
278   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
279   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
280   ASSERT_EQ(2U, Segments.size());
281   ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
282   ASSERT_EQ(CoverageSegment(3, 4, false),   Segments[1]);
283 }
284 
285 TEST_P(MaybeSparseCoverageMappingTest, uncovered_function_with_mapping) {
286   readProfCounts();
287 
288   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
289   addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
290   loadCoverageMapping("func", 0x1234);
291 
292   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
293   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
294   ASSERT_EQ(3U, Segments.size());
295   ASSERT_EQ(CoverageSegment(1, 1, 0, true),  Segments[0]);
296   ASSERT_EQ(CoverageSegment(4, 7, 0, false), Segments[1]);
297   ASSERT_EQ(CoverageSegment(9, 9, false),    Segments[2]);
298 }
299 
300 TEST_P(MaybeSparseCoverageMappingTest, combine_regions) {
301   InstrProfRecord Record("func", 0x1234, {10, 20, 30});
302   ProfileWriter.addRecord(std::move(Record));
303   readProfCounts();
304 
305   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
306   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
307   addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
308   loadCoverageMapping("func", 0x1234);
309 
310   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
311   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
312   ASSERT_EQ(4U, Segments.size());
313   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
314   ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
315   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
316   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
317 }
318 
319 TEST_P(MaybeSparseCoverageMappingTest, dont_combine_expansions) {
320   InstrProfRecord Record1("func", 0x1234, {10, 20});
321   InstrProfRecord Record2("func", 0x1234, {0, 0});
322   ProfileWriter.addRecord(std::move(Record1));
323   ProfileWriter.addRecord(std::move(Record2));
324   readProfCounts();
325 
326   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
327   addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
328   addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
329   addExpansionCMR("file1", "include1", 3, 3, 4, 4);
330   loadCoverageMapping("func", 0x1234);
331 
332   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
333   std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
334   ASSERT_EQ(4U, Segments.size());
335   ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
336   ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
337   ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
338   ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
339 }
340 
341 TEST_P(MaybeSparseCoverageMappingTest, strip_filename_prefix) {
342   InstrProfRecord Record("file1:func", 0x1234, {0});
343   ProfileWriter.addRecord(std::move(Record));
344   readProfCounts();
345 
346   addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
347   loadCoverageMapping("file1:func", 0x1234);
348 
349   std::vector<std::string> Names;
350   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
351     Names.push_back(Func.Name);
352   ASSERT_EQ(1U, Names.size());
353   ASSERT_EQ("func", Names[0]);
354 }
355 
356 TEST_P(MaybeSparseCoverageMappingTest, strip_unknown_filename_prefix) {
357   InstrProfRecord Record("<unknown>:func", 0x1234, {0});
358   ProfileWriter.addRecord(std::move(Record));
359   readProfCounts();
360 
361   addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
362   loadCoverageMapping("<unknown>:func", 0x1234, /*EmitFilenames=*/false);
363 
364   std::vector<std::string> Names;
365   for (const auto &Func : LoadedCoverage->getCoveredFunctions())
366     Names.push_back(Func.Name);
367   ASSERT_EQ(1U, Names.size());
368   ASSERT_EQ("func", Names[0]);
369 }
370 
371 INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseCoverageMappingTest,
372                         ::testing::Bool());
373 
374 } // end anonymous namespace
375