Deprecate TypeDecl::FindAttr(), replace with GetAttr()

This commit is contained in:
Jon Siwek 2020-05-26 15:41:25 -07:00
parent 5fc78a548c
commit e365105872
6 changed files with 26 additions and 23 deletions

View file

@ -2871,7 +2871,7 @@ IntrusivePtr<Expr> FieldExpr::MakeLvalue()
bool FieldExpr::CanDel() const bool FieldExpr::CanDel() const
{ {
return td->FindAttr(ATTR_DEFAULT) || td->FindAttr(ATTR_OPTIONAL); return td->GetAttr(ATTR_DEFAULT) || td->GetAttr(ATTR_OPTIONAL);
} }
void FieldExpr::Assign(Frame* f, IntrusivePtr<Val> v) void FieldExpr::Assign(Frame* f, IntrusivePtr<Val> v)
@ -2897,7 +2897,7 @@ IntrusivePtr<Val> FieldExpr::Fold(Val* v) const
return result; return result;
// Check for &default. // Check for &default.
const Attr* def_attr = td ? td->FindAttr(ATTR_DEFAULT) : nullptr; const Attr* def_attr = td ? td->GetAttr(ATTR_DEFAULT).get() : nullptr;
if ( def_attr ) if ( def_attr )
return def_attr->GetExpr()->Eval(nullptr); return def_attr->GetExpr()->Eval(nullptr);
@ -3625,7 +3625,7 @@ RecordCoerceExpr::RecordCoerceExpr(IntrusivePtr<Expr> arg_op,
{ {
if ( map[i] == -1 ) if ( map[i] == -1 )
{ {
if ( ! t_r->FieldDecl(i)->FindAttr(ATTR_OPTIONAL) ) if ( ! t_r->FieldDecl(i)->GetAttr(ATTR_OPTIONAL) )
{ {
std::string error_msg = fmt( std::string error_msg = fmt(
"non-optional field \"%s\" missing", t_r->FieldName(i)); "non-optional field \"%s\" missing", t_r->FieldName(i));
@ -3677,14 +3677,14 @@ IntrusivePtr<Val> RecordCoerceExpr::Fold(Val* v) const
if ( ! rhs ) if ( ! rhs )
{ {
const Attr* def = rv->GetType()->AsRecordType()->FieldDecl( const auto& def = rv->GetType()->AsRecordType()->FieldDecl(
map[i])->FindAttr(ATTR_DEFAULT); map[i])->GetAttr(ATTR_DEFAULT);
if ( def ) if ( def )
rhs = def->GetExpr()->Eval(nullptr); rhs = def->GetExpr()->Eval(nullptr);
} }
assert(rhs || GetType()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_OPTIONAL)); assert(rhs || GetType()->AsRecordType()->FieldDecl(i)->GetAttr(ATTR_OPTIONAL));
if ( ! rhs ) if ( ! rhs )
{ {
@ -3716,7 +3716,7 @@ IntrusivePtr<Val> RecordCoerceExpr::Fold(Val* v) const
} }
else else
{ {
if ( const Attr* def = GetType()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_DEFAULT) ) if ( const auto& def = GetType()->AsRecordType()->FieldDecl(i)->GetAttr(ATTR_DEFAULT) )
{ {
auto def_val = def->GetExpr()->Eval(nullptr); auto def_val = def->GetExpr()->Eval(nullptr);
const auto& def_type = def_val->GetType(); const auto& def_type = def_val->GetType();

View file

@ -816,7 +816,7 @@ IntrusivePtr<TableVal> RecordType::GetRecordFieldsVal(const RecordVal* rv) const
if ( rv ) if ( rv )
fv = rv->GetField(i); fv = rv->GetField(i);
bool logged = (fd->attrs && fd->FindAttr(ATTR_LOG) != nullptr); bool logged = (fd->attrs && fd->GetAttr(ATTR_LOG) != nullptr);
auto nr = make_intrusive<RecordVal>(record_field); auto nr = make_intrusive<RecordVal>(record_field);
@ -849,8 +849,7 @@ const char* RecordType::AddFields(type_decl_list* others, attr_list* attr)
for ( const auto& td : *others ) for ( const auto& td : *others )
{ {
if ( ! td->FindAttr(ATTR_DEFAULT) && if ( ! td->GetAttr(ATTR_DEFAULT) && ! td->GetAttr(ATTR_OPTIONAL) )
! td->FindAttr(ATTR_OPTIONAL) )
{ {
delete others; delete others;
return "extension field must be &optional or have &default"; return "extension field must be &optional or have &default";
@ -1016,7 +1015,7 @@ string RecordType::GetFieldDeprecationWarning(int field, bool has_check) const
if ( decl) if ( decl)
{ {
string result; string result;
if ( const Attr* deprecation = decl->FindAttr(ATTR_DEPRECATED) ) if ( const auto& deprecation = decl->GetAttr(ATTR_DEPRECATED) )
{ {
auto expr = static_cast<ConstExpr*>(deprecation->GetExpr().get()); auto expr = static_cast<ConstExpr*>(deprecation->GetExpr().get());
if ( expr ) if ( expr )

View file

@ -566,9 +566,13 @@ public:
TypeDecl(const TypeDecl& other); TypeDecl(const TypeDecl& other);
~TypeDecl(); ~TypeDecl();
[[deprecated("Remove in v4.1. Use GetAttr().")]]
const Attr* FindAttr(attr_tag a) const const Attr* FindAttr(attr_tag a) const
{ return attrs ? attrs->Find(a).get() : nullptr; } { return attrs ? attrs->Find(a).get() : nullptr; }
const IntrusivePtr<Attr>& GetAttr(attr_tag a) const
{ return attrs ? attrs->Find(a) : Attr::nil; }
void DescribeReST(ODesc* d, bool roles_only = false) const; void DescribeReST(ODesc* d, bool roles_only = false) const;
IntrusivePtr<BroType> type; IntrusivePtr<BroType> type;
@ -664,13 +668,13 @@ public:
bool IsFieldDeprecated(int field) const bool IsFieldDeprecated(int field) const
{ {
const TypeDecl* decl = FieldDecl(field); const TypeDecl* decl = FieldDecl(field);
return decl && decl->FindAttr(ATTR_DEPRECATED) != nullptr; return decl && decl->GetAttr(ATTR_DEPRECATED) != nullptr;
} }
bool FieldHasAttr(int field, attr_tag at) const bool FieldHasAttr(int field, attr_tag at) const
{ {
const TypeDecl* decl = FieldDecl(field); const TypeDecl* decl = FieldDecl(field);
return decl && decl->FindAttr(at) != nullptr; return decl && decl->GetAttr(at) != nullptr;
} }
std::string GetFieldDeprecationWarning(int field, bool has_check) const; std::string GetFieldDeprecationWarning(int field, bool has_check) const;

View file

@ -2881,7 +2881,7 @@ IntrusivePtr<RecordVal> RecordVal::CoerceTo(IntrusivePtr<RecordType> t,
for ( i = 0; i < ar_t->NumFields(); ++i ) for ( i = 0; i < ar_t->NumFields(); ++i )
if ( ! aggr->GetField(i) && if ( ! aggr->GetField(i) &&
! ar_t->FieldDecl(i)->FindAttr(ATTR_OPTIONAL) ) ! ar_t->FieldDecl(i)->GetAttr(ATTR_OPTIONAL) )
{ {
char buf[512]; char buf[512];
snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),

View file

@ -896,7 +896,7 @@ bool Manager::UnrollRecordType(vector<Field*> *fields, const RecordType *rec,
if ( ( rec->GetFieldType(i)->Tag() == TYPE_FILE || if ( ( rec->GetFieldType(i)->Tag() == TYPE_FILE ||
rec->GetFieldType(i)->Tag() == TYPE_FUNC || rec->GetFieldType(i)->Tag() == TYPE_FUNC ||
rec->GetFieldType(i)->Tag() == TYPE_OPAQUE ) && rec->GetFieldType(i)->Tag() == TYPE_OPAQUE ) &&
rec->FieldDecl(i)->FindAttr(ATTR_OPTIONAL) ) rec->FieldDecl(i)->GetAttr(ATTR_OPTIONAL) )
{ {
reporter->Info("Encountered incompatible type \"%s\" in type definition for field \"%s\" in ReaderFrontend. Ignoring optional field.", type_name(rec->GetFieldType(i)->Tag()), name.c_str()); reporter->Info("Encountered incompatible type \"%s\" in type definition for field \"%s\" in ReaderFrontend. Ignoring optional field.", type_name(rec->GetFieldType(i)->Tag()), name.c_str());
continue; continue;
@ -911,7 +911,7 @@ bool Manager::UnrollRecordType(vector<Field*> *fields, const RecordType *rec,
{ {
string prep = nameprepend + rec->FieldName(i) + "."; string prep = nameprepend + rec->FieldName(i) + ".";
if ( rec->FieldDecl(i)->FindAttr(ATTR_OPTIONAL) ) if ( rec->FieldDecl(i)->GetAttr(ATTR_OPTIONAL) )
{ {
reporter->Info("The input framework does not support optional record fields: \"%s\"", rec->FieldName(i)); reporter->Info("The input framework does not support optional record fields: \"%s\"", rec->FieldName(i));
return false; return false;
@ -940,11 +940,11 @@ bool Manager::UnrollRecordType(vector<Field*> *fields, const RecordType *rec,
st = rec->GetFieldType(i)->AsVectorType()->Yield()->Tag(); st = rec->GetFieldType(i)->AsVectorType()->Yield()->Tag();
else if ( ty == TYPE_PORT && else if ( ty == TYPE_PORT &&
rec->FieldDecl(i)->FindAttr(ATTR_TYPE_COLUMN) ) rec->FieldDecl(i)->GetAttr(ATTR_TYPE_COLUMN) )
{ {
// we have an annotation for the second column // we have an annotation for the second column
c = rec->FieldDecl(i)->FindAttr(ATTR_TYPE_COLUMN)->GetExpr()->Eval(nullptr); c = rec->FieldDecl(i)->GetAttr(ATTR_TYPE_COLUMN)->GetExpr()->Eval(nullptr);
assert(c); assert(c);
assert(c->GetType()->Tag() == TYPE_STRING); assert(c->GetType()->Tag() == TYPE_STRING);
@ -952,7 +952,7 @@ bool Manager::UnrollRecordType(vector<Field*> *fields, const RecordType *rec,
secondary = c->AsStringVal()->AsString()->CheckString(); secondary = c->AsStringVal()->AsString()->CheckString();
} }
if ( rec->FieldDecl(i)->FindAttr(ATTR_OPTIONAL ) ) if ( rec->FieldDecl(i)->GetAttr(ATTR_OPTIONAL ) )
optional = true; optional = true;
Field* field = new Field(name.c_str(), secondary, ty, st, optional); Field* field = new Field(name.c_str(), secondary, ty, st, optional);
@ -1866,7 +1866,7 @@ RecordVal* Manager::ValueToRecordVal(const Stream* stream, const Value* const *v
// Hence -> assign null to the field, done. // Hence -> assign null to the field, done.
// Better check that it really is optional. Uou never know. // Better check that it really is optional. Uou never know.
assert(request_type->FieldDecl(i)->FindAttr(ATTR_OPTIONAL)); assert(request_type->FieldDecl(i)->GetAttr(ATTR_OPTIONAL));
} }
else else
{ {

View file

@ -242,7 +242,7 @@ bool Manager::CreateStream(EnumVal* id, RecordVal* sval)
for ( int i = 0; i < columns->NumFields(); i++ ) for ( int i = 0; i < columns->NumFields(); i++ )
{ {
if ( ! (columns->FieldDecl(i)->FindAttr(ATTR_LOG)) ) if ( ! (columns->FieldDecl(i)->GetAttr(ATTR_LOG)) )
continue; continue;
if ( ! threading::Value::IsCompatibleType(columns->GetFieldType(i).get()) ) if ( ! threading::Value::IsCompatibleType(columns->GetFieldType(i).get()) )
@ -410,7 +410,7 @@ bool Manager::TraverseRecord(Stream* stream, Filter* filter, RecordType* rt,
const auto& t = rtype->GetFieldType(i); const auto& t = rtype->GetFieldType(i);
// Ignore if &log not specified. // Ignore if &log not specified.
if ( ! rtype->FieldDecl(i)->FindAttr(ATTR_LOG) ) if ( ! rtype->FieldDecl(i)->GetAttr(ATTR_LOG) )
continue; continue;
list<int> new_indices = indices; list<int> new_indices = indices;
@ -516,7 +516,7 @@ bool Manager::TraverseRecord(Stream* stream, Filter* filter, RecordType* rt,
else if ( t->Tag() == TYPE_VECTOR ) else if ( t->Tag() == TYPE_VECTOR )
st = t->AsVectorType()->Yield()->Tag(); st = t->AsVectorType()->Yield()->Tag();
bool optional = rtype->FieldDecl(i)->FindAttr(ATTR_OPTIONAL); bool optional = (bool)rtype->FieldDecl(i)->GetAttr(ATTR_OPTIONAL);
filter->fields[filter->num_fields - 1] = new threading::Field(new_path.c_str(), nullptr, t->Tag(), st, optional); filter->fields[filter->num_fields - 1] = new threading::Field(new_path.c_str(), nullptr, t->Tag(), st, optional);
} }