CommentSema.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 None;
  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. InlineCommandComment *actOnInlineCommand(SourceLocation CommandLocBegin,
  109. SourceLocation CommandLocEnd,
  110. unsigned CommandID,
  111. SourceLocation ArgLocBegin,
  112. SourceLocation ArgLocEnd,
  113. StringRef Arg);
  114. InlineContentComment *actOnUnknownCommand(SourceLocation LocBegin,
  115. SourceLocation LocEnd,
  116. StringRef CommandName);
  117. InlineContentComment *actOnUnknownCommand(SourceLocation LocBegin,
  118. SourceLocation LocEnd,
  119. unsigned CommandID);
  120. TextComment *actOnText(SourceLocation LocBegin,
  121. SourceLocation LocEnd,
  122. StringRef Text);
  123. VerbatimBlockComment *actOnVerbatimBlockStart(SourceLocation Loc,
  124. unsigned CommandID);
  125. VerbatimBlockLineComment *actOnVerbatimBlockLine(SourceLocation Loc,
  126. StringRef Text);
  127. void actOnVerbatimBlockFinish(VerbatimBlockComment *Block,
  128. SourceLocation CloseNameLocBegin,
  129. StringRef CloseName,
  130. ArrayRef<VerbatimBlockLineComment *> Lines);
  131. VerbatimLineComment *actOnVerbatimLine(SourceLocation LocBegin,
  132. unsigned CommandID,
  133. SourceLocation TextBegin,
  134. StringRef Text);
  135. HTMLStartTagComment *actOnHTMLStartTagStart(SourceLocation LocBegin,
  136. StringRef TagName);
  137. void actOnHTMLStartTagFinish(HTMLStartTagComment *Tag,
  138. ArrayRef<HTMLStartTagComment::Attribute> Attrs,
  139. SourceLocation GreaterLoc,
  140. bool IsSelfClosing);
  141. HTMLEndTagComment *actOnHTMLEndTag(SourceLocation LocBegin,
  142. SourceLocation LocEnd,
  143. StringRef TagName);
  144. FullComment *actOnFullComment(ArrayRef<BlockContentComment *> Blocks);
  145. private:
  146. void checkBlockCommandEmptyParagraph(BlockCommandComment *Command);
  147. void checkReturnsCommand(const BlockCommandComment *Command);
  148. /// Emit diagnostics about duplicate block commands that should be
  149. /// used only once per comment, e.g., \and \\returns.
  150. void checkBlockCommandDuplicate(const BlockCommandComment *Command);
  151. void checkDeprecatedCommand(const BlockCommandComment *Comment);
  152. void checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment);
  153. void checkContainerDeclVerbatimLine(const BlockCommandComment *Comment);
  154. void checkContainerDecl(const BlockCommandComment *Comment);
  155. /// Resolve parameter names to parameter indexes in function declaration.
  156. /// Emit diagnostics about unknown parametrs.
  157. void resolveParamCommandIndexes(const FullComment *FC);
  158. /// \returns \c true if the declaration that this comment is attached to
  159. /// is a pointer to function/method/block type or has such a type.
  160. bool involvesFunctionType();
  161. bool isFunctionDecl();
  162. bool isAnyFunctionDecl();
  163. /// \returns \c true if declaration that this comment is attached to declares
  164. /// a function pointer.
  165. bool isFunctionPointerVarDecl();
  166. bool isFunctionOrMethodVariadic();
  167. bool isObjCMethodDecl();
  168. bool isObjCPropertyDecl();
  169. bool isTemplateOrSpecialization();
  170. bool isRecordLikeDecl();
  171. bool isClassOrStructDecl();
  172. /// \return \c true if the declaration that this comment is attached to
  173. /// declares either struct, class or tag typedef.
  174. bool isClassOrStructOrTagTypedefDecl();
  175. bool isUnionDecl();
  176. bool isObjCInterfaceDecl();
  177. bool isObjCProtocolDecl();
  178. bool isClassTemplateDecl();
  179. bool isFunctionTemplateDecl();
  180. ArrayRef<const ParmVarDecl *> getParamVars();
  181. /// Extract all important semantic information from
  182. /// \c ThisDeclInfo->ThisDecl into \c ThisDeclInfo members.
  183. void inspectThisDecl();
  184. /// Returns index of a function parameter with a given name.
  185. unsigned resolveParmVarReference(StringRef Name,
  186. ArrayRef<const ParmVarDecl *> ParamVars);
  187. /// Returns index of a function parameter with the name closest to a given
  188. /// typo.
  189. unsigned correctTypoInParmVarReference(StringRef Typo,
  190. ArrayRef<const ParmVarDecl *> ParamVars);
  191. bool resolveTParamReference(StringRef Name,
  192. const TemplateParameterList *TemplateParameters,
  193. SmallVectorImpl<unsigned> *Position);
  194. StringRef correctTypoInTParamReference(
  195. StringRef Typo,
  196. const TemplateParameterList *TemplateParameters);
  197. InlineCommandComment::RenderKind
  198. getInlineCommandRenderKind(StringRef Name) const;
  199. };
  200. } // end namespace comments
  201. } // end namespace clang
  202. #endif
  203. #ifdef __GNUC__
  204. #pragma GCC diagnostic pop
  205. #endif