Comment.cpp 12 KB

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