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);
return 1;
}
}
extern int yyparse();
char* input_filename = nullptr;
@ -171,29 +170,25 @@ 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();
}
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,79 +277,71 @@ 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");
fprintf(fp_func_init, "\n");
fprintf(fp_func_init, "\n");
}
}
}
// GCC uses __SANITIZE_ADDRESS__, Clang uses __has_feature
#if defined(__SANITIZE_ADDRESS__)
#define USING_ASAN
#define USING_ASAN
#endif
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define USING_ASAN
#endif
#if __has_feature(address_sanitizer)
#define USING_ASAN
#endif
#endif
// FreeBSD doesn't support LeakSanitizer
#if defined(USING_ASAN) && !defined(__FreeBSD__)
#include <sanitizer/lsan_interface.h>
#define BIFCL_LSAN_DISABLE() __lsan_disable()
#if defined(USING_ASAN) && ! defined(__FreeBSD__)
#include <sanitizer/lsan_interface.h>
#define BIFCL_LSAN_DISABLE() __lsan_disable()
#else
#define BIFCL_LSAN_DISABLE()
#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;
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)
{
if (*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");
@ -464,4 +438,4 @@ void err_exit(void)
remove_file("netvar_def");
remove_file("netvar_init");
exit(1);
}
}

View file

@ -65,17 +65,15 @@ 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);
else
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 += " }";
@ -150,7 +146,7 @@ void set_decl_name(const char *name)
decl.c_fullname += decl.bare_name;
decl.zeek_name += name;
decl.enqueue_c_fullname += decl.enqueue_c_barename;
}
}
const char* arg_list_name = "BiF_ARGS";
@ -174,16 +170,13 @@ 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,63 +185,52 @@ 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);
char* s = new char[len1 + len2 +1];
char* s = new char[len1 + len2 + 1];
memcpy(s, str1, len1);
memcpy(s + len1, str2, len2);
s[len1+len2] = '\0';
s[len1 + len2] = '\0';
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, ", ");
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",
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(),
args.size() ? ", " : "" );
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);
fprintf(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.");
}
@ -284,16 +264,15 @@ static void print_event_c_body(FILE* fp)
fprintf(fp, ");\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 )
return;
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;
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];
@ -817,19 +795,17 @@ void print_msg(const char msg[])
fprintf(stderr, "line %d: ", line_number);
fprintf(stderr, "%s\n", msgbuf);
delete [] msgbuf;
}
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();
return 0;
}
}