Skip to content

[llvm][mustache][NFC] Use type alias for escape symbols #138050

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ilovepi
Copy link
Contributor

@ilovepi ilovepi commented Apr 30, 2025

This data structure's type and/or representation is likely to change.
Using an alias, makes it easy to refactor.

Copy link
Contributor Author

ilovepi commented Apr 30, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot
Copy link
Member

llvmbot commented Apr 30, 2025

@llvm/pr-subscribers-llvm-support

Author: Paul Kirth (ilovepi)

Changes

This data structure's type and/or representation is likely to change.
Using an alias, makes it easy to refactor.


Full diff: https://github.com/llvm/llvm-project/pull/138050.diff

1 Files Affected:

  • (modified) llvm/lib/Support/Mustache.cpp (+18-15)
diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index 2560619538f9a..f201e1c37bf94 100644
--- a/llvm/lib/Support/Mustache.cpp
+++ b/llvm/lib/Support/Mustache.cpp
@@ -110,6 +110,8 @@ class Token {
   size_t Indentation;
 };
 
+using EscapeMap = DenseMap<char, std::string>;
+
 class ASTNode {
 public:
   enum Type {
@@ -124,7 +126,7 @@ class ASTNode {
 
   ASTNode(llvm::StringMap<AstPtr> &Partials, llvm::StringMap<Lambda> &Lambdas,
           llvm::StringMap<SectionLambda> &SectionLambdas,
-          llvm::DenseMap<char, std::string> &Escapes)
+          EscapeMap &Escapes)
       : Partials(Partials), Lambdas(Lambdas), SectionLambdas(SectionLambdas),
         Escapes(Escapes), Ty(Type::Root), Parent(nullptr),
         ParentContext(nullptr) {}
@@ -132,7 +134,7 @@ class ASTNode {
   ASTNode(std::string Body, ASTNode *Parent, llvm::StringMap<AstPtr> &Partials,
           llvm::StringMap<Lambda> &Lambdas,
           llvm::StringMap<SectionLambda> &SectionLambdas,
-          llvm::DenseMap<char, std::string> &Escapes)
+          EscapeMap &Escapes)
       : Partials(Partials), Lambdas(Lambdas), SectionLambdas(SectionLambdas),
         Escapes(Escapes), Ty(Type::Text), Body(std::move(Body)), Parent(Parent),
         ParentContext(nullptr) {}
@@ -141,7 +143,7 @@ class ASTNode {
   ASTNode(Type Ty, Accessor Accessor, ASTNode *Parent,
           llvm::StringMap<AstPtr> &Partials, llvm::StringMap<Lambda> &Lambdas,
           llvm::StringMap<SectionLambda> &SectionLambdas,
-          llvm::DenseMap<char, std::string> &Escapes)
+          EscapeMap &Escapes)
       : Partials(Partials), Lambdas(Lambdas), SectionLambdas(SectionLambdas),
         Escapes(Escapes), Ty(Ty), Parent(Parent),
         AccessorValue(std::move(Accessor)), ParentContext(nullptr) {}
@@ -171,7 +173,7 @@ class ASTNode {
   StringMap<AstPtr> &Partials;
   StringMap<Lambda> &Lambdas;
   StringMap<SectionLambda> &SectionLambdas;
-  DenseMap<char, std::string> &Escapes;
+  EscapeMap &Escapes;
   Type Ty;
   size_t Indentation = 0;
   std::string RawBody;
@@ -187,7 +189,7 @@ class ASTNode {
 AstPtr createRootNode(llvm::StringMap<AstPtr> &Partials,
                       llvm::StringMap<Lambda> &Lambdas,
                       llvm::StringMap<SectionLambda> &SectionLambdas,
-                      llvm::DenseMap<char, std::string> &Escapes) {
+                      EscapeMap &Escapes) {
   return std::make_unique<ASTNode>(Partials, Lambdas, SectionLambdas, Escapes);
 }
 
@@ -195,7 +197,7 @@ AstPtr createNode(ASTNode::Type T, Accessor A, ASTNode *Parent,
                   llvm::StringMap<AstPtr> &Partials,
                   llvm::StringMap<Lambda> &Lambdas,
                   llvm::StringMap<SectionLambda> &SectionLambdas,
-                  llvm::DenseMap<char, std::string> &Escapes) {
+                  EscapeMap &Escapes) {
   return std::make_unique<ASTNode>(T, std::move(A), Parent, Partials, Lambdas,
                                    SectionLambdas, Escapes);
 }
@@ -204,7 +206,7 @@ AstPtr createTextNode(std::string Body, ASTNode *Parent,
                       llvm::StringMap<AstPtr> &Partials,
                       llvm::StringMap<Lambda> &Lambdas,
                       llvm::StringMap<SectionLambda> &SectionLambdas,
-                      llvm::DenseMap<char, std::string> &Escapes) {
+                      EscapeMap &Escapes) {
   return std::make_unique<ASTNode>(std::move(Body), Parent, Partials, Lambdas,
                                    SectionLambdas, Escapes);
 }
@@ -374,7 +376,7 @@ SmallVector<Token> tokenize(StringRef Template) {
 class EscapeStringStream : public raw_ostream {
 public:
   explicit EscapeStringStream(llvm::raw_ostream &WrappedStream,
-                              DenseMap<char, std::string> &Escape)
+                              EscapeMap &Escape)
       : Escape(Escape), WrappedStream(WrappedStream) {
     SetUnbuffered();
   }
@@ -394,7 +396,7 @@ class EscapeStringStream : public raw_ostream {
   uint64_t current_pos() const override { return WrappedStream.tell(); }
 
 private:
-  DenseMap<char, std::string> &Escape;
+  EscapeMap &Escape;
   llvm::raw_ostream &WrappedStream;
 };
 
@@ -433,13 +435,13 @@ class Parser {
   AstPtr parse(llvm::StringMap<AstPtr> &Partials,
                llvm::StringMap<Lambda> &Lambdas,
                llvm::StringMap<SectionLambda> &SectionLambdas,
-               llvm::DenseMap<char, std::string> &Escapes);
+               EscapeMap &Escapes);
 
 private:
   void parseMustache(ASTNode *Parent, llvm::StringMap<AstPtr> &Partials,
                      llvm::StringMap<Lambda> &Lambdas,
                      llvm::StringMap<SectionLambda> &SectionLambdas,
-                     llvm::DenseMap<char, std::string> &Escapes);
+                     EscapeMap &Escapes);
 
   SmallVector<Token> Tokens;
   size_t CurrentPtr;
@@ -449,7 +451,7 @@ class Parser {
 AstPtr Parser::parse(llvm::StringMap<AstPtr> &Partials,
                      llvm::StringMap<Lambda> &Lambdas,
                      llvm::StringMap<SectionLambda> &SectionLambdas,
-                     llvm::DenseMap<char, std::string> &Escapes) {
+                     EscapeMap &Escapes) {
   Tokens = tokenize(TemplateStr);
   CurrentPtr = 0;
   AstPtr RootNode = createRootNode(Partials, Lambdas, SectionLambdas, Escapes);
@@ -460,7 +462,7 @@ AstPtr Parser::parse(llvm::StringMap<AstPtr> &Partials,
 void Parser::parseMustache(ASTNode *Parent, llvm::StringMap<AstPtr> &Partials,
                            llvm::StringMap<Lambda> &Lambdas,
                            llvm::StringMap<SectionLambda> &SectionLambdas,
-                           llvm::DenseMap<char, std::string> &Escapes) {
+                           EscapeMap &Escapes) {
 
   while (CurrentPtr < Tokens.size()) {
     Token CurrentToken = Tokens[CurrentPtr];
@@ -726,7 +728,7 @@ void Template::registerLambda(std::string Name, SectionLambda L) {
   SectionLambdas[Name] = L;
 }
 
-void Template::overrideEscapeCharacters(DenseMap<char, std::string> E) {
+void Template::overrideEscapeCharacters(EscapeMap E) {
   Escapes = std::move(E);
 }
 
@@ -734,7 +736,8 @@ Template::Template(StringRef TemplateStr) {
   Parser P = Parser(TemplateStr);
   Tree = P.parse(Partials, Lambdas, SectionLambdas, Escapes);
   // The default behavior is to escape html entities.
-  DenseMap<char, std::string> HtmlEntities = {{'&', "&amp;"},
+  const
+  EscapeMap HtmlEntities = {{'&', "&amp;"},
                                               {'<', "&lt;"},
                                               {'>', "&gt;"},
                                               {'"', "&quot;"},

Copy link

github-actions bot commented Apr 30, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

This data structure's type and/or representation is likely to change.
Using an alias, makes it easy to refactor.
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-alias branch from 4c4b6e7 to 8470024 Compare May 1, 2025 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants