From: Mikko Rasa Date: Sat, 10 Dec 2016 20:43:51 +0000 (+0200) Subject: Some fixes to assignment management in UnusedVariableLocator X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4fe225bf15048fcb7a678370f87d09f2de37031a;p=libs%2Fgl.git Some fixes to assignment management in UnusedVariableLocator Sometimes assignments were being removed when they shouldn't (reference from a conditional sub-block). Other times they weren't being removed when they should (followup assignment in the same block). --- diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 14c9d3d3..cc4b5efe 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -1229,8 +1229,7 @@ void ProgramCompiler::UnusedVariableLocator::apply(Stage &s) if(!i->second.referenced) { unused_nodes.insert(i->first); - for(vector::iterator j=i->second.assignments.begin(); j!=i->second.assignments.end(); ++j) - unused_nodes.insert(*j); + clear_assignments(i->second, true); } } variables.pop_back(); @@ -1282,11 +1281,21 @@ void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclarati { VariableInfo &var_info = variables.back()[&var]; if(!self_ref) - var_info.assignments.clear(); + clear_assignments(var_info, true); var_info.assignments.push_back(&node); var_info.conditionally_assigned = false; } +void ProgramCompiler::UnusedVariableLocator::clear_assignments(VariableInfo &var_info, bool mark_unused) +{ + if(mark_unused) + { + for(vector::iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i) + unused_nodes.insert(*i); + } + var_info.assignments.clear(); +} + void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr) { assignment = 0; @@ -1350,8 +1359,7 @@ void ProgramCompiler::UnusedVariableLocator::merge_down_variables() { if(!i->second.referenced) unused_nodes.insert(i->first); - for(vector::iterator j=i->second.assignments.begin(); j!=i->second.assignments.end(); ++j) - unused_nodes.insert(*j); + clear_assignments(i->second, true); continue; } @@ -1360,11 +1368,9 @@ void ProgramCompiler::UnusedVariableLocator::merge_down_variables() parent_variables.insert(*i); else { - if(!i->second.conditionally_assigned) - { - j->second.assignments.clear(); - j->second.conditionally_assigned = true; - } + if(i->second.referenced || !i->second.conditionally_assigned) + clear_assignments(j->second, !i->second.referenced); + j->second.conditionally_assigned = i->second.conditionally_assigned; j->second.referenced |= i->second.referenced; j->second.assignments.insert(j->second.assignments.end(), i->second.assignments.begin(), i->second.assignments.end()); } diff --git a/source/programcompiler.h b/source/programcompiler.h index 0ff4f667..ac1f910d 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -276,6 +276,7 @@ private: virtual void visit(ProgramSyntax::BinaryExpression &); virtual void visit(ProgramSyntax::Assignment &); void record_assignment(ProgramSyntax::VariableDeclaration &, ProgramSyntax::Node &, bool); + void clear_assignments(VariableInfo &, bool); virtual void visit(ProgramSyntax::ExpressionStatement &); virtual void visit(ProgramSyntax::StructDeclaration &); virtual void visit(ProgramSyntax::VariableDeclaration &);