[clang-tools-extra] [clang-doc] Add Markdown AST node type definitions (PR #205609)
Neil Nair via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 1 23:53:21 PDT 2026
================
@@ -0,0 +1,304 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Defines the Markdown AST node hierarchy for the clang-doc Markdown parser.
+///
+/// Block nodes represent structural constructs (paragraphs, headings, lists,
+/// fenced code blocks, etc). Inline nodes represent span-level content (text,
+/// emphasis, inline code) that appears inside block nodes.
+///
+/// All nodes are arena-allocated via ASTContext, which manages their lifetime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_SUPPORT_MARKDOWN_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_SUPPORT_MARKDOWN_H
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/simple_ilist.h"
+#include "llvm/Support/Casting.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace clang::doc::markdown {
+
+enum class NodeKind {
+ // Inline nodes
+ NK_Text,
+ NK_InlineCode,
+ NK_Emphasis,
+ NK_Strong,
+ // Block nodes
+ NK_Paragraph,
+ NK_Heading,
+ NK_FencedCode,
+ NK_Table, // TODO: add TableNode
+ NK_UnorderedList,
+ NK_OrderedList,
+ NK_BlockQuote,
+ NK_ThematicBreak,
+ NK_Document,
+};
+
+/// Base class for all inline nodes. Inline nodes represent span-level content
+/// such as text, emphasis, and inline code.
+class InlineNode : public llvm::ilist_node<InlineNode> {
+public:
+ explicit InlineNode(NodeKind K) : Kind(K) {}
+ virtual ~InlineNode() = default;
+ NodeKind getKind() const { return Kind; }
+ /// Recursively prints the node and its children to OS.
+ virtual void print(llvm::raw_ostream &OS) const = 0;
+ /// Prints to llvm::errs(). Only available in assert builds.
+ LLVM_DUMP_METHOD void dump() const;
+
+private:
+ NodeKind Kind;
+};
+
+using InlineList = llvm::simple_ilist<InlineNode>;
+
+/// A plain text run.
+class TextNode : public InlineNode {
+public:
+ explicit TextNode(llvm::StringRef T)
+ : InlineNode(NodeKind::NK_Text), Text(T) {}
+ llvm::StringRef getText() const { return Text; }
+ void print(llvm::raw_ostream &OS) const override;
+ static bool classof(const InlineNode *N) {
+ return N->getKind() == NodeKind::NK_Text;
+ }
+
+private:
+ llvm::StringRef Text;
+};
+
+/// A backtick-delimited inline code span.
+class InlineCodeNode : public InlineNode {
+public:
+ explicit InlineCodeNode(llvm::StringRef C)
+ : InlineNode(NodeKind::NK_InlineCode), Code(C) {}
+ llvm::StringRef getCode() const { return Code; }
+ void print(llvm::raw_ostream &OS) const override;
+ static bool classof(const InlineNode *N) {
+ return N->getKind() == NodeKind::NK_InlineCode;
+ }
+
+private:
+ llvm::StringRef Code;
+};
+
+/// An emphasis span (* or _).
+class EmphasisNode : public InlineNode {
+public:
+ EmphasisNode() : InlineNode(NodeKind::NK_Emphasis) {}
+ void addChild(InlineNode &N) { Children.push_back(N); }
+ void removeChild(InlineNode &N) { Children.remove(N); }
+ InlineList &children() { return Children; }
+ const InlineList &children() const { return Children; }
+ void print(llvm::raw_ostream &OS) const override;
+ static bool classof(const InlineNode *N) {
+ return N->getKind() == NodeKind::NK_Emphasis;
+ }
+
+private:
+ InlineList Children;
----------------
Neil-N4 wrote:
Discussed on the call with Erick and Petr, deferring until the parser shows the actual access patterns
https://github.com/llvm/llvm-project/pull/205609
More information about the cfe-commits
mailing list