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

Paul Kirth via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 26 10:57:27 PDT 2025


================
@@ -681,10 +685,59 @@ 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 FinalNode) {
+  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 {
+      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;
+    }
----------------
ilovepi wrote:

hmm, actually, since you're looping over a bunch of things, the unreachable may not work unless you switch `break` to `continue`.

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


More information about the llvm-commits mailing list