-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[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
ilovepi
wants to merge
1
commit into
main
Choose a base branch
from
users/ilovepi/mustache-alias
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
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
@llvm/pr-subscribers-llvm-support Author: Paul Kirth (ilovepi) ChangesThis data structure's type and/or representation is likely to change. Full diff: https://github.com/llvm/llvm-project/pull/138050.diff 1 Files Affected:
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 = {{'&', "&"},
+ const
+ EscapeMap HtmlEntities = {{'&', "&"},
{'<', "<"},
{'>', ">"},
{'"', """},
|
✅ 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.
4c4b6e7
to
8470024
Compare
petrhosek
approved these changes
May 2, 2025
el-ev
approved these changes
May 2, 2025
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.
This data structure's type and/or representation is likely to change.
Using an alias, makes it easy to refactor.