LCOV - code coverage report
Current view: top level - include/llvm/DebugInfo/CodeView - CVRecord.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 23 26 88.5 %
Date: 2018-05-20 00:06:23 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- RecordIterator.h -----------------------------------------*- C++ -*-===//
       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             : #ifndef LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
      11             : #define LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
      12             : 
      13             : #include "llvm/ADT/ArrayRef.h"
      14             : #include "llvm/ADT/Optional.h"
      15             : #include "llvm/DebugInfo/CodeView/CodeViewError.h"
      16             : #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
      17             : #include "llvm/DebugInfo/CodeView/TypeIndex.h"
      18             : #include "llvm/Support/BinaryStreamReader.h"
      19             : #include "llvm/Support/BinaryStreamRef.h"
      20             : #include "llvm/Support/Endian.h"
      21             : #include "llvm/Support/Error.h"
      22             : #include <cstdint>
      23             : 
      24             : namespace llvm {
      25             : 
      26             : namespace codeview {
      27             : 
      28       64242 : template <typename Kind> class CVRecord {
      29             : public:
      30       72612 :   CVRecord() : Type(static_cast<Kind>(0)) {}
      31             : 
      32        5750 :   CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
      33             : 
      34             :   bool valid() const { return Type != static_cast<Kind>(0); }
      35             : 
      36       16869 :   uint32_t length() const { return RecordData.size(); }
      37             :   Kind kind() const { return Type; }
      38             :   ArrayRef<uint8_t> data() const { return RecordData; }
      39             :   StringRef str_data() const {
      40        1134 :     return StringRef(reinterpret_cast<const char *>(RecordData.data()),
      41        1134 :                      RecordData.size());
      42             :   }
      43             : 
      44             :   ArrayRef<uint8_t> content() const {
      45             :     return RecordData.drop_front(sizeof(RecordPrefix));
      46             :   }
      47             : 
      48             :   Optional<uint32_t> hash() const { return Hash; }
      49             : 
      50             :   void setHash(uint32_t Value) { Hash = Value; }
      51             : 
      52             :   Kind Type;
      53             :   ArrayRef<uint8_t> RecordData;
      54             :   Optional<uint32_t> Hash;
      55             : };
      56             : 
      57             : template <typename Kind> struct RemappedRecord {
      58             :   explicit RemappedRecord(const CVRecord<Kind> &R) : OriginalRecord(R) {}
      59             : 
      60             :   CVRecord<Kind> OriginalRecord;
      61             :   SmallVector<std::pair<uint32_t, TypeIndex>, 8> Mappings;
      62             : };
      63             : 
      64             : template <typename Record, typename Func>
      65         230 : Error forEachCodeViewRecord(ArrayRef<uint8_t> StreamBuffer, Func F) {
      66        2278 :   while (!StreamBuffer.empty()) {
      67        2048 :     if (StreamBuffer.size() < sizeof(RecordPrefix))
      68           0 :       return make_error<CodeViewError>(cv_error_code::corrupt_record);
      69             : 
      70             :     const RecordPrefix *Prefix =
      71             :         reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());
      72             : 
      73        2048 :     size_t RealLen = Prefix->RecordLen + 2;
      74        2048 :     if (StreamBuffer.size() < RealLen)
      75           0 :       return make_error<CodeViewError>(cv_error_code::corrupt_record);
      76             : 
      77             :     ArrayRef<uint8_t> Data = StreamBuffer.take_front(RealLen);
      78             :     StreamBuffer = StreamBuffer.drop_front(RealLen);
      79             : 
      80             :     Record R(static_cast<decltype(Record::Type)>((uint16_t)Prefix->RecordKind),
      81             :              Data);
      82        2555 :     if (auto EC = F(R))
      83             :       return EC;
      84             :   }
      85             :   return Error::success();
      86             : }
      87             : 
      88             : /// Read a complete record from a stream at a random offset.
      89             : template <typename Kind>
      90       10284 : inline Expected<CVRecord<Kind>> readCVRecordFromStream(BinaryStreamRef Stream,
      91             :                                                        uint32_t Offset) {
      92       10284 :   const RecordPrefix *Prefix = nullptr;
      93       20568 :   BinaryStreamReader Reader(Stream);
      94             :   Reader.setOffset(Offset);
      95             : 
      96       20568 :   if (auto EC = Reader.readObject(Prefix))
      97             :     return std::move(EC);
      98       20566 :   if (Prefix->RecordLen < 2)
      99           0 :     return make_error<CodeViewError>(cv_error_code::corrupt_record);
     100             :   Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
     101             : 
     102             :   Reader.setOffset(Offset);
     103       10283 :   ArrayRef<uint8_t> RawData;
     104       20566 :   if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
     105             :     return std::move(EC);
     106             :   return codeview::CVRecord<Kind>(K, RawData);
     107             : }
     108             : 
     109             : } // end namespace codeview
     110             : 
     111             : template <typename Kind>
     112             : struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
     113       10253 :   Error operator()(BinaryStreamRef Stream, uint32_t &Len,
     114             :                    codeview::CVRecord<Kind> &Item) {
     115       20506 :     auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
     116       10253 :     if (!ExpectedRec)
     117             :       return ExpectedRec.takeError();
     118             :     Item = *ExpectedRec;
     119       10252 :     Len = ExpectedRec->length();
     120             :     return Error::success();
     121             :   }
     122             : };
     123             : 
     124             : } // end namespace llvm
     125             : 
     126             : #endif // LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H

Generated by: LCOV version 1.13