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: cgo struct parsing regression #29781

Closed
jeddenlea opened this issue Jan 17, 2019 · 6 comments

Comments

@jeddenlea
Copy link
Contributor

commented Jan 17, 2019

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

go1.12beta2, Darwin and Linux, amd64.

Does this issue reproduce with the latest release?

I first encountered the issue trying go1.12beta2, and then tip (0456036). This is a regression since go1.11.

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

go env Output
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jed/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/jed/go"
GOPROXY=""
GORACE=""
GOROOT="/Users/jed/go-dev"
GOTMPDIR=""
GOTOOLDIR="/Users/jed/go-dev/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ps/3rjx30b12037ybv6v8tmfnr80000gn/T/go-build713347749=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Given test.go, a contrived example to reproduce a problem seen in a more complicated setup,

package main

/*
struct foo {
	void *p;
};

static void
blah(struct foo *f, int n) { }

#define BAR 0xc0
*/
import "C"

func main() {
	var c *C.struct_foo
	C.blah(c, C.BAR+2)
}
$ go build test.go
# command-line-arguments
./test.go:17:46: syntax error: unexpected semicolon, expecting expression

What did you expect to see?

The code should compile.

What did you see instead?

A syntax error, and a bizarre one at that. Line 17 is C.blah(c, C.BAR+2), and it is not 46 characters long.

This example compiles and runs fine with Go 1.11. However, with 1.12beta2 something is getting tripped up by the presence of the void *p; in the struct definition combined with the C.BAR+2 expression in the second parameter.

If void *p; is removed and replaced with something like int x, y;, there's no problem.

Also, all these forms of the call work:

  • C.blah(nil, C.BAR+2)
  • C.blah(c, 2)
  • C.blah(c, C.BAR)
  • C.blah(c, (C.BAR+2))
  • C.blah(c, 2+C.BAR)
@ALTree

This comment has been minimized.

Copy link
Member

commented Jan 17, 2019

Thanks for the report. This looks like #29748 and it's already fixed, so I'm closing this issue.

@ALTree ALTree closed this Jan 17, 2019

@ALTree

This comment has been minimized.

Copy link
Member

commented Jan 17, 2019

Just noticed that the tip commit you tested comes after the fix... so I guess it's not fixed? Can you double-check and confirm that the tip version you tested includes the fix for #29748?

@ALTree ALTree reopened this Jan 17, 2019

@jeddenlea

This comment has been minimized.

Copy link
Contributor Author

commented Jan 17, 2019

I just rebuilt to be sure, from the latest tip.

$ cat test.go
package main

/*
struct foo {
 	void *p;
};

static void
blah(struct foo *f, int n) { }

#define BAR 0xc0
*/
import "C"

func main() {
	var c *C.struct_foo
	C.blah(c, C.BAR+2)
}
$ go version
go version devel +006a5e7 Thu Jan 17 01:28:22 2019 +0000 darwin/amd64
$ go build test.go
# command-line-arguments
./test.go:17:46: syntax error: unexpected semicolon, expecting expression

Maybe another path to the same issue.

@ALTree ALTree changed the title Cgo struct parsing regression? cmd/cgo: cgo struct parsing regression Jan 17, 2019

@ALTree ALTree added this to the Go1.12 milestone Jan 17, 2019

@ALTree

This comment has been minimized.

Copy link
Member

commented Jan 17, 2019

@jeddenlea

This comment has been minimized.

Copy link
Contributor Author

commented Jan 17, 2019

It also works to change BAR to int BAR = 0xc0;

@ianlancetaylor ianlancetaylor self-assigned this Jan 18, 2019

@gopherbot

This comment has been minimized.

Copy link

commented Jan 18, 2019

Change https://golang.org/cl/158457 mentions this issue: cmd/cgo: don't replace newlines with semicolons in binary expressions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.