1
0
mirror of https://github.com/golang/go synced 2024-11-22 00:44:39 -07:00

gofmt: fix for gofmt rewrite feature

Fixes #643.

R=rsc
CC=golang-dev
https://golang.org/cl/576041
This commit is contained in:
Robert Griesemer 2010-03-15 15:42:09 -07:00
parent 6129dbbee4
commit b32f22b3f8
2 changed files with 15 additions and 5 deletions

View File

@ -41,8 +41,8 @@ The rewrite rule specified with the -r flag must be a string of the form:
pattern -> replacement pattern -> replacement
Both pattern and replacement must be valid Go expressions. Both pattern and replacement must be valid Go expressions.
In the pattern, single-character lowercase identifers serve as In the pattern, single-character lowercase identifiers serve as
wildcards matching arbitrary subexpressions; those expressions wildcards matching arbitrary sub-expressions; those expressions
will be substituted for the same identifiers in the replacement. will be substituted for the same identifiers in the replacement.

View File

@ -46,7 +46,7 @@ func parseExpr(s string, what string) ast.Expr {
} }
// rewriteFile applys the rewrite rule pattern -> replace to an entire file. // rewriteFile applies the rewrite rule 'pattern -> replace' to an entire file.
func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File { func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File {
m := make(map[string]reflect.Value) m := make(map[string]reflect.Value)
pat := reflect.NewValue(pattern) pat := reflect.NewValue(pattern)
@ -127,9 +127,19 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
return false return false
} }
// Token positions need not match. // Special cases.
if pattern.Type() == positionType { switch pattern.Type() {
case positionType:
// token positions don't need to match
return true return true
case identType:
// For identifiers, only the names need to match
// (and none of the other *ast.Object information).
// This is a common case, handle it all here instead
// of recursing down any further via reflection.
p := pattern.Interface().(*ast.Ident)
v := val.Interface().(*ast.Ident)
return p == nil && v == nil || p != nil && v != nil && p.Name() == v.Name()
} }
p := reflect.Indirect(pattern) p := reflect.Indirect(pattern)