CommentSema.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- CommentSema.h - Doxygen comment semantic analysis ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the semantic analysis class for Doxygen comments.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_COMMENTSEMA_H
  18. #define LLVM_CLANG_AST_COMMENTSEMA_H
  19. #include "clang/AST/Comment.h"
  20. #include "clang/Basic/Diagnostic.h"
  21. #include "clang/Basic/SourceLocation.h"
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/ADT/StringMap.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/Support/Allocator.h"
  26. namespace clang {
  27. class Decl;
  28. class SourceMgr;
  29. class Preprocessor;
  30. namespace comments {
  31. class CommandTraits;
  32. class Sema {
  33. Sema(const Sema &) = delete;
  34. void operator=(const Sema &) = delete;
  35. /// Allocator for AST nodes.
  36. llvm::BumpPtrAllocator &Allocator;
  37. /// Source manager for the comment being parsed.
  38. const SourceManager &SourceMgr;
  39. DiagnosticsEngine &Diags;
  40. CommandTraits &Traits;
  41. const Preprocessor *PP;
  42. /// Information about the declaration this comment is attached to.
  43. DeclInfo *ThisDeclInfo;
  44. /// Comment AST nodes that correspond to parameter names in
  45. /// \c TemplateParameters.
  46. ///
  47. /// Contains a valid value if \c DeclInfo->IsFilled is true.
  48. llvm::StringMap<TParamCommandComment *> TemplateParameterDocs;
  49. /// AST node for the \command and its aliases.
  50. const BlockCommandComment *BriefCommand;
  51. /// AST node for the \\headerfile command.
  52. const BlockCommandComment *HeaderfileCommand;
  53. DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) {
  54. return Diags.Report(Loc, DiagID);
  55. }
  56. /// A stack of HTML tags that are currently open (not matched with closing
  57. /// tags).
  58. SmallVector<HTMLStartTagComment *, 8> HTMLOpenTags;
  59. public:
  60. Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
  61. DiagnosticsEngine &Diags, CommandTraits &Traits,
  62. const Preprocessor *PP);
  63. void setDecl(const Decl *D);
  64. /// Returns a copy of array, owned by Sema's allocator.
  65. template<typename T>
  66. ArrayRef<T> copyArray(ArrayRef<T> Source) {
  67. if (!Source.empty())
  68. return Source.copy(Allocator);
  69. return std::nullopt;
  70. }
  71. ParagraphComment *actOnParagraphComment(
  72. ArrayRef<InlineContentComment *> Content);
  73. BlockCommandComment *actOnBlockCommandStart(SourceLocation LocBegin,
  74. SourceLocation LocEnd,
  75. unsigned CommandID,
  76. CommandMarkerKind CommandMarker);
  77. void actOnBlockCommandArgs(BlockCommandComment *Command,
  78. ArrayRef<BlockCommandComment::Argument> Args);
  79. void actOnBlockCommandFinish(BlockCommandComment *Command,
  80. ParagraphComment *Paragraph);
  81. ParamCommandComment *actOnParamCommandStart(SourceLocation LocBegin,
  82. SourceLocation LocEnd,
  83. unsigned CommandID,
  84. CommandMarkerKind CommandMarker);
  85. void actOnParamCommandDirectionArg(ParamCommandComment *Command,
  86. SourceLocation ArgLocBegin,
  87. SourceLocation ArgLocEnd,
  88. StringRef Arg);
  89. void actOnParamCommandParamNameArg(ParamCommandComment *Command,
  90. SourceLocation ArgLocBegin,
  91. SourceLocation ArgLocEnd,
  92. StringRef Arg);
  93. void actOnParamCommandFinish(ParamCommandComment *Command,
  94. ParagraphComment *Paragraph);
  95. TParamCommandComment *actOnTParamCommandStart(SourceLocation LocBegin,
  96. SourceLocation LocEnd,
  97. unsigned CommandID,
  98. CommandMarkerKind CommandMarker);
  99. void actOnTParamCommandParamNameArg(TParamCommandComment *Command,
  100. SourceLocation ArgLocBegin,
  101. SourceLocation ArgLocEnd,
  102. StringRef Arg);
  103. void actOnTParamCommandFinish(TParamCommandComment *Command,
  104. ParagraphComment *Paragraph);
  105. InlineCommandComment *actOnInlineCommand(SourceLocation CommandLocBegin,
  106. SourceLocation CommandLocEnd,
  107. unsigned CommandID,
  108. ArrayRef<Comment::Argument> Args);
  109. InlineContentComment *actOnUnknownCommand(SourceLocation LocBegin,
  110. SourceLocation LocEnd,
  111. StringRef CommandName);
  112. InlineContentComment *actOnUnknownCommand(SourceLocation LocBegin,
  113. SourceLocation LocEnd,
  114. unsigned CommandID);
  115. TextComment *actOnText(SourceLocation LocBegin,
  116. SourceLocation LocEnd,
  117. StringRef Text);
  118. VerbatimBlockComment *actOnVerbatimBlockStart(SourceLocation Loc,
  119. unsigned CommandID);
  120. VerbatimBlockLineComment *actOnVerbatimBlockLine(SourceLocation Loc,
  121. StringRef Text);
  122. void actOnVerbatimBlockFinish(VerbatimBlockComment *Block,
  123. SourceLocation CloseNameLocBegin,
  124. StringRef CloseName,
  125. ArrayRef<VerbatimBlockLineComment *> Lines);
  126. VerbatimLineComment *actOnVerbatimLine(SourceLocation LocBegin,
  127. unsigned CommandID,
  128. SourceLocation TextBegin,
  129. StringRef Text);
  130. HTMLStartTagComment *actOnHTMLStartTagStart(SourceLocation LocBegin,
  131. StringRef TagName);
  132. void actOnHTMLStartTagFinish(HTMLStartTagComment *Tag,
  133. ArrayRef<HTMLStartTagComment::Attribute> Attrs,
  134. SourceLocation GreaterLoc,
  135. bool IsSelfClosing);
  136. HTMLEndTagComment *actOnHTMLEndTag(SourceLocation LocBegin,
  137. SourceLocation LocEnd,
  138. StringRef TagName);
  139. FullComment *actOnFullComment(ArrayRef<BlockContentComment *> Blocks);
  140. private:
  141. void checkBlockCommandEmptyParagraph(BlockCommandComment *Command);
  142. void checkReturnsCommand(const BlockCommandComment *Command);
  143. /// Emit diagnostics about duplicate block commands that should be
  144. /// used only once per comment, e.g., \and \\returns.
  145. void checkBlockCommandDuplicate(const BlockCommandComment *Command);
  146. void checkDeprecatedCommand(const BlockCommandComment *Comment);
  147. void checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment);
  148. void checkContainerDeclVerbatimLine(const BlockCommandComment *Comment);
  149. void checkContainerDecl(const BlockCommandComment *Comment);
  150. /// Resolve parameter names to parameter indexes in function declaration.
  151. /// Emit diagnostics about unknown parametrs.
  152. void resolveParamCommandIndexes(const FullComment *FC);
  153. /// \returns \c true if the declaration that this comment is attached to
  154. /// is a pointer to function/method/block type or has such a type.
  155. bool involvesFunctionType();
  156. bool isFunctionDecl();
  157. bool isAnyFunctionDecl();
  158. /// \returns \c true if declaration that this comment is attached to declares
  159. /// a function pointer.
  160. bool isFunctionPointerVarDecl();
  161. bool isFunctionOrMethodVariadic();
  162. bool isObjCMethodDecl();
  163. bool isObjCPropertyDecl();
  164. bool isTemplateOrSpecialization();
  165. bool isRecordLikeDecl();
  166. bool isClassOrStructDecl();
  167. /// \return \c true if the declaration that this comment is attached to
  168. /// declares either struct, class or tag typedef.
  169. bool isClassOrStructOrTagTypedefDecl();
  170. bool isUnionDecl();
  171. bool isObjCInterfaceDecl();
  172. bool isObjCProtocolDecl();
  173. bool isClassTemplateDecl();
  174. bool isFunctionTemplateDecl();
  175. ArrayRef<const ParmVarDecl *> getParamVars();
  176. /// Extract all important semantic information from
  177. /// \c ThisDeclInfo->ThisDecl into \c ThisDeclInfo members.
  178. void inspectThisDecl();
  179. /// Returns index of a function parameter with a given name.
  180. unsigned resolveParmVarReference(StringRef Name,
  181. ArrayRef<const ParmVarDecl *> ParamVars);
  182. /// Returns index of a function parameter with the name closest to a given
  183. /// typo.
  184. unsigned correctTypoInParmVarReference(StringRef Typo,
  185. ArrayRef<const ParmVarDecl *> ParamVars);
  186. bool resolveTParamReference(StringRef Name,
  187. const TemplateParameterList *TemplateParameters,
  188. SmallVectorImpl<unsigned> *Position);
  189. StringRef correctTypoInTParamReference(
  190. StringRef Typo,
  191. const TemplateParameterList *TemplateParameters);
  192. InlineCommandComment::RenderKind
  193. getInlineCommandRenderKind(StringRef Name) const;
  194. };
  195. } // end namespace comments
  196. } // end namespace clang
  197. #endif
  198. #ifdef __GNUC__
  199. #pragma GCC diagnostic pop
  200. #endif