clang-tools 8.0.0

BitcodeWriter.cpp
Go to the documentation of this file.
1 //===-- BitcodeWriter.cpp - 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 #include "BitcodeWriter.h"
11 #include "llvm/ADT/IndexedMap.h"
12 #include <initializer_list>
13
14 namespace clang {
15 namespace doc {
16
17 // Empty SymbolID for comparison, so we don't have to construct one every time.
18 static const SymbolID EmptySID = SymbolID();
19
20 // Since id enums are not zero-indexed, we need to transform the given id into
21 // its associated index.
22 struct BlockIdToIndexFunctor {
23 using argument_type = unsigned;
24 unsigned operator()(unsigned ID) const { return ID - BI_FIRST; }
25 };
26
27 struct RecordIdToIndexFunctor {
28 using argument_type = unsigned;
29 unsigned operator()(unsigned ID) const { return ID - RI_FIRST; }
30 };
31
32 using AbbrevDsc = void (*)(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev);
33
34 static void AbbrevGen(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev,
35 const std::initializer_list<llvm::BitCodeAbbrevOp> Ops) {
36 for (const auto &Op : Ops)
37 Abbrev->Add(Op);
38 }
39
40 static void BoolAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
41 AbbrevGen(Abbrev,
42 {// 0. Boolean
43 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
44 BitCodeConstants::BoolSize)});
45 }
46
47 static void IntAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
48 AbbrevGen(Abbrev,
49 {// 0. Fixed-size integer
50 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
51 BitCodeConstants::IntSize)});
52 }
53
54 static void SymbolIDAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
55 AbbrevGen(Abbrev,
56 {// 0. Fixed-size integer (length of the sha1'd USR)
57 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
58 BitCodeConstants::USRLengthSize),
59 // 1. Fixed-size array of Char6 (USR)
60 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Array),
61 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
62 BitCodeConstants::USRBitLengthSize)});
63 }
64
65 static void StringAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
66 AbbrevGen(Abbrev,
67 {// 0. Fixed-size integer (length of the following string)
68 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
69 BitCodeConstants::StringLengthSize),
70 // 1. The string blob
71 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)});
72 }
73
74 // Assumes that the file will not have more than 65535 lines.
75 static void LocationAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
76 AbbrevGen(
77 Abbrev,
78 {// 0. Fixed-size integer (line number)
79 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
80 BitCodeConstants::LineNumberSize),
81 // 1. Fixed-size integer (length of the following string (filename))
82 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
83 BitCodeConstants::StringLengthSize),
84 // 2. The string blob
85 llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)});
86 }
87
88 struct RecordIdDsc {
89 llvm::StringRef Name;
90 AbbrevDsc Abbrev = nullptr;
91
92 RecordIdDsc() = default;
93 RecordIdDsc(llvm::StringRef Name, AbbrevDsc Abbrev)
94 : Name(Name), Abbrev(Abbrev) {}
95
96 // Is this 'description' valid?
97 operator bool() const {
98 return Abbrev != nullptr && Name.data() != nullptr && !Name.empty();
99 }
100 };
101
102 static const llvm::IndexedMap<llvm::StringRef, BlockIdToIndexFunctor>
103 BlockIdNameMap = []() {
104 llvm::IndexedMap<llvm::StringRef, BlockIdToIndexFunctor> BlockIdNameMap;
105 BlockIdNameMap.resize(BlockIdCount);
106
107 // There is no init-list constructor for the IndexedMap, so have to
108 // improvise
109 static const std::vector<std::pair<BlockId, const char *const>> Inits = {
110 {BI_VERSION_BLOCK_ID, "VersionBlock"},
111 {BI_NAMESPACE_BLOCK_ID, "NamespaceBlock"},
112 {BI_ENUM_BLOCK_ID, "EnumBlock"},
113 {BI_TYPE_BLOCK_ID, "TypeBlock"},
114 {BI_FIELD_TYPE_BLOCK_ID, "FieldTypeBlock"},
115 {BI_MEMBER_TYPE_BLOCK_ID, "MemberTypeBlock"},
116 {BI_RECORD_BLOCK_ID, "RecordBlock"},
117 {BI_FUNCTION_BLOCK_ID, "FunctionBlock"},
118 {BI_COMMENT_BLOCK_ID, "CommentBlock"},
119 {BI_REFERENCE_BLOCK_ID, "ReferenceBlock"}};
120 assert(Inits.size() == BlockIdCount);
121 for (const auto &Init : Inits)
122 BlockIdNameMap[Init.first] = Init.second;
123 assert(BlockIdNameMap.size() == BlockIdCount);
124 return BlockIdNameMap;
125 }();
126
127 static const llvm::IndexedMap<RecordIdDsc, RecordIdToIndexFunctor>
128 RecordIdNameMap = []() {
129 llvm::IndexedMap<RecordIdDsc, RecordIdToIndexFunctor> RecordIdNameMap;
130 RecordIdNameMap.resize(RecordIdCount);
131
132 // There is no init-list constructor for the IndexedMap, so have to
133 // improvise
134 static const std::vector<std::pair<RecordId, RecordIdDsc>> Inits = {
135 {VERSION, {"Version", &IntAbbrev}},
136 {COMMENT_KIND, {"Kind", &StringAbbrev}},
137 {COMMENT_TEXT, {"Text", &StringAbbrev}},
138 {COMMENT_NAME, {"Name", &StringAbbrev}},
139 {COMMENT_DIRECTION, {"Direction", &StringAbbrev}},
140 {COMMENT_PARAMNAME, {"ParamName", &StringAbbrev}},
141 {COMMENT_CLOSENAME, {"CloseName", &StringAbbrev}},
142 {COMMENT_SELFCLOSING, {"SelfClosing", &BoolAbbrev}},
143 {COMMENT_EXPLICIT, {"Explicit", &BoolAbbrev}},
144 {COMMENT_ATTRKEY, {"AttrKey", &StringAbbrev}},
145 {COMMENT_ATTRVAL, {"AttrVal", &StringAbbrev}},
146 {COMMENT_ARG, {"Arg", &StringAbbrev}},
147 {FIELD_TYPE_NAME, {"Name", &StringAbbrev}},
148 {MEMBER_TYPE_NAME, {"Name", &StringAbbrev}},
149 {MEMBER_TYPE_ACCESS, {"Access", &IntAbbrev}},
150 {NAMESPACE_USR, {"USR", &SymbolIDAbbrev}},
151 {NAMESPACE_NAME, {"Name", &StringAbbrev}},
152 {ENUM_USR, {"USR", &SymbolIDAbbrev}},
153 {ENUM_NAME, {"Name", &StringAbbrev}},
154 {ENUM_DEFLOCATION, {"DefLocation", &LocationAbbrev}},
155 {ENUM_LOCATION, {"Location", &LocationAbbrev}},
156 {ENUM_MEMBER, {"Member", &StringAbbrev}},
157 {ENUM_SCOPED, {"Scoped", &BoolAbbrev}},
158 {RECORD_USR, {"USR", &SymbolIDAbbrev}},
159 {RECORD_NAME, {"Name", &StringAbbrev}},
160 {RECORD_DEFLOCATION, {"DefLocation", &LocationAbbrev}},
161 {RECORD_LOCATION, {"Location", &LocationAbbrev}},
162 {RECORD_TAG_TYPE, {"TagType", &IntAbbrev}},
163 {FUNCTION_USR, {"USR", &SymbolIDAbbrev}},
164 {FUNCTION_NAME, {"Name", &StringAbbrev}},
165 {FUNCTION_DEFLOCATION, {"DefLocation", &LocationAbbrev}},
166 {FUNCTION_LOCATION, {"Location", &LocationAbbrev}},
167 {FUNCTION_ACCESS, {"Access", &IntAbbrev}},
168 {FUNCTION_IS_METHOD, {"IsMethod", &BoolAbbrev}},
169 {REFERENCE_USR, {"USR", &SymbolIDAbbrev}},
170 {REFERENCE_NAME, {"Name", &StringAbbrev}},
171 {REFERENCE_TYPE, {"RefType", &IntAbbrev}},
172 {REFERENCE_FIELD, {"Field", &IntAbbrev}}};
173 assert(Inits.size() == RecordIdCount);
174 for (const auto &Init : Inits) {
175 RecordIdNameMap[Init.first] = Init.second;
176 assert((Init.second.Name.size() + 1) <= BitCodeConstants::RecordSize);
177 }
178 assert(RecordIdNameMap.size() == RecordIdCount);
179 return RecordIdNameMap;
180 }();
181
182 static const std::vector<std::pair<BlockId, std::vector<RecordId>>>
183 RecordsByBlock{
184 // Version Block
185 {BI_VERSION_BLOCK_ID, {VERSION}},
186 // Comment Block
187 {BI_COMMENT_BLOCK_ID,
188 {COMMENT_KIND, COMMENT_TEXT, COMMENT_NAME, COMMENT_DIRECTION,
189 COMMENT_PARAMNAME, COMMENT_CLOSENAME, COMMENT_SELFCLOSING,
190 COMMENT_EXPLICIT, COMMENT_ATTRKEY, COMMENT_ATTRVAL, COMMENT_ARG}},
191 // Type Block
192 {BI_TYPE_BLOCK_ID, {}},
193 // FieldType Block
194 {BI_FIELD_TYPE_BLOCK_ID, {FIELD_TYPE_NAME}},
195 // MemberType Block
196 {BI_MEMBER_TYPE_BLOCK_ID, {MEMBER_TYPE_NAME, MEMBER_TYPE_ACCESS}},
197 // Enum Block
198 {BI_ENUM_BLOCK_ID,
199 {ENUM_USR, ENUM_NAME, ENUM_DEFLOCATION, ENUM_LOCATION, ENUM_MEMBER,
200 ENUM_SCOPED}},
201 // Namespace Block
202 {BI_NAMESPACE_BLOCK_ID, {NAMESPACE_USR, NAMESPACE_NAME}},
203 // Record Block
204 {BI_RECORD_BLOCK_ID,
205 {RECORD_USR, RECORD_NAME, RECORD_DEFLOCATION, RECORD_LOCATION,
206 RECORD_TAG_TYPE}},
207 // Function Block
208 {BI_FUNCTION_BLOCK_ID,
209 {FUNCTION_USR, FUNCTION_NAME, FUNCTION_DEFLOCATION, FUNCTION_LOCATION,
210 FUNCTION_ACCESS, FUNCTION_IS_METHOD}},
211 // Reference Block
212 {BI_REFERENCE_BLOCK_ID,
213 {REFERENCE_USR, REFERENCE_NAME, REFERENCE_TYPE, REFERENCE_FIELD}}};
214
215 // AbbreviationMap
216
217 constexpr char BitCodeConstants::Signature[];
218
219 void ClangDocBitcodeWriter::AbbreviationMap::add(RecordId RID,
220 unsigned AbbrevID) {
221 assert(RecordIdNameMap[RID] && "Unknown RecordId.");
222 assert(Abbrevs.find(RID) == Abbrevs.end() && "Abbreviation already added.");
223 Abbrevs[RID] = AbbrevID;
224 }
225
226 unsigned ClangDocBitcodeWriter::AbbreviationMap::get(RecordId RID) const {
227 assert(RecordIdNameMap[RID] && "Unknown RecordId.");
228 assert(Abbrevs.find(RID) != Abbrevs.end() && "Unknown abbreviation.");
229 return Abbrevs.lookup(RID);
230 }
231
232 // Validation and Overview Blocks
233
234 /// \brief Emits the magic number header to check that its the right format,
235 /// in this case, 'DOCS'.
236 void ClangDocBitcodeWriter::emitHeader() {
237 for (char C : BitCodeConstants::Signature)
238 Stream.Emit((unsigned)C, BitCodeConstants::SignatureBitSize);
239 }
240
241 void ClangDocBitcodeWriter::emitVersionBlock() {
242 StreamSubBlockGuard Block(Stream, BI_VERSION_BLOCK_ID);
243 emitRecord(VersionNumber, VERSION);
244 }
245
246 /// \brief Emits a block ID and the block name to the BLOCKINFO block.
247 void ClangDocBitcodeWriter::emitBlockID(BlockId BID) {
248 const auto &BlockIdName = BlockIdNameMap[BID];
249 assert(BlockIdName.data() && BlockIdName.size() && "Unknown BlockId.");
250
251 Record.clear();
252 Record.push_back(BID);
253 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
254 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME,
255 ArrayRef<unsigned char>(BlockIdName.bytes_begin(),
256 BlockIdName.bytes_end()));
257 }
258
259 /// \brief Emits a record name to the BLOCKINFO block.
260 void ClangDocBitcodeWriter::emitRecordID(RecordId ID) {
261 assert(RecordIdNameMap[ID] && "Unknown RecordId.");
262 prepRecordData(ID);
263 Record.append(RecordIdNameMap[ID].Name.begin(),
264 RecordIdNameMap[ID].Name.end());
265 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
266 }
267
268 // Abbreviations
269
270 void ClangDocBitcodeWriter::emitAbbrev(RecordId ID, BlockId Block) {
271 assert(RecordIdNameMap[ID] && "Unknown abbreviation.");
272 auto Abbrev = std::make_shared<llvm::BitCodeAbbrev>();
273 Abbrev->Add(llvm::BitCodeAbbrevOp(ID));
274 RecordIdNameMap[ID].Abbrev(Abbrev);
275 Abbrevs.add(ID, Stream.EmitBlockInfoAbbrev(Block, std::move(Abbrev)));
276 }
277
278 // Records
279
280 void ClangDocBitcodeWriter::emitRecord(const SymbolID &Sym, RecordId ID) {
281 assert(RecordIdNameMap[ID] && "Unknown RecordId.");
282 assert(RecordIdNameMap[ID].Abbrev == &SymbolIDAbbrev &&
283 "Abbrev type mismatch.");
284 if (!prepRecordData(ID, Sym != EmptySID))
285 return;
286 assert(Sym.size() == 20);
287 Record.push_back(Sym.size());
288 Record.append(Sym.begin(), Sym.end());
289 Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
290 }
291
292 void ClangDocBitcodeWriter::emitRecord(llvm::StringRef Str, RecordId ID) {
293 assert(RecordIdNameMap[ID] && "Unknown RecordId.");
294 assert(RecordIdNameMap[ID].Abbrev == &StringAbbrev &&
295 "Abbrev type mismatch.");
296 if (!prepRecordData(ID, !Str.empty()))
297 return;
298 assert(Str.size() < (1U << BitCodeConstants::StringLengthSize));
299 Record.push_back(Str.size());
300 Stream.EmitRecordWithBlob(Abbrevs.get(ID), Record, Str);
301 }
302
303 void ClangDocBitcodeWriter::emitRecord(const Location &Loc, RecordId ID) {
304 assert(RecordIdNameMap[ID] && "Unknown RecordId.");
305 assert(RecordIdNameMap[ID].Abbrev == &LocationAbbrev &&
306 "Abbrev type mismatch.");
307 if (!prepRecordData(ID, true))
308 return;
309 // FIXME: Assert that the line number is of the appropriate size.
310 Record.push_back(Loc.LineNumber);
311 assert(Loc.Filename.size() < (1U << BitCodeConstants::StringLengthSize));
312 Record.push_back(Loc.Filename.size());
313 Stream.EmitRecordWithBlob(Abbrevs.get(ID), Record, Loc.Filename);
314 }
315
316 void ClangDocBitcodeWriter::emitRecord(bool Val, RecordId ID) {
317 assert(RecordIdNameMap[ID] && "Unknown RecordId.");
318 assert(RecordIdNameMap[ID].Abbrev == &BoolAbbrev && "Abbrev type mismatch.");
319 if (!prepRecordData(ID, Val))
320 return;
321 Record.push_back(Val);
322 Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
323 }
324
325 void ClangDocBitcodeWriter::emitRecord(int Val, RecordId ID) {
326 assert(RecordIdNameMap[ID] && "Unknown RecordId.");
327 assert(RecordIdNameMap[ID].Abbrev == &IntAbbrev && "Abbrev type mismatch.");
328 if (!prepRecordData(ID, Val))
329 return;
330 // FIXME: Assert that the integer is of the appropriate size.
331 Record.push_back(Val);
332 Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
333 }
334
335 void ClangDocBitcodeWriter::emitRecord(unsigned Val, RecordId ID) {
336 assert(RecordIdNameMap[ID] && "Unknown RecordId.");
337 assert(RecordIdNameMap[ID].Abbrev == &IntAbbrev && "Abbrev type mismatch.");
338 if (!prepRecordData(ID, Val))
339 return;
340 assert(Val < (1U << BitCodeConstants::IntSize));
341 Record.push_back(Val);
342 Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
343 }
344
345 bool ClangDocBitcodeWriter::prepRecordData(RecordId ID, bool ShouldEmit) {
346 assert(RecordIdNameMap[ID] && "Unknown RecordId.");
347 if (!ShouldEmit)
348 return false;
349 Record.clear();
350 Record.push_back(ID);
351 return true;
352 }
353
354 // BlockInfo Block
355
356 void ClangDocBitcodeWriter::emitBlockInfoBlock() {
357 Stream.EnterBlockInfoBlock();
358 for (const auto &Block : RecordsByBlock) {
359 assert(Block.second.size() < (1U << BitCodeConstants::SubblockIDSize));
360 emitBlockInfo(Block.first, Block.second);
361 }
362 Stream.ExitBlock();
363 }
364
365 void ClangDocBitcodeWriter::emitBlockInfo(BlockId BID,
366 const std::vector<RecordId> &RIDs) {
367 assert(RIDs.size() < (1U << BitCodeConstants::SubblockIDSize));
368 emitBlockID(BID);
369 for (RecordId RID : RIDs) {
370 emitRecordID(RID);
371 emitAbbrev(RID, BID);
372 }
373 }
374
375 // Block emission
376
377 void ClangDocBitcodeWriter::emitBlock(const Reference &R, FieldId Field) {
378 if (R.USR == EmptySID && R.Name.empty())
379 return;
380 StreamSubBlockGuard Block(Stream, BI_REFERENCE_BLOCK_ID);
381 emitRecord(R.USR, REFERENCE_USR);
382 emitRecord(R.Name, REFERENCE_NAME);
383 emitRecord((unsigned)R.RefType, REFERENCE_TYPE);
384 emitRecord((unsigned)Field, REFERENCE_FIELD);
385 }
386
387 void ClangDocBitcodeWriter::emitBlock(const TypeInfo &T) {
388 StreamSubBlockGuard Block(Stream, BI_TYPE_BLOCK_ID);
389 emitBlock(T.Type, FieldId::F_type);
390 }
391
392 void ClangDocBitcodeWriter::emitBlock(const FieldTypeInfo &T) {
393 StreamSubBlockGuard Block(Stream, BI_FIELD_TYPE_BLOCK_ID);
394 emitBlock(T.Type, FieldId::F_type);
395 emitRecord(T.Name, FIELD_TYPE_NAME);
396 }
397
398 void ClangDocBitcodeWriter::emitBlock(const MemberTypeInfo &T) {
399 StreamSubBlockGuard Block(Stream, BI_MEMBER_TYPE_BLOCK_ID);
400 emitBlock(T.Type, FieldId::F_type);
401 emitRecord(T.Name, MEMBER_TYPE_NAME);
402 emitRecord(T.Access, MEMBER_TYPE_ACCESS);
403 }
404
405 void ClangDocBitcodeWriter::emitBlock(const CommentInfo &I) {
406 StreamSubBlockGuard Block(Stream, BI_COMMENT_BLOCK_ID);
407 for (const auto &L : std::vector<std::pair<llvm::StringRef, RecordId>>{
408 {I.Kind, COMMENT_KIND},
409 {I.Text, COMMENT_TEXT},
410 {I.Name, COMMENT_NAME},
411 {I.Direction, COMMENT_DIRECTION},
412 {I.ParamName, COMMENT_PARAMNAME},
413 {I.CloseName, COMMENT_CLOSENAME}})
414 emitRecord(L.first, L.second);
415 emitRecord(I.SelfClosing, COMMENT_SELFCLOSING);
416 emitRecord(I.Explicit, COMMENT_EXPLICIT);
417 for (const auto &A : I.AttrKeys)
418 emitRecord(A, COMMENT_ATTRKEY);
419 for (const auto &A : I.AttrValues)
420 emitRecord(A, COMMENT_ATTRVAL);
421 for (const auto &A : I.Args)
422 emitRecord(A, COMMENT_ARG);
423 for (const auto &C : I.Children)
424 emitBlock(*C);
425 }
426
427 void ClangDocBitcodeWriter::emitBlock(const NamespaceInfo &I) {
428 StreamSubBlockGuard Block(Stream, BI_NAMESPACE_BLOCK_ID);
429 emitRecord(I.USR, NAMESPACE_USR);
430 emitRecord(I.Name, NAMESPACE_NAME);
431 for (const auto &N : I.Namespace)
432 emitBlock(N, FieldId::F_namespace);
433 for (const auto &CI : I.Description)
434 emitBlock(CI);
435 for (const auto &C : I.ChildNamespaces)
436 emitBlock(C, FieldId::F_child_namespace);
437 for (const auto &C : I.ChildRecords)
438 emitBlock(C, FieldId::F_child_record);
439 for (const auto &C : I.ChildFunctions)
440 emitBlock(C);
441 for (const auto &C : I.ChildEnums)
442 emitBlock(C);
443 }
444
445 void ClangDocBitcodeWriter::emitBlock(const EnumInfo &I) {
446 StreamSubBlockGuard Block(Stream, BI_ENUM_BLOCK_ID);
447 emitRecord(I.USR, ENUM_USR);
448 emitRecord(I.Name, ENUM_NAME);
449 for (const auto &N : I.Namespace)
450 emitBlock(N, FieldId::F_namespace);
451 for (const auto &CI : I.Description)
452 emitBlock(CI);
453 if (I.DefLoc)
454 emitRecord(I.DefLoc.getValue(), ENUM_DEFLOCATION);
455 for (const auto &L : I.Loc)
456 emitRecord(L, ENUM_LOCATION);
457 emitRecord(I.Scoped, ENUM_SCOPED);
458 for (const auto &N : I.Members)
459 emitRecord(N, ENUM_MEMBER);
460 }
461
462 void ClangDocBitcodeWriter::emitBlock(const RecordInfo &I) {
463 StreamSubBlockGuard Block(Stream, BI_RECORD_BLOCK_ID);
464 emitRecord(I.USR, RECORD_USR);
465 emitRecord(I.Name, RECORD_NAME);
466 for (const auto &N : I.Namespace)
467 emitBlock(N, FieldId::F_namespace);
468 for (const auto &CI : I.Description)
469 emitBlock(CI);
470 if (I.DefLoc)
471 emitRecord(I.DefLoc.getValue(), RECORD_DEFLOCATION);
472 for (const auto &L : I.Loc)
473 emitRecord(L, RECORD_LOCATION);
474 emitRecord(I.TagType, RECORD_TAG_TYPE);
475 for (const auto &N : I.Members)
476 emitBlock(N);
477 for (const auto &P : I.Parents)
478 emitBlock(P, FieldId::F_parent);
479 for (const auto &P : I.VirtualParents)
480 emitBlock(P, FieldId::F_vparent);
481 for (const auto &C : I.ChildRecords)
482 emitBlock(C, FieldId::F_child_record);
483 for (const auto &C : I.ChildFunctions)
484 emitBlock(C);
485 for (const auto &C : I.ChildEnums)
486 emitBlock(C);
487 }
488
489 void ClangDocBitcodeWriter::emitBlock(const FunctionInfo &I) {
490 StreamSubBlockGuard Block(Stream, BI_FUNCTION_BLOCK_ID);
491 emitRecord(I.USR, FUNCTION_USR);
492 emitRecord(I.Name, FUNCTION_NAME);
493 for (const auto &N : I.Namespace)
494 emitBlock(N, FieldId::F_namespace);
495 for (const auto &CI : I.Description)
496 emitBlock(CI);
497 emitRecord(I.IsMethod, FUNCTION_IS_METHOD);
498 if (I.DefLoc)
499 emitRecord(I.DefLoc.getValue(), FUNCTION_DEFLOCATION);
500 for (const auto &L : I.Loc)
501 emitRecord(L, FUNCTION_LOCATION);
502 emitBlock(I.Parent, FieldId::F_parent);
503 emitBlock(I.ReturnType);
504 for (const auto &N : I.Params)
505 emitBlock(N);
506 }
507
508 bool ClangDocBitcodeWriter::dispatchInfoForWrite(Info *I) {
509 switch (I->IT) {
510 case InfoType::IT_namespace:
511 emitBlock(*static_cast<clang::doc::NamespaceInfo *>(I));
512 break;
513 case InfoType::IT_record:
514 emitBlock(*static_cast<clang::doc::RecordInfo *>(I));
515 break;
516 case InfoType::IT_enum:
517 emitBlock(*static_cast<clang::doc::EnumInfo *>(I));
518 break;
519 case InfoType::IT_function:
520 emitBlock(*static_cast<clang::doc::FunctionInfo *>(I));
521 break;
522 default:
523 llvm::errs() << "Unexpected info, unable to write.\n";
524 return true;
525 }
526 return false;
527 }
528
529 } // namespace doc
530 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::doc::VERSION
Definition: BitcodeWriter.h:72
clang::doc::StringAbbrev
static void StringAbbrev(std::shared_ptr< llvm::BitCodeAbbrev > &Abbrev)
Definition: BitcodeWriter.cpp:65
clang::doc::Info::Namespace
llvm::SmallVector< Reference, 4 > Namespace
Definition: Representation.h:173
clang::doc::EmptySID
static const SymbolID EmptySID
Definition: BitcodeWriter.cpp:18
clang::doc::COMMENT_NAME
Definition: BitcodeWriter.h:81
clang::doc::COMMENT_ATTRVAL
Definition: BitcodeWriter.h:88
clang::doc::AbbrevGen
static void AbbrevGen(std::shared_ptr< llvm::BitCodeAbbrev > &Abbrev, const std::initializer_list< llvm::BitCodeAbbrevOp > Ops)
Definition: BitcodeWriter.cpp:34
clang::doc::REFERENCE_USR
Definition: BitcodeWriter.h:106
clang::doc::COMMENT_CLOSENAME
Definition: BitcodeWriter.h:84
clang::doc::FieldId::F_child_namespace
clang::doc::BI_MEMBER_TYPE_BLOCK_ID
Definition: BitcodeWriter.h:60
clang::doc::FUNCTION_IS_METHOD
Definition: BitcodeWriter.h:78
clang::doc::BitCodeConstants::SignatureBitSize
static constexpr unsigned SignatureBitSize
Definition: BitcodeWriter.h:38
clang::doc::RecordsByBlock
static const std::vector< std::pair< BlockId, std::vector< RecordId > > > RecordsByBlock
Definition: BitcodeWriter.cpp:183
clang::doc::ENUM_LOCATION
Definition: BitcodeWriter.h:98
clang::doc::VersionNumber
static const unsigned VersionNumber
Definition: BitcodeWriter.h:34
clang::doc::InfoType::IT_function
clang::doc::BitCodeConstants::SubblockIDSize
static constexpr unsigned SubblockIDSize
Definition: BitcodeWriter.h:39
clang::doc::FUNCTION_USR
Definition: BitcodeWriter.h:73
clang::doc::Reference
Definition: Representation.h:75
clang::doc::NAMESPACE_NAME
Definition: BitcodeWriter.h:94
clang::doc::BlockIdCount
static constexpr unsigned BlockIdCount
Definition: BitcodeWriter.h:114
clang::doc::ClangDocBitcodeWriter::dispatchInfoForWrite
bool dispatchInfoForWrite(Info *I)
Definition: BitcodeWriter.cpp:508
clang::doc::Record
llvm::SmallVector< uint64_t, 1024 > Record
Definition: BitcodeReader.cpp:19
clang::doc::TypeInfo::Type
Reference Type
Definition: Representation.h:102
clang::doc::ClangDocBitcodeWriter::emitBlock
void emitBlock(const NamespaceInfo &I)
Definition: BitcodeWriter.cpp:427
clang::doc::Reference::Name
SmallString< 16 > Name
Definition: Representation.h:87
clang::doc::SymbolInfo::DefLoc
llvm::Optional< Location > DefLoc
Definition: Representation.h:211
clang::doc::RecordIdDsc::RecordIdDsc
RecordIdDsc(llvm::StringRef Name, AbbrevDsc Abbrev)
Definition: BitcodeWriter.cpp:93
clang::doc::FIELD_TYPE_NAME
Definition: BitcodeWriter.h:90
clang::doc::RecordIdToIndexFunctor
Definition: BitcodeWriter.cpp:27
clang::doc::ENUM_DEFLOCATION
Definition: BitcodeWriter.h:97
clang::doc::RecordInfo::ChildFunctions
std::vector< FunctionInfo > ChildFunctions
Definition: Representation.h:258
clang::doc::FunctionInfo::Parent
Reference Parent
Definition: Representation.h:224
clang::doc::BitCodeConstants::BoolSize
static constexpr unsigned BoolSize
Definition: BitcodeWriter.h:40
clang::doc::InfoType::IT_enum
clang::doc::InfoType::IT_record
clang::doc::InfoType::IT_namespace
clang::doc::SymbolInfo::Loc
llvm::SmallVector< Location, 2 > Loc
Definition: Representation.h:212
clang::doc::REFERENCE_FIELD
Definition: BitcodeWriter.h:109
clang::doc::REFERENCE_NAME
Definition: BitcodeWriter.h:107
clang::doc::MEMBER_TYPE_NAME
Definition: BitcodeWriter.h:91
clang::doc::ENUM_NAME
Definition: BitcodeWriter.h:96
clang::doc::BoolAbbrev
static void BoolAbbrev(std::shared_ptr< llvm::BitCodeAbbrev > &Abbrev)
Definition: BitcodeWriter.cpp:40
clang::doc::RecordIdNameMap
static const llvm::IndexedMap< RecordIdDsc, RecordIdToIndexFunctor > RecordIdNameMap
Definition: BitcodeWriter.cpp:128
clang::doc::COMMENT_TEXT
Definition: BitcodeWriter.h:80
clang::doc::NamespaceInfo::ChildFunctions
std::vector< FunctionInfo > ChildFunctions
Definition: Representation.h:199
clang::doc::RecordIdToIndexFunctor::operator()
unsigned operator()(unsigned ID) const
Definition: BitcodeWriter.cpp:29
clang::doc::RECORD_TAG_TYPE
Definition: BitcodeWriter.h:105
clang::doc::FUNCTION_ACCESS
Definition: BitcodeWriter.h:77
clang::doc::NamespaceInfo::ChildEnums
std::vector< EnumInfo > ChildEnums
Definition: Representation.h:200
clang::doc::EnumInfo::Scoped
bool Scoped
Definition: Representation.h:270
clang::doc::BlockIdToIndexFunctor::operator()
unsigned operator()(unsigned ID) const
Definition: BitcodeWriter.cpp:24
clang::doc::RecordIdToIndexFunctor::argument_type
unsigned argument_type
Definition: BitcodeWriter.cpp:28
clang::doc::BitCodeConstants::LineNumberSize
static constexpr unsigned LineNumberSize
Definition: BitcodeWriter.h:44
clang::doc::RecordInfo::ChildRecords
std::vector< Reference > ChildRecords
Definition: Representation.h:257
clang::doc::SymbolIDAbbrev
static void SymbolIDAbbrev(std::shared_ptr< llvm::BitCodeAbbrev > &Abbrev)
Definition: BitcodeWriter.cpp:54
clang::doc::BI_RECORD_BLOCK_ID
Definition: BitcodeWriter.h:61
clang::doc::FieldId
FieldId
Definition: BitcodeWriter.h:118
clang::doc::RECORD_DEFLOCATION
Definition: BitcodeWriter.h:103
clang::doc::RecordIdDsc::Name
llvm::StringRef Name
Definition: BitcodeWriter.cpp:89
clang::doc::RecordInfo::VirtualParents
llvm::SmallVector< Reference, 4 > VirtualParents
Definition: Representation.h:251
clang::doc::FunctionInfo
Definition: Representation.h:217
clang::doc::CommentInfo::SelfClosing
bool SelfClosing
Definition: Representation.h:62
clang::doc::FunctionInfo::Params
llvm::SmallVector< FieldTypeInfo, 4 > Params
Definition: Representation.h:226
clang::doc::BlockIdToIndexFunctor::argument_type
unsigned argument_type
Definition: BitcodeWriter.cpp:23
clang::doc::BitCodeConstants::Signature
static constexpr char Signature[4]
Definition: BitcodeWriter.h:48
clang::doc::Reference::USR
SymbolID USR
Definition: Representation.h:86
clang::doc::RECORD_NAME
Definition: BitcodeWriter.h:102
clang::doc::Reference::RefType
InfoType RefType
Definition: Representation.h:88
clang::doc::EnumInfo::Members
llvm::SmallVector< SmallString< 16 >, 4 > Members
Definition: Representation.h:272
clang::doc::RecordIdDsc
Definition: BitcodeWriter.cpp:88
clang::doc::FieldId::F_parent
clang::doc::IntAbbrev
static void IntAbbrev(std::shared_ptr< llvm::BitCodeAbbrev > &Abbrev)
Definition: BitcodeWriter.cpp:47
clang::doc::CommentInfo::AttrValues
llvm::SmallVector< SmallString< 16 >, 4 > AttrValues
Definition: Representation.h:68
clang::doc::COMMENT_PARAMNAME
Definition: BitcodeWriter.h:83
clang::doc::BI_ENUM_BLOCK_ID
Definition: BitcodeWriter.h:57
clang::doc::COMMENT_ATTRKEY
Definition: BitcodeWriter.h:87
clang::doc::BitCodeConstants::RecordSize
static constexpr unsigned RecordSize
Definition: BitcodeWriter.h:37
clang::doc::Info::Description
std::vector< CommentInfo > Description
Definition: Representation.h:174
clang::doc::NamespaceInfo
Definition: Representation.h:185
clang::doc::BI_VERSION_BLOCK_ID
Definition: BitcodeWriter.h:55
clang::doc::ENUM_USR
Definition: BitcodeWriter.h:95
clang::doc::RecordInfo
Definition: Representation.h:234
clang::doc::MemberTypeInfo::Access
AccessSpecifier Access
Definition: Representation.h:136
clang::doc::CommentInfo::Args
llvm::SmallVector< SmallString< 16 >, 4 > Args
Definition: Representation.h:70
clang::doc::AbbrevDsc
void(*)(std::shared_ptr< llvm::BitCodeAbbrev > &Abbrev) AbbrevDsc
Definition: BitcodeWriter.cpp:32
clang::doc::RecordId
RecordId
Definition: BitcodeWriter.h:71
clang::doc::BI_REFERENCE_BLOCK_ID
Definition: BitcodeWriter.h:64
clang::doc::BI_COMMENT_BLOCK_ID
Definition: BitcodeWriter.h:63
clang::doc::Info::Name
SmallString< 16 > Name
Definition: Representation.h:171
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::doc::RI_FIRST
Definition: BitcodeWriter.h:111
clang::doc::FUNCTION_DEFLOCATION
Definition: BitcodeWriter.h:75
clang::doc::CommentInfo::Explicit
bool Explicit
Definition: Representation.h:63
clang::doc::COMMENT_DIRECTION
Definition: BitcodeWriter.h:82
clang::doc::Info
A base struct for Infos.
Definition: Representation.h:157
clang::doc::COMMENT_ARG
Definition: BitcodeWriter.h:89
clang::doc::BI_FIELD_TYPE_BLOCK_ID
Definition: BitcodeWriter.h:59
clang::doc::RecordInfo::Parents
llvm::SmallVector< Reference, 4 > Parents
Definition: Representation.h:247
clang::doc::BI_TYPE_BLOCK_ID
Definition: BitcodeWriter.h:58
clang::doc::Location
Definition: Representation.h:142
clang::doc::BlockId
BlockId
Definition: BitcodeWriter.h:54
BitcodeWriter.h
clang::doc::MEMBER_TYPE_ACCESS
Definition: BitcodeWriter.h:92
clang::doc::FUNCTION_NAME
Definition: BitcodeWriter.h:74
clang::doc::CommentInfo::ParamName
SmallString< 16 > ParamName
Definition: Representation.h:60
clang::doc::FieldId::F_type
clang::doc::COMMENT_EXPLICIT
Definition: BitcodeWriter.h:86
clang::doc::RECORD_LOCATION
Definition: BitcodeWriter.h:104
clang::doc::RecordInfo::TagType
TagTypeKind TagType
Definition: Representation.h:242
clang::doc::BlockIdNameMap
static const llvm::IndexedMap< llvm::StringRef, BlockIdToIndexFunctor > BlockIdNameMap
Definition: BitcodeWriter.cpp:103
clang::doc::EnumInfo
Definition: Representation.h:264
clang::doc::CommentInfo::AttrKeys
llvm::SmallVector< SmallString< 16 >, 4 > AttrKeys
Definition: Representation.h:66
clang::doc::CommentInfo::Name
SmallString< 16 > Name
Definition: Representation.h:58
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::doc::FieldTypeInfo::Name
SmallString< 16 > Name
Definition: Representation.h:118
clang::doc::BI_NAMESPACE_BLOCK_ID
Definition: BitcodeWriter.h:56
clang::doc::NAMESPACE_USR
Definition: BitcodeWriter.h:93
clang::doc::TypeInfo
Definition: Representation.h:94
clang::doc::MemberTypeInfo
Definition: Representation.h:122
clang::doc::NamespaceInfo::ChildNamespaces
std::vector< Reference > ChildNamespaces
Definition: Representation.h:197
clang::doc::BI_FIRST
Definition: BitcodeWriter.h:66
clang::doc::BitCodeConstants::IntSize
static constexpr unsigned IntSize
Definition: BitcodeWriter.h:41
clang::doc::CommentInfo::CloseName
SmallString< 16 > CloseName
Definition: Representation.h:61
clang::doc::BI_FUNCTION_BLOCK_ID
Definition: BitcodeWriter.h:62
clang::doc::BitCodeConstants::USRBitLengthSize
static constexpr unsigned USRBitLengthSize
Definition: BitcodeWriter.h:47
clang::doc::NamespaceInfo::ChildRecords
std::vector< Reference > ChildRecords
Definition: Representation.h:198
clang::doc::COMMENT_KIND
Definition: BitcodeWriter.h:79
clang::doc::Location::Filename
SmallString< 32 > Filename
Definition: Representation.h:153
clang::doc::REFERENCE_TYPE
Definition: BitcodeWriter.h:108
clang::doc::CommentInfo::Direction
SmallString< 8 > Direction
Definition: Representation.h:59
clang::doc::Info::IT
const InfoType IT
Definition: Representation.h:170
clang::doc::FUNCTION_LOCATION
Definition: BitcodeWriter.h:76
clang::doc::RecordInfo::ChildEnums
std::vector< EnumInfo > ChildEnums
Definition: Representation.h:259
clang::doc::FieldId::F_namespace
clang::doc::BitCodeConstants::USRLengthSize
static constexpr unsigned USRLengthSize
Definition: BitcodeWriter.h:46
clang::doc::FunctionInfo::ReturnType
TypeInfo ReturnType
Definition: Representation.h:225
clang::doc::ENUM_SCOPED
Definition: BitcodeWriter.h:100
clang::doc::FieldTypeInfo
Definition: Representation.h:106
clang::doc::RECORD_USR
Definition: BitcodeWriter.h:101
clang::doc::LocationAbbrev
static void LocationAbbrev(std::shared_ptr< llvm::BitCodeAbbrev > &Abbrev)
Definition: BitcodeWriter.cpp:75
clang::doc::RecordInfo::Members
llvm::SmallVector< MemberTypeInfo, 4 > Members
Definition: Representation.h:246
clang::doc::Info::USR
SymbolID USR
Definition: Representation.h:168
clang::doc::CommentInfo::Kind
SmallString< 16 > Kind
Definition: Representation.h:52
clang::doc::CommentInfo::Children
std::vector< std::unique_ptr< CommentInfo > > Children
Definition: Representation.h:72
clang::doc::CommentInfo::Text
SmallString< 64 > Text
Definition: Representation.h:57
clang::doc::BitCodeConstants::StringLengthSize
static constexpr unsigned StringLengthSize
Definition: BitcodeWriter.h:42
clang::doc::FieldId::F_vparent
clang::doc::SymbolID
std::array< uint8_t, 20 > SymbolID
Definition: Representation.h:31
clang::doc::FunctionInfo::IsMethod
bool IsMethod
Definition: Representation.h:223
clang::doc::BlockIdToIndexFunctor
Definition: BitcodeWriter.cpp:22
clang::doc::ENUM_MEMBER
Definition: BitcodeWriter.h:99
clang::doc::FieldId::F_child_record
clang::doc::RecordIdCount
static constexpr unsigned RecordIdCount
Definition: BitcodeWriter.h:115
clang::doc::CommentInfo
Definition: Representation.h:46
clang::doc::COMMENT_SELFCLOSING
Definition: BitcodeWriter.h:85
clang::doc::Location::LineNumber
int LineNumber
Definition: Representation.h:152