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 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) \ 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 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) \ 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 116 static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) { 117 TypeLoc TL = SrcTL.IgnoreParens(); 118 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(); 137 138 return TL; 139 } 140 141 static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) { 142 TypeLoc PrevTL; 143 while (PrevTL != TL) { 144 PrevTL = TL; 145 TL = lookThroughTypedefOrTypeAliasLocs(TL); 146 } 147 148 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { 149 ResFTL = FTL; 150 return true; 151 } 152 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 158 // functions. 159 if (STL.getNumArgs() != 1) 160 return false; 161 TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0); 162 if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type) 163 return false; 164 TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo(); 165 TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc(); 166 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { 167 ResFTL = FTL; 168 return true; 169 } 170 } 171 172 return false; 173 } 174 175 const char *ParamCommandComment::getDirectionAsString(PassDirection D) { 176 switch (D) { 177 case ParamCommandComment::In: 178 return "[in]"; 179 case ParamCommandComment::Out: 180 return "[out]"; 181 case ParamCommandComment::InOut: 182 return "[in,out]"; 183 } 184 llvm_unreachable("unknown PassDirection"); 185 } 186 187 void DeclInfo::fill() { 188 assert(!IsFilled); 189 190 // Set defaults. 191 Kind = OtherKind; 192 TemplateKind = NotTemplate; 193 IsObjCMethod = false; 194 IsInstanceMethod = false; 195 IsClassMethod = false; 196 ParamVars = None; 197 TemplateParameters = nullptr; 198 199 if (!CommentDecl) { 200 // If there is no declaration, the defaults is our only guess. 201 IsFilled = true; 202 return; 203 } 204 CurrentDecl = CommentDecl; 205 206 Decl::Kind K = CommentDecl->getKind(); 207 switch (K) { 208 default: 209 // Defaults are should be good for declarations we don't handle explicitly. 210 break; 211 case Decl::Function: 212 case Decl::CXXMethod: 213 case Decl::CXXConstructor: 214 case Decl::CXXDestructor: 215 case Decl::CXXConversion: { 216 const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl); 217 Kind = FunctionKind; 218 ParamVars = FD->parameters(); 219 ReturnType = FD->getReturnType(); 220 unsigned NumLists = FD->getNumTemplateParameterLists(); 221 if (NumLists != 0) { 222 TemplateKind = TemplateSpecialization; 223 TemplateParameters = 224 FD->getTemplateParameterList(NumLists - 1); 225 } 226 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; 232 } 233 break; 234 } 235 case Decl::ObjCMethod: { 236 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl); 237 Kind = FunctionKind; 238 ParamVars = MD->parameters(); 239 ReturnType = MD->getReturnType(); 240 IsObjCMethod = true; 241 IsInstanceMethod = MD->isInstanceMethod(); 242 IsClassMethod = !IsInstanceMethod; 243 break; 244 } 245 case Decl::FunctionTemplate: { 246 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl); 247 Kind = FunctionKind; 248 TemplateKind = Template; 249 const FunctionDecl *FD = FTD->getTemplatedDecl(); 250 ParamVars = FD->parameters(); 251 ReturnType = FD->getReturnType(); 252 TemplateParameters = FTD->getTemplateParameters(); 253 break; 254 } 255 case Decl::ClassTemplate: { 256 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl); 257 Kind = ClassKind; 258 TemplateKind = Template; 259 TemplateParameters = CTD->getTemplateParameters(); 260 break; 261 } 262 case Decl::ClassTemplatePartialSpecialization: { 263 const ClassTemplatePartialSpecializationDecl *CTPSD = 264 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl); 265 Kind = ClassKind; 266 TemplateKind = TemplatePartialSpecialization; 267 TemplateParameters = CTPSD->getTemplateParameters(); 268 break; 269 } 270 case Decl::ClassTemplateSpecialization: 271 Kind = ClassKind; 272 TemplateKind = TemplateSpecialization; 273 break; 274 case Decl::Record: 275 case Decl::CXXRecord: 276 Kind = ClassKind; 277 break; 278 case Decl::Var: 279 case Decl::Field: 280 case Decl::EnumConstant: 281 case Decl::ObjCIvar: 282 case Decl::ObjCAtDefsField: 283 Kind = VariableKind; 284 break; 285 case Decl::Namespace: 286 Kind = NamespaceKind; 287 break; 288 case Decl::TypeAlias: 289 case Decl::Typedef: { 290 Kind = TypedefKind; 291 // If this is a typedef / using to something we consider a function, extract 292 // arguments and return type. 293 const TypeSourceInfo *TSI = 294 K == Decl::Typedef 295 ? cast<TypedefDecl>(CommentDecl)->getTypeSourceInfo() 296 : cast<TypeAliasDecl>(CommentDecl)->getTypeSourceInfo(); 297 if (!TSI) 298 break; 299 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc(); 300 FunctionTypeLoc FTL; 301 if (getFunctionTypeLoc(TL, FTL)) { 302 Kind = FunctionKind; 303 ParamVars = FTL.getParams(); 304 ReturnType = FTL.getReturnLoc().getType(); 305 } 306 break; 307 } 308 case Decl::TypeAliasTemplate: { 309 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl); 310 Kind = TypedefKind; 311 TemplateKind = Template; 312 TemplateParameters = TAT->getTemplateParameters(); 313 TypeAliasDecl *TAD = TAT->getTemplatedDecl(); 314 if (!TAD) 315 break; 316 317 const TypeSourceInfo *TSI = TAD->getTypeSourceInfo(); 318 if (!TSI) 319 break; 320 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc(); 321 FunctionTypeLoc FTL; 322 if (getFunctionTypeLoc(TL, FTL)) { 323 Kind = FunctionKind; 324 ParamVars = FTL.getParams(); 325 ReturnType = FTL.getReturnLoc().getType(); 326 } 327 break; 328 } 329 case Decl::Enum: 330 Kind = EnumKind; 331 break; 332 } 333 334 IsFilled = true; 335 } 336 337 StringRef ParamCommandComment::getParamName(const FullComment *FC) const { 338 assert(isParamIndexValid()); 339 if (isVarArgParam()) 340 return "..."; 341 return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName(); 342 } 343 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) { 348 if (i == e-1) 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(); 354 } 355 return ""; 356 } 357 358 } // end namespace comments 359 } // end namespace clang 360 361