From 801ecc0e1d75f092981361632265edce67130a73 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marcelina=20Ko=C5=9Bcielnicka?= Date: Mon, 14 Jun 2021 16:28:10 +0200 Subject: [PATCH] verilog: Squash a memory leak. That was added in ecc22f7fedfa639482dbc55a05709da85116a60f --- frontends/verilog/verilog_frontend.cc | 12 ++++-------- frontends/verilog/verilog_frontend.h | 2 +- frontends/verilog/verilog_lexer.l | 2 +- frontends/verilog/verilog_parser.y | 17 ++++++++--------- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/frontends/verilog/verilog_frontend.cc b/frontends/verilog/verilog_frontend.cc index ad0bb9ff7..9b277c6b9 100644 --- a/frontends/verilog/verilog_frontend.cc +++ b/frontends/verilog/verilog_frontend.cc @@ -482,18 +482,18 @@ struct VerilogFrontend : public Frontend { // make package typedefs available to parser add_package_types(pkg_user_types, design->verilog_packages); - UserTypeMap *global_types_map = new UserTypeMap(); + UserTypeMap global_types_map; for (auto def : design->verilog_globals) { if (def->type == AST::AST_TYPEDEF) { - (*global_types_map)[def->str] = def; + global_types_map[def->str] = def; } } log_assert(user_type_stack.empty()); // use previous global typedefs as bottom level of user type stack - user_type_stack.push_back(global_types_map); + user_type_stack.push_back(std::move(global_types_map)); // add a new empty type map to allow overriding existing global definitions - user_type_stack.push_back(new UserTypeMap()); + user_type_stack.push_back(UserTypeMap()); frontend_verilog_yyset_lineno(1); frontend_verilog_yyrestart(NULL); @@ -519,10 +519,6 @@ struct VerilogFrontend : public Frontend { // only the previous and new global type maps remain log_assert(user_type_stack.size() == 2); - for (auto it : user_type_stack) { - // the global typedefs have to remain valid for future invocations, so just drop the map without deleting values - delete it; - } user_type_stack.clear(); delete current_ast; diff --git a/frontends/verilog/verilog_frontend.h b/frontends/verilog/verilog_frontend.h index 30f1c1180..8454e7999 100644 --- a/frontends/verilog/verilog_frontend.h +++ b/frontends/verilog/verilog_frontend.h @@ -47,7 +47,7 @@ namespace VERILOG_FRONTEND // names of locally typedef'ed types in a stack typedef std::map UserTypeMap; - extern std::vector user_type_stack; + extern std::vector user_type_stack; // names of package typedef'ed types extern dict pkg_user_types; diff --git a/frontends/verilog/verilog_lexer.l b/frontends/verilog/verilog_lexer.l index b29e625d6..55e8b48b9 100644 --- a/frontends/verilog/verilog_lexer.l +++ b/frontends/verilog/verilog_lexer.l @@ -103,7 +103,7 @@ static bool isUserType(std::string &s) { // check current scope then outer scopes for a name for (auto it = user_type_stack.rbegin(); it != user_type_stack.rend(); ++it) { - if ((*it)->count(s) > 0) { + if (it->count(s) > 0) { return true; } } diff --git a/frontends/verilog/verilog_parser.y b/frontends/verilog/verilog_parser.y index 10d904dbd..3f4bf5bfd 100644 --- a/frontends/verilog/verilog_parser.y +++ b/frontends/verilog/verilog_parser.y @@ -54,7 +54,7 @@ namespace VERILOG_FRONTEND { dict *attr_list, default_attr_list; std::stack *> attr_list_stack; dict *albuf; - std::vector user_type_stack; + std::vector user_type_stack; dict pkg_user_types; std::vector ast_stack; struct AstNode *astbuf1, *astbuf2, *astbuf3; @@ -132,8 +132,8 @@ static void addTypedefNode(std::string *name, AstNode *node) log_assert(node); auto *tnode = new AstNode(AST_TYPEDEF, node); tnode->str = *name; - auto user_types = user_type_stack.back(); - (*user_types)[*name] = tnode; + auto &user_types = user_type_stack.back(); + user_types[*name] = tnode; if (current_ast_mod && current_ast_mod->type == AST_PACKAGE) { // typedef inside a package so we need the qualified name auto qname = current_ast_mod->str + "::" + (*name).substr(1); @@ -145,8 +145,7 @@ static void addTypedefNode(std::string *name, AstNode *node) static void enterTypeScope() { - auto user_types = new UserTypeMap(); - user_type_stack.push_back(user_types); + user_type_stack.push_back(UserTypeMap()); } static void exitTypeScope() @@ -157,17 +156,17 @@ static void exitTypeScope() static bool isInLocalScope(const std::string *name) { // tests if a name was declared in the current block scope - auto user_types = user_type_stack.back(); - return (user_types->count(*name) > 0); + auto &user_types = user_type_stack.back(); + return (user_types.count(*name) > 0); } static AstNode *getTypeDefinitionNode(std::string type_name) { // check current scope then outer scopes for a name for (auto it = user_type_stack.rbegin(); it != user_type_stack.rend(); ++it) { - if ((*it)->count(type_name) > 0) { + if (it->count(type_name) > 0) { // return the definition nodes from the typedef statement - auto typedef_node = (**it)[type_name]; + auto typedef_node = (*it)[type_name]; log_assert(typedef_node->type == AST_TYPEDEF); return typedef_node->children[0]; } -- 2.30.2