From 520a8298d8ef676b5da617ba3d2c7fa37381e939 Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Fri, 17 Jun 2016 21:53:31 +0000 Subject: [PATCH] [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 --- .../ProfileData/Coverage/CoverageMappingWriter.h | 24 +++++---- lib/ProfileData/Coverage/CoverageMappingWriter.cpp | 57 +++++++++++++++++++--- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/include/llvm/ProfileData/Coverage/CoverageMappingWriter.h b/include/llvm/ProfileData/Coverage/CoverageMappingWriter.h index 10269cc50f3..b70bc6f269a 100644 --- a/include/llvm/ProfileData/Coverage/CoverageMappingWriter.h +++ b/include/llvm/ProfileData/Coverage/CoverageMappingWriter.h @@ -23,19 +23,6 @@ namespace llvm { namespace coverage { -/// \brief Writer of the filenames section for the instrumentation -/// based code coverage. -class CoverageFilenamesSectionWriter { - ArrayRef Filenames; - -public: - CoverageFilenamesSectionWriter(ArrayRef Filenames) - : Filenames(Filenames) {} - - /// \brief Write encoded filenames to the given output stream. - void write(raw_ostream &OS); -}; - /// \brief Writer for instrumentation based coverage mapping data. class CoverageMappingWriter { ArrayRef VirtualFileMapping; @@ -57,6 +44,17 @@ public: void write(raw_ostream &OS); }; +/// \brief Encode a list of filenames and raw coverage mapping data using the +/// latest coverage data format. +/// +/// Set \p FilenamesSize to the size of the filenames section. +/// +/// Set \p CoverageMappingsSize to the size of the coverage mapping section +/// (including any necessary padding bytes). +Expected encodeFilenamesAndRawMappings( + ArrayRef Filenames, ArrayRef CoverageMappings, + size_t &FilenamesSize, size_t &CoverageMappingsSize); + } // end namespace coverage } // end namespace llvm diff --git a/lib/ProfileData/Coverage/CoverageMappingWriter.cpp b/lib/ProfileData/Coverage/CoverageMappingWriter.cpp index 8ff90d62cfd..4ac3ab3311c 100644 --- a/lib/ProfileData/Coverage/CoverageMappingWriter.cpp +++ b/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 Filenames, + ArrayRef 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 encodeFilenamesAndRawMappings( + ArrayRef Filenames, ArrayRef 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 -- 2.11.0