bifcl: Run clang-format on c++ code embedded in builtin-func.{l,y}

This commit is contained in:
Tim Wojtulewicz 2025-08-21 15:12:59 +00:00
parent 31b65f70da
commit 22d82edc74
2 changed files with 391 additions and 441 deletions

View file

@ -143,11 +143,10 @@ HEX [0-9a-fA-F]+
} }
%% %%
int yywrap() int yywrap() {
{
yy_delete_buffer(YY_CURRENT_BUFFER); yy_delete_buffer(YY_CURRENT_BUFFER);
return 1; return 1;
} }
extern int yyparse(); extern int yyparse();
char* input_filename = nullptr; char* input_filename = nullptr;
@ -171,29 +170,25 @@ void close_if_open(FILE **fpp);
void close_all_output_files(void); void close_all_output_files(void);
FILE* open_output_file(const char* surfix) FILE* open_output_file(const char* surfix) {
{
char fn[1024]; char fn[1024];
FILE* fp; FILE* fp;
snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix); snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix);
if ( (fp = fopen(fn, "w")) == NULL ) if ( (fp = fopen(fn, "w")) == NULL ) {
{
fprintf(stderr, "Error: cannot open file: %s\n", fn); fprintf(stderr, "Error: cannot open file: %s\n", fn);
err_exit(); err_exit();
} }
return fp; return fp;
} }
void usage() void usage() {
{
fprintf(stderr, "usage: bifcl [-p <plugin> | -s] *.bif\n"); fprintf(stderr, "usage: bifcl [-p <plugin> | -s] *.bif\n");
exit(1); exit(1);
} }
void init_alternative_mode() void init_alternative_mode() {
{
fp_zeek_init = open_output_file("zeek"); fp_zeek_init = open_output_file("zeek");
fp_func_h = open_output_file("h"); fp_func_h = open_output_file("h");
fp_func_def = open_output_file("cc"); fp_func_def = open_output_file("cc");
@ -208,8 +203,7 @@ void init_alternative_mode()
auto auto_gen_comment_buf = std::make_unique<char[]>(n); auto auto_gen_comment_buf = std::make_unique<char[]>(n);
auto auto_gen_comment = auto_gen_comment_buf.get(); auto auto_gen_comment = auto_gen_comment_buf.get();
snprintf(auto_gen_comment, n, snprintf(auto_gen_comment, n, "This file was automatically generated by bifcl from %s (%s mode).",
"This file was automatically generated by bifcl from %s (%s mode).",
input_filename_with_path, plugin ? "plugin" : "alternative"); input_filename_with_path, plugin ? "plugin" : "alternative");
fprintf(fp_zeek_init, "# %s\n\n", auto_gen_comment); fprintf(fp_zeek_init, "# %s\n\n", auto_gen_comment);
@ -222,16 +216,14 @@ void init_alternative_mode()
fprintf(fp_func_register, "// %s\n\n", auto_gen_comment); fprintf(fp_func_register, "// %s\n\n", auto_gen_comment);
static char guard[1024]; static char guard[1024];
if ( getcwd(guard, sizeof(guard)) == NULL ) if ( getcwd(guard, sizeof(guard)) == NULL ) {
{
fprintf(stderr, "Error: cannot get current working directory\n"); fprintf(stderr, "Error: cannot get current working directory\n");
err_exit(); err_exit();
} }
strncat(guard, "/", sizeof(guard) - strlen(guard) - 1); strncat(guard, "/", sizeof(guard) - strlen(guard) - 1);
strncat(guard, input_filename, sizeof(guard) - strlen(guard) - 1); strncat(guard, input_filename, sizeof(guard) - strlen(guard) - 1);
for ( char* p = guard; *p; p++ ) for ( char* p = guard; *p; p++ ) {
{
if ( ! isalnum(*p) ) if ( ! isalnum(*p) )
*p = '_'; *p = '_';
} }
@ -258,8 +250,7 @@ void init_alternative_mode()
if ( dot ) if ( dot )
*dot = '\0'; *dot = '\0';
if ( plugin ) if ( plugin ) {
{
static char plugin_canon[1024]; static char plugin_canon[1024];
strncpy(plugin_canon, plugin, sizeof(plugin_canon) - 1); strncpy(plugin_canon, plugin, sizeof(plugin_canon) - 1);
plugin_canon[sizeof(plugin_canon) - 1] = '\0'; plugin_canon[sizeof(plugin_canon) - 1] = '\0';
@ -286,79 +277,71 @@ void init_alternative_mode()
fprintf(fp_func_register, "\n"); fprintf(fp_func_register, "\n");
fprintf(fp_func_register, "namespace plugin::%s {\n", plugin_canon); fprintf(fp_func_register, "namespace plugin::%s {\n", plugin_canon);
fprintf(fp_func_register, "void __bif_%s_init(zeek::plugin::Plugin* plugin);\n", name); fprintf(fp_func_register, "void __bif_%s_init(zeek::plugin::Plugin* plugin);\n", name);
fprintf(fp_func_register, "zeek::plugin::detail::__RegisterBif __register_bifs_%s_%s(\"%s\", __bif_%s_init);\n", plugin_canon, name, plugin, name); fprintf(fp_func_register, "zeek::plugin::detail::__RegisterBif __register_bifs_%s_%s(\"%s\", __bif_%s_init);\n",
plugin_canon, name, plugin, name);
fprintf(fp_func_register, "}\n"); fprintf(fp_func_register, "}\n");
} }
} }
void finish_alternative_mode() void finish_alternative_mode() {
{
fprintf(fp_func_h, "\n"); fprintf(fp_func_h, "\n");
fprintf(fp_func_h, "#endif\n"); fprintf(fp_func_h, "#endif\n");
if ( plugin ) if ( plugin ) {
{
fprintf(fp_func_init, "\n"); fprintf(fp_func_init, "\n");
fprintf(fp_func_init, "\t}\n"); fprintf(fp_func_init, "\t}\n");
fprintf(fp_func_init, "}\n"); fprintf(fp_func_init, "}\n");
fprintf(fp_func_init, "\n"); fprintf(fp_func_init, "\n");
fprintf(fp_func_init, "\n"); fprintf(fp_func_init, "\n");
} }
} }
// GCC uses __SANITIZE_ADDRESS__, Clang uses __has_feature // GCC uses __SANITIZE_ADDRESS__, Clang uses __has_feature
#if defined(__SANITIZE_ADDRESS__) #if defined(__SANITIZE_ADDRESS__)
#define USING_ASAN #define USING_ASAN
#endif #endif
#if defined(__has_feature) #if defined(__has_feature)
#if __has_feature(address_sanitizer) #if __has_feature(address_sanitizer)
#define USING_ASAN #define USING_ASAN
#endif #endif
#endif #endif
// FreeBSD doesn't support LeakSanitizer // FreeBSD doesn't support LeakSanitizer
#if defined(USING_ASAN) && !defined(__FreeBSD__) #if defined(USING_ASAN) && ! defined(__FreeBSD__)
#include <sanitizer/lsan_interface.h> #include <sanitizer/lsan_interface.h>
#define BIFCL_LSAN_DISABLE() __lsan_disable() #define BIFCL_LSAN_DISABLE() __lsan_disable()
#else #else
#define BIFCL_LSAN_DISABLE() #define BIFCL_LSAN_DISABLE()
#endif #endif
int main(int argc, char* argv[]) int main(int argc, char* argv[]) {
{
// We generally do not care at all if bifcl is leaking and the default // We generally do not care at all if bifcl is leaking and the default
// behavior of LSAN to treat leaks as errors only trips up Zeek's build. // behavior of LSAN to treat leaks as errors only trips up Zeek's build.
BIFCL_LSAN_DISABLE(); BIFCL_LSAN_DISABLE();
int opt; int opt;
while ( (opt = getopt(argc, argv, "p:s")) != -1 ) while ( (opt = getopt(argc, argv, "p:s")) != -1 ) {
{
switch ( opt ) { switch ( opt ) {
case 'p': case 'p':
alternative_mode = true; alternative_mode = true;
plugin = (char*) optarg; plugin = (char*)optarg;
break; break;
case 's': case 's': alternative_mode = true; break;
alternative_mode = true;
break;
default: default: usage();
usage();
} }
} }
for ( int i = optind; i < argc; i++ ) for ( int i = optind; i < argc; i++ ) {
{
FILE* fp_input; FILE* fp_input;
input_filename = input_filename_with_path = argv[i]; input_filename = input_filename_with_path = argv[i];
char* slash = strrchr(input_filename, '/'); char* slash = strrchr(input_filename, '/');
if ( (fp_input = fopen(input_filename, "r")) == NULL ) if ( (fp_input = fopen(input_filename, "r")) == NULL ) {
{
fprintf(stderr, "Error: cannot open file: %s\n", input_filename); fprintf(stderr, "Error: cannot open file: %s\n", input_filename);
/* no output files open. can simply exit */ /* no output files open. can simply exit */
exit(1); exit(1);
@ -367,8 +350,7 @@ int main(int argc, char* argv[])
if ( slash ) if ( slash )
input_filename = slash + 1; input_filename = slash + 1;
if ( ! alternative_mode ) if ( ! alternative_mode ) {
{
fp_zeek_init = open_output_file("zeek"); fp_zeek_init = open_output_file("zeek");
fp_func_h = open_output_file("func_h"); fp_func_h = open_output_file("func_h");
fp_func_def = open_output_file("func_def"); fp_func_def = open_output_file("func_def");
@ -381,9 +363,7 @@ int main(int argc, char* argv[])
auto auto_gen_comment_buf = std::make_unique<char[]>(n); auto auto_gen_comment_buf = std::make_unique<char[]>(n);
auto auto_gen_comment = auto_gen_comment_buf.get(); auto auto_gen_comment = auto_gen_comment_buf.get();
snprintf(auto_gen_comment, n, snprintf(auto_gen_comment, n, "This file was automatically generated by bifcl from %s.", input_filename);
"This file was automatically generated by bifcl from %s.",
input_filename);
fprintf(fp_zeek_init, "# %s\n\n", auto_gen_comment); fprintf(fp_zeek_init, "# %s\n\n", auto_gen_comment);
fprintf(fp_func_def, "// %s\n\n", auto_gen_comment); fprintf(fp_func_def, "// %s\n\n", auto_gen_comment);
@ -416,43 +396,37 @@ int main(int argc, char* argv[])
fclose(fp_input); fclose(fp_input);
close_all_output_files(); close_all_output_files();
}
} }
}
void close_if_open(FILE **fpp) void close_if_open(FILE** fpp) {
{ if ( *fpp )
if (*fpp)
fclose(*fpp); fclose(*fpp);
*fpp = nullptr; *fpp = nullptr;
} }
void close_all_output_files(void) void close_all_output_files(void) {
{
close_if_open(&fp_zeek_init); close_if_open(&fp_zeek_init);
close_if_open(&fp_func_h); close_if_open(&fp_func_h);
close_if_open(&fp_func_def); close_if_open(&fp_func_def);
close_if_open(&fp_func_init); close_if_open(&fp_func_init);
close_if_open(&fp_func_register); close_if_open(&fp_func_register);
if ( ! alternative_mode ) if ( ! alternative_mode ) {
{
close_if_open(&fp_netvar_h); close_if_open(&fp_netvar_h);
close_if_open(&fp_netvar_def); close_if_open(&fp_netvar_def);
close_if_open(&fp_netvar_init); close_if_open(&fp_netvar_init);
} }
} }
void remove_file(const char *surfix) void remove_file(const char* surfix) {
{
char fn[1024]; char fn[1024];
snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix); snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix);
unlink(fn); unlink(fn);
} }
void err_exit(void) void err_exit(void) {
{
close_all_output_files(); close_all_output_files();
/* clean up. remove all output files we've generated so far */ /* clean up. remove all output files we've generated so far */
remove_file("zeek"); remove_file("zeek");
@ -464,4 +438,4 @@ void err_exit(void)
remove_file("netvar_def"); remove_file("netvar_def");
remove_file("netvar_init"); remove_file("netvar_init");
exit(1); exit(1);
} }

View file

@ -65,17 +65,15 @@ struct decl_struct {
string enqueue_c_fullname; string enqueue_c_fullname;
} decl; } decl;
void set_definition_type(int type, const char *arg_type_name) void set_definition_type(int type, const char* arg_type_name) {
{
definition_type = type; definition_type = type;
if ( type == TYPE_DEF && arg_type_name ) if ( type == TYPE_DEF && arg_type_name )
type_name = string(arg_type_name); type_name = string(arg_type_name);
else else
type_name = ""; type_name = "";
} }
void set_decl_name(const char *name) void set_decl_name(const char* name) {
{
decl.bare_name = extract_var_name(name); decl.bare_name = extract_var_name(name);
// make_full_var_name prepends the correct module, if any // make_full_var_name prepends the correct module, if any
@ -118,12 +116,10 @@ void set_decl_name(const char *name)
decl.enqueue_c_fullname = "zeek::BifEvent::"; decl.enqueue_c_fullname = "zeek::BifEvent::";
break; break;
default: default: break;
break;
} }
if ( decl.module_name != GLOBAL_MODULE_NAME ) if ( decl.module_name != GLOBAL_MODULE_NAME ) {
{
if ( decl.c_namespace_start.empty() ) { if ( decl.c_namespace_start.empty() ) {
decl.c_namespace_start += "namespace " + decl.module_name + " { "; decl.c_namespace_start += "namespace " + decl.module_name + " { ";
decl.c_namespace_end += " }"; decl.c_namespace_end += " }";
@ -150,7 +146,7 @@ void set_decl_name(const char *name)
decl.c_fullname += decl.bare_name; decl.c_fullname += decl.bare_name;
decl.zeek_name += name; decl.zeek_name += name;
decl.enqueue_c_fullname += decl.enqueue_c_barename; decl.enqueue_c_fullname += decl.enqueue_c_barename;
} }
const char* arg_list_name = "BiF_ARGS"; const char* arg_list_name = "BiF_ARGS";
@ -174,16 +170,13 @@ static struct {
#undef DEFINE_BIF_TYPE #undef DEFINE_BIF_TYPE
}; };
int get_type_index(const char *type_name) int get_type_index(const char* type_name) {
{ for ( int i = 0; builtin_types[i].bif_type[0] != '\0'; ++i ) {
for ( int i = 0; builtin_types[i].bif_type[0] != '\0'; ++i )
{
if ( strcmp(builtin_types[i].bif_type, type_name) == 0 ) if ( strcmp(builtin_types[i].bif_type, type_name) == 0 )
return i; return i;
} }
return TYPE_OTHER; return TYPE_OTHER;
} }
int var_arg; // whether the number of arguments is variable int var_arg; // whether the number of arguments is variable
std::vector<BuiltinFuncArg*> args; std::vector<BuiltinFuncArg*> args;
@ -192,63 +185,52 @@ extern int yyerror(const char[]);
extern int yywarn(const char msg[]); extern int yywarn(const char msg[]);
extern int yylex(); extern int yylex();
char* concat(const char* str1, const char* str2) char* concat(const char* str1, const char* str2) {
{
int len1 = strlen(str1); int len1 = strlen(str1);
int len2 = strlen(str2); int len2 = strlen(str2);
char* s = new char[len1 + len2 +1]; char* s = new char[len1 + len2 + 1];
memcpy(s, str1, len1); memcpy(s, str1, len1);
memcpy(s + len1, str2, len2); memcpy(s + len1, str2, len2);
s[len1+len2] = '\0'; s[len1 + len2] = '\0';
return s; return s;
} }
static void print_event_c_prototype_args(FILE* fp) static void print_event_c_prototype_args(FILE * fp) {
{ for ( auto i = 0u; i < args.size(); ++i ) {
for ( auto i = 0u; i < args.size(); ++i )
{
if ( i > 0 ) if ( i > 0 )
fprintf(fp, ", "); fprintf(fp, ", ");
args[i]->PrintCArg(fp, i); args[i]->PrintCArg(fp, i);
} }
} }
static void print_event_c_prototype_header(FILE* fp) static void print_event_c_prototype_header(FILE * fp) {
{
fprintf(fp, "namespace zeek::%s { void %s(zeek::analyzer::Analyzer* analyzer%s", fprintf(fp, "namespace zeek::%s { void %s(zeek::analyzer::Analyzer* analyzer%s",
decl.enqueue_c_namespace_start.c_str(), decl.enqueue_c_namespace_start.c_str(), decl.enqueue_c_barename.c_str(), args.size() ? ", " : "");
decl.enqueue_c_barename.c_str(),
args.size() ? ", " : "" );
print_event_c_prototype_args(fp); print_event_c_prototype_args(fp);
fprintf(fp, ")"); fprintf(fp, ")");
fprintf(fp, "; %s }\n", decl.enqueue_c_namespace_end.c_str()); fprintf(fp, "; %s }\n", decl.enqueue_c_namespace_end.c_str());
} }
static void print_event_c_prototype_impl(FILE* fp) static void print_event_c_prototype_impl(FILE * fp) {
{ fprintf(fp, "void %s(zeek::analyzer::Analyzer* analyzer%s", decl.enqueue_c_fullname.c_str(),
fprintf(fp, "void %s(zeek::analyzer::Analyzer* analyzer%s", args.size() ? ", " : "");
decl.enqueue_c_fullname.c_str(),
args.size() ? ", " : "" );
print_event_c_prototype_args(fp); print_event_c_prototype_args(fp);
fprintf(fp, ")"); fprintf(fp, ")");
fprintf(fp, "\n"); fprintf(fp, "\n");
} }
static void print_event_c_body(FILE* fp) static void print_event_c_body(FILE * fp) {
{
fprintf(fp, "\t{\n"); fprintf(fp, "\t{\n");
fprintf(fp, "\t// Note that it is intentional that here we do not\n"); fprintf(fp, "\t// Note that it is intentional that here we do not\n");
fprintf(fp, "\t// check if %s is NULL, which should happen *before*\n", fprintf(fp, "\t// check if %s is NULL, which should happen *before*\n", decl.c_fullname.c_str());
decl.c_fullname.c_str()); fprintf(fp, "\t// %s is called to avoid unnecessary Val\n", decl.enqueue_c_fullname.c_str());
fprintf(fp, "\t// %s is called to avoid unnecessary Val\n",
decl.enqueue_c_fullname.c_str());
fprintf(fp, "\t// allocation.\n"); fprintf(fp, "\t// allocation.\n");
fprintf(fp, "\n"); fprintf(fp, "\n");
@ -256,20 +238,18 @@ static void print_event_c_body(FILE* fp)
fprintf(fp, "\tzeek::event_mgr.Enqueue(%s, zeek::Args{\n", decl.c_fullname.c_str()); fprintf(fp, "\tzeek::event_mgr.Enqueue(%s, zeek::Args{\n", decl.c_fullname.c_str());
for ( int i = 0; i < (int) args.size(); ++i ) for ( int i = 0; i < (int)args.size(); ++i ) {
{
fprintf(fp, "\t "); fprintf(fp, "\t ");
args[i]->PrintValConstructor(fp); args[i]->PrintValConstructor(fp);
fprintf(fp, ",\n"); fprintf(fp, ",\n");
if ( args[i]->Type() == TYPE_CONNECTION ) if ( args[i]->Type() == TYPE_CONNECTION ) {
{
if ( connection_arg == nullptr ) if ( connection_arg == nullptr )
connection_arg = args[i]; connection_arg = args[i];
else else {
{
// We are seeing two connection type arguments. // We are seeing two connection type arguments.
yywarn("Warning: with more than connection-type " yywarn(
"Warning: with more than connection-type "
"event arguments, bifcl only passes " "event arguments, bifcl only passes "
"the first one to EventMgr as cookie."); "the first one to EventMgr as cookie.");
} }
@ -284,16 +264,15 @@ static void print_event_c_body(FILE* fp)
fprintf(fp, ");\n"); fprintf(fp, ");\n");
fprintf(fp, "\t}\n\n"); fprintf(fp, "\t}\n\n");
//fprintf(fp, "%s // end namespace\n", decl.enqueue_c_namespace_end.c_str()); // fprintf(fp, "%s // end namespace\n", decl.enqueue_c_namespace_end.c_str());
} }
void record_bif_item(const char* id, const char* type) void record_bif_item(const char* id, const char* type) {
{
if ( ! plugin ) if ( ! plugin )
return; return;
fprintf(fp_func_init, "\tplugin->AddBifItem(\"%s\", zeek::plugin::BifItem::%s);\n", id, type); fprintf(fp_func_init, "\tplugin->AddBifItem(\"%s\", zeek::plugin::BifItem::%s);\n", id, type);
} }
%} %}
@ -792,8 +771,7 @@ extern char* input_filename;
extern int line_number; extern int line_number;
void err_exit(void); void err_exit(void);
void print_msg(const char msg[]) void print_msg(const char msg[]) {
{
int msg_len = strlen(msg) + strlen(yytext) + 64; int msg_len = strlen(msg) + strlen(yytext) + 64;
char* msgbuf = new char[msg_len]; char* msgbuf = new char[msg_len];
@ -817,19 +795,17 @@ void print_msg(const char msg[])
fprintf(stderr, "line %d: ", line_number); fprintf(stderr, "line %d: ", line_number);
fprintf(stderr, "%s\n", msgbuf); fprintf(stderr, "%s\n", msgbuf);
delete [] msgbuf; delete[] msgbuf;
} }
int yywarn(const char msg[]) int yywarn(const char msg[]) {
{
print_msg(msg); print_msg(msg);
return 0; return 0;
} }
int yyerror(const char msg[]) int yyerror(const char msg[]) {
{
print_msg(msg); print_msg(msg);
err_exit(); err_exit();
return 0; return 0;
} }