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