LCOV - code coverage report
Current view: top level - include/llvm/DebugInfo/CodeView - CVRecord.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 40 43 93.0 %
Date: 2018-10-20 13:21:21 Functions: 6 11 54.5 %
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       80158 : template <typename Kind> class CVRecord {
      29             : public:
      30       91405 :   CVRecord() : Type(static_cast<Kind>(0)) {}
      31             : 
      32        5092 :   CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
      33             : 
      34           0 :   bool valid() const { return Type != static_cast<Kind>(0); }
      35             : 
      36       16102 :   uint32_t length() const { return RecordData.size(); }
      37           0 :   Kind kind() const { return Type; }
      38           0 :   ArrayRef<uint8_t> data() const { return RecordData; }
      39             :   StringRef str_data() const {
      40        1721 :     return StringRef(reinterpret_cast<const char *>(RecordData.data()),
      41        1721 :                      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         270 : Error forEachCodeViewRecord(ArrayRef<uint8_t> StreamBuffer, Func F) {
      66        2543 :   while (!StreamBuffer.empty()) {
      67        2273 :     if (StreamBuffer.size() < sizeof(RecordPrefix))
      68             :       return make_error<CodeViewError>(cv_error_code::corrupt_record);
      69             : 
      70             :     const RecordPrefix *Prefix =
      71             :         reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());
      72             : 
      73        2273 :     size_t RealLen = Prefix->RecordLen + 2;
      74        2273 :     if (StreamBuffer.size() < RealLen)
      75             :       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        4546 :     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       13098 : inline Expected<CVRecord<Kind>> readCVRecordFromStream(BinaryStreamRef Stream,
      91             :                                                        uint32_t Offset) {
      92       13098 :   const RecordPrefix *Prefix = nullptr;
      93       26196 :   BinaryStreamReader Reader(Stream);
      94             :   Reader.setOffset(Offset);
      95             : 
      96       26196 :   if (auto EC = Reader.readObject(Prefix))
      97             :     return std::move(EC);
      98       26194 :   if (Prefix->RecordLen < 2)
      99             :     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       13097 :   ArrayRef<uint8_t> RawData;
     104       26194 :   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         590 : 
     109             : } // end namespace codeview
     110         590 : 
     111        1180 : template <typename Kind>
     112             : struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
     113       12387 :   Error operator()(BinaryStreamRef Stream, uint32_t &Len,
     114        1180 :                    codeview::CVRecord<Kind> &Item) {
     115       24774 :     auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
     116       13567 :     if (!ExpectedRec)
     117             :       return ExpectedRec.takeError();
     118             :     Item = *ExpectedRec;
     119       12386 :     Len = ExpectedRec->length();
     120             :     return Error::success();
     121         590 :   }
     122        1180 : };
     123             : 
     124             : } // end namespace llvm
     125             : 
     126          90 : #endif // LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H

Generated by: LCOV version 1.13