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/ASTContext.h"
11 #include "clang/AST/Comment.h"
12 #include "clang/AST/Decl.h"
13 #include "clang/AST/DeclObjC.h"
14 #include "clang/AST/DeclTemplate.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.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!");
35 void Comment::dump() const {
36 // It is important that Comment::dump() is defined in a different TU than
37 // Comment::dump(raw_ostream, SourceManager). If both functions were defined
38 // in CommentDumper.cpp, that object file would be removed by linker because
39 // none of its functions are referenced by other object files, despite the
40 // LLVM_ATTRIBUTE_USED.
41 dump(llvm::errs(), NULL, NULL);
44 void Comment::dump(const ASTContext &Context) const {
45 dump(llvm::errs(), &Context.getCommentCommandTraits(),
46 &Context.getSourceManager());
54 good implements_child_begin_end(Comment::child_iterator (T::*)() const) {
58 static inline bad implements_child_begin_end(
59 Comment::child_iterator (Comment::*)() const) {
63 #define ASSERT_IMPLEMENTS_child_begin(function) \
64 (void) sizeof(good(implements_child_begin_end(function)))
66 static inline void CheckCommentASTNodes() {
67 #define ABSTRACT_COMMENT(COMMENT)
68 #define COMMENT(CLASS, PARENT) \
69 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \
70 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end);
71 #include "clang/AST/CommentNodes.inc"
73 #undef ABSTRACT_COMMENT
76 #undef ASSERT_IMPLEMENTS_child_begin
78 } // end unnamed namespace
80 Comment::child_iterator Comment::child_begin() const {
81 switch (getCommentKind()) {
82 case NoCommentKind: llvm_unreachable("comment without a kind");
83 #define ABSTRACT_COMMENT(COMMENT)
84 #define COMMENT(CLASS, PARENT) \
86 return static_cast<const CLASS *>(this)->child_begin();
87 #include "clang/AST/CommentNodes.inc"
89 #undef ABSTRACT_COMMENT
91 llvm_unreachable("Unknown comment kind!");
94 Comment::child_iterator Comment::child_end() const {
95 switch (getCommentKind()) {
96 case NoCommentKind: llvm_unreachable("comment without a kind");
97 #define ABSTRACT_COMMENT(COMMENT)
98 #define COMMENT(CLASS, PARENT) \
100 return static_cast<const CLASS *>(this)->child_end();
101 #include "clang/AST/CommentNodes.inc"
103 #undef ABSTRACT_COMMENT
105 llvm_unreachable("Unknown comment kind!");
108 bool TextComment::isWhitespaceNoCache() const {
109 for (StringRef::const_iterator I = Text.begin(), E = Text.end();
112 if (C != ' ' && C != '\n' && C != '\r' &&
113 C != '\t' && C != '\f' && C != '\v')
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 const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
132 case ParamCommandComment::In:
134 case ParamCommandComment::Out:
136 case ParamCommandComment::InOut:
139 llvm_unreachable("unknown PassDirection");
142 void DeclInfo::fill() {
147 TemplateKind = NotTemplate;
148 IsObjCMethod = false;
149 IsInstanceMethod = false;
150 IsClassMethod = false;
151 ParamVars = ArrayRef<const ParmVarDecl *>();
152 TemplateParameters = NULL;
155 // If there is no declaration, the defaults is our only guess.
159 CurrentDecl = CommentDecl;
161 Decl::Kind K = CommentDecl->getKind();
164 // Defaults are should be good for declarations we don't handle explicitly.
167 case Decl::CXXMethod:
168 case Decl::CXXConstructor:
169 case Decl::CXXDestructor:
170 case Decl::CXXConversion: {
171 const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
173 ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
175 ResultType = FD->getResultType();
176 unsigned NumLists = FD->getNumTemplateParameterLists();
178 TemplateKind = TemplateSpecialization;
180 FD->getTemplateParameterList(NumLists - 1);
183 if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
184 K == Decl::CXXDestructor || K == Decl::CXXConversion) {
185 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
186 IsInstanceMethod = MD->isInstance();
187 IsClassMethod = !IsInstanceMethod;
191 case Decl::ObjCMethod: {
192 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
194 ParamVars = ArrayRef<const ParmVarDecl *>(MD->param_begin(),
196 ResultType = MD->getResultType();
198 IsInstanceMethod = MD->isInstanceMethod();
199 IsClassMethod = !IsInstanceMethod;
202 case Decl::FunctionTemplate: {
203 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
205 TemplateKind = Template;
206 const FunctionDecl *FD = FTD->getTemplatedDecl();
207 ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
209 ResultType = FD->getResultType();
210 TemplateParameters = FTD->getTemplateParameters();
213 case Decl::ClassTemplate: {
214 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
216 TemplateKind = Template;
217 TemplateParameters = CTD->getTemplateParameters();
220 case Decl::ClassTemplatePartialSpecialization: {
221 const ClassTemplatePartialSpecializationDecl *CTPSD =
222 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
224 TemplateKind = TemplatePartialSpecialization;
225 TemplateParameters = CTPSD->getTemplateParameters();
228 case Decl::ClassTemplateSpecialization:
230 TemplateKind = TemplateSpecialization;
233 case Decl::CXXRecord:
238 case Decl::EnumConstant:
240 case Decl::ObjCAtDefsField:
243 case Decl::Namespace:
244 Kind = NamespaceKind;
246 case Decl::Typedef: {
248 // If this is a typedef to something we consider a function, extract
249 // arguments and return type.
250 const TypedefDecl *TD = cast<TypedefDecl>(CommentDecl);
251 const TypeSourceInfo *TSI = TD->getTypeSourceInfo();
254 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
256 TL = TL.IgnoreParens();
257 // Look through qualified types.
258 if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) {
259 TL = QualifiedTL->getUnqualifiedLoc();
262 // Look through pointer types.
263 if (PointerTypeLoc *PointerTL = dyn_cast<PointerTypeLoc>(&TL)) {
264 TL = PointerTL->getPointeeLoc().getUnqualifiedLoc();
267 if (BlockPointerTypeLoc *BlockPointerTL =
268 dyn_cast<BlockPointerTypeLoc>(&TL)) {
269 TL = BlockPointerTL->getPointeeLoc().getUnqualifiedLoc();
272 if (MemberPointerTypeLoc *MemberPointerTL =
273 dyn_cast<MemberPointerTypeLoc>(&TL)) {
274 TL = MemberPointerTL->getPointeeLoc().getUnqualifiedLoc();
277 // Is this a typedef for a function type?
278 if (FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
280 ArrayRef<ParmVarDecl *> Params = FTL->getParams();
281 ParamVars = ArrayRef<const ParmVarDecl *>(Params.data(),
283 ResultType = FTL->getResultLoc().getType();
290 case Decl::TypeAlias:
293 case Decl::TypeAliasTemplate: {
294 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
296 TemplateKind = Template;
297 TemplateParameters = TAT->getTemplateParameters();
308 StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
309 assert(isParamIndexValid());
310 return FC->getThisDeclInfo()->ParamVars[getParamIndex()]->getName();
313 StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
314 assert(isPositionValid());
315 const TemplateParameterList *TPL = FC->getThisDeclInfo()->TemplateParameters;
316 for (unsigned i = 0, e = getDepth(); i != e; ++i) {
318 return TPL->getParam(getIndex(i))->getName();
319 const NamedDecl *Param = TPL->getParam(getIndex(i));
320 if (const TemplateTemplateParmDecl *TTP =
321 dyn_cast<TemplateTemplateParmDecl>(Param))
322 TPL = TTP->getTemplateParameters();
327 } // end namespace comments
328 } // end namespace clang