]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: add -trimpath flag allowing paths to be rewritten in outputs
authorMichael Matloob <matloob@golang.org>
Tue, 27 Oct 2020 21:51:58 +0000 (17:51 -0400)
committerMichael Matloob <matloob@golang.org>
Sat, 31 Oct 2020 00:35:18 +0000 (00:35 +0000)
cmd/cgo now has a -trimpath flag that behaves the same as the
-trimpath flag to cmd/compile. This will be used to correct paths
to cgo files that are overlaid.

The code that processes trimpath in internal/objapi has been slightly
refactored because it's currently only accessible via AbsFile, which
does some additional processing to the path names. Now an
ApplyRewrites function is exported that just applies the trimpath
rewrites.

Also remove unused srcfile argument to cmd/cgo.(*Package).godefs.

For #39958

Change-Id: I497d48d0bc2fe1f6ab2b5835cbe79f15b839ee59
Reviewed-on: https://go-review.googlesource.com/c/go/+/266358
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/cgo/ast.go
src/cmd/cgo/godefs.go
src/cmd/cgo/main.go
src/cmd/internal/objabi/line.go

index 54d6bc2559dc9d901d7ac8ba6b59a6bb30426351..a073407a961e15af2a7b4cbe92afd925712e82b3 100644 (file)
@@ -13,7 +13,6 @@ import (
        "go/scanner"
        "go/token"
        "os"
-       "path/filepath"
        "strings"
 )
 
@@ -44,14 +43,7 @@ func sourceLine(n ast.Node) int {
 // attached to the import "C" comment, a list of references to C.xxx,
 // a list of exported functions, and the actual AST, to be rewritten and
 // printed.
-func (f *File) ParseGo(name string, src []byte) {
-       // Create absolute path for file, so that it will be used in error
-       // messages and recorded in debug line number information.
-       // This matches the rest of the toolchain. See golang.org/issue/5122.
-       if aname, err := filepath.Abs(name); err == nil {
-               name = aname
-       }
-
+func (f *File) ParseGo(abspath string, src []byte) {
        // Two different parses: once with comments, once without.
        // The printer is not good enough at printing comments in the
        // right place when we start editing the AST behind its back,
@@ -60,8 +52,8 @@ func (f *File) ParseGo(name string, src []byte) {
        // and reprinting.
        // In cgo mode, we ignore ast2 and just apply edits directly
        // the text behind ast1. In godefs mode we modify and print ast2.
-       ast1 := parse(name, src, parser.ParseComments)
-       ast2 := parse(name, src, 0)
+       ast1 := parse(abspath, src, parser.ParseComments)
+       ast2 := parse(abspath, src, 0)
 
        f.Package = ast1.Name.Name
        f.Name = make(map[string]*Name)
@@ -88,7 +80,7 @@ func (f *File) ParseGo(name string, src []byte) {
                                cg = d.Doc
                        }
                        if cg != nil {
-                               f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), name)
+                               f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), abspath)
                                f.Preamble += commentText(cg) + "\n"
                                f.Preamble += "#line 1 \"cgo-generated-wrapper\"\n"
                        }
index b4fd9c5a6e326661b0383d77f80319b0a6bebac6..c0d59aee01d747ec8ed02a587c72da0dd55f016b 100644 (file)
@@ -16,7 +16,7 @@ import (
 )
 
 // godefs returns the output for -godefs mode.
-func (p *Package) godefs(f *File, srcfile string) string {
+func (p *Package) godefs(f *File) string {
        var buf bytes.Buffer
 
        fmt.Fprintf(&buf, "// Code generated by cmd/cgo -godefs; DO NOT EDIT.\n")
index 7d02ac3c5467107336d710f81a3cb497e185abfa..c1116e28ecdb25324fd9e0d18251db85f4be9ba8 100644 (file)
@@ -243,6 +243,8 @@ var gccgopkgpath = flag.String("gccgopkgpath", "", "-fgo-pkgpath option used wit
 var gccgoMangler func(string) string
 var importRuntimeCgo = flag.Bool("import_runtime_cgo", true, "import runtime/cgo in generated code")
 var importSyscall = flag.Bool("import_syscall", true, "import syscall in generated code")
+var trimpath = flag.String("trimpath", "", "applies supplied rewrites or trims prefixes to recorded source file paths")
+
 var goarch, goos string
 
 func main() {
@@ -322,6 +324,13 @@ func main() {
                        input = filepath.Join(*srcDir, input)
                }
 
+               // Create absolute path for file, so that it will be used in error
+               // messages and recorded in debug line number information.
+               // This matches the rest of the toolchain. See golang.org/issue/5122.
+               if aname, err := filepath.Abs(input); err == nil {
+                       input = aname
+               }
+
                b, err := ioutil.ReadFile(input)
                if err != nil {
                        fatalf("%s", err)
@@ -330,6 +339,10 @@ func main() {
                        fatalf("%s", err)
                }
 
+               // Apply trimpath to the file path. The path won't be read from after this point.
+               input, _ = objabi.ApplyRewrites(input, *trimpath)
+               goFiles[i] = input
+
                f := new(File)
                f.Edit = edit.NewBuffer(b)
                f.ParseGo(input, b)
@@ -367,7 +380,7 @@ func main() {
                p.PackagePath = f.Package
                p.Record(f)
                if *godefs {
-                       os.Stdout.WriteString(p.godefs(f, input))
+                       os.Stdout.WriteString(p.godefs(f))
                } else {
                        p.writeOutput(f, input)
                }
index 178c8363d95d8a39b74167636f3b4972ccbfa975..0733b65138db50371baf7a3268d036bcc986f4f8 100644 (file)
@@ -37,25 +37,36 @@ func AbsFile(dir, file, rewrites string) string {
                abs = filepath.Join(dir, file)
        }
 
+       abs, rewritten := ApplyRewrites(abs, rewrites)
+       if !rewritten && hasPathPrefix(abs, GOROOT) {
+               abs = "$GOROOT" + abs[len(GOROOT):]
+       }
+
+       if abs == "" {
+               abs = "??"
+       }
+       return abs
+}
+
+// ApplyRewrites returns the filename for file in the given directory,
+// as rewritten by the rewrites argument.
+//
+// The rewrites argument is a ;-separated list of rewrites.
+// Each rewrite is of the form "prefix" or "prefix=>replace",
+// where prefix must match a leading sequence of path elements
+// and is either removed entirely or replaced by the replacement.
+func ApplyRewrites(file, rewrites string) (string, bool) {
        start := 0
        for i := 0; i <= len(rewrites); i++ {
                if i == len(rewrites) || rewrites[i] == ';' {
-                       if new, ok := applyRewrite(abs, rewrites[start:i]); ok {
-                               abs = new
-                               goto Rewritten
+                       if new, ok := applyRewrite(file, rewrites[start:i]); ok {
+                               return new, true
                        }
                        start = i + 1
                }
        }
-       if hasPathPrefix(abs, GOROOT) {
-               abs = "$GOROOT" + abs[len(GOROOT):]
-       }
 
-Rewritten:
-       if abs == "" {
-               abs = "??"
-       }
-       return abs
+       return file, false
 }
 
 // applyRewrite applies the rewrite to the path,