github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/gofmt/gofmt.go (about)

     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  
     5  package gofmt
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"flag"
    11  	"fmt"
    12  	"go/ast"
    13  	"go/parser"
    14  	"go/printer"
    15  	"go/scanner"
    16  	"go/token"
    17  	"io"
    18  	"io/fs"
    19  	"math/rand"
    20  	"os"
    21  	"path/filepath"
    22  	"runtime"
    23  	"runtime/pprof"
    24  	"strconv"
    25  	"strings"
    26  
    27  	"github.com/go-asm/go/diff"
    28  
    29  	"golang.org/x/sync/semaphore"
    30  )
    31  
    32  var (
    33  	// main operation modes
    34  	list        = flag.Bool("l", false, "list files whose formatting differs from gofmt's")
    35  	write       = flag.Bool("w", false, "write result to (source) file instead of stdout")
    36  	rewriteRule = flag.String("r", "", "rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')")
    37  	simplifyAST = flag.Bool("s", false, "simplify code")
    38  	doDiff      = flag.Bool("d", false, "display diffs instead of rewriting files")
    39  	allErrors   = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")
    40  
    41  	// debugging
    42  	cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
    43  )
    44  
    45  // Keep these in sync with go/format/format.go.
    46  const (
    47  	tabWidth    = 8
    48  	printerMode = printer.UseSpaces | printer.TabIndent | printerNormalizeNumbers
    49  
    50  	// printerNormalizeNumbers means to canonicalize number literal prefixes
    51  	// and exponents while printing. See https://golang.org/doc/go1.13#gofmt.
    52  	//
    53  	// This value is defined in go/printer specifically for go/format and cmd/gofmt.
    54  	printerNormalizeNumbers = 1 << 30
    55  )
    56  
    57  // fdSem guards the number of concurrently-open file descriptors.
    58  //
    59  // For now, this is arbitrarily set to 200, based on the observation that many
    60  // platforms default to a kernel limit of 256. Ideally, perhaps we should derive
    61  // it from rlimit on platforms that support that system call.
    62  //
    63  // File descriptors opened from outside of this package are not tracked,
    64  // so this limit may be approximate.
    65  var fdSem = make(chan bool, 200)
    66  
    67  var (
    68  	rewrite    func(*token.FileSet, *ast.File) *ast.File
    69  	parserMode parser.Mode
    70  )
    71  
    72  func usage() {
    73  	fmt.Fprintf(os.Stderr, "usage: gofmt [flags] [path ...]\n")
    74  	flag.PrintDefaults()
    75  }
    76  
    77  func initParserMode() {
    78  	parserMode = parser.ParseComments
    79  	if *allErrors {
    80  		parserMode |= parser.AllErrors
    81  	}
    82  	// It's only -r that makes use of go/ast's object resolution,
    83  	// so avoid the unnecessary work if the flag isn't used.
    84  	if *rewriteRule == "" {
    85  		parserMode |= parser.SkipObjectResolution
    86  	}
    87  }
    88  
    89  func isGoFile(f fs.DirEntry) bool {
    90  	// ignore non-Go files
    91  	name := f.Name()
    92  	return !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && !f.IsDir()
    93  }
    94  
    95  // A sequencer performs concurrent tasks that may write output, but emits that
    96  // output in a deterministic order.
    97  type sequencer struct {
    98  	maxWeight int64
    99  	sem       *semaphore.Weighted   // weighted by input bytes (an approximate proxy for memory overhead)
   100  	prev      <-chan *reporterState // 1-buffered
   101  }
   102  
   103  // newSequencer returns a sequencer that allows concurrent tasks up to maxWeight
   104  // and writes tasks' output to out and err.
   105  func newSequencer(maxWeight int64, out, err io.Writer) *sequencer {
   106  	sem := semaphore.NewWeighted(maxWeight)
   107  	prev := make(chan *reporterState, 1)
   108  	prev <- &reporterState{out: out, err: err}
   109  	return &sequencer{
   110  		maxWeight: maxWeight,
   111  		sem:       sem,
   112  		prev:      prev,
   113  	}
   114  }
   115  
   116  // exclusive is a weight that can be passed to a sequencer to cause
   117  // a task to be executed without any other concurrent tasks.
   118  const exclusive = -1
   119  
   120  // Add blocks until the sequencer has enough weight to spare, then adds f as a
   121  // task to be executed concurrently.
   122  //
   123  // If the weight is either negative or larger than the sequencer's maximum
   124  // weight, Add blocks until all other tasks have completed, then the task
   125  // executes exclusively (blocking all other calls to Add until it completes).
   126  //
   127  // f may run concurrently in a goroutine, but its output to the passed-in
   128  // reporter will be sequential relative to the other tasks in the sequencer.
   129  //
   130  // If f invokes a method on the reporter, execution of that method may block
   131  // until the previous task has finished. (To maximize concurrency, f should
   132  // avoid invoking the reporter until it has finished any parallelizable work.)
   133  //
   134  // If f returns a non-nil error, that error will be reported after f's output
   135  // (if any) and will cause a nonzero final exit code.
   136  func (s *sequencer) Add(weight int64, f func(*reporter) error) {
   137  	if weight < 0 || weight > s.maxWeight {
   138  		weight = s.maxWeight
   139  	}
   140  	if err := s.sem.Acquire(context.TODO(), weight); err != nil {
   141  		// Change the task from "execute f" to "report err".
   142  		weight = 0
   143  		f = func(*reporter) error { return err }
   144  	}
   145  
   146  	r := &reporter{prev: s.prev}
   147  	next := make(chan *reporterState, 1)
   148  	s.prev = next
   149  
   150  	// Start f in parallel: it can run until it invokes a method on r, at which
   151  	// point it will block until the previous task releases the output state.
   152  	go func() {
   153  		if err := f(r); err != nil {
   154  			r.Report(err)
   155  		}
   156  		next <- r.getState() // Release the next task.
   157  		s.sem.Release(weight)
   158  	}()
   159  }
   160  
   161  // AddReport prints an error to s after the output of any previously-added
   162  // tasks, causing the final exit code to be nonzero.
   163  func (s *sequencer) AddReport(err error) {
   164  	s.Add(0, func(*reporter) error { return err })
   165  }
   166  
   167  // GetExitCode waits for all previously-added tasks to complete, then returns an
   168  // exit code for the sequence suitable for passing to os.Exit.
   169  func (s *sequencer) GetExitCode() int {
   170  	c := make(chan int, 1)
   171  	s.Add(0, func(r *reporter) error {
   172  		c <- r.ExitCode()
   173  		return nil
   174  	})
   175  	return <-c
   176  }
   177  
   178  // A reporter reports output, warnings, and errors.
   179  type reporter struct {
   180  	prev  <-chan *reporterState
   181  	state *reporterState
   182  }
   183  
   184  // reporterState carries the state of a reporter instance.
   185  //
   186  // Only one reporter at a time may have access to a reporterState.
   187  type reporterState struct {
   188  	out, err io.Writer
   189  	exitCode int
   190  }
   191  
   192  // getState blocks until any prior reporters are finished with the reporter
   193  // state, then returns the state for manipulation.
   194  func (r *reporter) getState() *reporterState {
   195  	if r.state == nil {
   196  		r.state = <-r.prev
   197  	}
   198  	return r.state
   199  }
   200  
   201  // Warnf emits a warning message to the reporter's error stream,
   202  // without changing its exit code.
   203  func (r *reporter) Warnf(format string, args ...any) {
   204  	fmt.Fprintf(r.getState().err, format, args...)
   205  }
   206  
   207  // Write emits a slice to the reporter's output stream.
   208  //
   209  // Any error is returned to the caller, and does not otherwise affect the
   210  // reporter's exit code.
   211  func (r *reporter) Write(p []byte) (int, error) {
   212  	return r.getState().out.Write(p)
   213  }
   214  
   215  // Report emits a non-nil error to the reporter's error stream,
   216  // changing its exit code to a nonzero value.
   217  func (r *reporter) Report(err error) {
   218  	if err == nil {
   219  		panic("Report with nil error")
   220  	}
   221  	st := r.getState()
   222  	scanner.PrintError(st.err, err)
   223  	st.exitCode = 2
   224  }
   225  
   226  func (r *reporter) ExitCode() int {
   227  	return r.getState().exitCode
   228  }
   229  
   230  // If info == nil, we are formatting stdin instead of a file.
   231  // If in == nil, the source is the contents of the file with the given filename.
   232  func processFile(filename string, info fs.FileInfo, in io.Reader, r *reporter) error {
   233  	src, err := readFile(filename, info, in)
   234  	if err != nil {
   235  		return err
   236  	}
   237  
   238  	fileSet := token.NewFileSet()
   239  	// If we are formatting stdin, we accept a program fragment in lieu of a
   240  	// complete source file.
   241  	fragmentOk := info == nil
   242  	file, sourceAdj, indentAdj, err := parse(fileSet, filename, src, fragmentOk)
   243  	if err != nil {
   244  		return err
   245  	}
   246  
   247  	if rewrite != nil {
   248  		if sourceAdj == nil {
   249  			file = rewrite(fileSet, file)
   250  		} else {
   251  			r.Warnf("warning: rewrite ignored for incomplete programs\n")
   252  		}
   253  	}
   254  
   255  	ast.SortImports(fileSet, file)
   256  
   257  	if *simplifyAST {
   258  		simplify(file)
   259  	}
   260  
   261  	res, err := format(fileSet, file, sourceAdj, indentAdj, src, printer.Config{Mode: printerMode, Tabwidth: tabWidth})
   262  	if err != nil {
   263  		return err
   264  	}
   265  
   266  	if !bytes.Equal(src, res) {
   267  		// formatting has changed
   268  		if *list {
   269  			fmt.Fprintln(r, filename)
   270  		}
   271  		if *write {
   272  			if info == nil {
   273  				panic("-w should not have been allowed with stdin")
   274  			}
   275  
   276  			perm := info.Mode().Perm()
   277  			if err := writeFile(filename, src, res, perm, info.Size()); err != nil {
   278  				return err
   279  			}
   280  		}
   281  		if *doDiff {
   282  			newName := filepath.ToSlash(filename)
   283  			oldName := newName + ".orig"
   284  			r.Write(diff.Diff(oldName, src, newName, res))
   285  		}
   286  	}
   287  
   288  	if !*list && !*write && !*doDiff {
   289  		_, err = r.Write(res)
   290  	}
   291  
   292  	return err
   293  }
   294  
   295  // readFile reads the contents of filename, described by info.
   296  // If in is non-nil, readFile reads directly from it.
   297  // Otherwise, readFile opens and reads the file itself,
   298  // with the number of concurrently-open files limited by fdSem.
   299  func readFile(filename string, info fs.FileInfo, in io.Reader) ([]byte, error) {
   300  	if in == nil {
   301  		fdSem <- true
   302  		var err error
   303  		f, err := os.Open(filename)
   304  		if err != nil {
   305  			return nil, err
   306  		}
   307  		in = f
   308  		defer func() {
   309  			f.Close()
   310  			<-fdSem
   311  		}()
   312  	}
   313  
   314  	// Compute the file's size and read its contents with minimal allocations.
   315  	//
   316  	// If we have the FileInfo from filepath.WalkDir, use it to make
   317  	// a buffer of the right size and avoid ReadAll's reallocations.
   318  	//
   319  	// If the size is unknown (or bogus, or overflows an int), fall back to
   320  	// a size-independent ReadAll.
   321  	size := -1
   322  	if info != nil && info.Mode().IsRegular() && int64(int(info.Size())) == info.Size() {
   323  		size = int(info.Size())
   324  	}
   325  	if size+1 <= 0 {
   326  		// The file is not known to be regular, so we don't have a reliable size for it.
   327  		var err error
   328  		src, err := io.ReadAll(in)
   329  		if err != nil {
   330  			return nil, err
   331  		}
   332  		return src, nil
   333  	}
   334  
   335  	// We try to read size+1 bytes so that we can detect modifications: if we
   336  	// read more than size bytes, then the file was modified concurrently.
   337  	// (If that happens, we could, say, append to src to finish the read, or
   338  	// proceed with a truncated buffer — but the fact that it changed at all
   339  	// indicates a possible race with someone editing the file, so we prefer to
   340  	// stop to avoid corrupting it.)
   341  	src := make([]byte, size+1)
   342  	n, err := io.ReadFull(in, src)
   343  	switch err {
   344  	case nil, io.EOF, io.ErrUnexpectedEOF:
   345  		// io.ReadFull returns io.EOF (for an empty file) or io.ErrUnexpectedEOF
   346  		// (for a non-empty file) if the file was changed unexpectedly. Continue
   347  		// with comparing file sizes in those cases.
   348  	default:
   349  		return nil, err
   350  	}
   351  	if n < size {
   352  		return nil, fmt.Errorf("error: size of %s changed during reading (from %d to %d bytes)", filename, size, n)
   353  	} else if n > size {
   354  		return nil, fmt.Errorf("error: size of %s changed during reading (from %d to >=%d bytes)", filename, size, len(src))
   355  	}
   356  	return src[:n], nil
   357  }
   358  
   359  func main() {
   360  	// Arbitrarily limit in-flight work to 2MiB times the number of threads.
   361  	//
   362  	// The actual overhead for the parse tree and output will depend on the
   363  	// specifics of the file, but this at least keeps the footprint of the process
   364  	// roughly proportional to GOMAXPROCS.
   365  	maxWeight := (2 << 20) * int64(runtime.GOMAXPROCS(0))
   366  	s := newSequencer(maxWeight, os.Stdout, os.Stderr)
   367  
   368  	// call gofmtMain in a separate function
   369  	// so that it can use defer and have them
   370  	// run before the exit.
   371  	gofmtMain(s)
   372  	os.Exit(s.GetExitCode())
   373  }
   374  
   375  func gofmtMain(s *sequencer) {
   376  	flag.Usage = usage
   377  	flag.Parse()
   378  
   379  	if *cpuprofile != "" {
   380  		fdSem <- true
   381  		f, err := os.Create(*cpuprofile)
   382  		if err != nil {
   383  			s.AddReport(fmt.Errorf("creating cpu profile: %s", err))
   384  			return
   385  		}
   386  		defer func() {
   387  			f.Close()
   388  			<-fdSem
   389  		}()
   390  		pprof.StartCPUProfile(f)
   391  		defer pprof.StopCPUProfile()
   392  	}
   393  
   394  	initParserMode()
   395  	initRewrite()
   396  
   397  	args := flag.Args()
   398  	if len(args) == 0 {
   399  		if *write {
   400  			s.AddReport(fmt.Errorf("error: cannot use -w with standard input"))
   401  			return
   402  		}
   403  		s.Add(0, func(r *reporter) error {
   404  			return processFile("<standard input>", nil, os.Stdin, r)
   405  		})
   406  		return
   407  	}
   408  
   409  	for _, arg := range args {
   410  		switch info, err := os.Stat(arg); {
   411  		case err != nil:
   412  			s.AddReport(err)
   413  		case !info.IsDir():
   414  			// Non-directory arguments are always formatted.
   415  			arg := arg
   416  			s.Add(fileWeight(arg, info), func(r *reporter) error {
   417  				return processFile(arg, info, nil, r)
   418  			})
   419  		default:
   420  			// Directories are walked, ignoring non-Go files.
   421  			err := filepath.WalkDir(arg, func(path string, f fs.DirEntry, err error) error {
   422  				if err != nil || !isGoFile(f) {
   423  					return err
   424  				}
   425  				info, err := f.Info()
   426  				if err != nil {
   427  					s.AddReport(err)
   428  					return nil
   429  				}
   430  				s.Add(fileWeight(path, info), func(r *reporter) error {
   431  					return processFile(path, info, nil, r)
   432  				})
   433  				return nil
   434  			})
   435  			if err != nil {
   436  				s.AddReport(err)
   437  			}
   438  		}
   439  	}
   440  }
   441  
   442  func fileWeight(path string, info fs.FileInfo) int64 {
   443  	if info == nil {
   444  		return exclusive
   445  	}
   446  	if info.Mode().Type() == fs.ModeSymlink {
   447  		var err error
   448  		info, err = os.Stat(path)
   449  		if err != nil {
   450  			return exclusive
   451  		}
   452  	}
   453  	if !info.Mode().IsRegular() {
   454  		// For non-regular files, FileInfo.Size is system-dependent and thus not a
   455  		// reliable indicator of weight.
   456  		return exclusive
   457  	}
   458  	return info.Size()
   459  }
   460  
   461  // writeFile updates a file with the new formatted data.
   462  func writeFile(filename string, orig, formatted []byte, perm fs.FileMode, size int64) error {
   463  	// Make a temporary backup file before rewriting the original file.
   464  	bakname, err := backupFile(filename, orig, perm)
   465  	if err != nil {
   466  		return err
   467  	}
   468  
   469  	fdSem <- true
   470  	defer func() { <-fdSem }()
   471  
   472  	fout, err := os.OpenFile(filename, os.O_WRONLY, perm)
   473  	if err != nil {
   474  		// We couldn't even open the file, so it should
   475  		// not have changed.
   476  		os.Remove(bakname)
   477  		return err
   478  	}
   479  	defer fout.Close() // for error paths
   480  
   481  	restoreFail := func(err error) {
   482  		fmt.Fprintf(os.Stderr, "gofmt: %s: error restoring file to original: %v; backup in %s\n", filename, err, bakname)
   483  	}
   484  
   485  	n, err := fout.Write(formatted)
   486  	if err == nil && int64(n) < size {
   487  		err = fout.Truncate(int64(n))
   488  	}
   489  
   490  	if err != nil {
   491  		// Rewriting the file failed.
   492  
   493  		if n == 0 {
   494  			// Original file unchanged.
   495  			os.Remove(bakname)
   496  			return err
   497  		}
   498  
   499  		// Try to restore the original contents.
   500  
   501  		no, erro := fout.WriteAt(orig, 0)
   502  		if erro != nil {
   503  			// That failed too.
   504  			restoreFail(erro)
   505  			return err
   506  		}
   507  
   508  		if no < n {
   509  			// Original file is shorter. Truncate.
   510  			if erro = fout.Truncate(int64(no)); erro != nil {
   511  				restoreFail(erro)
   512  				return err
   513  			}
   514  		}
   515  
   516  		if erro := fout.Close(); erro != nil {
   517  			restoreFail(erro)
   518  			return err
   519  		}
   520  
   521  		// Original contents restored.
   522  		os.Remove(bakname)
   523  		return err
   524  	}
   525  
   526  	if err := fout.Close(); err != nil {
   527  		restoreFail(err)
   528  		return err
   529  	}
   530  
   531  	// File updated.
   532  	os.Remove(bakname)
   533  	return nil
   534  }
   535  
   536  // backupFile writes data to a new file named filename<number> with permissions perm,
   537  // with <number> randomly chosen such that the file name is unique. backupFile returns
   538  // the chosen file name.
   539  func backupFile(filename string, data []byte, perm fs.FileMode) (string, error) {
   540  	fdSem <- true
   541  	defer func() { <-fdSem }()
   542  
   543  	nextRandom := func() string {
   544  		return strconv.Itoa(rand.Int())
   545  	}
   546  
   547  	dir, base := filepath.Split(filename)
   548  	var (
   549  		bakname string
   550  		f       *os.File
   551  	)
   552  	for {
   553  		bakname = filepath.Join(dir, base+"."+nextRandom())
   554  		var err error
   555  		f, err = os.OpenFile(bakname, os.O_RDWR|os.O_CREATE|os.O_EXCL, perm)
   556  		if err == nil {
   557  			break
   558  		}
   559  		if err != nil && !os.IsExist(err) {
   560  			return "", err
   561  		}
   562  	}
   563  
   564  	// write data to backup file
   565  	_, err := f.Write(data)
   566  	if err1 := f.Close(); err == nil {
   567  		err = err1
   568  	}
   569  
   570  	return bakname, err
   571  }