Reformat embedded C++ code in bison/flex files

This commit is contained in:
Tim Wojtulewicz 2025-03-03 13:49:30 -07:00
parent a2a30f2a2b
commit 61cd5779f2
6 changed files with 944 additions and 1137 deletions

View file

@ -82,29 +82,28 @@
%type <expr> opt_assert_msg %type <expr> opt_assert_msg
%{ %{
#include <cassert>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <cassert>
#include <set> #include <set>
#include <string> #include <string>
#include "zeek/input.h"
#include "zeek/ZeekList.h"
#include "zeek/Desc.h" #include "zeek/Desc.h"
#include "zeek/Expr.h" #include "zeek/Expr.h"
#include "zeek/Func.h" #include "zeek/Func.h"
#include "zeek/IntrusivePtr.h"
#include "zeek/RE.h"
#include "zeek/Reporter.h"
#include "zeek/Scope.h"
#include "zeek/ScriptCoverageManager.h"
#include "zeek/ScriptValidation.h"
#include "zeek/Stmt.h" #include "zeek/Stmt.h"
#include "zeek/Val.h" #include "zeek/Val.h"
#include "zeek/Var.h" #include "zeek/Var.h"
#include "zeek/RE.h" #include "zeek/ZeekList.h"
#include "zeek/Scope.h" #include "zeek/input.h"
#include "zeek/Reporter.h"
#include "zeek/ScriptCoverageManager.h"
#include "zeek/ScriptValidation.h"
#include "zeek/zeekygen/Manager.h"
#include "zeek/module_util.h" #include "zeek/module_util.h"
#include "zeek/IntrusivePtr.h" #include "zeek/zeekygen/Manager.h"
extern const char* filename; // Absolute path of file currently being parsed. extern const char* filename; // Absolute path of file currently being parsed.
extern const char* last_filename; // Absolute path of last file parsed. extern const char* last_filename; // Absolute path of last file parsed.
@ -120,8 +119,7 @@ extern YYLTYPE GetCurrentLocation();
extern int yyerror(const char[]); extern int yyerror(const char[]);
extern int zeeklex(); extern int zeeklex();
#define YYLLOC_DEFAULT(Current, Rhs, N) \ #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = (Rhs)[(N)];
(Current) = (Rhs)[(N)];
using namespace zeek; using namespace zeek;
using namespace zeek::detail; using namespace zeek::detail;
@ -162,13 +160,11 @@ static int func_hdr_cond_epoch = 0;
EnumType* cur_enum_type = nullptr; EnumType* cur_enum_type = nullptr;
static ID* cur_decl_type_id = nullptr; static ID* cur_decl_type_id = nullptr;
static void parse_new_enum(void) static void parse_new_enum(void) {
{
// Starting a new enum definition. // Starting a new enum definition.
assert(cur_enum_type == nullptr); assert(cur_enum_type == nullptr);
if ( cur_decl_type_id ) if ( cur_decl_type_id ) {
{
auto name = make_full_var_name(current_module.c_str(), cur_decl_type_id->Name()); auto name = make_full_var_name(current_module.c_str(), cur_decl_type_id->Name());
cur_enum_type = new EnumType(name); cur_enum_type = new EnumType(name);
} }
@ -176,8 +172,7 @@ static void parse_new_enum(void)
reporter->FatalError("incorrect syntax for enum type declaration"); reporter->FatalError("incorrect syntax for enum type declaration");
} }
static void parse_redef_enum(ID* id) static void parse_redef_enum(ID* id) {
{
// Redef an enum. id points to the enum to be redefined. // Redef an enum. id points to the enum to be redefined.
// Let cur_enum_type point to it. // Let cur_enum_type point to it.
assert(cur_enum_type == nullptr); assert(cur_enum_type == nullptr);
@ -185,8 +180,7 @@ static void parse_redef_enum(ID* id)
// abort on errors; enums need to be accessible to continue parsing // abort on errors; enums need to be accessible to continue parsing
if ( ! id->GetType() ) if ( ! id->GetType() )
reporter->FatalError("unknown enum identifier \"%s\"", id->Name()); reporter->FatalError("unknown enum identifier \"%s\"", id->Name());
else else {
{
if ( ! id->GetType() || id->GetType()->Tag() != TYPE_ENUM ) if ( ! id->GetType() || id->GetType()->Tag() != TYPE_ENUM )
reporter->FatalError("identifier \"%s\" is not an enum", id->Name()); reporter->FatalError("identifier \"%s\" is not an enum", id->Name());
cur_enum_type = id->GetType()->AsEnumType(); cur_enum_type = id->GetType()->AsEnumType();
@ -194,26 +188,21 @@ static void parse_redef_enum(ID* id)
} }
static void parse_redef_record_field(ID* id, const char* field, InitClass ic, static void parse_redef_record_field(ID* id, const char* field, InitClass ic,
std::unique_ptr<std::vector<AttrPtr>> attrs) std::unique_ptr<std::vector<AttrPtr>> attrs) {
{ if ( ! id->GetType() ) {
if ( ! id->GetType() )
{
reporter->FatalError("unknown record identifier \"%s\"", id->Name()); reporter->FatalError("unknown record identifier \"%s\"", id->Name());
return; return;
} }
auto t = id->GetType(); auto t = id->GetType();
if ( ! t || t->Tag() != TYPE_RECORD ) if ( ! t || t->Tag() != TYPE_RECORD ) {
{ reporter->FatalError("identifier \"%s\" has type \"%s\", expected \"record\"", id->Name(), type_name(t->Tag()));
reporter->FatalError("identifier \"%s\" has type \"%s\", expected \"record\"",
id->Name(), type_name(t->Tag()));
return; return;
} }
auto rt = t->AsRecordType(); auto rt = t->AsRecordType();
auto idx = rt->FieldOffset(field); auto idx = rt->FieldOffset(field);
if ( idx < 0 ) if ( idx < 0 ) {
{
reporter->FatalError("field \"%s\" not in record \"%s\"", field, id->Name()); reporter->FatalError("field \"%s\" not in record \"%s\"", field, id->Name());
return; return;
} }
@ -221,15 +210,11 @@ static void parse_redef_record_field(ID* id, const char* field, InitClass ic,
auto decl = rt->FieldDecl(idx); auto decl = rt->FieldDecl(idx);
if ( ! decl->attrs ) if ( ! decl->attrs )
if ( ic == INIT_EXTRA ) if ( ic == INIT_EXTRA )
decl->attrs = make_intrusive<detail::Attributes>(decl->type, decl->attrs = make_intrusive<detail::Attributes>(decl->type, true /* in_record */, false /* is_global */);
true /* in_record */,
false /* is_global */);
for ( const auto& attr : *attrs ) for ( const auto& attr : *attrs ) {
{
// At this point, only support &log redef'ing. // At this point, only support &log redef'ing.
if ( attr->Tag() != ATTR_LOG ) if ( attr->Tag() != ATTR_LOG ) {
{
reporter->FatalError("Can only redef \"&log\" attributes of record fields"); reporter->FatalError("Can only redef \"&log\" attributes of record fields");
return; return;
} }
@ -243,13 +228,10 @@ static void parse_redef_record_field(ID* id, const char* field, InitClass ic,
} }
} }
static void extend_record(ID* id, std::unique_ptr<type_decl_list> fields, static void extend_record(ID* id, std::unique_ptr<type_decl_list> fields, std::unique_ptr<std::vector<AttrPtr>> attrs) {
std::unique_ptr<std::vector<AttrPtr>> attrs)
{
const auto& types = Type::Aliases(id->Name()); const auto& types = Type::Aliases(id->Name());
if ( types.empty() ) if ( types.empty() ) {
{
id->Error("failed to redef record: no types found in alias map"); id->Error("failed to redef record: no types found in alias map");
return; return;
} }
@ -258,39 +240,31 @@ static void extend_record(ID* id, std::unique_ptr<type_decl_list> fields,
if ( attrs ) if ( attrs )
for ( const auto& at : *attrs ) for ( const auto& at : *attrs )
if ( at->Tag() == ATTR_LOG ) if ( at->Tag() == ATTR_LOG ) {
{
add_log_attr = true; add_log_attr = true;
break; break;
} }
for ( const auto& t : types ) for ( const auto& t : types ) {
{
auto error = t->AsRecordType()->AddFields(*fields, add_log_attr); auto error = t->AsRecordType()->AddFields(*fields, add_log_attr);
if ( error ) if ( error ) {
{
id->Error(error); id->Error(error);
break; break;
} }
} }
} }
static AttributesPtr static AttributesPtr make_attributes(std::vector<AttrPtr>* attrs, TypePtr t, bool in_record, bool is_global) {
make_attributes(std::vector<AttrPtr>* attrs,
TypePtr t, bool in_record, bool is_global)
{
if ( ! attrs ) if ( ! attrs )
return nullptr; return nullptr;
auto rval = make_intrusive<Attributes>(std::move(*attrs), std::move(t), auto rval = make_intrusive<Attributes>(std::move(*attrs), std::move(t), in_record, is_global);
in_record, is_global);
delete attrs; delete attrs;
return rval; return rval;
} }
static bool expr_is_table_type_name(const Expr* expr) static bool expr_is_table_type_name(const Expr* expr) {
{
if ( expr->Tag() != EXPR_NAME ) if ( expr->Tag() != EXPR_NAME )
return false; return false;
@ -305,8 +279,7 @@ static bool expr_is_table_type_name(const Expr* expr)
return false; return false;
} }
static void check_loop_var(const IDPtr& var) static void check_loop_var(const IDPtr& var) {
{
if ( var->IsGlobal() ) if ( var->IsGlobal() )
var->Error("global variable used in 'for' loop"); var->Error("global variable used in 'for' loop");
@ -314,9 +287,7 @@ static void check_loop_var(const IDPtr& var)
var->Error("constant used in 'for' loop"); var->Error("constant used in 'for' loop");
} }
static void build_global(ID* id, Type* t, InitClass ic, Expr* e, static void build_global(ID* id, Type* t, InitClass ic, Expr* e, std::vector<AttrPtr>* attrs, DeclType dt) {
std::vector<AttrPtr>* attrs, DeclType dt)
{
IDPtr id_ptr{AdoptRef{}, id}; IDPtr id_ptr{AdoptRef{}, id};
TypePtr t_ptr{AdoptRef{}, t}; TypePtr t_ptr{AdoptRef{}, t};
ExprPtr e_ptr{AdoptRef{}, e}; ExprPtr e_ptr{AdoptRef{}, e};
@ -331,18 +302,15 @@ static void build_global(ID* id, Type* t, InitClass ic, Expr* e,
zeekygen_mgr->Identifier(std::move(id_ptr)); zeekygen_mgr->Identifier(std::move(id_ptr));
} }
static StmtPtr build_local(ID* id, Type* t, InitClass ic, Expr* e, static StmtPtr build_local(ID* id, Type* t, InitClass ic, Expr* e, std::vector<AttrPtr>* attrs, DeclType dt,
std::vector<AttrPtr>* attrs, DeclType dt, bool do_coverage) {
bool do_coverage)
{
IDPtr id_ptr{AdoptRef{}, id}; IDPtr id_ptr{AdoptRef{}, id};
TypePtr t_ptr{AdoptRef{}, t}; TypePtr t_ptr{AdoptRef{}, t};
ExprPtr e_ptr{AdoptRef{}, e}; ExprPtr e_ptr{AdoptRef{}, e};
auto attrs_ptr = attrs ? std::make_unique<std::vector<AttrPtr>>(*attrs) : nullptr; auto attrs_ptr = attrs ? std::make_unique<std::vector<AttrPtr>>(*attrs) : nullptr;
auto init = add_local(std::move(id_ptr), std::move(t_ptr), ic, auto init = add_local(std::move(id_ptr), std::move(t_ptr), ic, e_ptr, std::move(attrs_ptr), dt);
e_ptr, std::move(attrs_ptr), dt);
if ( do_coverage ) if ( do_coverage )
script_coverage_mgr.AddStmt(init.get()); script_coverage_mgr.AddStmt(init.get());
@ -350,8 +318,7 @@ static StmtPtr build_local(ID* id, Type* t, InitClass ic, Expr* e,
return init; return init;
} }
static void refine_location(zeek::detail::ID* id) static void refine_location(zeek::detail::ID* id) {
{
if ( *id->GetLocationInfo() == zeek::detail::no_location ) if ( *id->GetLocationInfo() == zeek::detail::no_location )
id->SetLocationInfo(&detail::start_location, &detail::end_location); id->SetLocationInfo(&detail::start_location, &detail::end_location);
} }
@ -384,8 +351,7 @@ static void refine_location(zeek::detail::ID* id)
zeek::FuncType::Capture* capture; zeek::FuncType::Capture* capture;
zeek::FuncType::CaptureList* captures; zeek::FuncType::CaptureList* captures;
zeek::detail::WhenInfo* when_clause; zeek::detail::WhenInfo* when_clause;
struct struct {
{
bool ignore_case; bool ignore_case;
bool single_line; bool single_line;
} re_modes; } re_modes;
@ -549,30 +515,25 @@ expr:
set_location(@1, @2); set_location(@1, @2);
$$ = new NegExpr({AdoptRef{}, $2}); $$ = new NegExpr({AdoptRef{}, $2});
if ( ! $$->IsError() && $2->IsConst() ) if ( ! $$->IsError() && $2->IsConst() ) {
{
auto v = $2->ExprVal(); auto v = $2->ExprVal();
auto tag = v->GetType()->Tag(); auto tag = v->GetType()->Tag();
if ( tag == TYPE_COUNT ) if ( tag == TYPE_COUNT ) {
{
auto c = v->AsCount(); auto c = v->AsCount();
uint64_t int_max = static_cast<uint64_t>(INT64_MAX) + 1; uint64_t int_max = static_cast<uint64_t>(INT64_MAX) + 1;
if ( c <= int_max ) if ( c <= int_max ) {
{
auto ce = new ConstExpr(val_mgr->Int(-c)); auto ce = new ConstExpr(val_mgr->Int(-c));
Unref($$); Unref($$);
$$ = ce; $$ = ce;
} }
else else {
{
$$->Error("literal is outside range of 'int' values"); $$->Error("literal is outside range of 'int' values");
$$->SetError(); $$->SetError();
} }
} }
else else {
{
auto ce = new ConstExpr($$->Eval(nullptr)); auto ce = new ConstExpr($$->Eval(nullptr));
Unref($$); Unref($$);
$$ = ce; $$ = ce;
@ -600,8 +561,7 @@ expr:
ExprPtr rhs = {AdoptRef{}, $3}; ExprPtr rhs = {AdoptRef{}, $3};
auto tag1 = $1->GetType()->Tag(); auto tag1 = $1->GetType()->Tag();
if ( IsArithmetic($1->GetType()->Tag()) ) if ( IsArithmetic($1->GetType()->Tag()) ) {
{
// Script optimization assumes that each AST // Script optimization assumes that each AST
// node is distinct, hence the call to // node is distinct, hence the call to
// Duplicate() here. // Duplicate() here.
@ -630,8 +590,7 @@ expr:
ExprPtr rhs = {AdoptRef{}, $3}; ExprPtr rhs = {AdoptRef{}, $3};
auto tag1 = $1->GetType()->Tag(); auto tag1 = $1->GetType()->Tag();
if ( IsArithmetic(tag1) ) if ( IsArithmetic(tag1) ) {
{
ExprPtr sum = make_intrusive<SubExpr>(lhs, rhs); ExprPtr sum = make_intrusive<SubExpr>(lhs, rhs);
if ( sum->GetType()->Tag() != tag1 ) if ( sum->GetType()->Tag() != tag1 )
@ -836,10 +795,8 @@ expr:
// used for an initializer. Interpret no expressions // used for an initializer. Interpret no expressions
// as an empty record constructor. // as an empty record constructor.
for ( int i = 0; i < $2->Exprs().length(); ++i ) for ( int i = 0; i < $2->Exprs().length(); ++i ) {
{ if ( $2->Exprs()[i]->Tag() != EXPR_FIELD_ASSIGN ) {
if ( $2->Exprs()[i]->Tag() != EXPR_FIELD_ASSIGN )
{
is_record_ctor = false; is_record_ctor = false;
break; break;
} }
@ -899,12 +856,11 @@ expr:
const auto& ctor_type = $1->AsNameExpr()->Id()->GetType(); const auto& ctor_type = $1->AsNameExpr()->Id()->GetType();
switch ( ctor_type->Tag() ) { switch ( ctor_type->Tag() ) {
case TYPE_RECORD: case TYPE_RECORD: {
{
auto rt = cast_intrusive<RecordType>(ctor_type); auto rt = cast_intrusive<RecordType>(ctor_type);
$$ = new RecordConstructorExpr(rt, ListExprPtr{AdoptRef{}, $4}); $$ = new RecordConstructorExpr(rt, ListExprPtr{AdoptRef{}, $4});
}
break; break;
}
case TYPE_TABLE: case TYPE_TABLE:
if ( ctor_type->IsTable() ) if ( ctor_type->IsTable() )
@ -960,10 +916,8 @@ expr:
set_location(@1); set_location(@1);
auto id = lookup_ID($1, current_module.c_str()); auto id = lookup_ID($1, current_module.c_str());
if ( ! id ) if ( ! id ) {
{ if ( ! in_debug ) {
if ( ! in_debug )
{
/* // CHECK THAT THIS IS NOT GLOBAL. /* // CHECK THAT THIS IS NOT GLOBAL.
id = install_ID($1, current_module.c_str(), id = install_ID($1, current_module.c_str(),
false, is_export); false, is_export);
@ -972,33 +926,26 @@ expr:
yyerror(util::fmt("unknown identifier %s", $1)); yyerror(util::fmt("unknown identifier %s", $1));
YYERROR; YYERROR;
} }
else else {
{
yyerror(util::fmt("unknown identifier %s", $1)); yyerror(util::fmt("unknown identifier %s", $1));
YYERROR; YYERROR;
} }
} }
else else {
{
if ( id->IsDeprecated() ) if ( id->IsDeprecated() )
reporter->Deprecation(id->GetDeprecationWarning()); reporter->Deprecation(id->GetDeprecationWarning());
if ( id->IsBlank() ) if ( id->IsBlank() ) {
{
$$ = new NameExpr(std::move(id)); $$ = new NameExpr(std::move(id));
$$->SetError("blank identifier used in expression"); $$->SetError("blank identifier used in expression");
} }
else if ( ! id->GetType() ) else if ( ! id->GetType() ) {
{
id->Error("undeclared variable"); id->Error("undeclared variable");
id->SetType(error_type()); id->SetType(error_type());
$$ = new NameExpr(std::move(id)); $$ = new NameExpr(std::move(id));
} }
else if ( id->IsEnumConst() ) {
else if ( id->IsEnumConst() ) if ( IsErrorType(id->GetType()->Tag()) ) {
{
if ( IsErrorType(id->GetType()->Tag()) )
{
// The most-relevant error message should already be reported, so // The most-relevant error message should already be reported, so
// just bail out. // just bail out.
YYERROR; YYERROR;
@ -1010,8 +957,7 @@ expr:
reporter->InternalError("enum value not found for %s", id->Name()); reporter->InternalError("enum value not found for %s", id->Name());
$$ = new ConstExpr(t->GetEnumVal(intval)); $$ = new ConstExpr(t->GetEnumVal(intval));
} }
else else {
{
if ( out_of_scope_locals.count(id.get()) > 0 ) if ( out_of_scope_locals.count(id.get()) > 0 )
id->Error("use of out-of-scope local; move declaration to outer scope"); id->Error("use of out-of-scope local; move declaration to outer scope");
@ -1324,15 +1270,13 @@ type:
| resolve_id | resolve_id
{ {
if ( ! $1 || ! ($$ = $1->IsType() ? $1->GetType().get() : nullptr) ) if ( ! $1 || ! ($$ = $1->IsType() ? $1->GetType().get() : nullptr) ) {
{
NullStmt here; NullStmt here;
if ( $1 ) if ( $1 )
$1->Error("not a Zeek type", &here); $1->Error("not a Zeek type", &here);
$$ = error_type()->Ref(); $$ = error_type()->Ref();
} }
else else {
{
Ref($$); Ref($$);
if ( $1->IsDeprecated() ) if ( $1->IsDeprecated() )
@ -1432,8 +1376,7 @@ decl:
} }
| TOK_REDEF global_id { | TOK_REDEF global_id {
if ( $2->IsType() ) if ( $2->IsType() ) {
{
auto tag = $2->GetType()->Tag(); auto tag = $2->GetType()->Tag();
auto tstr = type_name(tag); auto tstr = type_name(tag);
if ( tag == TYPE_RECORD || tag == TYPE_ENUM ) if ( tag == TYPE_RECORD || tag == TYPE_ENUM )
@ -1582,8 +1525,7 @@ func_body:
set_location(func_hdr_location, @5); set_location(func_hdr_location, @5);
bool free_of_conditionals = true; bool free_of_conditionals = true;
if ( current_file_has_conditionals || if ( current_file_has_conditionals || conditional_epoch > func_hdr_cond_epoch )
conditional_epoch > func_hdr_cond_epoch )
free_of_conditionals = false; free_of_conditionals = false;
end_func({AdoptRef{}, $3}, current_module.c_str(), free_of_conditionals); end_func({AdoptRef{}, $3}, current_module.c_str(), free_of_conditionals);
@ -1652,8 +1594,7 @@ begin_lambda:
std::optional<FuncType::CaptureList> captures; std::optional<FuncType::CaptureList> captures;
if ( $1 ) if ( $1 ) {
{
captures = *$1; captures = *$1;
delete $1; delete $1;
} }
@ -1692,13 +1633,11 @@ capture:
if ( ! id ) if ( ! id )
reporter->Error("no such local identifier: %s", $2); reporter->Error("no such local identifier: %s", $2);
else if ( id->IsType() ) else if ( id->IsType() ) {
{
reporter->Error("cannot specify type in capture: %s", $2); reporter->Error("cannot specify type in capture: %s", $2);
id = nullptr; id = nullptr;
} }
else if ( id->IsGlobal() ) else if ( id->IsGlobal() ) {
{
reporter->Error("cannot specify global in capture: %s", $2); reporter->Error("cannot specify global in capture: %s", $2);
id = nullptr; id = nullptr;
} }
@ -1836,8 +1775,7 @@ attr:
$$ = new Attr( $$ = new Attr(
ATTR_DEPRECATED, ATTR_DEPRECATED,
make_intrusive<ConstExpr>(IntrusivePtr{AdoptRef{}, $3})); make_intrusive<ConstExpr>(IntrusivePtr{AdoptRef{}, $3}));
else else {
{
ODesc d; ODesc d;
$3->Describe(&d); $3->Describe(&d);
Unref($3); Unref($3);
@ -2035,10 +1973,8 @@ event:
set_location(@1, @4); set_location(@1, @4);
const auto& id = lookup_ID($1, current_module.c_str()); const auto& id = lookup_ID($1, current_module.c_str());
if ( id ) if ( id ) {
{ if ( ! id->IsGlobal() ) {
if ( ! id->IsGlobal() )
{
yyerror(util::fmt("local identifier \"%s\" cannot be used to reference an event", $1)); yyerror(util::fmt("local identifier \"%s\" cannot be used to reference an event", $1));
YYERROR; YYERROR;
} }
@ -2048,8 +1984,7 @@ event:
$$ = new EventExpr(id->Name(), {AdoptRef{}, $3}); $$ = new EventExpr(id->Name(), {AdoptRef{}, $3});
} }
else else {
{
$$ = new EventExpr($1, {AdoptRef{}, $3}); $$ = new EventExpr($1, {AdoptRef{}, $3});
} }
} }
@ -2102,8 +2037,7 @@ case_type:
else else
case_var = install_ID(name, current_module.c_str(), false, false); case_var = install_ID(name, current_module.c_str(), false, false);
add_local(case_var, std::move(type), INIT_NONE, nullptr, nullptr, add_local(case_var, std::move(type), INIT_NONE, nullptr, nullptr, VAR_REGULAR);
VAR_REGULAR);
$$ = case_var.release(); $$ = case_var.release();
} }
@ -2120,10 +2054,8 @@ for_head:
if ( loop_var ) if ( loop_var )
check_loop_var(loop_var); check_loop_var(loop_var);
else else {
{ loop_var = install_ID($3, current_module.c_str(), false, false);
loop_var = install_ID($3, current_module.c_str(),
false, false);
} }
auto* loop_vars = new IDPList; auto* loop_vars = new IDPList;
@ -2198,8 +2130,7 @@ local_id:
auto id = lookup_ID($1, current_module.c_str()); auto id = lookup_ID($1, current_module.c_str());
$$ = id.release(); $$ = id.release();
if ( $$ ) if ( $$ ) {
{
if ( $$->IsGlobal() && ! $$->IsBlank() ) if ( $$->IsGlobal() && ! $$->IsBlank() )
$$->Error("already a global identifier"); $$->Error("already a global identifier");
@ -2208,11 +2139,8 @@ local_id:
delete [] $1; delete [] $1;
} }
else {
else $$ = install_ID($1, current_module.c_str(), false, false).release();
{
$$ = install_ID($1, current_module.c_str(),
false, false).release();
} }
} }
; ;
@ -2240,13 +2168,11 @@ global_or_event_id:
defining_global_ID); defining_global_ID);
$$ = id.release(); $$ = id.release();
if ( $$ ) if ( $$ ) {
{
if ( ! $$->IsGlobal() ) if ( ! $$->IsGlobal() )
$$->Error("already a local identifier"); $$->Error("already a local identifier");
if ( $$->IsDeprecated() ) if ( $$->IsDeprecated() ) {
{
const auto& t = $$->GetType(); const auto& t = $$->GetType();
if ( t->Tag() != TYPE_FUNC || if ( t->Tag() != TYPE_FUNC ||
@ -2257,15 +2183,12 @@ global_or_event_id:
refine_location($$); refine_location($$);
delete [] $1; delete [] $1;
} }
else {
else
{
const char* module_name = const char* module_name =
resolving_global_ID ? resolving_global_ID ?
current_module.c_str() : 0; current_module.c_str() : nullptr;
$$ = install_ID($1, module_name, $$ = install_ID($1, module_name, true, is_export).release();
true, is_export).release();
} }
} }
; ;
@ -2290,8 +2213,7 @@ lookup_identifier:
| |
TOK_GLOBAL_ID TOK_GLOBAL_ID
{ {
if ( is_export ) if ( is_export ) {
{
reporter->Error("cannot use :: prefix in export section: %s", $1); reporter->Error("cannot use :: prefix in export section: %s", $1);
YYERROR; YYERROR;
} }
@ -2328,8 +2250,7 @@ opt_deprecated:
{ {
if ( IsString($3->GetType()->Tag()) ) if ( IsString($3->GetType()->Tag()) )
$$ = new ConstExpr({AdoptRef{}, $3}); $$ = new ConstExpr({AdoptRef{}, $3});
else else {
{
ODesc d; ODesc d;
$3->Describe(&d); $3->Describe(&d);
reporter->Error("'&deprecated=%s' must use a string literal", reporter->Error("'&deprecated=%s' must use a string literal",
@ -2347,26 +2268,21 @@ expr_list_opt_comma: ',' { expr_list_has_opt_comma = 1; }
%% %%
int yyerror(const char msg[]) int yyerror(const char msg[]) {
{
if ( in_debug ) if ( in_debug )
g_curr_debug_error = util::copy_string(msg); g_curr_debug_error = util::copy_string(msg);
if ( last_tok[0] == '\n' ) if ( last_tok[0] == '\n' )
reporter->Error("%s, on previous line", msg); reporter->Error("%s, on previous line", msg);
else if ( last_tok[0] == '\0' ) else if ( last_tok[0] == '\0' ) {
{
if ( last_filename ) if ( last_filename )
reporter->Error("%s, at end of file %s", msg, last_filename); reporter->Error("%s, at end of file %s", msg, last_filename);
else else
reporter->Error("%s, at end of file", msg); reporter->Error("%s, at end of file", msg);
} }
else else {
{ if ( last_last_tok_filename && last_tok_filename && ! util::streq(last_last_tok_filename, last_tok_filename) )
if ( last_last_tok_filename && last_tok_filename && reporter->Error("%s, at or near \"%s\" or end of file %s", msg, last_tok, last_last_tok_filename);
! util::streq(last_last_tok_filename, last_tok_filename) )
reporter->Error("%s, at or near \"%s\" or end of file %s",
msg, last_tok, last_last_tok_filename);
else else
reporter->Error("%s, at or near \"%s\"", msg, last_tok); reporter->Error("%s, at or near \"%s\"", msg, last_tok);
} }

View file

@ -9,7 +9,6 @@
#include "zeek/EquivClass.h" #include "zeek/EquivClass.h"
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
namespace zeek::detail { namespace zeek::detail {
constexpr int csize = 256; constexpr int csize = 256;
bool re_syntax_error = 0; bool re_syntax_error = 0;
@ -69,17 +68,13 @@ singleton : singleton '*'
{ {
if ( $3 > $5 || $3 < 0 ) if ( $3 > $5 || $3 < 0 )
zeek::detail::synerr("bad iteration values"); zeek::detail::synerr("bad iteration values");
else else {
{ if ( $3 == 0 ) {
if ( $3 == 0 ) if ( $5 == 0 ) {
{
if ( $5 == 0 )
{
$$ = new zeek::detail::NFA_Machine(new zeek::detail::EpsilonState()); $$ = new zeek::detail::NFA_Machine(new zeek::detail::EpsilonState());
Unref($1); Unref($1);
} }
else else {
{
$1->MakeRepl(1, $5); $1->MakeRepl(1, $5);
$1->MakeOptional(); $1->MakeOptional();
} }
@ -105,8 +100,7 @@ singleton : singleton '*'
{ {
if ( $3 < 0 ) if ( $3 < 0 )
zeek::detail::synerr("iteration value must be positive"); zeek::detail::synerr("iteration value must be positive");
else if ( $3 == 0 ) else if ( $3 == 0 ) {
{
Unref($1); Unref($1);
$$ = new zeek::detail::NFA_Machine(new zeek::detail::EpsilonState()); $$ = new zeek::detail::NFA_Machine(new zeek::detail::EpsilonState());
} }
@ -146,8 +140,7 @@ singleton : singleton '*'
{ {
auto sym = $1; auto sym = $1;
if ( sym < 0 || ( sym >= NUM_SYM && sym != SYM_EPSILON ) ) if ( sym < 0 || ( sym >= NUM_SYM && sym != SYM_EPSILON ) ) {
{
zeek::reporter->Error("bad symbol %d (compiling pattern /%s/)", sym, zeek::reporter->Error("bad symbol %d (compiling pattern /%s/)", sym,
zeek::detail::RE_parse_input); zeek::detail::RE_parse_input);
return 1; return 1;
@ -184,28 +177,22 @@ ccl : ccl TOK_CHAR '-' TOK_CHAR
if ( $2 > $4 ) if ( $2 > $4 )
zeek::detail::synerr("negative range in character class"); zeek::detail::synerr("negative range in character class");
else if ( zeek::detail::case_insensitive && else if ( zeek::detail::case_insensitive && (isalpha($2) || isalpha($4)) ) {
(isalpha($2) || isalpha($4)) ) if ( isalpha($2) && isalpha($4) && isupper($2) == isupper($4) ) {
{ // Compatible range, do both versions
if ( isalpha($2) && isalpha($4) &&
isupper($2) == isupper($4) )
{ // Compatible range, do both versions
int l2 = tolower($2); int l2 = tolower($2);
int l4 = tolower($4); int l4 = tolower($4);
for ( int i = l2; i<= l4; ++i ) for ( int i = l2; i<= l4; ++i ) {
{
$1->Add(i); $1->Add(i);
$1->Add(toupper(i)); $1->Add(toupper(i));
} }
} }
else else
zeek::detail::synerr("ambiguous case-insensitive character class"); zeek::detail::synerr("ambiguous case-insensitive character class");
} }
else else {
{
for ( int i = $2; i <= $4; ++i ) for ( int i = $2; i <= $4; ++i )
$1->Add(i); $1->Add(i);
} }
@ -213,8 +200,7 @@ ccl : ccl TOK_CHAR '-' TOK_CHAR
| ccl TOK_CHAR | ccl TOK_CHAR
{ {
if ( zeek::detail::case_insensitive && isalpha($2) ) if ( zeek::detail::case_insensitive && isalpha($2) ) {
{
$1->Add(zeek::detail::clower($2)); $1->Add(zeek::detail::clower($2));
$1->Add(zeek::detail::cupper($2)); $1->Add(zeek::detail::cupper($2));
} }

View file

@ -69,15 +69,13 @@ CCL_EXPR ("[:"[[:alpha:]]+":]")
"["({FIRST_CCL_CHAR}|{CCL_EXPR})({CCL_CHAR}|{CCL_EXPR})* { "["({FIRST_CCL_CHAR}|{CCL_EXPR})({CCL_CHAR}|{CCL_EXPR})* {
zeek::detail::curr_ccl = zeek::detail::rem->LookupCCL(yytext); zeek::detail::curr_ccl = zeek::detail::rem->LookupCCL(yytext);
if ( zeek::detail::curr_ccl ) if ( zeek::detail::curr_ccl ) {
{
if ( yyinput() != ']' ) if ( yyinput() != ']' )
zeek::detail::synerr("bad character class"); zeek::detail::synerr("bad character class");
yylval.ccl_val = zeek::detail::curr_ccl; yylval.ccl_val = zeek::detail::curr_ccl;
return TOK_CCL; return TOK_CCL;
} }
else else {
{
zeek::detail::curr_ccl = new zeek::detail::CCL(); zeek::detail::curr_ccl = new zeek::detail::CCL();
zeek::detail::rem->InsertCCL(yytext, zeek::detail::curr_ccl); zeek::detail::rem->InsertCCL(yytext, zeek::detail::curr_ccl);
@ -99,22 +97,19 @@ CCL_EXPR ("[:"[[:alpha:]]+":]")
if ( namedef.empty() ) if ( namedef.empty() )
zeek::detail::synerr("undefined definition"); zeek::detail::synerr("undefined definition");
else else {
{ // push back name surrounded by ()'s // push back name surrounded by ()'s
int len = namedef.size(); int len = namedef.size();
if ( namedef[0] == '^' || if ( namedef[0] == '^' || (len > 0 && namedef[len - 1] == '$') ) {
(len > 0 && namedef[len - 1] == '$') ) // don't use ()'s after all
{ // don't use ()'s after all
for ( int i = len - 1; i >= 0; --i ) for ( int i = len - 1; i >= 0; --i )
unput(namedef[i]); unput(namedef[i]);
if ( namedef[0] == '^' ) if ( namedef[0] == '^' )
yy_set_bol(1); yy_set_bol(1);
} }
else {
else
{
unput(')'); unput(')');
for ( int i = len - 1; i >= 0; --i ) for ( int i = len - 1; i >= 0; --i )
unput(namedef[i]); unput(namedef[i]);
@ -127,8 +122,7 @@ CCL_EXPR ("[:"[[:alpha:]]+":]")
"(?s:" zeek::detail::re_single_line = true; return TOK_SINGLE_LINE; "(?s:" zeek::detail::re_single_line = true; return TOK_SINGLE_LINE;
[a-zA-Z] { [a-zA-Z] {
if ( zeek::detail::case_insensitive ) if ( zeek::detail::case_insensitive ) {
{
char c = yytext[0]; // unput trashes yytext! char c = yytext[0]; // unput trashes yytext!
// Push back the character inside a CCL, // Push back the character inside a CCL,
// so the parser can then expand it. // so the parser can then expand it.
@ -136,8 +130,7 @@ CCL_EXPR ("[:"[[:alpha:]]+":]")
unput(c); unput(c);
unput('['); unput('[');
} }
else else {
{
yylval.int_val = yytext[0]; yylval.int_val = yytext[0];
return TOK_CHAR; return TOK_CHAR;
} }
@ -238,13 +231,11 @@ CCL_EXPR ("[:"[[:alpha:]]+":]")
YY_BUFFER_STATE RE_buf; YY_BUFFER_STATE RE_buf;
void RE_set_input(const char* str) void RE_set_input(const char* str) {
{
zeek::detail::RE_parse_input = str; zeek::detail::RE_parse_input = str;
RE_buf = yy_scan_string(str); RE_buf = yy_scan_string(str);
} }
void RE_done_with_scan() void RE_done_with_scan() {
{
yy_delete_buffer(RE_buf); yy_delete_buffer(RE_buf);
} }

View file

@ -19,14 +19,14 @@ extern void end_PS();
zeek::detail::Rule* current_rule = nullptr; zeek::detail::Rule* current_rule = nullptr;
const char* current_rule_file = nullptr; const char* current_rule_file = nullptr;
static uint8_t ip4_mask_to_len(uint32_t mask) static uint8_t ip4_mask_to_len(uint32_t mask) {
{
if ( mask == 0xffffffff ) if ( mask == 0xffffffff )
return 32; return 32;
uint32_t x = ~mask + 1; uint32_t x = ~mask + 1;
uint8_t len; uint8_t len;
for ( len = 0; len < 32 && (! (x & (1 << len))); ++len ); for ( len = 0; len < 32 && (! (x & (1 << len))); ++len )
;
return 32 - len; return 32 - len;
} }
@ -165,8 +165,7 @@ rule_attr:
rules_error("internal_error: unknown protocol"); rules_error("internal_error: unknown protocol");
} }
if ( proto ) if ( proto ) {
{
auto* vallist = new zeek::detail::maskedvalue_list; auto* vallist = new zeek::detail::maskedvalue_list;
auto* val = new zeek::detail::MaskedValue(); auto* val = new zeek::detail::MaskedValue();
@ -303,8 +302,7 @@ value_list:
| value_list ',' ranged_value | value_list ',' ranged_value
{ {
int numVals = $3->length(); int numVals = $3->length();
for ( int idx = 0; idx < numVals; idx++ ) for ( int idx = 0; idx < numVals; idx++ ) {
{
zeek::detail::MaskedValue* val = (*$3)[idx]; zeek::detail::MaskedValue* val = (*$3)[idx];
$1->push_back(val); $1->push_back(val);
} }
@ -364,8 +362,7 @@ ranged_value:
TOK_INT '-' TOK_INT TOK_INT '-' TOK_INT
{ {
$$ = new zeek::detail::maskedvalue_list(); $$ = new zeek::detail::maskedvalue_list();
for ( int val = $1; val <= $3; val++ ) for ( int val = $1; val <= $3; val++ ) {
{
auto* masked = new zeek::detail::MaskedValue(); auto* masked = new zeek::detail::MaskedValue();
masked->val = val; masked->val = val;
masked->mask = 0xffffffff; masked->mask = 0xffffffff;
@ -447,29 +444,20 @@ pattern:
%% %%
void rules_error(const char* msg) void rules_error(const char* msg) {
{ zeek::reporter->Error("Error in signature (%s:%d): %s", current_rule_file, rules_line_number + 1, msg);
zeek::reporter->Error("Error in signature (%s:%d): %s",
current_rule_file, rules_line_number+1, msg);
zeek::detail::rule_matcher->SetParseError(); zeek::detail::rule_matcher->SetParseError();
} }
void rules_error(const char* msg, const char* addl) void rules_error(const char* msg, const char* addl) {
{ zeek::reporter->Error("Error in signature (%s:%d): %s (%s)", current_rule_file, rules_line_number + 1, msg, addl);
zeek::reporter->Error("Error in signature (%s:%d): %s (%s)",
current_rule_file, rules_line_number+1, msg, addl);
zeek::detail::rule_matcher->SetParseError(); zeek::detail::rule_matcher->SetParseError();
} }
void rules_error(zeek::detail::Rule* r, const char* msg) void rules_error(zeek::detail::Rule* r, const char* msg) {
{
const zeek::detail::Location& l = r->GetLocation(); const zeek::detail::Location& l = r->GetLocation();
zeek::reporter->Error("Error in signature %s (%s:%d): %s", zeek::reporter->Error("Error in signature %s (%s:%d): %s", r->ID(), l.filename, l.first_line, msg);
r->ID(), l.filename, l.first_line, msg);
zeek::detail::rule_matcher->SetParseError(); zeek::detail::rule_matcher->SetParseError();
} }
int rules_wrap(void) int rules_wrap(void) { return 1; }
{
return 1;
}

View file

@ -204,8 +204,7 @@ finger { rules_lval.val = zeek::detail::Rule::FINGER; return TOK_PATTERN_TYPE; }
{RE} { {RE} {
auto len = strlen(yytext); auto len = strlen(yytext);
if ( yytext[len - 1] == 'i' ) if ( yytext[len - 1] == 'i' ) {
{
*(yytext + len - 2) = '\0'; *(yytext + len - 2) = '\0';
const char fmt[] = "(?i:%s)"; const char fmt[] = "(?i:%s)";
int n = len + strlen(fmt); int n = len + strlen(fmt);
@ -213,8 +212,7 @@ finger { rules_lval.val = zeek::detail::Rule::FINGER; return TOK_PATTERN_TYPE; }
snprintf(s, n + 5, fmt, yytext + 1); snprintf(s, n + 5, fmt, yytext + 1);
rules_lval.str = s; rules_lval.str = s;
} }
else else {
{
*(yytext + len - 1) = '\0'; *(yytext + len - 1) = '\0';
rules_lval.str = zeek::util::copy_string(yytext + 1); rules_lval.str = zeek::util::copy_string(yytext + 1);
} }
@ -227,30 +225,25 @@ finger { rules_lval.val = zeek::detail::Rule::FINGER; return TOK_PATTERN_TYPE; }
%% %%
// We're about to parse a Zeek policy-layer symbol. // We're about to parse a Zeek policy-layer symbol.
void begin_PS() void begin_PS() {
{
BEGIN(PS); BEGIN(PS);
} }
void end_PS() void end_PS() {
{
BEGIN(INITIAL); BEGIN(INITIAL);
} }
static YY_BUFFER_STATE rules_buffer; static YY_BUFFER_STATE rules_buffer;
void rules_set_input_from_buffer(const char* data, size_t size) void rules_set_input_from_buffer(const char* data, size_t size) {
{
rules_buffer = yy_scan_bytes(data, size); // this copies the data rules_buffer = yy_scan_bytes(data, size); // this copies the data
} }
void rules_set_input_from_file(FILE* f) void rules_set_input_from_file(FILE* f) {
{
rules_buffer = yy_create_buffer(f, YY_BUF_SIZE); rules_buffer = yy_create_buffer(f, YY_BUF_SIZE);
} }
void rules_parse_input() void rules_parse_input() {
{
yy_switch_to_buffer(rules_buffer); yy_switch_to_buffer(rules_buffer);
rules_parse(); rules_parse();
yy_delete_buffer(rules_buffer); yy_delete_buffer(rules_buffer);

View file

@ -101,8 +101,7 @@ char last_tok[128];
if ( ((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin) ) \ if ( ((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin) ) \
zeek::reporter->Error("read failed with \"%s\"", strerror(errno)); zeek::reporter->Error("read failed with \"%s\"", strerror(errno));
static std::string find_relative_file(const std::string& filename, const std::string& ext) static std::string find_relative_file(const std::string& filename, const std::string& ext) {
{
if ( filename.empty() ) if ( filename.empty() )
return {}; return {};
@ -112,8 +111,7 @@ static std::string find_relative_file(const std::string& filename, const std::st
return zeek::util::find_file(filename, zeek::util::zeek_path(), ext); return zeek::util::find_file(filename, zeek::util::zeek_path(), ext);
} }
static std::string find_relative_script_file(const std::string& filename) static std::string find_relative_script_file(const std::string& filename) {
{
if ( filename.empty() ) if ( filename.empty() )
return {}; return {};
@ -123,8 +121,7 @@ static std::string find_relative_script_file(const std::string& filename)
return zeek::util::find_script_file(filename, zeek::util::zeek_path()); return zeek::util::find_script_file(filename, zeek::util::zeek_path());
} }
static void start_conditional() static void start_conditional() {
{
++conditional_depth; ++conditional_depth;
++conditional_epoch; ++conditional_epoch;
@ -135,8 +132,7 @@ static void start_conditional()
current_file_has_conditionals = true; current_file_has_conditionals = true;
} }
static void do_pragma(const std::string& pragma) static void do_pragma(const std::string& pragma) {
{
static std::unordered_set<std::string> known_stack_pragmas = { static std::unordered_set<std::string> known_stack_pragmas = {
"ignore-deprecations", "ignore-deprecations",
}; };
@ -145,16 +141,13 @@ static void do_pragma(const std::string& pragma)
auto new_end = std::remove(parts.begin(), parts.end(), ""); auto new_end = std::remove(parts.begin(), parts.end(), "");
parts.erase(new_end, parts.end()); parts.erase(new_end, parts.end());
if ( parts.empty() ) if ( parts.empty() ) {
{
zeek::reporter->FatalError("@pragma without value"); zeek::reporter->FatalError("@pragma without value");
return; return;
} }
if ( parts[0] == "push" ) if ( parts[0] == "push" ) {
{ if ( parts.size() < 2 ) {
if ( parts.size() < 2 )
{
zeek::reporter->FatalError("@pragma push without value"); zeek::reporter->FatalError("@pragma push without value");
return; return;
} }
@ -164,10 +157,8 @@ static void do_pragma(const std::string& pragma)
pragma_stack.push_back(parts[1]); pragma_stack.push_back(parts[1]);
} }
else if ( parts[0] == "pop" ) else if ( parts[0] == "pop" ) {
{ if ( pragma_stack.empty() || pragma_stack.size() == entry_pragma_stack_depth.back() ) {
if ( pragma_stack.empty() || pragma_stack.size() == entry_pragma_stack_depth.back() )
{
zeek::reporter->FatalError("@pragma pop without active pragma"); zeek::reporter->FatalError("@pragma pop without active pragma");
return; return;
} }
@ -175,17 +166,15 @@ static void do_pragma(const std::string& pragma)
// Popping with a value: Verify it's popping the right thing. Not providing // Popping with a value: Verify it's popping the right thing. Not providing
// a pop value itself is valid. Don't return, probably blows up below anyway. // a pop value itself is valid. Don't return, probably blows up below anyway.
if ( parts.size() > 1 && pragma_stack.back() != parts[1] ) { if ( parts.size() > 1 && pragma_stack.back() != parts[1] ) {
zeek::reporter->Error("@pragma pop with unexpected '%s', expected '%s'", zeek::reporter->Error("@pragma pop with unexpected '%s', expected '%s'", parts[1].c_str(),
parts[1].c_str(), pragma_stack.back().c_str()); pragma_stack.back().c_str());
} }
// Just pop anything // Just pop anything
pragma_stack.pop_back(); pragma_stack.pop_back();
} }
else else
{
zeek::reporter->Warning("Ignoring unknown pragma: '%s'", pragma.c_str()); zeek::reporter->Warning("Ignoring unknown pragma: '%s'", pragma.c_str());
}
// Cheap: Just walk the pragma_stack now to see if we should ignore deprecations. // Cheap: Just walk the pragma_stack now to see if we should ignore deprecations.
bool ignore_deprecations = std::any_of(pragma_stack.cbegin(), pragma_stack.cend(), bool ignore_deprecations = std::any_of(pragma_stack.cbegin(), pragma_stack.cend(),
@ -260,8 +249,7 @@ ESCSEQ (\\([^\r\n]|[0-7]+|x[[:xdigit:]]+))
} }
##<.* { ##<.* {
if ( zeek::detail::current_scope() == zeek::detail::global_scope() ) if ( zeek::detail::current_scope() == zeek::detail::global_scope() ) {
{
std::string hint(cur_enum_type && last_id_tok ? std::string hint(cur_enum_type && last_id_tok ?
zeek::detail::make_full_var_name(zeek::detail::current_module.c_str(), last_id_tok) : ""); zeek::detail::make_full_var_name(zeek::detail::current_module.c_str(), last_id_tok) : "");
@ -413,8 +401,7 @@ when return TOK_WHEN;
auto comment = zeek::util::skip_whitespace(yytext + 11); auto comment = zeek::util::skip_whitespace(yytext + 11);
std::string loaded_from; std::string loaded_from;
if ( num_files > 0 ) if ( num_files > 0 ) {
{
auto lf = file_stack[num_files - 1]; auto lf = file_stack[num_files - 1];
if ( lf->name ) if ( lf->name )
loaded_from = zeek::util::fmt(" from %s:%d", lf->name, lf->line); loaded_from = zeek::util::fmt(" from %s:%d", lf->name, lf->line);
@ -434,8 +421,7 @@ when return TOK_WHEN;
@DIR { @DIR {
std::string rval = zeek::util::SafeDirname(::filename).result; std::string rval = zeek::util::SafeDirname(::filename).result;
if ( ! rval.empty() && rval[0] == '.' ) if ( ! rval.empty() && rval[0] == '.' ) {
{
char path[MAXPATHLEN]; char path[MAXPATHLEN];
if ( ! getcwd(path, MAXPATHLEN) ) if ( ! getcwd(path, MAXPATHLEN) )
@ -564,8 +550,7 @@ when return TOK_WHEN;
const char* file = zeek::util::skip_whitespace(yytext + 7); const char* file = zeek::util::skip_whitespace(yytext + 7);
std::string path = find_relative_script_file(file); std::string path = find_relative_script_file(file);
if ( ! path.empty() && zeek::util::is_dir(path.c_str()) ) if ( ! path.empty() && zeek::util::is_dir(path.c_str()) ) {
{
// open_package() appends __load__.zeek to path and verifies existence // open_package() appends __load__.zeek to path and verifies existence
FILE *f = zeek::util::detail::open_package(path); FILE *f = zeek::util::detail::open_package(path);
if (f) if (f)
@ -588,8 +573,7 @@ when return TOK_WHEN;
char* pref = zeek::util::skip_whitespace(yytext + 9); // Skip "@prefixes". char* pref = zeek::util::skip_whitespace(yytext + 9); // Skip "@prefixes".
int append = 0; int append = 0;
if ( *pref == '+' ) if ( *pref == '+' ) {
{
append = 1; append = 1;
++pref; ++pref;
} }
@ -638,8 +622,7 @@ F RET_CONST(zeek::val_mgr->False()->Ref())
{D}"/tcp" { {D}"/tcp" {
uint32_t p = atoi(yytext); uint32_t p = atoi(yytext);
if ( p > 65535 ) if ( p > 65535 ) {
{
zeek::reporter->Error("bad port number - %s", yytext); zeek::reporter->Error("bad port number - %s", yytext);
p = 0; p = 0;
} }
@ -647,8 +630,7 @@ F RET_CONST(zeek::val_mgr->False()->Ref())
} }
{D}"/udp" { {D}"/udp" {
uint32_t p = atoi(yytext); uint32_t p = atoi(yytext);
if ( p > 65535 ) if ( p > 65535 ) {
{
zeek::reporter->Error("bad port number - %s", yytext); zeek::reporter->Error("bad port number - %s", yytext);
p = 0; p = 0;
} }
@ -656,8 +638,7 @@ F RET_CONST(zeek::val_mgr->False()->Ref())
} }
{D}"/icmp" { {D}"/icmp" {
uint32_t p = atoi(yytext); uint32_t p = atoi(yytext);
if ( p > 255 ) if ( p > 255 ) {
{
zeek::reporter->Error("bad port number - %s", yytext); zeek::reporter->Error("bad port number - %s", yytext);
p = 0; p = 0;
} }
@ -665,8 +646,7 @@ F RET_CONST(zeek::val_mgr->False()->Ref())
} }
{D}"/unknown" { {D}"/unknown" {
uint32_t p = atoi(yytext); uint32_t p = atoi(yytext);
if ( p > 255 ) if ( p > 255 ) {
{
zeek::reporter->Error("bad port number - %s", yytext); zeek::reporter->Error("bad port number - %s", yytext);
p = 0; p = 0;
} }
@ -692,16 +672,13 @@ F RET_CONST(zeek::val_mgr->False()->Ref())
char* s = new char[len]; char* s = new char[len];
// Skip leading quote. // Skip leading quote.
for ( ++text; *text; ++text ) for ( ++text; *text; ++text ) {
{ if ( *text == '\\' ) {
if ( *text == '\\' )
{
++text; // skip '\' ++text; // skip '\'
s[i++] = zeek::util::detail::expand_escape(text); s[i++] = zeek::util::detail::expand_escape(text);
--text; // point to end of sequence --text; // point to end of sequence
} }
else else {
{
s[i++] = *text; s[i++] = *text;
if ( i >= len ) if ( i >= len )
zeek::reporter->InternalError("bad string length computation"); zeek::reporter->InternalError("bad string length computation");
@ -732,13 +709,11 @@ F RET_CONST(zeek::val_mgr->False()->Ref())
<RE>(\/[is]{0,2}) { <RE>(\/[is]{0,2}) {
BEGIN(INITIAL); BEGIN(INITIAL);
if ( strlen(yytext) == 2 ) if ( strlen(yytext) == 2 ) {
{
yylval.re_modes.ignore_case = (yytext[1] == 'i'); yylval.re_modes.ignore_case = (yytext[1] == 'i');
yylval.re_modes.single_line = (yytext[1] == 's'); yylval.re_modes.single_line = (yytext[1] == 's');
} }
else else {
{
if ( yytext[1] == yytext[2] ) if ( yytext[1] == yytext[2] )
zeek::reporter->Error("pattern has duplicate mode %c", yytext[1]); zeek::reporter->Error("pattern has duplicate mode %c", yytext[1]);
@ -759,8 +734,7 @@ F RET_CONST(zeek::val_mgr->False()->Ref())
%% %%
YYLTYPE zeek::detail::GetCurrentLocation() YYLTYPE zeek::detail::GetCurrentLocation() {
{
static YYLTYPE currloc; static YYLTYPE currloc;
currloc.filename = filename; currloc.filename = filename;
@ -769,14 +743,12 @@ YYLTYPE zeek::detail::GetCurrentLocation()
return currloc; return currloc;
} }
void zeek::detail::SetCurrentLocation(YYLTYPE currloc) void zeek::detail::SetCurrentLocation(YYLTYPE currloc) {
{
::filename = currloc.filename; ::filename = currloc.filename;
line_number = currloc.first_line; line_number = currloc.first_line;
} }
static int switch_to(const char* file, YY_BUFFER_STATE buffer) static int switch_to(const char* file, YY_BUFFER_STATE buffer) {
{
yy_switch_to_buffer(buffer); yy_switch_to_buffer(buffer);
yylloc.first_line = yylloc.last_line = line_number = 1; yylloc.first_line = yylloc.last_line = line_number = 1;
@ -791,18 +763,18 @@ static int switch_to(const char* file, YY_BUFFER_STATE buffer)
return 1; return 1;
} }
static int load_files(const char* orig_file) {
static int load_files(const char* orig_file)
{
std::string file_path = find_relative_script_file(orig_file); std::string file_path = find_relative_script_file(orig_file);
std::pair<int, std::optional<std::string>> rc = {-1, std::nullopt}; std::pair<int, std::optional<std::string>> rc = {-1, std::nullopt};
rc.first = PLUGIN_HOOK_WITH_RESULT(HOOK_LOAD_FILE, HookLoadFile(zeek::plugin::Plugin::SCRIPT, orig_file, file_path), -1); rc.first =
PLUGIN_HOOK_WITH_RESULT(HOOK_LOAD_FILE, HookLoadFile(zeek::plugin::Plugin::SCRIPT, orig_file, file_path), -1);
if ( rc.first < 0 ) if ( rc.first < 0 )
rc = PLUGIN_HOOK_WITH_RESULT(HOOK_LOAD_FILE_EXT, HookLoadFileExtended(zeek::plugin::Plugin::SCRIPT, orig_file, file_path), std::make_pair(-1, std::nullopt)); rc = PLUGIN_HOOK_WITH_RESULT(HOOK_LOAD_FILE_EXT,
HookLoadFileExtended(zeek::plugin::Plugin::SCRIPT, orig_file, file_path),
std::make_pair(-1, std::nullopt));
if ( rc.first == 0 ) if ( rc.first == 0 ) {
{
if ( ! zeek::reporter->Errors() ) if ( ! zeek::reporter->Errors() )
// This is just in case the plugin failed to report // This is just in case the plugin failed to report
// the error itself, in which case we want to at // the error itself, in which case we want to at
@ -817,22 +789,18 @@ static int load_files(const char* orig_file)
FILE* f = nullptr; FILE* f = nullptr;
if ( rc.first == -1 ) if ( rc.first == -1 ) {
{ if ( zeek::util::streq(orig_file, "-") ) {
if ( zeek::util::streq(orig_file, "-") )
{
f = stdin; f = stdin;
file_path = zeek::detail::ScannedFile::canonical_stdin_path; file_path = zeek::detail::ScannedFile::canonical_stdin_path;
if ( zeek::detail::g_policy_debug ) if ( zeek::detail::g_policy_debug ) {
{ zeek::detail::debug_msg(
zeek::detail::debug_msg("Warning: can't use debugger while reading policy from stdin; turning off debugging.\n"); "Warning: can't use debugger while reading policy from stdin; turning off debugging.\n");
zeek::detail::g_policy_debug = false; zeek::detail::g_policy_debug = false;
} }
} }
else {
else
{
if ( file_path.empty() ) if ( file_path.empty() )
zeek::reporter->FatalError("can't find %s", orig_file); zeek::reporter->FatalError("can't find %s", orig_file);
@ -846,8 +814,7 @@ static int load_files(const char* orig_file)
} }
zeek::detail::ScannedFile sf(file_stack.length(), file_path); zeek::detail::ScannedFile sf(file_stack.length(), file_path);
if ( sf.AlreadyScanned() ) if ( sf.AlreadyScanned() ) {
{
if ( rc.first == -1 && f != stdin ) if ( rc.first == -1 && f != stdin )
fclose(f); fclose(f);
@ -857,8 +824,7 @@ static int load_files(const char* orig_file)
zeek::detail::files_scanned.push_back(std::move(sf)); zeek::detail::files_scanned.push_back(std::move(sf));
} }
if ( zeek::detail::g_policy_debug && ! file_path.empty() ) if ( zeek::detail::g_policy_debug && ! file_path.empty() ) {
{
// Add the filename to the file mapping table (Debug.h). // Add the filename to the file mapping table (Debug.h).
zeek::detail::Filemap* map = new zeek::detail::Filemap; zeek::detail::Filemap* map = new zeek::detail::Filemap;
zeek::detail::g_dbgfilemaps.emplace(file_path, map); zeek::detail::g_dbgfilemaps.emplace(file_path, map);
@ -875,35 +841,32 @@ static int load_files(const char* orig_file)
// and will be zapped after the yy_switch_to_buffer() below. // and will be zapped after the yy_switch_to_buffer() below.
YY_BUFFER_STATE buffer; YY_BUFFER_STATE buffer;
if ( rc.first == 1 ) if ( rc.first == 1 ) {
{
// Parse code provided by plugin. // Parse code provided by plugin.
assert(rc.second); assert(rc.second);
DBG_LOG(zeek::DBG_SCRIPTS, "Loading %s from code supplied by plugin ", file_path.c_str()); DBG_LOG(zeek::DBG_SCRIPTS, "Loading %s from code supplied by plugin ", file_path.c_str());
buffer = yy_scan_bytes(rc.second->data(), rc.second->size()); // this copies the data buffer = yy_scan_bytes(rc.second->data(), rc.second->size()); // this copies the data
} }
else else {
{
// Parse from file. // Parse from file.
assert(f); assert(f);
DBG_LOG(zeek::DBG_SCRIPTS, "Loading %s", file_path.c_str()); DBG_LOG(zeek::DBG_SCRIPTS, "Loading %s", file_path.c_str());
buffer = yy_create_buffer(f, YY_BUF_SIZE); buffer = yy_create_buffer(f, YY_BUF_SIZE);
} }
yy_switch_to_buffer(buffer);
yylloc.first_line = yylloc.last_line = line_number = 1;
return switch_to(file_path.c_str(), buffer); return switch_to(file_path.c_str(), buffer);
} }
void begin_RE() void begin_RE() { BEGIN(RE); }
{
BEGIN(RE);
}
class LocalNameFinder final : public zeek::detail::TraversalCallback { class LocalNameFinder final : public zeek::detail::TraversalCallback {
public: public:
LocalNameFinder() = default; LocalNameFinder() = default;
zeek::detail::TraversalCode PreExpr(const zeek::detail::Expr* expr) override zeek::detail::TraversalCode PreExpr(const zeek::detail::Expr* expr) override {
{
if ( expr->Tag() != EXPR_NAME ) if ( expr->Tag() != EXPR_NAME )
return zeek::detail::TC_CONTINUE; return zeek::detail::TC_CONTINUE;
@ -919,20 +882,17 @@ public:
std::vector<const zeek::detail::NameExpr*> local_names; std::vector<const zeek::detail::NameExpr*> local_names;
}; };
static void begin_ignoring() static void begin_ignoring() {
{
if_stack.push_back(conditional_depth); if_stack.push_back(conditional_depth);
BEGIN(IGNORE); BEGIN(IGNORE);
} }
static void resume_processing() static void resume_processing() {
{
if_stack.pop_back(); if_stack.pop_back();
BEGIN(INITIAL); BEGIN(INITIAL);
} }
void do_atif(zeek::detail::Expr* expr) void do_atif(zeek::detail::Expr* expr) {
{
start_conditional(); start_conditional();
LocalNameFinder cb; LocalNameFinder cb;
@ -941,14 +901,12 @@ void do_atif(zeek::detail::Expr* expr)
if ( cb.local_names.empty() ) if ( cb.local_names.empty() )
val = expr->Eval(nullptr); val = expr->Eval(nullptr);
else else {
{
for ( size_t i = 0; i < cb.local_names.size(); ++i ) for ( size_t i = 0; i < cb.local_names.size(); ++i )
cb.local_names[i]->Error("referencing a local name in @if"); cb.local_names[i]->Error("referencing a local name in @if");
} }
if ( ! val ) if ( ! val ) {
{
expr->Error("invalid expression in @if"); expr->Error("invalid expression in @if");
return; return;
} }
@ -957,8 +915,7 @@ void do_atif(zeek::detail::Expr* expr)
begin_ignoring(); begin_ignoring();
} }
void do_atifdef(const char* id) void do_atifdef(const char* id) {
{
start_conditional(); start_conditional();
const auto& i = zeek::detail::lookup_ID(id, zeek::detail::current_module.c_str()); const auto& i = zeek::detail::lookup_ID(id, zeek::detail::current_module.c_str());
@ -972,8 +929,7 @@ void do_atifdef(const char* id)
begin_ignoring(); begin_ignoring();
} }
void do_atifndef(const char *id) void do_atifndef(const char* id) {
{
start_conditional(); start_conditional();
const auto& i = zeek::detail::lookup_ID(id, zeek::detail::current_module.c_str()); const auto& i = zeek::detail::lookup_ID(id, zeek::detail::current_module.c_str());
@ -987,8 +943,7 @@ void do_atifndef(const char *id)
begin_ignoring(); begin_ignoring();
} }
void do_atelse() void do_atelse() {
{
if ( conditional_depth == 0 ) if ( conditional_depth == 0 )
zeek::reporter->Error("@else without @if..."); zeek::reporter->Error("@else without @if...");
@ -1001,8 +956,7 @@ void do_atelse()
resume_processing(); resume_processing();
} }
void do_atendif() void do_atendif() {
{
if ( conditional_depth <= entry_cond_depth.back() ) if ( conditional_depth <= entry_cond_depth.back() )
zeek::reporter->Error("unbalanced @if... @endif"); zeek::reporter->Error("unbalanced @if... @endif");
@ -1013,15 +967,13 @@ void do_atendif()
} }
// If the given Stmt is a directive, trigger an error so that its usage is rejected. // If the given Stmt is a directive, trigger an error so that its usage is rejected.
void reject_directive(zeek::detail::Stmt* s) void reject_directive(zeek::detail::Stmt* s) {
{
if ( s->Tag() == STMT_NULL ) if ( s->Tag() == STMT_NULL )
if ( s->AsNullStmt()->IsDirective() ) if ( s->AsNullStmt()->IsDirective() )
zeek::reporter->Error("incorrect use of directive"); zeek::reporter->Error("incorrect use of directive");
} }
void add_essential_input_file(const char* file) void add_essential_input_file(const char* file) {
{
if ( ! file ) if ( ! file )
zeek::reporter->InternalError("empty filename"); zeek::reporter->InternalError("empty filename");
@ -1031,8 +983,7 @@ void add_essential_input_file(const char* file)
essential_input_files.push_back(zeek::util::copy_string(file)); essential_input_files.push_back(zeek::util::copy_string(file));
} }
void add_input_file(const char* file) void add_input_file(const char* file) {
{
if ( ! file ) if ( ! file )
zeek::reporter->InternalError("empty filename"); zeek::reporter->InternalError("empty filename");
@ -1042,8 +993,7 @@ void add_input_file(const char* file)
input_files.push_back(zeek::util::copy_string(file)); input_files.push_back(zeek::util::copy_string(file));
} }
void add_input_file_at_front(const char* file) void add_input_file_at_front(const char* file) {
{
if ( ! file ) if ( ! file )
zeek::reporter->InternalError("empty filename"); zeek::reporter->InternalError("empty filename");
@ -1053,10 +1003,8 @@ void add_input_file_at_front(const char* file)
input_files.push_front(zeek::util::copy_string(file)); input_files.push_front(zeek::util::copy_string(file));
} }
void add_to_name_list(char* s, char delim, zeek::name_list& nl) void add_to_name_list(char* s, char delim, zeek::name_list& nl) {
{ while ( s ) {
while ( s )
{
char* s_delim = strchr(s, delim); char* s_delim = strchr(s, delim);
if ( s_delim ) if ( s_delim )
*s_delim = 0; *s_delim = 0;
@ -1070,17 +1018,14 @@ void add_to_name_list(char* s, char delim, zeek::name_list& nl)
} }
} }
int yywrap() int yywrap() {
{ if ( entry_cond_depth.size() > 0 ) {
if ( entry_cond_depth.size() > 0 )
{
if ( conditional_depth > entry_cond_depth.back() ) if ( conditional_depth > entry_cond_depth.back() )
zeek::reporter->FatalError("unbalanced @if... @endif"); zeek::reporter->FatalError("unbalanced @if... @endif");
entry_cond_depth.pop_back(); entry_cond_depth.pop_back();
} }
if ( entry_pragma_stack_depth.size() > 0 ) if ( entry_pragma_stack_depth.size() > 0 ) {
{
if ( pragma_stack.size() > entry_pragma_stack_depth.back() ) if ( pragma_stack.size() > entry_pragma_stack_depth.back() )
zeek::reporter->FatalError("unbalanced @pragma push pop in file"); zeek::reporter->FatalError("unbalanced @pragma push pop in file");
@ -1097,21 +1042,17 @@ int yywrap()
if ( file_stack.length() > 0 ) if ( file_stack.length() > 0 )
delete file_stack.remove_nth(file_stack.length() - 1); delete file_stack.remove_nth(file_stack.length() - 1);
if ( YY_CURRENT_BUFFER ) if ( YY_CURRENT_BUFFER ) {
{
// There's more on the stack to scan. // There's more on the stack to scan.
current_file_has_conditionals = files_with_conditionals.count(::filename) > 0; current_file_has_conditionals = files_with_conditionals.count(::filename) > 0;
return 0; return 0;
} }
// Stack is now empty. // Stack is now empty.
while ( essential_input_files.length() > 0 || input_files.length() > 0 ) while ( essential_input_files.length() > 0 || input_files.length() > 0 ) {
{ zeek::name_list& files = essential_input_files.length() > 0 ? essential_input_files : input_files;
zeek::name_list& files = essential_input_files.length() > 0 ?
essential_input_files : input_files;
if ( load_files(files[0]) ) if ( load_files(files[0]) ) {
{
// Don't delete the filename - it's pointed to by // Don't delete the filename - it's pointed to by
// every Obj created when parsing it. // every Obj created when parsing it.
(void)files.remove_nth(0); (void)files.remove_nth(0);
@ -1131,15 +1072,13 @@ int yywrap()
// file name is discarded. If the prefix is non-empty, it gets placed // file name is discarded. If the prefix is non-empty, it gets placed
// in front of the flattened path, separated with another '.' // in front of the flattened path, separated with another '.'
bool found_prefixed_files = false; bool found_prefixed_files = false;
for ( auto& scanned_file : zeek::detail::files_scanned ) for ( auto& scanned_file : zeek::detail::files_scanned ) {
{
if ( scanned_file.skipped || scanned_file.prefixes_checked ) if ( scanned_file.skipped || scanned_file.prefixes_checked )
continue; continue;
scanned_file.prefixes_checked = true; scanned_file.prefixes_checked = true;
// Prefixes are pushed onto a stack, so iterate backwards. // Prefixes are pushed onto a stack, so iterate backwards.
for ( int i = zeek::detail::zeek_script_prefixes.size() - 1; i >= 0; --i ) for ( int i = zeek::detail::zeek_script_prefixes.size() - 1; i >= 0; --i ) {
{
// Don't look at empty prefixes. // Don't look at empty prefixes.
if ( ! zeek::detail::zeek_script_prefixes[i][0] ) if ( ! zeek::detail::zeek_script_prefixes[i][0] )
continue; continue;
@ -1148,8 +1087,7 @@ int yywrap()
std::string flat = zeek::util::detail::flatten_script_name(canon, zeek::detail::zeek_script_prefixes[i]); std::string flat = zeek::util::detail::flatten_script_name(canon, zeek::detail::zeek_script_prefixes[i]);
std::string path = find_relative_script_file(flat); std::string path = find_relative_script_file(flat);
if ( ! path.empty() ) if ( ! path.empty() ) {
{
add_input_file(path.c_str()); add_input_file(path.c_str());
found_prefixed_files = true; found_prefixed_files = true;
} }
@ -1167,15 +1105,14 @@ int yywrap()
return 0; return 0;
// Add redef statements for any X=Y command line parameters. // Add redef statements for any X=Y command line parameters.
if ( ! zeek::detail::params.empty() ) if ( ! zeek::detail::params.empty() ) {
{
std::string policy; std::string policy;
for ( const auto& pi : zeek::detail::params ) for ( const auto& pi : zeek::detail::params ) {
{
auto p = pi.data(); auto p = pi.data();
while ( isalnum(*p) || *p == '_' || *p == ':' ) ++p; while ( isalnum(*p) || *p == '_' || *p == ':' )
++p;
auto first_non_id_char = p - pi.data(); auto first_non_id_char = p - pi.data();
auto eq_idx = pi.find('=', first_non_id_char); auto eq_idx = pi.find('=', first_non_id_char);
@ -1185,10 +1122,8 @@ int yywrap()
auto val_str = pi.substr(eq_idx + 1); auto val_str = pi.substr(eq_idx + 1);
const auto& id = zeek::id::find(id_str); const auto& id = zeek::id::find(id_str);
if ( ! id ) if ( ! id ) {
{ zeek::reporter->Error("unknown identifier '%s' in command-line options", id_str.data());
zeek::reporter->Error("unknown identifier '%s' in command-line options",
id_str.data());
continue; continue;
} }
@ -1196,23 +1131,24 @@ int yywrap()
// So far, that just means quoting the value for string types. // So far, that just means quoting the value for string types.
const auto& type = id->GetType(); const auto& type = id->GetType();
if ( ! type ) if ( ! type ) {
{ zeek::reporter->Error(
zeek::reporter->Error("can't set value of '%s' in command-line " "can't set value of '%s' in command-line "
"options: unknown type", id_str.data()); "options: unknown type",
id_str.data());
continue; continue;
} }
if ( val_str.empty() && ! zeek::IsString(type->Tag()) ) if ( val_str.empty() && ! zeek::IsString(type->Tag()) ) {
{ zeek::reporter->Error(
zeek::reporter->Error("must assign non-empty value to '%s' in " "must assign non-empty value to '%s' in "
"command-line options", id_str.data()); "command-line options",
id_str.data());
continue; continue;
} }
auto use_quotes = zeek::IsString(type->Tag()); auto use_quotes = zeek::IsString(type->Tag());
auto fmt_str = use_quotes ? "redef %s %s= \"%s\";" auto fmt_str = use_quotes ? "redef %s %s= \"%s\";" : "redef %s %s= %s;";
: "redef %s %s= %s;";
policy += zeek::util::fmt(fmt_str, id_str.data(), op.data(), val_str.data()); policy += zeek::util::fmt(fmt_str, id_str.data(), op.data(), val_str.data());
} }
@ -1228,8 +1164,7 @@ int yywrap()
// Use a synthetic filename, and add an extra semicolon on its own // Use a synthetic filename, and add an extra semicolon on its own
// line (so that things like @load work), so that a semicolon is // line (so that things like @load work), so that a semicolon is
// not strictly necessary. // not strictly necessary.
if ( zeek::detail::command_line_policy ) if ( zeek::detail::command_line_policy ) {
{
int tmp_len = strlen(zeek::detail::command_line_policy) + 32; int tmp_len = strlen(zeek::detail::command_line_policy) + 32;
char* tmp = new char[tmp_len]; char* tmp = new char[tmp_len];
snprintf(tmp, tmp_len, "%s\n;\n", zeek::detail::command_line_policy); snprintf(tmp, tmp_len, "%s\n;\n", zeek::detail::command_line_policy);
@ -1248,16 +1183,14 @@ int yywrap()
return 1; return 1;
} }
FileInfo::FileInfo(std::string arg_restore_module) FileInfo::FileInfo(std::string arg_restore_module) {
{
buffer_state = YY_CURRENT_BUFFER; buffer_state = YY_CURRENT_BUFFER;
restore_module = arg_restore_module; restore_module = arg_restore_module;
name = ::filename; name = ::filename;
line = ::line_number; line = ::line_number;
} }
FileInfo::~FileInfo() FileInfo::~FileInfo() {
{
if ( yyin && yyin != stdin ) if ( yyin && yyin != stdin )
fclose(yyin); fclose(yyin);