GH-2034: Store module names and use them in lookups for ifdef

This commit is contained in:
Tim Wojtulewicz 2022-08-02 12:19:36 -07:00 committed by Tim Wojtulewicz
parent 686e740bbe
commit 1870d26684
6 changed files with 88 additions and 4 deletions

View file

@ -156,6 +156,8 @@ 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;
std::set<std::string> module_names;
static void parse_new_enum(void) static void parse_new_enum(void)
{ {
// Starting a new enum definition. // Starting a new enum definition.
@ -1269,6 +1271,7 @@ decl:
TOK_MODULE TOK_ID ';' TOK_MODULE TOK_ID ';'
{ {
current_module = $2; current_module = $2;
module_names.insert($2);
zeekygen_mgr->ModuleUsage(::filename, current_module); zeekygen_mgr->ModuleUsage(::filename, current_module);
} }

View file

@ -80,6 +80,8 @@ const char* last_tok_filename = 0;
const char* last_last_tok_filename = 0; const char* last_last_tok_filename = 0;
char last_tok[128]; char last_tok[128];
extern std::set<std::string> module_names;
#define YY_USER_ACTION strncpy(last_tok, yytext, sizeof(last_tok) - 1); \ #define YY_USER_ACTION strncpy(last_tok, yytext, sizeof(last_tok) - 1); \
last_last_tok_filename = last_tok_filename; \ last_last_tok_filename = last_tok_filename; \
last_tok_filename = ::filename; last_tok_filename = ::filename;
@ -796,7 +798,12 @@ void do_atifdef(const char* id)
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());
if ( ! i ) // If the ID didn't exist, look to see if this is a module name instead.
bool found = (i != nullptr);
if ( ! found )
found = (module_names.count(id) != 0);
if ( ! found )
begin_ignoring(); begin_ignoring();
} }
@ -806,7 +813,12 @@ void do_atifndef(const char *id)
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());
if ( i ) // If the ID didn't exist, look to see if this is a module name instead.
bool found = (i != nullptr);
if ( ! found )
found = (module_names.count(id) != 0);
if ( found )
begin_ignoring(); begin_ignoring();
} }

View file

@ -2,3 +2,5 @@
@ifdef (PASS) @ifdef (PASS)
@ifdef...@else (PASS) @ifdef...@else (PASS)
@ifdef...@else (PASS) @ifdef...@else (PASS)
@ifdef module name (PASS)
@ifdef child variable (PASS)

View file

@ -2,3 +2,5 @@
@ifndef (PASS) @ifndef (PASS)
@ifndef...@else (PASS) @ifndef...@else (PASS)
@ifndef...@else (PASS) @ifndef...@else (PASS)
@ifndef module name (PASS)
@ifndef child variable (PASS)

View file

@ -1,7 +1,9 @@
# @TEST-EXEC: zeek -b %INPUT >out # @TEST-EXEC: zeek -b bar.zeek main.zeek >out
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff .stderr # @TEST-EXEC: btest-diff .stderr
@TEST-START-FILE main.zeek
function test_case(msg: string, expect: bool) function test_case(msg: string, expect: bool)
{ {
print fmt("%s (%s)", msg, expect ? "PASS" : "FAIL"); print fmt("%s (%s)", msg, expect ? "PASS" : "FAIL");
@ -46,5 +48,35 @@ event zeek_init()
test_case( "@ifdef...@else", xyz == 1 ); test_case( "@ifdef...@else", xyz == 1 );
xyz = 0;
@ifdef ( Bar )
xyz += 1;
@else
xyz += 2;
@endif
test_case( "@ifdef module name", xyz == 1 );
xyz = 0;
@ifdef ( Bar::exists )
xyz += 1;
@else
xyz += 2;
@endif
test_case( "@ifdef child variable", xyz == 1 );
} }
@TEST-END-FILE
@TEST-START-FILE bar.zeek
module Bar;
export {
option exists = T;
}
@TEST-END-FILE

View file

@ -1,7 +1,9 @@
# @TEST-EXEC: zeek -b %INPUT >out # @TEST-EXEC: zeek -b bar.zeek main.zeek >out
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff .stderr # @TEST-EXEC: btest-diff .stderr
@TEST-START-FILE main.zeek
function test_case(msg: string, expect: bool) function test_case(msg: string, expect: bool)
{ {
print fmt("%s (%s)", msg, expect ? "PASS" : "FAIL"); print fmt("%s (%s)", msg, expect ? "PASS" : "FAIL");
@ -46,5 +48,36 @@ event zeek_init()
test_case( "@ifndef...@else", xyz == 2 ); test_case( "@ifndef...@else", xyz == 2 );
xyz = 0;
@ifndef ( Bar )
xyz += 1;
@else
xyz += 2;
@endif
test_case( "@ifndef module name", xyz == 2 );
xyz = 0;
@ifndef ( Bar::exists )
xyz += 1;
@else
xyz += 2;
@endif
test_case( "@ifndef child variable", xyz == 2 );
} }
@TEST-END-FILE
@TEST-START-FILE bar.zeek
module Bar;
export {
option exists = T;
}
@TEST-END-FILE