[llvm] [llvm] fix mustache template whitespace (PR #153724)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 29 15:22:50 PDT 2025


https://github.com/mdenson updated https://github.com/llvm/llvm-project/pull/153724

>From 903069c55f7dc127d546960f1f33c02d79a060c3 Mon Sep 17 00:00:00 2001
From: Brock Denson <brock.denson at virscient.com>
Date: Thu, 14 Aug 2025 20:19:37 -0500
Subject: [PATCH 1/3] [clang-doc] fix mustache template whitespace

---
 llvm/lib/Support/Mustache.cpp | 55 +++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index 6c2ed6c84c6cf..205d9b51103c2 100644
--- a/llvm/lib/Support/Mustache.cpp
+++ b/llvm/lib/Support/Mustache.cpp
@@ -166,6 +166,10 @@ class ASTNode {
   void renderSectionLambdas(const llvm::json::Value &Contexts,
                             llvm::raw_ostream &OS, SectionLambda &L);
 
+  void indentTextNode(std::string &body, size_t Indentation, bool lastChild);
+
+  void indentNodes(ASTNode *Node, bool isPartial);
+
   void renderPartial(const llvm::json::Value &Contexts, llvm::raw_ostream &OS,
                      ASTNode *Partial);
 
@@ -681,10 +685,57 @@ void ASTNode::renderChild(const json::Value &Contexts, llvm::raw_ostream &OS) {
     Child->render(Contexts, OS);
 }
 
+void ASTNode::indentTextNode(std::string &body, size_t Indentation,
+                             bool lastChild) {
+  std::string spaces(Indentation, ' ');
+  size_t pos = 0;
+
+  if (lastChild)
+    body.erase(body.find_last_not_of(" \t\r\f\v") + 1); // .rtrim??
+
+  while ((pos = body.find('\n', pos)) != std::string::npos) {
+    if ((!lastChild) || (pos != body.size() - 1)) {
+      body.insert(pos + 1, spaces);
+      pos += 1 + Indentation;
+    } else {
+      break;
+    }
+  }
+}
+
+void ASTNode::indentNodes(ASTNode *Node, bool isPartial) {
+  size_t size = Node->Children.size();
+
+  for (size_t i = 0; i < size; ++i) {
+    ASTNode *child = Node->Children[i].get();
+    switch (child->Ty) {
+    case ASTNode::Text: {
+      indentTextNode(child->Body, Indentation, ((i == size - 1) && isPartial));
+      break;
+    }
+    case ASTNode::Section: {
+      indentNodes(child, false);
+      break;
+    }
+    case ASTNode::Partial: {
+      indentNodes(child, true);
+    }
+    case ASTNode::Root:
+    case ASTNode::Variable:
+    case ASTNode::UnescapeVariable:
+    case ASTNode::InvertSection:
+      break;
+    default:
+      llvm::outs() << "Invalid ASTNode type\n";
+      break;
+    }
+  }
+}
+
 void ASTNode::renderPartial(const json::Value &Contexts, llvm::raw_ostream &OS,
                             ASTNode *Partial) {
-  AddIndentationStringStream IS(OS, Indentation);
-  Partial->render(Contexts, IS);
+  indentNodes(Partial, true);
+  Partial->render(Contexts, OS);
 }
 
 void ASTNode::renderLambdas(const json::Value &Contexts, llvm::raw_ostream &OS,

>From 67c7a32d68e1062429887236a0f800aa014c8e4a Mon Sep 17 00:00:00 2001
From: Brock Denson <brock.denson at virscient.com>
Date: Fri, 15 Aug 2025 14:15:16 -0500
Subject: [PATCH 2/3] stop trimming partial whitespace

---
 llvm/lib/Support/Mustache.cpp | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index 205d9b51103c2..8e852cb6345f3 100644
--- a/llvm/lib/Support/Mustache.cpp
+++ b/llvm/lib/Support/Mustache.cpp
@@ -166,7 +166,7 @@ class ASTNode {
   void renderSectionLambdas(const llvm::json::Value &Contexts,
                             llvm::raw_ostream &OS, SectionLambda &L);
 
-  void indentTextNode(std::string &body, size_t Indentation, bool lastChild);
+  void indentTextNode(std::string &Body, size_t Indentation, bool FinalNode);
 
   void indentNodes(ASTNode *Node, bool isPartial);
 
@@ -685,17 +685,19 @@ void ASTNode::renderChild(const json::Value &Contexts, llvm::raw_ostream &OS) {
     Child->render(Contexts, OS);
 }
 
-void ASTNode::indentTextNode(std::string &body, size_t Indentation,
-                             bool lastChild) {
+void ASTNode::indentTextNode(std::string &Body, size_t Indentation,
+                             bool FinalNode) {
   std::string spaces(Indentation, ' ');
   size_t pos = 0;
+  size_t LastChar = std::string::npos;
 
-  if (lastChild)
-    body.erase(body.find_last_not_of(" \t\r\f\v") + 1); // .rtrim??
+  if (FinalNode)
+    // body.erase(body.find_last_not_of(" \t\r\f\v") + 1);
+    LastChar = Body.find_last_not_of(" \t\r\f\v");
 
-  while ((pos = body.find('\n', pos)) != std::string::npos) {
-    if ((!lastChild) || (pos != body.size() - 1)) {
-      body.insert(pos + 1, spaces);
+  while ((pos = Body.find('\n', pos)) != std::string::npos) {
+    if ((!FinalNode) || (pos != LastChar)) {
+      Body.insert(pos + 1, spaces);
       pos += 1 + Indentation;
     } else {
       break;

>From 353efef2e8cf36b63b2d49782762c79b8ea643a9 Mon Sep 17 00:00:00 2001
From: Brock Denson <mbdenson at gmail.com>
Date: Fri, 29 Aug 2025 15:58:47 -0500
Subject: [PATCH 3/3] Accept PR review changes less unreachable

---
 llvm/lib/Support/Mustache.cpp | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index 8e852cb6345f3..46a791505b71e 100644
--- a/llvm/lib/Support/Mustache.cpp
+++ b/llvm/lib/Support/Mustache.cpp
@@ -168,7 +168,7 @@ class ASTNode {
 
   void indentTextNode(std::string &Body, size_t Indentation, bool FinalNode);
 
-  void indentNodes(ASTNode *Node, bool isPartial);
+  void indentNodes(ASTNode *Node, bool IsPartial);
 
   void renderPartial(const llvm::json::Value &Contexts, llvm::raw_ostream &OS,
                      ASTNode *Partial);
@@ -687,40 +687,41 @@ void ASTNode::renderChild(const json::Value &Contexts, llvm::raw_ostream &OS) {
 
 void ASTNode::indentTextNode(std::string &Body, size_t Indentation,
                              bool FinalNode) {
-  std::string spaces(Indentation, ' ');
-  size_t pos = 0;
+  std::string Spaces(Indentation, ' ');
+  size_t Pos = 0;
   size_t LastChar = std::string::npos;
 
   if (FinalNode)
-    // body.erase(body.find_last_not_of(" \t\r\f\v") + 1);
     LastChar = Body.find_last_not_of(" \t\r\f\v");
 
   while ((pos = Body.find('\n', pos)) != std::string::npos) {
-    if ((!FinalNode) || (pos != LastChar)) {
-      Body.insert(pos + 1, spaces);
-      pos += 1 + Indentation;
-    } else {
+    if (FinalNode && (pos == LastChar))
       break;
-    }
+
+    Body.insert(pos + 1, Spaces);
+    pos += 1 + Indentation;
   }
 }
 
-void ASTNode::indentNodes(ASTNode *Node, bool isPartial) {
-  size_t size = Node->Children.size();
+void ASTNode::indentNodes(ASTNode *Node, bool IsPartial) {
+  size_t Size = Node->Children.size();
 
-  for (size_t i = 0; i < size; ++i) {
-    ASTNode *child = Node->Children[i].get();
-    switch (child->Ty) {
+  for (size_t i = 0; i < Size; ++i) {
+    ASTNode *Child = Node->Children[i].get();
+    switch (Child->Ty) {
     case ASTNode::Text: {
-      indentTextNode(child->Body, Indentation, ((i == size - 1) && isPartial));
+      // Only track the final node for partials.
+      bool IsFinalNode = ((i == Size - 1) && IsPartial);
+      indentTextNode(Child->Body, Indentation, IsFinalNode);
       break;
     }
     case ASTNode::Section: {
-      indentNodes(child, false);
+      indentNodes(Child, false);
       break;
     }
     case ASTNode::Partial: {
-      indentNodes(child, true);
+      indentNodes(Child, true);
+      break;
     }
     case ASTNode::Root:
     case ASTNode::Variable:



More information about the llvm-commits mailing list