llvm.org GIT mirror llvm / 520a829
[Coverage] Move logic to encode filenames and mappings into llvm (NFC) Currently, frontends which emit source-based code coverage have to duplicate logic to encode filenames and raw coverage mappings properly. This violates an abstraction layer and forces frontends to copy tricky code. Introduce llvm::coverage::encodeFilenamesAndRawMappings() to take care of this. This will help us experiment with zlib-compressing coverage mapping data. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273055 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 4 years ago
2 changed file(s) with 60 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
2222 namespace llvm {
2323 namespace coverage {
2424
25 /// \brief Writer of the filenames section for the instrumentation
26 /// based code coverage.
27 class CoverageFilenamesSectionWriter {
28 ArrayRef Filenames;
29
30 public:
31 CoverageFilenamesSectionWriter(ArrayRef Filenames)
32 : Filenames(Filenames) {}
33
34 /// \brief Write encoded filenames to the given output stream.
35 void write(raw_ostream &OS);
36 };
37
3825 /// \brief Writer for instrumentation based coverage mapping data.
3926 class CoverageMappingWriter {
4027 ArrayRef VirtualFileMapping;
5643 void write(raw_ostream &OS);
5744 };
5845
46 /// \brief Encode a list of filenames and raw coverage mapping data using the
47 /// latest coverage data format.
48 ///
49 /// Set \p FilenamesSize to the size of the filenames section.
50 ///
51 /// Set \p CoverageMappingsSize to the size of the coverage mapping section
52 /// (including any necessary padding bytes).
53 Expected encodeFilenamesAndRawMappings(
54 ArrayRef Filenames, ArrayRef CoverageMappings,
55 size_t &FilenamesSize, size_t &CoverageMappingsSize);
56
5957 } // end namespace coverage
6058 } // end namespace llvm
6159
1616
1717 using namespace llvm;
1818 using namespace coverage;
19
20 void CoverageFilenamesSectionWriter::write(raw_ostream &OS) {
21 encodeULEB128(Filenames.size(), OS);
22 for (const auto &Filename : Filenames) {
23 encodeULEB128(Filename.size(), OS);
24 OS << Filename;
25 }
26 }
2719
2820 namespace {
2921 /// \brief Gather only the expressions that are used by the mapping
180172 // Ensure that all file ids have at least one mapping region.
181173 assert(CurrentFileID == (VirtualFileMapping.size() - 1));
182174 }
175
176 /// \brief Encode coverage data into \p OS.
177 static void encodeCoverageData(ArrayRef Filenames,
178 ArrayRef CoverageMappings,
179 size_t &FilenamesSize,
180 size_t &CoverageMappingsSize, raw_ostream &OS) {
181 size_t OSOffset = OS.GetNumBytesInBuffer();
182
183 // Encode the filenames.
184 encodeULEB128(Filenames.size(), OS);
185 for (const auto &Filename : Filenames) {
186 encodeULEB128(Filename.size(), OS);
187 OS << Filename;
188 }
189
190 FilenamesSize = OS.GetNumBytesInBuffer() - OSOffset;
191
192 // Encode the coverage mappings.
193 for (const auto &RawMapping : CoverageMappings)
194 OS << RawMapping;
195
196 // Pad the output stream to an 8-byte boundary. Account for the padding bytes
197 // in \p CoverageMappingsSize.
198 if (size_t Rem = OS.GetNumBytesInBuffer() % 8) {
199 CoverageMappingsSize += 8 - Rem;
200 for (size_t I = 0, S = 8 - Rem; I < S; ++I)
201 OS << '\0';
202 }
203
204 CoverageMappingsSize = OS.GetNumBytesInBuffer() - FilenamesSize - OSOffset;
205 }
206
207 namespace llvm {
208 namespace coverage {
209
210 Expected encodeFilenamesAndRawMappings(
211 ArrayRef Filenames, ArrayRef CoverageMappings,
212 size_t &FilenamesSize, size_t &CoverageMappingsSize) {
213 std::string CoverageData;
214 {
215 raw_string_ostream OS{CoverageData};
216 encodeCoverageData(Filenames, CoverageMappings, FilenamesSize,
217 CoverageMappingsSize, OS);
218 }
219 return std::move(CoverageData);
220 }
221
222 } // end namespace coverage
223 } // end namespace llvm