aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
diff options
context:
space:
mode:
authorVedant Kumar <vsk@apple.com>2016-06-17 21:53:31 +0000
committerVedant Kumar <vsk@apple.com>2016-06-17 21:53:31 +0000
commit8039e92ac5e7b0756e0d1f88156546030e7e3fff (patch)
tree4c1bddb49782048acb9a18bebff3ddc42be4e58f /llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
parent93e3212c7cb20a03db5148e7f0458b9a848e3624 (diff)
downloadllvm-8039e92ac5e7b0756e0d1f88156546030e7e3fff.zip
llvm-8039e92ac5e7b0756e0d1f88156546030e7e3fff.tar.gz
llvm-8039e92ac5e7b0756e0d1f88156546030e7e3fff.tar.bz2
[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. llvm-svn: 273055
Diffstat (limited to 'llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp')
-rw-r--r--llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp57
1 files changed, 49 insertions, 8 deletions
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
index 8ff90d6..4ac3ab3 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
@@ -18,14 +18,6 @@
using namespace llvm;
using namespace coverage;
-void CoverageFilenamesSectionWriter::write(raw_ostream &OS) {
- encodeULEB128(Filenames.size(), OS);
- for (const auto &Filename : Filenames) {
- encodeULEB128(Filename.size(), OS);
- OS << Filename;
- }
-}
-
namespace {
/// \brief Gather only the expressions that are used by the mapping
/// regions in this function.
@@ -181,3 +173,52 @@ void CoverageMappingWriter::write(raw_ostream &OS) {
// Ensure that all file ids have at least one mapping region.
assert(CurrentFileID == (VirtualFileMapping.size() - 1));
}
+
+/// \brief Encode coverage data into \p OS.
+static void encodeCoverageData(ArrayRef<std::string> Filenames,
+ ArrayRef<std::string> CoverageMappings,
+ size_t &FilenamesSize,
+ size_t &CoverageMappingsSize, raw_ostream &OS) {
+ size_t OSOffset = OS.GetNumBytesInBuffer();
+
+ // Encode the filenames.
+ encodeULEB128(Filenames.size(), OS);
+ for (const auto &Filename : Filenames) {
+ encodeULEB128(Filename.size(), OS);
+ OS << Filename;
+ }
+
+ FilenamesSize = OS.GetNumBytesInBuffer() - OSOffset;
+
+ // Encode the coverage mappings.
+ for (const auto &RawMapping : CoverageMappings)
+ OS << RawMapping;
+
+ // Pad the output stream to an 8-byte boundary. Account for the padding bytes
+ // in \p CoverageMappingsSize.
+ if (size_t Rem = OS.GetNumBytesInBuffer() % 8) {
+ CoverageMappingsSize += 8 - Rem;
+ for (size_t I = 0, S = 8 - Rem; I < S; ++I)
+ OS << '\0';
+ }
+
+ CoverageMappingsSize = OS.GetNumBytesInBuffer() - FilenamesSize - OSOffset;
+}
+
+namespace llvm {
+namespace coverage {
+
+Expected<std::string> encodeFilenamesAndRawMappings(
+ ArrayRef<std::string> Filenames, ArrayRef<std::string> CoverageMappings,
+ size_t &FilenamesSize, size_t &CoverageMappingsSize) {
+ std::string CoverageData;
+ {
+ raw_string_ostream OS{CoverageData};
+ encodeCoverageData(Filenames, CoverageMappings, FilenamesSize,
+ CoverageMappingsSize, OS);
+ }
+ return std::move(CoverageData);
+}
+
+} // end namespace coverage
+} // end namespace llvm