~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/cmd/fix/imagenew.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2011 The Go Authors.  All rights reserved.
2
 
// Use of this source code is governed by a BSD-style
3
 
// license that can be found in the LICENSE file.
4
 
 
5
 
package main
6
 
 
7
 
import (
8
 
        "go/ast"
9
 
)
10
 
 
11
 
func init() {
12
 
        register(imagenewFix)
13
 
}
14
 
 
15
 
var imagenewFix = fix{
16
 
        "imagenew",
17
 
        "2011-09-14",
18
 
        imagenew,
19
 
        `Adapt image.NewXxx calls to pass an image.Rectangle instead of (w, h int).
20
 
 
21
 
http://codereview.appspot.com/4964073
22
 
`,
23
 
}
24
 
 
25
 
var imagenewFuncs = map[string]bool{
26
 
        "NewRGBA":    true,
27
 
        "NewRGBA64":  true,
28
 
        "NewNRGBA":   true,
29
 
        "NewNRGBA64": true,
30
 
        "NewAlpha":   true,
31
 
        "NewAlpha16": true,
32
 
        "NewGray":    true,
33
 
        "NewGray16":  true,
34
 
}
35
 
 
36
 
func imagenew(f *ast.File) bool {
37
 
        if !imports(f, "image") {
38
 
                return false
39
 
        }
40
 
 
41
 
        fixed := false
42
 
        walk(f, func(n interface{}) {
43
 
                call, ok := n.(*ast.CallExpr)
44
 
                if !ok {
45
 
                        return
46
 
                }
47
 
                isNewFunc := false
48
 
                for newFunc := range imagenewFuncs {
49
 
                        if len(call.Args) == 2 && isPkgDot(call.Fun, "image", newFunc) {
50
 
                                isNewFunc = true
51
 
                                break
52
 
                        }
53
 
                }
54
 
                if len(call.Args) == 3 && isPkgDot(call.Fun, "image", "NewPaletted") {
55
 
                        isNewFunc = true
56
 
                }
57
 
                if !isNewFunc {
58
 
                        return
59
 
                }
60
 
                // Replace image.NewXxx(w, h) with image.NewXxx(image.Rect(0, 0, w, h)).
61
 
                rectArgs := []ast.Expr{
62
 
                        &ast.BasicLit{Value: "0"},
63
 
                        &ast.BasicLit{Value: "0"},
64
 
                }
65
 
                rectArgs = append(rectArgs, call.Args[:2]...)
66
 
                rect := []ast.Expr{
67
 
                        &ast.CallExpr{
68
 
                                Fun: &ast.SelectorExpr{
69
 
                                        X: &ast.Ident{
70
 
                                                Name: "image",
71
 
                                        },
72
 
                                        Sel: &ast.Ident{
73
 
                                                Name: "Rect",
74
 
                                        },
75
 
                                },
76
 
                                Args: rectArgs,
77
 
                        },
78
 
                }
79
 
                call.Args = append(rect, call.Args[2:]...)
80
 
                fixed = true
81
 
        })
82
 
        return fixed
83
 
}