clang-tools  7.0.0
BitcodeWriter.h
Go to the documentation of this file.
1 //===-- BitcodeWriter.h - ClangDoc Bitcode Writer --------------*- 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 // This file implements a writer for serializing the clang-doc internal
11 // representation to LLVM bitcode. The writer takes in a stream and emits the
12 // generated bitcode to that stream.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
17 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
18 
19 #include "Representation.h"
20 #include "clang/AST/AST.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Bitcode/BitstreamWriter.h"
25 #include <initializer_list>
26 #include <vector>
27 
28 namespace clang {
29 namespace doc {
30 
31 // Current version number of clang-doc bitcode.
32 // Should be bumped when removing or changing BlockIds, RecordIds, or
33 // BitCodeConstants, though they can be added without breaking it.
34 static const unsigned VersionNumber = 2;
35 
37  static constexpr unsigned RecordSize = 32U;
38  static constexpr unsigned SignatureBitSize = 8U;
39  static constexpr unsigned SubblockIDSize = 4U;
40  static constexpr unsigned BoolSize = 1U;
41  static constexpr unsigned IntSize = 16U;
42  static constexpr unsigned StringLengthSize = 16U;
43  static constexpr unsigned FilenameLengthSize = 16U;
44  static constexpr unsigned LineNumberSize = 16U;
45  static constexpr unsigned ReferenceTypeSize = 8U;
46  static constexpr unsigned USRLengthSize = 6U;
47  static constexpr unsigned USRBitLengthSize = 8U;
48  static constexpr char Signature[4] = {'D', 'O', 'C', 'S'};
49  static constexpr int USRHashSize = 20;
50 };
51 
52 // New Ids need to be added to both the enum here and the relevant IdNameMap in
53 // the implementation file.
54 enum BlockId {
55  BI_VERSION_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID,
67 };
68 
69 // New Ids need to be added to the enum here, and to the relevant IdNameMap and
70 // initialization list in the implementation file.
71 #define INFORECORDS(X) X##_USR, X##_NAME
72 
73 enum RecordId {
74  VERSION = 1,
75  INFORECORDS(FUNCTION),
94  INFORECORDS(NAMESPACE),
95  INFORECORDS(ENUM),
100  INFORECORDS(RECORD),
110 };
111 
112 static constexpr unsigned BlockIdCount = BI_LAST - BI_FIRST;
113 static constexpr unsigned RecordIdCount = RI_LAST - RI_FIRST;
114 
115 #undef INFORECORDS
116 
117 // Identifiers for differentiating between subblocks
119 
121 public:
122  ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream) : Stream(Stream) {
123  emitHeader();
124  emitBlockInfoBlock();
125  emitVersionBlock();
126  }
127 
128  // Write a specific info to a bitcode stream.
129  bool dispatchInfoForWrite(Info *I);
130 
131  // Block emission of different info types.
132  void emitBlock(const NamespaceInfo &I);
133  void emitBlock(const RecordInfo &I);
134  void emitBlock(const FunctionInfo &I);
135  void emitBlock(const EnumInfo &I);
136  void emitBlock(const TypeInfo &B);
137  void emitBlock(const FieldTypeInfo &B);
138  void emitBlock(const MemberTypeInfo &B);
139  void emitBlock(const CommentInfo &B);
140  void emitBlock(const Reference &B, FieldId F);
141 
142 private:
143  class AbbreviationMap {
144  llvm::DenseMap<unsigned, unsigned> Abbrevs;
145 
146  public:
147  AbbreviationMap() : Abbrevs(RecordIdCount) {}
148 
149  void add(RecordId RID, unsigned AbbrevID);
150  unsigned get(RecordId RID) const;
151  };
152 
153  class StreamSubBlockGuard {
154  llvm::BitstreamWriter &Stream;
155 
156  public:
157  StreamSubBlockGuard(llvm::BitstreamWriter &Stream_, BlockId ID)
158  : Stream(Stream_) {
159  // NOTE: SubBlockIDSize could theoretically be calculated on the fly,
160  // based on the initialization list of records in each block.
161  Stream.EnterSubblock(ID, BitCodeConstants::SubblockIDSize);
162  }
163 
164  StreamSubBlockGuard() = default;
165  StreamSubBlockGuard(const StreamSubBlockGuard &) = delete;
166  StreamSubBlockGuard &operator=(const StreamSubBlockGuard &) = delete;
167 
168  ~StreamSubBlockGuard() { Stream.ExitBlock(); }
169  };
170 
171  // Emission of validation and overview blocks.
172  void emitHeader();
173  void emitVersionBlock();
174  void emitRecordID(RecordId ID);
175  void emitBlockID(BlockId ID);
176  void emitBlockInfoBlock();
177  void emitBlockInfo(BlockId BID, const std::vector<RecordId> &RIDs);
178 
179  // Emission of individual record types.
180  void emitRecord(StringRef Str, RecordId ID);
181  void emitRecord(const SymbolID &Str, RecordId ID);
182  void emitRecord(const Location &Loc, RecordId ID);
183  void emitRecord(const Reference &Ref, RecordId ID);
184  void emitRecord(bool Value, RecordId ID);
185  void emitRecord(int Value, RecordId ID);
186  void emitRecord(unsigned Value, RecordId ID);
187  bool prepRecordData(RecordId ID, bool ShouldEmit = true);
188 
189  // Emission of appropriate abbreviation type.
190  void emitAbbrev(RecordId ID, BlockId Block);
191 
192  // Static size is the maximum length of the block/record names we're pushing
193  // to this + 1. Longest is currently `MemberTypeBlock` at 15 chars.
194  SmallVector<uint32_t, BitCodeConstants::RecordSize> Record;
195  llvm::BitstreamWriter &Stream;
196  AbbreviationMap Abbrevs;
197 };
198 
199 } // namespace doc
200 } // namespace clang
201 
202 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
SourceLocation Loc
&#39;#&#39; location in the include directive
static constexpr unsigned SignatureBitSize
Definition: BitcodeWriter.h:38
static const unsigned VersionNumber
Definition: BitcodeWriter.h:34
static constexpr unsigned SubblockIDSize
Definition: BitcodeWriter.h:39
static constexpr unsigned BlockIdCount
ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream)
llvm::SmallVector< uint64_t, 1024 > Record
static constexpr unsigned BoolSize
Definition: BitcodeWriter.h:40
static constexpr unsigned LineNumberSize
Definition: BitcodeWriter.h:44
static constexpr char Signature[4]
Definition: BitcodeWriter.h:48
static constexpr unsigned RecordSize
Definition: BitcodeWriter.h:37
static constexpr int USRHashSize
Definition: BitcodeWriter.h:49
static constexpr unsigned ReferenceTypeSize
Definition: BitcodeWriter.h:45
A base struct for Infos.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static constexpr unsigned IntSize
Definition: BitcodeWriter.h:41
static constexpr unsigned USRBitLengthSize
Definition: BitcodeWriter.h:47
static constexpr unsigned USRLengthSize
Definition: BitcodeWriter.h:46
static constexpr unsigned StringLengthSize
Definition: BitcodeWriter.h:42
std::array< uint8_t, 20 > SymbolID
static constexpr unsigned FilenameLengthSize
Definition: BitcodeWriter.h:43
static constexpr unsigned RecordIdCount