1 //===--- Comment.cpp - Comment AST node implementation --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/Comment.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/Decl.h"
13 #include "clang/AST/DeclObjC.h"
14 #include "clang/AST/DeclTemplate.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "llvm/Support/ErrorHandling.h"
21 const char *Comment::getCommentKindName() const {
22 switch (getCommentKind()) {
23 case NoCommentKind: return "NoCommentKind";
24 #define ABSTRACT_COMMENT(COMMENT)
25 #define COMMENT(CLASS, PARENT) \
28 #include "clang/AST/CommentNodes.inc"
30 #undef ABSTRACT_COMMENT
32 llvm_unreachable("Unknown comment kind!");
40 good implements_child_begin_end(Comment::child_iterator (T::*)() const) {
45 static inline bad implements_child_begin_end(
46 Comment::child_iterator (Comment::*)() const) {
50 #define ASSERT_IMPLEMENTS_child_begin(function) \
51 (void) good(implements_child_begin_end(function))
54 static inline void CheckCommentASTNodes() {
55 #define ABSTRACT_COMMENT(COMMENT)
56 #define COMMENT(CLASS, PARENT) \
57 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \
58 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end);
59 #include "clang/AST/CommentNodes.inc"
61 #undef ABSTRACT_COMMENT
64 #undef ASSERT_IMPLEMENTS_child_begin
66 } // end unnamed namespace
68 Comment::child_iterator Comment::child_begin() const {
69 switch (getCommentKind()) {
70 case NoCommentKind: llvm_unreachable("comment without a kind");
71 #define ABSTRACT_COMMENT(COMMENT)
72 #define COMMENT(CLASS, PARENT) \
74 return static_cast<const CLASS *>(this)->child_begin();
75 #include "clang/AST/CommentNodes.inc"
77 #undef ABSTRACT_COMMENT
79 llvm_unreachable("Unknown comment kind!");
82 Comment::child_iterator Comment::child_end() 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_end();
89 #include "clang/AST/CommentNodes.inc"
91 #undef ABSTRACT_COMMENT
93 llvm_unreachable("Unknown comment kind!");
96 bool TextComment::isWhitespaceNoCache() const {
97 for (StringRef::const_iterator I = Text.begin(), E = Text.end();
99 if (!clang::isWhitespace(*I))
105 bool ParagraphComment::isWhitespaceNoCache() const {
106 for (child_iterator I = child_begin(), E = child_end(); I != E; ++I) {
107 if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
108 if (!TC->isWhitespace())
116 static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) {
117 TypeLoc TL = SrcTL.IgnoreParens();
119 // Look through attribute types.
120 if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>())
121 return AttributeTL.getModifiedLoc();
122 // Look through qualified types.
123 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
124 return QualifiedTL.getUnqualifiedLoc();
125 // Look through pointer types.
126 if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
127 return PointerTL.getPointeeLoc().getUnqualifiedLoc();
128 // Look through reference types.
129 if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
130 return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
131 // Look through adjusted types.
132 if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
133 return ATL.getOriginalLoc();
134 if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
135 return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
136 if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
137 return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
138 if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
139 return ETL.getNamedTypeLoc();
144 static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
146 while (PrevTL != TL) {
148 TL = lookThroughTypedefOrTypeAliasLocs(TL);
151 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
156 if (TemplateSpecializationTypeLoc STL =
157 TL.getAs<TemplateSpecializationTypeLoc>()) {
158 // If we have a typedef to a template specialization with exactly one
159 // template argument of a function type, this looks like std::function,
160 // boost::function, or other function wrapper. Treat these typedefs as
162 if (STL.getNumArgs() != 1)
164 TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
165 if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
167 TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
168 TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
169 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
178 const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
180 case ParamCommandComment::In:
182 case ParamCommandComment::Out:
184 case ParamCommandComment::InOut:
187 llvm_unreachable("unknown PassDirection");
190 void DeclInfo::fill() {
195 TemplateKind = NotTemplate;
196 IsObjCMethod = false;
197 IsInstanceMethod = false;
198 IsClassMethod = false;
200 TemplateParameters = nullptr;
203 // If there is no declaration, the defaults is our only guess.
207 CurrentDecl = CommentDecl;
209 Decl::Kind K = CommentDecl->getKind();
212 // Defaults are should be good for declarations we don't handle explicitly.
215 case Decl::CXXMethod:
216 case Decl::CXXConstructor:
217 case Decl::CXXDestructor:
218 case Decl::CXXConversion: {
219 const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
221 ParamVars = FD->parameters();
222 ReturnType = FD->getReturnType();
223 unsigned NumLists = FD->getNumTemplateParameterLists();
225 TemplateKind = TemplateSpecialization;
227 FD->getTemplateParameterList(NumLists - 1);
230 if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
231 K == Decl::CXXDestructor || K == Decl::CXXConversion) {
232 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
233 IsInstanceMethod = MD->isInstance();
234 IsClassMethod = !IsInstanceMethod;
238 case Decl::ObjCMethod: {
239 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
241 ParamVars = MD->parameters();
242 ReturnType = MD->getReturnType();
244 IsInstanceMethod = MD->isInstanceMethod();
245 IsClassMethod = !IsInstanceMethod;
248 case Decl::FunctionTemplate: {
249 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
251 TemplateKind = Template;
252 const FunctionDecl *FD = FTD->getTemplatedDecl();
253 ParamVars = FD->parameters();
254 ReturnType = FD->getReturnType();
255 TemplateParameters = FTD->getTemplateParameters();
258 case Decl::ClassTemplate: {
259 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
261 TemplateKind = Template;
262 TemplateParameters = CTD->getTemplateParameters();
265 case Decl::ClassTemplatePartialSpecialization: {
266 const ClassTemplatePartialSpecializationDecl *CTPSD =
267 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
269 TemplateKind = TemplatePartialSpecialization;
270 TemplateParameters = CTPSD->getTemplateParameters();
273 case Decl::ClassTemplateSpecialization:
275 TemplateKind = TemplateSpecialization;
278 case Decl::CXXRecord:
283 case Decl::EnumConstant:
285 case Decl::ObjCAtDefsField:
286 case Decl::ObjCProperty: {
287 const TypeSourceInfo *TSI;
288 if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
289 TSI = VD->getTypeSourceInfo();
290 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
291 TSI = PD->getTypeSourceInfo();
295 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
297 if (getFunctionTypeLoc(TL, FTL)) {
298 ParamVars = FTL.getParams();
299 ReturnType = FTL.getReturnLoc().getType();
305 case Decl::Namespace:
306 Kind = NamespaceKind;
308 case Decl::TypeAlias:
309 case Decl::Typedef: {
311 // If this is a typedef / using to something we consider a function, extract
312 // arguments and return type.
313 const TypeSourceInfo *TSI =
315 ? cast<TypedefDecl>(CommentDecl)->getTypeSourceInfo()
316 : cast<TypeAliasDecl>(CommentDecl)->getTypeSourceInfo();
319 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
321 if (getFunctionTypeLoc(TL, FTL)) {
323 ParamVars = FTL.getParams();
324 ReturnType = FTL.getReturnLoc().getType();
328 case Decl::TypeAliasTemplate: {
329 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
331 TemplateKind = Template;
332 TemplateParameters = TAT->getTemplateParameters();
333 TypeAliasDecl *TAD = TAT->getTemplatedDecl();
337 const TypeSourceInfo *TSI = TAD->getTypeSourceInfo();
340 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
342 if (getFunctionTypeLoc(TL, FTL)) {
344 ParamVars = FTL.getParams();
345 ReturnType = FTL.getReturnLoc().getType();
357 StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
358 assert(isParamIndexValid());
361 return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
364 StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
365 assert(isPositionValid());
366 const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
367 for (unsigned i = 0, e = getDepth(); i != e; ++i) {
369 return TPL->getParam(getIndex(i))->getName();
370 const NamedDecl *Param = TPL->getParam(getIndex(i));
371 if (const TemplateTemplateParmDecl *TTP =
372 dyn_cast<TemplateTemplateParmDecl>(Param))
373 TPL = TTP->getTemplateParameters();
378 } // end namespace comments
379 } // end namespace clang