Commit a6820b65 authored by Robert Griesemer's avatar Robert Griesemer

gofmt: don't attempt certain illegal rewrites

(e.g.: echo 'package main' | gofmt -r 'x->7'
cannot change the package name to 7)

R=rsc
CC=golang-dev
https://golang.org/cl/3913041
parent f383062e
...@@ -66,13 +66,19 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File { ...@@ -66,13 +66,19 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File {
} }
var positionType = reflect.Typeof(token.NoPos) // setValue is a wrapper for x.SetValue(y); it protects
var identType = reflect.Typeof((*ast.Ident)(nil)) // the caller from panics if x cannot be changed to y.
func setValue(x, y reflect.Value) {
defer func() {
func isWildcard(s string) bool { if x := recover(); x != nil {
rune, size := utf8.DecodeRuneInString(s) if s, ok := x.(string); ok && strings.HasPrefix(s, "type mismatch") {
return size == len(s) && unicode.IsLower(rune) // x cannot be set to y - ignore this rewrite
return
}
panic(x)
}
}()
x.SetValue(y)
} }
...@@ -86,21 +92,31 @@ func apply(f func(reflect.Value) reflect.Value, val reflect.Value) reflect.Value ...@@ -86,21 +92,31 @@ func apply(f func(reflect.Value) reflect.Value, val reflect.Value) reflect.Value
case *reflect.SliceValue: case *reflect.SliceValue:
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
e := v.Elem(i) e := v.Elem(i)
e.SetValue(f(e)) setValue(e, f(e))
} }
case *reflect.StructValue: case *reflect.StructValue:
for i := 0; i < v.NumField(); i++ { for i := 0; i < v.NumField(); i++ {
e := v.Field(i) e := v.Field(i)
e.SetValue(f(e)) setValue(e, f(e))
} }
case *reflect.InterfaceValue: case *reflect.InterfaceValue:
e := v.Elem() e := v.Elem()
v.SetValue(f(e)) setValue(v, f(e))
} }
return val return val
} }
var positionType = reflect.Typeof(token.NoPos)
var identType = reflect.Typeof((*ast.Ident)(nil))
func isWildcard(s string) bool {
rune, size := utf8.DecodeRuneInString(s)
return size == len(s) && unicode.IsLower(rune)
}
// match returns true if pattern matches val, // match returns true if pattern matches val,
// recording wildcard submatches in m. // recording wildcard submatches in m.
// If m == nil, match checks whether pattern == val. // If m == nil, match checks whether pattern == val.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment