github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/ssa/mode.go (about)

     1  // Copyright 2015 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 ssa
     6  
     7  // This file defines the BuilderMode type and its command-line flag.
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  )
    13  
    14  // BuilderMode is a bitmask of options for diagnostics and checking.
    15  //
    16  // *BuilderMode satisfies the flag.Value interface.  Example:
    17  //
    18  // 	var mode = ssa.BuilderMode(0)
    19  // 	func init() { flag.Var(&mode, "build", ssa.BuilderModeDoc) }
    20  //
    21  type BuilderMode uint
    22  
    23  const (
    24  	PrintPackages        BuilderMode = 1 << iota // Print package inventory to stdout
    25  	PrintFunctions                               // Print function SSA code to stdout
    26  	LogSource                                    // Log source locations as SSA builder progresses
    27  	SanityCheckFunctions                         // Perform sanity checking of function bodies
    28  	NaiveForm                                    // Build naïve SSA form: don't replace local loads/stores with registers
    29  	BuildSerially                                // Build packages serially, not in parallel.
    30  	GlobalDebug                                  // Enable debug info for all packages
    31  	BareInits                                    // Build init functions without guards or calls to dependent inits
    32  )
    33  
    34  const BuilderModeDoc = `Options controlling the SSA builder.
    35  The value is a sequence of zero or more of these letters:
    36  C	perform sanity [C]hecking of the SSA form.
    37  D	include [D]ebug info for every function.
    38  P	print [P]ackage inventory.
    39  F	print [F]unction SSA code.
    40  S	log [S]ource locations as SSA builder progresses.
    41  L	build distinct packages seria[L]ly instead of in parallel.
    42  N	build [N]aive SSA form: don't replace local loads/stores with registers.
    43  I	build bare [I]nit functions: no init guards or calls to dependent inits.
    44  `
    45  
    46  func (m BuilderMode) String() string {
    47  	var buf bytes.Buffer
    48  	if m&GlobalDebug != 0 {
    49  		buf.WriteByte('D')
    50  	}
    51  	if m&PrintPackages != 0 {
    52  		buf.WriteByte('P')
    53  	}
    54  	if m&PrintFunctions != 0 {
    55  		buf.WriteByte('F')
    56  	}
    57  	if m&LogSource != 0 {
    58  		buf.WriteByte('S')
    59  	}
    60  	if m&SanityCheckFunctions != 0 {
    61  		buf.WriteByte('C')
    62  	}
    63  	if m&NaiveForm != 0 {
    64  		buf.WriteByte('N')
    65  	}
    66  	if m&BuildSerially != 0 {
    67  		buf.WriteByte('L')
    68  	}
    69  	return buf.String()
    70  }
    71  
    72  // Set parses the flag characters in s and updates *m.
    73  func (m *BuilderMode) Set(s string) error {
    74  	var mode BuilderMode
    75  	for _, c := range s {
    76  		switch c {
    77  		case 'D':
    78  			mode |= GlobalDebug
    79  		case 'P':
    80  			mode |= PrintPackages
    81  		case 'F':
    82  			mode |= PrintFunctions
    83  		case 'S':
    84  			mode |= LogSource | BuildSerially
    85  		case 'C':
    86  			mode |= SanityCheckFunctions
    87  		case 'N':
    88  			mode |= NaiveForm
    89  		case 'L':
    90  			mode |= BuildSerially
    91  		default:
    92  			return fmt.Errorf("unknown BuilderMode option: %q", c)
    93  		}
    94  	}
    95  	*m = mode
    96  	return nil
    97  }
    98  
    99  // Get returns m.
   100  func (m BuilderMode) Get() interface{} { return m }