[clang-tools-extra] [clang-doc] Add Markdown AST node type definitions (PR #205609)
Paul Kirth via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 28 10:16:38 PDT 2026
================
@@ -0,0 +1,177 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Markdown.h"
+#include "llvm/Support/Casting.h"
+
+namespace clang::doc::markdown {
+
+//===----------------------------------------------------------------------===//
+// Inline node print/dump
+//===----------------------------------------------------------------------===//
+
+void InlineNode::print(llvm::raw_ostream &OS) const {
+ switch (Kind) {
+ case NodeKind::NK_Text:
+ llvm::cast<TextNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_InlineCode:
+ llvm::cast<InlineCodeNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_Emphasis:
+ llvm::cast<EmphasisNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_Strong:
+ llvm::cast<StrongNode>(this)->print(OS);
+ break;
+ default:
+ OS << "UnknownInlineNode\n";
+ break;
+ }
+}
+
+LLVM_DUMP_METHOD void InlineNode::dump() const { print(llvm::errs()); }
+
+void TextNode::print(llvm::raw_ostream &OS) const {
+ OS << "TextNode: " << getText() << "\n";
+}
+
+LLVM_DUMP_METHOD void TextNode::dump() const { print(llvm::errs()); }
+
+void InlineCodeNode::print(llvm::raw_ostream &OS) const {
+ OS << "InlineCodeNode: " << getCode() << "\n";
+}
+
+LLVM_DUMP_METHOD void InlineCodeNode::dump() const { print(llvm::errs()); }
+
+void EmphasisNode::print(llvm::raw_ostream &OS) const {
+ OS << "EmphasisNode\n";
+ for (const auto &Child : Children)
+ Child.print(OS);
+}
+
+LLVM_DUMP_METHOD void EmphasisNode::dump() const { print(llvm::errs()); }
+
+void StrongNode::print(llvm::raw_ostream &OS) const {
+ OS << "StrongNode\n";
+ for (const auto &Child : Children)
+ Child.print(OS);
+}
+
+LLVM_DUMP_METHOD void StrongNode::dump() const { print(llvm::errs()); }
+
+//===----------------------------------------------------------------------===//
+// Block node print/dump
+//===----------------------------------------------------------------------===//
+
+void BlockNode::print(llvm::raw_ostream &OS) const {
+ switch (Kind) {
+ case NodeKind::NK_Paragraph:
+ llvm::cast<ParagraphNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_Heading:
+ llvm::cast<HeadingNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_FencedCode:
+ llvm::cast<FencedCodeNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_UnorderedList:
+ llvm::cast<UnorderedListNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_OrderedList:
+ llvm::cast<OrderedListNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_ListItem:
+ llvm::cast<ListItemNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_BlockQuote:
+ llvm::cast<BlockQuoteNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_ThematicBreak:
+ llvm::cast<ThematicBreakNode>(this)->print(OS);
+ break;
+ case NodeKind::NK_Document:
+ llvm::cast<DocumentNode>(this)->print(OS);
+ break;
+ default:
+ OS << "UnknownBlockNode\n";
+ break;
----------------
ilovepi wrote:
please omit the default case in switches like this. We use the `enum class` because it will break -Werror` builds if the enum isn't covered fully. Many other places in llvm use `llvm_unreachable` after the switch to be doubly sure (e..g. because they return instead of `break;`). I'd encourage you to take a look at other implementations throughout the broader codebase.
That said, its a bit questionable to have this kind of casting in the base classes.
https://github.com/llvm/llvm-project/pull/205609
More information about the cfe-commits
mailing list