binpac: Add cmake-format and typos pre-commit configs

This commit is contained in:
Tim Wojtulewicz 2023-10-30 13:16:15 -07:00
parent 3297de477b
commit 964817f9bf
8 changed files with 108 additions and 104 deletions

View file

@ -14,49 +14,48 @@ set(BINPAC_SOVERSION 0)
set(ENABLE_SHARED true) set(ENABLE_SHARED true)
if ( ENABLE_STATIC_ONLY ) if(ENABLE_STATIC_ONLY)
set(ENABLE_STATIC true) set(ENABLE_STATIC true)
set(ENABLE_SHARED false) set(ENABLE_SHARED false)
endif () endif()
# Set default install paths # Set default install paths
include(GNUInstallDirs) include(GNUInstallDirs)
######################################################################## # ##############################################################################
## Dependency Configuration # Dependency Configuration
find_package(FLEX REQUIRED) find_package(FLEX REQUIRED)
find_package(BISON REQUIRED) find_package(BISON 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
endif() endif()
######################################################################## # ##############################################################################
## System Introspection # System Introspection
configure_file(${PROJECT_SOURCE_DIR}/config.h.in configure_file(${PROJECT_SOURCE_DIR}/config.h.in ${PROJECT_BINARY_DIR}/config.h)
${PROJECT_BINARY_DIR}/config.h)
include_directories(BEFORE ${PROJECT_BINARY_DIR}) include_directories(BEFORE ${PROJECT_BINARY_DIR})
######################################################################## # ##############################################################################
## Recurse on sub-directories # Recurse on sub-directories
add_subdirectory(lib) add_subdirectory(lib)
add_subdirectory(src) add_subdirectory(src)
######################################################################## # ##############################################################################
## Build Summary # Build Summary
if (CMAKE_BUILD_TYPE) if(CMAKE_BUILD_TYPE)
string(TOUPPER ${CMAKE_BUILD_TYPE} BuildType) string(TOUPPER ${CMAKE_BUILD_TYPE} BuildType)
endif () endif()
macro(display test desc summary) macro(display test desc summary)
if ( ${test} ) if(${test})
set(${summary} ${desc}) set(${summary} ${desc})
else () else()
set(${summary} no) set(${summary} no)
endif() endif()
endmacro() endmacro()
@ -81,7 +80,6 @@ message(
"\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)

View file

@ -7,61 +7,55 @@ check_type_size("unsigned int" SIZEOF_UNSIGNED_INT)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/binpac.h.in configure_file(${CMAKE_CURRENT_SOURCE_DIR}/binpac.h.in
${CMAKE_CURRENT_BINARY_DIR}/binpac.h) ${CMAKE_CURRENT_BINARY_DIR}/binpac.h)
include_directories(${CMAKE_CURRENT_SOURCE_DIR} include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
${CMAKE_CURRENT_BINARY_DIR})
set(binpac_headers set(binpac_headers
${CMAKE_CURRENT_BINARY_DIR}/binpac.h ${CMAKE_CURRENT_BINARY_DIR}/binpac.h binpac_analyzer.h binpac_buffer.h
binpac_analyzer.h binpac_bytestring.h binpac_exception.h binpac_regex.h)
binpac_buffer.h
binpac_bytestring.h
binpac_exception.h
binpac_regex.h
)
set(binpac_lib_SRCS set(binpac_lib_SRCS binpac_buffer.cc binpac_bytestring.cc binpac_regex.cc
binpac_buffer.cc ${binpac_headers})
binpac_bytestring.cc
binpac_regex.cc
${binpac_headers}
)
if ( ENABLE_SHARED ) if(ENABLE_SHARED)
add_library(binpac_lib SHARED ${binpac_lib_SRCS}) add_library(binpac_lib SHARED ${binpac_lib_SRCS})
target_compile_features(binpac_lib PRIVATE cxx_std_17) target_compile_features(binpac_lib PRIVATE cxx_std_17)
set_target_properties(binpac_lib PROPERTIES set_target_properties(
CXX_EXTENSIONS OFF binpac_lib
PROPERTIES CXX_EXTENSIONS OFF
SOVERSION ${BINPAC_SOVERSION} SOVERSION ${BINPAC_SOVERSION}
VERSION ${BINPAC_VERSION_MAJOR}.${BINPAC_VERSION_MINOR} VERSION ${BINPAC_VERSION_MAJOR}.${BINPAC_VERSION_MINOR}
MACOSX_RPATH true MACOSX_RPATH true
OUTPUT_NAME binpac) OUTPUT_NAME binpac)
install(TARGETS binpac_lib DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(TARGETS binpac_lib DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif () endif()
if ( ENABLE_STATIC ) if(ENABLE_STATIC)
add_library(binpac_static STATIC ${binpac_lib_SRCS}) add_library(binpac_static STATIC ${binpac_lib_SRCS})
target_compile_features(binpac_static PRIVATE cxx_std_17) target_compile_features(binpac_static PRIVATE cxx_std_17)
set_target_properties(binpac_static PROPERTIES set_target_properties(binpac_static PROPERTIES CXX_EXTENSIONS OFF OUTPUT_NAME
CXX_EXTENSIONS OFF binpac)
OUTPUT_NAME binpac)
install(TARGETS binpac_static DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(TARGETS binpac_static DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif () endif()
if ( ZEEK_ROOT_DIR ) if(ZEEK_ROOT_DIR)
# Installed in binpac subdir just for organization purposes. # Installed in binpac subdir just for organization purposes.
install(FILES ${binpac_headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/binpac) install(FILES ${binpac_headers}
else () DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/binpac)
else()
install(FILES ${binpac_headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(FILES ${binpac_headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif () endif()
# This is set to assist superprojects that want to build BinPac # This is set to assist superprojects that want to build BinPac from source and
# from source and rely on it as a target # rely on it as a target
if ( ENABLE_SHARED ) if(ENABLE_SHARED)
set(BinPAC_LIBRARY binpac_lib CACHE STRING "BinPAC library" FORCE) set(BinPAC_LIBRARY
else () binpac_lib
set(BinPAC_LIBRARY binpac_static CACHE STRING "BinPAC library" FORCE) CACHE STRING "BinPAC library" FORCE)
endif () else()
set(BinPAC_LIBRARY
binpac_static
CACHE STRING "BinPAC library" FORCE)
endif()
set(BinPAC_INCLUDE_DIR set(BinPAC_INCLUDE_DIR
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}
CACHE STRING "BinPAC header directories" FORCE CACHE STRING "BinPAC header directories" FORCE)
)

View file

@ -72,6 +72,8 @@ inline uint16 pac_swap(const uint16 x)
inline int16 pac_swap(const int16 x) inline int16 pac_swap(const int16 x)
{ {
// Forward to unsigned version with argument/result casted
// appropriately.
uint16 (*p)(const uint16) = &pac_swap; uint16 (*p)(const uint16) = &pac_swap;
return (*p)(x); return (*p)(x);
} }
@ -86,6 +88,8 @@ inline uint32 pac_swap(const uint32 x)
inline int32 pac_swap(const int32 x) inline int32 pac_swap(const int32 x)
{ {
// Forward to unsigned version with argument/result casted
// appropriately.
uint32 (*p)(const uint32) = &pac_swap; uint32 (*p)(const uint32) = &pac_swap;
return (*p)(x); return (*p)(x);
} }
@ -104,6 +108,8 @@ inline uint64 pac_swap(const uint64 x)
inline int64 pac_swap(const int64 x) inline int64 pac_swap(const int64 x)
{ {
// Forward to unsigned version with argument/result casted
// appropriately.
uint64 (*p)(const uint64) = &pac_swap; uint64 (*p)(const uint64) = &pac_swap;
return (*p)(x); return (*p)(x);
} }

View file

@ -31,7 +31,7 @@ public:
// Interface for delayed parsing. Sometimes BinPAC doesn't get the // Interface for delayed parsing. Sometimes BinPAC doesn't get the
// buffering right and then one can use these to feed parts // buffering right and then one can use these to feed parts
// individually and assemble them internally. After calling // individually and assemble them internally. After calling
// FinishBuffer(), one can send the uppper-layer flow an FlowEOF() to // FinishBuffer(), one can send the upper-layer flow an FlowEOF() to
// trigger parsing. // trigger parsing.
void BufferData(const_byteptr data, const_byteptr end); void BufferData(const_byteptr data, const_byteptr end);
void FinishBuffer(); void FinishBuffer();

View file

@ -62,7 +62,7 @@ numbers of issues below correspond to the patch numbers
in cases in cases
- problem: initialization of buffer occurs in every Parse call, - problem: initialization of buffer occurs in every Parse call,
regardless if it was initialized before or not; initialization regardless if it was initialized before or not; initialization
is correct only on first such occurence is correct only on first such occurrence
- solution: check to buffer_state is to be created always when - solution: check to buffer_state is to be created always when
buffering_state_id is in environment in Type::GenBufferConfig buffering_state_id is in environment in Type::GenBufferConfig
- changed condition from buffering_state_var_field_ to - changed condition from buffering_state_var_field_ to

View file

@ -1,16 +1,22 @@
bison_target(PACParser pac_parse.yy ${BinPAC_BINARY_DIR}/src/pac_parse.cc bison_target(
PACParser pac_parse.yy ${BinPAC_BINARY_DIR}/src/pac_parse.cc
DEFINES_FILE ${BinPAC_BINARY_DIR}/src/pac_parse.h DEFINES_FILE ${BinPAC_BINARY_DIR}/src/pac_parse.h
COMPILE_FLAGS "--debug") COMPILE_FLAGS "--debug")
flex_target(PACScanner pac_scan.ll ${BinPAC_BINARY_DIR}/pac_scan.cc) flex_target(PACScanner pac_scan.ll ${BinPAC_BINARY_DIR}/pac_scan.cc)
add_flex_bison_dependency(PACScanner PACParser) add_flex_bison_dependency(PACScanner PACParser)
if (MSVC) if(MSVC)
set_property(SOURCE pac_scan.cc APPEND_STRING PROPERTY COMPILE_FLAGS "/wd4018") set_property(
SOURCE pac_scan.cc
APPEND_STRING
PROPERTY COMPILE_FLAGS "/wd4018")
else() else()
set_property(SOURCE pac_scan.cc APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-sign-compare") set_property(
SOURCE pac_scan.cc
APPEND_STRING
PROPERTY COMPILE_FLAGS "-Wno-sign-compare")
endif() endif()
include_directories(${PROJECT_SOURCE_DIR}/src include_directories(${PROJECT_SOURCE_DIR}/src ${PROJECT_BINARY_DIR}/src)
${PROJECT_BINARY_DIR}/src)
set(binpac_SRCS set(binpac_SRCS
${BISON_PACParser_INPUT} ${BISON_PACParser_INPUT}
@ -98,17 +104,16 @@ set(binpac_SRCS
pac_typedecl.h pac_typedecl.h
pac_utils.h pac_utils.h
pac_varfield.h pac_varfield.h
pac_withinput.h pac_withinput.h)
)
add_executable(binpac ${binpac_SRCS}) add_executable(binpac ${binpac_SRCS})
target_compile_features(binpac PRIVATE cxx_std_17) target_compile_features(binpac PRIVATE cxx_std_17)
set_target_properties(binpac PROPERTIES CXX_EXTENSIONS OFF) set_target_properties(binpac 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 "BinPAC") if("${CMAKE_PROJECT_NAME}" STREQUAL "BinPAC")
add_subdirectory(auxil/libunistd EXCLUDE_FROM_ALL) add_subdirectory(auxil/libunistd EXCLUDE_FROM_ALL)
endif() endif()
target_link_libraries(binpac PRIVATE libunistd) target_link_libraries(binpac PRIVATE libunistd)
@ -116,7 +121,8 @@ endif()
install(TARGETS binpac DESTINATION bin) install(TARGETS binpac DESTINATION bin)
# This is set to assist superprojects that want to build BinPac # This is set to assist superprojects that want to build BinPac from source and
# from source and rely on it as a target # rely on it as a target
set(BinPAC_EXE binpac set(BinPAC_EXE
binpac
CACHE STRING "BinPAC executable" FORCE) CACHE STRING "BinPAC executable" FORCE)

View file

@ -55,7 +55,7 @@ public:
// //
const char* EvalExpr(Output* out, Env* env); const char* EvalExpr(Output* out, Env* env);
// force evaulation of IDs contained in this expression; // force evaluation of IDs contained in this expression;
// necessary with case expr and conditional let fields (&if) // necessary with case expr and conditional let fields (&if)
// for correct parsing of fields // for correct parsing of fields
void ForceIDEval(Output* out_cc, Env* env); void ForceIDEval(Output* out_cc, Env* env);

View file

@ -5,7 +5,7 @@
// ExternType represent external C++ types that are not defined in // ExternType represent external C++ types that are not defined in
// PAC specification (therefore they cannot appear in data layout // PAC specification (therefore they cannot appear in data layout
// spefication, e.g., in a record field). The type name is copied // specification, e.g., in a record field). The type name is copied
// literally to the compiled code. // literally to the compiled code.
class ExternType : public Type { class ExternType : public Type {