clang 22.0.0git
Comment.cpp
Go to the documentation of this file.
1//===--- Comment.cpp - Comment AST node implementation --------------------===//
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#include "clang/AST/Comment.h"
11#include "clang/AST/Decl.h"
12#include "clang/AST/DeclObjC.h"
15#include "llvm/Support/ErrorHandling.h"
16#include <type_traits>
17
18namespace clang {
19namespace comments {
20
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"
28#undef COMMENT
29#undef ABSTRACT_COMMENT
30
31// DeclInfo is also allocated with a BumpPtrAllocator.
32static_assert(std::is_trivially_destructible_v<DeclInfo>,
33 "DeclInfo should be trivially destructible!");
34
35const char *Comment::getCommentKindName() const {
36 switch (getCommentKind()) {
38 return "None";
39#define ABSTRACT_COMMENT(COMMENT)
40#define COMMENT(CLASS, PARENT) \
41 case CommentKind::CLASS: \
42 return #CLASS;
43#include "clang/AST/CommentNodes.inc"
44#undef COMMENT
45#undef ABSTRACT_COMMENT
46 }
47 llvm_unreachable("Unknown comment kind!");
48}
49
50namespace {
51struct good {};
52struct bad {};
53
54template <typename T>
55good implements_child_begin_end(Comment::child_iterator (T::*)() const) {
56 return good();
57}
58
59LLVM_ATTRIBUTE_UNUSED
60static inline bad implements_child_begin_end(
61 Comment::child_iterator (Comment::*)() const) {
62 return bad();
63}
64
65#define ASSERT_IMPLEMENTS_child_begin(function) \
66 (void) good(implements_child_begin_end(function))
67
68LLVM_ATTRIBUTE_UNUSED
69static inline void CheckCommentASTNodes() {
70#define ABSTRACT_COMMENT(COMMENT)
71#define COMMENT(CLASS, PARENT) \
72 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \
73 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end);
74#include "clang/AST/CommentNodes.inc"
75#undef COMMENT
76#undef ABSTRACT_COMMENT
77}
78
79#undef ASSERT_IMPLEMENTS_child_begin
80
81} // end unnamed namespace
82
84 switch (getCommentKind()) {
86 llvm_unreachable("comment without a kind");
87#define ABSTRACT_COMMENT(COMMENT)
88#define COMMENT(CLASS, PARENT) \
89 case CommentKind::CLASS: \
90 return static_cast<const CLASS *>(this)->child_begin();
91#include "clang/AST/CommentNodes.inc"
92#undef COMMENT
93#undef ABSTRACT_COMMENT
94 }
95 llvm_unreachable("Unknown comment kind!");
96}
97
99 switch (getCommentKind()) {
101 llvm_unreachable("comment without a kind");
102#define ABSTRACT_COMMENT(COMMENT)
103#define COMMENT(CLASS, PARENT) \
104 case CommentKind::CLASS: \
105 return static_cast<const CLASS *>(this)->child_end();
106#include "clang/AST/CommentNodes.inc"
107#undef COMMENT
108#undef ABSTRACT_COMMENT
109 }
110 llvm_unreachable("Unknown comment kind!");
111}
112
113bool TextComment::isWhitespaceNoCache() const {
114 return llvm::all_of(Text, clang::isWhitespace);
115}
116
117bool ParagraphComment::isWhitespaceNoCache() const {
118 for (child_iterator I = child_begin(), E = child_end(); I != E; ++I) {
119 if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
120 if (!TC->isWhitespace())
121 return false;
122 } else
123 return false;
124 }
125 return true;
126}
127
129 TypeLoc TL = SrcTL.IgnoreParens();
130
131 // Look through attribute types.
132 if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>())
133 return AttributeTL.getModifiedLoc();
134 // Look through qualified types.
135 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
136 return QualifiedTL.getUnqualifiedLoc();
137 // Look through pointer types.
138 if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
139 return PointerTL.getPointeeLoc().getUnqualifiedLoc();
140 // Look through reference types.
141 if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
142 return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
143 // Look through adjusted types.
144 if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
145 return ATL.getOriginalLoc();
146 if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
147 return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
148 if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
149 return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
150
151 return TL;
152}
153
155 TypeLoc PrevTL;
156 while (PrevTL != TL) {
157 PrevTL = TL;
159 }
160
161 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
162 ResFTL = FTL;
163 return true;
164 }
165
168 // If we have a typedef to a template specialization with exactly one
169 // template argument of a function type, this looks like std::function,
170 // boost::function, or other function wrapper. Treat these typedefs as
171 // functions.
172 if (STL.getNumArgs() != 1)
173 return false;
174 TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
175 if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
176 return false;
177 TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
178 TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
179 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
180 ResFTL = FTL;
181 return true;
182 }
183 }
184
185 return false;
186}
187
188const char *
190 switch (D) {
192 return "[in]";
194 return "[out]";
196 return "[in,out]";
197 }
198 llvm_unreachable("unknown PassDirection");
199}
200
202 assert(!IsFilled);
203
204 // Set defaults.
205 Kind = OtherKind;
207 IsObjCMethod = false;
208 IsInstanceMethod = false;
209 IsClassMethod = false;
210 IsVariadic = false;
211 ParamVars = {};
212 TemplateParameters = nullptr;
213
214 if (!CommentDecl) {
215 // If there is no declaration, the defaults is our only guess.
216 IsFilled = true;
217 return;
218 }
220
222 const TypeSourceInfo *TSI = nullptr;
223 switch (K) {
224 default:
225 // Defaults are should be good for declarations we don't handle explicitly.
226 break;
227 case Decl::Function:
228 case Decl::CXXMethod:
229 case Decl::CXXConstructor:
230 case Decl::CXXDestructor:
231 case Decl::CXXConversion: {
232 const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
234 ParamVars = FD->parameters();
236 unsigned NumLists = FD->getNumTemplateParameterLists();
237 if (NumLists != 0) {
240 FD->getTemplateParameterList(NumLists - 1);
241 }
242
243 if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
244 K == Decl::CXXDestructor || K == Decl::CXXConversion) {
245 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
248 }
249 IsVariadic = FD->isVariadic();
250 assert(involvesFunctionType());
251 break;
252 }
253 case Decl::ObjCMethod: {
254 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
256 ParamVars = MD->parameters();
258 IsObjCMethod = true;
261 IsVariadic = MD->isVariadic();
262 assert(involvesFunctionType());
263 break;
264 }
265 case Decl::FunctionTemplate: {
266 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
269 const FunctionDecl *FD = FTD->getTemplatedDecl();
270 ParamVars = FD->parameters();
273 IsVariadic = FD->isVariadic();
274 assert(involvesFunctionType());
275 break;
276 }
277 case Decl::ClassTemplate: {
278 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
279 Kind = ClassKind;
282 break;
283 }
284 case Decl::ClassTemplatePartialSpecialization: {
286 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
287 Kind = ClassKind;
290 break;
291 }
292 case Decl::VarTemplatePartialSpecialization: {
293 const auto *VTPSD = cast<VarTemplatePartialSpecializationDecl>(CommentDecl);
296 TemplateParameters = VTPSD->getTemplateParameters();
297 break;
298 }
299 case Decl::ClassTemplateSpecialization:
300 Kind = ClassKind;
302 break;
303 case Decl::Record:
304 case Decl::CXXRecord:
305 Kind = ClassKind;
306 break;
307 case Decl::Var:
308 if (const VarTemplateDecl *VTD =
309 cast<VarDecl>(CommentDecl)->getDescribedVarTemplate()) {
311 TemplateParameters = VTD->getTemplateParameters();
312 }
313 [[fallthrough]];
314 case Decl::Field:
315 case Decl::EnumConstant:
316 case Decl::ObjCIvar:
317 case Decl::ObjCAtDefsField:
318 case Decl::ObjCProperty:
319 if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
320 TSI = VD->getTypeSourceInfo();
321 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
322 TSI = PD->getTypeSourceInfo();
324 break;
325 case Decl::VarTemplate: {
326 const VarTemplateDecl *VTD = cast<VarTemplateDecl>(CommentDecl);
330 if (const VarDecl *VD = VTD->getTemplatedDecl())
331 TSI = VD->getTypeSourceInfo();
332 break;
333 }
334 case Decl::Namespace:
336 break;
337 case Decl::TypeAlias:
338 case Decl::Typedef:
340 TSI = cast<TypedefNameDecl>(CommentDecl)->getTypeSourceInfo();
341 break;
342 case Decl::TypeAliasTemplate: {
343 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
347 if (TypeAliasDecl *TAD = TAT->getTemplatedDecl())
348 TSI = TAD->getTypeSourceInfo();
349 break;
350 }
351 case Decl::Enum:
352 Kind = EnumKind;
353 break;
354 }
355
356 // If the type is a typedef / using to something we consider a function,
357 // extract arguments and return type.
358 if (TSI) {
360 FunctionTypeLoc FTL;
361 if (getFunctionTypeLoc(TL, FTL)) {
362 ParamVars = FTL.getParams();
364 if (const auto *FPT = dyn_cast<FunctionProtoType>(FTL.getTypePtr()))
365 IsVariadic = FPT->isVariadic();
366 assert(involvesFunctionType());
367 }
368 }
369
370 IsFilled = true;
371}
372
374 assert(isParamIndexValid());
375 if (isVarArgParam())
376 return "...";
377 return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
378}
379
381 assert(isPositionValid());
383 for (unsigned i = 0, e = getDepth(); i != e; ++i) {
384 assert(TPL && "Unknown TemplateParameterList");
385 if (i == e - 1)
386 return TPL->getParam(getIndex(i))->getName();
387 const NamedDecl *Param = TPL->getParam(getIndex(i));
388 if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param))
389 TPL = TTP->getTemplateParameters();
390 }
391 return "";
392}
393
394} // end namespace comments
395} // end namespace clang
396
Defines the clang::ASTContext interface.
const Decl * D
Expr * E
Defines the C++ template declaration subclasses.
Type source information for an attributed type.
Definition: TypeLoc.h:1017
Wrapper for source info for block pointers.
Definition: TypeLoc.h:1506
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2129
bool isInstance() const
Definition: DeclCXX.h:2156
Declaration of a class template.
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
const TypeClass * getTypePtr() const
Definition: TypeLoc.h:438
Kind
Lists the kind of concrete classes of Decl.
Definition: DeclBase.h:89
Kind getKind() const
Definition: DeclBase.h:442
TemplateParameterList * getTemplateParameterList(unsigned index) const
Definition: Decl.h:865
unsigned getNumTemplateParameterLists() const
Definition: Decl.h:861
Represents a function declaration or definition.
Definition: Decl.h:1999
QualType getReturnType() const
Definition: Decl.h:2842
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2771
bool isVariadic() const
Whether this function is variadic.
Definition: Decl.cpp:3125
Declaration of a template function.
Definition: DeclTemplate.h:952
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
Definition: DeclTemplate.h:998
Wrapper for source info for functions.
Definition: TypeLoc.h:1624
ArrayRef< ParmVarDecl * > getParams() const
Definition: TypeLoc.h:1687
TypeLoc getReturnLoc() const
Definition: TypeLoc.h:1705
Wrapper for source info for member pointers.
Definition: TypeLoc.h:1524
This represents a decl that may have a name.
Definition: Decl.h:273
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:300
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
ArrayRef< ParmVarDecl * > parameters() const
Definition: DeclObjC.h:373
bool isVariadic() const
Definition: DeclObjC.h:431
bool isInstanceMethod() const
Definition: DeclObjC.h:426
QualType getReturnType() const
Definition: DeclObjC.h:329
Wrapper for source info for pointers.
Definition: TypeLoc.h:1493
Wrapper of type source information for a type with non-trivial direct qualifiers.
Definition: TypeLoc.h:305
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:528
const TemplateArgument & getArgument() const
Definition: TemplateBase.h:574
TypeSourceInfo * getTypeSourceInfo() const
Definition: TemplateBase.h:578
@ Type
The template argument is a type.
Definition: TemplateBase.h:70
ArgKind getKind() const
Return the kind of stored template argument.
Definition: TemplateBase.h:296
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Definition: DeclTemplate.h:415
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:74
NamedDecl * getParam(unsigned Idx)
Definition: DeclTemplate.h:146
Represents the declaration of a typedef-name via a C++11 alias-declaration.
Definition: Decl.h:3685
Declaration of an alias template.
TypeAliasDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:59
UnqualTypeLoc getUnqualifiedLoc() const
Skips past any qualifiers, if this is qualified.
Definition: TypeLoc.h:354
QualType getType() const
Get the type for which this source info wrapper provides information.
Definition: TypeLoc.h:133
T getAs() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition: TypeLoc.h:89
TypeLoc IgnoreParens() const
Definition: TypeLoc.h:1417
A container of type source information.
Definition: TypeBase.h:8314
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition: TypeLoc.h:272
Represents a variable declaration or definition.
Definition: Decl.h:925
Declaration of a variable template.
VarDecl * getTemplatedDecl() const
Get the underlying variable declarations of the template.
Comment *const * child_iterator
Definition: Comment.h:257
child_iterator child_end() const
Definition: Comment.cpp:98
child_iterator child_begin() const
Definition: Comment.cpp:83
const char * getCommentKindName() const
Definition: Comment.cpp:35
CommentKind getCommentKind() const
Definition: Comment.h:239
A full comment attached to a declaration, contains block content.
Definition: Comment.h:1104
const DeclInfo * getDeclInfo() const LLVM_READONLY
Definition: Comment.h:1136
child_iterator child_begin() const
Definition: Comment.h:601
child_iterator child_end() const
Definition: Comment.h:605
unsigned getParamIndex() const LLVM_READONLY
Definition: Comment.h:800
static const char * getDirectionAsString(ParamCommandPassDirection D)
Definition: Comment.cpp:189
StringRef getParamName(const FullComment *FC) const
Definition: Comment.cpp:373
bool isParamIndexValid() const LLVM_READONLY
Definition: Comment.h:787
bool isVarArgParam() const LLVM_READONLY
Definition: Comment.h:791
bool isPositionValid() const LLVM_READONLY
Definition: Comment.h:854
StringRef getParamName(const FullComment *FC) const
Definition: Comment.cpp:380
unsigned getIndex(unsigned Depth) const
Definition: Comment.h:863
ParamCommandPassDirection
Definition: Comment.h:729
static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL)
Definition: Comment.cpp:154
static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL)
Definition: Comment.cpp:128
The JSON file list parser is used to communicate input to InstallAPI.
LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: ' ', '\t',...
Definition: CharInfo.h:108
const FunctionProtoType * T
unsigned TemplateKind
Is CommentDecl a template declaration.
Definition: Comment.h:1068
unsigned IsClassMethod
Is CommentDecl a static member function of C++ class or class method of ObjC class.
Definition: Comment.h:1084
@ FunctionKind
Something that we consider a "function":
Definition: Comment.h:1025
@ EnumKind
An enumeration or scoped enumeration.
Definition: Comment.h:1047
@ OtherKind
Everything else not explicitly mentioned below.
Definition: Comment.h:1015
@ NamespaceKind
A C++ namespace.
Definition: Comment.h:1040
@ VariableKind
Something that we consider a "variable":
Definition: Comment.h:1037
@ ClassKind
Something that we consider a "class":
Definition: Comment.h:1031
@ TypedefKind
A C++ typedef-name (a 'typedef' decl specifier or alias-declaration), see TypedefNameDecl.
Definition: Comment.h:1044
unsigned Kind
Simplified kind of CommentDecl, see DeclKind enum.
Definition: Comment.h:1064
unsigned IsObjCMethod
Is CommentDecl an ObjCMethodDecl.
Definition: Comment.h:1072
const TemplateParameterList * TemplateParameters
Template parameters that can be referenced by \tparam if CommentDecl is a template (IsTemplateDecl or...
Definition: Comment.h:1009
ArrayRef< const ParmVarDecl * > ParamVars
Parameters that can be referenced by \param if CommentDecl is something that we consider a "function"...
Definition: Comment.h:1000
unsigned IsInstanceMethod
Is CommentDecl a non-static member function of C++ class or instance method of ObjC class.
Definition: Comment.h:1078
bool involvesFunctionType() const
Definition: Comment.h:1100
unsigned IsFilled
If false, only CommentDecl is valid.
Definition: Comment.h:1060
const Decl * CommentDecl
Declaration the comment is actually attached to (in the source).
Definition: Comment.h:986
unsigned IsVariadic
Is CommentDecl something we consider a "function" that's variadic.
Definition: Comment.h:1088
const Decl * CurrentDecl
CurrentDecl is the declaration with which the FullComment is associated.
Definition: Comment.h:996
QualType ReturnType
Function return type if CommentDecl is something that we consider a "function".
Definition: Comment.h:1004