1 //===--- Comment.cpp - Comment AST node implementation --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "clang/AST/Comment.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/AST/DeclObjC.h"
13 #include "clang/AST/DeclTemplate.h"
14 #include "clang/Basic/CharInfo.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include <type_traits>
21 // Check that no comment class has a non-trival destructor. They are allocated
22 // with a BumpPtrAllocator and therefore their destructor is not executed.
23 #define ABSTRACT_COMMENT(COMMENT)
24 #define COMMENT(CLASS, PARENT) \
25 static_assert(std::is_trivially_destructible<CLASS>::value, \
26 #CLASS " should be trivially destructible!");
27 #include "clang/AST/CommentNodes.inc"
29 #undef ABSTRACT_COMMENT
31 // DeclInfo is also allocated with a BumpPtrAllocator.
32 static_assert(std::is_trivially_destructible_v
<DeclInfo
>,
33 "DeclInfo should be trivially destructible!");
35 const char *Comment::getCommentKindName() const {
36 switch (getCommentKind()) {
37 case NoCommentKind
: return "NoCommentKind";
38 #define ABSTRACT_COMMENT(COMMENT)
39 #define COMMENT(CLASS, PARENT) \
42 #include "clang/AST/CommentNodes.inc"
44 #undef ABSTRACT_COMMENT
46 llvm_unreachable("Unknown comment kind!");
54 good
implements_child_begin_end(Comment::child_iterator (T::*)() const) {
59 static inline bad
implements_child_begin_end(
60 Comment::child_iterator (Comment::*)() const) {
64 #define ASSERT_IMPLEMENTS_child_begin(function) \
65 (void) good(implements_child_begin_end(function))
68 static inline void CheckCommentASTNodes() {
69 #define ABSTRACT_COMMENT(COMMENT)
70 #define COMMENT(CLASS, PARENT) \
71 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \
72 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end);
73 #include "clang/AST/CommentNodes.inc"
75 #undef ABSTRACT_COMMENT
78 #undef ASSERT_IMPLEMENTS_child_begin
80 } // end unnamed namespace
82 Comment::child_iterator
Comment::child_begin() const {
83 switch (getCommentKind()) {
84 case NoCommentKind
: llvm_unreachable("comment without a kind");
85 #define ABSTRACT_COMMENT(COMMENT)
86 #define COMMENT(CLASS, PARENT) \
88 return static_cast<const CLASS *>(this)->child_begin();
89 #include "clang/AST/CommentNodes.inc"
91 #undef ABSTRACT_COMMENT
93 llvm_unreachable("Unknown comment kind!");
96 Comment::child_iterator
Comment::child_end() const {
97 switch (getCommentKind()) {
98 case NoCommentKind
: llvm_unreachable("comment without a kind");
99 #define ABSTRACT_COMMENT(COMMENT)
100 #define COMMENT(CLASS, PARENT) \
102 return static_cast<const CLASS *>(this)->child_end();
103 #include "clang/AST/CommentNodes.inc"
105 #undef ABSTRACT_COMMENT
107 llvm_unreachable("Unknown comment kind!");
110 bool TextComment::isWhitespaceNoCache() const {
111 return llvm::all_of(Text
, clang::isWhitespace
);
114 bool ParagraphComment::isWhitespaceNoCache() const {
115 for (child_iterator I
= child_begin(), E
= child_end(); I
!= E
; ++I
) {
116 if (const TextComment
*TC
= dyn_cast
<TextComment
>(*I
)) {
117 if (!TC
->isWhitespace())
125 static TypeLoc
lookThroughTypedefOrTypeAliasLocs(TypeLoc
&SrcTL
) {
126 TypeLoc TL
= SrcTL
.IgnoreParens();
128 // Look through attribute types.
129 if (AttributedTypeLoc AttributeTL
= TL
.getAs
<AttributedTypeLoc
>())
130 return AttributeTL
.getModifiedLoc();
131 // Look through qualified types.
132 if (QualifiedTypeLoc QualifiedTL
= TL
.getAs
<QualifiedTypeLoc
>())
133 return QualifiedTL
.getUnqualifiedLoc();
134 // Look through pointer types.
135 if (PointerTypeLoc PointerTL
= TL
.getAs
<PointerTypeLoc
>())
136 return PointerTL
.getPointeeLoc().getUnqualifiedLoc();
137 // Look through reference types.
138 if (ReferenceTypeLoc ReferenceTL
= TL
.getAs
<ReferenceTypeLoc
>())
139 return ReferenceTL
.getPointeeLoc().getUnqualifiedLoc();
140 // Look through adjusted types.
141 if (AdjustedTypeLoc ATL
= TL
.getAs
<AdjustedTypeLoc
>())
142 return ATL
.getOriginalLoc();
143 if (BlockPointerTypeLoc BlockPointerTL
= TL
.getAs
<BlockPointerTypeLoc
>())
144 return BlockPointerTL
.getPointeeLoc().getUnqualifiedLoc();
145 if (MemberPointerTypeLoc MemberPointerTL
= TL
.getAs
<MemberPointerTypeLoc
>())
146 return MemberPointerTL
.getPointeeLoc().getUnqualifiedLoc();
147 if (ElaboratedTypeLoc ETL
= TL
.getAs
<ElaboratedTypeLoc
>())
148 return ETL
.getNamedTypeLoc();
153 static bool getFunctionTypeLoc(TypeLoc TL
, FunctionTypeLoc
&ResFTL
) {
155 while (PrevTL
!= TL
) {
157 TL
= lookThroughTypedefOrTypeAliasLocs(TL
);
160 if (FunctionTypeLoc FTL
= TL
.getAs
<FunctionTypeLoc
>()) {
165 if (TemplateSpecializationTypeLoc STL
=
166 TL
.getAs
<TemplateSpecializationTypeLoc
>()) {
167 // If we have a typedef to a template specialization with exactly one
168 // template argument of a function type, this looks like std::function,
169 // boost::function, or other function wrapper. Treat these typedefs as
171 if (STL
.getNumArgs() != 1)
173 TemplateArgumentLoc MaybeFunction
= STL
.getArgLoc(0);
174 if (MaybeFunction
.getArgument().getKind() != TemplateArgument::Type
)
176 TypeSourceInfo
*MaybeFunctionTSI
= MaybeFunction
.getTypeSourceInfo();
177 TypeLoc TL
= MaybeFunctionTSI
->getTypeLoc().getUnqualifiedLoc();
178 if (FunctionTypeLoc FTL
= TL
.getAs
<FunctionTypeLoc
>()) {
187 const char *ParamCommandComment::getDirectionAsString(PassDirection D
) {
189 case ParamCommandComment::In
:
191 case ParamCommandComment::Out
:
193 case ParamCommandComment::InOut
:
196 llvm_unreachable("unknown PassDirection");
199 void DeclInfo::fill() {
204 TemplateKind
= NotTemplate
;
205 IsObjCMethod
= false;
206 IsInstanceMethod
= false;
207 IsClassMethod
= false;
209 ParamVars
= std::nullopt
;
210 TemplateParameters
= nullptr;
213 // If there is no declaration, the defaults is our only guess.
217 CurrentDecl
= CommentDecl
;
219 Decl::Kind K
= CommentDecl
->getKind();
220 const TypeSourceInfo
*TSI
= nullptr;
223 // Defaults are should be good for declarations we don't handle explicitly.
226 case Decl::CXXMethod
:
227 case Decl::CXXConstructor
:
228 case Decl::CXXDestructor
:
229 case Decl::CXXConversion
: {
230 const FunctionDecl
*FD
= cast
<FunctionDecl
>(CommentDecl
);
232 ParamVars
= FD
->parameters();
233 ReturnType
= FD
->getReturnType();
234 unsigned NumLists
= FD
->getNumTemplateParameterLists();
236 TemplateKind
= TemplateSpecialization
;
238 FD
->getTemplateParameterList(NumLists
- 1);
241 if (K
== Decl::CXXMethod
|| K
== Decl::CXXConstructor
||
242 K
== Decl::CXXDestructor
|| K
== Decl::CXXConversion
) {
243 const CXXMethodDecl
*MD
= cast
<CXXMethodDecl
>(CommentDecl
);
244 IsInstanceMethod
= MD
->isInstance();
245 IsClassMethod
= !IsInstanceMethod
;
247 IsVariadic
= FD
->isVariadic();
248 assert(involvesFunctionType());
251 case Decl::ObjCMethod
: {
252 const ObjCMethodDecl
*MD
= cast
<ObjCMethodDecl
>(CommentDecl
);
254 ParamVars
= MD
->parameters();
255 ReturnType
= MD
->getReturnType();
257 IsInstanceMethod
= MD
->isInstanceMethod();
258 IsClassMethod
= !IsInstanceMethod
;
259 IsVariadic
= MD
->isVariadic();
260 assert(involvesFunctionType());
263 case Decl::FunctionTemplate
: {
264 const FunctionTemplateDecl
*FTD
= cast
<FunctionTemplateDecl
>(CommentDecl
);
266 TemplateKind
= Template
;
267 const FunctionDecl
*FD
= FTD
->getTemplatedDecl();
268 ParamVars
= FD
->parameters();
269 ReturnType
= FD
->getReturnType();
270 TemplateParameters
= FTD
->getTemplateParameters();
271 IsVariadic
= FD
->isVariadic();
272 assert(involvesFunctionType());
275 case Decl::ClassTemplate
: {
276 const ClassTemplateDecl
*CTD
= cast
<ClassTemplateDecl
>(CommentDecl
);
278 TemplateKind
= Template
;
279 TemplateParameters
= CTD
->getTemplateParameters();
282 case Decl::ClassTemplatePartialSpecialization
: {
283 const ClassTemplatePartialSpecializationDecl
*CTPSD
=
284 cast
<ClassTemplatePartialSpecializationDecl
>(CommentDecl
);
286 TemplateKind
= TemplatePartialSpecialization
;
287 TemplateParameters
= CTPSD
->getTemplateParameters();
290 case Decl::ClassTemplateSpecialization
:
292 TemplateKind
= TemplateSpecialization
;
295 case Decl::CXXRecord
:
299 if (const VarTemplateDecl
*VTD
=
300 cast
<VarDecl
>(CommentDecl
)->getDescribedVarTemplate()) {
301 TemplateKind
= TemplateSpecialization
;
302 TemplateParameters
= VTD
->getTemplateParameters();
306 case Decl::EnumConstant
:
308 case Decl::ObjCAtDefsField
:
309 case Decl::ObjCProperty
:
310 if (const auto *VD
= dyn_cast
<DeclaratorDecl
>(CommentDecl
))
311 TSI
= VD
->getTypeSourceInfo();
312 else if (const auto *PD
= dyn_cast
<ObjCPropertyDecl
>(CommentDecl
))
313 TSI
= PD
->getTypeSourceInfo();
316 case Decl::VarTemplate
: {
317 const VarTemplateDecl
*VTD
= cast
<VarTemplateDecl
>(CommentDecl
);
319 TemplateKind
= Template
;
320 TemplateParameters
= VTD
->getTemplateParameters();
321 if (const VarDecl
*VD
= VTD
->getTemplatedDecl())
322 TSI
= VD
->getTypeSourceInfo();
325 case Decl::Namespace
:
326 Kind
= NamespaceKind
;
328 case Decl::TypeAlias
:
331 TSI
= cast
<TypedefNameDecl
>(CommentDecl
)->getTypeSourceInfo();
333 case Decl::TypeAliasTemplate
: {
334 const TypeAliasTemplateDecl
*TAT
= cast
<TypeAliasTemplateDecl
>(CommentDecl
);
336 TemplateKind
= Template
;
337 TemplateParameters
= TAT
->getTemplateParameters();
338 if (TypeAliasDecl
*TAD
= TAT
->getTemplatedDecl())
339 TSI
= TAD
->getTypeSourceInfo();
347 // If the type is a typedef / using to something we consider a function,
348 // extract arguments and return type.
350 TypeLoc TL
= TSI
->getTypeLoc().getUnqualifiedLoc();
352 if (getFunctionTypeLoc(TL
, FTL
)) {
353 ParamVars
= FTL
.getParams();
354 ReturnType
= FTL
.getReturnLoc().getType();
355 if (const auto *FPT
= dyn_cast
<FunctionProtoType
>(FTL
.getTypePtr()))
356 IsVariadic
= FPT
->isVariadic();
357 assert(involvesFunctionType());
364 StringRef
ParamCommandComment::getParamName(const FullComment
*FC
) const {
365 assert(isParamIndexValid());
368 return FC
->getDeclInfo()->ParamVars
[getParamIndex()]->getName();
371 StringRef
TParamCommandComment::getParamName(const FullComment
*FC
) const {
372 assert(isPositionValid());
373 const TemplateParameterList
*TPL
= FC
->getDeclInfo()->TemplateParameters
;
374 for (unsigned i
= 0, e
= getDepth(); i
!= e
; ++i
) {
375 assert(TPL
&& "Unknown TemplateParameterList");
377 return TPL
->getParam(getIndex(i
))->getName();
378 const NamedDecl
*Param
= TPL
->getParam(getIndex(i
));
379 if (auto *TTP
= dyn_cast
<TemplateTemplateParmDecl
>(Param
))
380 TPL
= TTP
->getTemplateParameters();
385 } // end namespace comments
386 } // end namespace clang