diff --git a/NEWS b/NEWS index a8261084ce..370fbafac4 100644 --- a/NEWS +++ b/NEWS @@ -105,9 +105,6 @@ Changed Functionality - ``AsVector()`` has changed to return ``std::vector>*``. -- ``Attributes::Attrs()`` now returns ``const std::vector>&`` - instead of ``attr_list*`` - - Moved a large number of classes from the global namespace into either the ``zeek`` or ``zeek::detail`` namespace. See https://github.com/zeek/zeek/issues/266 for the rationale behind these changes. Most types that were moved and functions diff --git a/src/Attr.cc b/src/Attr.cc index bd722e68c2..a942d174d6 100644 --- a/src/Attr.cc +++ b/src/Attr.cc @@ -48,8 +48,6 @@ Attr::Attr(::attr_tag t) : Attr(static_cast(t)) } #pragma GCC diagnostic pop -Attr::~Attr() = default; - void Attr::SetAttrExpr(IntrusivePtr e) { expr = std::move(e); } @@ -192,10 +190,17 @@ Attributes::Attributes(std::vector> a, AddAttr(std::move(attr)); } +Attributes::~Attributes() + { + attrs_list.clear(); + attrs.clear(); + } + void Attributes::AddAttr(IntrusivePtr attr) { // We overwrite old attributes by deleting them first. RemoveAttr(attr->Tag()); + attrs_list.push_back(attr.get()); attrs.emplace_back(attr); // We only check the attribute after we've added it, to facilitate @@ -206,23 +211,31 @@ void Attributes::AddAttr(IntrusivePtr attr) // those attributes only have meaning for a redefinable value. if ( (attr->Tag() == ATTR_ADD_FUNC || attr->Tag() == ATTR_DEL_FUNC) && ! Find(ATTR_REDEF) ) - attrs.emplace_back(make_intrusive(ATTR_REDEF)); + { + auto a = make_intrusive(ATTR_REDEF); + attrs_list.push_back(a.get()); + attrs.emplace_back(a); + } // For DEFAULT, add an implicit OPTIONAL if it's not a global. if ( ! global_var && attr->Tag() == ATTR_DEFAULT && ! Find(ATTR_OPTIONAL) ) - attrs.emplace_back(make_intrusive(ATTR_OPTIONAL)); + { + auto a = make_intrusive(ATTR_OPTIONAL); + attrs_list.push_back(a.get()); + attrs.emplace_back(a); + } } void Attributes::AddAttrs(const IntrusivePtr& a) { - for ( const auto& attr : a->Attrs() ) + for ( const auto& attr : a->GetAttrs() ) AddAttr(attr); } void Attributes::AddAttrs(Attributes* a) { - for ( const auto& attr : a->Attrs() ) + for ( const auto& attr : a->GetAttrs() ) AddAttr(attr); Unref(a); @@ -248,6 +261,10 @@ const IntrusivePtr& Attributes::Find(attr_tag t) const void Attributes::RemoveAttr(attr_tag t) { + for ( int i = 0; i < attrs_list.length(); i++ ) + if ( attrs_list[i]->Tag() == t ) + attrs_list.remove_nth(i--); + for ( auto it = attrs.begin(); it != attrs.end(); ) { if ( (*it)->Tag() == t ) diff --git a/src/Attr.h b/src/Attr.h index 39c333447a..ff858cc618 100644 --- a/src/Attr.h +++ b/src/Attr.h @@ -76,7 +76,7 @@ public: explicit Attr(::attr_tag t); #pragma GCC diagnostic pop - ~Attr() override; + ~Attr() override = default; attr_tag Tag() const { return tag; } @@ -122,6 +122,8 @@ public: bool in_record, bool is_global); Attributes(IntrusivePtr t, bool in_record, bool is_global); + ~Attributes() override; + void AddAttr(IntrusivePtr a); void AddAttrs(const IntrusivePtr& a); @@ -147,7 +149,11 @@ public: void Describe(ODesc* d) const override; void DescribeReST(ODesc* d, bool shorten = false) const; - const std::vector>& Attrs() const + [[deprecated("Remove in v4.1. Use GetAttrs().")]] + const attr_list* Attrs() const + { return &attrs_list; } + + const std::vector>& GetAttrs() const { return attrs; } bool operator==(const Attributes& other) const; @@ -157,6 +163,9 @@ protected: IntrusivePtr type; std::vector> attrs; + + // Remove in v4.1. This is used by Attrs(), which is deprecated. + attr_list attrs_list; bool in_record; bool global_var; }; diff --git a/src/Expr.cc b/src/Expr.cc index 4cbbc4520b..4d92224d64 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -2076,7 +2076,7 @@ bool AssignExpr::TypeCheck(const IntrusivePtr& attrs) std::unique_ptr>> attr_copy; if ( attrs ) - attr_copy = std::make_unique>>(attrs->Attrs()); + attr_copy = std::make_unique>>(attrs->GetAttrs()); bool empty_list_assignment = (op2->AsListExpr()->Exprs().empty()); @@ -2172,7 +2172,7 @@ bool AssignExpr::TypeCheck(const IntrusivePtr& attrs) if ( sce->GetAttrs() ) { - const auto& a = sce->GetAttrs()->Attrs(); + const auto& a = sce->GetAttrs()->GetAttrs(); attr_copy = std::make_unique>>(a); }