verilog: Squash a memory leak.

That was added in ecc22f7fed
This commit is contained in:
Marcelina Kościelnicka 2021-06-14 16:28:10 +02:00
parent 438bcc68c0
commit 801ecc0e1d
4 changed files with 14 additions and 19 deletions

View File

@ -482,18 +482,18 @@ struct VerilogFrontend : public Frontend {
// make package typedefs available to parser // make package typedefs available to parser
add_package_types(pkg_user_types, design->verilog_packages); 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) { for (auto def : design->verilog_globals) {
if (def->type == AST::AST_TYPEDEF) { if (def->type == AST::AST_TYPEDEF) {
(*global_types_map)[def->str] = def; global_types_map[def->str] = def;
} }
} }
log_assert(user_type_stack.empty()); log_assert(user_type_stack.empty());
// use previous global typedefs as bottom level of user type stack // 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 // 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_yyset_lineno(1);
frontend_verilog_yyrestart(NULL); frontend_verilog_yyrestart(NULL);
@ -519,10 +519,6 @@ struct VerilogFrontend : public Frontend {
// only the previous and new global type maps remain // only the previous and new global type maps remain
log_assert(user_type_stack.size() == 2); 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(); user_type_stack.clear();
delete current_ast; delete current_ast;

View File

@ -47,7 +47,7 @@ namespace VERILOG_FRONTEND
// names of locally typedef'ed types in a stack // names of locally typedef'ed types in a stack
typedef std::map<std::string, AST::AstNode*> UserTypeMap; typedef std::map<std::string, AST::AstNode*> UserTypeMap;
extern std::vector<UserTypeMap *> user_type_stack; extern std::vector<UserTypeMap> user_type_stack;
// names of package typedef'ed types // names of package typedef'ed types
extern dict<std::string, AST::AstNode*> pkg_user_types; extern dict<std::string, AST::AstNode*> pkg_user_types;

View File

@ -103,7 +103,7 @@ static bool isUserType(std::string &s)
{ {
// check current scope then outer scopes for a name // check current scope then outer scopes for a name
for (auto it = user_type_stack.rbegin(); it != user_type_stack.rend(); ++it) { 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; return true;
} }
} }

View File

@ -54,7 +54,7 @@ namespace VERILOG_FRONTEND {
dict<IdString, AstNode*> *attr_list, default_attr_list; dict<IdString, AstNode*> *attr_list, default_attr_list;
std::stack<dict<IdString, AstNode*> *> attr_list_stack; std::stack<dict<IdString, AstNode*> *> attr_list_stack;
dict<IdString, AstNode*> *albuf; dict<IdString, AstNode*> *albuf;
std::vector<UserTypeMap*> user_type_stack; std::vector<UserTypeMap> user_type_stack;
dict<std::string, AstNode*> pkg_user_types; dict<std::string, AstNode*> pkg_user_types;
std::vector<AstNode*> ast_stack; std::vector<AstNode*> ast_stack;
struct AstNode *astbuf1, *astbuf2, *astbuf3; struct AstNode *astbuf1, *astbuf2, *astbuf3;
@ -132,8 +132,8 @@ static void addTypedefNode(std::string *name, AstNode *node)
log_assert(node); log_assert(node);
auto *tnode = new AstNode(AST_TYPEDEF, node); auto *tnode = new AstNode(AST_TYPEDEF, node);
tnode->str = *name; tnode->str = *name;
auto user_types = user_type_stack.back(); auto &user_types = user_type_stack.back();
(*user_types)[*name] = tnode; user_types[*name] = tnode;
if (current_ast_mod && current_ast_mod->type == AST_PACKAGE) { if (current_ast_mod && current_ast_mod->type == AST_PACKAGE) {
// typedef inside a package so we need the qualified name // typedef inside a package so we need the qualified name
auto qname = current_ast_mod->str + "::" + (*name).substr(1); auto qname = current_ast_mod->str + "::" + (*name).substr(1);
@ -145,8 +145,7 @@ static void addTypedefNode(std::string *name, AstNode *node)
static void enterTypeScope() static void enterTypeScope()
{ {
auto user_types = new UserTypeMap(); user_type_stack.push_back(UserTypeMap());
user_type_stack.push_back(user_types);
} }
static void exitTypeScope() static void exitTypeScope()
@ -157,17 +156,17 @@ static void exitTypeScope()
static bool isInLocalScope(const std::string *name) static bool isInLocalScope(const std::string *name)
{ {
// tests if a name was declared in the current block scope // tests if a name was declared in the current block scope
auto user_types = user_type_stack.back(); auto &user_types = user_type_stack.back();
return (user_types->count(*name) > 0); return (user_types.count(*name) > 0);
} }
static AstNode *getTypeDefinitionNode(std::string type_name) static AstNode *getTypeDefinitionNode(std::string type_name)
{ {
// check current scope then outer scopes for a name // check current scope then outer scopes for a name
for (auto it = user_type_stack.rbegin(); it != user_type_stack.rend(); ++it) { 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 // 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); log_assert(typedef_node->type == AST_TYPEDEF);
return typedef_node->children[0]; return typedef_node->children[0];
} }