Merge remote-tracking branch 'origin/topic/timw/cleanup'

* origin/topic/timw/cleanup:
  Expr: use fmt instead of sprintf
  Expr: other minor initialization cleanup
  Expr: use List::empty()
  Expr: Convert a bunch of methods returning ints to return bools
  IPAddr: minor cleanup
  PriorityQueue: initialization cleanup
  IP: Cleanup initialization, make a few functions consistent with others
This commit is contained in:
Jon Siwek 2020-02-27 14:24:55 -08:00
commit 646a2313ae
10 changed files with 167 additions and 165 deletions

View file

@ -55,12 +55,8 @@ const char* expr_name(BroExprTag t)
return expr_names[int(t)];
}
Expr::Expr(BroExprTag arg_tag)
Expr::Expr(BroExprTag arg_tag) : tag(arg_tag), type(0), paren(false)
{
tag = arg_tag;
type = 0;
paren = 0;
SetLocationInfo(&start_location, &end_location);
}
@ -69,14 +65,14 @@ Expr::~Expr()
Unref(type);
}
int Expr::CanAdd() const
bool Expr::CanAdd() const
{
return 0;
return false;
}
int Expr::CanDel() const
bool Expr::CanDel() const
{
return 0;
return false;
}
void Expr::Add(Frame* /* f */)
@ -113,14 +109,14 @@ BroType* Expr::InitType() const
return type->Ref();
}
int Expr::IsRecordElement(TypeDecl* /* td */) const
bool Expr::IsRecordElement(TypeDecl* /* td */) const
{
return 0;
return false;
}
int Expr::IsPure() const
bool Expr::IsPure() const
{
return 1;
return true;
}
Val* Expr::InitVal(const BroType* t, Val* aggr) const
@ -137,7 +133,7 @@ Val* Expr::InitVal(const BroType* t, Val* aggr) const
return check_and_promote(Eval(0), t, 1);
}
int Expr::IsError() const
bool Expr::IsError() const
{
return type && type->Tag() == TYPE_ERROR;
}
@ -153,12 +149,12 @@ void Expr::SetError(const char* msg)
SetError();
}
int Expr::IsZero() const
bool Expr::IsZero() const
{
return IsConst() && ExprVal()->IsZero();
}
int Expr::IsOne() const
bool Expr::IsOne() const
{
return IsConst() && ExprVal()->IsOne();
}
@ -295,7 +291,7 @@ void NameExpr::Assign(Frame* f, Val* v)
f->SetElement(id, v);
}
int NameExpr::IsPure() const
bool NameExpr::IsPure() const
{
return id->IsConst();
}
@ -414,7 +410,7 @@ Val* UnaryExpr::Eval(Frame* f) const
}
}
int UnaryExpr::IsPure() const
bool UnaryExpr::IsPure() const
{
return op->IsPure();
}
@ -551,7 +547,7 @@ Val* BinaryExpr::Eval(Frame* f) const
return result;
}
int BinaryExpr::IsPure() const
bool BinaryExpr::IsPure() const
{
return op1->IsPure() && op2->IsPure();
}
@ -604,20 +600,20 @@ Val* BinaryExpr::Fold(Val* v1, Val* v2) const
bro_int_t i1 = 0, i2 = 0, i3 = 0;
bro_uint_t u1 = 0, u2 = 0, u3 = 0;
double d1 = 0.0, d2 = 0.0, d3 = 0.0;
int is_integral = 0;
int is_unsigned = 0;
bool is_integral = false;
bool is_unsigned = false;
if ( it == TYPE_INTERNAL_INT )
{
i1 = v1->InternalInt();
i2 = v2->InternalInt();
++is_integral;
is_integral = true;
}
else if ( it == TYPE_INTERNAL_UNSIGNED )
{
u1 = v1->InternalUnsigned();
u2 = v2->InternalUnsigned();
++is_unsigned;
is_unsigned = true;
}
else if ( it == TYPE_INTERNAL_DOUBLE )
{
@ -858,7 +854,7 @@ Val* BinaryExpr::AddrFold(Val* v1, Val* v2) const
{
IPAddr a1 = v1->AsAddr();
IPAddr a2 = v2->AsAddr();
int result = 0;
bool result = false;
switch ( tag ) {
@ -893,7 +889,7 @@ Val* BinaryExpr::SubNetFold(Val* v1, Val* v2) const
const IPPrefix& n1 = v1->AsSubNet();
const IPPrefix& n2 = v2->AsSubNet();
bool result = ( n1 == n2 ) ? true : false;
bool result = n1 == n2;
if ( tag == EXPR_NE )
result = ! result;
@ -1053,9 +1049,9 @@ Val* IncrExpr::Eval(Frame* f) const
}
}
int IncrExpr::IsPure() const
bool IncrExpr::IsPure() const
{
return 0;
return false;
}
ComplementExpr::ComplementExpr(Expr* arg_op) : UnaryExpr(EXPR_COMPLEMENT, arg_op)
@ -1605,8 +1601,8 @@ Val* BoolExpr::Eval(Frame* f) const
if ( ! v1 )
return 0;
int is_vec1 = is_vector(op1);
int is_vec2 = is_vector(op2);
bool is_vec1 = is_vector(op1);
bool is_vec2 = is_vector(op2);
// Handle scalar op scalar
if ( ! is_vec1 && ! is_vec2 )
@ -2044,7 +2040,7 @@ Val* CondExpr::Eval(Frame* f) const
return result;
}
int CondExpr::IsPure() const
bool CondExpr::IsPure() const
{
return op1->IsPure() && op2->IsPure() && op3->IsPure();
}
@ -2172,7 +2168,7 @@ bool AssignExpr::TypeCheck(attr_list* attrs)
std::copy(attrs->begin(), attrs->end(), std::back_inserter(*attr_copy));
}
bool empty_list_assignment = (op2->AsListExpr()->Exprs().length() == 0);
bool empty_list_assignment = (op2->AsListExpr()->Exprs().empty());
if ( op1->Type()->IsSet() )
op2 = new SetConstructorExpr(op2->AsListExpr(), attr_copy);
@ -2289,11 +2285,8 @@ bool AssignExpr::TypeCheckArithmetics(TypeTag bt1, TypeTag bt2)
{
if ( ! IsArithmetic(bt2) )
{
char err[512];
snprintf(err, sizeof(err),
"assignment of non-arithmetic value to arithmetic (%s/%s)",
type_name(bt1), type_name(bt2));
ExprError(err);
ExprError(fmt("assignment of non-arithmetic value to arithmetic (%s/%s)",
type_name(bt1), type_name(bt2)));
return false;
}
@ -2499,7 +2492,7 @@ Val* AssignExpr::InitVal(const BroType* t, Val* aggr) const
}
}
int AssignExpr::IsRecordElement(TypeDecl* td) const
bool AssignExpr::IsRecordElement(TypeDecl* td) const
{
if ( op1->Tag() == EXPR_NAME )
{
@ -2510,15 +2503,15 @@ int AssignExpr::IsRecordElement(TypeDecl* td) const
td->id = copy_string(n->Id()->Name());
}
return 1;
return true;
}
else
return 0;
return false;
}
int AssignExpr::IsPure() const
bool AssignExpr::IsPure() const
{
return 0;
return false;
}
IndexSliceAssignExpr::IndexSliceAssignExpr(Expr* op1, Expr* op2, int is_init)
@ -2595,19 +2588,19 @@ IndexExpr::IndexExpr(Expr* arg_op1, ListExpr* arg_op2, bool arg_is_slice)
}
int IndexExpr::CanAdd() const
bool IndexExpr::CanAdd() const
{
if ( IsError() )
return 1; // avoid cascading the error report
return true; // avoid cascading the error report
// "add" only allowed if our type is "set".
return op1->Type()->IsSet();
}
int IndexExpr::CanDel() const
bool IndexExpr::CanDel() const
{
if ( IsError() )
return 1; // avoid cascading the error report
return true; // avoid cascading the error report
return op1->Type()->Tag() == TYPE_TABLE;
}
@ -2993,7 +2986,7 @@ Expr* FieldExpr::MakeLvalue()
return new RefExpr(this);
}
int FieldExpr::CanDel() const
bool FieldExpr::CanDel() const
{
return td->FindAttr(ATTR_DEFAULT) || td->FindAttr(ATTR_OPTIONAL);
}
@ -3212,7 +3205,7 @@ TableConstructorExpr::TableConstructorExpr(ListExpr* constructor_list,
}
else
{
if ( constructor_list->Exprs().length() == 0 )
if ( constructor_list->Exprs().empty() )
SetType(new TableType(new TypeList(base_type(TYPE_ANY)), 0));
else
{
@ -3324,7 +3317,7 @@ SetConstructorExpr::SetConstructorExpr(ListExpr* constructor_list,
}
else
{
if ( constructor_list->Exprs().length() == 0 )
if ( constructor_list->Exprs().empty() )
SetType(new ::SetType(new TypeList(base_type(TYPE_ANY)), 0));
else
SetType(init_type(constructor_list));
@ -3441,7 +3434,7 @@ VectorConstructorExpr::VectorConstructorExpr(ListExpr* constructor_list,
}
else
{
if ( constructor_list->Exprs().length() == 0 )
if ( constructor_list->Exprs().empty() )
{
// vector().
// By default, assign VOID type here. A vector with
@ -3553,7 +3546,7 @@ void FieldAssignExpr::EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f)
}
}
int FieldAssignExpr::IsRecordElement(TypeDecl* td) const
bool FieldAssignExpr::IsRecordElement(TypeDecl* td) const
{
if ( td )
{
@ -3561,7 +3554,7 @@ int FieldAssignExpr::IsRecordElement(TypeDecl* td) const
td->id = copy_string(field_name.c_str());
}
return 1;
return true;
}
void FieldAssignExpr::ExprDescribe(ODesc* d) const
@ -4028,9 +4021,9 @@ ScheduleExpr::~ScheduleExpr()
Unref(event);
}
int ScheduleExpr::IsPure() const
bool ScheduleExpr::IsPure() const
{
return 0;
return false;
}
Val* ScheduleExpr::Eval(Frame* f) const
@ -4304,7 +4297,7 @@ CallExpr::~CallExpr()
Unref(args);
}
int CallExpr::IsPure() const
bool CallExpr::IsPure() const
{
if ( IsError() )
return 1;
@ -4614,22 +4607,22 @@ void ListExpr::Append(Expr* e)
((TypeList*) type)->Append(e->Type()->Ref());
}
int ListExpr::IsPure() const
bool ListExpr::IsPure() const
{
for ( const auto& expr : exprs )
if ( ! expr->IsPure() )
return 0;
return false;
return 1;
return true;
}
int ListExpr::AllConst() const
bool ListExpr::AllConst() const
{
for ( const auto& expr : exprs )
if ( ! expr->IsConst() )
return 0;
return false;
return 1;
return true;
}
Val* ListExpr::Eval(Frame* f) const
@ -4654,7 +4647,7 @@ Val* ListExpr::Eval(Frame* f) const
BroType* ListExpr::InitType() const
{
if ( exprs.length() == 0 )
if ( exprs.empty() )
{
Error("empty list in untyped initialization");
return 0;
@ -5321,7 +5314,7 @@ val_list* eval_list(Frame* f, const ListExpr* l)
return v;
}
int expr_greater(const Expr* e1, const Expr* e2)
bool expr_greater(const Expr* e1, const Expr* e2)
{
return int(e1->Tag()) > int(e2->Tag());
return e1->Tag() > e2->Tag();
}