mirror of
https://github.com/zeek/zeek.git
synced 2025-10-08 01:28: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,73 +6,74 @@ include(cmake/CommonCMakeConfig.cmake)
|
||||||
find_package(BISON REQUIRED)
|
find_package(BISON REQUIRED)
|
||||||
find_package(FLEX REQUIRED)
|
find_package(FLEX REQUIRED)
|
||||||
|
|
||||||
if (MSVC)
|
if(MSVC)
|
||||||
add_compile_options(/J) # Similar to -funsigned-char on other platforms
|
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()
|
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()
|
endif()
|
||||||
|
|
||||||
include_directories(BEFORE
|
include_directories(BEFORE ${BifCl_SOURCE_DIR}/include ${BifCl_BINARY_DIR})
|
||||||
${BifCl_SOURCE_DIR}/include
|
|
||||||
${BifCl_BINARY_DIR}
|
|
||||||
)
|
|
||||||
|
|
||||||
set(BISON_FLAGS "--debug")
|
set(BISON_FLAGS "--debug")
|
||||||
|
|
||||||
# BIF parser/scanner
|
# BIF parser/scanner
|
||||||
bison_target(BIFParser builtin-func.y
|
bison_target(
|
||||||
${BifCl_BINARY_DIR}/bif_parse.cc
|
BIFParser builtin-func.y ${BifCl_BINARY_DIR}/bif_parse.cc
|
||||||
DEFINES_FILE ${BifCl_BINARY_DIR}/bif_parse.h
|
DEFINES_FILE ${BifCl_BINARY_DIR}/bif_parse.h
|
||||||
COMPILE_FLAGS "${BISON_FLAGS}")
|
COMPILE_FLAGS "${BISON_FLAGS}")
|
||||||
flex_target(BIFScanner builtin-func.l ${BifCl_BINARY_DIR}/bif_lex.cc)
|
flex_target(BIFScanner builtin-func.l ${BifCl_BINARY_DIR}/bif_lex.cc)
|
||||||
add_flex_bison_dependency(BIFScanner BIFParser)
|
add_flex_bison_dependency(BIFScanner BIFParser)
|
||||||
|
|
||||||
set(bifcl_SRCS
|
set(bifcl_SRCS
|
||||||
${BISON_BIFParser_INPUT}
|
${BISON_BIFParser_INPUT}
|
||||||
${FLEX_BIFScanner_INPUT}
|
${FLEX_BIFScanner_INPUT}
|
||||||
${BISON_BIFParser_OUTPUTS}
|
${BISON_BIFParser_OUTPUTS}
|
||||||
${FLEX_BIFScanner_OUTPUTS}
|
${FLEX_BIFScanner_OUTPUTS}
|
||||||
bif_arg.cc
|
bif_arg.cc
|
||||||
include/bif_arg.h
|
include/bif_arg.h
|
||||||
module_util.cc
|
module_util.cc
|
||||||
include/module_util.h
|
include/module_util.h)
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(bifcl ${bifcl_SRCS})
|
add_executable(bifcl ${bifcl_SRCS})
|
||||||
target_compile_features(bifcl PRIVATE cxx_std_17)
|
target_compile_features(bifcl PRIVATE cxx_std_17)
|
||||||
set_target_properties(bifcl PROPERTIES CXX_EXTENSIONS OFF)
|
set_target_properties(bifcl PROPERTIES CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
if (MSVC)
|
if(MSVC)
|
||||||
# If building separately from zeek, we need to add the libunistd subdirectory so
|
# If building separately from zeek, we need to add the libunistd subdirectory
|
||||||
# that linking doesn't fail.
|
# so that linking doesn't fail.
|
||||||
if ("${CMAKE_PROJECT_NAME}" STREQUAL "BifCl")
|
if("${CMAKE_PROJECT_NAME}" STREQUAL "BifCl")
|
||||||
add_subdirectory(auxil/libunistd EXCLUDE_FROM_ALL)
|
add_subdirectory(auxil/libunistd EXCLUDE_FROM_ALL)
|
||||||
endif()
|
endif()
|
||||||
target_link_libraries(bifcl PRIVATE libunistd)
|
target_link_libraries(bifcl PRIVATE libunistd)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
install(TARGETS bifcl DESTINATION bin)
|
install(TARGETS bifcl DESTINATION bin)
|
||||||
|
|
||||||
if (CMAKE_BUILD_TYPE)
|
if(CMAKE_BUILD_TYPE)
|
||||||
string(TOUPPER ${CMAKE_BUILD_TYPE} BuildType)
|
string(TOUPPER ${CMAKE_BUILD_TYPE} BuildType)
|
||||||
endif ()
|
endif()
|
||||||
|
|
||||||
message(
|
message(
|
||||||
"\n====================| Bifcl Build Summary |====================="
|
"\n====================| Bifcl Build Summary |====================="
|
||||||
"\n"
|
"\n"
|
||||||
"\nBuild type: ${CMAKE_BUILD_TYPE}"
|
"\nBuild type: ${CMAKE_BUILD_TYPE}"
|
||||||
"\nBuild dir: ${PROJECT_BINARY_DIR}"
|
"\nBuild dir: ${PROJECT_BINARY_DIR}"
|
||||||
"\nInstall prefix: ${CMAKE_INSTALL_PREFIX}"
|
"\nInstall prefix: ${CMAKE_INSTALL_PREFIX}"
|
||||||
"\nDebug mode: ${ENABLE_DEBUG}"
|
"\nDebug mode: ${ENABLE_DEBUG}"
|
||||||
"\n"
|
"\n"
|
||||||
"\nCC: ${CMAKE_C_COMPILER}"
|
"\nCC: ${CMAKE_C_COMPILER}"
|
||||||
"\nCFLAGS: ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BuildType}}"
|
"\nCFLAGS: ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BuildType}}"
|
||||||
"\nCXX: ${CMAKE_CXX_COMPILER}"
|
"\nCXX: ${CMAKE_CXX_COMPILER}"
|
||||||
"\nCXXFLAGS: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${BuildType}}"
|
"\nCXXFLAGS: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${BuildType}}"
|
||||||
"\nCPP: ${CMAKE_CXX_COMPILER}"
|
"\nCPP: ${CMAKE_CXX_COMPILER}"
|
||||||
"\n"
|
"\n"
|
||||||
"\n================================================================\n"
|
"\n================================================================\n")
|
||||||
)
|
|
||||||
|
|
||||||
include(UserChangedWarning)
|
include(UserChangedWarning)
|
||||||
|
|
|
@ -7,93 +7,79 @@ using namespace std;
|
||||||
|
|
||||||
#include "bif_arg.h"
|
#include "bif_arg.h"
|
||||||
|
|
||||||
static struct
|
static struct {
|
||||||
{
|
const char* type_enum;
|
||||||
const char* type_enum;
|
const char* bif_type;
|
||||||
const char* bif_type;
|
const char* zeek_type;
|
||||||
const char* zeek_type;
|
const char* c_type;
|
||||||
const char* c_type;
|
const char* c_type_smart;
|
||||||
const char* c_type_smart;
|
const char* accessor;
|
||||||
const char* accessor;
|
const char* accessor_smart;
|
||||||
const char* accessor_smart;
|
const char* cast_smart;
|
||||||
const char* cast_smart;
|
const char* constructor;
|
||||||
const char* constructor;
|
const char* ctor_smart;
|
||||||
const char* ctor_smart;
|
} builtin_func_arg_type[] = {
|
||||||
} builtin_func_arg_type[] = {
|
#define DEFINE_BIF_TYPE(id, bif_type, zeek_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, \
|
||||||
#define DEFINE_BIF_TYPE(id, bif_type, zeek_type, c_type, c_type_smart, accessor, accessor_smart, \
|
constructor, ctor_smart) \
|
||||||
cast_smart, constructor, ctor_smart) \
|
{#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"
|
#include "bif_type.def"
|
||||||
#undef DEFINE_BIF_TYPE
|
#undef DEFINE_BIF_TYPE
|
||||||
};
|
};
|
||||||
|
|
||||||
extern const char* arg_list_name;
|
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;
|
||||||
name = arg_name;
|
type = arg_type;
|
||||||
type = arg_type;
|
type_str = "";
|
||||||
type_str = "";
|
attr_str = "";
|
||||||
attr_str = "";
|
}
|
||||||
}
|
|
||||||
|
|
||||||
BuiltinFuncArg::BuiltinFuncArg(const char* arg_name, const char* arg_type_str,
|
BuiltinFuncArg::BuiltinFuncArg(const char* arg_name, const char* arg_type_str, const char* arg_attr_str) {
|
||||||
const char* arg_attr_str)
|
name = arg_name;
|
||||||
{
|
type = TYPE_OTHER;
|
||||||
name = arg_name;
|
type_str = arg_type_str;
|
||||||
type = TYPE_OTHER;
|
attr_str = arg_attr_str;
|
||||||
type_str = arg_type_str;
|
|
||||||
attr_str = arg_attr_str;
|
|
||||||
|
|
||||||
for ( int i = 0; builtin_func_arg_type[i].bif_type[0] != '\0'; ++i )
|
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 = i;
|
type_str = "";
|
||||||
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);
|
||||||
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
|
||||||
// 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\t{\n");
|
fprintf(fp, "\t\tzeek::TypeTag __tag = (*%s)[%d]->GetType()->Tag();\n", arg_list_name, n);
|
||||||
fprintf(fp, "\t\t// Runtime type check for %s argument\n", name);
|
fprintf(fp, "\t\tif ( __tag != %s )\n", builtin_func_arg_type[type].type_enum);
|
||||||
fprintf(fp, "\t\tzeek::TypeTag __tag = (*%s)[%d]->GetType()->Tag();\n", arg_list_name, n);
|
fprintf(fp, "\t\t\t{\n");
|
||||||
fprintf(fp, "\t\tif ( __tag != %s )\n", builtin_func_arg_type[type].type_enum);
|
fprintf(fp,
|
||||||
fprintf(fp, "\t\t\t{\n");
|
"\t\t\tzeek::emit_builtin_error(zeek::util::fmt(\"expected type %s for %s, got "
|
||||||
fprintf(fp,
|
"%%s\", zeek::type_name(__tag)));\n",
|
||||||
"\t\t\tzeek::emit_builtin_error(zeek::util::fmt(\"expected type %s for %s, got "
|
builtin_func_arg_type[type].zeek_type, name);
|
||||||
"%%s\", zeek::type_name(__tag)));\n",
|
fprintf(fp, "\t\t\treturn nullptr;\n");
|
||||||
builtin_func_arg_type[type].zeek_type, name);
|
fprintf(fp, "\t\t\t}\n");
|
||||||
fprintf(fp, "\t\t\treturn nullptr;\n");
|
fprintf(fp, "\t\t}\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);
|
||||||
}
|
|
||||||
fprintf(fp, "\t%s %s = (%s) (", builtin_func_arg_type[type].c_type, name,
|
|
||||||
builtin_func_arg_type[type].c_type);
|
|
||||||
|
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
snprintf(buf, sizeof(buf), "(*%s)[%d].get()", arg_list_name, n);
|
snprintf(buf, sizeof(buf), "(*%s)[%d].get()", arg_list_name, n);
|
||||||
// Print the accessor expression.
|
// Print the accessor expression.
|
||||||
fprintf(fp, builtin_func_arg_type[type].accessor, buf);
|
fprintf(fp, builtin_func_arg_type[type].accessor, buf);
|
||||||
|
|
||||||
fprintf(fp, ");\n");
|
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);
|
||||||
fprintf(fp, "%s %s", builtin_func_arg_type[type].c_type_smart, name);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void BuiltinFuncArg::PrintValConstructor(FILE* fp)
|
void BuiltinFuncArg::PrintValConstructor(FILE* fp) { fprintf(fp, builtin_func_arg_type[type].ctor_smart, name); }
|
||||||
{
|
|
||||||
fprintf(fp, builtin_func_arg_type[type].ctor_smart, name);
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,36 +2,34 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
enum builtin_func_arg_type
|
enum builtin_func_arg_type {
|
||||||
{
|
#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, \
|
||||||
#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, \
|
constructor, ctor_smart) \
|
||||||
cast_smart, constructor, ctor_smart) \
|
id,
|
||||||
id,
|
|
||||||
#include "bif_type.def"
|
#include "bif_type.def"
|
||||||
#undef DEFINE_BIF_TYPE
|
#undef DEFINE_BIF_TYPE
|
||||||
};
|
};
|
||||||
|
|
||||||
extern const char* builtin_func_arg_type_bro_name[];
|
extern const char* builtin_func_arg_type_bro_name[];
|
||||||
|
|
||||||
class BuiltinFuncArg final
|
class BuiltinFuncArg final {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
BuiltinFuncArg(const char* arg_name, int arg_type);
|
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, 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; }
|
const char* Name() const { return name; }
|
||||||
int Type() const { return type; }
|
int Type() const { return type; }
|
||||||
|
|
||||||
void PrintZeek(FILE* fp);
|
void PrintZeek(FILE* fp);
|
||||||
void PrintCDef(FILE* fp, int n, bool runtime_type_check = false);
|
void PrintCDef(FILE* fp, int n, bool runtime_type_check = false);
|
||||||
void PrintCArg(FILE* fp, int n);
|
void PrintCArg(FILE* fp, int n);
|
||||||
void PrintValConstructor(FILE* fp);
|
void PrintValConstructor(FILE* fp);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* name;
|
const char* name;
|
||||||
int type;
|
int type;
|
||||||
const char* type_str;
|
const char* type_str;
|
||||||
const char* attr_str;
|
const char* attr_str;
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,61 +6,53 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
static int streq(const char* s1, const char* s2)
|
static int streq(const char* s1, const char* s2) { return ! strcmp(s1, s2); }
|
||||||
{
|
|
||||||
return ! strcmp(s1, s2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns it without trailing "::".
|
// Returns it without trailing "::".
|
||||||
string extract_module_name(const char* name)
|
string extract_module_name(const char* name) {
|
||||||
{
|
string module_name = name;
|
||||||
string module_name = name;
|
string::size_type pos = module_name.rfind("::");
|
||||||
string::size_type pos = module_name.rfind("::");
|
|
||||||
|
|
||||||
if ( pos == string::npos )
|
if ( pos == string::npos )
|
||||||
return string(GLOBAL_MODULE_NAME);
|
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 extract_var_name(const char* name) {
|
||||||
{
|
string var_name = name;
|
||||||
string var_name = name;
|
string::size_type pos = var_name.rfind("::");
|
||||||
string::size_type pos = var_name.rfind("::");
|
|
||||||
|
|
||||||
if ( pos == string::npos )
|
if ( pos == string::npos )
|
||||||
return var_name;
|
return var_name;
|
||||||
|
|
||||||
if ( pos + 2 > var_name.size() )
|
if ( pos + 2 > var_name.size() )
|
||||||
return string("");
|
return string("");
|
||||||
|
|
||||||
return var_name.substr(pos + 2);
|
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;
|
||||||
int mod_len;
|
if ( (mod_len = strlen(module_name)) >= 2 && streq(module_name + mod_len - 2, "::") )
|
||||||
if ( (mod_len = strlen(module_name)) >= 2 && streq(module_name + mod_len - 2, "::") )
|
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)
|
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 ( ! 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);
|
||||||
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);
|
string full_name = normalized_module_name(module_name);
|
||||||
full_name += "::";
|
full_name += "::";
|
||||||
full_name += var_name;
|
full_name += var_name;
|
||||||
|
|
||||||
return full_name;
|
return full_name;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue