mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +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,73 +6,74 @@ 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
|
||||
DEFINES_FILE ${BifCl_BINARY_DIR}/bif_parse.h
|
||||
COMPILE_FLAGS "${BISON_FLAGS}")
|
||||
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)
|
||||
add_flex_bison_dependency(BIFScanner BIFParser)
|
||||
|
||||
set(bifcl_SRCS
|
||||
${BISON_BIFParser_INPUT}
|
||||
${FLEX_BIFScanner_INPUT}
|
||||
${BISON_BIFParser_OUTPUTS}
|
||||
${FLEX_BIFScanner_OUTPUTS}
|
||||
bif_arg.cc
|
||||
include/bif_arg.h
|
||||
module_util.cc
|
||||
include/module_util.h
|
||||
)
|
||||
${BISON_BIFParser_INPUT}
|
||||
${FLEX_BIFScanner_INPUT}
|
||||
${BISON_BIFParser_OUTPUTS}
|
||||
${FLEX_BIFScanner_OUTPUTS}
|
||||
bif_arg.cc
|
||||
include/bif_arg.h
|
||||
module_util.cc
|
||||
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")
|
||||
add_subdirectory(auxil/libunistd EXCLUDE_FROM_ALL)
|
||||
endif()
|
||||
target_link_libraries(bifcl PRIVATE libunistd)
|
||||
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)
|
||||
endif()
|
||||
|
||||
install(TARGETS bifcl DESTINATION bin)
|
||||
|
||||
if (CMAKE_BUILD_TYPE)
|
||||
string(TOUPPER ${CMAKE_BUILD_TYPE} BuildType)
|
||||
endif ()
|
||||
if(CMAKE_BUILD_TYPE)
|
||||
string(TOUPPER ${CMAKE_BUILD_TYPE} BuildType)
|
||||
endif()
|
||||
|
||||
message(
|
||||
"\n====================| Bifcl Build Summary |====================="
|
||||
"\n"
|
||||
"\nBuild type: ${CMAKE_BUILD_TYPE}"
|
||||
"\nBuild dir: ${PROJECT_BINARY_DIR}"
|
||||
"\nInstall prefix: ${CMAKE_INSTALL_PREFIX}"
|
||||
"\nDebug mode: ${ENABLE_DEBUG}"
|
||||
"\n"
|
||||
"\nCC: ${CMAKE_C_COMPILER}"
|
||||
"\nCFLAGS: ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BuildType}}"
|
||||
"\nCXX: ${CMAKE_CXX_COMPILER}"
|
||||
"\nCXXFLAGS: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${BuildType}}"
|
||||
"\nCPP: ${CMAKE_CXX_COMPILER}"
|
||||
"\n"
|
||||
"\n================================================================\n"
|
||||
)
|
||||
"\n====================| Bifcl Build Summary |====================="
|
||||
"\n"
|
||||
"\nBuild type: ${CMAKE_BUILD_TYPE}"
|
||||
"\nBuild dir: ${PROJECT_BINARY_DIR}"
|
||||
"\nInstall prefix: ${CMAKE_INSTALL_PREFIX}"
|
||||
"\nDebug mode: ${ENABLE_DEBUG}"
|
||||
"\n"
|
||||
"\nCC: ${CMAKE_C_COMPILER}"
|
||||
"\nCFLAGS: ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BuildType}}"
|
||||
"\nCXX: ${CMAKE_CXX_COMPILER}"
|
||||
"\nCXXFLAGS: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${BuildType}}"
|
||||
"\nCPP: ${CMAKE_CXX_COMPILER}"
|
||||
"\n"
|
||||
"\n================================================================\n")
|
||||
|
||||
include(UserChangedWarning)
|
||||
|
|
|
@ -7,93 +7,79 @@ using namespace std;
|
|||
|
||||
#include "bif_arg.h"
|
||||
|
||||
static struct
|
||||
{
|
||||
const char* type_enum;
|
||||
const char* bif_type;
|
||||
const char* zeek_type;
|
||||
const char* c_type;
|
||||
const char* c_type_smart;
|
||||
const char* accessor;
|
||||
const char* accessor_smart;
|
||||
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},
|
||||
static struct {
|
||||
const char* type_enum;
|
||||
const char* bif_type;
|
||||
const char* zeek_type;
|
||||
const char* c_type;
|
||||
const char* c_type_smart;
|
||||
const char* accessor;
|
||||
const char* accessor_smart;
|
||||
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},
|
||||
#include "bif_type.def"
|
||||
#undef DEFINE_BIF_TYPE
|
||||
};
|
||||
};
|
||||
|
||||
extern const char* arg_list_name;
|
||||
|
||||
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, 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)
|
||||
{
|
||||
name = arg_name;
|
||||
type = TYPE_OTHER;
|
||||
type_str = arg_type_str;
|
||||
attr_str = 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) )
|
||||
{
|
||||
type = i;
|
||||
type_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) ) {
|
||||
type = i;
|
||||
type_str = "";
|
||||
}
|
||||
}
|
||||
|
||||
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::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)
|
||||
{
|
||||
// Generate a runtime type-check pre-amble for types we understand
|
||||
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);
|
||||
fprintf(fp, "\t\tif ( __tag != %s )\n", builtin_func_arg_type[type].type_enum);
|
||||
fprintf(fp, "\t\t\t{\n");
|
||||
fprintf(fp,
|
||||
"\t\t\tzeek::emit_builtin_error(zeek::util::fmt(\"expected type %s for %s, got "
|
||||
"%%s\", zeek::type_name(__tag)));\n",
|
||||
builtin_func_arg_type[type].zeek_type, name);
|
||||
fprintf(fp, "\t\t\treturn nullptr;\n");
|
||||
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);
|
||||
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 ) {
|
||||
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);
|
||||
fprintf(fp, "\t\tif ( __tag != %s )\n", builtin_func_arg_type[type].type_enum);
|
||||
fprintf(fp, "\t\t\t{\n");
|
||||
fprintf(fp,
|
||||
"\t\t\tzeek::emit_builtin_error(zeek::util::fmt(\"expected type %s for %s, got "
|
||||
"%%s\", zeek::type_name(__tag)));\n",
|
||||
builtin_func_arg_type[type].zeek_type, name);
|
||||
fprintf(fp, "\t\t\treturn nullptr;\n");
|
||||
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);
|
||||
|
||||
char buf[1024];
|
||||
snprintf(buf, sizeof(buf), "(*%s)[%d].get()", arg_list_name, n);
|
||||
// Print the accessor expression.
|
||||
fprintf(fp, builtin_func_arg_type[type].accessor, buf);
|
||||
char buf[1024];
|
||||
snprintf(buf, sizeof(buf), "(*%s)[%d].get()", arg_list_name, n);
|
||||
// Print the accessor expression.
|
||||
fprintf(fp, builtin_func_arg_type[type].accessor, buf);
|
||||
|
||||
fprintf(fp, ");\n");
|
||||
}
|
||||
fprintf(fp, ");\n");
|
||||
}
|
||||
|
||||
void BuiltinFuncArg::PrintCArg(FILE* fp, int n)
|
||||
{
|
||||
fprintf(fp, "%s %s", builtin_func_arg_type[type].c_type_smart, name);
|
||||
}
|
||||
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,36 +2,34 @@
|
|||
|
||||
#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) \
|
||||
id,
|
||||
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 = "");
|
||||
BuiltinFuncArg(const char* arg_name, int arg_type);
|
||||
BuiltinFuncArg(const char* arg_name, const char* arg_type_str, const char* arg_attr_str = "");
|
||||
|
||||
void SetAttrStr(const char* arg_attr_str) { attr_str = arg_attr_str; };
|
||||
void SetAttrStr(const char* arg_attr_str) { attr_str = arg_attr_str; };
|
||||
|
||||
const char* Name() const { return name; }
|
||||
int Type() const { return type; }
|
||||
const char* Name() const { return name; }
|
||||
int Type() const { return type; }
|
||||
|
||||
void PrintZeek(FILE* fp);
|
||||
void PrintCDef(FILE* fp, int n, bool runtime_type_check = false);
|
||||
void PrintCArg(FILE* fp, int n);
|
||||
void PrintValConstructor(FILE* fp);
|
||||
void PrintZeek(FILE* fp);
|
||||
void PrintCDef(FILE* fp, int n, bool runtime_type_check = false);
|
||||
void PrintCArg(FILE* fp, int n);
|
||||
void PrintValConstructor(FILE* fp);
|
||||
|
||||
private:
|
||||
const char* name;
|
||||
int type;
|
||||
const char* type_str;
|
||||
const char* attr_str;
|
||||
};
|
||||
const char* name;
|
||||
int type;
|
||||
const char* type_str;
|
||||
const char* attr_str;
|
||||
};
|
||||
|
|
|
@ -6,61 +6,53 @@
|
|||
#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 module_name = name;
|
||||
string::size_type pos = module_name.rfind("::");
|
||||
string extract_module_name(const char* name) {
|
||||
string module_name = name;
|
||||
string::size_type pos = module_name.rfind("::");
|
||||
|
||||
if ( pos == string::npos )
|
||||
return string(GLOBAL_MODULE_NAME);
|
||||
if ( pos == string::npos )
|
||||
return string(GLOBAL_MODULE_NAME);
|
||||
|
||||
module_name.erase(pos);
|
||||
module_name.erase(pos);
|
||||
|
||||
return module_name;
|
||||
}
|
||||
return module_name;
|
||||
}
|
||||
|
||||
string extract_var_name(const char* name)
|
||||
{
|
||||
string var_name = name;
|
||||
string::size_type pos = var_name.rfind("::");
|
||||
string extract_var_name(const char* name) {
|
||||
string var_name = name;
|
||||
string::size_type pos = var_name.rfind("::");
|
||||
|
||||
if ( pos == string::npos )
|
||||
return var_name;
|
||||
if ( pos == string::npos )
|
||||
return var_name;
|
||||
|
||||
if ( pos + 2 > var_name.size() )
|
||||
return string("");
|
||||
if ( pos + 2 > var_name.size() )
|
||||
return string("");
|
||||
|
||||
return var_name.substr(pos + 2);
|
||||
}
|
||||
return var_name.substr(pos + 2);
|
||||
}
|
||||
|
||||
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;
|
||||
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);
|
||||
}
|
||||
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, "::") )
|
||||
{
|
||||
if ( streq(GLOBAL_MODULE_NAME, extract_module_name(var_name).c_str()) )
|
||||
return extract_var_name(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);
|
||||
|
||||
return string(var_name);
|
||||
}
|
||||
return string(var_name);
|
||||
}
|
||||
|
||||
string full_name = normalized_module_name(module_name);
|
||||
full_name += "::";
|
||||
full_name += var_name;
|
||||
string full_name = normalized_module_name(module_name);
|
||||
full_name += "::";
|
||||
full_name += var_name;
|
||||
|
||||
return full_name;
|
||||
}
|
||||
return full_name;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue