fix a couple of TODOs in position.go

Now that we've required Go 1.18 or later for some time,
stop supporting `// +build` directives entirely.
That should be fine, given that `go build` will fail too.

The TODO about ToObfuscate is also obsolete; see the added comment.

Finally, tweak the comments a bit after reading them again.
pull/551/head
Daniel Martí 4 years ago committed by lu4p
parent d18dd73556
commit 99f9b88363

@ -15,12 +15,6 @@ import (
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
func isDirective(text string) bool {
// TODO: can we remove the check for "// +build" now that we require Go 1.18
// or later? we should update the tests too.
return strings.HasPrefix(text, "//go:") || strings.HasPrefix(text, "// +build")
}
var printBuf1, printBuf2 bytes.Buffer var printBuf1, printBuf2 bytes.Buffer
// printFile prints a Go file to a buffer, while also removing non-directive // printFile prints a Go file to a buffer, while also removing non-directive
@ -36,13 +30,13 @@ func printFile(file1 *ast.File) ([]byte, error) {
src := printBuf1.Bytes() src := printBuf1.Bytes()
if !curPkg.ToObfuscate { if !curPkg.ToObfuscate {
// TODO(mvdan): make transformCompile handle untouched // We lightly transform packages which shouldn't be obfuscated,
// packages like runtime earlier on, to remove these checks. // such as when rewriting go:linkname directives to obfuscated packages.
// We still need to print the files, but without obfuscating positions.
return src, nil return src, nil
} }
absFilename := fset.Position(file1.Pos()).Filename filename := filepath.Base(fset.Position(file1.Pos()).Filename)
filename := filepath.Base(absFilename)
if strings.HasPrefix(filename, "_cgo_") { if strings.HasPrefix(filename, "_cgo_") {
// cgo-generated files don't need changed line numbers. // cgo-generated files don't need changed line numbers.
// Plus, the compiler can complain rather easily. // Plus, the compiler can complain rather easily.
@ -67,10 +61,10 @@ func printFile(file1 *ast.File) ([]byte, error) {
// Remove any comments by making them whitespace. // Remove any comments by making them whitespace.
// Keep directives, as they affect the build. // Keep directives, as they affect the build.
// This is superior to removing the comments before printing, // This is superior to removing the comments before printing,
// because then the final source would have different line numbers. // as otherwise 'garble reverse' would show different line numbers.
for _, group := range file2.Comments { for _, group := range file2.Comments {
for _, comment := range group.List { for _, comment := range group.List {
if isDirective(comment.Text) { if strings.HasPrefix(comment.Text, "//go:") {
continue continue
} }
start := fset.Position(comment.Pos()).Offset start := fset.Position(comment.Pos()).Offset
@ -81,6 +75,7 @@ func printFile(file1 *ast.File) ([]byte, error) {
} }
} }
// We want to use the original positions for the hashed positions.
var origCallExprs []*ast.CallExpr var origCallExprs []*ast.CallExpr
ast.Inspect(file1, func(node ast.Node) bool { ast.Inspect(file1, func(node ast.Node) bool {
if node, ok := node.(*ast.CallExpr); ok { if node, ok := node.(*ast.CallExpr); ok {
@ -89,7 +84,6 @@ func printFile(file1 *ast.File) ([]byte, error) {
return true return true
}) })
// Keep the compiler directives, and change position info.
type commentToAdd struct { type commentToAdd struct {
offset int offset int
text string text string

Loading…
Cancel
Save