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,8 +143,7 @@ HEX [0-9a-fA-F]+
}
%%
int yywrap()
{
int yywrap() {
yy_delete_buffer(YY_CURRENT_BUFFER);
return 1;
}
@ -171,14 +170,12 @@ void close_if_open(FILE **fpp);
void close_all_output_files(void);
FILE* open_output_file(const char* surfix)
{
FILE* open_output_file(const char* surfix) {
char fn[1024];
FILE* fp;
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);
err_exit();
}
@ -186,14 +183,12 @@ FILE* open_output_file(const char* surfix)
return fp;
}
void usage()
{
void usage() {
fprintf(stderr, "usage: bifcl [-p <plugin> | -s] *.bif\n");
exit(1);
}
void init_alternative_mode()
{
void init_alternative_mode() {
fp_zeek_init = open_output_file("zeek");
fp_func_h = open_output_file("h");
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 = auto_gen_comment_buf.get();
snprintf(auto_gen_comment, n,
"This file was automatically generated by bifcl from %s (%s mode).",
snprintf(auto_gen_comment, n, "This file was automatically generated by bifcl from %s (%s mode).",
input_filename_with_path, plugin ? "plugin" : "alternative");
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);
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");
err_exit();
}
strncat(guard, "/", 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) )
*p = '_';
}
@ -258,8 +250,7 @@ void init_alternative_mode()
if ( dot )
*dot = '\0';
if ( plugin )
{
if ( plugin ) {
static char plugin_canon[1024];
strncpy(plugin_canon, plugin, sizeof(plugin_canon) - 1);
plugin_canon[sizeof(plugin_canon) - 1] = '\0';
@ -286,18 +277,17 @@ void init_alternative_mode()
fprintf(fp_func_register, "\n");
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, "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");
}
}
void finish_alternative_mode()
{
void finish_alternative_mode() {
fprintf(fp_func_h, "\n");
fprintf(fp_func_h, "#endif\n");
if ( plugin )
{
if ( plugin ) {
fprintf(fp_func_init, "\n");
fprintf(fp_func_init, "\t}\n");
fprintf(fp_func_init, "}\n");
@ -325,40 +315,33 @@ void finish_alternative_mode()
#define BIFCL_LSAN_DISABLE()
#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
// behavior of LSAN to treat leaks as errors only trips up Zeek's build.
BIFCL_LSAN_DISABLE();
int opt;
while ( (opt = getopt(argc, argv, "p:s")) != -1 )
{
while ( (opt = getopt(argc, argv, "p:s")) != -1 ) {
switch ( opt ) {
case 'p':
alternative_mode = true;
plugin = (char*)optarg;
break;
case 's':
alternative_mode = true;
break;
case 's': alternative_mode = true; break;
default:
usage();
default: usage();
}
}
for ( int i = optind; i < argc; i++ )
{
for ( int i = optind; i < argc; i++ ) {
FILE* fp_input;
input_filename = input_filename_with_path = argv[i];
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);
/* no output files open. can simply exit */
exit(1);
@ -367,8 +350,7 @@ int main(int argc, char* argv[])
if ( slash )
input_filename = slash + 1;
if ( ! alternative_mode )
{
if ( ! alternative_mode ) {
fp_zeek_init = open_output_file("zeek");
fp_func_h = open_output_file("func_h");
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 = auto_gen_comment_buf.get();
snprintf(auto_gen_comment, n,
"This file was automatically generated by bifcl from %s.",
input_filename);
snprintf(auto_gen_comment, n, "This file was automatically generated by bifcl from %s.", input_filename);
fprintf(fp_zeek_init, "# %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);
close_all_output_files();
}
}
void close_if_open(FILE **fpp)
{
void close_if_open(FILE** fpp) {
if ( *fpp )
fclose(*fpp);
*fpp = nullptr;
}
void close_all_output_files(void)
{
void close_all_output_files(void) {
close_if_open(&fp_zeek_init);
close_if_open(&fp_func_h);
close_if_open(&fp_func_def);
close_if_open(&fp_func_init);
close_if_open(&fp_func_register);
if ( ! alternative_mode )
{
if ( ! alternative_mode ) {
close_if_open(&fp_netvar_h);
close_if_open(&fp_netvar_def);
close_if_open(&fp_netvar_init);
}
}
void remove_file(const char *surfix)
{
void remove_file(const char* surfix) {
char fn[1024];
snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix);
unlink(fn);
}
void err_exit(void)
{
void err_exit(void) {
close_all_output_files();
/* clean up. remove all output files we've generated so far */
remove_file("zeek");

View file

@ -65,8 +65,7 @@ struct decl_struct {
string enqueue_c_fullname;
} 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;
if ( type == TYPE_DEF && arg_type_name )
type_name = string(arg_type_name);
@ -74,8 +73,7 @@ void set_definition_type(int type, const char *arg_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);
// 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::";
break;
default:
break;
default: break;
}
if ( decl.module_name != GLOBAL_MODULE_NAME )
{
if ( decl.module_name != GLOBAL_MODULE_NAME ) {
if ( decl.c_namespace_start.empty() ) {
decl.c_namespace_start += "namespace " + decl.module_name + " { ";
decl.c_namespace_end += " }";
@ -174,17 +170,14 @@ static struct {
#undef DEFINE_BIF_TYPE
};
int get_type_index(const char *type_name)
{
for ( int i = 0; builtin_types[i].bif_type[0] != '\0'; ++i )
{
int get_type_index(const char* type_name) {
for ( int i = 0; builtin_types[i].bif_type[0] != '\0'; ++i ) {
if ( strcmp(builtin_types[i].bif_type, type_name) == 0 )
return i;
}
return TYPE_OTHER;
}
int var_arg; // whether the number of arguments is variable
std::vector<BuiltinFuncArg*> args;
@ -192,8 +185,7 @@ extern int yyerror(const char[]);
extern int yywarn(const char msg[]);
extern int yylex();
char* concat(const char* str1, const char* str2)
{
char* concat(const char* str1, const char* str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
@ -207,10 +199,8 @@ char* concat(const char* str1, const char* str2)
return s;
}
static void print_event_c_prototype_args(FILE* fp)
{
for ( auto i = 0u; i < args.size(); ++i )
{
static void print_event_c_prototype_args(FILE * fp) {
for ( auto i = 0u; i < args.size(); ++i ) {
if ( i > 0 )
fprintf(fp, ", ");
@ -218,22 +208,17 @@ static void print_event_c_prototype_args(FILE* fp)
}
}
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",
decl.enqueue_c_namespace_start.c_str(),
decl.enqueue_c_barename.c_str(),
args.size() ? ", " : "" );
decl.enqueue_c_namespace_start.c_str(), decl.enqueue_c_barename.c_str(), args.size() ? ", " : "");
print_event_c_prototype_args(fp);
fprintf(fp, ")");
fprintf(fp, "; %s }\n", decl.enqueue_c_namespace_end.c_str());
}
static void print_event_c_prototype_impl(FILE* fp)
{
fprintf(fp, "void %s(zeek::analyzer::Analyzer* analyzer%s",
decl.enqueue_c_fullname.c_str(),
static void print_event_c_prototype_impl(FILE * fp) {
fprintf(fp, "void %s(zeek::analyzer::Analyzer* analyzer%s", decl.enqueue_c_fullname.c_str(),
args.size() ? ", " : "");
print_event_c_prototype_args(fp);
@ -241,14 +226,11 @@ static void print_event_c_prototype_impl(FILE* fp)
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// Note that it is intentional that here we do not\n");
fprintf(fp, "\t// check if %s is NULL, which should happen *before*\n",
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// check if %s is NULL, which should happen *before*\n", 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// allocation.\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());
for ( int i = 0; i < (int) args.size(); ++i )
{
for ( int i = 0; i < (int)args.size(); ++i ) {
fprintf(fp, "\t ");
args[i]->PrintValConstructor(fp);
fprintf(fp, ",\n");
if ( args[i]->Type() == TYPE_CONNECTION )
{
if ( args[i]->Type() == TYPE_CONNECTION ) {
if ( connection_arg == nullptr )
connection_arg = args[i];
else
{
else {
// 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 "
"the first one to EventMgr as cookie.");
}
@ -287,8 +267,7 @@ static void print_event_c_body(FILE* fp)
// 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 )
return;
@ -792,8 +771,7 @@ extern char* input_filename;
extern int line_number;
void err_exit(void);
void print_msg(const char msg[])
{
void print_msg(const char msg[]) {
int msg_len = strlen(msg) + strlen(yytext) + 64;
char* msgbuf = new char[msg_len];
@ -820,14 +798,12 @@ void print_msg(const char msg[])
delete[] msgbuf;
}
int yywarn(const char msg[])
{
int yywarn(const char msg[]) {
print_msg(msg);
return 0;
}
int yyerror(const char msg[])
{
int yyerror(const char msg[]) {
print_msg(msg);
err_exit();