mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
bifcl: Reformat Bifcl in Spicy style
This commit is contained in:
parent
8e5b722145
commit
f2cd4ae3e6
4 changed files with 164 additions and 187 deletions
|
@ -6,23 +6,26 @@ include(cmake/CommonCMakeConfig.cmake)
|
|||
find_package(BISON REQUIRED)
|
||||
find_package(FLEX REQUIRED)
|
||||
|
||||
if (MSVC)
|
||||
if(MSVC)
|
||||
add_compile_options(/J) # Similar to -funsigned-char on other platforms
|
||||
set_property(SOURCE bif_lex.cc APPEND_STRING PROPERTY COMPILE_FLAGS "/wd4018")
|
||||
set_property(
|
||||
SOURCE bif_lex.cc
|
||||
APPEND_STRING
|
||||
PROPERTY COMPILE_FLAGS "/wd4018")
|
||||
else()
|
||||
set_property(SOURCE bif_lex.cc APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-sign-compare")
|
||||
set_property(
|
||||
SOURCE bif_lex.cc
|
||||
APPEND_STRING
|
||||
PROPERTY COMPILE_FLAGS "-Wno-sign-compare")
|
||||
endif()
|
||||
|
||||
include_directories(BEFORE
|
||||
${BifCl_SOURCE_DIR}/include
|
||||
${BifCl_BINARY_DIR}
|
||||
)
|
||||
include_directories(BEFORE ${BifCl_SOURCE_DIR}/include ${BifCl_BINARY_DIR})
|
||||
|
||||
set(BISON_FLAGS "--debug")
|
||||
|
||||
# BIF parser/scanner
|
||||
bison_target(BIFParser builtin-func.y
|
||||
${BifCl_BINARY_DIR}/bif_parse.cc
|
||||
bison_target(
|
||||
BIFParser builtin-func.y ${BifCl_BINARY_DIR}/bif_parse.cc
|
||||
DEFINES_FILE ${BifCl_BINARY_DIR}/bif_parse.h
|
||||
COMPILE_FLAGS "${BISON_FLAGS}")
|
||||
flex_target(BIFScanner builtin-func.l ${BifCl_BINARY_DIR}/bif_lex.cc)
|
||||
|
@ -36,17 +39,16 @@ set(bifcl_SRCS
|
|||
bif_arg.cc
|
||||
include/bif_arg.h
|
||||
module_util.cc
|
||||
include/module_util.h
|
||||
)
|
||||
include/module_util.h)
|
||||
|
||||
add_executable(bifcl ${bifcl_SRCS})
|
||||
target_compile_features(bifcl PRIVATE cxx_std_17)
|
||||
set_target_properties(bifcl PROPERTIES CXX_EXTENSIONS OFF)
|
||||
|
||||
if (MSVC)
|
||||
# If building separately from zeek, we need to add the libunistd subdirectory so
|
||||
# that linking doesn't fail.
|
||||
if ("${CMAKE_PROJECT_NAME}" STREQUAL "BifCl")
|
||||
if(MSVC)
|
||||
# If building separately from zeek, we need to add the libunistd subdirectory
|
||||
# so that linking doesn't fail.
|
||||
if("${CMAKE_PROJECT_NAME}" STREQUAL "BifCl")
|
||||
add_subdirectory(auxil/libunistd EXCLUDE_FROM_ALL)
|
||||
endif()
|
||||
target_link_libraries(bifcl PRIVATE libunistd)
|
||||
|
@ -54,9 +56,9 @@ endif()
|
|||
|
||||
install(TARGETS bifcl DESTINATION bin)
|
||||
|
||||
if (CMAKE_BUILD_TYPE)
|
||||
if(CMAKE_BUILD_TYPE)
|
||||
string(TOUPPER ${CMAKE_BUILD_TYPE} BuildType)
|
||||
endif ()
|
||||
endif()
|
||||
|
||||
message(
|
||||
"\n====================| Bifcl Build Summary |====================="
|
||||
|
@ -72,7 +74,6 @@ message(
|
|||
"\nCXXFLAGS: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${BuildType}}"
|
||||
"\nCPP: ${CMAKE_CXX_COMPILER}"
|
||||
"\n"
|
||||
"\n================================================================\n"
|
||||
)
|
||||
"\n================================================================\n")
|
||||
|
||||
include(UserChangedWarning)
|
||||
|
|
|
@ -7,8 +7,7 @@ using namespace std;
|
|||
|
||||
#include "bif_arg.h"
|
||||
|
||||
static struct
|
||||
{
|
||||
static struct {
|
||||
const char* type_enum;
|
||||
const char* bif_type;
|
||||
const char* zeek_type;
|
||||
|
@ -19,51 +18,43 @@ static struct
|
|||
const char* cast_smart;
|
||||
const char* constructor;
|
||||
const char* ctor_smart;
|
||||
} builtin_func_arg_type[] = {
|
||||
#define DEFINE_BIF_TYPE(id, bif_type, zeek_type, c_type, c_type_smart, accessor, accessor_smart, \
|
||||
cast_smart, constructor, ctor_smart) \
|
||||
{#id, bif_type, zeek_type, c_type, c_type_smart, \
|
||||
accessor, accessor_smart, cast_smart, constructor, ctor_smart},
|
||||
} builtin_func_arg_type[] = {
|
||||
#define DEFINE_BIF_TYPE(id, bif_type, zeek_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, \
|
||||
constructor, ctor_smart) \
|
||||
{#id, bif_type, zeek_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, constructor, ctor_smart},
|
||||
#include "bif_type.def"
|
||||
#undef DEFINE_BIF_TYPE
|
||||
};
|
||||
};
|
||||
|
||||
extern const char* arg_list_name;
|
||||
|
||||
BuiltinFuncArg::BuiltinFuncArg(const char* arg_name, int arg_type)
|
||||
{
|
||||
BuiltinFuncArg::BuiltinFuncArg(const char* arg_name, int arg_type) {
|
||||
name = arg_name;
|
||||
type = arg_type;
|
||||
type_str = "";
|
||||
attr_str = "";
|
||||
}
|
||||
}
|
||||
|
||||
BuiltinFuncArg::BuiltinFuncArg(const char* arg_name, const char* arg_type_str,
|
||||
const char* arg_attr_str)
|
||||
{
|
||||
BuiltinFuncArg::BuiltinFuncArg(const char* arg_name, const char* arg_type_str, const char* arg_attr_str) {
|
||||
name = arg_name;
|
||||
type = TYPE_OTHER;
|
||||
type_str = arg_type_str;
|
||||
attr_str = arg_attr_str;
|
||||
|
||||
for ( int i = 0; builtin_func_arg_type[i].bif_type[0] != '\0'; ++i )
|
||||
if ( ! strcmp(builtin_func_arg_type[i].bif_type, arg_type_str) )
|
||||
{
|
||||
if ( ! strcmp(builtin_func_arg_type[i].bif_type, arg_type_str) ) {
|
||||
type = i;
|
||||
type_str = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BuiltinFuncArg::PrintZeek(FILE* fp)
|
||||
{
|
||||
void BuiltinFuncArg::PrintZeek(FILE* fp) {
|
||||
fprintf(fp, "%s: %s%s %s", name, builtin_func_arg_type[type].zeek_type, type_str, attr_str);
|
||||
}
|
||||
}
|
||||
|
||||
void BuiltinFuncArg::PrintCDef(FILE* fp, int n, bool runtime_type_check)
|
||||
{
|
||||
void BuiltinFuncArg::PrintCDef(FILE* fp, int n, bool runtime_type_check) {
|
||||
// Generate a runtime type-check pre-amble for types we understand
|
||||
if ( runtime_type_check && type != TYPE_OTHER && type != TYPE_ANY )
|
||||
{
|
||||
if ( runtime_type_check && type != TYPE_OTHER && type != TYPE_ANY ) {
|
||||
fprintf(fp, "\t\t{\n");
|
||||
fprintf(fp, "\t\t// Runtime type check for %s argument\n", name);
|
||||
fprintf(fp, "\t\tzeek::TypeTag __tag = (*%s)[%d]->GetType()->Tag();\n", arg_list_name, n);
|
||||
|
@ -77,8 +68,7 @@ void BuiltinFuncArg::PrintCDef(FILE* fp, int n, bool runtime_type_check)
|
|||
fprintf(fp, "\t\t\t}\n");
|
||||
fprintf(fp, "\t\t}\n");
|
||||
}
|
||||
fprintf(fp, "\t%s %s = (%s) (", builtin_func_arg_type[type].c_type, name,
|
||||
builtin_func_arg_type[type].c_type);
|
||||
fprintf(fp, "\t%s %s = (%s) (", builtin_func_arg_type[type].c_type, name, builtin_func_arg_type[type].c_type);
|
||||
|
||||
char buf[1024];
|
||||
snprintf(buf, sizeof(buf), "(*%s)[%d].get()", arg_list_name, n);
|
||||
|
@ -86,14 +76,10 @@ void BuiltinFuncArg::PrintCDef(FILE* fp, int n, bool runtime_type_check)
|
|||
fprintf(fp, builtin_func_arg_type[type].accessor, buf);
|
||||
|
||||
fprintf(fp, ");\n");
|
||||
}
|
||||
}
|
||||
|
||||
void BuiltinFuncArg::PrintCArg(FILE* fp, int n)
|
||||
{
|
||||
void BuiltinFuncArg::PrintCArg(FILE* fp, int n) {
|
||||
fprintf(fp, "%s %s", builtin_func_arg_type[type].c_type_smart, name);
|
||||
}
|
||||
}
|
||||
|
||||
void BuiltinFuncArg::PrintValConstructor(FILE* fp)
|
||||
{
|
||||
fprintf(fp, builtin_func_arg_type[type].ctor_smart, name);
|
||||
}
|
||||
void BuiltinFuncArg::PrintValConstructor(FILE* fp) { fprintf(fp, builtin_func_arg_type[type].ctor_smart, name); }
|
||||
|
|
|
@ -2,19 +2,17 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
enum builtin_func_arg_type
|
||||
{
|
||||
#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, \
|
||||
cast_smart, constructor, ctor_smart) \
|
||||
enum builtin_func_arg_type {
|
||||
#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, \
|
||||
constructor, ctor_smart) \
|
||||
id,
|
||||
#include "bif_type.def"
|
||||
#undef DEFINE_BIF_TYPE
|
||||
};
|
||||
};
|
||||
|
||||
extern const char* builtin_func_arg_type_bro_name[];
|
||||
|
||||
class BuiltinFuncArg final
|
||||
{
|
||||
class BuiltinFuncArg final {
|
||||
public:
|
||||
BuiltinFuncArg(const char* arg_name, int arg_type);
|
||||
BuiltinFuncArg(const char* arg_name, const char* arg_type_str, const char* arg_attr_str = "");
|
||||
|
@ -34,4 +32,4 @@ private:
|
|||
int type;
|
||||
const char* type_str;
|
||||
const char* attr_str;
|
||||
};
|
||||
};
|
||||
|
|
|
@ -6,14 +6,10 @@
|
|||
#include <string.h>
|
||||
#include <string>
|
||||
|
||||
static int streq(const char* s1, const char* s2)
|
||||
{
|
||||
return ! strcmp(s1, s2);
|
||||
}
|
||||
static int streq(const char* s1, const char* s2) { return ! strcmp(s1, s2); }
|
||||
|
||||
// Returns it without trailing "::".
|
||||
string extract_module_name(const char* name)
|
||||
{
|
||||
string extract_module_name(const char* name) {
|
||||
string module_name = name;
|
||||
string::size_type pos = module_name.rfind("::");
|
||||
|
||||
|
@ -23,10 +19,9 @@ string extract_module_name(const char* name)
|
|||
module_name.erase(pos);
|
||||
|
||||
return module_name;
|
||||
}
|
||||
}
|
||||
|
||||
string extract_var_name(const char* name)
|
||||
{
|
||||
string extract_var_name(const char* name) {
|
||||
string var_name = name;
|
||||
string::size_type pos = var_name.rfind("::");
|
||||
|
||||
|
@ -37,21 +32,18 @@ string extract_var_name(const char* name)
|
|||
return string("");
|
||||
|
||||
return var_name.substr(pos + 2);
|
||||
}
|
||||
}
|
||||
|
||||
string normalized_module_name(const char* module_name)
|
||||
{
|
||||
string normalized_module_name(const char* module_name) {
|
||||
int mod_len;
|
||||
if ( (mod_len = strlen(module_name)) >= 2 && streq(module_name + mod_len - 2, "::") )
|
||||
mod_len -= 2;
|
||||
|
||||
return string(module_name, mod_len);
|
||||
}
|
||||
}
|
||||
|
||||
string make_full_var_name(const char* module_name, const char* var_name)
|
||||
{
|
||||
if ( ! module_name || streq(module_name, GLOBAL_MODULE_NAME) || strstr(var_name, "::") )
|
||||
{
|
||||
string make_full_var_name(const char* module_name, const char* var_name) {
|
||||
if ( ! module_name || streq(module_name, GLOBAL_MODULE_NAME) || strstr(var_name, "::") ) {
|
||||
if ( streq(GLOBAL_MODULE_NAME, extract_module_name(var_name).c_str()) )
|
||||
return extract_var_name(var_name);
|
||||
|
||||
|
@ -63,4 +55,4 @@ string make_full_var_name(const char* module_name, const char* var_name)
|
|||
full_name += var_name;
|
||||
|
||||
return full_name;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue