Comment.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- Comment.h - Comment AST nodes --------------------------*- 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 comment AST nodes.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_COMMENT_H
  18. #define LLVM_CLANG_AST_COMMENT_H
  19. #include "clang/AST/CommentCommandTraits.h"
  20. #include "clang/AST/DeclObjC.h"
  21. #include "clang/AST/Type.h"
  22. #include "clang/Basic/SourceLocation.h"
  23. #include "llvm/ADT/ArrayRef.h"
  24. #include "llvm/ADT/StringRef.h"
  25. namespace clang {
  26. class Decl;
  27. class ParmVarDecl;
  28. class TemplateParameterList;
  29. namespace comments {
  30. class FullComment;
  31. /// Describes the syntax that was used in a documentation command.
  32. ///
  33. /// Exact values of this enumeration are important because they used to select
  34. /// parts of diagnostic messages. Audit diagnostics before changing or adding
  35. /// a new value.
  36. enum CommandMarkerKind {
  37. /// Command started with a backslash character:
  38. /// \code
  39. /// \foo
  40. /// \endcode
  41. CMK_Backslash = 0,
  42. /// Command started with an 'at' character:
  43. /// \code
  44. /// @foo
  45. /// \endcode
  46. CMK_At = 1
  47. };
  48. /// Any part of the comment.
  49. /// Abstract class.
  50. class Comment {
  51. protected:
  52. /// Preferred location to show caret.
  53. SourceLocation Loc;
  54. /// Source range of this AST node.
  55. SourceRange Range;
  56. class CommentBitfields {
  57. friend class Comment;
  58. /// Type of this AST node.
  59. unsigned Kind : 8;
  60. };
  61. enum { NumCommentBits = 8 };
  62. class InlineContentCommentBitfields {
  63. friend class InlineContentComment;
  64. unsigned : NumCommentBits;
  65. /// True if there is a newline after this inline content node.
  66. /// (There is no separate AST node for a newline.)
  67. unsigned HasTrailingNewline : 1;
  68. };
  69. enum { NumInlineContentCommentBits = NumCommentBits + 1 };
  70. class TextCommentBitfields {
  71. friend class TextComment;
  72. unsigned : NumInlineContentCommentBits;
  73. /// True if \c IsWhitespace field contains a valid value.
  74. mutable unsigned IsWhitespaceValid : 1;
  75. /// True if this comment AST node contains only whitespace.
  76. mutable unsigned IsWhitespace : 1;
  77. };
  78. enum { NumTextCommentBits = NumInlineContentCommentBits + 2 };
  79. class InlineCommandCommentBitfields {
  80. friend class InlineCommandComment;
  81. unsigned : NumInlineContentCommentBits;
  82. unsigned RenderKind : 3;
  83. unsigned CommandID : CommandInfo::NumCommandIDBits;
  84. };
  85. enum { NumInlineCommandCommentBits = NumInlineContentCommentBits + 3 +
  86. CommandInfo::NumCommandIDBits };
  87. class HTMLTagCommentBitfields {
  88. friend class HTMLTagComment;
  89. unsigned : NumInlineContentCommentBits;
  90. /// True if we found that this tag is malformed in some way.
  91. unsigned IsMalformed : 1;
  92. };
  93. enum { NumHTMLTagCommentBits = NumInlineContentCommentBits + 1 };
  94. class HTMLStartTagCommentBitfields {
  95. friend class HTMLStartTagComment;
  96. unsigned : NumHTMLTagCommentBits;
  97. /// True if this tag is self-closing (e. g., <br />). This is based on tag
  98. /// spelling in comment (plain <br> would not set this flag).
  99. unsigned IsSelfClosing : 1;
  100. };
  101. enum { NumHTMLStartTagCommentBits = NumHTMLTagCommentBits + 1 };
  102. class ParagraphCommentBitfields {
  103. friend class ParagraphComment;
  104. unsigned : NumCommentBits;
  105. /// True if \c IsWhitespace field contains a valid value.
  106. mutable unsigned IsWhitespaceValid : 1;
  107. /// True if this comment AST node contains only whitespace.
  108. mutable unsigned IsWhitespace : 1;
  109. };
  110. enum { NumParagraphCommentBits = NumCommentBits + 2 };
  111. class BlockCommandCommentBitfields {
  112. friend class BlockCommandComment;
  113. unsigned : NumCommentBits;
  114. unsigned CommandID : CommandInfo::NumCommandIDBits;
  115. /// Describes the syntax that was used in a documentation command.
  116. /// Contains values from CommandMarkerKind enum.
  117. unsigned CommandMarker : 1;
  118. };
  119. enum { NumBlockCommandCommentBits = NumCommentBits +
  120. CommandInfo::NumCommandIDBits + 1 };
  121. class ParamCommandCommentBitfields {
  122. friend class ParamCommandComment;
  123. unsigned : NumBlockCommandCommentBits;
  124. /// Parameter passing direction, see ParamCommandComment::PassDirection.
  125. unsigned Direction : 2;
  126. /// True if direction was specified explicitly in the comment.
  127. unsigned IsDirectionExplicit : 1;
  128. };
  129. enum { NumParamCommandCommentBits = NumBlockCommandCommentBits + 3 };
  130. union {
  131. CommentBitfields CommentBits;
  132. InlineContentCommentBitfields InlineContentCommentBits;
  133. TextCommentBitfields TextCommentBits;
  134. InlineCommandCommentBitfields InlineCommandCommentBits;
  135. HTMLTagCommentBitfields HTMLTagCommentBits;
  136. HTMLStartTagCommentBitfields HTMLStartTagCommentBits;
  137. ParagraphCommentBitfields ParagraphCommentBits;
  138. BlockCommandCommentBitfields BlockCommandCommentBits;
  139. ParamCommandCommentBitfields ParamCommandCommentBits;
  140. };
  141. void setSourceRange(SourceRange SR) {
  142. Range = SR;
  143. }
  144. void setLocation(SourceLocation L) {
  145. Loc = L;
  146. }
  147. public:
  148. enum CommentKind {
  149. NoCommentKind = 0,
  150. #define COMMENT(CLASS, PARENT) CLASS##Kind,
  151. #define COMMENT_RANGE(BASE, FIRST, LAST) \
  152. First##BASE##Constant=FIRST##Kind, Last##BASE##Constant=LAST##Kind,
  153. #define LAST_COMMENT_RANGE(BASE, FIRST, LAST) \
  154. First##BASE##Constant=FIRST##Kind, Last##BASE##Constant=LAST##Kind
  155. #define ABSTRACT_COMMENT(COMMENT)
  156. #include "clang/AST/CommentNodes.inc"
  157. };
  158. Comment(CommentKind K,
  159. SourceLocation LocBegin,
  160. SourceLocation LocEnd) :
  161. Loc(LocBegin), Range(SourceRange(LocBegin, LocEnd)) {
  162. CommentBits.Kind = K;
  163. }
  164. CommentKind getCommentKind() const {
  165. return static_cast<CommentKind>(CommentBits.Kind);
  166. }
  167. const char *getCommentKindName() const;
  168. void dump() const;
  169. void dumpColor() const;
  170. void dump(raw_ostream &OS, const ASTContext &Context) const;
  171. SourceRange getSourceRange() const LLVM_READONLY { return Range; }
  172. SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
  173. SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
  174. SourceLocation getLocation() const LLVM_READONLY { return Loc; }
  175. typedef Comment * const *child_iterator;
  176. child_iterator child_begin() const;
  177. child_iterator child_end() const;
  178. // TODO: const child iterator
  179. unsigned child_count() const {
  180. return child_end() - child_begin();
  181. }
  182. };
  183. /// Inline content (contained within a block).
  184. /// Abstract class.
  185. class InlineContentComment : public Comment {
  186. protected:
  187. InlineContentComment(CommentKind K,
  188. SourceLocation LocBegin,
  189. SourceLocation LocEnd) :
  190. Comment(K, LocBegin, LocEnd) {
  191. InlineContentCommentBits.HasTrailingNewline = 0;
  192. }
  193. public:
  194. static bool classof(const Comment *C) {
  195. return C->getCommentKind() >= FirstInlineContentCommentConstant &&
  196. C->getCommentKind() <= LastInlineContentCommentConstant;
  197. }
  198. void addTrailingNewline() {
  199. InlineContentCommentBits.HasTrailingNewline = 1;
  200. }
  201. bool hasTrailingNewline() const {
  202. return InlineContentCommentBits.HasTrailingNewline;
  203. }
  204. };
  205. /// Plain text.
  206. class TextComment : public InlineContentComment {
  207. StringRef Text;
  208. public:
  209. TextComment(SourceLocation LocBegin,
  210. SourceLocation LocEnd,
  211. StringRef Text) :
  212. InlineContentComment(TextCommentKind, LocBegin, LocEnd),
  213. Text(Text) {
  214. TextCommentBits.IsWhitespaceValid = false;
  215. }
  216. static bool classof(const Comment *C) {
  217. return C->getCommentKind() == TextCommentKind;
  218. }
  219. child_iterator child_begin() const { return nullptr; }
  220. child_iterator child_end() const { return nullptr; }
  221. StringRef getText() const LLVM_READONLY { return Text; }
  222. bool isWhitespace() const {
  223. if (TextCommentBits.IsWhitespaceValid)
  224. return TextCommentBits.IsWhitespace;
  225. TextCommentBits.IsWhitespace = isWhitespaceNoCache();
  226. TextCommentBits.IsWhitespaceValid = true;
  227. return TextCommentBits.IsWhitespace;
  228. }
  229. private:
  230. bool isWhitespaceNoCache() const;
  231. };
  232. /// A command with word-like arguments that is considered inline content.
  233. class InlineCommandComment : public InlineContentComment {
  234. public:
  235. struct Argument {
  236. SourceRange Range;
  237. StringRef Text;
  238. Argument(SourceRange Range, StringRef Text) : Range(Range), Text(Text) { }
  239. };
  240. /// The most appropriate rendering mode for this command, chosen on command
  241. /// semantics in Doxygen.
  242. enum RenderKind {
  243. RenderNormal,
  244. RenderBold,
  245. RenderMonospaced,
  246. RenderEmphasized,
  247. RenderAnchor
  248. };
  249. protected:
  250. /// Command arguments.
  251. ArrayRef<Argument> Args;
  252. public:
  253. InlineCommandComment(SourceLocation LocBegin,
  254. SourceLocation LocEnd,
  255. unsigned CommandID,
  256. RenderKind RK,
  257. ArrayRef<Argument> Args) :
  258. InlineContentComment(InlineCommandCommentKind, LocBegin, LocEnd),
  259. Args(Args) {
  260. InlineCommandCommentBits.RenderKind = RK;
  261. InlineCommandCommentBits.CommandID = CommandID;
  262. }
  263. static bool classof(const Comment *C) {
  264. return C->getCommentKind() == InlineCommandCommentKind;
  265. }
  266. child_iterator child_begin() const { return nullptr; }
  267. child_iterator child_end() const { return nullptr; }
  268. unsigned getCommandID() const {
  269. return InlineCommandCommentBits.CommandID;
  270. }
  271. StringRef getCommandName(const CommandTraits &Traits) const {
  272. return Traits.getCommandInfo(getCommandID())->Name;
  273. }
  274. SourceRange getCommandNameRange() const {
  275. return SourceRange(getBeginLoc().getLocWithOffset(-1), getEndLoc());
  276. }
  277. RenderKind getRenderKind() const {
  278. return static_cast<RenderKind>(InlineCommandCommentBits.RenderKind);
  279. }
  280. unsigned getNumArgs() const {
  281. return Args.size();
  282. }
  283. StringRef getArgText(unsigned Idx) const {
  284. return Args[Idx].Text;
  285. }
  286. SourceRange getArgRange(unsigned Idx) const {
  287. return Args[Idx].Range;
  288. }
  289. };
  290. /// Abstract class for opening and closing HTML tags. HTML tags are always
  291. /// treated as inline content (regardless HTML semantics).
  292. class HTMLTagComment : public InlineContentComment {
  293. protected:
  294. StringRef TagName;
  295. SourceRange TagNameRange;
  296. HTMLTagComment(CommentKind K,
  297. SourceLocation LocBegin,
  298. SourceLocation LocEnd,
  299. StringRef TagName,
  300. SourceLocation TagNameBegin,
  301. SourceLocation TagNameEnd) :
  302. InlineContentComment(K, LocBegin, LocEnd),
  303. TagName(TagName),
  304. TagNameRange(TagNameBegin, TagNameEnd) {
  305. setLocation(TagNameBegin);
  306. HTMLTagCommentBits.IsMalformed = 0;
  307. }
  308. public:
  309. static bool classof(const Comment *C) {
  310. return C->getCommentKind() >= FirstHTMLTagCommentConstant &&
  311. C->getCommentKind() <= LastHTMLTagCommentConstant;
  312. }
  313. StringRef getTagName() const LLVM_READONLY { return TagName; }
  314. SourceRange getTagNameSourceRange() const LLVM_READONLY {
  315. SourceLocation L = getLocation();
  316. return SourceRange(L.getLocWithOffset(1),
  317. L.getLocWithOffset(1 + TagName.size()));
  318. }
  319. bool isMalformed() const {
  320. return HTMLTagCommentBits.IsMalformed;
  321. }
  322. void setIsMalformed() {
  323. HTMLTagCommentBits.IsMalformed = 1;
  324. }
  325. };
  326. /// An opening HTML tag with attributes.
  327. class HTMLStartTagComment : public HTMLTagComment {
  328. public:
  329. class Attribute {
  330. public:
  331. SourceLocation NameLocBegin;
  332. StringRef Name;
  333. SourceLocation EqualsLoc;
  334. SourceRange ValueRange;
  335. StringRef Value;
  336. Attribute() { }
  337. Attribute(SourceLocation NameLocBegin, StringRef Name)
  338. : NameLocBegin(NameLocBegin), Name(Name), EqualsLoc(SourceLocation()) {}
  339. Attribute(SourceLocation NameLocBegin, StringRef Name,
  340. SourceLocation EqualsLoc, SourceRange ValueRange, StringRef Value)
  341. : NameLocBegin(NameLocBegin), Name(Name), EqualsLoc(EqualsLoc),
  342. ValueRange(ValueRange), Value(Value) {}
  343. SourceLocation getNameLocEnd() const {
  344. return NameLocBegin.getLocWithOffset(Name.size());
  345. }
  346. SourceRange getNameRange() const {
  347. return SourceRange(NameLocBegin, getNameLocEnd());
  348. }
  349. };
  350. private:
  351. ArrayRef<Attribute> Attributes;
  352. public:
  353. HTMLStartTagComment(SourceLocation LocBegin,
  354. StringRef TagName) :
  355. HTMLTagComment(HTMLStartTagCommentKind,
  356. LocBegin, LocBegin.getLocWithOffset(1 + TagName.size()),
  357. TagName,
  358. LocBegin.getLocWithOffset(1),
  359. LocBegin.getLocWithOffset(1 + TagName.size())) {
  360. HTMLStartTagCommentBits.IsSelfClosing = false;
  361. }
  362. static bool classof(const Comment *C) {
  363. return C->getCommentKind() == HTMLStartTagCommentKind;
  364. }
  365. child_iterator child_begin() const { return nullptr; }
  366. child_iterator child_end() const { return nullptr; }
  367. unsigned getNumAttrs() const {
  368. return Attributes.size();
  369. }
  370. const Attribute &getAttr(unsigned Idx) const {
  371. return Attributes[Idx];
  372. }
  373. void setAttrs(ArrayRef<Attribute> Attrs) {
  374. Attributes = Attrs;
  375. if (!Attrs.empty()) {
  376. const Attribute &Attr = Attrs.back();
  377. SourceLocation L = Attr.ValueRange.getEnd();
  378. if (L.isValid())
  379. Range.setEnd(L);
  380. else {
  381. Range.setEnd(Attr.getNameLocEnd());
  382. }
  383. }
  384. }
  385. void setGreaterLoc(SourceLocation GreaterLoc) {
  386. Range.setEnd(GreaterLoc);
  387. }
  388. bool isSelfClosing() const {
  389. return HTMLStartTagCommentBits.IsSelfClosing;
  390. }
  391. void setSelfClosing() {
  392. HTMLStartTagCommentBits.IsSelfClosing = true;
  393. }
  394. };
  395. /// A closing HTML tag.
  396. class HTMLEndTagComment : public HTMLTagComment {
  397. public:
  398. HTMLEndTagComment(SourceLocation LocBegin,
  399. SourceLocation LocEnd,
  400. StringRef TagName) :
  401. HTMLTagComment(HTMLEndTagCommentKind,
  402. LocBegin, LocEnd,
  403. TagName,
  404. LocBegin.getLocWithOffset(2),
  405. LocBegin.getLocWithOffset(2 + TagName.size()))
  406. { }
  407. static bool classof(const Comment *C) {
  408. return C->getCommentKind() == HTMLEndTagCommentKind;
  409. }
  410. child_iterator child_begin() const { return nullptr; }
  411. child_iterator child_end() const { return nullptr; }
  412. };
  413. /// Block content (contains inline content).
  414. /// Abstract class.
  415. class BlockContentComment : public Comment {
  416. protected:
  417. BlockContentComment(CommentKind K,
  418. SourceLocation LocBegin,
  419. SourceLocation LocEnd) :
  420. Comment(K, LocBegin, LocEnd)
  421. { }
  422. public:
  423. static bool classof(const Comment *C) {
  424. return C->getCommentKind() >= FirstBlockContentCommentConstant &&
  425. C->getCommentKind() <= LastBlockContentCommentConstant;
  426. }
  427. };
  428. /// A single paragraph that contains inline content.
  429. class ParagraphComment : public BlockContentComment {
  430. ArrayRef<InlineContentComment *> Content;
  431. public:
  432. ParagraphComment(ArrayRef<InlineContentComment *> Content) :
  433. BlockContentComment(ParagraphCommentKind,
  434. SourceLocation(),
  435. SourceLocation()),
  436. Content(Content) {
  437. if (Content.empty()) {
  438. ParagraphCommentBits.IsWhitespace = true;
  439. ParagraphCommentBits.IsWhitespaceValid = true;
  440. return;
  441. }
  442. ParagraphCommentBits.IsWhitespaceValid = false;
  443. setSourceRange(SourceRange(Content.front()->getBeginLoc(),
  444. Content.back()->getEndLoc()));
  445. setLocation(Content.front()->getBeginLoc());
  446. }
  447. static bool classof(const Comment *C) {
  448. return C->getCommentKind() == ParagraphCommentKind;
  449. }
  450. child_iterator child_begin() const {
  451. return reinterpret_cast<child_iterator>(Content.begin());
  452. }
  453. child_iterator child_end() const {
  454. return reinterpret_cast<child_iterator>(Content.end());
  455. }
  456. bool isWhitespace() const {
  457. if (ParagraphCommentBits.IsWhitespaceValid)
  458. return ParagraphCommentBits.IsWhitespace;
  459. ParagraphCommentBits.IsWhitespace = isWhitespaceNoCache();
  460. ParagraphCommentBits.IsWhitespaceValid = true;
  461. return ParagraphCommentBits.IsWhitespace;
  462. }
  463. private:
  464. bool isWhitespaceNoCache() const;
  465. };
  466. /// A command that has zero or more word-like arguments (number of word-like
  467. /// arguments depends on command name) and a paragraph as an argument
  468. /// (e. g., \\brief).
  469. class BlockCommandComment : public BlockContentComment {
  470. public:
  471. struct Argument {
  472. SourceRange Range;
  473. StringRef Text;
  474. Argument() { }
  475. Argument(SourceRange Range, StringRef Text) : Range(Range), Text(Text) { }
  476. };
  477. protected:
  478. /// Word-like arguments.
  479. ArrayRef<Argument> Args;
  480. /// Paragraph argument.
  481. ParagraphComment *Paragraph;
  482. BlockCommandComment(CommentKind K,
  483. SourceLocation LocBegin,
  484. SourceLocation LocEnd,
  485. unsigned CommandID,
  486. CommandMarkerKind CommandMarker) :
  487. BlockContentComment(K, LocBegin, LocEnd),
  488. Paragraph(nullptr) {
  489. setLocation(getCommandNameBeginLoc());
  490. BlockCommandCommentBits.CommandID = CommandID;
  491. BlockCommandCommentBits.CommandMarker = CommandMarker;
  492. }
  493. public:
  494. BlockCommandComment(SourceLocation LocBegin,
  495. SourceLocation LocEnd,
  496. unsigned CommandID,
  497. CommandMarkerKind CommandMarker) :
  498. BlockContentComment(BlockCommandCommentKind, LocBegin, LocEnd),
  499. Paragraph(nullptr) {
  500. setLocation(getCommandNameBeginLoc());
  501. BlockCommandCommentBits.CommandID = CommandID;
  502. BlockCommandCommentBits.CommandMarker = CommandMarker;
  503. }
  504. static bool classof(const Comment *C) {
  505. return C->getCommentKind() >= FirstBlockCommandCommentConstant &&
  506. C->getCommentKind() <= LastBlockCommandCommentConstant;
  507. }
  508. child_iterator child_begin() const {
  509. return reinterpret_cast<child_iterator>(&Paragraph);
  510. }
  511. child_iterator child_end() const {
  512. return reinterpret_cast<child_iterator>(&Paragraph + 1);
  513. }
  514. unsigned getCommandID() const {
  515. return BlockCommandCommentBits.CommandID;
  516. }
  517. StringRef getCommandName(const CommandTraits &Traits) const {
  518. return Traits.getCommandInfo(getCommandID())->Name;
  519. }
  520. SourceLocation getCommandNameBeginLoc() const {
  521. return getBeginLoc().getLocWithOffset(1);
  522. }
  523. SourceRange getCommandNameRange(const CommandTraits &Traits) const {
  524. StringRef Name = getCommandName(Traits);
  525. return SourceRange(getCommandNameBeginLoc(),
  526. getBeginLoc().getLocWithOffset(1 + Name.size()));
  527. }
  528. unsigned getNumArgs() const {
  529. return Args.size();
  530. }
  531. StringRef getArgText(unsigned Idx) const {
  532. return Args[Idx].Text;
  533. }
  534. SourceRange getArgRange(unsigned Idx) const {
  535. return Args[Idx].Range;
  536. }
  537. void setArgs(ArrayRef<Argument> A) {
  538. Args = A;
  539. if (Args.size() > 0) {
  540. SourceLocation NewLocEnd = Args.back().Range.getEnd();
  541. if (NewLocEnd.isValid())
  542. setSourceRange(SourceRange(getBeginLoc(), NewLocEnd));
  543. }
  544. }
  545. ParagraphComment *getParagraph() const LLVM_READONLY {
  546. return Paragraph;
  547. }
  548. bool hasNonWhitespaceParagraph() const {
  549. return Paragraph && !Paragraph->isWhitespace();
  550. }
  551. void setParagraph(ParagraphComment *PC) {
  552. Paragraph = PC;
  553. SourceLocation NewLocEnd = PC->getEndLoc();
  554. if (NewLocEnd.isValid())
  555. setSourceRange(SourceRange(getBeginLoc(), NewLocEnd));
  556. }
  557. CommandMarkerKind getCommandMarker() const LLVM_READONLY {
  558. return static_cast<CommandMarkerKind>(
  559. BlockCommandCommentBits.CommandMarker);
  560. }
  561. };
  562. /// Doxygen \\param command.
  563. class ParamCommandComment : public BlockCommandComment {
  564. private:
  565. /// Parameter index in the function declaration.
  566. unsigned ParamIndex;
  567. public:
  568. enum : unsigned {
  569. InvalidParamIndex = ~0U,
  570. VarArgParamIndex = ~0U/*InvalidParamIndex*/ - 1U
  571. };
  572. ParamCommandComment(SourceLocation LocBegin,
  573. SourceLocation LocEnd,
  574. unsigned CommandID,
  575. CommandMarkerKind CommandMarker) :
  576. BlockCommandComment(ParamCommandCommentKind, LocBegin, LocEnd,
  577. CommandID, CommandMarker),
  578. ParamIndex(InvalidParamIndex) {
  579. ParamCommandCommentBits.Direction = In;
  580. ParamCommandCommentBits.IsDirectionExplicit = false;
  581. }
  582. static bool classof(const Comment *C) {
  583. return C->getCommentKind() == ParamCommandCommentKind;
  584. }
  585. enum PassDirection {
  586. In,
  587. Out,
  588. InOut
  589. };
  590. static const char *getDirectionAsString(PassDirection D);
  591. PassDirection getDirection() const LLVM_READONLY {
  592. return static_cast<PassDirection>(ParamCommandCommentBits.Direction);
  593. }
  594. bool isDirectionExplicit() const LLVM_READONLY {
  595. return ParamCommandCommentBits.IsDirectionExplicit;
  596. }
  597. void setDirection(PassDirection Direction, bool Explicit) {
  598. ParamCommandCommentBits.Direction = Direction;
  599. ParamCommandCommentBits.IsDirectionExplicit = Explicit;
  600. }
  601. bool hasParamName() const {
  602. return getNumArgs() > 0;
  603. }
  604. StringRef getParamName(const FullComment *FC) const;
  605. StringRef getParamNameAsWritten() const {
  606. return Args[0].Text;
  607. }
  608. SourceRange getParamNameRange() const {
  609. return Args[0].Range;
  610. }
  611. bool isParamIndexValid() const LLVM_READONLY {
  612. return ParamIndex != InvalidParamIndex;
  613. }
  614. bool isVarArgParam() const LLVM_READONLY {
  615. return ParamIndex == VarArgParamIndex;
  616. }
  617. void setIsVarArgParam() {
  618. ParamIndex = VarArgParamIndex;
  619. assert(isParamIndexValid());
  620. }
  621. unsigned getParamIndex() const LLVM_READONLY {
  622. assert(isParamIndexValid());
  623. assert(!isVarArgParam());
  624. return ParamIndex;
  625. }
  626. void setParamIndex(unsigned Index) {
  627. ParamIndex = Index;
  628. assert(isParamIndexValid());
  629. assert(!isVarArgParam());
  630. }
  631. };
  632. /// Doxygen \\tparam command, describes a template parameter.
  633. class TParamCommandComment : public BlockCommandComment {
  634. private:
  635. /// If this template parameter name was resolved (found in template parameter
  636. /// list), then this stores a list of position indexes in all template
  637. /// parameter lists.
  638. ///
  639. /// For example:
  640. /// \verbatim
  641. /// template<typename C, template<typename T> class TT>
  642. /// void test(TT<int> aaa);
  643. /// \endverbatim
  644. /// For C: Position = { 0 }
  645. /// For TT: Position = { 1 }
  646. /// For T: Position = { 1, 0 }
  647. ArrayRef<unsigned> Position;
  648. public:
  649. TParamCommandComment(SourceLocation LocBegin,
  650. SourceLocation LocEnd,
  651. unsigned CommandID,
  652. CommandMarkerKind CommandMarker) :
  653. BlockCommandComment(TParamCommandCommentKind, LocBegin, LocEnd, CommandID,
  654. CommandMarker)
  655. { }
  656. static bool classof(const Comment *C) {
  657. return C->getCommentKind() == TParamCommandCommentKind;
  658. }
  659. bool hasParamName() const {
  660. return getNumArgs() > 0;
  661. }
  662. StringRef getParamName(const FullComment *FC) const;
  663. StringRef getParamNameAsWritten() const {
  664. return Args[0].Text;
  665. }
  666. SourceRange getParamNameRange() const {
  667. return Args[0].Range;
  668. }
  669. bool isPositionValid() const LLVM_READONLY {
  670. return !Position.empty();
  671. }
  672. unsigned getDepth() const {
  673. assert(isPositionValid());
  674. return Position.size();
  675. }
  676. unsigned getIndex(unsigned Depth) const {
  677. assert(isPositionValid());
  678. return Position[Depth];
  679. }
  680. void setPosition(ArrayRef<unsigned> NewPosition) {
  681. Position = NewPosition;
  682. assert(isPositionValid());
  683. }
  684. };
  685. /// A line of text contained in a verbatim block.
  686. class VerbatimBlockLineComment : public Comment {
  687. StringRef Text;
  688. public:
  689. VerbatimBlockLineComment(SourceLocation LocBegin,
  690. StringRef Text) :
  691. Comment(VerbatimBlockLineCommentKind,
  692. LocBegin,
  693. LocBegin.getLocWithOffset(Text.size())),
  694. Text(Text)
  695. { }
  696. static bool classof(const Comment *C) {
  697. return C->getCommentKind() == VerbatimBlockLineCommentKind;
  698. }
  699. child_iterator child_begin() const { return nullptr; }
  700. child_iterator child_end() const { return nullptr; }
  701. StringRef getText() const LLVM_READONLY {
  702. return Text;
  703. }
  704. };
  705. /// A verbatim block command (e. g., preformatted code). Verbatim block has an
  706. /// opening and a closing command and contains multiple lines of text
  707. /// (VerbatimBlockLineComment nodes).
  708. class VerbatimBlockComment : public BlockCommandComment {
  709. protected:
  710. StringRef CloseName;
  711. SourceLocation CloseNameLocBegin;
  712. ArrayRef<VerbatimBlockLineComment *> Lines;
  713. public:
  714. VerbatimBlockComment(SourceLocation LocBegin,
  715. SourceLocation LocEnd,
  716. unsigned CommandID) :
  717. BlockCommandComment(VerbatimBlockCommentKind,
  718. LocBegin, LocEnd, CommandID,
  719. CMK_At) // FIXME: improve source fidelity.
  720. { }
  721. static bool classof(const Comment *C) {
  722. return C->getCommentKind() == VerbatimBlockCommentKind;
  723. }
  724. child_iterator child_begin() const {
  725. return reinterpret_cast<child_iterator>(Lines.begin());
  726. }
  727. child_iterator child_end() const {
  728. return reinterpret_cast<child_iterator>(Lines.end());
  729. }
  730. void setCloseName(StringRef Name, SourceLocation LocBegin) {
  731. CloseName = Name;
  732. CloseNameLocBegin = LocBegin;
  733. }
  734. void setLines(ArrayRef<VerbatimBlockLineComment *> L) {
  735. Lines = L;
  736. }
  737. StringRef getCloseName() const {
  738. return CloseName;
  739. }
  740. unsigned getNumLines() const {
  741. return Lines.size();
  742. }
  743. StringRef getText(unsigned LineIdx) const {
  744. return Lines[LineIdx]->getText();
  745. }
  746. };
  747. /// A verbatim line command. Verbatim line has an opening command, a single
  748. /// line of text (up to the newline after the opening command) and has no
  749. /// closing command.
  750. class VerbatimLineComment : public BlockCommandComment {
  751. protected:
  752. StringRef Text;
  753. SourceLocation TextBegin;
  754. public:
  755. VerbatimLineComment(SourceLocation LocBegin,
  756. SourceLocation LocEnd,
  757. unsigned CommandID,
  758. SourceLocation TextBegin,
  759. StringRef Text) :
  760. BlockCommandComment(VerbatimLineCommentKind,
  761. LocBegin, LocEnd,
  762. CommandID,
  763. CMK_At), // FIXME: improve source fidelity.
  764. Text(Text),
  765. TextBegin(TextBegin)
  766. { }
  767. static bool classof(const Comment *C) {
  768. return C->getCommentKind() == VerbatimLineCommentKind;
  769. }
  770. child_iterator child_begin() const { return nullptr; }
  771. child_iterator child_end() const { return nullptr; }
  772. StringRef getText() const {
  773. return Text;
  774. }
  775. SourceRange getTextRange() const {
  776. return SourceRange(TextBegin, getEndLoc());
  777. }
  778. };
  779. /// Information about the declaration, useful to clients of FullComment.
  780. struct DeclInfo {
  781. /// Declaration the comment is actually attached to (in the source).
  782. /// Should not be NULL.
  783. const Decl *CommentDecl;
  784. /// CurrentDecl is the declaration with which the FullComment is associated.
  785. ///
  786. /// It can be different from \c CommentDecl. It happens when we decide
  787. /// that the comment originally attached to \c CommentDecl is fine for
  788. /// \c CurrentDecl too (for example, for a redeclaration or an overrider of
  789. /// \c CommentDecl).
  790. ///
  791. /// The information in the DeclInfo corresponds to CurrentDecl.
  792. const Decl *CurrentDecl;
  793. /// Parameters that can be referenced by \\param if \c CommentDecl is something
  794. /// that we consider a "function".
  795. ArrayRef<const ParmVarDecl *> ParamVars;
  796. /// Function return type if \c CommentDecl is something that we consider
  797. /// a "function".
  798. QualType ReturnType;
  799. /// Template parameters that can be referenced by \\tparam if \c CommentDecl is
  800. /// a template (\c IsTemplateDecl or \c IsTemplatePartialSpecialization is
  801. /// true).
  802. const TemplateParameterList *TemplateParameters;
  803. /// A simplified description of \c CommentDecl kind that should be good enough
  804. /// for documentation rendering purposes.
  805. enum DeclKind {
  806. /// Everything else not explicitly mentioned below.
  807. OtherKind,
  808. /// Something that we consider a "function":
  809. /// \li function,
  810. /// \li function template,
  811. /// \li function template specialization,
  812. /// \li member function,
  813. /// \li member function template,
  814. /// \li member function template specialization,
  815. /// \li ObjC method,
  816. FunctionKind,
  817. /// Something that we consider a "class":
  818. /// \li class/struct,
  819. /// \li class template,
  820. /// \li class template (partial) specialization.
  821. ClassKind,
  822. /// Something that we consider a "variable":
  823. /// \li namespace scope variables and variable templates;
  824. /// \li static and non-static class data members and member templates;
  825. /// \li enumerators.
  826. VariableKind,
  827. /// A C++ namespace.
  828. NamespaceKind,
  829. /// A C++ typedef-name (a 'typedef' decl specifier or alias-declaration),
  830. /// see \c TypedefNameDecl.
  831. TypedefKind,
  832. /// An enumeration or scoped enumeration.
  833. EnumKind
  834. };
  835. /// What kind of template specialization \c CommentDecl is.
  836. enum TemplateDeclKind {
  837. NotTemplate,
  838. Template,
  839. TemplateSpecialization,
  840. TemplatePartialSpecialization
  841. };
  842. /// If false, only \c CommentDecl is valid.
  843. unsigned IsFilled : 1;
  844. /// Simplified kind of \c CommentDecl, see \c DeclKind enum.
  845. unsigned Kind : 3;
  846. /// Is \c CommentDecl a template declaration.
  847. unsigned TemplateKind : 2;
  848. /// Is \c CommentDecl an ObjCMethodDecl.
  849. unsigned IsObjCMethod : 1;
  850. /// Is \c CommentDecl a non-static member function of C++ class or
  851. /// instance method of ObjC class.
  852. /// Can be true only if \c IsFunctionDecl is true.
  853. unsigned IsInstanceMethod : 1;
  854. /// Is \c CommentDecl a static member function of C++ class or
  855. /// class method of ObjC class.
  856. /// Can be true only if \c IsFunctionDecl is true.
  857. unsigned IsClassMethod : 1;
  858. /// Is \c CommentDecl something we consider a "function" that's variadic.
  859. unsigned IsVariadic : 1;
  860. void fill();
  861. DeclKind getKind() const LLVM_READONLY {
  862. return static_cast<DeclKind>(Kind);
  863. }
  864. TemplateDeclKind getTemplateKind() const LLVM_READONLY {
  865. return static_cast<TemplateDeclKind>(TemplateKind);
  866. }
  867. bool involvesFunctionType() const { return !ReturnType.isNull(); }
  868. };
  869. /// A full comment attached to a declaration, contains block content.
  870. class FullComment : public Comment {
  871. ArrayRef<BlockContentComment *> Blocks;
  872. DeclInfo *ThisDeclInfo;
  873. public:
  874. FullComment(ArrayRef<BlockContentComment *> Blocks, DeclInfo *D) :
  875. Comment(FullCommentKind, SourceLocation(), SourceLocation()),
  876. Blocks(Blocks), ThisDeclInfo(D) {
  877. if (Blocks.empty())
  878. return;
  879. setSourceRange(
  880. SourceRange(Blocks.front()->getBeginLoc(), Blocks.back()->getEndLoc()));
  881. setLocation(Blocks.front()->getBeginLoc());
  882. }
  883. static bool classof(const Comment *C) {
  884. return C->getCommentKind() == FullCommentKind;
  885. }
  886. child_iterator child_begin() const {
  887. return reinterpret_cast<child_iterator>(Blocks.begin());
  888. }
  889. child_iterator child_end() const {
  890. return reinterpret_cast<child_iterator>(Blocks.end());
  891. }
  892. const Decl *getDecl() const LLVM_READONLY {
  893. return ThisDeclInfo->CommentDecl;
  894. }
  895. const DeclInfo *getDeclInfo() const LLVM_READONLY {
  896. if (!ThisDeclInfo->IsFilled)
  897. ThisDeclInfo->fill();
  898. return ThisDeclInfo;
  899. }
  900. ArrayRef<BlockContentComment *> getBlocks() const { return Blocks; }
  901. };
  902. } // end namespace comments
  903. } // end namespace clang
  904. #endif
  905. #ifdef __GNUC__
  906. #pragma GCC diagnostic pop
  907. #endif