[
  {
    "path": "LICENSE",
    "content": "Copyright (c) 2012 The Go Authors. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n   * Redistributions of source code must retain the above copyright\nnotice, this list of conditions and the following disclaimer.\n   * Redistributions in binary form must reproduce the above\ncopyright notice, this list of conditions and the following disclaimer\nin the documentation and/or other materials provided with the\ndistribution.\n   * Neither the name of Google Inc. nor the names of its\ncontributors may be used to endorse or promote products derived from\nthis software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "README.md",
    "content": "**Deprecated:** Use https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment instead.\n\nInstall:\n\n    go get github.com/mdempsky/maligned\n\nUsage:\n\n    maligned cmd/compile/internal/gc cmd/link/internal/ld\n"
  },
  {
    "path": "go.mod",
    "content": "module github.com/mdempsky/maligned\n\ngo 1.15\n\nrequire (\n\tgolang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect\n\tgolang.org/x/tools v0.1.9\n)\n"
  },
  {
    "path": "go.sum",
    "content": "github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=\ngolang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=\ngolang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=\ngolang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=\ngolang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=\ngolang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=\ngolang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=\ngolang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=\ngolang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=\ngolang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=\ngolang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=\ngolang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=\ngolang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=\ngolang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=\ngolang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=\ngolang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=\ngolang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=\ngolang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=\ngolang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=\ngolang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=\ngolang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=\ngolang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=\ngolang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=\ngolang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8=\ngolang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=\ngolang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=\ngolang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=\ngolang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=\ngolang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=\n"
  },
  {
    "path": "maligned.go",
    "content": "// Copyright 2013 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\npackage main\n\nimport (\n\t\"flag\"\n\n\t\"github.com/mdempsky/maligned/passes/maligned\"\n\t\"golang.org/x/tools/go/analysis/singlechecker\"\n)\n\nfunc main() {\n\tflag.Parse()\n\tsinglechecker.Main(maligned.Analyzer)\n}\n"
  },
  {
    "path": "passes/maligned/analyzer.go",
    "content": "package maligned\n\nimport (\n\t\"go/ast\"\n\t\"go/token\"\n\t\"go/types\"\n\t\"sort\"\n\n\t\"golang.org/x/tools/go/analysis\"\n\t\"golang.org/x/tools/go/analysis/passes/inspect\"\n\t\"golang.org/x/tools/go/ast/inspector\"\n)\n\nconst Doc = \"TODO\"\n\nvar Analyzer = &analysis.Analyzer{\n\tName:     \"maligned\",\n\tDoc:      Doc,\n\tRequires: []*analysis.Analyzer{inspect.Analyzer},\n\tRun:      run,\n}\n\nfunc run(pass *analysis.Pass) (interface{}, error) {\n\tinspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)\n\tnodeFilter := []ast.Node{\n\t\t(*ast.StructType)(nil),\n\t}\n\tinspect.Preorder(nodeFilter, func(node ast.Node) {\n\t\tif s, ok := node.(*ast.StructType); ok {\n\t\t\tmalign(pass, node.Pos(), pass.TypesInfo.Types[s].Type.(*types.Struct))\n\t\t}\n\t})\n\treturn nil, nil\n}\n\nvar unsafePointerTyp = types.Unsafe.Scope().Lookup(\"Pointer\").(*types.TypeName).Type()\n\nfunc malign(pass *analysis.Pass, pos token.Pos, str *types.Struct) {\n\twordSize := pass.TypesSizes.Sizeof(unsafePointerTyp)\n\tmaxAlign := pass.TypesSizes.Alignof(unsafePointerTyp)\n\n\ts := gcSizes{wordSize, maxAlign}\n\toptsz, optptrs := optimalOrder(str, &s)\n\n\tif sz := s.Sizeof(str); sz != optsz {\n\t\tpass.Reportf(pos, \"struct of size %d could be %d\", sz, optsz)\n\t}\n\tif ptrs := s.ptrdata(str); ptrs != optptrs {\n\t\tpass.Reportf(pos, \"struct with %d pointer bytes could be %d\", ptrs, optptrs)\n\t}\n}\n\nfunc optimalOrder(str *types.Struct, sizes *gcSizes) (int64, int64) {\n\tnf := str.NumFields()\n\n\ttype elem struct {\n\t\tfield   *types.Var\n\t\talignof int64\n\t\tsizeof  int64\n\t\tptrdata int64\n\t}\n\n\telems := make([]elem, nf)\n\tfor i := 0; i < nf; i++ {\n\t\tfield := str.Field(i)\n\t\tft := field.Type()\n\t\telems[i] = elem{\n\t\t\tfield,\n\t\t\tsizes.Alignof(ft),\n\t\t\tsizes.Sizeof(ft),\n\t\t\tsizes.ptrdata(ft),\n\t\t}\n\t}\n\n\tsort.Slice(elems, func(i, j int) bool {\n\t\tei := &elems[i]\n\t\tej := &elems[j]\n\n\t\t// Place zero sized objects before non-zero sized objects.\n\t\tzeroi := ei.sizeof == 0\n\t\tzeroj := ej.sizeof == 0\n\t\tif zeroi != zeroj {\n\t\t\treturn zeroi\n\t\t}\n\n\t\t// Next, place more tightly aligned objects before less tightly aligned objects.\n\t\tif ei.alignof != ej.alignof {\n\t\t\treturn ei.alignof > ej.alignof\n\t\t}\n\n\t\t// Place pointerful objects before pointer-free objects.\n\t\tnoptrsi := ei.ptrdata == 0\n\t\tnoptrsj := ej.ptrdata == 0\n\t\tif noptrsi != noptrsj {\n\t\t\treturn noptrsj\n\t\t}\n\n\t\tif !noptrsi {\n\t\t\t// If both have pointers...\n\n\t\t\t// ... then place objects with less trailing\n\t\t\t// non-pointer bytes earlier. That is, place\n\t\t\t// the field with the most trailing\n\t\t\t// non-pointer bytes at the end of the\n\t\t\t// pointerful section.\n\t\t\ttraili := ei.sizeof - ei.ptrdata\n\t\t\ttrailj := ej.sizeof - ej.ptrdata\n\t\t\tif traili != trailj {\n\t\t\t\treturn traili < trailj\n\t\t\t}\n\t\t}\n\n\t\t// Lastly, order by size.\n\t\tif ei.sizeof != ej.sizeof {\n\t\t\treturn ei.sizeof > ej.sizeof\n\t\t}\n\n\t\treturn false\n\t})\n\n\tvar fields []*types.Var\n\tfor _, e := range elems {\n\t\tfields = append(fields, e.field)\n\t}\n\toptimal := types.NewStruct(fields, nil)\n\n\treturn sizes.Sizeof(optimal), sizes.ptrdata(optimal)\n}\n\n// Code below based on go/types.StdSizes.\n\ntype gcSizes struct {\n\tWordSize int64\n\tMaxAlign int64\n}\n\nfunc (s *gcSizes) Alignof(T types.Type) int64 {\n\t// NOTE: On amd64, complex64 is 8 byte aligned,\n\t// even though float32 is only 4 byte aligned.\n\n\t// For arrays and structs, alignment is defined in terms\n\t// of alignment of the elements and fields, respectively.\n\tswitch t := T.Underlying().(type) {\n\tcase *types.Array:\n\t\t// spec: \"For a variable x of array type: unsafe.Alignof(x)\n\t\t// is the same as unsafe.Alignof(x[0]), but at least 1.\"\n\t\treturn s.Alignof(t.Elem())\n\tcase *types.Struct:\n\t\t// spec: \"For a variable x of struct type: unsafe.Alignof(x)\n\t\t// is the largest of the values unsafe.Alignof(x.f) for each\n\t\t// field f of x, but at least 1.\"\n\t\tmax := int64(1)\n\t\tfor i, nf := 0, t.NumFields(); i < nf; i++ {\n\t\t\tif a := s.Alignof(t.Field(i).Type()); a > max {\n\t\t\t\tmax = a\n\t\t\t}\n\t\t}\n\t\treturn max\n\t}\n\ta := s.Sizeof(T) // may be 0\n\t// spec: \"For a variable x of any type: unsafe.Alignof(x) is at least 1.\"\n\tif a < 1 {\n\t\treturn 1\n\t}\n\tif a > s.MaxAlign {\n\t\treturn s.MaxAlign\n\t}\n\treturn a\n}\n\nvar basicSizes = [...]byte{\n\ttypes.Bool:       1,\n\ttypes.Int8:       1,\n\ttypes.Int16:      2,\n\ttypes.Int32:      4,\n\ttypes.Int64:      8,\n\ttypes.Uint8:      1,\n\ttypes.Uint16:     2,\n\ttypes.Uint32:     4,\n\ttypes.Uint64:     8,\n\ttypes.Float32:    4,\n\ttypes.Float64:    8,\n\ttypes.Complex64:  8,\n\ttypes.Complex128: 16,\n}\n\nfunc (s *gcSizes) Sizeof(T types.Type) int64 {\n\tswitch t := T.Underlying().(type) {\n\tcase *types.Basic:\n\t\tk := t.Kind()\n\t\tif int(k) < len(basicSizes) {\n\t\t\tif s := basicSizes[k]; s > 0 {\n\t\t\t\treturn int64(s)\n\t\t\t}\n\t\t}\n\t\tif k == types.String {\n\t\t\treturn s.WordSize * 2\n\t\t}\n\tcase *types.Array:\n\t\treturn t.Len() * s.Sizeof(t.Elem())\n\tcase *types.Slice:\n\t\treturn s.WordSize * 3\n\tcase *types.Struct:\n\t\tnf := t.NumFields()\n\t\tif nf == 0 {\n\t\t\treturn 0\n\t\t}\n\n\t\tvar o int64\n\t\tmax := int64(1)\n\t\tfor i := 0; i < nf; i++ {\n\t\t\tft := t.Field(i).Type()\n\t\t\ta, sz := s.Alignof(ft), s.Sizeof(ft)\n\t\t\tif a > max {\n\t\t\t\tmax = a\n\t\t\t}\n\t\t\tif i == nf-1 && sz == 0 && o != 0 {\n\t\t\t\tsz = 1\n\t\t\t}\n\t\t\to = align(o, a) + sz\n\t\t}\n\t\treturn align(o, max)\n\tcase *types.Interface:\n\t\treturn s.WordSize * 2\n\t}\n\treturn s.WordSize // catch-all\n}\n\n// align returns the smallest y >= x such that y % a == 0.\nfunc align(x, a int64) int64 {\n\ty := x + a - 1\n\treturn y - y%a\n}\n\nfunc (s *gcSizes) ptrdata(T types.Type) int64 {\n\tswitch t := T.Underlying().(type) {\n\tcase *types.Basic:\n\t\tswitch t.Kind() {\n\t\tcase types.String, types.UnsafePointer:\n\t\t\treturn s.WordSize\n\t\t}\n\t\treturn 0\n\tcase *types.Chan, *types.Map, *types.Pointer, *types.Signature, *types.Slice:\n\t\treturn s.WordSize\n\tcase *types.Interface:\n\t\treturn 2 * s.WordSize\n\tcase *types.Array:\n\t\tn := t.Len()\n\t\tif n == 0 {\n\t\t\treturn 0\n\t\t}\n\t\ta := s.ptrdata(t.Elem())\n\t\tif a == 0 {\n\t\t\treturn 0\n\t\t}\n\t\tz := s.Sizeof(t.Elem())\n\t\treturn (n-1)*z + a\n\tcase *types.Struct:\n\t\tnf := t.NumFields()\n\t\tif nf == 0 {\n\t\t\treturn 0\n\t\t}\n\n\t\tvar o, p int64\n\t\tmax := int64(1)\n\t\tfor i := 0; i < nf; i++ {\n\t\t\tft := t.Field(i).Type()\n\t\t\ta, sz := s.Alignof(ft), s.Sizeof(ft)\n\t\t\tfp := s.ptrdata(ft)\n\t\t\tif a > max {\n\t\t\t\tmax = a\n\t\t\t}\n\t\t\tif i == nf-1 && sz == 0 && o != 0 {\n\t\t\t\tsz = 1\n\t\t\t}\n\t\t\to = align(o, a)\n\t\t\tif fp != 0 {\n\t\t\t\tp = o + fp\n\t\t\t}\n\t\t\to += sz\n\t\t}\n\t\treturn p\n\t}\n\n\tpanic(\"impossible\")\n\treturn 0\n}\n"
  },
  {
    "path": "passes/maligned/analyzer_test.go",
    "content": "package maligned_test\n\nimport (\n\t\"testing\"\n\n\t\"github.com/mdempsky/maligned/passes/maligned\"\n\t\"golang.org/x/tools/go/analysis/analysistest\"\n)\n\nfunc TestTest(t *testing.T) {\n\ttestdata := analysistest.TestData()\n\tanalysistest.Run(t, testdata, maligned.Analyzer, \"a\")\n}\n"
  },
  {
    "path": "passes/maligned/testdata/src/a/a.go",
    "content": "package a\n\ntype Good struct {\n\ty int32\n\tx byte\n\tz byte\n}\n\ntype Bad struct { // want \"struct of size 12 could be 8\"\n\tx byte\n\ty int32\n\tz byte\n}\n"
  },
  {
    "path": "passes/maligned/testdata/src/a/ptr.go",
    "content": "package a\n\ntype PointerGood struct {\n\tP   *int\n\tbuf [1000]uintptr\n}\n\ntype PointerBad struct { // want \"struct with 8008 pointer bytes could be 8\"\n\tbuf [1000]uintptr\n\tP   *int\n}\n\ntype PointerSorta struct {\n\ta struct {\n\t\tp *int\n\t\tq uintptr\n\t}\n\tb struct {\n\t\tp *int\n\t\tq [2]uintptr\n\t}\n}\n\ntype PointerSortaBad struct { // want \"struct with 32 pointer bytes could be 24\"\n\ta struct {\n\t\tp *int\n\t\tq [2]uintptr\n\t}\n\tb struct {\n\t\tp *int\n\t\tq uintptr\n\t}\n}\n"
  },
  {
    "path": "passes/maligned/testdata/src/a/zero.go",
    "content": "package a\n\ntype ZeroGood struct {\n\ta [0]byte\n\tb uint32\n}\n\ntype ZeroBad struct { // want \"struct of size 8 could be 4\"\n\ta uint32\n\tb [0]byte\n}\n"
  }
]