Пример #1
0
// compiles a binary expression x 'op' y
func (w *World) compileBinaryExpr(n *ast.BinaryExpr) Expr {
	switch n.Op {
	default:
		panic(err(n.Pos(), "not allowed:", n.Op))
	case token.ADD:
		return &add{w.newBinExpr(n)}
	case token.SUB:
		return &sub{w.newBinExpr(n)}
	case token.MUL:
		return &mul{w.newBinExpr(n)}
	case token.QUO:
		return &quo{w.newBinExpr(n)}
	case token.LSS:
		return &lss{w.newComp(n)}
	case token.GTR:
		return &gtr{w.newComp(n)}
	case token.LEQ:
		return &leq{w.newComp(n)}
	case token.GEQ:
		return &geq{w.newComp(n)}
	case token.EQL:
		return &eql{w.newComp(n)}
	case token.NEQ:
		return &neq{w.newComp(n)}
	case token.LAND:
		return &and{w.newBoolOp(n)}
	case token.LOR:
		return &or{w.newBoolOp(n)}
	}
}
Пример #2
0
func RunMutation(index int, exp *ast.BinaryExpr, f, t token.Token, src string, fset *token.FileSet, file *ast.File) error {
	exp.Op = t
	defer func() {
		exp.Op = f
	}()

	err := printFile(src, fset, file)
	if err != nil {
		return err
	}

	cmd := exec.Command("go", "test")
	cmd.Dir = filepath.Dir(src)
	output, err := cmd.CombinedOutput()
	if err == nil {
		code = 1
		log.Printf("mutation %d failed to break any tests", index)
	} else if _, ok := err.(*exec.ExitError); ok {
		lines := bytes.Split(output, []byte("\n"))
		lastLine := lines[len(lines)-2]
		if bytes.HasPrefix(lastLine, []byte("FAIL")) {
			log.Printf("mutation %d failed the tests properly", index)
		} else {
			log.Printf("mutation %d created an error: %s", index, lastLine)
		}
	} else {
		return fmt.Errorf("mutation %d failed to run: %s", index, err)
	}
	return nil
}
Пример #3
0
func (v *powerVisitor) leaveBinaryExpr(binaryExpr *ast.BinaryExpr) {
	if modified, ok := captExpr(binaryExpr.X); ok {
		binaryExpr.X = modified
	}
	if modified, ok := captExpr(binaryExpr.Y); ok {
		binaryExpr.Y = modified
	}
}
Пример #4
0
func (rp *rewritePackage) binaryExprTFunc(binaryExpr *ast.BinaryExpr) {
	if x, ok := binaryExpr.X.(*ast.BasicLit); ok {
		binaryExpr.X = rp.wrapBasicLitWithT(x)
	}

	if y, ok := binaryExpr.Y.(*ast.BasicLit); ok {
		binaryExpr.Y = rp.wrapBasicLitWithT(y)
	}
}
Пример #5
0
func (f *File) checkNilFuncComparison(e *ast.BinaryExpr) {
	if !vet("nilfunc") {
		return
	}

	// Only want == or != comparisons.
	if e.Op != token.EQL && e.Op != token.NEQ {
		return
	}

	// Only want comparisons with a nil identifier on one side.
	var e2 ast.Expr
	switch {
	case f.isNil(e.X):
		e2 = e.Y
	case f.isNil(e.Y):
		e2 = e.X
	default:
		return
	}

	// Only want identifiers or selector expressions.
	var obj types.Object
	switch v := e2.(type) {
	case *ast.Ident:
		obj = f.pkg.idents[v]
	case *ast.SelectorExpr:
		obj = f.pkg.idents[v.Sel]
	default:
		return
	}

	// Only want functions.
	if _, ok := obj.(*types.Func); !ok {
		return
	}

	f.Badf(e.Pos(), "comparison of function %v %v nil is always %v", obj.Name(), e.Op, e.Op == token.NEQ)
}
Пример #6
0
func (w *World) newBoolOp(n *ast.BinaryExpr) boolOp {
	x := typeConv(n.Pos(), w.compileExpr(n.X), bool_t)
	y := typeConv(n.Pos(), w.compileExpr(n.Y), bool_t)
	return boolOp{x, y}
}
Пример #7
0
func (w *World) newBinExpr(n *ast.BinaryExpr) binaryExpr {
	x := typeConv(n.Pos(), w.compileExpr(n.X), float64_t)
	y := typeConv(n.Pos(), w.compileExpr(n.Y), float64_t)
	return binaryExpr{x, y}
}