From cf79c05e3ad87f6d27e8079b0d4ae6476df8d964 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Mon, 19 Apr 2021 16:03:29 -0700 Subject: [PATCH] tracking of expressions used to define/redef variables --- src/ID.cc | 5 +++++ src/ID.h | 11 +++++++++++ src/parse.y | 6 +++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/ID.cc b/src/ID.cc index 437049b942..620e35ef11 100644 --- a/src/ID.cc +++ b/src/ID.cc @@ -285,6 +285,11 @@ const AttrPtr& ID::GetAttr(AttrTag t) const return attrs ? attrs->Find(t) : Attr::nil; } +void ID::AddInitExpr(ExprPtr init_expr) + { + init_exprs.emplace_back(std::move(init_expr)); + } + bool ID::IsDeprecated() const { return GetAttr(ATTR_DEPRECATED) != nullptr; diff --git a/src/ID.h b/src/ID.h index 9997967475..0d6e8c4e34 100644 --- a/src/ID.h +++ b/src/ID.h @@ -112,6 +112,10 @@ public: const AttrPtr& GetAttr(AttrTag t) const; + void AddInitExpr(ExprPtr init_expr); + const std::vector& GetInitExprs() const + { return init_exprs; } + bool IsDeprecated() const; void MakeDeprecated(ExprPtr deprecation); @@ -156,6 +160,13 @@ protected: int offset; ValPtr val; AttributesPtr attrs; + + // Expressions used to initialize the identifier, for use by + // the scripts-to-C++ compiler. We need to track all of them + // because it's possible that a global value gets created using + // one of the earlier instances rather than the last one. + std::vector init_exprs; + // contains list of functions that are called when an option changes std::multimap option_handlers; diff --git a/src/parse.y b/src/parse.y index b7d3017d71..e63b33595b 100644 --- a/src/parse.y +++ b/src/parse.y @@ -244,6 +244,8 @@ static void build_global(ID* id, Type* t, InitClass ic, Expr* e, add_global(id_ptr, std::move(t_ptr), ic, e_ptr, std::move(attrs_ptr), dt); + id->AddInitExpr(e_ptr); + if ( dt == VAR_REDEF ) zeekygen_mgr->Redef(id, ::filename, ic, std::move(e_ptr)); else @@ -261,7 +263,9 @@ static StmtPtr build_local(ID* id, Type* t, InitClass ic, Expr* e, auto attrs_ptr = attrs ? std::make_unique>(*attrs) : nullptr; auto init = add_local(std::move(id_ptr), std::move(t_ptr), ic, - std::move(e_ptr), std::move(attrs_ptr), dt); + e_ptr, std::move(attrs_ptr), dt); + + id->AddInitExpr(std::move(e_ptr)); if ( do_coverage ) script_coverage_mgr.AddStmt(init.get());