Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: shifting identifier position in ast #42580

Closed
wolf1996 opened this issue Nov 13, 2020 · 5 comments
Closed

cmd/cgo: shifting identifier position in ast #42580

wolf1996 opened this issue Nov 13, 2020 · 5 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@wolf1996
Copy link
Contributor

What version of Go are you using (go version)?

go version go1.15.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/ksg/go/bin"
GOCACHE="/home/ksg/.cache/go-build"
GOENV="/home/ksg/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ksg/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ksg/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/ksg/d_ext4_storage/Projects/golang/stdgo/workdir/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build994304763=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I worked on my task related to ast processing.I faced unexpected behaviour in the AST for code compiled with cgo.
I wrote some simplified test function to represent the bug.
package pg

// typedef int (*intFunc) ();
//
// char* strarg = "";
//
// int func_with_char(char* arg, void* dummy)
// {return 5;}

import "C"
import "unsafe"

var (
	checkedPointer            = []byte{1}
)

func TestSingleArgumentCast() C.int {
	retcode := C.func_with_char((*C.char)(unsafe.Pointer(&checkedPointer[0])), unsafe.Pointer(C.strarg))
	return retcode
}

What did you expect to see?

checkedPointer first symbol has 56 column.
When I set second parameter to nil the result ast is correct

func TestSingleArgumentCastOk() C.int {
   retcode := C.func_with_char((*C.char)(unsafe.Pointer(&checkedPointer[0])), nil) // position of “checkedPointer” is 56 column as in code as in ast
   return retcode
}

What did you see instead?

checkedPointer first symbol has 61 column in ast.

Test Cases

https://play.golang.org/p/w7MiDEKg7bx

@wolf1996
Copy link
Contributor Author

I’ve made a little investigation:

func TestSingleArgumentCast() C.int {
   retcode := C.func_with_char((*C.char)(unsafe.Pointer(&checkedPointer[0])), unsafe.Pointer(C.strarg))
   return retcode
}
 
func TestSingleArgumentCastOk() C.int {
   retcode := C.func_with_char((*C.char)(unsafe.Pointer(&checkedPointer[0])), nil)
   return retcode
}

Passing this snippet to cgo and comparing the result I've noticed that there is no information about the end of original type identifier in type cast expression.

 
func TestSingleArgumentCast()  /*line :21:31*/_Ctype_int /*line :21:36*/ {
   retcode := func() _Ctype_int{ var _cgo0 *_Ctype_char = /*line :22:30*/(*_Ctype_char)(unsafe.Pointer(&checkedPointer[0])); _cgo1 := /*line :22:77*/unsafe.Pointer(*_Cvar_strarg); _cgoCheckPointer(_cgo1, nil); return _Cfunc_func_with_char(_cgo0, _cgo1); }()
   return retcode
}

func TestSingleArgumentCastOk()  /*line :21:31*/_Ctype_int /*line :21:36*/ {
   retcode := ( /*line :22:13*/_Cfunc_func_with_char /*line :22:28*/)((* /*line :22:32*/_Ctype_char /*line :22:38*/)(unsafe.Pointer(&checkedPointer[0])), nil)
   return retcode
}

There should be another one “line” comment to fix the identifier position in the first case .

func TestSingleArgumentCast()  /*line :21:31*/_Ctype_int /*line :21:36*/ {
   retcode := func() _Ctype_int{ var _cgo0 *_Ctype_char = /*line :22:30*/(*_Ctype_char /*line :22:38*/)(unsafe.Pointer(&checkedPointer[0])); _cgo1 := /*line :22:77*/unsafe.Pointer(*_Cvar_strarg /*line :22:100*/); _cgoCheckPointer(_cgo1, nil); return _Cfunc_func_with_char(_cgo0, _cgo1); }()
   return retcode
}

@gopherbot
Copy link

Change https://golang.org/cl/269760 mentions this issue: cmd/cgo: pass end position info for C function arguments.

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 8, 2020
@cagedmantis cagedmantis added this to the Go1.17 milestone Dec 8, 2020
@wolf1996
Copy link
Contributor Author

wolf1996 commented Mar 2, 2021

Can I help with any further work?

@ianlancetaylor
Copy link
Contributor

Thanks for the ping. I made some comments on the change.

@wolf1996
Copy link
Contributor Author

wolf1996 commented Mar 30, 2021

ping @ianlancetaylor

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Apr 2, 2021
@golang golang locked and limited conversation to collaborators Apr 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants