1// Copyright 2009 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
5package main
6
7import (
8	"bytes"
9	"flag"
10	"fmt"
11	"go/ast"
12	"go/parser"
13	"go/printer"
14	"go/scanner"
15	"go/token"
16	"io"
17	"io/ioutil"
18	"os"
19	"os/exec"
20	"path/filepath"
21	"runtime/pprof"
22	"strings"
23)
24
25var (
26	// main operation modes
27	list        = flag.Bool("l", false, "list files whose formatting differs from gofmt's")
28	write       = flag.Bool("w", false, "write result to (source) file instead of stdout")
29	rewriteRule = flag.String("r", "", "rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')")
30	simplifyAST = flag.Bool("s", false, "simplify code")
31	doDiff      = flag.Bool("d", false, "display diffs instead of rewriting files")
32	allErrors   = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")
33
34	// debugging
35	cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
36)
37
38const (
39	tabWidth    = 8
40	printerMode = printer.UseSpaces | printer.TabIndent
41)
42
43var (
44	fileSet    = token.NewFileSet() // per process FileSet
45	exitCode   = 0
46	rewrite    func(*ast.File) *ast.File
47	parserMode parser.Mode
48)
49
50func report(err error) {
51	scanner.PrintError(os.Stderr, err)
52	exitCode = 2
53}
54
55func usage() {
56	fmt.Fprintf(os.Stderr, "usage: gofmt [flags] [path ...]\n")
57	flag.PrintDefaults()
58	os.Exit(2)
59}
60
61func initParserMode() {
62	parserMode = parser.ParseComments
63	if *allErrors {
64		parserMode |= parser.AllErrors
65	}
66}
67
68func isGoFile(f os.FileInfo) bool {
69	// ignore non-Go files
70	name := f.Name()
71	return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
72}
73
74// If in == nil, the source is the contents of the file with the given filename.
75func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error {
76	if in == nil {
77		f, err := os.Open(filename)
78		if err != nil {
79			return err
80		}
81		defer f.Close()
82		in = f
83	}
84
85	src, err := ioutil.ReadAll(in)
86	if err != nil {
87		return err
88	}
89
90	file, sourceAdj, indentAdj, err := parse(fileSet, filename, src, stdin)
91	if err != nil {
92		return err
93	}
94
95	if rewrite != nil {
96		if sourceAdj == nil {
97			file = rewrite(file)
98		} else {
99			fmt.Fprintf(os.Stderr, "warning: rewrite ignored for incomplete programs\n")
100		}
101	}
102
103	ast.SortImports(fileSet, file)
104
105	if *simplifyAST {
106		simplify(file)
107	}
108
109	res, err := format(fileSet, file, sourceAdj, indentAdj, src, printer.Config{Mode: printerMode, Tabwidth: tabWidth})
110	if err != nil {
111		return err
112	}
113
114	if !bytes.Equal(src, res) {
115		// formatting has changed
116		if *list {
117			fmt.Fprintln(out, filename)
118		}
119		if *write {
120			err = ioutil.WriteFile(filename, res, 0644)
121			if err != nil {
122				return err
123			}
124		}
125		if *doDiff {
126			data, err := diff(src, res)
127			if err != nil {
128				return fmt.Errorf("computing diff: %s", err)
129			}
130			fmt.Printf("diff %s gofmt/%s\n", filename, filename)
131			out.Write(data)
132		}
133	}
134
135	if !*list && !*write && !*doDiff {
136		_, err = out.Write(res)
137	}
138
139	return err
140}
141
142func visitFile(path string, f os.FileInfo, err error) error {
143	if err == nil && isGoFile(f) {
144		err = processFile(path, nil, os.Stdout, false)
145	}
146	if err != nil {
147		report(err)
148	}
149	return nil
150}
151
152func walkDir(path string) {
153	filepath.Walk(path, visitFile)
154}
155
156func main() {
157	// call gofmtMain in a separate function
158	// so that it can use defer and have them
159	// run before the exit.
160	gofmtMain()
161	os.Exit(exitCode)
162}
163
164func gofmtMain() {
165	flag.Usage = usage
166	flag.Parse()
167
168	if *cpuprofile != "" {
169		f, err := os.Create(*cpuprofile)
170		if err != nil {
171			fmt.Fprintf(os.Stderr, "creating cpu profile: %s\n", err)
172			exitCode = 2
173			return
174		}
175		defer f.Close()
176		pprof.StartCPUProfile(f)
177		defer pprof.StopCPUProfile()
178	}
179
180	initParserMode()
181	initRewrite()
182
183	if flag.NArg() == 0 {
184		if *write {
185			fmt.Fprintln(os.Stderr, "error: cannot use -w with standard input")
186			exitCode = 2
187			return
188		}
189		if err := processFile("<standard input>", os.Stdin, os.Stdout, true); err != nil {
190			report(err)
191		}
192		return
193	}
194
195	for i := 0; i < flag.NArg(); i++ {
196		path := flag.Arg(i)
197		switch dir, err := os.Stat(path); {
198		case err != nil:
199			report(err)
200		case dir.IsDir():
201			walkDir(path)
202		default:
203			if err := processFile(path, nil, os.Stdout, false); err != nil {
204				report(err)
205			}
206		}
207	}
208}
209
210func diff(b1, b2 []byte) (data []byte, err error) {
211	f1, err := ioutil.TempFile("", "gofmt")
212	if err != nil {
213		return
214	}
215	defer os.Remove(f1.Name())
216	defer f1.Close()
217
218	f2, err := ioutil.TempFile("", "gofmt")
219	if err != nil {
220		return
221	}
222	defer os.Remove(f2.Name())
223	defer f2.Close()
224
225	f1.Write(b1)
226	f2.Write(b2)
227
228	data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
229	if len(data) > 0 {
230		// diff exits with a non-zero status when the files don't match.
231		// Ignore that failure as long as we get output.
232		err = nil
233	}
234	return
235
236}
237