From 99f9b883630a5d10855f5d6507a064dd245e22e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 15 Jun 2022 17:13:15 +0100 Subject: [PATCH] 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. --- position.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/position.go b/position.go index 010518b..03e460a 100644 --- a/position.go +++ b/position.go @@ -15,12 +15,6 @@ import ( "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 // 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() if !curPkg.ToObfuscate { - // TODO(mvdan): make transformCompile handle untouched - // packages like runtime earlier on, to remove these checks. + // We lightly transform packages which shouldn't be obfuscated, + // such as when rewriting go:linkname directives to obfuscated packages. + // We still need to print the files, but without obfuscating positions. return src, nil } - absFilename := fset.Position(file1.Pos()).Filename - filename := filepath.Base(absFilename) + filename := filepath.Base(fset.Position(file1.Pos()).Filename) if strings.HasPrefix(filename, "_cgo_") { // cgo-generated files don't need changed line numbers. // 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. // Keep directives, as they affect the build. // 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 _, comment := range group.List { - if isDirective(comment.Text) { + if strings.HasPrefix(comment.Text, "//go:") { continue } 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 ast.Inspect(file1, func(node ast.Node) bool { if node, ok := node.(*ast.CallExpr); ok { @@ -89,7 +84,6 @@ func printFile(file1 *ast.File) ([]byte, error) { return true }) - // Keep the compiler directives, and change position info. type commentToAdd struct { offset int text string