~ubuntu-branches/ubuntu/trusty/golang/trusty

« back to all changes in this revision

Viewing changes to src/cmd/fix/newwriter.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 2012 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(newWriterFix)
13
 
}
14
 
 
15
 
var newWriterFix = fix{
16
 
        "newWriter",
17
 
        "2012-02-14",
18
 
        newWriter,
19
 
        `Adapt bufio, gzip and zlib NewWriterXxx calls for whether they return errors.
20
 
 
21
 
Also rename gzip.Compressor and gzip.Decompressor to gzip.Writer and gzip.Reader.
22
 
 
23
 
http://codereview.appspot.com/5639057 and
24
 
http://codereview.appspot.com/5642054
25
 
`,
26
 
}
27
 
 
28
 
func newWriter(f *ast.File) bool {
29
 
        if !imports(f, "bufio") && !imports(f, "compress/gzip") && !imports(f, "compress/zlib") {
30
 
                return false
31
 
        }
32
 
 
33
 
        fixed := false
34
 
        walk(f, func(n interface{}) {
35
 
                switch n := n.(type) {
36
 
                case *ast.SelectorExpr:
37
 
                        if isTopName(n.X, "gzip") {
38
 
                                switch n.Sel.String() {
39
 
                                case "Compressor":
40
 
                                        n.Sel = &ast.Ident{Name: "Writer"}
41
 
                                        fixed = true
42
 
                                case "Decompressor":
43
 
                                        n.Sel = &ast.Ident{Name: "Reader"}
44
 
                                        fixed = true
45
 
                                }
46
 
                        } else if isTopName(n.X, "zlib") {
47
 
                                if n.Sel.String() == "NewWriterDict" {
48
 
                                        n.Sel = &ast.Ident{Name: "NewWriterLevelDict"}
49
 
                                        fixed = true
50
 
                                }
51
 
                        }
52
 
 
53
 
                case *ast.AssignStmt:
54
 
                        // Drop the ", _" in assignments of the form:
55
 
                        //      w0, _ = gzip.NewWriter(w1)
56
 
                        if len(n.Lhs) != 2 || len(n.Rhs) != 1 {
57
 
                                return
58
 
                        }
59
 
                        i, ok := n.Lhs[1].(*ast.Ident)
60
 
                        if !ok {
61
 
                                return
62
 
                        }
63
 
                        if i.String() != "_" {
64
 
                                return
65
 
                        }
66
 
                        c, ok := n.Rhs[0].(*ast.CallExpr)
67
 
                        if !ok {
68
 
                                return
69
 
                        }
70
 
                        s, ok := c.Fun.(*ast.SelectorExpr)
71
 
                        if !ok {
72
 
                                return
73
 
                        }
74
 
                        sel := s.Sel.String()
75
 
                        switch {
76
 
                        case isTopName(s.X, "bufio") && (sel == "NewReaderSize" || sel == "NewWriterSize"):
77
 
                                // No-op.
78
 
                        case isTopName(s.X, "gzip") && sel == "NewWriter":
79
 
                                // No-op.
80
 
                        case isTopName(s.X, "zlib") && sel == "NewWriter":
81
 
                                // No-op.
82
 
                        default:
83
 
                                return
84
 
                        }
85
 
                        n.Lhs = n.Lhs[:1]
86
 
                        fixed = true
87
 
                }
88
 
        })
89
 
        return fixed
90
 
}