Skip to content

Commit

Permalink
fix(filter): save parser positions when compiling
Browse files Browse the repository at this point in the history
Use parser positions saved in ast nodes as a compiler position. That
way, if any part of compilation throws an exception, the caller can
call get_pos() to retrieve the position of the node that had the
compliation error.

Signed-off-by: Mark Stemm <mark.stemm@gmail.com>
  • Loading branch information
mstemm authored and poiana committed Sep 13, 2022
1 parent f4ac877 commit cc31a5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions userspace/libsinsp/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,7 @@ sinsp_filter* sinsp_filter_compiler::compile()

void sinsp_filter_compiler::visit(libsinsp::filter::ast::and_expr* e)
{
m_pos = e->get_pos();
bool nested = m_last_boolop != BO_AND;
if (nested)
{
Expand All @@ -1603,6 +1604,7 @@ void sinsp_filter_compiler::visit(libsinsp::filter::ast::and_expr* e)

void sinsp_filter_compiler::visit(libsinsp::filter::ast::or_expr* e)
{
m_pos = e->get_pos();
bool nested = m_last_boolop != BO_OR;
if (nested)
{
Expand All @@ -1622,6 +1624,7 @@ void sinsp_filter_compiler::visit(libsinsp::filter::ast::or_expr* e)

void sinsp_filter_compiler::visit(libsinsp::filter::ast::not_expr* e)
{
m_pos = e->get_pos();
m_last_boolop = (boolop)((uint32_t)m_last_boolop | BO_NOT);
m_filter->push_expression(m_last_boolop);
m_last_boolop = BO_NONE;
Expand All @@ -1631,6 +1634,7 @@ void sinsp_filter_compiler::visit(libsinsp::filter::ast::not_expr* e)

void sinsp_filter_compiler::visit(libsinsp::filter::ast::unary_check_expr* e)
{
m_pos = e->get_pos();
string field = create_filtercheck_name(e->field, e->arg);
gen_event_filter_check *check = create_filtercheck(field);
m_filter->add_check(check);
Expand Down Expand Up @@ -1661,6 +1665,7 @@ static void add_filtercheck_value(gen_event_filter_check *chk, size_t idx, const

void sinsp_filter_compiler::visit(libsinsp::filter::ast::binary_check_expr* e)
{
m_pos = e->get_pos();
string field = create_filtercheck_name(e->field, e->arg);
gen_event_filter_check *check = create_filtercheck(field);
m_filter->add_check(check);
Expand All @@ -1685,6 +1690,7 @@ void sinsp_filter_compiler::visit(libsinsp::filter::ast::binary_check_expr* e)

void sinsp_filter_compiler::visit(libsinsp::filter::ast::value_expr* e)
{
m_pos = e->get_pos();
if (!m_expect_values)
{
// this ensures that identifiers, such as Falco macros, are not left
Expand All @@ -1697,6 +1703,7 @@ void sinsp_filter_compiler::visit(libsinsp::filter::ast::value_expr* e)

void sinsp_filter_compiler::visit(libsinsp::filter::ast::list_expr* e)
{
m_pos = e->get_pos();
if (!m_expect_values)
{
ASSERT(false);
Expand Down
3 changes: 3 additions & 0 deletions userspace/libsinsp/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class SINSP_PUBLIC sinsp_filter_compiler:
*/
sinsp_filter* compile();

const libsinsp::filter::ast::pos_info& get_pos() const { return m_pos; }

private:
void visit(libsinsp::filter::ast::and_expr*) override;
void visit(libsinsp::filter::ast::or_expr*) override;
Expand All @@ -118,6 +120,7 @@ class SINSP_PUBLIC sinsp_filter_compiler:
std::string create_filtercheck_name(std::string& name, std::string& arg);
gen_event_filter_check* create_filtercheck(std::string& field);

libsinsp::filter::ast::pos_info m_pos;
bool m_ttable_only;
bool m_expect_values;
boolop m_last_boolop;
Expand Down

0 comments on commit cc31a5b

Please sign in to comment.