cmd/cgo: pass end position info for C function arguments.

Pass information about original end position for c function arguments
processed in pointer checking generated code.

Fixes #42580

Change-Id: Ic8a578168362f0ca6055064dbbea092ad37477a6
Reviewed-on: https://go-review.googlesource.com/c/go/+/269760
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
wolf1996 2020-11-13 14:30:15 +03:00 committed by Ian Lance Taylor
parent e985245cd5
commit d5b9dc1317
3 changed files with 203 additions and 9 deletions

View file

@ -909,7 +909,7 @@ func (p *Package) rewriteCall(f *File, call *Call) (string, bool) {
var sbCheck bytes.Buffer
for i, param := range params {
origArg := args[i]
arg, nu := p.mangle(f, &args[i])
arg, nu := p.mangle(f, &args[i], true)
if nu {
needsUnsafe = true
}
@ -952,7 +952,7 @@ func (p *Package) rewriteCall(f *File, call *Call) (string, bool) {
sb.WriteString("return ")
}
m, nu := p.mangle(f, &call.Call.Fun)
m, nu := p.mangle(f, &call.Call.Fun, false)
if nu {
needsUnsafe = true
}
@ -1086,7 +1086,8 @@ func (p *Package) hasPointer(f *File, t ast.Expr, top bool) bool {
// rewriting calls when it finds them.
// It removes the corresponding references in f.Ref and f.Calls, so that we
// don't try to do the replacement again in rewriteRef or rewriteCall.
func (p *Package) mangle(f *File, arg *ast.Expr) (ast.Expr, bool) {
// If addPosition is true, add position info to the idents of C names in arg.
func (p *Package) mangle(f *File, arg *ast.Expr, addPosition bool) (ast.Expr, bool) {
needsUnsafe := false
f.walk(arg, ctxExpr, func(f *File, arg interface{}, context astContext) {
px, ok := arg.(*ast.Expr)
@ -1101,7 +1102,7 @@ func (p *Package) mangle(f *File, arg *ast.Expr) (ast.Expr, bool) {
for _, r := range f.Ref {
if r.Expr == px {
*px = p.rewriteName(f, r)
*px = p.rewriteName(f, r, addPosition)
r.Done = true
break
}
@ -1361,7 +1362,7 @@ func (p *Package) rewriteRef(f *File) {
}
}
expr := p.rewriteName(f, r)
expr := p.rewriteName(f, r, false)
if *godefs {
// Substitute definition for mangled type name.
@ -1424,8 +1425,23 @@ func (p *Package) rewriteRef(f *File) {
}
// rewriteName returns the expression used to rewrite a reference.
func (p *Package) rewriteName(f *File, r *Ref) ast.Expr {
var expr ast.Expr = ast.NewIdent(r.Name.Mangle) // default
// If addPosition is true, add position info in the ident name.
func (p *Package) rewriteName(f *File, r *Ref, addPosition bool) ast.Expr {
getNewIdent := ast.NewIdent
if addPosition {
getNewIdent = func(newName string) *ast.Ident {
mangledIdent := ast.NewIdent(newName)
if len(newName) == len(r.Name.Go) {
return mangledIdent
}
p := fset.Position((*r.Expr).End())
if p.Column == 0 {
return mangledIdent
}
return ast.NewIdent(fmt.Sprintf("%s /*line :%d:%d*/", newName, p.Line, p.Column))
}
}
var expr ast.Expr = getNewIdent(r.Name.Mangle) // default
switch r.Context {
case ctxCall, ctxCall2:
if r.Name.Kind != "func" {
@ -1453,7 +1469,7 @@ func (p *Package) rewriteName(f *File, r *Ref) ast.Expr {
n.Mangle = "_C2func_" + n.Go
f.Name["2"+r.Name.Go] = n
}
expr = ast.NewIdent(n.Mangle)
expr = getNewIdent(n.Mangle)
r.Name = n
break
}
@ -1484,7 +1500,7 @@ func (p *Package) rewriteName(f *File, r *Ref) ast.Expr {
// issue 7757.
expr = &ast.CallExpr{
Fun: &ast.Ident{NamePos: (*r.Expr).Pos(), Name: "_Cgo_ptr"},
Args: []ast.Expr{ast.NewIdent(name.Mangle)},
Args: []ast.Expr{getNewIdent(name.Mangle)},
}
case "type":
// Okay - might be new(T)