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<DeclInfo>::value,
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 for (StringRef::const_iterator I = Text.begin(), E = Text.end();
113 if (!clang::isWhitespace(*I))
119 bool ParagraphComment::isWhitespaceNoCache() const {
120 for (child_iterator I = child_begin(), E = child_end(); I != E; ++I) {
121 if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
122 if (!TC->isWhitespace())
130 static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) {
131 TypeLoc TL = SrcTL.IgnoreParens();
133 // Look through attribute types.
134 if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>())
135 return AttributeTL.getModifiedLoc();
136 // Look through qualified types.
137 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
138 return QualifiedTL.getUnqualifiedLoc();
139 // Look through pointer types.
140 if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
141 return PointerTL.getPointeeLoc().getUnqualifiedLoc();
142 // Look through reference types.
143 if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
144 return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
145 // Look through adjusted types.
146 if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
147 return ATL.getOriginalLoc();
148 if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
149 return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
150 if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
151 return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
152 if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
153 return ETL.getNamedTypeLoc();
158 static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
160 while (PrevTL != TL) {
162 TL = lookThroughTypedefOrTypeAliasLocs(TL);
165 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
170 if (TemplateSpecializationTypeLoc STL =
171 TL.getAs<TemplateSpecializationTypeLoc>()) {
172 // If we have a typedef to a template specialization with exactly one
173 // template argument of a function type, this looks like std::function,
174 // boost::function, or other function wrapper. Treat these typedefs as
176 if (STL.getNumArgs() != 1)
178 TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
179 if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
181 TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
182 TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
183 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
192 const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
194 case ParamCommandComment::In:
196 case ParamCommandComment::Out:
198 case ParamCommandComment::InOut:
201 llvm_unreachable("unknown PassDirection");
204 void DeclInfo::fill() {
209 TemplateKind = NotTemplate;
210 IsObjCMethod = false;
211 IsInstanceMethod = false;
212 IsClassMethod = false;
214 TemplateParameters = nullptr;
217 // If there is no declaration, the defaults is our only guess.
221 CurrentDecl = CommentDecl;
223 Decl::Kind K = CommentDecl->getKind();
226 // Defaults are should be good for declarations we don't handle explicitly.
229 case Decl::CXXMethod:
230 case Decl::CXXConstructor:
231 case Decl::CXXDestructor:
232 case Decl::CXXConversion: {
233 const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
235 ParamVars = FD->parameters();
236 ReturnType = FD->getReturnType();
237 unsigned NumLists = FD->getNumTemplateParameterLists();
239 TemplateKind = TemplateSpecialization;
241 FD->getTemplateParameterList(NumLists - 1);
244 if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
245 K == Decl::CXXDestructor || K == Decl::CXXConversion) {
246 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
247 IsInstanceMethod = MD->isInstance();
248 IsClassMethod = !IsInstanceMethod;
252 case Decl::ObjCMethod: {
253 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
255 ParamVars = MD->parameters();
256 ReturnType = MD->getReturnType();
258 IsInstanceMethod = MD->isInstanceMethod();
259 IsClassMethod = !IsInstanceMethod;
262 case Decl::FunctionTemplate: {
263 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
265 TemplateKind = Template;
266 const FunctionDecl *FD = FTD->getTemplatedDecl();
267 ParamVars = FD->parameters();
268 ReturnType = FD->getReturnType();
269 TemplateParameters = FTD->getTemplateParameters();
272 case Decl::ClassTemplate: {
273 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
275 TemplateKind = Template;
276 TemplateParameters = CTD->getTemplateParameters();
279 case Decl::ClassTemplatePartialSpecialization: {
280 const ClassTemplatePartialSpecializationDecl *CTPSD =
281 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
283 TemplateKind = TemplatePartialSpecialization;
284 TemplateParameters = CTPSD->getTemplateParameters();
287 case Decl::ClassTemplateSpecialization:
289 TemplateKind = TemplateSpecialization;
292 case Decl::CXXRecord:
297 case Decl::EnumConstant:
299 case Decl::ObjCAtDefsField:
300 case Decl::ObjCProperty: {
301 const TypeSourceInfo *TSI;
302 if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
303 TSI = VD->getTypeSourceInfo();
304 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
305 TSI = PD->getTypeSourceInfo();
309 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
311 if (getFunctionTypeLoc(TL, FTL)) {
312 ParamVars = FTL.getParams();
313 ReturnType = FTL.getReturnLoc().getType();
319 case Decl::Namespace:
320 Kind = NamespaceKind;
322 case Decl::TypeAlias:
323 case Decl::Typedef: {
325 // If this is a typedef / using to something we consider a function, extract
326 // arguments and return type.
327 const TypeSourceInfo *TSI =
329 ? cast<TypedefDecl>(CommentDecl)->getTypeSourceInfo()
330 : cast<TypeAliasDecl>(CommentDecl)->getTypeSourceInfo();
333 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
335 if (getFunctionTypeLoc(TL, FTL)) {
337 ParamVars = FTL.getParams();
338 ReturnType = FTL.getReturnLoc().getType();
342 case Decl::TypeAliasTemplate: {
343 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
345 TemplateKind = Template;
346 TemplateParameters = TAT->getTemplateParameters();
347 TypeAliasDecl *TAD = TAT->getTemplatedDecl();
351 const TypeSourceInfo *TSI = TAD->getTypeSourceInfo();
354 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
356 if (getFunctionTypeLoc(TL, FTL)) {
358 ParamVars = FTL.getParams();
359 ReturnType = FTL.getReturnLoc().getType();
371 StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
372 assert(isParamIndexValid());
375 return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
378 StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
379 assert(isPositionValid());
380 const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
381 for (unsigned i = 0, e = getDepth(); i != e; ++i) {
383 return TPL->getParam(getIndex(i))->getName();
384 const NamedDecl *Param = TPL->getParam(getIndex(i));
385 if (const TemplateTemplateParmDecl *TTP =
386 dyn_cast<TemplateTemplateParmDecl>(Param))
387 TPL = TTP->getTemplateParameters();
392 } // end namespace comments
393 } // end namespace clang