clang 23.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
59[[maybe_unused]]
60static inline bad
61implements_child_begin_end(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
68[[maybe_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
221 Decl::Kind K = CommentDecl->getKind();
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: {
234 ParamVars = FD->parameters();
239 !TemplateParameters && !TPLs.empty())
240 TemplateParameters = TPLs.back();
243
244 if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
245 K == Decl::CXXDestructor || K == Decl::CXXConversion) {
249 }
250 IsVariadic = FD->isVariadic();
251 assert(involvesFunctionType());
252 break;
253 }
254 case Decl::ObjCMethod: {
257 ParamVars = MD->parameters();
259 IsObjCMethod = true;
262 IsVariadic = MD->isVariadic();
263 assert(involvesFunctionType());
264 break;
265 }
266 case Decl::FunctionTemplate: {
270 const FunctionDecl *FD = FTD->getTemplatedDecl();
271 ParamVars = FD->parameters();
274 IsVariadic = FD->isVariadic();
275 assert(involvesFunctionType());
276 break;
277 }
278 case Decl::ClassTemplate: {
280 Kind = ClassKind;
283 break;
284 }
285 case Decl::ClassTemplatePartialSpecialization: {
288 Kind = ClassKind;
291 break;
292 }
293 case Decl::VarTemplatePartialSpecialization: {
297 TemplateParameters = VTPSD->getTemplateParameters();
298 break;
299 }
300 case Decl::ClassTemplateSpecialization:
301 Kind = ClassKind;
303 break;
304 case Decl::Record:
305 case Decl::CXXRecord:
306 Kind = ClassKind;
307 break;
308 case Decl::Var:
309 if (const VarTemplateDecl *VTD =
310 cast<VarDecl>(CommentDecl)->getDescribedVarTemplate()) {
312 TemplateParameters = VTD->getTemplateParameters();
313 }
314 [[fallthrough]];
315 case Decl::Field:
316 case Decl::EnumConstant:
317 case Decl::ObjCIvar:
318 case Decl::ObjCAtDefsField:
319 case Decl::ObjCProperty:
320 if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
321 TSI = VD->getTypeSourceInfo();
322 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
323 TSI = PD->getTypeSourceInfo();
325 break;
326 case Decl::VarTemplate: {
331 if (const VarDecl *VD = VTD->getTemplatedDecl())
332 TSI = VD->getTypeSourceInfo();
333 break;
334 }
335 case Decl::Namespace:
337 break;
338 case Decl::TypeAlias:
339 case Decl::Typedef:
341 TSI = cast<TypedefNameDecl>(CommentDecl)->getTypeSourceInfo();
342 break;
343 case Decl::TypeAliasTemplate: {
348 if (TypeAliasDecl *TAD = TAT->getTemplatedDecl())
349 TSI = TAD->getTypeSourceInfo();
350 break;
351 }
352 case Decl::Enum:
353 Kind = EnumKind;
354 break;
355 case Decl::Concept:
359 TemplateParameters = Concept->getTemplateParameters();
360 break;
361 }
362
363 // If the type is a typedef / using to something we consider a function,
364 // extract arguments and return type.
365 if (TSI) {
367 FunctionTypeLoc FTL;
368 if (getFunctionTypeLoc(TL, FTL)) {
369 ParamVars = FTL.getParams();
371 if (const auto *FPT = dyn_cast<FunctionProtoType>(FTL.getTypePtr()))
372 IsVariadic = FPT->isVariadic();
373 assert(involvesFunctionType());
374 }
375 }
376
377 IsFilled = true;
378}
379
381 assert(isParamIndexValid());
382 if (isVarArgParam())
383 return "...";
384 return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
385}
386
388 assert(isPositionValid());
390 for (unsigned i = 0, e = getDepth(); i != e; ++i) {
391 assert(TPL && "Unknown TemplateParameterList");
392 if (i == e - 1)
393 return TPL->getParam(getIndex(i))->getName();
394 const NamedDecl *Param = TPL->getParam(getIndex(i));
395 if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param))
396 TPL = TTP->getTemplateParameters();
397 }
398 return "";
399}
400
401} // end namespace comments
402} // end namespace clang
403
Defines the clang::ASTContext interface.
Defines the C++ template declaration subclasses.
Type source information for an attributed type.
Definition TypeLoc.h:1008
Wrapper for source info for block pointers.
Definition TypeLoc.h:1526
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
bool isInstance() const
Definition DeclCXX.h:2172
Declaration of a class template.
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Declaration of a C++20 concept.
const TypeClass * getTypePtr() const
Definition TypeLoc.h:433
Kind
Lists the kind of concrete classes of Decl.
Definition DeclBase.h:89
ArrayRef< TemplateParameterList * > getTemplateParameterLists() const
Definition Decl.h:862
Represents a function declaration or definition.
Definition Decl.h:2027
QualType getReturnType() const
Definition Decl.h:2850
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2779
bool isVariadic() const
Whether this function is variadic.
Definition Decl.cpp:3111
const TemplateParameterList * getTemplateSpecializationParameters() const
Returns the template parameter list for an explicit specialization.
Definition Decl.cpp:4333
Declaration of a template function.
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
Wrapper for source info for functions.
Definition TypeLoc.h:1644
ArrayRef< ParmVarDecl * > getParams() const
Definition TypeLoc.h:1707
TypeLoc getReturnLoc() const
Definition TypeLoc.h:1725
Wrapper for source info for member pointers.
Definition TypeLoc.h:1544
This represents a decl that may have a name.
Definition Decl.h:274
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
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:1513
Wrapper of type source information for a type with non-trivial direct qualifiers.
Definition TypeLoc.h:300
Location wrapper for a TemplateArgument.
const TemplateArgument & getArgument() const
TypeSourceInfo * getTypeSourceInfo() const
@ Type
The template argument is a type.
ArgKind getKind() const
Return the kind of stored template argument.
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Stores a list of template parameters for a TemplateDecl and its derived classes.
NamedDecl * getParam(unsigned Idx)
Represents the declaration of a typedef-name via a C++11 alias-declaration.
Definition Decl.h:3707
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:349
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:1437
A container of type source information.
Definition TypeBase.h:8418
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition TypeLoc.h:267
Represents a variable declaration or definition.
Definition Decl.h:932
Declaration of a variable template.
VarDecl * getTemplatedDecl() const
Get the underlying variable declarations of the template.
Any part of the comment.
Definition Comment.h:66
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:1097
const DeclInfo * getDeclInfo() const LLVM_READONLY
Definition Comment.h:1129
child_iterator child_begin() const
Definition Comment.h:592
child_iterator child_end() const
Definition Comment.h:596
unsigned getParamIndex() const LLVM_READONLY
Definition Comment.h:791
static const char * getDirectionAsString(ParamCommandPassDirection D)
Definition Comment.cpp:189
StringRef getParamName(const FullComment *FC) const
Definition Comment.cpp:380
bool isParamIndexValid() const LLVM_READONLY
Definition Comment.h:778
bool isVarArgParam() const LLVM_READONLY
Definition Comment.h:782
bool isPositionValid() const LLVM_READONLY
Definition Comment.h:845
StringRef getParamName(const FullComment *FC) const
Definition Comment.cpp:387
unsigned getIndex(unsigned Depth) const
Definition Comment.h:854
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
@ Concept
The name was classified as a concept name.
Definition Sema.h:591
U cast(CodeGen::Address addr)
Definition Address.h:327
unsigned TemplateKind
Is CommentDecl a template declaration.
Definition Comment.h:1061
unsigned IsClassMethod
Is CommentDecl a static member function of C++ class or class method of ObjC class.
Definition Comment.h:1077
@ FunctionKind
Something that we consider a "function":
Definition Comment.h:1016
@ EnumKind
An enumeration or scoped enumeration.
Definition Comment.h:1038
@ OtherKind
Everything else not explicitly mentioned below.
Definition Comment.h:1006
@ NamespaceKind
A C++ namespace.
Definition Comment.h:1031
@ VariableKind
Something that we consider a "variable":
Definition Comment.h:1028
@ ClassKind
Something that we consider a "class":
Definition Comment.h:1022
@ TypedefKind
A C++ typedef-name (a 'typedef' decl specifier or alias-declaration), see TypedefNameDecl.
Definition Comment.h:1035
unsigned Kind
Simplified kind of CommentDecl, see DeclKind enum.
Definition Comment.h:1057
unsigned IsObjCMethod
Is CommentDecl an ObjCMethodDecl.
Definition Comment.h:1065
const TemplateParameterList * TemplateParameters
Template parameters that can be referenced by \tparam if CommentDecl is a template (IsTemplateDecl or...
Definition Comment.h:1000
ArrayRef< const ParmVarDecl * > ParamVars
Parameters that can be referenced by \param if CommentDecl is something that we consider a "function"...
Definition Comment.h:991
unsigned IsInstanceMethod
Is CommentDecl a non-static member function of C++ class or instance method of ObjC class.
Definition Comment.h:1071
bool involvesFunctionType() const
Definition Comment.h:1093
unsigned IsFilled
If false, only CommentDecl is valid.
Definition Comment.h:1053
const Decl * CommentDecl
Declaration the comment is actually attached to (in the source).
Definition Comment.h:977
unsigned IsVariadic
Is CommentDecl something we consider a "function" that's variadic.
Definition Comment.h:1081
const Decl * CurrentDecl
CurrentDecl is the declaration with which the FullComment is associated.
Definition Comment.h:987
QualType ReturnType
Function return type if CommentDecl is something that we consider a "function".
Definition Comment.h:995