]> git.tdb.fi Git - libs/gl.git/commitdiff
Some fixes to assignment management in UnusedVariableLocator
authorMikko Rasa <tdb@tdb.fi>
Sat, 10 Dec 2016 20:43:51 +0000 (22:43 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 10 Dec 2016 20:43:51 +0000 (22:43 +0200)
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).

source/programcompiler.cpp
source/programcompiler.h

index 14c9d3d3ced42958e539e7ef27a6623f297e41cb..cc4b5efe3df0bfca482feb8d9ae3f46e46391d42 100644 (file)
@@ -1229,8 +1229,7 @@ void ProgramCompiler::UnusedVariableLocator::apply(Stage &s)
                if(!i->second.referenced)
                {
                        unused_nodes.insert(i->first);
-                       for(vector<Node *>::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<Node *>::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<Node *>::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());
                }
index 0ff4f667d3feb830013d30c908a4eb97847b4ec4..ac1f910d530dd7b53c984c195d3d4310d372c39f 100644 (file)
@@ -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 &);