clang-tools 23.0.0git
Serialize.h
Go to the documentation of this file.
1//===-- Serializer.h - ClangDoc Serializer ----------------------*- 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 the serializing functions fro the clang-doc tool. Given
10// a particular declaration, it collects the appropriate information and returns
11// a serialized bitcode string for the declaration.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_SERIALIZE_H
16#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_SERIALIZE_H
17
18#include "Representation.h"
19#include <string>
20
21using namespace clang::comments;
22
23namespace clang {
24namespace doc {
25namespace serialize {
26
27// The first element will contain the relevant information about the declaration
28// passed as parameter.
29// The second element will contain the relevant information about the
30// declaration's parent, it can be a NamespaceInfo or RecordInfo.
31// Both elements can be nullptrs if the declaration shouldn't be handled.
32// When the declaration is handled, the first element will be a nullptr for
33// EnumDecl, FunctionDecl and CXXMethodDecl; they are only returned wrapped in
34// its parent scope. For NamespaceDecl and RecordDecl both elements are not
35// nullptr.
37public:
38 Serializer() = default;
39
40 std::pair<Info *, Info *> emitInfo(const NamespaceDecl *D,
41 const FullComment *FC, Location Loc,
42 bool PublicOnly);
43
44 std::pair<Info *, Info *> emitInfo(const RecordDecl *D, const FullComment *FC,
45 Location Loc, bool PublicOnly);
46
47 std::pair<Info *, Info *> emitInfo(const EnumDecl *D, const FullComment *FC,
48 Location Loc, bool PublicOnly);
49
50 std::pair<Info *, Info *> emitInfo(const FunctionDecl *D,
51 const FullComment *FC, Location Loc,
52 bool PublicOnly);
53
54 std::pair<Info *, Info *> emitInfo(const VarDecl *D, const FullComment *FC,
55 int LineNumber, StringRef File,
56 bool IsFileInRootDir, bool PublicOnly);
57
58 std::pair<Info *, Info *> emitInfo(const CXXMethodDecl *D,
59 const FullComment *FC, Location Loc,
60 bool PublicOnly);
61
62 std::pair<Info *, Info *> emitInfo(const TypedefDecl *D,
63 const FullComment *FC, Location Loc,
64 bool PublicOnly);
65
66 std::pair<Info *, Info *> emitInfo(const TypeAliasDecl *D,
67 const FullComment *FC, Location Loc,
68 bool PublicOnly);
69
70 std::pair<Info *, Info *> emitInfo(const ConceptDecl *D,
71 const FullComment *FC, const Location &Loc,
72 bool PublicOnly);
73
74 std::pair<Info *, Info *> emitInfo(const VarDecl *D, const FullComment *FC,
75 const Location &Loc, bool PublicOnly);
76
77private:
78 void getTemplateParameters(const TemplateParameterList *TemplateParams,
79 llvm::raw_ostream &Stream);
80
81 StringRef getFunctionPrototype(const FunctionDecl *FuncDecl);
82
83 StringRef getTypeAlias(const TypeAliasDecl *Alias);
84
85 StringRef getInfoRelativePath(llvm::ArrayRef<doc::Reference> Namespaces);
86
87 StringRef getInfoRelativePath(const Decl *D);
88
89 llvm::StringRef getSourceCode(const Decl *D, const SourceRange &R);
90
91 void parseFullComment(const FullComment *C, CommentInfo &CI);
92
93 SymbolID getUSRForDecl(const Decl *D);
94
95 TagDecl *getTagDeclForType(const QualType &T);
96
97 RecordDecl *getRecordDeclForType(const QualType &T);
98
99 TypeInfo getTypeInfoForType(const QualType &T, const PrintingPolicy &Policy);
100
101 bool isPublic(const clang::AccessSpecifier AS, const clang::Linkage Link);
102
103 bool shouldSerializeInfo(bool PublicOnly, bool IsInAnonymousNamespace,
104 const NamedDecl *D);
105
106 void InsertChild(ScopeChildren &Scope, const NamespaceInfo &Info);
107 void InsertChild(ScopeChildren &Scope, const RecordInfo &Info);
108 void InsertChild(ScopeChildren &Scope, EnumInfo &Info);
109 void InsertChild(ScopeChildren &Scope, FunctionInfo &Info);
110 void InsertChild(ScopeChildren &Scope, TypedefInfo &Info);
111 void InsertChild(ScopeChildren &Scope, ConceptInfo &Info);
112 void InsertChild(ScopeChildren &Scope, VarInfo &Info);
113
114 template <typename ChildType> Info *makeAndInsertIntoParent(ChildType &Child);
115
116 AccessSpecifier getFinalAccessSpecifier(AccessSpecifier FirstAS,
117 AccessSpecifier SecondAS);
118
119 void parseFields(RecordInfo &I, const RecordDecl *D, bool PublicOnly,
120 AccessSpecifier Access = AccessSpecifier::AS_public);
121
122 void parseEnumerators(EnumInfo &I, const EnumDecl *D);
123
124 void parseParameters(FunctionInfo &I, const FunctionDecl *D);
125
126 void parseBases(RecordInfo &I, const CXXRecordDecl *D);
127
128 void parseBases(llvm::SmallVectorImpl<BaseRecordInfo> &Bases,
129 const CXXRecordDecl *D, bool IsFileInRootDir, bool PublicOnly,
130 bool IsParent,
131 AccessSpecifier ParentAccess = AccessSpecifier::AS_public);
132
133 template <typename T>
134 void populateParentNamespaces(llvm::SmallVector<Reference, 4> &Namespaces,
135 const T *D, bool &IsInAnonymousNamespace);
136
137 void populateTemplateParameters(std::optional<TemplateInfo> &TemplateInfo,
138 const clang::Decl *D);
139
140 TemplateParamInfo convertTemplateArgToInfo(const clang::Decl *D,
141 const TemplateArgument &Arg);
142
143 bool isSupportedContext(Decl::Kind DeclKind);
144
145 void findParent(Info &I, const Decl *D);
146
147 template <typename T>
148 void populateInfo(Info &I, const T *D, const FullComment *C,
149 bool &IsInAnonymousNamespace);
150
151 template <typename T>
152 void populateSymbolInfo(SymbolInfo &I, const T *D, const FullComment *C,
153 Location Loc, bool &IsInAnonymousNamespace);
154
155 void handleCompoundConstraints(
156 const Expr *Constraint,
158
159 void populateConstraints(TemplateInfo &I, const TemplateDecl *D);
160
161 void populateFunctionInfo(FunctionInfo &I, const FunctionDecl *D,
162 const FullComment *FC, Location Loc,
163 bool &IsInAnonymousNamespace);
164
165 template <typename T> void populateMemberTypeInfo(T &I, const Decl *D);
166
167 void populateMemberTypeInfo(llvm::SmallVectorImpl<MemberTypeInfo> &Members,
168 AccessSpecifier &Access, const DeclaratorDecl *D,
169 bool IsStatic = false);
170
171 void parseFriends(RecordInfo &RI, const CXXRecordDecl *D);
172
173 void extractCommentFromDecl(const Decl *D, TypedefInfo &Info);
174};
175
176// Function to hash a given USR value for storage.
177// As USRs (Unified Symbol Resolution) could be large, especially for functions
178// with long type arguments, we use 160-bits SHA1(USR) values to
179// guarantee the uniqueness of symbols while using a relatively small amount of
180// memory (vs storing USRs directly).
181SymbolID hashUSR(llvm::StringRef USR);
182
183std::string serialize(const Info &I, DiagnosticsEngine &Diags);
184
185} // namespace serialize
186} // namespace doc
187} // namespace clang
188
189#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_SERIALIZE_H
static llvm::cl::opt< bool > PublicOnly("public", llvm::cl::desc("Document only public declarations."), llvm::cl::init(false), llvm::cl::cat(ClangDocCategory))
std::pair< Info *, Info * > emitInfo(const VarDecl *D, const FullComment *FC, int LineNumber, StringRef File, bool IsFileInRootDir, bool PublicOnly)
std::pair< Info *, Info * > emitInfo(const NamespaceDecl *D, const FullComment *FC, Location Loc, bool PublicOnly)
SymbolID hashUSR(llvm::StringRef USR)
Definition Serialize.cpp:38
static std::string serialize(const T &I, DiagnosticsEngine &Diags)
std::array< uint8_t, 20 > SymbolID
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
A base struct for Infos.
static constexpr const char FuncDecl[]