clang-tools  14.0.0git
BitcodeWriter.h
Go to the documentation of this file.
1 //===-- BitcodeWriter.h - ClangDoc Bitcode Writer --------------*- 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 implements a writer for serializing the clang-doc internal
10 // representation to LLVM bitcode. The writer takes in a stream and emits the
11 // generated bitcode to that stream.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
16 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
17 
18 #include "Representation.h"
19 #include "clang/AST/AST.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Bitstream/BitstreamWriter.h"
24 #include <initializer_list>
25 #include <vector>
26 
27 namespace clang {
28 namespace doc {
29 
30 // Current version number of clang-doc bitcode.
31 // Should be bumped when removing or changing BlockIds, RecordIds, or
32 // BitCodeConstants, though they can be added without breaking it.
33 static const unsigned VersionNumber = 3;
34 
36  static constexpr unsigned RecordSize = 32U;
37  static constexpr unsigned SignatureBitSize = 8U;
38  static constexpr unsigned SubblockIDSize = 4U;
39  static constexpr unsigned BoolSize = 1U;
40  static constexpr unsigned IntSize = 16U;
41  static constexpr unsigned StringLengthSize = 16U;
42  static constexpr unsigned FilenameLengthSize = 16U;
43  static constexpr unsigned LineNumberSize = 32U;
44  static constexpr unsigned ReferenceTypeSize = 8U;
45  static constexpr unsigned USRLengthSize = 6U;
46  static constexpr unsigned USRBitLengthSize = 8U;
47  static constexpr unsigned char Signature[4] = {'D', 'O', 'C', 'S'};
48  static constexpr int USRHashSize = 20;
49 };
50 
51 // New Ids need to be added to both the enum here and the relevant IdNameMap in
52 // the implementation file.
53 enum BlockId {
54  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 enum RecordId {
72  VERSION = 1,
124 };
125 
126 static constexpr unsigned BlockIdCount = BI_LAST - BI_FIRST;
127 static constexpr unsigned RecordIdCount = RI_LAST - RI_FIRST;
128 
129 // Identifiers for differentiating between subblocks
130 enum class FieldId {
131  F_default,
132  F_namespace,
133  F_parent,
134  F_vparent,
135  F_type,
138 };
139 
141 public:
142  ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream) : Stream(Stream) {
143  emitHeader();
144  emitBlockInfoBlock();
145  emitVersionBlock();
146  }
147 
148  // Write a specific info to a bitcode stream.
149  bool dispatchInfoForWrite(Info *I);
150 
151  // Block emission of different info types.
152  void emitBlock(const NamespaceInfo &I);
153  void emitBlock(const RecordInfo &I);
154  void emitBlock(const BaseRecordInfo &I);
155  void emitBlock(const FunctionInfo &I);
156  void emitBlock(const EnumInfo &I);
157  void emitBlock(const TypeInfo &B);
158  void emitBlock(const FieldTypeInfo &B);
159  void emitBlock(const MemberTypeInfo &B);
160  void emitBlock(const CommentInfo &B);
161  void emitBlock(const Reference &B, FieldId F);
162 
163 private:
164  class AbbreviationMap {
165  llvm::DenseMap<unsigned, unsigned> Abbrevs;
166 
167  public:
168  AbbreviationMap() : Abbrevs(RecordIdCount) {}
169 
170  void add(RecordId RID, unsigned AbbrevID);
171  unsigned get(RecordId RID) const;
172  };
173 
174  class StreamSubBlockGuard {
175  llvm::BitstreamWriter &Stream;
176 
177  public:
178  StreamSubBlockGuard(llvm::BitstreamWriter &Stream_, BlockId ID)
179  : Stream(Stream_) {
180  // NOTE: SubBlockIDSize could theoretically be calculated on the fly,
181  // based on the initialization list of records in each block.
182  Stream.EnterSubblock(ID, BitCodeConstants::SubblockIDSize);
183  }
184 
185  StreamSubBlockGuard(const StreamSubBlockGuard &) = delete;
186  StreamSubBlockGuard &operator=(const StreamSubBlockGuard &) = delete;
187 
188  ~StreamSubBlockGuard() { Stream.ExitBlock(); }
189  };
190 
191  // Emission of validation and overview blocks.
192  void emitHeader();
193  void emitVersionBlock();
194  void emitRecordID(RecordId ID);
195  void emitBlockID(BlockId ID);
196  void emitBlockInfoBlock();
197  void emitBlockInfo(BlockId BID, const std::vector<RecordId> &RIDs);
198 
199  // Emission of individual record types.
200  void emitRecord(StringRef Str, RecordId ID);
201  void emitRecord(const SymbolID &Str, RecordId ID);
202  void emitRecord(const Location &Loc, RecordId ID);
203  void emitRecord(const Reference &Ref, RecordId ID);
204  void emitRecord(bool Value, RecordId ID);
205  void emitRecord(int Value, RecordId ID);
206  void emitRecord(unsigned Value, RecordId ID);
207  bool prepRecordData(RecordId ID, bool ShouldEmit = true);
208 
209  // Emission of appropriate abbreviation type.
210  void emitAbbrev(RecordId ID, BlockId Block);
211 
212  // Static size is the maximum length of the block/record names we're pushing
213  // to this + 1. Longest is currently `MemberTypeBlock` at 15 chars.
214  SmallVector<uint32_t, BitCodeConstants::RecordSize> Record;
215  llvm::BitstreamWriter &Stream;
216  AbbreviationMap Abbrevs;
217 };
218 
219 } // namespace doc
220 } // namespace clang
221 
222 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_BITCODEWRITER_H
clang::doc::ClangDocBitcodeWriter::dispatchInfoForWrite
bool dispatchInfoForWrite(Info *I)
Definition: BitcodeWriter.cpp:555
clang::doc::FieldId::F_type
@ F_type
clang::doc::BI_VERSION_BLOCK_ID
@ BI_VERSION_BLOCK_ID
Definition: BitcodeWriter.h:54
clang::doc::FieldId::F_child_record
@ F_child_record
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::doc::FieldId::F_default
@ F_default
clang::doc::BitCodeConstants::IntSize
static constexpr unsigned IntSize
Definition: BitcodeWriter.h:40
clang::doc::BitCodeConstants::LineNumberSize
static constexpr unsigned LineNumberSize
Definition: BitcodeWriter.h:43
clang::doc::ENUM_MEMBER
@ ENUM_MEMBER
Definition: BitcodeWriter.h:100
clang::doc::BI_FUNCTION_BLOCK_ID
@ BI_FUNCTION_BLOCK_ID
Definition: BitcodeWriter.h:62
clang::doc::BlockId
BlockId
Definition: BitcodeWriter.h:53
clang::doc::BitCodeConstants::StringLengthSize
static constexpr unsigned StringLengthSize
Definition: BitcodeWriter.h:41
clang::doc::FUNCTION_DEFLOCATION
@ FUNCTION_DEFLOCATION
Definition: BitcodeWriter.h:75
clang::doc::RECORD_IS_TYPE_DEF
@ RECORD_IS_TYPE_DEF
Definition: BitcodeWriter.h:108
Location
Definition: Modularize.cpp:382
clang::doc::COMMENT_CLOSENAME
@ COMMENT_CLOSENAME
Definition: BitcodeWriter.h:84
clang::doc::FunctionInfo
Definition: Representation.h:319
clang::doc::COMMENT_NAME
@ COMMENT_NAME
Definition: BitcodeWriter.h:81
clang::doc::ClangDocBitcodeWriter
Definition: BitcodeWriter.h:140
clang::doc::RECORD_LOCATION
@ RECORD_LOCATION
Definition: BitcodeWriter.h:106
clang::doc::COMMENT_PARAMNAME
@ COMMENT_PARAMNAME
Definition: BitcodeWriter.h:83
clang::doc::CommentInfo
Definition: Representation.h:46
clang::doc::FieldId::F_child_namespace
@ F_child_namespace
clang::doc::BI_BASE_RECORD_BLOCK_ID
@ BI_BASE_RECORD_BLOCK_ID
Definition: BitcodeWriter.h:61
clang::doc::ENUM_NAME
@ ENUM_NAME
Definition: BitcodeWriter.h:97
clang::doc::COMMENT_EXPLICIT
@ COMMENT_EXPLICIT
Definition: BitcodeWriter.h:86
clang::doc::BI_MEMBER_TYPE_BLOCK_ID
@ BI_MEMBER_TYPE_BLOCK_ID
Definition: BitcodeWriter.h:59
clang::doc::RecordId
RecordId
Definition: BitcodeWriter.h:71
clang::doc::EnumInfo
Definition: Representation.h:390
clang::doc::REFERENCE_IS_IN_GLOBAL_NAMESPACE
@ REFERENCE_IS_IN_GLOBAL_NAMESPACE
Definition: BitcodeWriter.h:120
clang::doc::BitCodeConstants::FilenameLengthSize
static constexpr unsigned FilenameLengthSize
Definition: BitcodeWriter.h:42
clang::doc::ENUM_USR
@ ENUM_USR
Definition: BitcodeWriter.h:96
clang::doc::BASE_RECORD_PATH
@ BASE_RECORD_PATH
Definition: BitcodeWriter.h:111
Representation.h
clang::doc::BI_FIRST
@ BI_FIRST
Definition: BitcodeWriter.h:66
clang::doc::RECORD_PATH
@ RECORD_PATH
Definition: BitcodeWriter.h:104
clang::doc::NAMESPACE_USR
@ NAMESPACE_USR
Definition: BitcodeWriter.h:93
clang::doc::BitCodeConstants::USRHashSize
static constexpr int USRHashSize
Definition: BitcodeWriter.h:48
clang::doc::RI_FIRST
@ RI_FIRST
Definition: BitcodeWriter.h:123
clang::doc::BaseRecordInfo
Definition: Representation.h:373
clang::doc::ENUM_SCOPED
@ ENUM_SCOPED
Definition: BitcodeWriter.h:101
clang::doc::REFERENCE_PATH
@ REFERENCE_PATH
Definition: BitcodeWriter.h:119
clang::doc::BitCodeConstants
Definition: BitcodeWriter.h:35
clang::doc::RecordIdCount
static constexpr unsigned RecordIdCount
Definition: BitcodeWriter.h:127
clang::doc::FieldId::F_namespace
@ F_namespace
clang::doc::BitCodeConstants::SignatureBitSize
static constexpr unsigned SignatureBitSize
Definition: BitcodeWriter.h:37
clang::doc::MEMBER_TYPE_ACCESS
@ MEMBER_TYPE_ACCESS
Definition: BitcodeWriter.h:92
clang::doc::BitCodeConstants::SubblockIDSize
static constexpr unsigned SubblockIDSize
Definition: BitcodeWriter.h:38
clang::doc::BASE_RECORD_IS_PARENT
@ BASE_RECORD_IS_PARENT
Definition: BitcodeWriter.h:115
clang::doc::FUNCTION_USR
@ FUNCTION_USR
Definition: BitcodeWriter.h:73
clang::doc::NAMESPACE_PATH
@ NAMESPACE_PATH
Definition: BitcodeWriter.h:95
clang::doc::FieldId::F_vparent
@ F_vparent
clang::doc::BASE_RECORD_USR
@ BASE_RECORD_USR
Definition: BitcodeWriter.h:109
clang::doc::COMMENT_DIRECTION
@ COMMENT_DIRECTION
Definition: BitcodeWriter.h:82
clang::doc::BitCodeConstants::BoolSize
static constexpr unsigned BoolSize
Definition: BitcodeWriter.h:39
clang::doc::BitCodeConstants::USRBitLengthSize
static constexpr unsigned USRBitLengthSize
Definition: BitcodeWriter.h:46
clang::doc::BlockIdCount
static constexpr unsigned BlockIdCount
Definition: BitcodeWriter.h:126
clang::doc::RECORD_USR
@ RECORD_USR
Definition: BitcodeWriter.h:102
clang::doc::REFERENCE_FIELD
@ REFERENCE_FIELD
Definition: BitcodeWriter.h:121
clang::doc::REFERENCE_NAME
@ REFERENCE_NAME
Definition: BitcodeWriter.h:117
clang::doc::BitCodeConstants::USRLengthSize
static constexpr unsigned USRLengthSize
Definition: BitcodeWriter.h:45
clang::doc::BitCodeConstants::ReferenceTypeSize
static constexpr unsigned ReferenceTypeSize
Definition: BitcodeWriter.h:44
clang::doc::RECORD_NAME
@ RECORD_NAME
Definition: BitcodeWriter.h:103
clang::doc::SymbolID
std::array< uint8_t, 20 > SymbolID
Definition: Representation.h:30
clang::doc::REFERENCE_USR
@ REFERENCE_USR
Definition: BitcodeWriter.h:116
clang::doc::RecordInfo
Definition: Representation.h:339
clang::doc::BitCodeConstants::Signature
static constexpr unsigned char Signature[4]
Definition: BitcodeWriter.h:47
clang::doc::ENUM_LOCATION
@ ENUM_LOCATION
Definition: BitcodeWriter.h:99
clang::doc::BASE_RECORD_IS_VIRTUAL
@ BASE_RECORD_IS_VIRTUAL
Definition: BitcodeWriter.h:113
clang::doc::BASE_RECORD_TAG_TYPE
@ BASE_RECORD_TAG_TYPE
Definition: BitcodeWriter.h:112
clang::doc::BI_COMMENT_BLOCK_ID
@ BI_COMMENT_BLOCK_ID
Definition: BitcodeWriter.h:63
clang::doc::BI_LAST
@ BI_LAST
Definition: BitcodeWriter.h:65
clang::doc::BASE_RECORD_ACCESS
@ BASE_RECORD_ACCESS
Definition: BitcodeWriter.h:114
clang::doc::Info
A base struct for Infos.
Definition: Representation.h:243
clang::doc::COMMENT_SELFCLOSING
@ COMMENT_SELFCLOSING
Definition: BitcodeWriter.h:85
clang::doc::Reference
Definition: Representation.h:115
clang::doc::BI_RECORD_BLOCK_ID
@ BI_RECORD_BLOCK_ID
Definition: BitcodeWriter.h:60
clang::doc::BI_REFERENCE_BLOCK_ID
@ BI_REFERENCE_BLOCK_ID
Definition: BitcodeWriter.h:64
clang::doc::REFERENCE_TYPE
@ REFERENCE_TYPE
Definition: BitcodeWriter.h:118
clang::doc::FIELD_TYPE_NAME
@ FIELD_TYPE_NAME
Definition: BitcodeWriter.h:90
clang::doc::ENUM_DEFLOCATION
@ ENUM_DEFLOCATION
Definition: BitcodeWriter.h:98
clang::doc::VERSION
@ VERSION
Definition: BitcodeWriter.h:72
clang::doc::RI_LAST
@ RI_LAST
Definition: BitcodeWriter.h:122
ID
static char ID
Definition: Logger.cpp:74
clang::doc::VersionNumber
static const unsigned VersionNumber
Definition: BitcodeWriter.h:33
clang::doc::FUNCTION_LOCATION
@ FUNCTION_LOCATION
Definition: BitcodeWriter.h:76
clang::doc::BI_FIELD_TYPE_BLOCK_ID
@ BI_FIELD_TYPE_BLOCK_ID
Definition: BitcodeWriter.h:58
clang::doc::COMMENT_ATTRVAL
@ COMMENT_ATTRVAL
Definition: BitcodeWriter.h:88
clang::doc::MEMBER_TYPE_NAME
@ MEMBER_TYPE_NAME
Definition: BitcodeWriter.h:91
clang::doc::BASE_RECORD_NAME
@ BASE_RECORD_NAME
Definition: BitcodeWriter.h:110
clang::doc::COMMENT_TEXT
@ COMMENT_TEXT
Definition: BitcodeWriter.h:80
clang::doc::BitCodeConstants::RecordSize
static constexpr unsigned RecordSize
Definition: BitcodeWriter.h:36
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::doc::NamespaceInfo
Definition: Representation.h:283
clang::doc::FieldId::F_parent
@ F_parent
clang::doc::RECORD_TAG_TYPE
@ RECORD_TAG_TYPE
Definition: BitcodeWriter.h:107
clang::doc::FUNCTION_NAME
@ FUNCTION_NAME
Definition: BitcodeWriter.h:74
clang::doc::COMMENT_ATTRKEY
@ COMMENT_ATTRKEY
Definition: BitcodeWriter.h:87
clang::doc::TypeInfo
Definition: Representation.h:158
clang::doc::ClangDocBitcodeWriter::emitBlock
void emitBlock(const NamespaceInfo &I)
Definition: BitcodeWriter.cpp:453
clang::doc::BI_NAMESPACE_BLOCK_ID
@ BI_NAMESPACE_BLOCK_ID
Definition: BitcodeWriter.h:55
clang::doc::RECORD_DEFLOCATION
@ RECORD_DEFLOCATION
Definition: BitcodeWriter.h:105
ns1::ns2::B
@ B
Definition: CategoricalFeature.h:3
clang::doc::COMMENT_KIND
@ COMMENT_KIND
Definition: BitcodeWriter.h:79
clang::doc::COMMENT_ARG
@ COMMENT_ARG
Definition: BitcodeWriter.h:89
clang::doc::ClangDocBitcodeWriter::ClangDocBitcodeWriter
ClangDocBitcodeWriter(llvm::BitstreamWriter &Stream)
Definition: BitcodeWriter.h:142
clang::doc::FieldTypeInfo
Definition: Representation.h:173
clang::doc::FUNCTION_IS_METHOD
@ FUNCTION_IS_METHOD
Definition: BitcodeWriter.h:78
clang::doc::MemberTypeInfo
Definition: Representation.h:191
clang::doc::BI_TYPE_BLOCK_ID
@ BI_TYPE_BLOCK_ID
Definition: BitcodeWriter.h:57
clang::doc::BI_ENUM_BLOCK_ID
@ BI_ENUM_BLOCK_ID
Definition: BitcodeWriter.h:56
clang::doc::FieldId
FieldId
Definition: BitcodeWriter.h:130
clang::doc::FUNCTION_ACCESS
@ FUNCTION_ACCESS
Definition: BitcodeWriter.h:77
clang::doc::NAMESPACE_NAME
@ NAMESPACE_NAME
Definition: BitcodeWriter.h:94