Skip to content
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

Filterx small changes #443

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/filterx/expr-compound.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ filterx_compound_expr_new(gboolean return_value_of_last_expr)
{
FilterXCompoundExpr *self = g_new0(FilterXCompoundExpr, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "compound");
self->super.eval = _eval;
self->super.optimize = _optimize;
self->super.init = _init;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ FilterXExpr *
filterx_conditional_new(FilterXExpr *condition)
{
FilterXConditional *self = g_new0(FilterXConditional, 1);
filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "conditional");
self->super.eval = _eval;
self->super.optimize = _optimize;
self->super.init = _init;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-done.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ FilterXExpr *
filterx_expr_done(void)
{
FilterXExpr *self = g_new0(FilterXExpr, 1);
filterx_expr_init_instance(self);
filterx_expr_init_instance(self, "done");
self->eval = _eval;

return self;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-drop.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ FilterXExpr *
filterx_expr_drop_msg(void)
{
FilterXExpr *self = g_new0(FilterXExpr, 1);
filterx_expr_init_instance(self);
filterx_expr_init_instance(self, "drop");
self->eval = _eval;

return self;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ _function_free(FilterXExpr *s)
void
filterx_function_init_instance(FilterXFunction *s, const gchar *function_name)
{
filterx_expr_init_instance(&s->super);
filterx_expr_init_instance(&s->super, "call");
s->function_name = g_strdup_printf("%s()", function_name);
s->super.optimize = _function_optimize;
s->super.init = _function_init;
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/expr-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ filterx_generator_optimize_method(FilterXExpr *s)
void
filterx_generator_init_instance(FilterXExpr *s)
{
filterx_expr_init_instance(s);
filterx_expr_init_instance(s, "generator");
s->optimize = filterx_generator_optimize_method;
s->init = filterx_generator_init_method;
s->deinit = filterx_generator_deinit_method;
Expand Down Expand Up @@ -188,7 +188,7 @@ filterx_generator_create_container_new(FilterXExpr *g, FilterXExpr *fillable_par
{
FilterXExprGeneratorCreateContainer *self = g_new0(FilterXExprGeneratorCreateContainer, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "create_container");
self->generator = (FilterXExprGenerator *) g;
self->fillable_parent = fillable_parent;
self->super.optimize = _create_container_optimize;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-get-subscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ filterx_get_subscript_new(FilterXExpr *operand, FilterXExpr *key)
{
FilterXGetSubscript *self = g_new0(FilterXGetSubscript, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "get_subscript");
self->super.eval = _eval;
self->super.is_set = _isset;
self->super.unset = _unset;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-getattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ filterx_getattr_new(FilterXExpr *operand, FilterXString *attr_name)
{
FilterXGetAttr *self = g_new0(FilterXGetAttr, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "getattr");
self->super.eval = _eval;
self->super.unset = _unset;
self->super.is_set = _isset;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-literal-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ static void
_literal_inner_generator_init_instance(FilterXLiteralInnerGenerator *self, FilterXExpr *root_literal_generator,
GList *elements)
{
filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "literal_inner_generator");
self->super.free_fn = _literal_inner_generator_free;

/*
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-literal.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ filterx_literal_new(FilterXObject *object)
{
FilterXLiteral *self = g_new0(FilterXLiteral, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "literal");
self->super.eval = _eval;
self->super.free_fn = _free;
self->object = object;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ filterx_expr_regexp_match_new(FilterXExpr *lhs, const gchar *pattern)
{
FilterXExprRegexpMatch *self = g_new0(FilterXExprRegexpMatch, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "regexp_match");
self->super.eval = _regexp_match_eval;
self->super.optimize = _regexp_match_optimize;
self->super.init = _regexp_match_init;
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/expr-set-subscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ filterx_nullv_set_subscript_new(FilterXExpr *object, FilterXExpr *key, FilterXEx
{
FilterXSetSubscript *self = g_new0(FilterXSetSubscript, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "nullv_set_subscript");
self->super.eval = _nullv_set_subscript_eval;
self->super.optimize = _optimize;
self->super.init = _init;
Expand All @@ -240,7 +240,7 @@ filterx_set_subscript_new(FilterXExpr *object, FilterXExpr *key, FilterXExpr *ne
{
FilterXSetSubscript *self = g_new0(FilterXSetSubscript, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "set_subscript");
self->super.eval = _set_subscript_eval;
self->super.optimize = _optimize;
self->super.init = _init;
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/expr-setattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ filterx_nullv_setattr_new(FilterXExpr *object, FilterXString *attr_name, FilterX
{
FilterXSetAttr *self = g_new0(FilterXSetAttr, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "nullv_setattr");
self->super.eval = _nullv_setattr_eval;
self->super.optimize = _optimize;
self->super.init = _init;
Expand All @@ -217,7 +217,7 @@ filterx_setattr_new(FilterXExpr *object, FilterXString *attr_name, FilterXExpr *
{
FilterXSetAttr *self = g_new0(FilterXSetAttr, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "setattr");
self->super.eval = _setattr_eval;
self->super.optimize = _optimize;
self->super.init = _init;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-template.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ filterx_template_new(LogTemplate *template)
{
FilterXTemplate *self = g_new0(FilterXTemplate, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "template");
self->super.init = _template_init;
self->super.deinit = _template_deinit;
self->super.eval = _eval;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ filterx_variable_expr_new(FilterXString *name, FilterXVariableType type)
{
FilterXVariableExpr *self = g_new0(FilterXVariableExpr, 1);

filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, "variable");
self->super.free_fn = _free;
self->super.init = _init;
self->super.deinit = _deinit;
Expand Down
25 changes: 12 additions & 13 deletions lib/filterx/filterx-expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ filterx_expr_optimize(FilterXExpr *self)
optimized = filterx_expr_optimize(optimized);

msg_trace("FilterX: expression optimized",
filterx_expr_format_location_tag(self));
filterx_expr_format_location_tag(self),
evt_tag_str("old_type", self->type),
evt_tag_str("new_type", optimized->type));
bazsi marked this conversation as resolved.
Show resolved Hide resolved
if (self->lloc)
{
/* copy location information to the optimized representation */
Expand Down Expand Up @@ -118,19 +120,20 @@ filterx_expr_free_method(FilterXExpr *self)
}

void
filterx_expr_init_instance(FilterXExpr *self)
filterx_expr_init_instance(FilterXExpr *self, const gchar *type)
{
self->ref_cnt = 1;
self->init = filterx_expr_init_method;
self->deinit = filterx_expr_deinit_method;
self->free_fn = filterx_expr_free_method;
self->type = type;
}

FilterXExpr *
filterx_expr_new(void)
{
FilterXExpr *self = g_new0(FilterXExpr, 1);
filterx_expr_init_instance(self);
filterx_expr_init_instance(self, "expr");
return self;
}

Expand Down Expand Up @@ -180,7 +183,7 @@ filterx_unary_op_init_method(FilterXExpr *s, GlobalConfig *cfg)

stats_lock();
StatsClusterKey sc_key;
StatsClusterLabel labels[] = { stats_cluster_label("name", self->name) };
StatsClusterLabel labels[] = { stats_cluster_label("name", self->super.type) };
stats_cluster_single_key_set(&sc_key, "fx_op_evals_total", labels, G_N_ELEMENTS(labels));
stats_register_counter(STATS_LEVEL3, &sc_key, SC_TYPE_SINGLE_VALUE, &self->super.eval_count);
stats_unlock();
Expand All @@ -195,7 +198,7 @@ filterx_unary_op_deinit_method(FilterXExpr *s, GlobalConfig *cfg)

stats_lock();
StatsClusterKey sc_key;
StatsClusterLabel labels[] = { stats_cluster_label("name", self->name) };
StatsClusterLabel labels[] = { stats_cluster_label("name", self->super.type) };
stats_cluster_single_key_set(&sc_key, "fx_op_evals_total", labels, G_N_ELEMENTS(labels));
stats_unregister_counter(&sc_key, SC_TYPE_SINGLE_VALUE, &self->super.eval_count);
stats_unlock();
Expand All @@ -216,14 +219,12 @@ filterx_unary_op_free_method(FilterXExpr *s)
void
filterx_unary_op_init_instance(FilterXUnaryOp *self, const gchar *name, FilterXExpr *operand)
{
filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, name);
self->super.optimize = filterx_unary_op_optimize_method;
self->super.init = filterx_unary_op_init_method;
self->super.deinit = filterx_unary_op_deinit_method;
self->super.free_fn = filterx_unary_op_free_method;
self->operand = operand;

self->name = name;
}

void
Expand Down Expand Up @@ -259,7 +260,7 @@ filterx_binary_op_init_method(FilterXExpr *s, GlobalConfig *cfg)

stats_lock();
StatsClusterKey sc_key;
StatsClusterLabel labels[] = { stats_cluster_label("name", self->name) };
StatsClusterLabel labels[] = { stats_cluster_label("name", self->super.type) };
stats_cluster_single_key_set(&sc_key, "fx_op_evals_total", labels, G_N_ELEMENTS(labels));
stats_register_counter(STATS_LEVEL3, &sc_key, SC_TYPE_SINGLE_VALUE, &self->super.eval_count);
stats_unlock();
Expand All @@ -274,7 +275,7 @@ filterx_binary_op_deinit_method(FilterXExpr *s, GlobalConfig *cfg)

stats_lock();
StatsClusterKey sc_key;
StatsClusterLabel labels[] = { stats_cluster_label("name", self->name) };
StatsClusterLabel labels[] = { stats_cluster_label("name", self->super.type) };
stats_cluster_single_key_set(&sc_key, "fx_op_evals_total", labels, G_N_ELEMENTS(labels));
stats_unregister_counter(&sc_key, SC_TYPE_SINGLE_VALUE, &self->super.eval_count);
stats_unlock();
Expand All @@ -287,7 +288,7 @@ filterx_binary_op_deinit_method(FilterXExpr *s, GlobalConfig *cfg)
void
filterx_binary_op_init_instance(FilterXBinaryOp *self, const gchar *name, FilterXExpr *lhs, FilterXExpr *rhs)
{
filterx_expr_init_instance(&self->super);
filterx_expr_init_instance(&self->super, name);
self->super.optimize = filterx_binary_op_optimize_method;
self->super.init = filterx_binary_op_init_method;
self->super.deinit = filterx_binary_op_deinit_method;
Expand All @@ -296,6 +297,4 @@ filterx_binary_op_init_instance(FilterXBinaryOp *self, const gchar *name, Filter
g_assert(rhs);
self->lhs = lhs;
self->rhs = rhs;

self->name = name;
}
5 changes: 2 additions & 3 deletions lib/filterx/filterx-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct _FilterXExpr
FilterXExpr *(*optimize)(FilterXExpr *self);
void (*free_fn)(FilterXExpr *self);

/* type of the expr */
const gchar *type;
CFG_LTYPE *lloc;
gchar *expr_text;
Expand Down Expand Up @@ -151,7 +152,7 @@ void filterx_expr_set_location(FilterXExpr *self, CfgLexer *lexer, CFG_LTYPE *ll
void filterx_expr_set_location_with_text(FilterXExpr *self, CFG_LTYPE *lloc, const gchar *text);
EVTTAG *filterx_expr_format_location_tag(FilterXExpr *self);
FilterXExpr *filterx_expr_optimize(FilterXExpr *self);
void filterx_expr_init_instance(FilterXExpr *self);
void filterx_expr_init_instance(FilterXExpr *self, const gchar *type);
FilterXExpr *filterx_expr_new(void);
FilterXExpr *filterx_expr_ref(FilterXExpr *self);
void filterx_expr_unref(FilterXExpr *self);
Expand Down Expand Up @@ -190,7 +191,6 @@ typedef struct _FilterXUnaryOp
{
FilterXExpr super;
FilterXExpr *operand;
const gchar *name;
} FilterXUnaryOp;

FilterXExpr *filterx_unary_op_optimize_method(FilterXExpr *s);
Expand All @@ -203,7 +203,6 @@ typedef struct _FilterXBinaryOp
{
FilterXExpr super;
FilterXExpr *lhs, *rhs;
const gchar *name;
} FilterXBinaryOp;

FilterXExpr *filterx_binary_op_optimize_method(FilterXExpr *s);
Expand Down
23 changes: 13 additions & 10 deletions lib/filterx/filterx-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,18 @@ plus_assignment
| expr '[' expr ']' KW_PLUS_ASSIGN expr { $$ = filterx_set_subscript_new(filterx_expr_ref($1), filterx_expr_ref($3), filterx_operator_plus_new(filterx_get_subscript_new($1, $3), $6)); }
| expr '.' identifier KW_PLUS_ASSIGN expr
{
$$ = filterx_setattr_new(filterx_expr_ref($1), filterx_config_frozen_string(configuration, $3), filterx_operator_plus_new(filterx_getattr_new($1, filterx_config_frozen_string(configuration, $3)), $5));
free($3);
$$ = filterx_setattr_new(filterx_expr_ref($1),
filterx_config_frozen_string(configuration, $3),
filterx_operator_plus_new(filterx_getattr_new($1, filterx_config_frozen_string(configuration, $3)),
$5));
free($3);
}
;


assignment
/* TODO extract lvalues */
: variable KW_ASSIGN expr { $$ = filterx_assign_new($1, $3); }
: variable KW_ASSIGN expr { $$ = filterx_assign_new($1, $3); }
| expr '.' identifier KW_ASSIGN expr { $$ = filterx_setattr_new($1, filterx_config_frozen_string(configuration, $3), $5); free($3); }
| expr '[' expr ']' KW_ASSIGN expr { $$ = filterx_set_subscript_new($1, $3, $6); }
| expr '[' ']' KW_ASSIGN expr { $$ = filterx_set_subscript_new($1, NULL, $5); }
Expand Down Expand Up @@ -308,7 +311,7 @@ generator_assignment
;

generator_plus_assignment
: variable KW_PLUS_ASSIGN expr_generator { $$ = $3; filterx_generator_set_fillable($3, $1); }
: variable KW_PLUS_ASSIGN expr_generator { $$ = $3; filterx_generator_set_fillable($3, $1); }
| expr '[' expr ']' KW_PLUS_ASSIGN expr_generator { $$ = $6; filterx_generator_set_fillable($6, filterx_get_subscript_new($1, $3)); }
| expr '.' identifier KW_PLUS_ASSIGN expr_generator { $$ = $5; filterx_generator_set_fillable($5, filterx_getattr_new($1, filterx_config_frozen_string(configuration, $3))); free($3);}

Expand Down Expand Up @@ -397,16 +400,16 @@ declaration
}
;

expr: __expr { $$ = $1; filterx_expr_set_location($1, lexer, &@1); }
expr: __expr { $$ = _assign_location($1, lexer, &@1); }

__expr
: expr_value
| function_call
| expr_operator
| '(' expr ')' { $$ = $2; }
| KW_ISSET '(' expr ')' { $$ = filterx_isset_new($3); }
| KW_DROP { $$ = filterx_expr_drop_msg(); }
| KW_DONE { $$ = filterx_expr_done(); }
| '(' expr ')' { $$ = $2; }
| KW_ISSET '(' expr ')' { $$ = filterx_isset_new($3); }
| KW_DROP { $$ = filterx_expr_drop_msg(); }
| KW_DONE { $$ = filterx_expr_done(); }
;

expr_operator
Expand All @@ -417,7 +420,7 @@ expr_operator
| default
/* TODO extract lvalues */
| expr '.' identifier { $$ = filterx_getattr_new($1, filterx_config_frozen_string(configuration, $3)); free($3); }
| expr '[' expr ']' { $$ = filterx_get_subscript_new($1, $3); }
| expr '[' expr ']' { $$ = filterx_get_subscript_new($1, $3); }
;

boolalg_operator
Expand Down
6 changes: 2 additions & 4 deletions lib/filterx/filterx-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,10 @@ _filterx_ref_add(FilterXObject *s, FilterXObject *object)
return filterx_object_add_object(self->value, object);
}

/* NOTE: fastpath is in the header as an inline function */
FilterXObject *
filterx_ref_new(FilterXObject *value)
_filterx_ref_new(FilterXObject *value)
{
if (!value || value->readonly || !_filterx_type_is_referenceable(value->type))
return value;

if (filterx_object_is_type(value, &FILTERX_TYPE_NAME(ref)))
{
FilterXRef *absorbed_ref = (FilterXRef *) value;
Expand Down
12 changes: 10 additions & 2 deletions lib/filterx/filterx-ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ typedef struct _FilterXRef
} FilterXRef;


FilterXObject *filterx_ref_new(FilterXObject *value);

/* Call them only where downcasting to a specific type is needed, the returned object should only be used locally. */
FilterXObject *filterx_ref_unwrap_ro(FilterXObject *s);
FilterXObject *filterx_ref_unwrap_rw(FilterXObject *s);
Expand All @@ -60,4 +58,14 @@ _filterx_type_is_referenceable(FilterXType *t)
return t->is_mutable && t != &FILTERX_TYPE_NAME(ref);
}

FilterXObject *_filterx_ref_new(FilterXObject *value);

static inline FilterXObject *
filterx_ref_new(FilterXObject *value)
{
if (!value || value->readonly || !_filterx_type_is_referenceable(value->type))
return value;
return _filterx_ref_new(value);
}

#endif
Loading
Loading