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

« back to all changes in this revision

Viewing changes to src/cmd/fix/oserrorstring.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(oserrorstringFix)
13
 
}
14
 
 
15
 
var oserrorstringFix = fix{
16
 
        "oserrorstring",
17
 
        "2011-06-22",
18
 
        oserrorstring,
19
 
        `Replace os.ErrorString() conversions with calls to os.NewError().
20
 
 
21
 
http://codereview.appspot.com/4607052
22
 
`,
23
 
}
24
 
 
25
 
func oserrorstring(f *ast.File) bool {
26
 
        if !imports(f, "os") {
27
 
                return false
28
 
        }
29
 
 
30
 
        fixed := false
31
 
        walk(f, func(n interface{}) {
32
 
                // The conversion os.ErrorString(x) looks like a call
33
 
                // of os.ErrorString with one argument.
34
 
                if call := callExpr(n, "os", "ErrorString"); call != nil {
35
 
                        // os.ErrorString(args) -> os.NewError(args)
36
 
                        call.Fun.(*ast.SelectorExpr).Sel.Name = "NewError"
37
 
                        // os.ErrorString(args) -> os.NewError(args)
38
 
                        call.Fun.(*ast.SelectorExpr).Sel.Name = "NewError"
39
 
                        fixed = true
40
 
                        return
41
 
                }
42
 
 
43
 
                // Remove os.Error type from variable declarations initialized
44
 
                // with an os.NewError.
45
 
                // (An *ast.ValueSpec may also be used in a const declaration
46
 
                // but those won't be initialized with a call to os.NewError.)
47
 
                if spec, ok := n.(*ast.ValueSpec); ok &&
48
 
                        len(spec.Names) == 1 &&
49
 
                        isPkgDot(spec.Type, "os", "Error") &&
50
 
                        len(spec.Values) == 1 &&
51
 
                        callExpr(spec.Values[0], "os", "NewError") != nil {
52
 
                        // var name os.Error = os.NewError(x) ->
53
 
                        // var name          = os.NewError(x)
54
 
                        spec.Type = nil
55
 
                        fixed = true
56
 
                        return
57
 
                }
58
 
 
59
 
                // Other occurrences of os.ErrorString are not fixed
60
 
                // but they are rare.
61
 
 
62
 
        })
63
 
        return fixed
64
 
}
65
 
 
66
 
// callExpr returns the call expression if x is a call to pkg.name with one argument;
67
 
// otherwise it returns nil.
68
 
func callExpr(x interface{}, pkg, name string) *ast.CallExpr {
69
 
        if call, ok := x.(*ast.CallExpr); ok &&
70
 
                len(call.Args) == 1 &&
71
 
                isPkgDot(call.Fun, pkg, name) {
72
 
                return call
73
 
        }
74
 
        return nil
75
 
}