summaryrefslogtreecommitdiff
path: root/src/cmd/gofmt
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/gofmt')
-rw-r--r--src/cmd/gofmt/doc.go11
-rw-r--r--src/cmd/gofmt/gofmt.go39
-rw-r--r--src/cmd/gofmt/gofmt_test.go1
-rw-r--r--src/cmd/gofmt/long_test.go2
-rw-r--r--src/cmd/gofmt/rewrite.go27
-rw-r--r--src/cmd/gofmt/testdata/typeswitch.golden2
-rw-r--r--src/cmd/gofmt/testdata/typeswitch.input2
7 files changed, 32 insertions, 52 deletions
diff --git a/src/cmd/gofmt/doc.go b/src/cmd/gofmt/doc.go
index 94e67fd89..8f73ef5b9 100644
--- a/src/cmd/gofmt/doc.go
+++ b/src/cmd/gofmt/doc.go
@@ -4,6 +4,7 @@
/*
Gofmt formats Go programs.
+It uses tabs (width = 8) for indentation and blanks for alignment.
Without an explicit path, it processes the standard input. Given a file,
it operates on that file; given a directory, it operates on all .go files in
@@ -33,13 +34,9 @@ The flags are:
If a file's formatting is different from gofmt's, overwrite it
with gofmt's version.
-Formatting control flags:
- -comments=true
- Print comments; if false, all comments are elided from the output.
- -tabs=true
- Indent with tabs; if false, spaces are used instead.
- -tabwidth=8
- Tab width in spaces.
+Debugging support:
+ -cpuprofile filename
+ Write cpu profile to the specified file.
The rewrite rule specified with the -r flag must be a string of the form:
diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go
index 861ff9390..576cae522 100644
--- a/src/cmd/gofmt/gofmt.go
+++ b/src/cmd/gofmt/gofmt.go
@@ -31,21 +31,20 @@ var (
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")
- // layout control
- comments = flag.Bool("comments", true, "print comments")
- tabWidth = flag.Int("tabwidth", 8, "tab width")
- tabIndent = flag.Bool("tabs", true, "indent with tabs")
-
// debugging
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
)
+const (
+ tabWidth = 8
+ printerMode = printer.UseSpaces | printer.TabIndent
+)
+
var (
- fileSet = token.NewFileSet() // per process FileSet
- exitCode = 0
- rewrite func(*ast.File) *ast.File
- parserMode parser.Mode
- printerMode printer.Mode
+ fileSet = token.NewFileSet() // per process FileSet
+ exitCode = 0
+ rewrite func(*ast.File) *ast.File
+ parserMode parser.Mode
)
func report(err error) {
@@ -60,22 +59,12 @@ func usage() {
}
func initParserMode() {
- parserMode = parser.Mode(0)
- if *comments {
- parserMode |= parser.ParseComments
- }
+ parserMode = parser.ParseComments
if *allErrors {
parserMode |= parser.AllErrors
}
}
-func initPrinterMode() {
- printerMode = printer.UseSpaces
- if *tabIndent {
- printerMode |= printer.TabIndent
- }
-}
-
func isGoFile(f os.FileInfo) bool {
// ignore non-Go files
name := f.Name()
@@ -118,7 +107,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
}
var buf bytes.Buffer
- err = (&printer.Config{Mode: printerMode, Tabwidth: *tabWidth}).Fprint(&buf, fileSet, file)
+ err = (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(&buf, fileSet, file)
if err != nil {
return err
}
@@ -180,11 +169,6 @@ func main() {
func gofmtMain() {
flag.Usage = usage
flag.Parse()
- if *tabWidth < 0 {
- fmt.Fprintf(os.Stderr, "negative tabwidth %d\n", *tabWidth)
- exitCode = 2
- return
- }
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
@@ -199,7 +183,6 @@ func gofmtMain() {
}
initParserMode()
- initPrinterMode()
initRewrite()
if flag.NArg() == 0 {
diff --git a/src/cmd/gofmt/gofmt_test.go b/src/cmd/gofmt/gofmt_test.go
index 75a322a6c..b9335b8f3 100644
--- a/src/cmd/gofmt/gofmt_test.go
+++ b/src/cmd/gofmt/gofmt_test.go
@@ -40,7 +40,6 @@ func runTest(t *testing.T, in, out, flags string) {
}
initParserMode()
- initPrinterMode()
initRewrite()
var buf bytes.Buffer
diff --git a/src/cmd/gofmt/long_test.go b/src/cmd/gofmt/long_test.go
index 862e9d987..108278b33 100644
--- a/src/cmd/gofmt/long_test.go
+++ b/src/cmd/gofmt/long_test.go
@@ -38,7 +38,7 @@ func gofmt(fset *token.FileSet, filename string, src *bytes.Buffer) error {
}
ast.SortImports(fset, f)
src.Reset()
- return (&printer.Config{Mode: printerMode, Tabwidth: *tabWidth}).Fprint(src, fset, f)
+ return (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(src, fset, f)
}
func testFile(t *testing.T, b1, b2 *bytes.Buffer, filename string) {
diff --git a/src/cmd/gofmt/rewrite.go b/src/cmd/gofmt/rewrite.go
index 66d2331a5..fb6c6fc81 100644
--- a/src/cmd/gofmt/rewrite.go
+++ b/src/cmd/gofmt/rewrite.go
@@ -48,7 +48,7 @@ func parseExpr(s, what string) ast.Expr {
/*
func dump(msg string, val reflect.Value) {
fmt.Printf("%s:\n", msg)
- ast.Print(fset, val.Interface())
+ ast.Print(fileSet, val.Interface())
fmt.Println()
}
*/
@@ -59,8 +59,9 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File {
m := make(map[string]reflect.Value)
pat := reflect.ValueOf(pattern)
repl := reflect.ValueOf(replace)
- var f func(val reflect.Value) reflect.Value // f is recursive
- f = func(val reflect.Value) reflect.Value {
+
+ var rewriteVal func(val reflect.Value) reflect.Value
+ rewriteVal = func(val reflect.Value) reflect.Value {
// don't bother if val is invalid to start with
if !val.IsValid() {
return reflect.Value{}
@@ -68,22 +69,22 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File {
for k := range m {
delete(m, k)
}
- val = apply(f, val)
+ val = apply(rewriteVal, val)
if match(m, pat, val) {
val = subst(m, repl, reflect.ValueOf(val.Interface().(ast.Node).Pos()))
}
return val
}
- r := apply(f, reflect.ValueOf(p)).Interface().(*ast.File)
+
+ r := apply(rewriteVal, reflect.ValueOf(p)).Interface().(*ast.File)
r.Comments = cmap.Filter(r).Comments() // recreate comments list
return r
}
-// setValue is a wrapper for x.SetValue(y); it protects
-// the caller from panics if x cannot be changed to y.
-func setValue(x, y reflect.Value) {
- // don't bother if y is invalid to start with
- if !y.IsValid() {
+// set is a wrapper for x.Set(y); it protects the caller from panics if x cannot be changed to y.
+func set(x, y reflect.Value) {
+ // don't bother if x cannot be set or y is invalid
+ if !x.CanSet() || !y.IsValid() {
return
}
defer func() {
@@ -134,16 +135,16 @@ func apply(f func(reflect.Value) reflect.Value, val reflect.Value) reflect.Value
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
e := v.Index(i)
- setValue(e, f(e))
+ set(e, f(e))
}
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
e := v.Field(i)
- setValue(e, f(e))
+ set(e, f(e))
}
case reflect.Interface:
e := v.Elem()
- setValue(v, f(e))
+ set(v, f(e))
}
return val
}
diff --git a/src/cmd/gofmt/testdata/typeswitch.golden b/src/cmd/gofmt/testdata/typeswitch.golden
index 87e916181..2b1905edd 100644
--- a/src/cmd/gofmt/testdata/typeswitch.golden
+++ b/src/cmd/gofmt/testdata/typeswitch.golden
@@ -4,7 +4,7 @@
into the correct unparenthesized form.
Only type-switches that didn't declare a variable
- in the the type switch type assertion and which
+ in the type switch type assertion and which
contained only "expression-like" (named) types in their
cases were permitted to have their type assertion parenthesized
by go/parser (due to a weak predicate in the parser). All others
diff --git a/src/cmd/gofmt/testdata/typeswitch.input b/src/cmd/gofmt/testdata/typeswitch.input
index f90f28949..8f8cba9b8 100644
--- a/src/cmd/gofmt/testdata/typeswitch.input
+++ b/src/cmd/gofmt/testdata/typeswitch.input
@@ -4,7 +4,7 @@
into the correct unparenthesized form.
Only type-switches that didn't declare a variable
- in the the type switch type assertion and which
+ in the type switch type assertion and which
contained only "expression-like" (named) types in their
cases were permitted to have their type assertion parenthesized
by go/parser (due to a weak predicate in the parser). All others