Comment.cpp revision bf967be66ea8c51b66c61659c23240f762a56dbe
15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//===--- Comment.cpp - Comment AST node implementation --------------------===//
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// License. See LICENSE.TXT for details.
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//===----------------------------------------------------------------------===//
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "clang/AST/ASTContext.h"
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "clang/AST/Comment.h"
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "clang/AST/Decl.h"
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "clang/AST/DeclObjC.h"
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "clang/AST/DeclTemplate.h"
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/Support/ErrorHandling.h"
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "llvm/Support/raw_ostream.h"
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace clang {
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace comments {
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)const char *Comment::getCommentKindName() const {
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  switch (getCommentKind()) {
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  case NoCommentKind: return "NoCommentKind";
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define ABSTRACT_COMMENT(COMMENT)
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define COMMENT(CLASS, PARENT) \
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  case CLASS##Kind: \
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return #CLASS;
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "clang/AST/CommentNodes.inc"
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#undef COMMENT
3053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#undef ABSTRACT_COMMENT
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  }
32c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)  llvm_unreachable("Unknown comment kind!");
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)void Comment::dump() const {
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // It is important that Comment::dump() is defined in a different TU than
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // Comment::dump(raw_ostream, SourceManager).  If both functions were defined
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // in CommentDumper.cpp, that object file would be removed by linker because
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // none of its functions are referenced by other object files, despite the
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  // LLVM_ATTRIBUTE_USED.
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  dump(llvm::errs(), NULL, NULL);
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
43197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)void Comment::dump(const ASTContext &Context) const {
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  dump(llvm::errs(), &Context.getCommentCommandTraits(),
46197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch       &Context.getSourceManager());
47d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
48d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
49197021e6b966cfb06891637935ef33fff06433d1Ben Murdochnamespace {
509bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)struct good {};
51197021e6b966cfb06891637935ef33fff06433d1Ben Murdochstruct bad {};
529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
53d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)template <typename T>
54197021e6b966cfb06891637935ef33fff06433d1Ben Murdochgood implements_child_begin_end(Comment::child_iterator (T::*)() const) {
559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)  return good();
565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)static inline bad implements_child_begin_end(
59c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                      Comment::child_iterator (Comment::*)() const) {
60  return bad();
61}
62
63#define ASSERT_IMPLEMENTS_child_begin(function) \
64  (void) sizeof(good(implements_child_begin_end(function)))
65
66static 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"
72#undef COMMENT
73#undef ABSTRACT_COMMENT
74}
75
76#undef ASSERT_IMPLEMENTS_child_begin
77
78} // end unnamed namespace
79
80Comment::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) \
85  case CLASS##Kind: \
86    return static_cast<const CLASS *>(this)->child_begin();
87#include "clang/AST/CommentNodes.inc"
88#undef COMMENT
89#undef ABSTRACT_COMMENT
90  }
91  llvm_unreachable("Unknown comment kind!");
92}
93
94Comment::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) \
99  case CLASS##Kind: \
100    return static_cast<const CLASS *>(this)->child_end();
101#include "clang/AST/CommentNodes.inc"
102#undef COMMENT
103#undef ABSTRACT_COMMENT
104  }
105  llvm_unreachable("Unknown comment kind!");
106}
107
108bool TextComment::isWhitespaceNoCache() const {
109  for (StringRef::const_iterator I = Text.begin(), E = Text.end();
110       I != E; ++I) {
111    const char C = *I;
112    if (C != ' ' && C != '\n' && C != '\r' &&
113        C != '\t' && C != '\f' && C != '\v')
114      return false;
115  }
116  return true;
117}
118
119bool 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())
123        return false;
124    } else
125      return false;
126  }
127  return true;
128}
129
130const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
131  switch (D) {
132  case ParamCommandComment::In:
133    return "[in]";
134  case ParamCommandComment::Out:
135    return "[out]";
136  case ParamCommandComment::InOut:
137    return "[in,out]";
138  }
139  llvm_unreachable("unknown PassDirection");
140}
141
142void DeclInfo::fill() {
143  assert(!IsFilled);
144
145  // Set defaults.
146  Kind = OtherKind;
147  TemplateKind = NotTemplate;
148  IsObjCMethod = false;
149  IsInstanceMethod = false;
150  IsClassMethod = false;
151  ParamVars = ArrayRef<const ParmVarDecl *>();
152  TemplateParameters = NULL;
153
154  if (!CommentDecl) {
155    // If there is no declaration, the defaults is our only guess.
156    IsFilled = true;
157    return;
158  }
159
160  Decl::Kind K = CommentDecl->getKind();
161  switch (K) {
162  default:
163    // Defaults are should be good for declarations we don't handle explicitly.
164    break;
165  case Decl::Function:
166  case Decl::CXXMethod:
167  case Decl::CXXConstructor:
168  case Decl::CXXDestructor:
169  case Decl::CXXConversion: {
170    const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
171    Kind = FunctionKind;
172    ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
173                                              FD->getNumParams());
174    ResultType = FD->getResultType();
175    unsigned NumLists = FD->getNumTemplateParameterLists();
176    if (NumLists != 0) {
177      TemplateKind = TemplateSpecialization;
178      TemplateParameters =
179          FD->getTemplateParameterList(NumLists - 1);
180    }
181
182    if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
183        K == Decl::CXXDestructor || K == Decl::CXXConversion) {
184      const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
185      IsInstanceMethod = MD->isInstance();
186      IsClassMethod = !IsInstanceMethod;
187    }
188    break;
189  }
190  case Decl::ObjCMethod: {
191    const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
192    Kind = FunctionKind;
193    ParamVars = ArrayRef<const ParmVarDecl *>(MD->param_begin(),
194                                              MD->param_size());
195    ResultType = MD->getResultType();
196    IsObjCMethod = true;
197    IsInstanceMethod = MD->isInstanceMethod();
198    IsClassMethod = !IsInstanceMethod;
199    break;
200  }
201  case Decl::FunctionTemplate: {
202    const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
203    Kind = FunctionKind;
204    TemplateKind = Template;
205    const FunctionDecl *FD = FTD->getTemplatedDecl();
206    ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
207                                              FD->getNumParams());
208    ResultType = FD->getResultType();
209    TemplateParameters = FTD->getTemplateParameters();
210    break;
211  }
212  case Decl::ClassTemplate: {
213    const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
214    Kind = ClassKind;
215    TemplateKind = Template;
216    TemplateParameters = CTD->getTemplateParameters();
217    break;
218  }
219  case Decl::ClassTemplatePartialSpecialization: {
220    const ClassTemplatePartialSpecializationDecl *CTPSD =
221        cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
222    Kind = ClassKind;
223    TemplateKind = TemplatePartialSpecialization;
224    TemplateParameters = CTPSD->getTemplateParameters();
225    break;
226  }
227  case Decl::ClassTemplateSpecialization:
228    Kind = ClassKind;
229    TemplateKind = TemplateSpecialization;
230    break;
231  case Decl::Record:
232  case Decl::CXXRecord:
233    Kind = ClassKind;
234    break;
235  case Decl::Var:
236  case Decl::Field:
237  case Decl::EnumConstant:
238  case Decl::ObjCIvar:
239  case Decl::ObjCAtDefsField:
240    Kind = VariableKind;
241    break;
242  case Decl::Namespace:
243    Kind = NamespaceKind;
244    break;
245  case Decl::Typedef: {
246    Kind = TypedefKind;
247    // If this is a typedef to something we consider a function, extract
248    // arguments and return type.
249    const TypedefDecl *TD = cast<TypedefDecl>(CommentDecl);
250    const TypeSourceInfo *TSI = TD->getTypeSourceInfo();
251    if (!TSI)
252      break;
253    TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
254    while (true) {
255      TL = TL.IgnoreParens();
256      // Look through qualified types.
257      if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) {
258        TL = QualifiedTL->getUnqualifiedLoc();
259        continue;
260      }
261      // Look through pointer types.
262      if (PointerTypeLoc *PointerTL = dyn_cast<PointerTypeLoc>(&TL)) {
263        TL = PointerTL->getPointeeLoc().getUnqualifiedLoc();
264        continue;
265      }
266      if (BlockPointerTypeLoc *BlockPointerTL =
267              dyn_cast<BlockPointerTypeLoc>(&TL)) {
268        TL = BlockPointerTL->getPointeeLoc().getUnqualifiedLoc();
269        continue;
270      }
271      if (MemberPointerTypeLoc *MemberPointerTL =
272              dyn_cast<MemberPointerTypeLoc>(&TL)) {
273        TL = MemberPointerTL->getPointeeLoc().getUnqualifiedLoc();
274        continue;
275      }
276      // Is this a typedef for a function type?
277      if (FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
278        Kind = FunctionKind;
279        ArrayRef<ParmVarDecl *> Params = FTL->getParams();
280        ParamVars = ArrayRef<const ParmVarDecl *>(Params.data(),
281                                                  Params.size());
282        ResultType = FTL->getResultLoc().getType();
283        break;
284      }
285      break;
286    }
287    break;
288  }
289  case Decl::TypeAlias:
290    Kind = TypedefKind;
291    break;
292  case Decl::TypeAliasTemplate: {
293    const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
294    Kind = TypedefKind;
295    TemplateKind = Template;
296    TemplateParameters = TAT->getTemplateParameters();
297    break;
298  }
299  case Decl::Enum:
300    Kind = EnumKind;
301    break;
302  }
303
304  IsFilled = true;
305}
306
307} // end namespace comments
308} // end namespace clang
309
310