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"
20 const char *Comment::getCommentKindName() const {
21 switch (getCommentKind()) {
22 case NoCommentKind: return "NoCommentKind";
23 #define ABSTRACT_COMMENT(COMMENT)
24 #define COMMENT(CLASS, PARENT) \
27 #include "clang/AST/CommentNodes.inc"
29 #undef ABSTRACT_COMMENT
31 llvm_unreachable("Unknown comment kind!");
39 good implements_child_begin_end(Comment::child_iterator (T::*)() const) {
44 static inline bad implements_child_begin_end(
45 Comment::child_iterator (Comment::*)() const) {
49 #define ASSERT_IMPLEMENTS_child_begin(function) \
50 (void) good(implements_child_begin_end(function))
53 static inline void CheckCommentASTNodes() {
54 #define ABSTRACT_COMMENT(COMMENT)
55 #define COMMENT(CLASS, PARENT) \
56 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \
57 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end);
58 #include "clang/AST/CommentNodes.inc"
60 #undef ABSTRACT_COMMENT
63 #undef ASSERT_IMPLEMENTS_child_begin
65 } // end unnamed namespace
67 Comment::child_iterator Comment::child_begin() const {
68 switch (getCommentKind()) {
69 case NoCommentKind: llvm_unreachable("comment without a kind");
70 #define ABSTRACT_COMMENT(COMMENT)
71 #define COMMENT(CLASS, PARENT) \
73 return static_cast<const CLASS *>(this)->child_begin();
74 #include "clang/AST/CommentNodes.inc"
76 #undef ABSTRACT_COMMENT
78 llvm_unreachable("Unknown comment kind!");
81 Comment::child_iterator Comment::child_end() const {
82 switch (getCommentKind()) {
83 case NoCommentKind: llvm_unreachable("comment without a kind");
84 #define ABSTRACT_COMMENT(COMMENT)
85 #define COMMENT(CLASS, PARENT) \
87 return static_cast<const CLASS *>(this)->child_end();
88 #include "clang/AST/CommentNodes.inc"
90 #undef ABSTRACT_COMMENT
92 llvm_unreachable("Unknown comment kind!");
95 bool TextComment::isWhitespaceNoCache() const {
96 for (StringRef::const_iterator I = Text.begin(), E = Text.end();
98 if (!clang::isWhitespace(*I))
104 bool ParagraphComment::isWhitespaceNoCache() const {
105 for (child_iterator I = child_begin(), E = child_end(); I != E; ++I) {
106 if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
107 if (!TC->isWhitespace())
115 static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) {
116 TypeLoc TL = SrcTL.IgnoreParens();
118 // Look through attribute types.
119 if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>())
120 return AttributeTL.getModifiedLoc();
121 // Look through qualified types.
122 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
123 return QualifiedTL.getUnqualifiedLoc();
124 // Look through pointer types.
125 if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
126 return PointerTL.getPointeeLoc().getUnqualifiedLoc();
127 // Look through reference types.
128 if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
129 return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
130 // Look through adjusted types.
131 if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
132 return ATL.getOriginalLoc();
133 if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
134 return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
135 if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
136 return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
137 if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
138 return ETL.getNamedTypeLoc();
143 static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
145 while (PrevTL != TL) {
147 TL = lookThroughTypedefOrTypeAliasLocs(TL);
150 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
155 if (TemplateSpecializationTypeLoc STL =
156 TL.getAs<TemplateSpecializationTypeLoc>()) {
157 // If we have a typedef to a template specialization with exactly one
158 // template argument of a function type, this looks like std::function,
159 // boost::function, or other function wrapper. Treat these typedefs as
161 if (STL.getNumArgs() != 1)
163 TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
164 if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
166 TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
167 TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
168 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
177 const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
179 case ParamCommandComment::In:
181 case ParamCommandComment::Out:
183 case ParamCommandComment::InOut:
186 llvm_unreachable("unknown PassDirection");
189 void DeclInfo::fill() {
194 TemplateKind = NotTemplate;
195 IsObjCMethod = false;
196 IsInstanceMethod = false;
197 IsClassMethod = false;
199 TemplateParameters = nullptr;
202 // If there is no declaration, the defaults is our only guess.
206 CurrentDecl = CommentDecl;
208 Decl::Kind K = CommentDecl->getKind();
211 // Defaults are should be good for declarations we don't handle explicitly.
214 case Decl::CXXMethod:
215 case Decl::CXXConstructor:
216 case Decl::CXXDestructor:
217 case Decl::CXXConversion: {
218 const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
220 ParamVars = FD->parameters();
221 ReturnType = FD->getReturnType();
222 unsigned NumLists = FD->getNumTemplateParameterLists();
224 TemplateKind = TemplateSpecialization;
226 FD->getTemplateParameterList(NumLists - 1);
229 if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
230 K == Decl::CXXDestructor || K == Decl::CXXConversion) {
231 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
232 IsInstanceMethod = MD->isInstance();
233 IsClassMethod = !IsInstanceMethod;
237 case Decl::ObjCMethod: {
238 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
240 ParamVars = MD->parameters();
241 ReturnType = MD->getReturnType();
243 IsInstanceMethod = MD->isInstanceMethod();
244 IsClassMethod = !IsInstanceMethod;
247 case Decl::FunctionTemplate: {
248 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
250 TemplateKind = Template;
251 const FunctionDecl *FD = FTD->getTemplatedDecl();
252 ParamVars = FD->parameters();
253 ReturnType = FD->getReturnType();
254 TemplateParameters = FTD->getTemplateParameters();
257 case Decl::ClassTemplate: {
258 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
260 TemplateKind = Template;
261 TemplateParameters = CTD->getTemplateParameters();
264 case Decl::ClassTemplatePartialSpecialization: {
265 const ClassTemplatePartialSpecializationDecl *CTPSD =
266 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
268 TemplateKind = TemplatePartialSpecialization;
269 TemplateParameters = CTPSD->getTemplateParameters();
272 case Decl::ClassTemplateSpecialization:
274 TemplateKind = TemplateSpecialization;
277 case Decl::CXXRecord:
282 case Decl::EnumConstant:
284 case Decl::ObjCAtDefsField:
285 case Decl::ObjCProperty: {
286 const TypeSourceInfo *TSI;
287 if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
288 TSI = VD->getTypeSourceInfo();
289 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
290 TSI = PD->getTypeSourceInfo();
294 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
296 if (getFunctionTypeLoc(TL, FTL)) {
297 ParamVars = FTL.getParams();
298 ReturnType = FTL.getReturnLoc().getType();
304 case Decl::Namespace:
305 Kind = NamespaceKind;
307 case Decl::TypeAlias:
308 case Decl::Typedef: {
310 // If this is a typedef / using to something we consider a function, extract
311 // arguments and return type.
312 const TypeSourceInfo *TSI =
314 ? cast<TypedefDecl>(CommentDecl)->getTypeSourceInfo()
315 : cast<TypeAliasDecl>(CommentDecl)->getTypeSourceInfo();
318 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
320 if (getFunctionTypeLoc(TL, FTL)) {
322 ParamVars = FTL.getParams();
323 ReturnType = FTL.getReturnLoc().getType();
327 case Decl::TypeAliasTemplate: {
328 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
330 TemplateKind = Template;
331 TemplateParameters = TAT->getTemplateParameters();
332 TypeAliasDecl *TAD = TAT->getTemplatedDecl();
336 const TypeSourceInfo *TSI = TAD->getTypeSourceInfo();
339 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
341 if (getFunctionTypeLoc(TL, FTL)) {
343 ParamVars = FTL.getParams();
344 ReturnType = FTL.getReturnLoc().getType();
356 StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
357 assert(isParamIndexValid());
360 return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
363 StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
364 assert(isPositionValid());
365 const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
366 for (unsigned i = 0, e = getDepth(); i != e; ++i) {
368 return TPL->getParam(getIndex(i))->getName();
369 const NamedDecl *Param = TPL->getParam(getIndex(i));
370 if (const TemplateTemplateParmDecl *TTP =
371 dyn_cast<TemplateTemplateParmDecl>(Param))
372 TPL = TTP->getTemplateParameters();
377 } // end namespace comments
378 } // end namespace clang