grep: use grep_or_expr() in compile_pattern_or()
PATCH 1/2 grep: use grep_or_expr() in compile_pattern_or()
Move the definition of grep_or_expr() up and use this function in compile_pattern_or() to reduce code duplication.
--- grep.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-)
grep.c +11 -15
@@ -595,6 +595,15 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) } } +static struct grep_expr *grep_or_expr (struct grep_expr *left,struct grep_expr *right) +{ +struct grep_expr *z =xcalloc (1 ,sizeof (*z)); + z->node =GREP_NODE_OR ; + z->u.binary.left = left; + z->u.binary.right = right; +return z; +} +static struct grep_expr *compile_pattern_or (struct grep_pat **);static struct grep_expr *compile_pattern_atom (struct grep_pat **list) {
@@ -677,7 +686,7 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)static struct grep_expr *compile_pattern_or (struct grep_pat **list) {struct grep_pat *p; -struct grep_expr *x, *y, *z; +struct grep_expr *x, *y; x =compile_pattern_and (list); p = *list;
@@ -685,11 +694,7 @@ static struct grep_expr *compile_pattern_or(struct grep_pat **list) y =compile_pattern_or (list);if (!y)die ("not a pattern expression %s" , p->pattern); -CALLOC_ARRAY (z,1 ); - z->node =GREP_NODE_OR ; - z->u.binary.left = x; - z->u.binary.right = y; -return z; +return grep_or_expr (x, y); }return x; }
@@ -714,15 +719,6 @@ static struct grep_expr *grep_true_expr(void)return z; } -static struct grep_expr *grep_or_expr (struct grep_expr *left,struct grep_expr *right) -{ -struct grep_expr *z =xcalloc (1 ,sizeof (*z)); - z->node =GREP_NODE_OR ; - z->u.binary.left = left; - z->u.binary.right = right; -return z; -} -static struct grep_expr *prep_header_patterns (struct grep_opt *opt) {struct grep_pat *p;
--
2.34.1
PATCH 2/2 grep: use grep_not_expr() in compile_pattern_not()
Move the definition of grep_not_expr() up and use this function in compile_pattern_not() to simplify the code and reduce duplication.
--- grep.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)
grep.c +11 -13
@@ -595,6 +595,14 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) } } +static struct grep_expr *grep_not_expr (struct grep_expr *expr) +{ +struct grep_expr *z =xcalloc (1 ,sizeof (*z)); + z->node =GREP_NODE_NOT ; + z->u.unary = expr; +return z; +} +static struct grep_expr *grep_or_expr (struct grep_expr *left,struct grep_expr *right) {struct grep_expr *z =xcalloc (1 ,sizeof (*z));
@@ -647,12 +655,10 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)if (!p->next)die ("--not not followed by pattern expression" ); *list = p->next; -CALLOC_ARRAY (x,1 ); - x->node =GREP_NODE_NOT ; - x->u.unary =compile_pattern_not (list); -if (!x->u.unary) + x =compile_pattern_not (list); +if (!x)die ("--not followed by non pattern expression" ); -return x; +return grep_not_expr (x);default :return compile_pattern_atom (list); }
@@ -704,14 +710,6 @@ static struct grep_expr *compile_pattern_expr(struct grep_pat **list)return compile_pattern_or (list); } -static struct grep_expr *grep_not_expr (struct grep_expr *expr) -{ -struct grep_expr *z =xcalloc (1 ,sizeof (*z)); - z->node =GREP_NODE_NOT ; - z->u.unary = expr; -return z; -} -static struct grep_expr *grep_true_expr (void ) {struct grep_expr *z =xcalloc (1 ,sizeof (*z));
--
2.34.1
cover grep: introduce and use grep_and_expr()
In reply to René ScharfeRené,
Here are a couple of extra patches on top of your series which introduce and use a new grep_and_expr() function.
Like the final patch says, this isn't about reducing code duplication, but rather about adding visual consistency with the other compile_pattern_xyz() functions.
Taylor Blau (2):
grep: extract grep_binexp() from grep_or_expr()
grep: use grep_and_expr() in compile_pattern_and()
grep.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
--
2.34.1.455.gd6eb6fd089
PATCH 1/2 grep: extract grep_binexp() from grep_or_expr()
When constructing an OR node, the grep.c code uses grep_or_expr() to make a node, assign its kind, and set its left and right children. The same is not done for AND nodes.
Prepare to introduce a new grep_and_expr() function which will share code with the existing implementation of grep_or_expr() by introducing a new function which compiles either kind of binary expression, and reimplement grep_or_expr() in terms of it.
--- grep.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
grep.c +9 -2
@@ -603,15 +603,22 @@ static struct grep_expr *grep_not_expr(struct grep_expr *expr)return z; } -static struct grep_expr *grep_or_expr (struct grep_expr *left,struct grep_expr *right) +static struct grep_expr *grep_binexp (enum grep_expr_node kind, +struct grep_expr *left, +struct grep_expr *right) {struct grep_expr *z =xcalloc (1 ,sizeof (*z)); - z->node =GREP_NODE_OR ; + z->node = kind; z->u.binary.left = left; z->u.binary.right = right;return z; } +static struct grep_expr *grep_or_expr (struct grep_expr *left,struct grep_expr *right) +{ +return grep_binexp (GREP_NODE_OR , left, right); +} +static struct grep_expr *compile_pattern_or (struct grep_pat **);static struct grep_expr *compile_pattern_atom (struct grep_pat **list) {
Signature · 1 line
2.34.1.455.gd6eb6fd089
PATCH 2/2 grep: use grep_and_expr() in compile_pattern_and()
In reply to Taylor BlauIn a similar spirit as a previous commit, use the new grep_and_expr() to construct the AND node in compile_pattern_and().
Unlike the aforementioned previous commit, this is not about code duplication, since this is the only spot in grep.c where an AND node is constructed. Rather, this is about visual consistency with the other compile_pattern_xyz() functions.
--- grep.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
grep.c +6 -5
@@ -619,6 +619,11 @@ static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *return grep_binexp (GREP_NODE_OR , left, right); } +static struct grep_expr *grep_and_expr (struct grep_expr *left,struct grep_expr *right) +{ +return grep_binexp (GREP_NODE_AND , left, right); +} +static struct grep_expr *compile_pattern_or (struct grep_pat **);static struct grep_expr *compile_pattern_atom (struct grep_pat **list) {
@@ -687,11 +692,7 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list) y =compile_pattern_and (list);if (!y)die ("--and not followed by pattern expression" ); -CALLOC_ARRAY (z,1 ); - z->node =GREP_NODE_AND ; - z->u.binary.left = x; - z->u.binary.right = y; -return z; +return grep_and_expr (x, y); }return x; }
Signature · 1 line
2.34.1.455.gd6eb6fd089
Re: [PATCH 2/2] grep: use grep_and_expr() in compile_pattern_and()
Quoted 30 lines · Taylor Blau
In a similar spirit as a previous commit, use the new
grep_and_expr()to construct the AND node incompile_pattern_and().Unlike the aforementioned previous commit, this is not about code duplication, since this is the only spot in
grep.cwhere an AND node is constructed. Rather, this is about visual consistency with the othercompile_pattern_xyz()functions.Signed-off-by Taylor Blau <[email protected]>--- grep.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)grep.c +5
-0@@ -619,6 +619,11 @@ static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *return grep_binexp (GREP_NODE_OR , left, right); } +static struct grep_expr *grep_and_expr (struct grep_expr *left,struct grep_expr *right) +{ +return grep_binexp (GREP_NODE_AND , left, right); +} +static struct grep_expr *compile_pattern_or (struct grep_pat **);static struct grep_expr *compile_pattern_atom (struct grep_pat **list) {
grep.c static struct grep_expr *compile_pattern_and(struct grep_pat **list) · line 695
Quoted 9 lines
grep.c +1
-5@@ -687,11 +692,7 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list) y =compile_pattern_and (list);if (!y)die ("--and not followed by pattern expression" ); -CALLOC_ARRAY (z,1 ); - z->node =GREP_NODE_AND ; - z->u.binary.left = x; - z->u.binary.right = y; -return z; +return grep_and_expr (x, y);
You'd need to remove 'z' from the function to avoid getting yelled at by your compiler for unused variable.
Re: [PATCH 2/2] grep: use grep_and_expr() in compile_pattern_and()
Quoted 14 lines · Junio C Hamano
grep.c static struct grep_expr *compile_pattern_and(struct grep_pat **list) · line 695
Quoted 10 lines · Taylor Blau
grep.c +1
-5@@ -687,11 +692,7 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list) y =compile_pattern_and (list);if (!y)die ("--and not followed by pattern expression" ); -CALLOC_ARRAY (z,1 ); - z->node =GREP_NODE_AND ; - z->u.binary.left = x; - z->u.binary.right = y; -return z; +return grep_and_expr (x, y);You'd need to remove 'z' from the function to avoid getting yelled at by your compiler for unused variable.
How embarrassing :-). Thanks for noticing, I wrote this so hastily I neglected to even compile it with DEVELOPER=1.
The new version I sent in response fixes this issue.
Thanks,
Taylor
v2 cover grep: introduce and use grep_and_expr()
In reply to Taylor BlauThis small reroll fixes an issue in the second patch where a stack variable was left unused.
Taylor Blau (2):
grep: extract grep_binexp() from grep_or_expr()
grep: use grep_and_expr() in compile_pattern_and()
grep.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
Range-diff against v1:
-: ---------- > 1: dae476d1bd grep: extract grep_binexp() from grep_or_expr() 1: 71bd2d1bcc ! 2: f637e02422 grep: use grep_and_expr() in compile_pattern_and()
@@ grep.c: static struct grep_expr *grep_or_expr(struct grep_expr *left, struct gre
static struct grep_expr *compile_pattern_or(struct grep_pat **); static struct grep_expr *compile_pattern_atom(struct grep_pat **list) {
+@@ grep.c: static struct grep_expr *compile_pattern_not(struct grep_pat **list)
+ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
+ {
+ struct grep_pat *p;
+- struct grep_expr *x, *y, *z;
++ struct grep_expr *x, *y;
+
+ x = compile_pattern_not(list);
+ p = *list;
@@ grep.c: static struct grep_expr *compile_pattern_and(struct grep_pat **list)
y = compile_pattern_and(list);
if (!y)
--
2.34.1.455.gd6eb6fd089
PATCH v2 1/2 grep: extract grep_binexp() from grep_or_expr()
When constructing an OR node, the grep.c code uses grep_or_expr() to make a node, assign its kind, and set its left and right children. The same is not done for AND nodes.
Prepare to introduce a new grep_and_expr() function which will share code with the existing implementation of grep_or_expr() by introducing a new function which compiles either kind of binary expression, and reimplement grep_or_expr() in terms of it.
--- grep.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
grep.c +9 -2
@@ -603,15 +603,22 @@ static struct grep_expr *grep_not_expr(struct grep_expr *expr)return z; } -static struct grep_expr *grep_or_expr (struct grep_expr *left,struct grep_expr *right) +static struct grep_expr *grep_binexp (enum grep_expr_node kind, +struct grep_expr *left, +struct grep_expr *right) {struct grep_expr *z =xcalloc (1 ,sizeof (*z)); - z->node =GREP_NODE_OR ; + z->node = kind; z->u.binary.left = left; z->u.binary.right = right;return z; } +static struct grep_expr *grep_or_expr (struct grep_expr *left,struct grep_expr *right) +{ +return grep_binexp (GREP_NODE_OR , left, right); +} +static struct grep_expr *compile_pattern_or (struct grep_pat **);static struct grep_expr *compile_pattern_atom (struct grep_pat **list) {
Signature · 1 line
2.34.1.455.gd6eb6fd089
PATCH v2 2/2 grep: use grep_and_expr() in compile_pattern_and()
In reply to Taylor BlauIn a similar spirit as a previous commit, use the new grep_and_expr() to construct the AND node in compile_pattern_and().
Unlike the aforementioned previous commit, this is not about code duplication, since this is the only spot in grep.c where an AND node is constructed. Rather, this is about visual consistency with the other compile_pattern_xyz() functions.
--- grep.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
grep.c +7 -6
@@ -619,6 +619,11 @@ static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *return grep_binexp (GREP_NODE_OR , left, right); } +static struct grep_expr *grep_and_expr (struct grep_expr *left,struct grep_expr *right) +{ +return grep_binexp (GREP_NODE_AND , left, right); +} +static struct grep_expr *compile_pattern_or (struct grep_pat **);static struct grep_expr *compile_pattern_atom (struct grep_pat **list) {
@@ -674,7 +679,7 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)static struct grep_expr *compile_pattern_and (struct grep_pat **list) {struct grep_pat *p; -struct grep_expr *x, *y, *z; +struct grep_expr *x, *y; x =compile_pattern_not (list); p = *list;
@@ -687,11 +692,7 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list) y =compile_pattern_and (list);if (!y)die ("--and not followed by pattern expression" ); -CALLOC_ARRAY (z,1 ); - z->node =GREP_NODE_AND ; - z->u.binary.left = x; - z->u.binary.right = y; -return z; +return grep_and_expr (x, y); }return x; }
Signature · 1 line
2.34.1.455.gd6eb6fd089
Re: [PATCH 0/2] grep: introduce and use grep_and_expr()
In reply to Taylor BlauAm 06.01.22 um 20:50 schrieb Taylor Blau:
Quoted 11 lines
René,
Here are a couple of extra patches on top of your series which introduce and use a new
grep_and_expr()function.Like the final patch says, this isn't about reducing code duplication, but rather about adding visual consistency with the other
compile_pattern_xyz()functions.Taylor Blau (2):
grep: extractgrep_binexp()fromgrep_or_expr()
I considered extracting such a function as well. I'd have called it grep_binary_expr(), though, to match the existing names.
I decided against it because it can be misused by passing a non-binary kind to it. (That's a weak objection, but the benefit of such a function was low already in my mind because it doesn't do much.) You solve this by keeping grep_or_expr() and adding grep_and_expr(), which cannot be misused in this way -- OK.
Quoted 1 line
grep: use
grep_and_expr()incompile_pattern_and()
I think reversing the order of changes would make more sense. You wouldn't have to talk about the respective other patch in the commit messages -- each would stand on their own. Not worth a reroll, though.
Quoted 6 lines
grep.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)--
2.34.1.455.gd6eb6fd089
Re: [PATCH 0/2] grep: introduce and use grep_and_expr()
Quoted 13 lines · René Scharfe
Am 06.01.22 um 20:50 schrieb Taylor Blau:
Quoted 11 lines
René,
Here are a couple of extra patches on top of your series which introduce and use a new
grep_and_expr()function.Like the final patch says, this isn't about reducing code duplication, but rather about adding visual consistency with the other
compile_pattern_xyz()functions.Taylor Blau (2):
grep: extractgrep_binexp()fromgrep_or_expr()
Quoted 8 lines
I considered extracting such a function as well. I'd have called it
grep_binary_expr(), though, to match the existing names.I decided against it because it can be misused by passing a non-binary kind to it. (That's a weak objection, but the benefit of such a function was low already in my mind because it doesn't do much.) You solve this by keeping
grep_or_expr()and addinggrep_and_expr(), which cannot be misused in this way -- OK.
That makes sense. If it's keeping you up at night, we could easily add a check to ensure that kind is one of GREP_NODE_OR or GREP_NODE_AND. But I think that any new code that looks like:
grep_binexp(GREP_NODE_NOT, xyz, NULL);
would probably stick out like a sore thumb. So I doubt that such a check would buy us much practically speaking.
But I agree that this whole thing probably isn't worth the minimal effort required, since the couple of patches I posted on top are purely about cosmetics.
All of that is to say that I'd be happy to see these patches picked up, and I would also not be sad at all to see them left on the floor.
Thanks,
Taylor
Re: [PATCH 0/2] grep: introduce and use grep_and_expr()
Am 07.01.22 um 20:33 schrieb Taylor Blau:
Quoted 14 lines · René Scharfe
Quoted 13 lines
Am 06.01.22 um 20:50 schrieb Taylor Blau:
Quoted 11 lines
René,
Here are a couple of extra patches on top of your series which introduce and use a new
grep_and_expr()function.Like the final patch says, this isn't about reducing code duplication, but rather about adding visual consistency with the other
compile_pattern_xyz()functions.Taylor Blau (2):
grep: extractgrep_binexp()fromgrep_or_expr()
Quoted 8 lines
Quoted 8 lines
I considered extracting such a function as well. I'd have called it
grep_binary_expr(), though, to match the existing names.I decided against it because it can be misused by passing a non-binary kind to it. (That's a weak objection, but the benefit of such a function was low already in my mind because it doesn't do much.) You solve this by keeping
grep_or_expr()and addinggrep_and_expr(), which cannot be misused in this way -- OK.
That "OK" should probably have been a "Good".
Quoted 9 lines
That makes sense. If it's keeping you up at night, we could easily add a check to ensure that
kindis one ofGREP_NODE_ORorGREP_NODE_AND. But I think that any new code that looks like:grep_binexp(GREP_NODE_NOT, xyz, NULL);would probably stick out like a sore thumb. So I doubt that such a check would buy us much practically speaking.
Having the wrappers for all the binary node types is good enough. I'm not so sure about the sticking out thing -- I managed to miss bigger mistakes before..
Quoted 6 lines
But I agree that this whole thing probably isn't worth the minimal effort required, since the couple of patches I posted on top are purely about cosmetics.
All of that is to say that I'd be happy to see these patches picked up, and I would also not be sad at all to see them left on the floor.
I think the end result is an improvement.
René