Repository: VKCOM/noverify
Pull request #651 (2020-08-10 11:55:02)

ir/irutil: port AST utils to IR utils


Patch:
diff --git a/src/php/astutil/clone.go b/src/ir/irutil/clone.go
similarity index 99%
rename from src/php/astutil/clone.go
rename to src/ir/irutil/clone.go
index 1f0d778..c41cc0d 100644
--- a/src/php/astutil/clone.go
+++ b/src/ir/irutil/clone.go
@@ -1,9 +1,8 @@
 // Code generated by `codegen.go`. DO NOT EDIT.
-package astutil
+package irutil
 
 import (
 	"fmt"
-
 	"github.com/VKCOM/noverify/src/ir"
 )
 
diff --git a/src/php/astutil/codegen.go b/src/ir/irutil/codegen.go
similarity index 96%
rename from src/php/astutil/codegen.go
rename to src/ir/irutil/codegen.go
index 4f7b726..0c4807f 100644
--- a/src/php/astutil/codegen.go
+++ b/src/ir/irutil/codegen.go
@@ -38,15 +38,6 @@ type context struct {
 
 func main() {
 	packages := []string{
-		// "parser/node",
-		// "parser/node/stmt",
-		// "parser/node/name",
-		// "parser/node/scalar",
-		// "parser/node/expr",
-		// "parser/node/expr/binary",
-		// "parser/node/expr/cast",
-		// "parser/node/expr/assign",
-
 		"ir",
 	}
 
@@ -68,7 +59,7 @@ func main() {
 func generateClone(ctx *context) {
 	var buf bytes.Buffer
 	buf.WriteString("// Code generated by `codegen.go`. DO NOT EDIT.\n")
-	buf.WriteString("package astutil\n")
+	buf.WriteString("package irutil\n")
 	buf.WriteString("import (\n")
 	for _, path := range importPaths(ctx.packagePaths) {
 		buf.WriteString("  \"" + path + "\"\n")
@@ -199,7 +190,7 @@ func writeCloneCase(w *bytes.Buffer, pkg *packageData, typ *typeData) {
 func generateEqual(ctx *context) {
 	var buf bytes.Buffer
 	buf.WriteString("// Code generated by `codegen.go`. DO NOT EDIT.\n")
-	buf.WriteString("package astutil\n")
+	buf.WriteString("package irutil\n")
 	buf.WriteString("import (\n")
 	for _, path := range importPaths(ctx.packagePaths) {
 		buf.WriteString("  \"" + path + "\"\n")
diff --git a/src/php/astutil/equal.go b/src/ir/irutil/equal.go
similarity index 99%
rename from src/php/astutil/equal.go
rename to src/ir/irutil/equal.go
index e3e8c16..3cf629d 100644
--- a/src/php/astutil/equal.go
+++ b/src/ir/irutil/equal.go
@@ -1,9 +1,8 @@
 // Code generated by `codegen.go`. DO NOT EDIT.
-package astutil
+package irutil
 
 import (
 	"fmt"
-
 	"github.com/VKCOM/noverify/src/ir"
 )
 
diff --git a/src/php/astutil/astutil.go b/src/ir/irutil/irtutil.go
similarity index 98%
rename from src/php/astutil/astutil.go
rename to src/ir/irutil/irtutil.go
index db9f3d5..7762ffc 100644
--- a/src/php/astutil/astutil.go
+++ b/src/ir/irutil/irtutil.go
@@ -1,4 +1,4 @@
-package astutil
+package irutil
 
 import (
 	"github.com/VKCOM/noverify/src/ir"
diff --git a/src/php/astutil/nodeSet.go b/src/ir/irutil/nodeSet.go
similarity index 99%
rename from src/php/astutil/nodeSet.go
rename to src/ir/irutil/nodeSet.go
index 33ea7fb..c2bef38 100644
--- a/src/php/astutil/nodeSet.go
+++ b/src/ir/irutil/nodeSet.go
@@ -1,4 +1,4 @@
-package astutil
+package irutil
 
 import (
 	"github.com/VKCOM/noverify/src/ir"
diff --git a/src/linter/block_context.go b/src/linter/block_context.go
index 773a066..1137d08 100644
--- a/src/linter/block_context.go
+++ b/src/linter/block_context.go
@@ -2,8 +2,8 @@ package linter
 
 import (
 	"github.com/VKCOM/noverify/src/ir"
+	"github.com/VKCOM/noverify/src/ir/irutil"
 	"github.com/VKCOM/noverify/src/meta"
-	"github.com/VKCOM/noverify/src/php/astutil"
 	"github.com/VKCOM/noverify/src/solver"
 )
 
@@ -52,15 +52,15 @@ func (ctx *blockContext) customFunctionExists(nm ir.Node) bool {
 
 func (ctx *blockContext) addCustomMethod(obj ir.Node, methodName string) {
 	ctx.customMethods = append(ctx.customMethods, customMethod{
-		obj:  astutil.Unparen(obj),
+		obj:  irutil.Unparen(obj),
 		name: methodName,
 	})
 }
 
 func (ctx *blockContext) customMethodExists(obj ir.Node, methodName string) bool {
-	obj = astutil.Unparen(obj)
+	obj = irutil.Unparen(obj)
 	for _, m := range ctx.customMethods {
-		if m.name == methodName && astutil.NodeEqual(m.obj, obj) {
+		if m.name == methodName && irutil.NodeEqual(m.obj, obj) {
 			return true
 		}
 	}
diff --git a/src/linter/block_linter.go b/src/linter/block_linter.go
index 1632bdc..5542794 100644
--- a/src/linter/block_linter.go
+++ b/src/linter/block_linter.go
@@ -5,8 +5,8 @@ import (
 	"strings"
 
 	"github.com/VKCOM/noverify/src/ir"
+	"github.com/VKCOM/noverify/src/ir/irutil"
 	"github.com/VKCOM/noverify/src/meta"
-	"github.com/VKCOM/noverify/src/php/astutil"
 	"github.com/VKCOM/noverify/src/php/parser/freefloating"
 	"github.com/VKCOM/noverify/src/solver"
 )
@@ -300,7 +300,7 @@ func (b *blockLinter) checkStrictCmp(n ir.Node, left, right ir.Node) {
 			suggest = "!=="
 		}
 		b.report(n, LevelWarning, "strictCmp", "non-strict comparison with %s (use %s)",
-			astutil.FmtNode(badNode), suggest)
+			irutil.FmtNode(badNode), suggest)
 	}
 }
 
@@ -392,7 +392,7 @@ func (b *blockLinter) checkStmtExpression(s *ir.ExpressionStmt) {
 
 	// All branches except default try to filter-out common
 	// cases to reduce the number of type solving performed.
-	if astutil.IsAssign(s.Expr) {
+	if irutil.IsAssign(s.Expr) {
 		return
 	}
 	switch s.Expr.(type) {
@@ -447,7 +447,7 @@ func (b *blockLinter) checkTernary(e *ir.TernaryExpr) {
 	}
 
 	// Check for `$cond ? $x : $x` which makes no sense.
-	if astutil.NodeEqual(e.IfTrue, e.IfFalse) {
+	if irutil.NodeEqual(e.IfTrue, e.IfFalse) {
 		b.report(e, LevelWarning, "dupBranchBody", "then/else operands are identical")
 	}
 }
@@ -491,7 +491,7 @@ func (b *blockLinter) checkIfStmt(s *ir.IfStmt) {
 	if len(s.ElseIf) == 0 && s.Else != nil {
 		x := s.Stmt
 		y := s.Else.(*ir.ElseStmt).Stmt
-		if astutil.NodeEqual(x, y) {
+		if irutil.NodeEqual(x, y) {
 			b.report(s, LevelWarning, "dupBranchBody", "duplicated if/else actions")
 		}
 	}
@@ -500,7 +500,7 @@ func (b *blockLinter) checkIfStmt(s *ir.IfStmt) {
 }
 
 func (b *blockLinter) checkIfStmtDupCond(s *ir.IfStmt) {
-	conditions := astutil.NewNodeSet()
+	conditions := irutil.NewNodeSet()
 	conditions.Add(s.Cond)
 	for _, elseif := range s.ElseIf {
 		elseif := elseif.(*ir.ElseIfStmt)
diff --git a/src/linter/block_utils.go b/src/linter/block_utils.go
index 1779f31..d43b7ca 100644
--- a/src/linter/block_utils.go
+++ b/src/linter/block_utils.go
@@ -6,8 +6,8 @@ import (
 	"strings"
 
 	"github.com/VKCOM/noverify/src/ir"
+	"github.com/VKCOM/noverify/src/ir/irutil"
 	"github.com/VKCOM/noverify/src/meta"
-	"github.com/VKCOM/noverify/src/php/astutil"
 	"github.com/VKCOM/noverify/src/php/parser/freefloating"
 	"github.com/VKCOM/noverify/src/solver"
 )
@@ -134,7 +134,7 @@ func nodeEqual(st *meta.ClassParseState, x, y ir.Node) bool {
 		return info1.Value.IsEqual(info2.Value)
 
 	default:
-		return astutil.NodeEqual(x, y)
+		return irutil.NodeEqual(x, y)
 	}
 }
 
diff --git a/src/linter/root.go b/src/linter/root.go
index 1c0aac3..f786b7e 100644
--- a/src/linter/root.go
+++ b/src/linter/root.go
@@ -12,8 +12,8 @@ import (
 	"github.com/VKCOM/noverify/src/baseline"
 	"github.com/VKCOM/noverify/src/git"
 	"github.com/VKCOM/noverify/src/ir"
+	"github.com/VKCOM/noverify/src/ir/irutil"
 	"github.com/VKCOM/noverify/src/meta"
-	"github.com/VKCOM/noverify/src/php/astutil"
 	"github.com/VKCOM/noverify/src/php/parser/freefloating"
 	"github.com/VKCOM/noverify/src/php/parser/php7"
 	"github.com/VKCOM/noverify/src/php/parser/position"
@@ -48,7 +48,7 @@ type RootWalker struct {
 	ctx rootContext
 
 	// nodeSet is a reusable node set for both root and block walkers.
-	nodeSet astutil.NodeSet
+	nodeSet irutil.NodeSet
 
 	reSimplifier *regexpSimplifier
 	reVet        *regexpVet
@@ -1734,7 +1734,7 @@ func (d *RootWalker) sourceNodeString(n ir.Node) string {
 		return string(src[from:to])
 	}
 	// If we can't take node out of the source text, print it.
-	return astutil.FmtNode(n)
+	return irutil.FmtNode(n)
 }
 
 func (d *RootWalker) renderRuleMessage(msg string, n ir.Node, m phpgrep.MatchData, truncate bool) string {
diff --git a/src/solver/exprtype.go b/src/solver/exprtype.go
index 0c7f3fb..8391581 100644
--- a/src/solver/exprtype.go
+++ b/src/solver/exprtype.go
@@ -6,8 +6,8 @@ import (
 	"strings"
 
 	"github.com/VKCOM/noverify/src/ir"
+	"github.com/VKCOM/noverify/src/ir/irutil"
 	"github.com/VKCOM/noverify/src/meta"
-	"github.com/VKCOM/noverify/src/php/astutil"
 )
 
 func bitwiseOpType(sc *meta.Scope, cs *meta.ClassParseState, left, right ir.Node, custom []CustomType) meta.TypesMap {
@@ -167,7 +167,7 @@ func exprTypeLocalCustom(sc *meta.Scope, cs *meta.ClassParseState, n ir.Node, cu
 	}
 
 	for _, c := range custom {
-		if astutil.NodeEqual(c.Node, n) {
+		if irutil.NodeEqual(c.Node, n) {
 			return c.Typ
 		}
 	}


Test patch:
diff --git a/src/php/astutil/nodeSet_test.go b/src/ir/irutil/nodeSet_test.go
similarity index 98%
rename from src/php/astutil/nodeSet_test.go
rename to src/ir/irutil/nodeSet_test.go
index 6fd9d33..61e50cf 100644
--- a/src/php/astutil/nodeSet_test.go
+++ b/src/ir/irutil/nodeSet_test.go
@@ -1,4 +1,4 @@
-package astutil
+package irutil
 
 import (
 	"fmt"
diff --git a/src/phpgrep/matcher_test.go b/src/phpgrep/matcher_test.go
index 79d5a26..b5dc655 100644
--- a/src/phpgrep/matcher_test.go
+++ b/src/phpgrep/matcher_test.go
@@ -6,8 +6,8 @@ import (
 	"testing"
 
 	"github.com/VKCOM/noverify/src/ir"
+	"github.com/VKCOM/noverify/src/ir/irutil"
 	"github.com/VKCOM/noverify/src/irgen"
-	"github.com/VKCOM/noverify/src/php/astutil"
 	"github.com/VKCOM/noverify/src/php/parseutil"
 )
 
@@ -68,7 +68,7 @@ func TestMatchCapture(t *testing.T) {
 			t.Errorf("%s not captured", name)
 			return
 		}
-		have := astutil.FmtNode(n)
+		have := irutil.FmtNode(n)
 		if have != want {
 			t.Errorf("%s mismatched: have %s, want %s", name, have, want)
 		}
diff --git a/src/tests/exprtype/exprtype_test.go b/src/tests/exprtype/exprtype_test.go
index 1b60729..3154e41 100644
--- a/src/tests/exprtype/exprtype_test.go
+++ b/src/tests/exprtype/exprtype_test.go
@@ -8,10 +8,10 @@ import (
 	"github.com/google/go-cmp/cmp"
 
 	"github.com/VKCOM/noverify/src/ir"
+	"github.com/VKCOM/noverify/src/ir/irutil"
 	"github.com/VKCOM/noverify/src/linter"
 	"github.com/VKCOM/noverify/src/linttest"
 	"github.com/VKCOM/noverify/src/meta"
-	"github.com/VKCOM/noverify/src/php/astutil"
 )
 
 // Tests in this file make it less likely that type solving will break
@@ -2011,7 +2011,7 @@ func (w *exprTypeWalker) EnterNode(n ir.Node) bool {
 		expectedType := call.ArgumentList.Arguments[1].(*ir.Argument).Expr.(*ir.String).Value
 		actualType, ok := exprTypeResult[checkedExpr]
 		if !ok {
-			w.t.Fatalf("no type found for %s expression", astutil.FmtNode(checkedExpr))
+			w.t.Fatalf("no type found for %s expression", irutil.FmtNode(checkedExpr))
 		}
 		want := makeType(expectedType[len(`"`) : len(expectedType)-len(`"`)])
 		have := testTypesMap{
@@ -2021,7 +2021,7 @@ func (w *exprTypeWalker) EnterNode(n ir.Node) bool {
 		if diff := cmp.Diff(have, want); diff != "" {
 			line := ir.GetPosition(checkedExpr).StartLine
 			w.t.Errorf("line %d: type mismatch for %s (-have +want):\n%s",
-				line, astutil.FmtNode(checkedExpr), diff)
+				line, irutil.FmtNode(checkedExpr), diff)
 		}
 		return false
 	}