Comment.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. for (StringRef::const_iterator I = Text.begin(), E = Text.end();
  97. I != E; ++I) {
  98. if (!clang::isWhitespace(*I))
  99. return false;
  100. }
  101. return true;
  102. }
  103. bool ParagraphComment::isWhitespaceNoCache() const {
  104. for (child_iterator I = child_begin(), E = child_end(); I != E; ++I) {
  105. if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
  106. if (!TC->isWhitespace())
  107. return false;
  108. } else
  109. return false;
  110. }
  111. return true;
  112. }
  113. static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) {
  114. TypeLoc TL = SrcTL.IgnoreParens();
  115. // Look through attribute types.
  116. if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>())
  117. return AttributeTL.getModifiedLoc();
  118. // Look through qualified types.
  119. if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
  120. return QualifiedTL.getUnqualifiedLoc();
  121. // Look through pointer types.
  122. if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
  123. return PointerTL.getPointeeLoc().getUnqualifiedLoc();
  124. // Look through reference types.
  125. if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
  126. return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
  127. // Look through adjusted types.
  128. if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
  129. return ATL.getOriginalLoc();
  130. if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
  131. return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
  132. if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
  133. return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
  134. if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
  135. return ETL.getNamedTypeLoc();
  136. return TL;
  137. }
  138. static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
  139. TypeLoc PrevTL;
  140. while (PrevTL != TL) {
  141. PrevTL = TL;
  142. TL = lookThroughTypedefOrTypeAliasLocs(TL);
  143. }
  144. if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
  145. ResFTL = FTL;
  146. return true;
  147. }
  148. if (TemplateSpecializationTypeLoc STL =
  149. TL.getAs<TemplateSpecializationTypeLoc>()) {
  150. // If we have a typedef to a template specialization with exactly one
  151. // template argument of a function type, this looks like std::function,
  152. // boost::function, or other function wrapper. Treat these typedefs as
  153. // functions.
  154. if (STL.getNumArgs() != 1)
  155. return false;
  156. TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
  157. if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
  158. return false;
  159. TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
  160. TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
  161. if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
  162. ResFTL = FTL;
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
  169. switch (D) {
  170. case ParamCommandComment::In:
  171. return "[in]";
  172. case ParamCommandComment::Out:
  173. return "[out]";
  174. case ParamCommandComment::InOut:
  175. return "[in,out]";
  176. }
  177. llvm_unreachable("unknown PassDirection");
  178. }
  179. void DeclInfo::fill() {
  180. assert(!IsFilled);
  181. // Set defaults.
  182. Kind = OtherKind;
  183. TemplateKind = NotTemplate;
  184. IsObjCMethod = false;
  185. IsInstanceMethod = false;
  186. IsClassMethod = false;
  187. ParamVars = None;
  188. TemplateParameters = nullptr;
  189. if (!CommentDecl) {
  190. // If there is no declaration, the defaults is our only guess.
  191. IsFilled = true;
  192. return;
  193. }
  194. CurrentDecl = CommentDecl;
  195. Decl::Kind K = CommentDecl->getKind();
  196. switch (K) {
  197. default:
  198. // Defaults are should be good for declarations we don't handle explicitly.
  199. break;
  200. case Decl::Function:
  201. case Decl::CXXMethod:
  202. case Decl::CXXConstructor:
  203. case Decl::CXXDestructor:
  204. case Decl::CXXConversion: {
  205. const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
  206. Kind = FunctionKind;
  207. ParamVars = FD->parameters();
  208. ReturnType = FD->getReturnType();
  209. unsigned NumLists = FD->getNumTemplateParameterLists();
  210. if (NumLists != 0) {
  211. TemplateKind = TemplateSpecialization;
  212. TemplateParameters =
  213. FD->getTemplateParameterList(NumLists - 1);
  214. }
  215. if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
  216. K == Decl::CXXDestructor || K == Decl::CXXConversion) {
  217. const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
  218. IsInstanceMethod = MD->isInstance();
  219. IsClassMethod = !IsInstanceMethod;
  220. }
  221. break;
  222. }
  223. case Decl::ObjCMethod: {
  224. const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
  225. Kind = FunctionKind;
  226. ParamVars = MD->parameters();
  227. ReturnType = MD->getReturnType();
  228. IsObjCMethod = true;
  229. IsInstanceMethod = MD->isInstanceMethod();
  230. IsClassMethod = !IsInstanceMethod;
  231. break;
  232. }
  233. case Decl::FunctionTemplate: {
  234. const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
  235. Kind = FunctionKind;
  236. TemplateKind = Template;
  237. const FunctionDecl *FD = FTD->getTemplatedDecl();
  238. ParamVars = FD->parameters();
  239. ReturnType = FD->getReturnType();
  240. TemplateParameters = FTD->getTemplateParameters();
  241. break;
  242. }
  243. case Decl::ClassTemplate: {
  244. const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
  245. Kind = ClassKind;
  246. TemplateKind = Template;
  247. TemplateParameters = CTD->getTemplateParameters();
  248. break;
  249. }
  250. case Decl::ClassTemplatePartialSpecialization: {
  251. const ClassTemplatePartialSpecializationDecl *CTPSD =
  252. cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
  253. Kind = ClassKind;
  254. TemplateKind = TemplatePartialSpecialization;
  255. TemplateParameters = CTPSD->getTemplateParameters();
  256. break;
  257. }
  258. case Decl::ClassTemplateSpecialization:
  259. Kind = ClassKind;
  260. TemplateKind = TemplateSpecialization;
  261. break;
  262. case Decl::Record:
  263. case Decl::CXXRecord:
  264. Kind = ClassKind;
  265. break;
  266. case Decl::Var:
  267. case Decl::Field:
  268. case Decl::EnumConstant:
  269. case Decl::ObjCIvar:
  270. case Decl::ObjCAtDefsField:
  271. case Decl::ObjCProperty: {
  272. const TypeSourceInfo *TSI;
  273. if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
  274. TSI = VD->getTypeSourceInfo();
  275. else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
  276. TSI = PD->getTypeSourceInfo();
  277. else
  278. TSI = nullptr;
  279. if (TSI) {
  280. TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
  281. FunctionTypeLoc FTL;
  282. if (getFunctionTypeLoc(TL, FTL)) {
  283. ParamVars = FTL.getParams();
  284. ReturnType = FTL.getReturnLoc().getType();
  285. }
  286. }
  287. Kind = VariableKind;
  288. break;
  289. }
  290. case Decl::Namespace:
  291. Kind = NamespaceKind;
  292. break;
  293. case Decl::TypeAlias:
  294. case Decl::Typedef: {
  295. Kind = TypedefKind;
  296. // If this is a typedef / using to something we consider a function, extract
  297. // arguments and return type.
  298. const TypeSourceInfo *TSI =
  299. K == Decl::Typedef
  300. ? cast<TypedefDecl>(CommentDecl)->getTypeSourceInfo()
  301. : cast<TypeAliasDecl>(CommentDecl)->getTypeSourceInfo();
  302. if (!TSI)
  303. break;
  304. TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
  305. FunctionTypeLoc FTL;
  306. if (getFunctionTypeLoc(TL, FTL)) {
  307. Kind = FunctionKind;
  308. ParamVars = FTL.getParams();
  309. ReturnType = FTL.getReturnLoc().getType();
  310. }
  311. break;
  312. }
  313. case Decl::TypeAliasTemplate: {
  314. const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
  315. Kind = TypedefKind;
  316. TemplateKind = Template;
  317. TemplateParameters = TAT->getTemplateParameters();
  318. TypeAliasDecl *TAD = TAT->getTemplatedDecl();
  319. if (!TAD)
  320. break;
  321. const TypeSourceInfo *TSI = TAD->getTypeSourceInfo();
  322. if (!TSI)
  323. break;
  324. TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
  325. FunctionTypeLoc FTL;
  326. if (getFunctionTypeLoc(TL, FTL)) {
  327. Kind = FunctionKind;
  328. ParamVars = FTL.getParams();
  329. ReturnType = FTL.getReturnLoc().getType();
  330. }
  331. break;
  332. }
  333. case Decl::Enum:
  334. Kind = EnumKind;
  335. break;
  336. }
  337. IsFilled = true;
  338. }
  339. StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
  340. assert(isParamIndexValid());
  341. if (isVarArgParam())
  342. return "...";
  343. return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
  344. }
  345. StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
  346. assert(isPositionValid());
  347. const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
  348. for (unsigned i = 0, e = getDepth(); i != e; ++i) {
  349. if (i == e-1)
  350. return TPL->getParam(getIndex(i))->getName();
  351. const NamedDecl *Param = TPL->getParam(getIndex(i));
  352. if (const TemplateTemplateParmDecl *TTP =
  353. dyn_cast<TemplateTemplateParmDecl>(Param))
  354. TPL = TTP->getTemplateParameters();
  355. }
  356. return "";
  357. }
  358. } // end namespace comments
  359. } // end namespace clang