clang  6.0.0svn
Comment.cpp
Go to the documentation of this file.
1 //===--- Comment.cpp - Comment AST node implementation --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
17 
18 namespace clang {
19 namespace comments {
20 
21 const char *Comment::getCommentKindName() const {
22  switch (getCommentKind()) {
23  case NoCommentKind: return "NoCommentKind";
24 #define ABSTRACT_COMMENT(COMMENT)
25 #define COMMENT(CLASS, PARENT) \
26  case CLASS##Kind: \
27  return #CLASS;
28 #include "clang/AST/CommentNodes.inc"
29 #undef COMMENT
30 #undef ABSTRACT_COMMENT
31  }
32  llvm_unreachable("Unknown comment kind!");
33 }
34 
35 namespace {
36 struct good {};
37 struct bad {};
38 
39 template <typename T>
40 good implements_child_begin_end(Comment::child_iterator (T::*)() const) {
41  return good();
42 }
43 
44 LLVM_ATTRIBUTE_UNUSED
45 static inline bad implements_child_begin_end(
46  Comment::child_iterator (Comment::*)() const) {
47  return bad();
48 }
49 
50 #define ASSERT_IMPLEMENTS_child_begin(function) \
51  (void) good(implements_child_begin_end(function))
52 
53 LLVM_ATTRIBUTE_UNUSED
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"
60 #undef COMMENT
61 #undef ABSTRACT_COMMENT
62 }
63 
64 #undef ASSERT_IMPLEMENTS_child_begin
65 
66 } // end unnamed namespace
67 
69  switch (getCommentKind()) {
70  case NoCommentKind: llvm_unreachable("comment without a kind");
71 #define ABSTRACT_COMMENT(COMMENT)
72 #define COMMENT(CLASS, PARENT) \
73  case CLASS##Kind: \
74  return static_cast<const CLASS *>(this)->child_begin();
75 #include "clang/AST/CommentNodes.inc"
76 #undef COMMENT
77 #undef ABSTRACT_COMMENT
78  }
79  llvm_unreachable("Unknown comment kind!");
80 }
81 
83  switch (getCommentKind()) {
84  case NoCommentKind: llvm_unreachable("comment without a kind");
85 #define ABSTRACT_COMMENT(COMMENT)
86 #define COMMENT(CLASS, PARENT) \
87  case CLASS##Kind: \
88  return static_cast<const CLASS *>(this)->child_end();
89 #include "clang/AST/CommentNodes.inc"
90 #undef COMMENT
91 #undef ABSTRACT_COMMENT
92  }
93  llvm_unreachable("Unknown comment kind!");
94 }
95 
96 bool TextComment::isWhitespaceNoCache() const {
97  for (StringRef::const_iterator I = Text.begin(), E = Text.end();
98  I != E; ++I) {
99  if (!clang::isWhitespace(*I))
100  return false;
101  }
102  return true;
103 }
104 
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())
109  return false;
110  } else
111  return false;
112  }
113  return true;
114 }
115 
117  TypeLoc TL = SrcTL.IgnoreParens();
118 
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();
140 
141  return TL;
142 }
143 
144 static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
145  TypeLoc PrevTL;
146  while (PrevTL != TL) {
147  PrevTL = TL;
149  }
150 
151  if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
152  ResFTL = FTL;
153  return true;
154  }
155 
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
161  // functions.
162  if (STL.getNumArgs() != 1)
163  return false;
164  TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
165  if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
166  return false;
167  TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
168  TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
169  if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
170  ResFTL = FTL;
171  return true;
172  }
173  }
174 
175  return false;
176 }
177 
179  switch (D) {
181  return "[in]";
183  return "[out]";
185  return "[in,out]";
186  }
187  llvm_unreachable("unknown PassDirection");
188 }
189 
191  assert(!IsFilled);
192 
193  // Set defaults.
194  Kind = OtherKind;
195  TemplateKind = NotTemplate;
196  IsObjCMethod = false;
197  IsInstanceMethod = false;
198  IsClassMethod = false;
199  ParamVars = None;
200  TemplateParameters = nullptr;
201 
202  if (!CommentDecl) {
203  // If there is no declaration, the defaults is our only guess.
204  IsFilled = true;
205  return;
206  }
207  CurrentDecl = CommentDecl;
208 
209  Decl::Kind K = CommentDecl->getKind();
210  switch (K) {
211  default:
212  // Defaults are should be good for declarations we don't handle explicitly.
213  break;
214  case Decl::Function:
215  case Decl::CXXMethod:
216  case Decl::CXXConstructor:
217  case Decl::CXXDestructor:
218  case Decl::CXXConversion: {
219  const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
220  Kind = FunctionKind;
221  ParamVars = FD->parameters();
222  ReturnType = FD->getReturnType();
223  unsigned NumLists = FD->getNumTemplateParameterLists();
224  if (NumLists != 0) {
225  TemplateKind = TemplateSpecialization;
226  TemplateParameters =
227  FD->getTemplateParameterList(NumLists - 1);
228  }
229 
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;
235  }
236  break;
237  }
238  case Decl::ObjCMethod: {
239  const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
240  Kind = FunctionKind;
241  ParamVars = MD->parameters();
242  ReturnType = MD->getReturnType();
243  IsObjCMethod = true;
244  IsInstanceMethod = MD->isInstanceMethod();
245  IsClassMethod = !IsInstanceMethod;
246  break;
247  }
248  case Decl::FunctionTemplate: {
249  const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
250  Kind = FunctionKind;
251  TemplateKind = Template;
252  const FunctionDecl *FD = FTD->getTemplatedDecl();
253  ParamVars = FD->parameters();
254  ReturnType = FD->getReturnType();
255  TemplateParameters = FTD->getTemplateParameters();
256  break;
257  }
258  case Decl::ClassTemplate: {
259  const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
260  Kind = ClassKind;
261  TemplateKind = Template;
262  TemplateParameters = CTD->getTemplateParameters();
263  break;
264  }
265  case Decl::ClassTemplatePartialSpecialization: {
267  cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
268  Kind = ClassKind;
269  TemplateKind = TemplatePartialSpecialization;
270  TemplateParameters = CTPSD->getTemplateParameters();
271  break;
272  }
273  case Decl::ClassTemplateSpecialization:
274  Kind = ClassKind;
275  TemplateKind = TemplateSpecialization;
276  break;
277  case Decl::Record:
278  case Decl::CXXRecord:
279  Kind = ClassKind;
280  break;
281  case Decl::Var:
282  case Decl::Field:
283  case Decl::EnumConstant:
284  case Decl::ObjCIvar:
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();
292  else
293  TSI = nullptr;
294  if (TSI) {
295  TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
296  FunctionTypeLoc FTL;
297  if (getFunctionTypeLoc(TL, FTL)) {
298  ParamVars = FTL.getParams();
299  ReturnType = FTL.getReturnLoc().getType();
300  }
301  }
302  Kind = VariableKind;
303  break;
304  }
305  case Decl::Namespace:
306  Kind = NamespaceKind;
307  break;
308  case Decl::TypeAlias:
309  case Decl::Typedef: {
310  Kind = TypedefKind;
311  // If this is a typedef / using to something we consider a function, extract
312  // arguments and return type.
313  const TypeSourceInfo *TSI =
314  K == Decl::Typedef
315  ? cast<TypedefDecl>(CommentDecl)->getTypeSourceInfo()
316  : cast<TypeAliasDecl>(CommentDecl)->getTypeSourceInfo();
317  if (!TSI)
318  break;
319  TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
320  FunctionTypeLoc FTL;
321  if (getFunctionTypeLoc(TL, FTL)) {
322  Kind = FunctionKind;
323  ParamVars = FTL.getParams();
324  ReturnType = FTL.getReturnLoc().getType();
325  }
326  break;
327  }
328  case Decl::TypeAliasTemplate: {
329  const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
330  Kind = TypedefKind;
331  TemplateKind = Template;
332  TemplateParameters = TAT->getTemplateParameters();
333  TypeAliasDecl *TAD = TAT->getTemplatedDecl();
334  if (!TAD)
335  break;
336 
337  const TypeSourceInfo *TSI = TAD->getTypeSourceInfo();
338  if (!TSI)
339  break;
340  TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
341  FunctionTypeLoc FTL;
342  if (getFunctionTypeLoc(TL, FTL)) {
343  Kind = FunctionKind;
344  ParamVars = FTL.getParams();
345  ReturnType = FTL.getReturnLoc().getType();
346  }
347  break;
348  }
349  case Decl::Enum:
350  Kind = EnumKind;
351  break;
352  }
353 
354  IsFilled = true;
355 }
356 
357 StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
358  assert(isParamIndexValid());
359  if (isVarArgParam())
360  return "...";
361  return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
362 }
363 
365  assert(isPositionValid());
367  for (unsigned i = 0, e = getDepth(); i != e; ++i) {
368  if (i == e-1)
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();
374  }
375  return "";
376 }
377 
378 } // end namespace comments
379 } // end namespace clang
380 
Defines the clang::ASTContext interface.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1631
static LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: &#39; &#39;, &#39;\t&#39;, &#39;\f&#39;, &#39;\v&#39;, &#39;\n&#39;, &#39;\r&#39;.
Definition: CharInfo.h:88
Defines the C++ template declaration subclasses.
NamedDecl * getParam(unsigned Idx)
Definition: DeclTemplate.h:112
StringRef getParamName(const FullComment *FC) const
Definition: Comment.cpp:357
A container of type source information.
Definition: Decl.h:62
child_iterator child_begin() const
Definition: Comment.cpp:68
static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL)
Definition: Comment.cpp:144
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
QualType getReturnType() const
Definition: Decl.h:2122
Wrapper for source info for member pointers.
Definition: TypeLoc.h:1265
Wrapper of type source information for a type with non-trivial direct qualifiers. ...
Definition: TypeLoc.h:254
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:50
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:40
TypeSourceInfo * getTypeSourceInfo() const
Definition: TemplateBase.h:479
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2082
TypeAliasDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
Wrapper for source info for functions.
Definition: TypeLoc.h:1357
Any part of the comment.
Definition: Comment.h:53
bool isInstance() const
Definition: DeclCXX.h:1945
QualType getReturnType() const
Definition: DeclObjC.h:330
TypeAliasDecl - Represents the declaration of a typedef-name via a C++0x alias-declaration.
Definition: Decl.h:2820
child_iterator child_end() const
Definition: Comment.cpp:82
static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL)
Definition: Comment.cpp:116
TypeSourceInfo * getTypeSourceInfo() const
Definition: Decl.h:2752
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Definition: DeclTemplate.h:410
Type source information for an attributed type.
Definition: TypeLoc.h:827
const FunctionProtoType * T
UnqualTypeLoc getUnqualifiedLoc() const
Skips past any qualifiers, if this is qualified.
Definition: TypeLoc.h:304
T getAs() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition: TypeLoc.h:64
TemplateParameterList * getTemplateParameterList(unsigned index) const
Definition: Decl.h:720
const TemplateParameterList * TemplateParameters
Template parameters that can be referenced by \tparam if CommentDecl is a template (IsTemplateDecl or...
Definition: Comment.h:1012
CommentKind getCommentKind() const
Definition: Comment.h:204
Declaration of an alias template.
bool isInstanceMethod() const
Definition: DeclObjC.h:416
TemplateTemplateParmDecl - Declares a template template parameter, e.g., "T" in.
static const char * getDirectionAsString(PassDirection D)
Definition: Comment.cpp:178
TypeLoc getReturnLoc() const
Definition: TypeLoc.h:1434
Kind
TypeLoc IgnoreParens() const
Definition: TypeLoc.h:1158
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
Definition: DeclTemplate.h:998
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1918
Comment *const * child_iterator
Definition: Comment.h:228
ArrayRef< ParmVarDecl * > getParams() const
Definition: TypeLoc.h:1417
Dataflow Directional Tag Classes.
const TemplateArgument & getArgument() const
Definition: TemplateBase.h:471
QualType getType() const
Get the type for which this source info wrapper provides information.
Definition: TypeLoc.h:114
unsigned getNumTemplateParameterLists() const
Definition: Decl.h:717
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:428
The template argument is a type.
Definition: TemplateBase.h:48
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition: TypeLoc.h:222
ArgKind getKind() const
Return the kind of stored template argument.
Definition: TemplateBase.h:213
const char * getCommentKindName() const
Definition: Comment.cpp:21
Declaration of a class template.
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:237
Kind
Lists the kind of concrete classes of Decl.
Definition: DeclBase.h:84
const DeclInfo * getDeclInfo() const LLVM_READONLY
Definition: Comment.h:1129
StringRef Text
Definition: Format.cpp:1317
ArrayRef< const ParmVarDecl * > ParamVars
Parameters that can be referenced by \param if CommentDecl is something that we consider a "function"...
Definition: Comment.h:1003
NamedDecl - This represents a decl with a name.
Definition: Decl.h:213
StringRef getParamName(const FullComment *FC) const
Definition: Comment.cpp:364
Wrapper for source info for pointers.
Definition: TypeLoc.h:1236
Wrapper for source info for block pointers.
Definition: TypeLoc.h:1249
Declaration of a template function.
Definition: DeclTemplate.h:939
A full comment attached to a declaration, contains block content.
Definition: Comment.h:1097
ArrayRef< ParmVarDecl * > parameters() const
Definition: DeclObjC.h:371