From ebce56c29c2aebc933d0ca2b87abc4bddc42bf83 Mon Sep 17 00:00:00 2001 From: zawwz Date: Fri, 11 Dec 2020 12:28:09 +0100 Subject: [PATCH] fix bug on minimizing collisions on 'unset' with bash --- include/processing.hpp | 1 + include/struc.hpp | 5 ++++- include/util.hpp | 9 ++++++++- include/version.h | 2 +- src/minimize.cpp | 4 +++- src/processing.cpp | 25 ++++++++++++++++++++++++- src/util.cpp | 8 -------- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/include/processing.hpp b/include/processing.hpp index dc51260..759a253 100644 --- a/include/processing.hpp +++ b/include/processing.hpp @@ -61,6 +61,7 @@ void list_fcts(_obj* in, std::regex const& exclude); void list_cmds(_obj* in, std::regex const& exclude); // recursives +bool r_get_unsets(_obj* in, set_t* unsets); bool r_get_var(_obj* in, countmap_t* defmap, countmap_t* callmap); bool r_get_cmd(_obj* in, countmap_t* all_cmds); bool r_get_fct(_obj* in, countmap_t* fct_map); diff --git a/include/struc.hpp b/include/struc.hpp index 7447686..169af79 100644 --- a/include/struc.hpp +++ b/include/struc.hpp @@ -262,10 +262,13 @@ public: void add_arg(arg* in); + // preceding var assigns std::vector> var_assigns; - // get var assigns in special cmds (export, unset, read) + // check if cmd is this (ex: unset) + bool is(std::string const& in); + // for var assigns in special cmds (export, unset, read, local) bool is_argvar(); std::vector subarg_vars(); diff --git a/include/util.hpp b/include/util.hpp index 0877205..ec570e3 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -111,7 +111,14 @@ std::set map_to_set(std::map in) return ret; } -void concat_sets(std::set& a, std::set const& b); +template +void concat_sets(std::set& a, std::set const& b) +{ + for(auto it: b) + { + a.insert( it ); + } +} std::set prune_matching(std::set& in, std::regex re); diff --git a/include/version.h b/include/version.h index 8a318b8..dd49b59 100644 --- a/include/version.h +++ b/include/version.h @@ -1,6 +1,6 @@ #ifndef VERSION_H #define VERSION_H -#define VERSION_STRING "v0.2" +#define VERSION_STRING "v0.2.1" #endif //VERSION_H diff --git a/src/minimize.cpp b/src/minimize.cpp index e16015e..9ec348c 100644 --- a/src/minimize.cpp +++ b/src/minimize.cpp @@ -179,14 +179,16 @@ void minimize_var(_obj* in, std::regex const& exclude) void minimize_fct(_obj* in, std::regex const& exclude) { // countmap_t fcts, cmdmap; - set_t allcmds, excluded; + set_t allcmds, excluded, unsets; strmap_t fctmap; // get fcts and cmds fctmap_get(in, exclude); cmdmap_get(in, regex_null); + recurse(r_get_unsets, in, &unsets); // concatenate cmds and excluded commands allcmds=map_to_set(m_cmds); concat_sets(allcmds, m_excluded_fct); + concat_sets(allcmds, unsets); // create mapping fctmap=gen_minimal_map(m_fcts, allcmds); // perform replace diff --git a/src/processing.cpp b/src/processing.cpp index a6a12a8..97f1ac0 100644 --- a/src/processing.cpp +++ b/src/processing.cpp @@ -161,6 +161,11 @@ bool cmd::is_argvar() return cmd_is_argvar(this->firstarg_string()); } +bool cmd::is(std::string const& in) +{ + return in == this->firstarg_string(); +} + /** GETTERS **/ void varmap_get(_obj* in, std::regex const& exclude) @@ -196,7 +201,6 @@ void cmdmap_get(_obj* in, std::regex const& exclude) /** OUTPUT **/ - void list_vars(_obj* in, std::regex const& exclude) { varmap_get(in, exclude); @@ -286,6 +290,25 @@ bool r_get_var(_obj* in, countmap_t* defmap, countmap_t* callmap) return true; } +bool r_get_unsets(_obj* in, set_t* unsets) +{ + switch(in->type) + { + case _obj::block_cmd: { + cmd* t = dynamic_cast(in); + if(t->is("unset")) + { + for(uint32_t i=1; iargs->size(); i++) + { + unsets->insert(t->args->args[i]->string()); + } + } + }; break; + default: break; + } + return true; +} + bool r_get_cmd(_obj* in, countmap_t* all_cmds) { switch(in->type) diff --git a/src/util.cpp b/src/util.cpp index e109be7..eedc38d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -114,14 +114,6 @@ std::string concatargs(std::vector const& args) return ret; } -void concat_sets(std::set& a, std::set const& b) -{ - for(auto it: b) - { - a.insert( it ); - } -} - std::set prune_matching(std::set& in, std::regex re) { std::set ret;