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

Open
wolf1996 opened this issue Nov 13, 2020 · 2 comments
Open

cmd/cgo: shifting identifier position in ast #42580

wolf1996 opened this issue Nov 13, 2020 · 2 comments

Comments

@wolf1996
Copy link

@wolf1996 wolf1996 commented Nov 13, 2020

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
Author

@wolf1996 wolf1996 commented Nov 13, 2020

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

@gopherbot gopherbot commented Nov 13, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.