Reformat Zeek in Spicy style

This largely copies over Spicy's `.clang-format` configuration file. The
one place where we deviate is header include order since Zeek depends on
headers being included in a certain order.
This commit is contained in:
Benjamin Bannier 2023-10-10 21:13:34 +02:00
parent 7b8e7ed72c
commit f5a76c1aed
786 changed files with 131714 additions and 153609 deletions

View file

@ -8,19 +8,16 @@
#include "zeek/OpaqueVal.h"
#include "zeek/script_opt/CPP/Func.h"
namespace zeek
{
namespace zeek {
using SubNetValPtr = IntrusivePtr<zeek::SubNetVal>;
namespace detail
{
namespace detail {
class CPPRuntime
{
class CPPRuntime {
public:
static auto RawOptField(const RecordValPtr& rv, int field) { return rv->RawOptField(field); }
};
static auto RawOptField(const RecordValPtr& rv, int field) { return rv->RawOptField(field); }
};
// Returns the concatenation of the given strings.
extern StringValPtr str_concat__CPP(const String* s1, const String* s2);
@ -50,27 +47,21 @@ extern ValPtr when_index_slice__CPP(VectorVal* vec, const ListVal* lv);
// Calls out to the given script or BiF function, which does not return
// a value.
inline ValPtr invoke_void__CPP(Func* f, std::vector<ValPtr> args, Frame* frame)
{
return f->Invoke(&args, frame);
}
inline ValPtr invoke_void__CPP(Func* f, std::vector<ValPtr> args, Frame* frame) { return f->Invoke(&args, frame); }
// Used for error propagation by failed calls.
class CPPInterpreterException : public InterpreterException
{
};
class CPPInterpreterException : public InterpreterException {};
// Calls out to the given script or BiF function. A separate function because
// of the need to (1) construct the "args" vector using {} initializers,
// but (2) needing to have the address of that vector.
inline ValPtr invoke__CPP(Func* f, std::vector<ValPtr> args, Frame* frame)
{
auto v = f->Invoke(&args, frame);
if ( ! v )
throw CPPInterpreterException();
inline ValPtr invoke__CPP(Func* f, std::vector<ValPtr> args, Frame* frame) {
auto v = f->Invoke(&args, frame);
if ( ! v )
throw CPPInterpreterException();
return v;
}
return v;
}
// The same, but raises an interpreter exception if the function does
// not return a value. Used for calls inside "when" conditions. The
@ -80,17 +71,14 @@ inline ValPtr invoke__CPP(Func* f, std::vector<ValPtr> args, Frame* frame)
extern ValPtr when_invoke__CPP(Func* f, std::vector<ValPtr> args, Frame* frame, void* caller_addr);
// Thrown when a call inside a "when" delays.
class CPPDelayedCallException : public InterpreterException
{
};
class CPPDelayedCallException : public InterpreterException {};
// Assigns the given value to the given global. A separate function because
// we also need to return the value, for use in assignment cascades.
inline ValPtr set_global__CPP(IDPtr g, ValPtr v)
{
g->SetVal(v);
return v;
}
inline ValPtr set_global__CPP(IDPtr g, ValPtr v) {
g->SetVal(v);
return v;
}
// Assigns the given global to the given value, which corresponds to an
// event handler.
@ -114,31 +102,28 @@ extern SubNetValPtr addr_mask__CPP(const IPAddr& a, uint32_t mask);
// Assigns the given field in the given record to the given value. A
// separate function to allow for assignment cascades.
inline ValPtr assign_field__CPP(RecordValPtr rec, int field, ValPtr v)
{
rec->Assign(field, v);
return v;
}
inline ValPtr assign_field__CPP(RecordValPtr rec, int field, ValPtr v) {
rec->Assign(field, v);
return v;
}
// Returns the given field in the given record. A separate function to
// support error handling.
inline ValPtr field_access__CPP(const RecordValPtr& rec, int field)
{
auto v = rec->GetFieldOrDefault(field);
if ( ! v )
reporter->CPPRuntimeError("field value missing");
inline ValPtr field_access__CPP(const RecordValPtr& rec, int field) {
auto v = rec->GetFieldOrDefault(field);
if ( ! v )
reporter->CPPRuntimeError("field value missing");
return v;
}
return v;
}
#define NATIVE_FIELD_ACCESS(type, zaccessor, vaccessor) \
inline type field_access_##type##__CPP(const RecordValPtr& r, int field) \
{ \
auto rv = CPPRuntime::RawOptField(r, field); \
if ( rv ) \
return (*rv).zaccessor(); \
return field_access__CPP(r, field)->vaccessor(); \
}
#define NATIVE_FIELD_ACCESS(type, zaccessor, vaccessor) \
inline type field_access_##type##__CPP(const RecordValPtr& r, int field) { \
auto rv = CPPRuntime::RawOptField(r, field); \
if ( rv ) \
return (*rv).zaccessor(); \
return field_access__CPP(r, field)->vaccessor(); \
}
NATIVE_FIELD_ACCESS(bool, AsInt, AsBool)
NATIVE_FIELD_ACCESS(int, AsInt, AsInt)
@ -146,14 +131,13 @@ NATIVE_FIELD_ACCESS(zeek_int_t, AsInt, AsInt)
NATIVE_FIELD_ACCESS(zeek_uint_t, AsCount, AsCount)
NATIVE_FIELD_ACCESS(double, AsDouble, AsDouble)
#define VP_FIELD_ACCESS(type, zaccessor) \
inline type##Ptr field_access_##type##__CPP(const RecordValPtr& r, int field) \
{ \
auto rv = CPPRuntime::RawOptField(r, field); \
if ( rv ) \
return {NewRef{}, rv->zaccessor()}; \
return cast_intrusive<type>(field_access__CPP(r, field)); \
}
#define VP_FIELD_ACCESS(type, zaccessor) \
inline type##Ptr field_access_##type##__CPP(const RecordValPtr& r, int field) { \
auto rv = CPPRuntime::RawOptField(r, field); \
if ( rv ) \
return {NewRef{}, rv->zaccessor()}; \
return cast_intrusive<type>(field_access__CPP(r, field)); \
}
VP_FIELD_ACCESS(StringVal, AsString)
VP_FIELD_ACCESS(AddrVal, AsAddr)
@ -182,53 +166,48 @@ extern void remove_element__CPP(TableValPtr aggr, ListValPtr indices);
// Returns the given table/set (which should be empty) coerced to
// the given Zeek type. A separate function in order to deal with
// error handling. Inlined because this gets invoked a lot.
inline TableValPtr table_coerce__CPP(const ValPtr& v, const TypePtr& t)
{
TableVal* tv = v->AsTableVal();
inline TableValPtr table_coerce__CPP(const ValPtr& v, const TypePtr& t) {
TableVal* tv = v->AsTableVal();
if ( tv->Size() > 0 )
reporter->CPPRuntimeError("coercion of non-empty table/set");
if ( tv->Size() > 0 )
reporter->CPPRuntimeError("coercion of non-empty table/set");
return make_intrusive<TableVal>(cast_intrusive<TableType>(t), tv->GetAttrs());
}
return make_intrusive<TableVal>(cast_intrusive<TableType>(t), tv->GetAttrs());
}
// For tables, executes t1 += t2.
inline TableValPtr table_append__CPP(const TableValPtr& t1, const TableValPtr& t2)
{
t2->AddTo(t1.get(), false);
return t1;
}
inline TableValPtr table_append__CPP(const TableValPtr& t1, const TableValPtr& t2) {
t2->AddTo(t1.get(), false);
return t1;
}
// For tables, executes t1 -= t2.
inline TableValPtr table_remove_from__CPP(const TableValPtr& t1, const TableValPtr& t2)
{
if ( t2->Size() > 0 )
t2->RemoveFrom(t1.get());
return t1;
}
inline TableValPtr table_remove_from__CPP(const TableValPtr& t1, const TableValPtr& t2) {
if ( t2->Size() > 0 )
t2->RemoveFrom(t1.get());
return t1;
}
// The same, for an empty record.
inline VectorValPtr vector_coerce__CPP(const ValPtr& v, const TypePtr& t)
{
VectorVal* vv = v->AsVectorVal();
inline VectorValPtr vector_coerce__CPP(const ValPtr& v, const TypePtr& t) {
VectorVal* vv = v->AsVectorVal();
if ( vv->Size() > 0 )
reporter->CPPRuntimeError("coercion of non-empty vector");
if ( vv->Size() > 0 )
reporter->CPPRuntimeError("coercion of non-empty vector");
return make_intrusive<VectorVal>(cast_intrusive<VectorType>(t));
}
return make_intrusive<VectorVal>(cast_intrusive<VectorType>(t));
}
// Constructs a set of the given type, containing the given elements, and
// with the associated attributes.
extern TableValPtr set_constructor__CPP(std::vector<ValPtr> elements, TableTypePtr t,
std::vector<int> attr_tags, std::vector<ValPtr> attr_vals);
extern TableValPtr set_constructor__CPP(std::vector<ValPtr> elements, TableTypePtr t, std::vector<int> attr_tags,
std::vector<ValPtr> attr_vals);
// Constructs a table of the given type, containing the given elements
// (specified as parallel index/value vectors), and with the associated
// attributes.
extern TableValPtr table_constructor__CPP(std::vector<ValPtr> indices, std::vector<ValPtr> vals,
TableTypePtr t, std::vector<int> attr_tags,
std::vector<ValPtr> attr_vals);
extern TableValPtr table_constructor__CPP(std::vector<ValPtr> indices, std::vector<ValPtr> vals, TableTypePtr t,
std::vector<int> attr_tags, std::vector<ValPtr> attr_vals);
// Assigns a set of attributes to an identifier.
extern void assign_attrs__CPP(IDPtr id, std::vector<int> attr_tags, std::vector<ValPtr> attr_vals);
@ -238,18 +217,16 @@ extern void assign_attrs__CPP(IDPtr id, std::vector<int> attr_tags, std::vector<
extern RecordValPtr record_constructor__CPP(std::vector<ValPtr> vals, RecordTypePtr t);
// Same, but with a map when using a named constructor.
extern RecordValPtr record_constructor_map__CPP(std::vector<ValPtr> vals, std::vector<int> map,
RecordTypePtr t);
extern RecordValPtr record_constructor_map__CPP(std::vector<ValPtr> vals, std::vector<int> map, RecordTypePtr t);
// Constructs a vector of the given type, populated with the given values.
extern VectorValPtr vector_constructor__CPP(std::vector<ValPtr> vals, VectorTypePtr t);
// For patterns, executes p1 += p2.
inline PatternValPtr re_append__CPP(const PatternValPtr& p1, const PatternValPtr& p2)
{
p2->AddTo(p1.get(), false);
return p1;
}
inline PatternValPtr re_append__CPP(const PatternValPtr& p1, const PatternValPtr& p2) {
p2->AddTo(p1.get(), false);
return p1;
}
// Schedules an event to occur at the given absolute time, parameterized
// with the given set of values. A separate function to facilitate avoiding
@ -257,52 +234,41 @@ inline PatternValPtr re_append__CPP(const PatternValPtr& p1, const PatternValPtr
extern ValPtr schedule__CPP(double dt, EventHandlerPtr event, std::vector<ValPtr> args);
// Simple helper functions for supporting absolute value.
inline zeek_uint_t iabs__CPP(zeek_int_t v)
{
return v < 0 ? -v : v;
}
inline zeek_uint_t iabs__CPP(zeek_int_t v) { return v < 0 ? -v : v; }
inline double fabs__CPP(double v)
{
return v < 0.0 ? -v : v;
}
inline double fabs__CPP(double v) { return v < 0.0 ? -v : v; }
// The following operations are provided using functions to support
// error checking/reporting.
inline zeek_int_t idiv__CPP(zeek_int_t v1, zeek_int_t v2)
{
if ( v2 == 0 )
reporter->CPPRuntimeError("division by zero");
return v1 / v2;
}
inline zeek_int_t idiv__CPP(zeek_int_t v1, zeek_int_t v2) {
if ( v2 == 0 )
reporter->CPPRuntimeError("division by zero");
return v1 / v2;
}
inline zeek_int_t imod__CPP(zeek_int_t v1, zeek_int_t v2)
{
if ( v2 == 0 )
reporter->CPPRuntimeError("modulo by zero");
return v1 % v2;
}
inline zeek_int_t imod__CPP(zeek_int_t v1, zeek_int_t v2) {
if ( v2 == 0 )
reporter->CPPRuntimeError("modulo by zero");
return v1 % v2;
}
inline zeek_uint_t udiv__CPP(zeek_uint_t v1, zeek_uint_t v2)
{
if ( v2 == 0 )
reporter->CPPRuntimeError("division by zero");
return v1 / v2;
}
inline zeek_uint_t udiv__CPP(zeek_uint_t v1, zeek_uint_t v2) {
if ( v2 == 0 )
reporter->CPPRuntimeError("division by zero");
return v1 / v2;
}
inline zeek_uint_t umod__CPP(zeek_uint_t v1, zeek_uint_t v2)
{
if ( v2 == 0 )
reporter->CPPRuntimeError("modulo by zero");
return v1 % v2;
}
inline zeek_uint_t umod__CPP(zeek_uint_t v1, zeek_uint_t v2) {
if ( v2 == 0 )
reporter->CPPRuntimeError("modulo by zero");
return v1 % v2;
}
inline double fdiv__CPP(double v1, double v2)
{
if ( v2 == 0.0 )
reporter->CPPRuntimeError("division by zero");
return v1 / v2;
}
inline double fdiv__CPP(double v1, double v2) {
if ( v2 == 0.0 )
reporter->CPPRuntimeError("division by zero");
return v1 / v2;
}
} // namespace zeek::detail
} // namespace zeek
} // namespace detail
} // namespace zeek