1 //===--- Comment.cpp - Comment AST node implementation --------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/AST/Comment.h" 10 #include "clang/AST/ASTContext.h" 11 #include "clang/AST/Decl.h" 12 #include "clang/AST/DeclObjC.h" 13 #include "clang/AST/DeclTemplate.h" 14 #include "clang/Basic/CharInfo.h" 15 #include "llvm/Support/ErrorHandling.h" 16 #include <type_traits> 17 18 namespace clang { 19 namespace comments { 20 21 // Check that no comment class has a non-trival destructor. They are allocated 22 // with a BumpPtrAllocator and therefore their destructor is not executed. 23 #define ABSTRACT_COMMENT(COMMENT) 24 #define COMMENT(CLASS, PARENT) \ 25 static_assert(std::is_trivially_destructible<CLASS>::value, \ 26 #CLASS " should be trivially destructible!"); 27 #include "clang/AST/CommentNodes.inc" 28 #undef COMMENT 29 #undef ABSTRACT_COMMENT 30 31 // DeclInfo is also allocated with a BumpPtrAllocator. 32 static_assert(std::is_trivially_destructible<DeclInfo>::value, 33 "DeclInfo should be trivially destructible!"); 34 35 const char *Comment::getCommentKindName() const { 36 switch (getCommentKind()) { 37 case NoCommentKind: return "NoCommentKind"; 38 #define ABSTRACT_COMMENT(COMMENT) 39 #define COMMENT(CLASS, PARENT) \ 40 case CLASS##Kind: \ 41 return #CLASS; 42 #include "clang/AST/CommentNodes.inc" 43 #undef COMMENT 44 #undef ABSTRACT_COMMENT 45 } 46 llvm_unreachable("Unknown comment kind!"); 47 } 48 49 namespace { 50 struct good {}; 51 struct bad {}; 52 53 template <typename T> 54 good implements_child_begin_end(Comment::child_iterator (T::*)() const) { 55 return good(); 56 } 57 58 LLVM_ATTRIBUTE_UNUSED 59 static inline bad implements_child_begin_end( 60 Comment::child_iterator (Comment::*)() const) { 61 return bad(); 62 } 63 64 #define ASSERT_IMPLEMENTS_child_begin(function) \ 65 (void) good(implements_child_begin_end(function)) 66 67 LLVM_ATTRIBUTE_UNUSED 68 static inline void CheckCommentASTNodes() { 69 #define ABSTRACT_COMMENT(COMMENT) 70 #define COMMENT(CLASS, PARENT) \ 71 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \ 72 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end); 73 #include "clang/AST/CommentNodes.inc" 74 #undef COMMENT 75 #undef ABSTRACT_COMMENT 76 } 77 78 #undef ASSERT_IMPLEMENTS_child_begin 79 80 } // end unnamed namespace 81 82 Comment::child_iterator Comment::child_begin() 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_begin(); 89 #include "clang/AST/CommentNodes.inc" 90 #undef COMMENT 91 #undef ABSTRACT_COMMENT 92 } 93 llvm_unreachable("Unknown comment kind!"); 94 } 95 96 Comment::child_iterator Comment::child_end() const { 97 switch (getCommentKind()) { 98 case NoCommentKind: llvm_unreachable("comment without a kind"); 99 #define ABSTRACT_COMMENT(COMMENT) 100 #define COMMENT(CLASS, PARENT) \ 101 case CLASS##Kind: \ 102 return static_cast<const CLASS *>(this)->child_end(); 103 #include "clang/AST/CommentNodes.inc" 104 #undef COMMENT 105 #undef ABSTRACT_COMMENT 106 } 107 llvm_unreachable("Unknown comment kind!"); 108 } 109 110 bool TextComment::isWhitespaceNoCache() const { 111 for (StringRef::const_iterator I = Text.begin(), E = Text.end(); 112 I != E; ++I) { 113 if (!clang::isWhitespace(*I)) 114 return false; 115 } 116 return true; 117 } 118 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()) 123 return false; 124 } else 125 return false; 126 } 127 return true; 128 } 129 130 static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) { 131 TypeLoc TL = SrcTL.IgnoreParens(); 132 133 // Look through attribute types. 134 if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>()) 135 return AttributeTL.getModifiedLoc(); 136 // Look through qualified types. 137 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) 138 return QualifiedTL.getUnqualifiedLoc(); 139 // Look through pointer types. 140 if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>()) 141 return PointerTL.getPointeeLoc().getUnqualifiedLoc(); 142 // Look through reference types. 143 if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>()) 144 return ReferenceTL.getPointeeLoc().getUnqualifiedLoc(); 145 // Look through adjusted types. 146 if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>()) 147 return ATL.getOriginalLoc(); 148 if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>()) 149 return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc(); 150 if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>()) 151 return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc(); 152 if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>()) 153 return ETL.getNamedTypeLoc(); 154 155 return TL; 156 } 157 158 static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) { 159 TypeLoc PrevTL; 160 while (PrevTL != TL) { 161 PrevTL = TL; 162 TL = lookThroughTypedefOrTypeAliasLocs(TL); 163 } 164 165 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { 166 ResFTL = FTL; 167 return true; 168 } 169 170 if (TemplateSpecializationTypeLoc STL = 171 TL.getAs<TemplateSpecializationTypeLoc>()) { 172 // If we have a typedef to a template specialization with exactly one 173 // template argument of a function type, this looks like std::function, 174 // boost::function, or other function wrapper. Treat these typedefs as 175 // functions. 176 if (STL.getNumArgs() != 1) 177 return false; 178 TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0); 179 if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type) 180 return false; 181 TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo(); 182 TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc(); 183 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { 184 ResFTL = FTL; 185 return true; 186 } 187 } 188 189 return false; 190 } 191 192 const char *ParamCommandComment::getDirectionAsString(PassDirection D) { 193 switch (D) { 194 case ParamCommandComment::In: 195 return "[in]"; 196 case ParamCommandComment::Out: 197 return "[out]"; 198 case ParamCommandComment::InOut: 199 return "[in,out]"; 200 } 201 llvm_unreachable("unknown PassDirection"); 202 } 203 204 void DeclInfo::fill() { 205 assert(!IsFilled); 206 207 // Set defaults. 208 Kind = OtherKind; 209 TemplateKind = NotTemplate; 210 IsObjCMethod = false; 211 IsInstanceMethod = false; 212 IsClassMethod = false; 213 ParamVars = None; 214 TemplateParameters = nullptr; 215 216 if (!CommentDecl) { 217 // If there is no declaration, the defaults is our only guess. 218 IsFilled = true; 219 return; 220 } 221 CurrentDecl = CommentDecl; 222 223 Decl::Kind K = CommentDecl->getKind(); 224 const TypeSourceInfo *TSI = nullptr; 225 switch (K) { 226 default: 227 // Defaults are should be good for declarations we don't handle explicitly. 228 break; 229 case Decl::Function: 230 case Decl::CXXMethod: 231 case Decl::CXXConstructor: 232 case Decl::CXXDestructor: 233 case Decl::CXXConversion: { 234 const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl); 235 Kind = FunctionKind; 236 ParamVars = FD->parameters(); 237 ReturnType = FD->getReturnType(); 238 unsigned NumLists = FD->getNumTemplateParameterLists(); 239 if (NumLists != 0) { 240 TemplateKind = TemplateSpecialization; 241 TemplateParameters = 242 FD->getTemplateParameterList(NumLists - 1); 243 } 244 245 if (K == Decl::CXXMethod || K == Decl::CXXConstructor || 246 K == Decl::CXXDestructor || K == Decl::CXXConversion) { 247 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl); 248 IsInstanceMethod = MD->isInstance(); 249 IsClassMethod = !IsInstanceMethod; 250 } 251 break; 252 } 253 case Decl::ObjCMethod: { 254 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl); 255 Kind = FunctionKind; 256 ParamVars = MD->parameters(); 257 ReturnType = MD->getReturnType(); 258 IsObjCMethod = true; 259 IsInstanceMethod = MD->isInstanceMethod(); 260 IsClassMethod = !IsInstanceMethod; 261 break; 262 } 263 case Decl::FunctionTemplate: { 264 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl); 265 Kind = FunctionKind; 266 TemplateKind = Template; 267 const FunctionDecl *FD = FTD->getTemplatedDecl(); 268 ParamVars = FD->parameters(); 269 ReturnType = FD->getReturnType(); 270 TemplateParameters = FTD->getTemplateParameters(); 271 break; 272 } 273 case Decl::ClassTemplate: { 274 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl); 275 Kind = ClassKind; 276 TemplateKind = Template; 277 TemplateParameters = CTD->getTemplateParameters(); 278 break; 279 } 280 case Decl::ClassTemplatePartialSpecialization: { 281 const ClassTemplatePartialSpecializationDecl *CTPSD = 282 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl); 283 Kind = ClassKind; 284 TemplateKind = TemplatePartialSpecialization; 285 TemplateParameters = CTPSD->getTemplateParameters(); 286 break; 287 } 288 case Decl::ClassTemplateSpecialization: 289 Kind = ClassKind; 290 TemplateKind = TemplateSpecialization; 291 break; 292 case Decl::Record: 293 case Decl::CXXRecord: 294 Kind = ClassKind; 295 break; 296 case Decl::Var: 297 if (const VarTemplateDecl *VTD = 298 cast<VarDecl>(CommentDecl)->getDescribedVarTemplate()) { 299 TemplateKind = TemplateSpecialization; 300 TemplateParameters = VTD->getTemplateParameters(); 301 } 302 LLVM_FALLTHROUGH; 303 case Decl::Field: 304 case Decl::EnumConstant: 305 case Decl::ObjCIvar: 306 case Decl::ObjCAtDefsField: 307 case Decl::ObjCProperty: 308 if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl)) 309 TSI = VD->getTypeSourceInfo(); 310 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl)) 311 TSI = PD->getTypeSourceInfo(); 312 Kind = VariableKind; 313 break; 314 case Decl::VarTemplate: { 315 const VarTemplateDecl *VTD = cast<VarTemplateDecl>(CommentDecl); 316 Kind = VariableKind; 317 TemplateKind = Template; 318 TemplateParameters = VTD->getTemplateParameters(); 319 if (const VarDecl *VD = VTD->getTemplatedDecl()) 320 TSI = VD->getTypeSourceInfo(); 321 break; 322 } 323 case Decl::Namespace: 324 Kind = NamespaceKind; 325 break; 326 case Decl::TypeAlias: 327 case Decl::Typedef: 328 Kind = TypedefKind; 329 TSI = cast<TypedefNameDecl>(CommentDecl)->getTypeSourceInfo(); 330 break; 331 case Decl::TypeAliasTemplate: { 332 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl); 333 Kind = TypedefKind; 334 TemplateKind = Template; 335 TemplateParameters = TAT->getTemplateParameters(); 336 if (TypeAliasDecl *TAD = TAT->getTemplatedDecl()) 337 TSI = TAD->getTypeSourceInfo(); 338 break; 339 } 340 case Decl::Enum: 341 Kind = EnumKind; 342 break; 343 } 344 345 // If the type is a typedef / using to something we consider a function, 346 // extract arguments and return type. 347 if (TSI) { 348 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc(); 349 FunctionTypeLoc FTL; 350 if (getFunctionTypeLoc(TL, FTL)) { 351 Kind = FunctionKind; 352 ParamVars = FTL.getParams(); 353 ReturnType = FTL.getReturnLoc().getType(); 354 } 355 } 356 357 IsFilled = true; 358 } 359 360 StringRef ParamCommandComment::getParamName(const FullComment *FC) const { 361 assert(isParamIndexValid()); 362 if (isVarArgParam()) 363 return "..."; 364 return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName(); 365 } 366 367 StringRef TParamCommandComment::getParamName(const FullComment *FC) const { 368 assert(isPositionValid()); 369 const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters; 370 for (unsigned i = 0, e = getDepth(); i != e; ++i) { 371 assert(TPL && "Unknown TemplateParameterList"); 372 if (i == e - 1) 373 return TPL->getParam(getIndex(i))->getName(); 374 const NamedDecl *Param = TPL->getParam(getIndex(i)); 375 if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) 376 TPL = TTP->getTemplateParameters(); 377 } 378 return ""; 379 } 380 381 } // end namespace comments 382 } // end namespace clang 383 384