gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/aligncheck/aligncheck.go (about)

     1  // This program is free software: you can redistribute it and/or modify
     2  // it under the terms of the GNU General Public License as published by
     3  // the Free Software Foundation, either version 3 of the License, or
     4  // (at your option) any later version.
     5  //
     6  // This program is distributed in the hope that it will be useful,
     7  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     8  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     9  // GNU General Public License for more details.
    10  //
    11  // You should have received a copy of the GNU General Public License
    12  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    13  
    14  package aligncheck
    15  
    16  import (
    17  	"fmt"
    18  	"go/build"
    19  	"log"
    20  	"sort"
    21  	"unsafe"
    22  
    23  	"go/types"
    24  	"golang.org/x/tools/go/loader"
    25  )
    26  
    27  type LinterAligncheck struct {
    28  }
    29  
    30  func (l *LinterAligncheck) ComputeMetric(projectPath string) []string {
    31  	importPaths := []string{projectPath}
    32  	if len(importPaths) == 0 {
    33  		importPaths = []string{"."}
    34  	}
    35  
    36  	ctx := build.Default
    37  	loadcfg := loader.Config{
    38  		Build: &ctx,
    39  	}
    40  	rest, err := loadcfg.FromArgs(importPaths, false)
    41  	if err != nil {
    42  		log.Fatalf("could not parse arguments: %s", err)
    43  	}
    44  	if len(rest) > 0 {
    45  		log.Fatalf("unhandled extra arguments: %v", rest)
    46  	}
    47  
    48  	program, err := loadcfg.Load()
    49  	if err != nil {
    50  		log.Fatalf("could not type check: %s", err)
    51  	}
    52  
    53  	var lines []string
    54  
    55  	for _, pkgInfo := range program.InitialPackages() {
    56  		for _, obj := range pkgInfo.Defs {
    57  			if obj == nil {
    58  				continue
    59  			}
    60  
    61  			if _, ok := obj.(*types.TypeName); !ok {
    62  				continue
    63  			}
    64  
    65  			typ, ok := obj.Type().(*types.Named)
    66  			if !ok {
    67  				continue
    68  			}
    69  
    70  			strukt, ok := typ.Underlying().(*types.Struct)
    71  			if !ok {
    72  				continue
    73  			}
    74  
    75  			structAlign := int(stdSizes.Alignof(strukt))
    76  			structSize := int(stdSizes.Sizeof(strukt))
    77  			if structSize%structAlign != 0 {
    78  				structSize += structAlign - structSize%structAlign
    79  			}
    80  
    81  			minSize := 0
    82  			for i := 0; i < strukt.NumFields(); i++ {
    83  				field := strukt.Field(i)
    84  				fieldType := field.Type()
    85  				typeSize := int(stdSizes.Sizeof(fieldType))
    86  				minSize += typeSize
    87  			}
    88  			if minSize%structAlign != 0 {
    89  				minSize += structAlign - minSize%structAlign
    90  			}
    91  
    92  			if minSize != structSize {
    93  				pos := program.Fset.Position(obj.Pos())
    94  				lines = append(lines, fmt.Sprintf(
    95  					"%s: %s:%d:%d: struct %s could have size %d (currently %d)",
    96  					obj.Pkg().Path(),
    97  					pos.Filename,
    98  					pos.Line,
    99  					pos.Column,
   100  					obj.Name(),
   101  					minSize,
   102  					structSize,
   103  				))
   104  			}
   105  		}
   106  	}
   107  
   108  	sort.Strings(lines)
   109  	// for _, line := range lines {
   110  	// 	fmt.Println(line)
   111  	// }
   112  	return lines
   113  	// os.Exit(exitStatus)
   114  }
   115  
   116  var stdSizes = types.StdSizes{
   117  	WordSize: int64(unsafe.Sizeof(int(0))),
   118  	MaxAlign: 8,
   119  }