Author: Vlad Serebrennikov Date: 2023-11-06T22:17:03+03:00 New Revision: 60f7fa123d77ecf8e138be35ad8880622586fa03
URL: https://github.com/llvm/llvm-project/commit/60f7fa123d77ecf8e138be35ad8880622586fa03 DIFF: https://github.com/llvm/llvm-project/commit/60f7fa123d77ecf8e138be35ad8880622586fa03.diff LOG: [clang][NFC] Refactor `Comment::CommentKind` This patch converts `Comment::CommentKind` into a scoped enum at namespace scope, making it eligible for forward declaring. This is useful for e.g. annotating bit-fields with `preferred_type`. Added: Modified: clang/include/clang/AST/Comment.h clang/include/clang/AST/CommentVisitor.h clang/lib/AST/Comment.cpp clang/lib/Index/CommentToXML.cpp clang/tools/libclang/CXComment.cpp Removed: ################################################################################ diff --git a/clang/include/clang/AST/Comment.h b/clang/include/clang/AST/Comment.h index 0b68c11316649aa..7cbed3600d28287 100644 --- a/clang/include/clang/AST/Comment.h +++ b/clang/include/clang/AST/Comment.h @@ -47,6 +47,17 @@ enum CommandMarkerKind { CMK_At = 1 }; +enum class CommentKind { + None = 0, +#define COMMENT(CLASS, PARENT) CLASS, +#define COMMENT_RANGE(BASE, FIRST, LAST) \ + First##BASE##Constant = FIRST, Last##BASE##Constant = LAST, +#define LAST_COMMENT_RANGE(BASE, FIRST, LAST) \ + First##BASE##Constant = FIRST, Last##BASE##Constant = LAST +#define ABSTRACT_COMMENT(COMMENT) +#include "clang/AST/CommentNodes.inc" +}; + /// Any part of the comment. /// Abstract class. class Comment { @@ -183,17 +194,6 @@ class Comment { } public: - enum CommentKind { - NoCommentKind = 0, -#define COMMENT(CLASS, PARENT) CLASS##Kind, -#define COMMENT_RANGE(BASE, FIRST, LAST) \ - First##BASE##Constant=FIRST##Kind, Last##BASE##Constant=LAST##Kind, -#define LAST_COMMENT_RANGE(BASE, FIRST, LAST) \ - First##BASE##Constant=FIRST##Kind, Last##BASE##Constant=LAST##Kind -#define ABSTRACT_COMMENT(COMMENT) -#include "clang/AST/CommentNodes.inc" - }; - struct Argument { SourceRange Range; StringRef Text; @@ -203,7 +203,7 @@ class Comment { SourceLocation LocBegin, SourceLocation LocEnd) : Loc(LocBegin), Range(SourceRange(LocBegin, LocEnd)) { - CommentBits.Kind = K; + CommentBits.Kind = llvm::to_underlying(K); } CommentKind getCommentKind() const { @@ -249,8 +249,9 @@ class InlineContentComment : public Comment { public: static bool classof(const Comment *C) { - return C->getCommentKind() >= FirstInlineContentCommentConstant && - C->getCommentKind() <= LastInlineContentCommentConstant; + return C->getCommentKind() >= + CommentKind::FirstInlineContentCommentConstant && + C->getCommentKind() <= CommentKind::LastInlineContentCommentConstant; } void addTrailingNewline() { @@ -267,16 +268,14 @@ class TextComment : public InlineContentComment { StringRef Text; public: - TextComment(SourceLocation LocBegin, - SourceLocation LocEnd, - StringRef Text) : - InlineContentComment(TextCommentKind, LocBegin, LocEnd), - Text(Text) { + TextComment(SourceLocation LocBegin, SourceLocation LocEnd, StringRef Text) + : InlineContentComment(CommentKind::TextComment, LocBegin, LocEnd), + Text(Text) { TextCommentBits.IsWhitespaceValid = false; } static bool classof(const Comment *C) { - return C->getCommentKind() == TextCommentKind; + return C->getCommentKind() == CommentKind::TextComment; } child_iterator child_begin() const { return nullptr; } @@ -316,19 +315,18 @@ class InlineCommandComment : public InlineContentComment { ArrayRef<Argument> Args; public: - InlineCommandComment(SourceLocation LocBegin, - SourceLocation LocEnd, - unsigned CommandID, - RenderKind RK, - ArrayRef<Argument> Args) : - InlineContentComment(InlineCommandCommentKind, LocBegin, LocEnd), - Args(Args) { + InlineCommandComment(SourceLocation LocBegin, SourceLocation LocEnd, + unsigned CommandID, RenderKind RK, + ArrayRef<Argument> Args) + : InlineContentComment(CommentKind::InlineCommandComment, LocBegin, + LocEnd), + Args(Args) { InlineCommandCommentBits.RenderKind = RK; InlineCommandCommentBits.CommandID = CommandID; } static bool classof(const Comment *C) { - return C->getCommentKind() == InlineCommandCommentKind; + return C->getCommentKind() == CommentKind::InlineCommandComment; } child_iterator child_begin() const { return nullptr; } @@ -386,8 +384,8 @@ class HTMLTagComment : public InlineContentComment { public: static bool classof(const Comment *C) { - return C->getCommentKind() >= FirstHTMLTagCommentConstant && - C->getCommentKind() <= LastHTMLTagCommentConstant; + return C->getCommentKind() >= CommentKind::FirstHTMLTagCommentConstant && + C->getCommentKind() <= CommentKind::LastHTMLTagCommentConstant; } StringRef getTagName() const LLVM_READONLY { return TagName; } @@ -443,18 +441,16 @@ class HTMLStartTagComment : public HTMLTagComment { ArrayRef<Attribute> Attributes; public: - HTMLStartTagComment(SourceLocation LocBegin, - StringRef TagName) : - HTMLTagComment(HTMLStartTagCommentKind, - LocBegin, LocBegin.getLocWithOffset(1 + TagName.size()), - TagName, - LocBegin.getLocWithOffset(1), - LocBegin.getLocWithOffset(1 + TagName.size())) { + HTMLStartTagComment(SourceLocation LocBegin, StringRef TagName) + : HTMLTagComment(CommentKind::HTMLStartTagComment, LocBegin, + LocBegin.getLocWithOffset(1 + TagName.size()), TagName, + LocBegin.getLocWithOffset(1), + LocBegin.getLocWithOffset(1 + TagName.size())) { HTMLStartTagCommentBits.IsSelfClosing = false; } static bool classof(const Comment *C) { - return C->getCommentKind() == HTMLStartTagCommentKind; + return C->getCommentKind() == CommentKind::HTMLStartTagComment; } child_iterator child_begin() const { return nullptr; } @@ -498,18 +494,14 @@ class HTMLStartTagComment : public HTMLTagComment { /// A closing HTML tag. class HTMLEndTagComment : public HTMLTagComment { public: - HTMLEndTagComment(SourceLocation LocBegin, - SourceLocation LocEnd, - StringRef TagName) : - HTMLTagComment(HTMLEndTagCommentKind, - LocBegin, LocEnd, - TagName, - LocBegin.getLocWithOffset(2), - LocBegin.getLocWithOffset(2 + TagName.size())) - { } + HTMLEndTagComment(SourceLocation LocBegin, SourceLocation LocEnd, + StringRef TagName) + : HTMLTagComment(CommentKind::HTMLEndTagComment, LocBegin, LocEnd, + TagName, LocBegin.getLocWithOffset(2), + LocBegin.getLocWithOffset(2 + TagName.size())) {} static bool classof(const Comment *C) { - return C->getCommentKind() == HTMLEndTagCommentKind; + return C->getCommentKind() == CommentKind::HTMLEndTagComment; } child_iterator child_begin() const { return nullptr; } @@ -529,8 +521,9 @@ class BlockContentComment : public Comment { public: static bool classof(const Comment *C) { - return C->getCommentKind() >= FirstBlockContentCommentConstant && - C->getCommentKind() <= LastBlockContentCommentConstant; + return C->getCommentKind() >= + CommentKind::FirstBlockContentCommentConstant && + C->getCommentKind() <= CommentKind::LastBlockContentCommentConstant; } }; @@ -539,11 +532,10 @@ class ParagraphComment : public BlockContentComment { ArrayRef<InlineContentComment *> Content; public: - ParagraphComment(ArrayRef<InlineContentComment *> Content) : - BlockContentComment(ParagraphCommentKind, - SourceLocation(), - SourceLocation()), - Content(Content) { + ParagraphComment(ArrayRef<InlineContentComment *> Content) + : BlockContentComment(CommentKind::ParagraphComment, SourceLocation(), + SourceLocation()), + Content(Content) { if (Content.empty()) { ParagraphCommentBits.IsWhitespace = true; ParagraphCommentBits.IsWhitespaceValid = true; @@ -558,7 +550,7 @@ class ParagraphComment : public BlockContentComment { } static bool classof(const Comment *C) { - return C->getCommentKind() == ParagraphCommentKind; + return C->getCommentKind() == CommentKind::ParagraphComment; } child_iterator child_begin() const { @@ -606,20 +598,19 @@ class BlockCommandComment : public BlockContentComment { } public: - BlockCommandComment(SourceLocation LocBegin, - SourceLocation LocEnd, - unsigned CommandID, - CommandMarkerKind CommandMarker) : - BlockContentComment(BlockCommandCommentKind, LocBegin, LocEnd), - Paragraph(nullptr) { + BlockCommandComment(SourceLocation LocBegin, SourceLocation LocEnd, + unsigned CommandID, CommandMarkerKind CommandMarker) + : BlockContentComment(CommentKind::BlockCommandComment, LocBegin, LocEnd), + Paragraph(nullptr) { setLocation(getCommandNameBeginLoc()); BlockCommandCommentBits.CommandID = CommandID; BlockCommandCommentBits.CommandMarker = CommandMarker; } static bool classof(const Comment *C) { - return C->getCommentKind() >= FirstBlockCommandCommentConstant && - C->getCommentKind() <= LastBlockCommandCommentConstant; + return C->getCommentKind() >= + CommentKind::FirstBlockCommandCommentConstant && + C->getCommentKind() <= CommentKind::LastBlockCommandCommentConstant; } child_iterator child_begin() const { @@ -702,19 +693,17 @@ class ParamCommandComment : public BlockCommandComment { VarArgParamIndex = ~0U/*InvalidParamIndex*/ - 1U }; - ParamCommandComment(SourceLocation LocBegin, - SourceLocation LocEnd, - unsigned CommandID, - CommandMarkerKind CommandMarker) : - BlockCommandComment(ParamCommandCommentKind, LocBegin, LocEnd, - CommandID, CommandMarker), - ParamIndex(InvalidParamIndex) { + ParamCommandComment(SourceLocation LocBegin, SourceLocation LocEnd, + unsigned CommandID, CommandMarkerKind CommandMarker) + : BlockCommandComment(CommentKind::ParamCommandComment, LocBegin, LocEnd, + CommandID, CommandMarker), + ParamIndex(InvalidParamIndex) { ParamCommandCommentBits.Direction = In; ParamCommandCommentBits.IsDirectionExplicit = false; } static bool classof(const Comment *C) { - return C->getCommentKind() == ParamCommandCommentKind; + return C->getCommentKind() == CommentKind::ParamCommandComment; } enum PassDirection { @@ -796,16 +785,13 @@ class TParamCommandComment : public BlockCommandComment { ArrayRef<unsigned> Position; public: - TParamCommandComment(SourceLocation LocBegin, - SourceLocation LocEnd, - unsigned CommandID, - CommandMarkerKind CommandMarker) : - BlockCommandComment(TParamCommandCommentKind, LocBegin, LocEnd, CommandID, - CommandMarker) - { } + TParamCommandComment(SourceLocation LocBegin, SourceLocation LocEnd, + unsigned CommandID, CommandMarkerKind CommandMarker) + : BlockCommandComment(CommentKind::TParamCommandComment, LocBegin, LocEnd, + CommandID, CommandMarker) {} static bool classof(const Comment *C) { - return C->getCommentKind() == TParamCommandCommentKind; + return C->getCommentKind() == CommentKind::TParamCommandComment; } bool hasParamName() const { @@ -847,16 +833,13 @@ class VerbatimBlockLineComment : public Comment { StringRef Text; public: - VerbatimBlockLineComment(SourceLocation LocBegin, - StringRef Text) : - Comment(VerbatimBlockLineCommentKind, - LocBegin, - LocBegin.getLocWithOffset(Text.size())), - Text(Text) - { } + VerbatimBlockLineComment(SourceLocation LocBegin, StringRef Text) + : Comment(CommentKind::VerbatimBlockLineComment, LocBegin, + LocBegin.getLocWithOffset(Text.size())), + Text(Text) {} static bool classof(const Comment *C) { - return C->getCommentKind() == VerbatimBlockLineCommentKind; + return C->getCommentKind() == CommentKind::VerbatimBlockLineComment; } child_iterator child_begin() const { return nullptr; } @@ -878,16 +861,15 @@ class VerbatimBlockComment : public BlockCommandComment { ArrayRef<VerbatimBlockLineComment *> Lines; public: - VerbatimBlockComment(SourceLocation LocBegin, - SourceLocation LocEnd, - unsigned CommandID) : - BlockCommandComment(VerbatimBlockCommentKind, - LocBegin, LocEnd, CommandID, - CMK_At) // FIXME: improve source fidelity. - { } + VerbatimBlockComment(SourceLocation LocBegin, SourceLocation LocEnd, + unsigned CommandID) + : BlockCommandComment(CommentKind::VerbatimBlockComment, LocBegin, LocEnd, + CommandID, + CMK_At) // FIXME: improve source fidelity. + {} static bool classof(const Comment *C) { - return C->getCommentKind() == VerbatimBlockCommentKind; + return C->getCommentKind() == CommentKind::VerbatimBlockComment; } child_iterator child_begin() const { @@ -929,21 +911,16 @@ class VerbatimLineComment : public BlockCommandComment { SourceLocation TextBegin; public: - VerbatimLineComment(SourceLocation LocBegin, - SourceLocation LocEnd, - unsigned CommandID, - SourceLocation TextBegin, - StringRef Text) : - BlockCommandComment(VerbatimLineCommentKind, - LocBegin, LocEnd, - CommandID, - CMK_At), // FIXME: improve source fidelity. - Text(Text), - TextBegin(TextBegin) - { } + VerbatimLineComment(SourceLocation LocBegin, SourceLocation LocEnd, + unsigned CommandID, SourceLocation TextBegin, + StringRef Text) + : BlockCommandComment(CommentKind::VerbatimLineComment, LocBegin, LocEnd, + CommandID, + CMK_At), // FIXME: improve source fidelity. + Text(Text), TextBegin(TextBegin) {} static bool classof(const Comment *C) { - return C->getCommentKind() == VerbatimLineCommentKind; + return C->getCommentKind() == CommentKind::VerbatimLineComment; } child_iterator child_begin() const { return nullptr; } @@ -1079,9 +1056,9 @@ class FullComment : public Comment { DeclInfo *ThisDeclInfo; public: - FullComment(ArrayRef<BlockContentComment *> Blocks, DeclInfo *D) : - Comment(FullCommentKind, SourceLocation(), SourceLocation()), - Blocks(Blocks), ThisDeclInfo(D) { + FullComment(ArrayRef<BlockContentComment *> Blocks, DeclInfo *D) + : Comment(CommentKind::FullComment, SourceLocation(), SourceLocation()), + Blocks(Blocks), ThisDeclInfo(D) { if (Blocks.empty()) return; @@ -1091,7 +1068,7 @@ class FullComment : public Comment { } static bool classof(const Comment *C) { - return C->getCommentKind() == FullCommentKind; + return C->getCommentKind() == CommentKind::FullComment; } child_iterator child_begin() const { diff --git a/clang/include/clang/AST/CommentVisitor.h b/clang/include/clang/AST/CommentVisitor.h index d9a7439f7cc0554..bbb624a23e68cb7 100644 --- a/clang/include/clang/AST/CommentVisitor.h +++ b/clang/include/clang/AST/CommentVisitor.h @@ -31,8 +31,9 @@ class CommentVisitorBase { switch (C->getCommentKind()) { default: llvm_unreachable("Unknown comment kind!"); #define ABSTRACT_COMMENT(COMMENT) -#define COMMENT(CLASS, PARENT) \ - case Comment::CLASS##Kind: DISPATCH(CLASS, CLASS); +#define COMMENT(CLASS, PARENT) \ + case CommentKind::CLASS: \ + DISPATCH(CLASS, CLASS); #include "clang/AST/CommentNodes.inc" #undef ABSTRACT_COMMENT #undef COMMENT diff --git a/clang/lib/AST/Comment.cpp b/clang/lib/AST/Comment.cpp index 2bb6bb5cbcb67d4..0a2d310e5b48911 100644 --- a/clang/lib/AST/Comment.cpp +++ b/clang/lib/AST/Comment.cpp @@ -34,10 +34,11 @@ static_assert(std::is_trivially_destructible_v<DeclInfo>, const char *Comment::getCommentKindName() const { switch (getCommentKind()) { - case NoCommentKind: return "NoCommentKind"; + case CommentKind::None: + return "None"; #define ABSTRACT_COMMENT(COMMENT) -#define COMMENT(CLASS, PARENT) \ - case CLASS##Kind: \ +#define COMMENT(CLASS, PARENT) \ + case CommentKind::CLASS: \ return #CLASS; #include "clang/AST/CommentNodes.inc" #undef COMMENT @@ -81,10 +82,11 @@ static inline void CheckCommentASTNodes() { Comment::child_iterator Comment::child_begin() const { switch (getCommentKind()) { - case NoCommentKind: llvm_unreachable("comment without a kind"); + case CommentKind::None: + llvm_unreachable("comment without a kind"); #define ABSTRACT_COMMENT(COMMENT) -#define COMMENT(CLASS, PARENT) \ - case CLASS##Kind: \ +#define COMMENT(CLASS, PARENT) \ + case CommentKind::CLASS: \ return static_cast<const CLASS *>(this)->child_begin(); #include "clang/AST/CommentNodes.inc" #undef COMMENT @@ -95,10 +97,11 @@ Comment::child_iterator Comment::child_begin() const { Comment::child_iterator Comment::child_end() const { switch (getCommentKind()) { - case NoCommentKind: llvm_unreachable("comment without a kind"); + case CommentKind::None: + llvm_unreachable("comment without a kind"); #define ABSTRACT_COMMENT(COMMENT) -#define COMMENT(CLASS, PARENT) \ - case CLASS##Kind: \ +#define COMMENT(CLASS, PARENT) \ + case CommentKind::CLASS: \ return static_cast<const CLASS *>(this)->child_end(); #include "clang/AST/CommentNodes.inc" #undef COMMENT diff --git a/clang/lib/Index/CommentToXML.cpp b/clang/lib/Index/CommentToXML.cpp index f599e83aeca7093..ddd3498f92718e7 100644 --- a/clang/lib/Index/CommentToXML.cpp +++ b/clang/lib/Index/CommentToXML.cpp @@ -103,10 +103,10 @@ FullCommentParts::FullCommentParts(const FullComment *C, if (!Child) continue; switch (Child->getCommentKind()) { - case Comment::NoCommentKind: + case CommentKind::None: continue; - case Comment::ParagraphCommentKind: { + case CommentKind::ParagraphComment: { const ParagraphComment *PC = cast<ParagraphComment>(Child); if (PC->isWhitespace()) break; @@ -117,7 +117,7 @@ FullCommentParts::FullCommentParts(const FullComment *C, break; } - case Comment::BlockCommandCommentKind: { + case CommentKind::BlockCommandComment: { const BlockCommandComment *BCC = cast<BlockCommandComment>(Child); const CommandInfo *Info = Traits.getCommandInfo(BCC->getCommandID()); if (!Brief && Info->IsBriefCommand) { @@ -140,7 +140,7 @@ FullCommentParts::FullCommentParts(const FullComment *C, break; } - case Comment::ParamCommandCommentKind: { + case CommentKind::ParamCommandComment: { const ParamCommandComment *PCC = cast<ParamCommandComment>(Child); if (!PCC->hasParamName()) break; @@ -152,7 +152,7 @@ FullCommentParts::FullCommentParts(const FullComment *C, break; } - case Comment::TParamCommandCommentKind: { + case CommentKind::TParamCommandComment: { const TParamCommandComment *TPCC = cast<TParamCommandComment>(Child); if (!TPCC->hasParamName()) break; @@ -164,11 +164,11 @@ FullCommentParts::FullCommentParts(const FullComment *C, break; } - case Comment::VerbatimBlockCommentKind: + case CommentKind::VerbatimBlockComment: MiscBlocks.push_back(cast<BlockCommandComment>(Child)); break; - case Comment::VerbatimLineCommentKind: { + case CommentKind::VerbatimLineComment: { const VerbatimLineComment *VLC = cast<VerbatimLineComment>(Child); const CommandInfo *Info = Traits.getCommandInfo(VLC->getCommandID()); if (!Info->IsDeclarationCommand) @@ -176,12 +176,12 @@ FullCommentParts::FullCommentParts(const FullComment *C, break; } - case Comment::TextCommentKind: - case Comment::InlineCommandCommentKind: - case Comment::HTMLStartTagCommentKind: - case Comment::HTMLEndTagCommentKind: - case Comment::VerbatimBlockLineCommentKind: - case Comment::FullCommentKind: + case CommentKind::TextComment: + case CommentKind::InlineCommandComment: + case CommentKind::HTMLStartTagComment: + case CommentKind::HTMLEndTagComment: + case CommentKind::VerbatimBlockLineComment: + case CommentKind::FullComment: llvm_unreachable("AST node of this kind can't be a child of " "a FullComment"); } diff --git a/clang/tools/libclang/CXComment.cpp b/clang/tools/libclang/CXComment.cpp index bafaeb0fd00c5fa..79b42ae0c00120f 100644 --- a/clang/tools/libclang/CXComment.cpp +++ b/clang/tools/libclang/CXComment.cpp @@ -44,43 +44,43 @@ enum CXCommentKind clang_Comment_getKind(CXComment CXC) { return CXComment_Null; switch (C->getCommentKind()) { - case Comment::NoCommentKind: + case CommentKind::None: return CXComment_Null; - case Comment::TextCommentKind: + case CommentKind::TextComment: return CXComment_Text; - case Comment::InlineCommandCommentKind: + case CommentKind::InlineCommandComment: return CXComment_InlineCommand; - case Comment::HTMLStartTagCommentKind: + case CommentKind::HTMLStartTagComment: return CXComment_HTMLStartTag; - case Comment::HTMLEndTagCommentKind: + case CommentKind::HTMLEndTagComment: return CXComment_HTMLEndTag; - case Comment::ParagraphCommentKind: + case CommentKind::ParagraphComment: return CXComment_Paragraph; - case Comment::BlockCommandCommentKind: + case CommentKind::BlockCommandComment: return CXComment_BlockCommand; - case Comment::ParamCommandCommentKind: + case CommentKind::ParamCommandComment: return CXComment_ParamCommand; - case Comment::TParamCommandCommentKind: + case CommentKind::TParamCommandComment: return CXComment_TParamCommand; - case Comment::VerbatimBlockCommentKind: + case CommentKind::VerbatimBlockComment: return CXComment_VerbatimBlockCommand; - case Comment::VerbatimBlockLineCommentKind: + case CommentKind::VerbatimBlockLineComment: return CXComment_VerbatimBlockLine; - case Comment::VerbatimLineCommentKind: + case CommentKind::VerbatimLineComment: return CXComment_VerbatimLine; - case Comment::FullCommentKind: + case CommentKind::FullComment: return CXComment_FullComment; } llvm_unreachable("unknown CommentKind"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits