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: can generate functions with unused variables #6883

Closed
gopherbot opened this issue Dec 3, 2013 · 10 comments

Comments

@gopherbot
Copy link

commented Dec 3, 2013

by vbatts:

From the question raised on golang-nuts:
https://groups.google.com/forum/#!topic/golang-nuts/SJn48BkCUqA

What steps will reproduce the problem?
1. <code>
tar xf go1.2.src.tar.gz
cd go
mkdir -p zz
CFLAGS="-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector
--param=ssp-buffer-size=4 -m64 -mtune=generic"
echo -e '#!/bin/sh' > ./zz/gcc
echo "/usr/bin/gcc $CFLAGS \"\$@\"" >> ./zz/gcc
chmod +x ./zz/gcc
cd src 
# use our gcc wrapper
PATH="$(pwd -P)/../zz:$PATH" CC="gcc" ./all.bash
</code>
2.
3.


What is the expected output?

---
Installed Go for linux/amd64 in /home/vbatts/tmp/go
Installed commands in /home/vbatts/bin


What do you see instead?
[...]
ok      regexp  0.105s
ok      regexp/syntax   1.565s
--- FAIL: TestCgoTraceback (1.02 seconds)
        crash_cgo_test.go:33: expected "OK\n", but got "# command-line-arguments\n/tmp/go-build355509872/main.go: In function ‘_cgo_65eb2f8ba551_Cfunc_foo’:\n/tmp/go-build355509872/main.go:31: warning: unused variable ‘a’\nOK\n"
FAIL
FAIL    runtime 21.837s
?       runtime/cgo     [no test files]
ok      runtime/debug   0.007s
ok      runtime/pprof   7.550s
?       runtime/race    [no test files]
ok      sort    0.272s
[...]

Turns out that the cgoTracebackSource from crash_cgo_test.go causes the intermediary *.c
to have unused variables

void
_cgo_92e4c70c8af1_Cfunc_foo(void *v)
{
        struct {
                char unused;
        } __attribute__((__packed__, __gcc_struct__)) *a = v;
        foo();
}

minix proposed that __attribte__((unused)) may be used here



Which compiler are you using (5g, 6g, 8g, gccgo)?
n/a



Which operating system are you using?
LSB_VERSION=base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Red Hat Enterprise Linux Workstation release 6.5 (Santiago)



Which version are you using?  (run 'go version')
go1.2.src.tar.gz



Please provide any additional information below.
related request for CFLAGS support https://golang.org/issue/6882
@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

Comment 1:

Labels changed: added release-none, removed go1.3maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

Comment 2:

Labels changed: added repo-main.

@gopherbot gopherbot added new labels Dec 4, 2013

@bradfitz bradfitz removed the new label Dec 18, 2014

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@rsc rsc removed release-none labels Apr 10, 2015

@ianlancetaylor ianlancetaylor changed the title runtime: crash_cgo_test.go fails on cgoTracebackSource generating unused variables cmd/cgo: can generate functions with unused variables Feb 10, 2016

@cstyan

This comment has been minimized.

Copy link

commented Mar 1, 2018

I just ran into this issue in 1.10.

go env:

GOBIN=""
GOCACHE="/home/callum/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/callum/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="/usr/bin/gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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-build177554193=/tmp/go-build -gno-record-gcc-switches"

go version: go version go1.10 linux/amd64

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Mar 1, 2018

@cstyan Can you show us how to reproduce the problem? Thanks.

@cstyan

This comment has been minimized.

Copy link

commented Mar 1, 2018

@ianlancetaylor I'll try to reproduce but I only saw this once so far. The code is all internal where I work and the build that failed with functions that had unused variables was in a Travis CI build.

Not sure if this is relevant, but the C functions that cgo generated with unused variables were all in C files, not in C code at the top of a Go file.

@gopherbot

This comment has been minimized.

Copy link
Author

commented Apr 13, 2018

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@gopherbot gopherbot closed this Apr 13, 2018

@conradoplg

This comment has been minimized.

Copy link
Contributor

commented Apr 23, 2018

To reproduce:

main.go:

package main

// #cgo CFLAGS: -Wall -W
import "C"

func main() {}

Run:

go build .

Output:

_cgo_main.c: In function ‘crosscall2’:
_cgo_main.c:2:23: warning: unused parameter ‘fn’ [-Wunused-parameter]
 void crosscall2(void(*fn)(void*, int, __SIZE_TYPE__), void *a, int c, __SIZE_TYPE__ ctxt) { }
                       ^
_cgo_main.c:2:61: warning: unused parameter ‘a’ [-Wunused-parameter]
 void crosscall2(void(*fn)(void*, int, __SIZE_TYPE__), void *a, int c, __SIZE_TYPE__ ctxt) { }
                                                             ^
_cgo_main.c:2:68: warning: unused parameter ‘c’ [-Wunused-parameter]
 void crosscall2(void(*fn)(void*, int, __SIZE_TYPE__), void *a, int c, __SIZE_TYPE__ ctxt) { }
                                                                    ^
_cgo_main.c:2:85: warning: unused parameter ‘ctxt’ [-Wunused-parameter]
 void crosscall2(void(*fn)(void*, int, __SIZE_TYPE__), void *a, int c, __SIZE_TYPE__ ctxt) { }
                                                                                     ^
_cgo_main.c: In function ‘_cgo_release_context’:
_cgo_main.c:4:41: warning: unused parameter ‘ctxt’ [-Wunused-parameter]
 void _cgo_release_context(__SIZE_TYPE__ ctxt) { }
                                         ^
_cgo_main.c: In function ‘_cgo_allocate’:
_cgo_main.c:6:26: warning: unused parameter ‘a’ [-Wunused-parameter]
 void _cgo_allocate(void *a, int c) { }
                          ^
_cgo_main.c:6:33: warning: unused parameter ‘c’ [-Wunused-parameter]
 void _cgo_allocate(void *a, int c) { }
                                 ^
_cgo_main.c: In function ‘_cgo_panic’:
_cgo_main.c:7:23: warning: unused parameter ‘a’ [-Wunused-parameter]
 void _cgo_panic(void *a, int c) { }
                       ^
_cgo_main.c:7:30: warning: unused parameter ‘c’ [-Wunused-parameter]
 void _cgo_panic(void *a, int c) { }
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Apr 24, 2018

@conradoplg Do not use -Wall in #cgo CFLAGS. As you've seen, it doesn't work.

@conradoplg

This comment has been minimized.

Copy link
Contributor

commented Apr 24, 2018

@ianlancetaylor that's the issue, isn't it? (Maybe it's not exactly the same as OP's, but seems related). It appears fixable, since it's just this warning, but I understand if zero warnings is not a goal for Go.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Apr 24, 2018

Zero warnings can't be a goal. The warnings change over time, and the mechanisms required to avoid the warnings change over time. We have no idea which version of which C compiler will be used.

I'm happy to accept simple fixes to avoid warnings that are guaranteed to to work with all compiler versions. It's not something I'll work on myself. And I would not accept test cases that just add -W options to verify that some warnings are avoided.

@golang golang locked and limited conversation to collaborators Apr 24, 2019

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