Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/AsmPrinter/ByteStreamer.h
Line
Count
Source (jump to first uncovered line)
1
//===-- llvm/CodeGen/ByteStreamer.h - ByteStreamer class --------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains a class that can take bytes that would normally be
10
// streamed via the AsmPrinter.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
15
#define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
16
17
#include "DIEHash.h"
18
#include "llvm/CodeGen/AsmPrinter.h"
19
#include "llvm/MC/MCStreamer.h"
20
#include "llvm/Support/LEB128.h"
21
#include <string>
22
23
namespace llvm {
24
class ByteStreamer {
25
 protected:
26
  ~ByteStreamer() = default;
27
  ByteStreamer(const ByteStreamer&) = default;
28
1.31k
  ByteStreamer() = default;
29
30
 public:
31
  // For now we're just handling the calls we need for dwarf emission/hashing.
32
  virtual void EmitInt8(uint8_t Byte, const Twine &Comment = "") = 0;
33
  virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
34
  virtual void EmitULEB128(uint64_t DWord, const Twine &Comment = "", unsigned PadTo = 0) = 0;
35
};
36
37
class APByteStreamer final : public ByteStreamer {
38
private:
39
  AsmPrinter &AP;
40
41
public:
42
642
  APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
43
1.54k
  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
44
1.54k
    AP.OutStreamer->AddComment(Comment);
45
1.54k
    AP.emitInt8(Byte);
46
1.54k
  }
47
0
  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
48
0
    AP.OutStreamer->AddComment(Comment);
49
0
    AP.EmitSLEB128(DWord);
50
0
  }
51
0
  void EmitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override {
52
0
    AP.OutStreamer->AddComment(Comment);
53
0
    AP.EmitULEB128(DWord);
54
0
  }
55
};
56
57
class HashingByteStreamer final : public ByteStreamer {
58
 private:
59
  DIEHash &Hash;
60
 public:
61
3
 HashingByteStreamer(DIEHash &H) : Hash(H) {}
62
7
  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
63
7
    Hash.update(Byte);
64
7
  }
65
0
  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
66
0
    Hash.addSLEB128(DWord);
67
0
  }
68
0
  void EmitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override {
69
0
    Hash.addULEB128(DWord);
70
0
  }
71
};
72
73
class BufferByteStreamer final : public ByteStreamer {
74
private:
75
  SmallVectorImpl<char> &Buffer;
76
  SmallVectorImpl<std::string> &Comments;
77
78
  /// Only verbose textual output needs comments.  This will be set to
79
  /// true for that case, and false otherwise.  If false, comments passed in to
80
  /// the emit methods will be ignored.
81
  bool GenerateComments;
82
83
public:
84
  BufferByteStreamer(SmallVectorImpl<char> &Buffer,
85
                     SmallVectorImpl<std::string> &Comments,
86
                     bool GenerateComments)
87
667
  : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {}
88
17.0k
  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
89
17.0k
    Buffer.push_back(Byte);
90
17.0k
    if (GenerateComments)
91
583
      Comments.push_back(Comment.str());
92
17.0k
  }
93
194
  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
94
194
    raw_svector_ostream OSE(Buffer);
95
194
    unsigned Length = encodeSLEB128(DWord, OSE);
96
194
    if (GenerateComments) {
97
88
      Comments.push_back(Comment.str());
98
88
      // Add some empty comments to keep the Buffer and Comments vectors aligned
99
88
      // with each other.
100
89
      for (size_t i = 1; i < Length; 
++i1
)
101
1
        Comments.push_back("");
102
88
103
88
    }
104
194
  }
105
12.1k
  void EmitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override {
106
12.1k
    raw_svector_ostream OSE(Buffer);
107
12.1k
    unsigned Length = encodeULEB128(DWord, OSE, PadTo);
108
12.1k
    if (GenerateComments) {
109
118
      Comments.push_back(Comment.str());
110
118
      // Add some empty comments to keep the Buffer and Comments vectors aligned
111
118
      // with each other.
112
162
      for (size_t i = 1; i < Length; 
++i44
)
113
44
        Comments.push_back("");
114
118
115
118
    }
116
12.1k
  }
117
};
118
119
}
120
121
#endif