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 qualified types.
120 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
121 return QualifiedTL.getUnqualifiedLoc();
122 // Look through pointer types.
123 if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
124 return PointerTL.getPointeeLoc().getUnqualifiedLoc();
125 // Look through reference types.
126 if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
127 return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
128 // Look through adjusted types.
129 if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
130 return ATL.getOriginalLoc();
131 if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
132 return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
133 if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
134 return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
135 if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
136 return ETL.getNamedTypeLoc();
141 static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
143 while (PrevTL != TL) {
145 TL = lookThroughTypedefOrTypeAliasLocs(TL);
148 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
153 if (TemplateSpecializationTypeLoc STL =
154 TL.getAs<TemplateSpecializationTypeLoc>()) {
155 // If we have a typedef to a template specialization with exactly one
156 // template argument of a function type, this looks like std::function,
157 // boost::function, or other function wrapper. Treat these typedefs as
159 if (STL.getNumArgs() != 1)
161 TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
162 if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
164 TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
165 TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
166 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
175 const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
177 case ParamCommandComment::In:
179 case ParamCommandComment::Out:
181 case ParamCommandComment::InOut:
184 llvm_unreachable("unknown PassDirection");
187 void DeclInfo::fill() {
192 TemplateKind = NotTemplate;
193 IsObjCMethod = false;
194 IsInstanceMethod = false;
195 IsClassMethod = false;
197 TemplateParameters = nullptr;
200 // If there is no declaration, the defaults is our only guess.
204 CurrentDecl = CommentDecl;
206 Decl::Kind K = CommentDecl->getKind();
209 // Defaults are should be good for declarations we don't handle explicitly.
212 case Decl::CXXMethod:
213 case Decl::CXXConstructor:
214 case Decl::CXXDestructor:
215 case Decl::CXXConversion: {
216 const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
218 ParamVars = FD->parameters();
219 ReturnType = FD->getReturnType();
220 unsigned NumLists = FD->getNumTemplateParameterLists();
222 TemplateKind = TemplateSpecialization;
224 FD->getTemplateParameterList(NumLists - 1);
227 if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
228 K == Decl::CXXDestructor || K == Decl::CXXConversion) {
229 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
230 IsInstanceMethod = MD->isInstance();
231 IsClassMethod = !IsInstanceMethod;
235 case Decl::ObjCMethod: {
236 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
238 ParamVars = MD->parameters();
239 ReturnType = MD->getReturnType();
241 IsInstanceMethod = MD->isInstanceMethod();
242 IsClassMethod = !IsInstanceMethod;
245 case Decl::FunctionTemplate: {
246 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
248 TemplateKind = Template;
249 const FunctionDecl *FD = FTD->getTemplatedDecl();
250 ParamVars = FD->parameters();
251 ReturnType = FD->getReturnType();
252 TemplateParameters = FTD->getTemplateParameters();
255 case Decl::ClassTemplate: {
256 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
258 TemplateKind = Template;
259 TemplateParameters = CTD->getTemplateParameters();
262 case Decl::ClassTemplatePartialSpecialization: {
263 const ClassTemplatePartialSpecializationDecl *CTPSD =
264 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
266 TemplateKind = TemplatePartialSpecialization;
267 TemplateParameters = CTPSD->getTemplateParameters();
270 case Decl::ClassTemplateSpecialization:
272 TemplateKind = TemplateSpecialization;
275 case Decl::CXXRecord:
280 case Decl::EnumConstant:
282 case Decl::ObjCAtDefsField:
285 case Decl::Namespace:
286 Kind = NamespaceKind;
288 case Decl::TypeAlias:
289 case Decl::Typedef: {
291 // If this is a typedef / using to something we consider a function, extract
292 // arguments and return type.
293 const TypeSourceInfo *TSI =
295 ? cast<TypedefDecl>(CommentDecl)->getTypeSourceInfo()
296 : cast<TypeAliasDecl>(CommentDecl)->getTypeSourceInfo();
299 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
301 if (getFunctionTypeLoc(TL, FTL)) {
303 ParamVars = FTL.getParams();
304 ReturnType = FTL.getReturnLoc().getType();
308 case Decl::TypeAliasTemplate: {
309 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
311 TemplateKind = Template;
312 TemplateParameters = TAT->getTemplateParameters();
313 TypeAliasDecl *TAD = TAT->getTemplatedDecl();
317 const TypeSourceInfo *TSI = TAD->getTypeSourceInfo();
320 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
322 if (getFunctionTypeLoc(TL, FTL)) {
324 ParamVars = FTL.getParams();
325 ReturnType = FTL.getReturnLoc().getType();
337 StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
338 assert(isParamIndexValid());
341 return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
344 StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
345 assert(isPositionValid());
346 const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
347 for (unsigned i = 0, e = getDepth(); i != e; ++i) {
349 return TPL->getParam(getIndex(i))->getName();
350 const NamedDecl *Param = TPL->getParam(getIndex(i));
351 if (const TemplateTemplateParmDecl *TTP =
352 dyn_cast<TemplateTemplateParmDecl>(Param))
353 TPL = TTP->getTemplateParameters();
358 } // end namespace comments
359 } // end namespace clang