summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2010-03-15 15:42:09 -0700
committerRobert Griesemer <gri@golang.org>2010-03-15 15:42:09 -0700
commit158f989e3728ccc902dd07021773c2f25de58543 (patch)
tree63c5bec0d048ebe7716b3ed5c77fb95df9bb9b2e
parentfd92a01d9ec0e495884bf98ed2fd62af1298b2c3 (diff)
downloadgolang-158f989e3728ccc902dd07021773c2f25de58543.tar.gz
gofmt: fix for gofmt rewrite feature
Fixes issue 643. R=rsc CC=golang-dev http://codereview.appspot.com/576041
-rw-r--r--src/cmd/gofmt/doc.go4
-rw-r--r--src/cmd/gofmt/rewrite.go16
2 files changed, 15 insertions, 5 deletions
diff --git a/src/cmd/gofmt/doc.go b/src/cmd/gofmt/doc.go
index 4b4adba03..2e4c40c21 100644
--- a/src/cmd/gofmt/doc.go
+++ b/src/cmd/gofmt/doc.go
@@ -41,8 +41,8 @@ The rewrite rule specified with the -r flag must be a string of the form:
pattern -> replacement
Both pattern and replacement must be valid Go expressions.
-In the pattern, single-character lowercase identifers serve as
-wildcards matching arbitrary subexpressions; those expressions
+In the pattern, single-character lowercase identifiers serve as
+wildcards matching arbitrary sub-expressions; those expressions
will be substituted for the same identifiers in the replacement.
diff --git a/src/cmd/gofmt/rewrite.go b/src/cmd/gofmt/rewrite.go
index b2b21597d..9c238fab2 100644
--- a/src/cmd/gofmt/rewrite.go
+++ b/src/cmd/gofmt/rewrite.go
@@ -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 {
m := make(map[string]reflect.Value)
pat := reflect.NewValue(pattern)
@@ -127,9 +127,19 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
return false
}
- // Token positions need not match.
- if pattern.Type() == positionType {
+ // Special cases.
+ switch pattern.Type() {
+ case positionType:
+ // token positions don't need to match
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)