verilog: Squash a memory leak.
authorMarcelina Kościelnicka <mwk@0x04.net>
Mon, 14 Jun 2021 14:28:10 +0000 (16:28 +0200)
committerMarcelina Kościelnicka <mwk@0x04.net>
Mon, 14 Jun 2021 15:07:41 +0000 (17:07 +0200)
That was added in ecc22f7fedfa639482dbc55a05709da85116a60f

frontends/verilog/verilog_frontend.cc
frontends/verilog/verilog_frontend.h
frontends/verilog/verilog_lexer.l
frontends/verilog/verilog_parser.y

index ad0bb9ff7214066cc30e966bfb7196e5da2ae461..9b277c6b977d28ab3cc6fe9ac8f1815643efd834 100644 (file)
@@ -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;
index 30f1c11802aa9003471a06d2c03547c7ad899bb6..8454e7999e7d7b54c81c863d9e170e21cf4aead4 100644 (file)
@@ -47,7 +47,7 @@ namespace VERILOG_FRONTEND
 
        // names of locally typedef'ed types in a stack
        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
        extern dict<std::string, AST::AstNode*> pkg_user_types;
index b29e625d648c6813e0d56abf7d5a46f5793fe8c0..55e8b48b9a96799c9ecbf6eabdd56c8bc70de4f3 100644 (file)
@@ -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;
                }
        }
index 10d904dbde43e5da9d0341089114e1861134a6ff..3f4bf5bfd9387330f462aabe6848a42cd239f2ae 100644 (file)
@@ -54,7 +54,7 @@ namespace VERILOG_FRONTEND {
        dict<IdString, AstNode*> *attr_list, default_attr_list;
        std::stack<dict<IdString, AstNode*> *> attr_list_stack;
        dict<IdString, AstNode*> *albuf;
-       std::vector<UserTypeMap*> user_type_stack;
+       std::vector<UserTypeMap> user_type_stack;
        dict<std::string, AstNode*> pkg_user_types;
        std::vector<AstNode*> 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];
                }