From 903069c55f7dc127d546960f1f33c02d79a060c3 Mon Sep 17 00:00:00 2001 From: Brock Denson Date: Thu, 14 Aug 2025 20:19:37 -0500 Subject: [PATCH 1/4] [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 Date: Fri, 15 Aug 2025 14:15:16 -0500 Subject: [PATCH 2/4] 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 Date: Fri, 29 Aug 2025 15:58:47 -0500 Subject: [PATCH 3/4] 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: From 9dba275ea7fd5111630a2889ce28aee102508690 Mon Sep 17 00:00:00 2001 From: Brock Denson Date: Fri, 29 Aug 2025 18:11:02 -0500 Subject: [PATCH 4/4] passing llvm-test-mustache-spec partial test --- llvm/lib/Support/Mustache.cpp | 9 +++++---- .../llvm-test-mustache-spec/llvm-test-mustache-spec.cpp | 1 - 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp index 46a791505b71e..40b39b803c40f 100644 --- a/llvm/lib/Support/Mustache.cpp +++ b/llvm/lib/Support/Mustache.cpp @@ -694,12 +694,13 @@ void ASTNode::indentTextNode(std::string &Body, size_t Indentation, if (FinalNode) LastChar = Body.find_last_not_of(" \t\r\f\v"); - while ((pos = Body.find('\n', pos)) != std::string::npos) { - if (FinalNode && (pos == LastChar)) + while ((Pos = Body.find('\n', Pos)) != std::string::npos) { + if (FinalNode && (Pos == LastChar)) break; - Body.insert(pos + 1, Spaces); - pos += 1 + Indentation; + Body.insert(Pos + 1, Spaces); + Pos += 1 + Indentation; + LastChar += Indentation; } } diff --git a/llvm/utils/llvm-test-mustache-spec/llvm-test-mustache-spec.cpp b/llvm/utils/llvm-test-mustache-spec/llvm-test-mustache-spec.cpp index 1f566e13f070a..a3d0f661ec36e 100644 --- a/llvm/utils/llvm-test-mustache-spec/llvm-test-mustache-spec.cpp +++ b/llvm/utils/llvm-test-mustache-spec/llvm-test-mustache-spec.cpp @@ -141,7 +141,6 @@ static const StringMap> XFailTestNames = {{ "Triple Mustache - Standalone", "Triple Mustache With Padding", }}, - {"partials.json", {"Standalone Indentation"}}, {"sections.json", {"Implicit Iterator - Triple mustache"}}, }};