]> git.tdb.fi Git - libs/gl.git/commitdiff
Fix unused variable decetion for self-referenging assignments
authorMikko Rasa <tdb@tdb.fi>
Sat, 12 Nov 2016 16:09:32 +0000 (18:09 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 12 Nov 2016 16:10:39 +0000 (18:10 +0200)
Extend it to initialization expressions as well.

source/programcompiler.cpp
source/programcompiler.h

index 934416c28684f250fa8c37b9d842f5f05c39b7a9..55f62ae93e16d954b846fa13262f9108220c2dda 100644 (file)
@@ -627,22 +627,34 @@ void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var)
 
 ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
        assignment(false),
-       assignment_target(0)
+       record_target(false),
+       assignment_target(0),
+       indeterminate_target(false),
+       self_referencing(false)
 { }
 
 void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
 {
-       if(assignment)
-               assignment_target = var.declaration;
+       if(record_target)
+       {
+               if(assignment_target)
+                       indeterminate_target = true;
+               else
+                       assignment_target = var.declaration;
+       }
        else
        {
                unused_nodes.erase(var.declaration);
+
                map<VariableDeclaration *, Node *>::iterator i = assignments.find(var.declaration);
                if(i!=assignments.end())
                {
                        unused_nodes.erase(i->second);
                        assignments.erase(i);
                }
+
+               if(assignment && var.declaration==assignment_target)
+                       self_referencing = true;
        }
 }
 
@@ -656,9 +668,20 @@ void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
 {
        if(binary.assignment)
        {
-               binary.right->visit(*this);
                assignment = true;
+               {
+                       SetFlag set(record_target);
+                       binary.left->visit(*this);
+               }
+               if(binary.oper!="=")
+                       self_referencing = true;
+               binary.right->visit(*this);
+       }
+       else if(record_target && binary.oper=="[")
+       {
                binary.left->visit(*this);
+               SetForScope<bool> set(record_target, false);
+               binary.right->visit(*this);
        }
        else
                TraversingVisitor::visit(binary);
@@ -668,17 +691,21 @@ void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
 {
        assignment = false;
        assignment_target = 0;
+       indeterminate_target = false;
+       self_referencing = false;
        TraversingVisitor::visit(expr);
-       if(assignment && assignment_target)
+       if(assignment && assignment_target && !indeterminate_target)
        {
                Node *&assign = assignments[assignment_target];
-               if(assign)
+               if(self_referencing)
+                       unused_nodes.erase(assign);
+               else if(assign)
                        unused_nodes.insert(assign);
                assign = &expr;
-               if(assignment_target->interface!="out" || (stage->type!=FRAGMENT && !assignment_target->linked_declaration))
-                       unused_nodes.insert(&expr);
-               else
+               if(assignment_target->interface=="out" && (stage->type==FRAGMENT || assignment_target->linked_declaration))
                        unused_nodes.erase(assignment_target);
+               else
+                       unused_nodes.insert(&expr);
        }
        assignment = false;
 }
@@ -686,6 +713,11 @@ void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
 void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
 {
        unused_nodes.insert(&var);
+       if(var.init_expression)
+       {
+               unused_nodes.insert(&*var.init_expression);
+               assignments[&var] = &*var.init_expression;
+       }
        TraversingVisitor::visit(var);
 }
 
@@ -731,6 +763,8 @@ void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
                if(var.linked_declaration)
                        var.linked_declaration->linked_declaration = 0;
        }
+       else if(var.init_expression && to_remove.count(&*var.init_expression))
+               var.init_expression = 0;
 }
 
 void ProgramCompiler::NodeRemover::visit(InterfaceBlock &iface)
index 34b64b557ca29a9185dc2f5e2aacaa1420bbf325..f1ea165296b49f5fc122cbc344a6b37693dd182f 100644 (file)
@@ -100,7 +100,10 @@ private:
                std::set<ProgramSyntax::Node *> unused_nodes;
                std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Node *> assignments;
                bool assignment;
+               bool record_target;
                ProgramSyntax::VariableDeclaration *assignment_target;
+               bool indeterminate_target;
+               bool self_referencing;
 
                UnusedVariableLocator();