clean up sksl dead variable handling

Change-Id: I301e82bf87d976e59a02b0f383da67eaf5a8795a
Reviewed-on: https://skia-review.googlesource.com/18494
Reviewed-by: Chris Dalton <csmartdalton@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
diff --git a/src/sksl/SkSLCompiler.cpp b/src/sksl/SkSLCompiler.cpp
index 2d541a3..0818b75 100644
--- a/src/sksl/SkSLCompiler.cpp
+++ b/src/sksl/SkSLCompiler.cpp
@@ -1024,11 +1024,11 @@
     } while (updated);
     ASSERT(!needsRescan);
 
-    // verify static ifs & switches
+    // verify static ifs & switches, clean up dead variable decls
     for (BasicBlock& b : cfg.fBlocks) {
         DefinitionMap definitions = b.fBefore;
 
-        for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
+        for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
             if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
                 const Statement& s = **iter->statement();
                 switch (s.fKind) {
@@ -1036,15 +1036,36 @@
                         if (((const IfStatement&) s).fIsStatic) {
                             this->error(s.fPosition, "static if has non-static test");
                         }
+                        ++iter;
                         break;
                     case Statement::kSwitch_Kind:
                         if (((const SwitchStatement&) s).fIsStatic) {
                             this->error(s.fPosition, "static switch has non-static test");
                         }
+                        ++iter;
                         break;
+                    case Statement::kVarDeclarations_Kind: {
+                        VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
+                        for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
+                            if ((*varIter)->fKind == Statement::kNop_Kind) {
+                                varIter = decls.fVars.erase(varIter);
+                            } else {
+                                ++varIter;
+                            }
+                        }
+                        if (!decls.fVars.size()) {
+                            iter = b.fNodes.erase(iter);
+                        } else {
+                            ++iter;
+                        }
+                        break;
+                    }
                     default:
+                        ++iter;
                         break;
                 }
+            } else {
+                ++iter;
             }
         }
     }