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<OwnedPtr<Info>, OwnedPtr<Info>> emitInfo(const NamespaceDecl *D,
41 const FullComment *FC,
42 Location Loc,
43 bool PublicOnly);
44
45 std::pair<OwnedPtr<Info>, OwnedPtr<Info>> emitInfo(const RecordDecl *D,
46 const FullComment *FC,
47 Location Loc,
48 bool PublicOnly);
49
50 std::pair<OwnedPtr<Info>, OwnedPtr<Info>> emitInfo(const EnumDecl *D,
51 const FullComment *FC,
52 Location Loc,
53 bool PublicOnly);
54
55 std::pair<OwnedPtr<Info>, OwnedPtr<Info>> emitInfo(const FunctionDecl *D,
56 const FullComment *FC,
57 Location Loc,
58 bool PublicOnly);
59
60 std::pair<OwnedPtr<Info>, OwnedPtr<Info>>
61 emitInfo(const VarDecl *D, const FullComment *FC, int LineNumber,
62 StringRef File, bool IsFileInRootDir, bool PublicOnly);
63
64 std::pair<OwnedPtr<Info>, OwnedPtr<Info>> emitInfo(const CXXMethodDecl *D,
65 const FullComment *FC,
66 Location Loc,
67 bool PublicOnly);
68
69 std::pair<OwnedPtr<Info>, OwnedPtr<Info>> emitInfo(const TypedefDecl *D,
70 const FullComment *FC,
71 Location Loc,
72 bool PublicOnly);
73
74 std::pair<OwnedPtr<Info>, OwnedPtr<Info>> emitInfo(const TypeAliasDecl *D,
75 const FullComment *FC,
76 Location Loc,
77 bool PublicOnly);
78
79 std::pair<OwnedPtr<Info>, OwnedPtr<Info>> emitInfo(const ConceptDecl *D,
80 const FullComment *FC,
81 const Location &Loc,
82 bool PublicOnly);
83
84 std::pair<OwnedPtr<Info>, OwnedPtr<Info>> emitInfo(const VarDecl *D,
85 const FullComment *FC,
86 const Location &Loc,
87 bool PublicOnly);
88
89private:
90 void getTemplateParameters(const TemplateParameterList *TemplateParams,
91 llvm::raw_ostream &Stream);
92
93 llvm::SmallString<256> getFunctionPrototype(const FunctionDecl *FuncDecl);
94
95 llvm::SmallString<16> getTypeAlias(const TypeAliasDecl *Alias);
96
97 llvm::SmallString<128>
98 getInfoRelativePath(const llvm::SmallVectorImpl<doc::Reference> &Namespaces);
99
100 llvm::SmallString<128> getInfoRelativePath(const Decl *D);
101
102 std::string getSourceCode(const Decl *D, const SourceRange &R);
103
104 void parseFullComment(const FullComment *C, CommentInfo &CI);
105
106 SymbolID getUSRForDecl(const Decl *D);
107
108 TagDecl *getTagDeclForType(const QualType &T);
109
110 RecordDecl *getRecordDeclForType(const QualType &T);
111
112 TypeInfo getTypeInfoForType(const QualType &T, const PrintingPolicy &Policy);
113
114 bool isPublic(const clang::AccessSpecifier AS, const clang::Linkage Link);
115
116 bool shouldSerializeInfo(bool PublicOnly, bool IsInAnonymousNamespace,
117 const NamedDecl *D);
118
119 void InsertChild(ScopeChildren &Scope, const NamespaceInfo &Info);
120 void InsertChild(ScopeChildren &Scope, const RecordInfo &Info);
121 void InsertChild(ScopeChildren &Scope, EnumInfo Info);
122 void InsertChild(ScopeChildren &Scope, FunctionInfo Info);
123 void InsertChild(ScopeChildren &Scope, TypedefInfo Info);
124 void InsertChild(ScopeChildren &Scope, ConceptInfo Info);
125 void InsertChild(ScopeChildren &Scope, VarInfo Info);
126
127 template <typename ChildType>
128 OwnedPtr<Info> makeAndInsertIntoParent(ChildType Child);
129
130 AccessSpecifier getFinalAccessSpecifier(AccessSpecifier FirstAS,
131 AccessSpecifier SecondAS);
132
133 void parseFields(RecordInfo &I, const RecordDecl *D, bool PublicOnly,
134 AccessSpecifier Access = AccessSpecifier::AS_public);
135
136 void parseEnumerators(EnumInfo &I, const EnumDecl *D);
137
138 void parseParameters(FunctionInfo &I, const FunctionDecl *D);
139
140 void parseBases(RecordInfo &I, const CXXRecordDecl *D);
141
142 void parseBases(RecordInfo &I, const CXXRecordDecl *D, bool IsFileInRootDir,
143 bool PublicOnly, bool IsParent,
144 AccessSpecifier ParentAccess = AccessSpecifier::AS_public);
145
146 template <typename T>
147 void populateParentNamespaces(llvm::SmallVector<Reference, 4> &Namespaces,
148 const T *D, bool &IsInAnonymousNamespace);
149
150 void populateTemplateParameters(std::optional<TemplateInfo> &TemplateInfo,
151 const clang::Decl *D);
152
153 TemplateParamInfo convertTemplateArgToInfo(const clang::Decl *D,
154 const TemplateArgument &Arg);
155
156 bool isSupportedContext(Decl::Kind DeclKind);
157
158 void findParent(Info &I, const Decl *D);
159
160 template <typename T>
161 void populateInfo(Info &I, const T *D, const FullComment *C,
162 bool &IsInAnonymousNamespace);
163
164 template <typename T>
165 void populateSymbolInfo(SymbolInfo &I, const T *D, const FullComment *C,
166 Location Loc, bool &IsInAnonymousNamespace);
167
168 void handleCompoundConstraints(const Expr *Constraint,
169 OwningVec<ConstraintInfo> &ConstraintInfos);
170
171 void populateConstraints(TemplateInfo &I, const TemplateDecl *D);
172
173 void populateFunctionInfo(FunctionInfo &I, const FunctionDecl *D,
174 const FullComment *FC, Location Loc,
175 bool &IsInAnonymousNamespace);
176
177 template <typename T> void populateMemberTypeInfo(T &I, const Decl *D);
178
179 void populateMemberTypeInfo(RecordInfo &I, AccessSpecifier &Access,
180 const DeclaratorDecl *D, bool IsStatic = false);
181
182 void parseFriends(RecordInfo &RI, const CXXRecordDecl *D);
183
184 void extractCommentFromDecl(const Decl *D, TypedefInfo &Info);
185};
186
187// Function to hash a given USR value for storage.
188// As USRs (Unified Symbol Resolution) could be large, especially for functions
189// with long type arguments, we use 160-bits SHA1(USR) values to
190// guarantee the uniqueness of symbols while using a relatively small amount of
191// memory (vs storing USRs directly).
192SymbolID hashUSR(llvm::StringRef USR);
193
194std::string serialize(OwnedPtr<Info> &I, DiagnosticsEngine &Diags);
195
196} // namespace serialize
197} // namespace doc
198} // namespace clang
199
200#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< OwnedPtr< Info >, OwnedPtr< Info > > emitInfo(const VarDecl *D, const FullComment *FC, int LineNumber, StringRef File, bool IsFileInRootDir, bool PublicOnly)
std::pair< OwnedPtr< Info >, OwnedPtr< Info > > emitInfo(const NamespaceDecl *D, const FullComment *FC, Location Loc, bool PublicOnly)
static std::string serialize(T &I, DiagnosticsEngine &Diags)
SymbolID hashUSR(llvm::StringRef USR)
Definition Serialize.cpp:40
std::unique_ptr< T > OwnedPtr
std::vector< T > OwningVec
std::array< uint8_t, 20 > SymbolID
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
A base struct for Infos.
static constexpr const char FuncDecl[]