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 error in generated wrappers points at unrelated source line #15836

Closed
ALTree opened this issue May 25, 2016 · 5 comments
Closed
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@ALTree
Copy link
Member

ALTree commented May 25, 2016

go version devel +095fbdc Wed May 25 16:49:02 2016 +0000 linux/amd64

The following program (sorry about the mpfr.h dependency, I wasn't able to reproduce it with a custom .h file):

package foo

/*
#cgo LDFLAGS: -lmpfr -lgmp
#include <gmp.h>
#include <mpfr.h>
*/
import "C"

func Bar() {
    _ = 1
    _ = 2
    _ = 3
    _ = 4
    _ = 5
    _ = 6
    _ = 7
    _ = 8
}

func Prec() C.mpfr_prec_t {
    return C.mpfr_get_prec(nil)
}

func Foo3() {
    _ = 1
    _ = 2
    _ = 3
}

func Foo2() {
    _ = 4
    _ = 5
    _ = 6
}

func Foo() {
    _ = 1
    _ = 2
    _ = 3
    _ = 4
    _ = 5
    _ = 6
    _ = 7
    _ = 8
    _ = 9
    _ = 10
    _ = 11
    // this isn't the line you're looking for
}

gives, on tip, an error that points to a bogus line in the source file. With tip:

# command-line-arguments
In file included from ./test.go:6:0:
./test.go: In function ‘_cgo_b5bec8c53dc8_Cfunc_mpfr_get_prec’:
./test.go:49:6: error: request for member ‘_mpfr_prec’ in something not a structure or union
  // this isn't the line you're looking for
      ^
./test.go:49:6: error: request for member ‘_mpfr_prec’ in something not a structure or union
  // this isn't the line you're looking for
      ^

with go1.6:

# command-line-arguments
In file included from ./test.go:6:0:
./test.go: In function ‘_cgo_b5bec8c53dc8_Cfunc_mpfr_get_prec’:
./test.go:41:23: error: request for member ‘_mpfr_prec’ in something not a structure or union
./test.go:41:23: error: request for member ‘_mpfr_prec’ in something not a structure or union

go1.6 is wrong too (it points to line 41), but at least it doesn't print it.

@ALTree ALTree changed the title cmd/go: cgo error points to the wrong line cmd/go: cgo error points to a wrong line May 25, 2016
@ianlancetaylor
Copy link
Contributor

Note that the line is not being printed by any Go tool. It's being printed by the C compiler. I don't know why whether the line is printed or not changed from Go 1.6 to tip; using go build -x may show that we are passing different arguments to the compiler.

@ianlancetaylor ianlancetaylor changed the title cmd/go: cgo error points to a wrong line cmd/cgo: cgo error points to a wrong line May 25, 2016
@ianlancetaylor ianlancetaylor added this to the Go1.8 milestone May 25, 2016
@ALTree
Copy link
Member Author

ALTree commented May 25, 2016

I don't know if error reporting for cgo code is supposed to be accurate when printing line numbers, so I'm not sure if this a bug or not; but right now we are printing completely bogus information, and it's rather confusing (even more confusing on tip, with a random line of code appearing in the error message).

More info about my env below.

Go 1.6:

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/alberto/gocode"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

tip:

$ gotip env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/alberto/gocode"
GORACE=""
GOROOT="/home/alberto/go"
GOTOOLDIR="/home/alberto/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build949309868=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

the c compiler:

$ gcc --version
gcc (Debian 5.3.1-19) 5.3.1 20160509

-x output for go1.6:

$ go build -x test.go 
WORK=/tmp/go-build652767123
mkdir -p $WORK/command-line-arguments/_obj/
mkdir -p $WORK/
cd /home/alberto/Desktop/foo
CGO_LDFLAGS="-g" "-O2" "-lmpfr" "-lgmp" /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/command-line-arguments/_obj/ -importpath command-line-arguments -- -I $WORK/command-line-arguments/_obj/ test.go
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -I $WORK/command-line-arguments/_obj/ -g -O2 -o $WORK/command-line-arguments/_obj/_cgo_main.o -c $WORK/command-line-arguments/_obj/_cgo_main.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -I $WORK/command-line-arguments/_obj/ -g -O2 -o $WORK/command-line-arguments/_obj/_cgo_export.o -c $WORK/command-line-arguments/_obj/_cgo_export.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -I $WORK/command-line-arguments/_obj/ -g -O2 -o $WORK/command-line-arguments/_obj/test.cgo2.o -c $WORK/command-line-arguments/_obj/test.cgo2.c
# command-line-arguments
In file included from ./test.go:6:0:
./test.go: In function ‘_cgo_b5bec8c53dc8_Cfunc_mpfr_get_prec’:
./test.go:41:23: error: request for member ‘_mpfr_prec’ in something not a structure or union
./test.go:41:23: error: request for member ‘_mpfr_prec’ in something not a structure or union

-x output for tip:

$ gotip build -x test.go 
WORK=/tmp/go-build055975085
mkdir -p $WORK/command-line-arguments/_obj/
mkdir -p $WORK/
cd /home/alberto/Desktop/foo
CGO_LDFLAGS="-g" "-O2" "-lmpfr" "-lgmp" /home/alberto/go/pkg/tool/linux_amd64/cgo -objdir $WORK/command-line-arguments/_obj/ -importpath command-line-arguments -- -I $WORK/command-line-arguments/_obj/ test.go
cd $WORK
gcc -fdebug-prefix-map=a=b -c trivial.c
gcc -gno-record-gcc-switches -c trivial.c
cd /home/alberto/Desktop/foo
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/command-line-arguments/_obj/ -g -O2 -o $WORK/command-line-arguments/_obj/_cgo_main.o -c $WORK/command-line-arguments/_obj/_cgo_main.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/command-line-arguments/_obj/ -g -O2 -o $WORK/command-line-arguments/_obj/_cgo_export.o -c $WORK/command-line-arguments/_obj/_cgo_export.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/command-line-arguments/_obj/ -g -O2 -o $WORK/command-line-arguments/_obj/test.cgo2.o -c $WORK/command-line-arguments/_obj/test.cgo2.c
# command-line-arguments
In file included from ./test.go:6:0:
./test.go: In function ‘_cgo_b5bec8c53dc8_Cfunc_mpfr_get_prec’:
./test.go:49:6: error: request for member ‘_mpfr_prec’ in something not a structure or union
  // this isn't the line you're looking for
      ^
./test.go:49:6: error: request for member ‘_mpfr_prec’ in something not a structure or union
  // this isn't the line you're looking for
      ^

@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 5, 2016
@quentinmit
Copy link
Contributor

It seems like we must be passing the wrong line numbers to gcc; since gcc is printing errors in "test.go" and not in the generated C file, that means we're already providing line number info to gcc.

Docs at https://gcc.gnu.org/onlinedocs/cpp/Line-Control.html for how the line control directives are supposed to be formatted.

@rsc
Copy link
Contributor

rsc commented Oct 21, 2016

The line number is not really interesting here. The error is being reported in generated code, specifically the code that Go calls to wrap C's mpfr_get_prec. Maybe we should emit a #line that sets the file name to something like "".

Filed #17537 for the more serious problem that somehow mpfr_get_prec's wrapper is not being generated correctly.

@rsc rsc changed the title cmd/cgo: cgo error points to a wrong line cmd/cgo: cgo error in generated wrappers points at unrelated source line Oct 21, 2016
@gopherbot
Copy link

CL https://golang.org/cl/32613 mentions this issue.

@golang golang locked and limited conversation to collaborators Nov 3, 2017
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