Add ability to exclude identifiers fixes #15

pull/22/head
lu4p 5 years ago
parent 3c97725ccc
commit 1574059917

@ -3,6 +3,6 @@ module mvdan.cc/garble
go 1.14
require (
github.com/rogpeppe/go-internal v1.5.2
golang.org/x/tools v0.0.0-20200330040139-fa3cc9eebcfe
github.com/rogpeppe/go-internal v1.6.0
golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375
)

@ -3,9 +3,9 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/rogpeppe/go-internal v1.5.2 h1:qLvObTrvO/XRCqmkKxUlOBc48bI3efyDuAZe25QiF0w=
github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/rogpeppe/go-internal v1.6.0 h1:IZRgg4sfrDH7nsAD1Y/Nwj+GzIfEwpJSLjCaNC3SbsI=
github.com/rogpeppe/go-internal v1.6.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
@ -18,8 +18,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200330040139-fa3cc9eebcfe h1:sOd+hT8wBUrIFR5Q6uQb/rg50z8NjHk96kC4adwvxjw=
golang.org/x/tools v0.0.0-20200330040139-fa3cc9eebcfe/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375 h1:SjQ2+AKWgZLc1xej6WSzL+Dfs5Uyd5xcZH1mGC411IA=
golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

@ -316,6 +316,9 @@ func transformCompile(args []string) ([]string, error) {
flags = flagSetValue(flags, "-trimpath", tempDir+"=>;"+trimpath)
// log.Println(flags)
args = flags
blacklist := buildBlacklist(files, info)
// TODO: randomize the order and names of the files
for i, file := range files {
origName := filepath.Base(filepath.Clean(paths[i]))
@ -327,7 +330,7 @@ func transformCompile(args []string) ([]string, error) {
// messy.
name = "_cgo_" + name
default:
file = transformGo(file, info)
file = transformGo(file, info, blacklist)
}
tempFile := filepath.Join(tempDir, name)
f, err := os.Create(tempFile)
@ -436,8 +439,55 @@ func hashWith(salt, value string) string {
return "z" + sum[:length]
}
type blacklistItem struct {
pkg string
typename string
}
func buildBlacklist(files []*ast.File, info *types.Info) []blacklistItem {
var (
blacklist []blacklistItem
prevReflect bool
)
pre := func(cursor *astutil.Cursor) bool {
node, ok := cursor.Node().(*ast.Ident)
if !ok {
return true
}
obj := info.ObjectOf(node)
tmpReflect := prevReflect
prevReflect = false
switch x := obj.(type) {
case *types.Func:
if x.Pkg().Name() == "reflect" && (x.Name() == "TypeOf" || x.Name() == "ValueOf") {
prevReflect = true
}
case *types.TypeName:
if tmpReflect {
blacklist = append(blacklist, blacklistItem{
pkg: x.Pkg().Path(),
typename: x.Name(),
})
}
}
return true
}
for _, file := range files {
astutil.Apply(file, pre, nil)
}
return blacklist
}
// transformGo garbles the provided Go syntax node.
func transformGo(file *ast.File, info *types.Info) *ast.File {
func transformGo(file *ast.File, info *types.Info, blacklist []blacklistItem) *ast.File {
// Remove all comments, minus the "//go:" compiler directives.
// The final binary should still not contain comment text, but removing
// it helps ensure that (and makes position info less predictable).
@ -499,6 +549,11 @@ func transformGo(file *ast.File, info *types.Info) *ast.File {
}
case *types.Const:
case *types.TypeName:
for _, item := range blacklist {
if item.pkg == obj.Pkg().Path() && item.typename == obj.Name() {
return true
}
}
case *types.Func:
sign := obj.Type().(*types.Signature)
if obj.Exported() && sign.Recv() != nil {

@ -3,6 +3,7 @@ exec ./main
cmp stdout main.stdout
! binsubstr main$exe 'ImportedVar' 'ImportedConst' 'ImportedFunc' 'ImportedType' 'main.go' 'imported.go'
binsubstr main$exe 'ImportedAPI'
[short] stop # checking that the build is reproducible is slow
@ -24,6 +25,7 @@ package main
import (
"fmt"
_ "unsafe"
"reflect"
"test/main/imported"
@ -38,6 +40,9 @@ func main() {
fmt.Println(imported.ImportedConst)
imported.ImportedFunc('x')
fmt.Println(imported.ImportedType(3))
api := new(imported.ImportedAPI)
fmt.Println(reflect.TypeOf(api))
linkedPrintln(nil)
fmt.Println(quote.Go())
}
@ -45,11 +50,14 @@ func main() {
imported var value
imported const value
3
*imported.ImportedAPI
<nil>
Don't communicate by sharing memory, share memory by communicating.
-- imported/imported.go --
package imported
import "reflect"
var ImportedVar = "imported var value"
const ImportedConst = "imported const value"
@ -59,3 +67,7 @@ func ImportedFunc(param rune) string {
}
type ImportedType int
type ImportedAPI int
var _ = reflect.TypeOf(ImportedAPI(0))
Loading…
Cancel
Save