-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[llvm][mustache] Refactor template rendering #159189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Sep 16, 2025
This was referenced Sep 16, 2025
@llvm/pr-subscribers-llvm-support Author: Paul Kirth (ilovepi) ChangesMove the rendering logic into the ASTNode, and break the logic down into Full diff: https://github.com/llvm/llvm-project/pull/159189.diff 1 Files Affected:
diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index 08317a7b98902..e2de7645e8dfb 100644
--- a/llvm/lib/Support/Mustache.cpp
+++ b/llvm/lib/Support/Mustache.cpp
@@ -181,6 +181,14 @@ class ASTNode {
const llvm::json::Value *findContext();
+ void renderRoot(const json::Value &CurrentCtx, raw_ostream &OS);
+ void renderText(raw_ostream &OS);
+ void renderPartial(const json::Value &CurrentCtx, raw_ostream &OS);
+ void renderVariable(const json::Value &CurrentCtx, raw_ostream &OS);
+ void renderUnescapeVariable(const json::Value &CurrentCtx, raw_ostream &OS);
+ void renderSection(const json::Value &CurrentCtx, raw_ostream &OS);
+ void renderInvertSection(const json::Value &CurrentCtx, raw_ostream &OS);
+
StringMap<AstPtr> &Partials;
StringMap<Lambda> &Lambdas;
StringMap<SectionLambda> &SectionLambdas;
@@ -198,26 +206,26 @@ class ASTNode {
// A wrapper for arena allocator for ASTNodes
static AstPtr createRootNode(llvm::StringMap<AstPtr> &Partials,
- llvm::StringMap<Lambda> &Lambdas,
- llvm::StringMap<SectionLambda> &SectionLambdas,
- EscapeMap &Escapes) {
+ llvm::StringMap<Lambda> &Lambdas,
+ llvm::StringMap<SectionLambda> &SectionLambdas,
+ EscapeMap &Escapes) {
return std::make_unique<ASTNode>(Partials, Lambdas, SectionLambdas, Escapes);
}
static AstPtr createNode(ASTNode::Type T, Accessor A, ASTNode *Parent,
- llvm::StringMap<AstPtr> &Partials,
- llvm::StringMap<Lambda> &Lambdas,
- llvm::StringMap<SectionLambda> &SectionLambdas,
- EscapeMap &Escapes) {
+ llvm::StringMap<AstPtr> &Partials,
+ llvm::StringMap<Lambda> &Lambdas,
+ llvm::StringMap<SectionLambda> &SectionLambdas,
+ EscapeMap &Escapes) {
return std::make_unique<ASTNode>(T, std::move(A), Parent, Partials, Lambdas,
SectionLambdas, Escapes);
}
static AstPtr createTextNode(std::string Body, ASTNode *Parent,
- llvm::StringMap<AstPtr> &Partials,
- llvm::StringMap<Lambda> &Lambdas,
- llvm::StringMap<SectionLambda> &SectionLambdas,
- EscapeMap &Escapes) {
+ llvm::StringMap<AstPtr> &Partials,
+ llvm::StringMap<Lambda> &Lambdas,
+ llvm::StringMap<SectionLambda> &SectionLambdas,
+ EscapeMap &Escapes) {
return std::make_unique<ASTNode>(std::move(Body), Parent, Partials, Lambdas,
SectionLambdas, Escapes);
}
@@ -295,7 +303,7 @@ static void stripTokenAhead(SmallVectorImpl<Token> &Tokens, size_t Idx) {
// The exception for this is partial tag which requires us to
// keep track of the indentation once it's rendered.
static void stripTokenBefore(SmallVectorImpl<Token> &Tokens, size_t Idx,
- Token &CurrentToken, Token::Type CurrentType) {
+ Token &CurrentToken, Token::Type CurrentType) {
Token &PrevToken = Tokens[Idx - 1];
StringRef PrevTokenBody = PrevToken.TokenBody;
StringRef Unindented = PrevTokenBody.rtrim(" \r\t\v");
@@ -676,76 +684,96 @@ static void toMustacheString(const json::Value &Data, raw_ostream &OS) {
}
}
+void ASTNode::renderRoot(const json::Value &CurrentCtx, raw_ostream &OS) {
+ renderChild(CurrentCtx, OS);
+}
+
+void ASTNode::renderText(raw_ostream &OS) { OS << Body; }
+
+void ASTNode::renderPartial(const json::Value &CurrentCtx, raw_ostream &OS) {
+ auto Partial = Partials.find(AccessorValue[0]);
+ if (Partial != Partials.end())
+ renderPartial(CurrentCtx, OS, Partial->getValue().get());
+}
+
+void ASTNode::renderVariable(const json::Value &CurrentCtx, raw_ostream &OS) {
+ auto Lambda = Lambdas.find(AccessorValue[0]);
+ if (Lambda != Lambdas.end()) {
+ renderLambdas(CurrentCtx, OS, Lambda->getValue());
+ } else if (const json::Value *ContextPtr = findContext()) {
+ EscapeStringStream ES(OS, Escapes);
+ toMustacheString(*ContextPtr, ES);
+ }
+}
+
+void ASTNode::renderUnescapeVariable(const json::Value &CurrentCtx,
+ raw_ostream &OS) {
+ auto Lambda = Lambdas.find(AccessorValue[0]);
+ if (Lambda != Lambdas.end()) {
+ renderLambdas(CurrentCtx, OS, Lambda->getValue());
+ } else if (const json::Value *ContextPtr = findContext()) {
+ toMustacheString(*ContextPtr, OS);
+ }
+}
+
+void ASTNode::renderSection(const json::Value &CurrentCtx, raw_ostream &OS) {
+ auto SectionLambda = SectionLambdas.find(AccessorValue[0]);
+ if (SectionLambda != SectionLambdas.end()) {
+ renderSectionLambdas(CurrentCtx, OS, SectionLambda->getValue());
+ return;
+ }
+
+ const json::Value *ContextPtr = findContext();
+ if (isContextFalsey(ContextPtr))
+ return;
+
+ if (const json::Array *Arr = ContextPtr->getAsArray()) {
+ for (const json::Value &V : *Arr)
+ renderChild(V, OS);
+ return;
+ }
+ renderChild(*ContextPtr, OS);
+}
+
+void ASTNode::renderInvertSection(const json::Value &CurrentCtx,
+ raw_ostream &OS) {
+ bool IsLambda = SectionLambdas.contains(AccessorValue[0]);
+ const json::Value *ContextPtr = findContext();
+ if (isContextFalsey(ContextPtr) && !IsLambda) {
+ renderChild(CurrentCtx, OS);
+ }
+}
+
void ASTNode::render(const json::Value &CurrentCtx, raw_ostream &OS) {
if (Ty != Root && Ty != Text && AccessorValue.empty())
return;
// Set the parent context to the incoming context so that we
// can walk up the context tree correctly in findContext().
ParentContext = &CurrentCtx;
- const json::Value *ContextPtr = Ty == Root ? ParentContext : findContext();
switch (Ty) {
case Root:
- renderChild(CurrentCtx, OS);
+ renderRoot(CurrentCtx, OS);
return;
case Text:
- OS << Body;
+ renderText(OS);
return;
- case Partial: {
- auto Partial = Partials.find(AccessorValue[0]);
- if (Partial != Partials.end())
- renderPartial(CurrentCtx, OS, Partial->getValue().get());
+ case Partial:
+ renderPartial(CurrentCtx, OS);
return;
- }
- case Variable: {
- auto Lambda = Lambdas.find(AccessorValue[0]);
- if (Lambda != Lambdas.end()) {
- renderLambdas(CurrentCtx, OS, Lambda->getValue());
- } else if (ContextPtr) {
- EscapeStringStream ES(OS, Escapes);
- toMustacheString(*ContextPtr, ES);
- }
+ case Variable:
+ renderVariable(CurrentCtx, OS);
return;
- }
- case UnescapeVariable: {
- auto Lambda = Lambdas.find(AccessorValue[0]);
- if (Lambda != Lambdas.end()) {
- renderLambdas(CurrentCtx, OS, Lambda->getValue());
- } else if (ContextPtr) {
- toMustacheString(*ContextPtr, OS);
- }
+ case UnescapeVariable:
+ renderUnescapeVariable(CurrentCtx, OS);
return;
- }
- case Section: {
- auto SectionLambda = SectionLambdas.find(AccessorValue[0]);
- bool IsLambda = SectionLambda != SectionLambdas.end();
-
- if (IsLambda) {
- renderSectionLambdas(CurrentCtx, OS, SectionLambda->getValue());
- return;
- }
-
- if (isContextFalsey(ContextPtr))
- return;
-
- if (const json::Array *Arr = ContextPtr->getAsArray()) {
- for (const json::Value &V : *Arr)
- renderChild(V, OS);
- return;
- }
- renderChild(*ContextPtr, OS);
+ case Section:
+ renderSection(CurrentCtx, OS);
return;
- }
- case InvertSection: {
- bool IsLambda = SectionLambdas.contains(AccessorValue[0]);
- if (isContextFalsey(ContextPtr) && !IsLambda) {
- // The context for the children remains unchanged from the parent's, so
- // we pass this node's original incoming context.
- renderChild(CurrentCtx, OS);
- }
+ case InvertSection:
+ renderInvertSection(CurrentCtx, OS);
return;
}
- }
llvm_unreachable("Invalid ASTNode type");
}
|
This was referenced Sep 16, 2025
c8b70b1
to
6f78e4d
Compare
8175095
to
61bcc58
Compare
6f78e4d
to
97182cf
Compare
61bcc58
to
6a7db43
Compare
97182cf
to
7a0b305
Compare
6a7db43
to
3d00de1
Compare
7a0b305
to
8be25f4
Compare
3d00de1
to
f18412d
Compare
evelez7
approved these changes
Sep 29, 2025
8be25f4
to
289f098
Compare
7ca57bb
to
af8b255
Compare
289f098
to
41ea349
Compare
457c8d5
to
0693f63
Compare
af8b255
to
26f0197
Compare
Move the rendering logic into the ASTNode, and break the logic down into individual methods.
26f0197
to
16f12ac
Compare
mahesh-attarde
pushed a commit
to mahesh-attarde/llvm-project
that referenced
this pull request
Oct 3, 2025
Move the rendering logic into the ASTNode, and break the logic down into individual methods.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Move the rendering logic into the ASTNode, and break the logic down into
individual methods.