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: generates incorrect int constants from #define with cast to ulong typedef with clang #46247

Open
ansiwen opened this issue May 18, 2021 · 4 comments

Comments

@ansiwen
Copy link

@ansiwen ansiwen commented May 18, 2021

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

go version go1.16.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/svanders/Library/Caches/go-build"
GOENV="/Users/svanders/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/svanders/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/svanders/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/svanders/sdk/go1.16.4"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/svanders/sdk/go1.16.4/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/t8/5_d5bl793710gx_c4fh3_k6r0000gn/T/go-build2777539228=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

$ cat main.go

package main

// typedef unsigned long my_ul;
// #define UL ((unsigned long)(-2))
// #define MY_UL ((my_ul)(-2))
import "C"
import "fmt"

func main() {
	fmt.Println(C.UL / 2)
	fmt.Println(C.MY_UL / 2)
}

$ go run ./main.go

What did you expect to see?

Like on linux I would expect this output:

9223372036854775807
9223372036854775807

What did you see instead?

On MacOS Catalina (10.15.7) I get this output instead

9223372036854775807
-1

the according generated code in _cgo_gotypes.go looks like this:

const _Ciconst_UI64 = -0x2
const _Ciconst_ULL = 0xfffffffffffffffe

so the cast seems to be ignored, if it is a typedef of unsigned long (or unsigned long long).

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 19, 2021

This is probably a dup of #39136.

Loading

@ansiwen
Copy link
Author

@ansiwen ansiwen commented May 20, 2021

@ianlancetaylor

This is probably a dup of #39136.

Ok, might be. But I want to point out, that a typedef for unsigned int does work. Only unsigned long or unsigned long long is failing.

Loading

@ansiwen
Copy link
Author

@ansiwen ansiwen commented May 20, 2021

I can confirm, that using GCC 11.1.0 instead of the MacOS clang (12.0.0) makes the issue disappear.

Loading

@ansiwen
Copy link
Author

@ansiwen ansiwen commented May 20, 2021

I can also confirm, that the patch from #39136 does fix this issue here as well.

Loading

@dmitshur dmitshur added this to the Backlog milestone May 21, 2021
@ansiwen ansiwen changed the title cmd/cgo: generates incorrect int constants from #define with cast to ulong typedef on darwin cmd/cgo: generates incorrect int constants from #define with cast to ulong typedef with clang Jun 1, 2021
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
3 participants