From a431a7bfb69e2b43812afb9b02130d88dc0527ab Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 12 Jan 2021 14:41:14 +0800 Subject: [PATCH 1/4] cmd/cgo: fix unused parameter warnings in generated _cgo_main.c Applying -Werror compiler option to request warnings is an usual way to discover potential errors. Go user may put a cgo directive in preamble: `// #cgo CFLAGS: -Werror=unused-parameter`. However, the directive also takes effect on the cgo generated files. I cleaned _cgo_main.c to help Go user only concentrate on warnings of their own file. Fixes #43639 --- src/cmd/cgo/out.go | 8 ++++---- src/cmd/cgo/unused_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/cmd/cgo/unused_test.go diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 11c53facf8f979..437a4e2da25af4 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -59,9 +59,9 @@ func (p *Package) writeDefs() { // Write C main file for using gcc to resolve imports. fmt.Fprintf(fm, "int main() { return 0; }\n") if *importRuntimeCgo { - fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*), void *a, int c, __SIZE_TYPE__ ctxt) { }\n") + fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*), void *a, int c, __SIZE_TYPE__ ctxt) { (void)fn; (void)a; (void)c; (void)ctxt; }\n") fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void) { return 0; }\n") - fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__ ctxt) { }\n") + fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__ ctxt) { (void)ctxt; }\n") fmt.Fprintf(fm, "char* _cgo_topofstack(void) { return (char*)0; }\n") } else { // If we're not importing runtime/cgo, we *are* runtime/cgo, @@ -70,8 +70,8 @@ func (p *Package) writeDefs() { fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void);\n") fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__);\n") } - fmt.Fprintf(fm, "void _cgo_allocate(void *a, int c) { }\n") - fmt.Fprintf(fm, "void _cgo_panic(void *a, int c) { }\n") + fmt.Fprintf(fm, "void _cgo_allocate(void *a, int c) { (void)a; (void)c; }\n") + fmt.Fprintf(fm, "void _cgo_panic(void *a, int c) { (void)a; (void)c; }\n") fmt.Fprintf(fm, "void _cgo_reginit(void) { }\n") // Write second Go output: definitions of _C_xxx. diff --git a/src/cmd/cgo/unused_test.go b/src/cmd/cgo/unused_test.go new file mode 100644 index 00000000000000..5a0e47693b03ea --- /dev/null +++ b/src/cmd/cgo/unused_test.go @@ -0,0 +1,40 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "os" + "os/exec" + "testing" +) + +func TestUnusedParameter(t *testing.T) { + + filename := "tmp.go" + file, err := os.Create(filename) + if err != nil { + t.Fatal(err) + } + defer os.Remove(filename) + defer file.Close() + + code := ` + package p + + // #cgo CFLAGS: -Werror=unused-parameter + import "C" + ` + file.WriteString(code) + + cmd := exec.Command("../../../bin/go", "build", filename) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatal(err) + } + if len(out) != 0 { + t.Fatalf("unexpected output: %s", string(out)) + } + +} From a06530ebaa095c5bcc192cb3778f003c40a8b97a Mon Sep 17 00:00:00 2001 From: Hans Date: Wed, 13 Jan 2021 19:43:23 +0800 Subject: [PATCH 2/4] cmd/cgo: use __attribute__ ((unused)) and improve the test --- misc/cgo/test/cgo_test.go | 1 + .../cgo/test/issue43639.go | 19 ++++++++++++------- src/cmd/cgo/out.go | 8 ++++---- 3 files changed, 17 insertions(+), 11 deletions(-) rename src/cmd/cgo/unused_test.go => misc/cgo/test/issue43639.go (61%) diff --git a/misc/cgo/test/cgo_test.go b/misc/cgo/test/cgo_test.go index f7a76d047bd951..45d8befbcf589c 100644 --- a/misc/cgo/test/cgo_test.go +++ b/misc/cgo/test/cgo_test.go @@ -59,6 +59,7 @@ func Test28896(t *testing.T) { test28896(t) } func Test30065(t *testing.T) { test30065(t) } func Test32579(t *testing.T) { test32579(t) } func Test31891(t *testing.T) { test31891(t) } +func Test43639(t *testing.T) { test43639(t) } func TestAlign(t *testing.T) { testAlign(t) } func TestAtol(t *testing.T) { testAtol(t) } func TestBlocking(t *testing.T) { testBlocking(t) } diff --git a/src/cmd/cgo/unused_test.go b/misc/cgo/test/issue43639.go similarity index 61% rename from src/cmd/cgo/unused_test.go rename to misc/cgo/test/issue43639.go index 5a0e47693b03ea..1aba9888917fe8 100644 --- a/src/cmd/cgo/unused_test.go +++ b/misc/cgo/test/issue43639.go @@ -2,33 +2,39 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package main +package cgotest import ( + "io/ioutil" "os" "os/exec" + "path/filepath" "testing" ) -func TestUnusedParameter(t *testing.T) { +func test43639(t *testing.T) { + dir, err := ioutil.TempDir("", filepath.Base(t.Name())) + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) - filename := "tmp.go" + filename := filepath.Join(dir, "tmp.go") file, err := os.Create(filename) if err != nil { t.Fatal(err) } - defer os.Remove(filename) defer file.Close() code := ` package p - // #cgo CFLAGS: -Werror=unused-parameter + // #cgo CFLAGS: -W -Wall -Werror import "C" ` file.WriteString(code) - cmd := exec.Command("../../../bin/go", "build", filename) + cmd := exec.Command("go", "build", filename) out, err := cmd.CombinedOutput() if err != nil { t.Fatal(err) @@ -36,5 +42,4 @@ func TestUnusedParameter(t *testing.T) { if len(out) != 0 { t.Fatalf("unexpected output: %s", string(out)) } - } diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 437a4e2da25af4..b2d0bcc32b21d1 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -59,9 +59,9 @@ func (p *Package) writeDefs() { // Write C main file for using gcc to resolve imports. fmt.Fprintf(fm, "int main() { return 0; }\n") if *importRuntimeCgo { - fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*), void *a, int c, __SIZE_TYPE__ ctxt) { (void)fn; (void)a; (void)c; (void)ctxt; }\n") + fmt.Fprintf(fm, "void crosscall2(void(* __attribute__((unused)) fn)(void*), void * __attribute__((unused)) a, int __attribute__((unused)) c, __SIZE_TYPE__ __attribute__((unused)) ctxt) { }\n") fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void) { return 0; }\n") - fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__ ctxt) { (void)ctxt; }\n") + fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__ __attribute__((unused)) ctxt) { }\n") fmt.Fprintf(fm, "char* _cgo_topofstack(void) { return (char*)0; }\n") } else { // If we're not importing runtime/cgo, we *are* runtime/cgo, @@ -70,8 +70,8 @@ func (p *Package) writeDefs() { fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void);\n") fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__);\n") } - fmt.Fprintf(fm, "void _cgo_allocate(void *a, int c) { (void)a; (void)c; }\n") - fmt.Fprintf(fm, "void _cgo_panic(void *a, int c) { (void)a; (void)c; }\n") + fmt.Fprintf(fm, "void _cgo_allocate(void * __attribute__((unused)) a, int __attribute__((unused)) c) { }\n") + fmt.Fprintf(fm, "void _cgo_panic(void * __attribute__((unused)) a, int __attribute__((unused)) c) { }\n") fmt.Fprintf(fm, "void _cgo_reginit(void) { }\n") // Write second Go output: definitions of _C_xxx. From b24d43a8b813818328a9e6b27eee5c7c68fe492f Mon Sep 17 00:00:00 2001 From: Hans Date: Thu, 14 Jan 2021 13:46:09 +0800 Subject: [PATCH 3/4] cmd/cgo: simplify the test --- misc/cgo/test/cgo_test.go | 1 - misc/cgo/test/issue43639.go | 45 -------------------------- misc/cgo/test/testdata/issue43639.go | 9 ++++++ misc/cgo/test/testdata/issue43639/a.go | 8 +++++ 4 files changed, 17 insertions(+), 46 deletions(-) delete mode 100644 misc/cgo/test/issue43639.go create mode 100644 misc/cgo/test/testdata/issue43639.go create mode 100644 misc/cgo/test/testdata/issue43639/a.go diff --git a/misc/cgo/test/cgo_test.go b/misc/cgo/test/cgo_test.go index 45d8befbcf589c..f7a76d047bd951 100644 --- a/misc/cgo/test/cgo_test.go +++ b/misc/cgo/test/cgo_test.go @@ -59,7 +59,6 @@ func Test28896(t *testing.T) { test28896(t) } func Test30065(t *testing.T) { test30065(t) } func Test32579(t *testing.T) { test32579(t) } func Test31891(t *testing.T) { test31891(t) } -func Test43639(t *testing.T) { test43639(t) } func TestAlign(t *testing.T) { testAlign(t) } func TestAtol(t *testing.T) { testAtol(t) } func TestBlocking(t *testing.T) { testBlocking(t) } diff --git a/misc/cgo/test/issue43639.go b/misc/cgo/test/issue43639.go deleted file mode 100644 index 1aba9888917fe8..00000000000000 --- a/misc/cgo/test/issue43639.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2021 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package cgotest - -import ( - "io/ioutil" - "os" - "os/exec" - "path/filepath" - "testing" -) - -func test43639(t *testing.T) { - dir, err := ioutil.TempDir("", filepath.Base(t.Name())) - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) - - filename := filepath.Join(dir, "tmp.go") - file, err := os.Create(filename) - if err != nil { - t.Fatal(err) - } - defer file.Close() - - code := ` - package p - - // #cgo CFLAGS: -W -Wall -Werror - import "C" - ` - file.WriteString(code) - - cmd := exec.Command("go", "build", filename) - out, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - if len(out) != 0 { - t.Fatalf("unexpected output: %s", string(out)) - } -} diff --git a/misc/cgo/test/testdata/issue43639.go b/misc/cgo/test/testdata/issue43639.go new file mode 100644 index 00000000000000..e755fbd4bc005f --- /dev/null +++ b/misc/cgo/test/testdata/issue43639.go @@ -0,0 +1,9 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cgotest + +// Issue 43639: No runtime test needed, make sure package cgotest/issue43639 compiles well. + +import _ "cgotest/issue43639" diff --git a/misc/cgo/test/testdata/issue43639/a.go b/misc/cgo/test/testdata/issue43639/a.go new file mode 100644 index 00000000000000..fe37d5e4b0f00d --- /dev/null +++ b/misc/cgo/test/testdata/issue43639/a.go @@ -0,0 +1,8 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package issue43639 + +// #cgo CFLAGS: -W -Wall -Werror +import "C" From f09d172f979acfba855be8108e7d79ec2778c406 Mon Sep 17 00:00:00 2001 From: Hans Date: Fri, 15 Jan 2021 14:30:03 +0800 Subject: [PATCH 4/4] cmd/cgo: move attribute specifier to the end of parameter declaration --- src/cmd/cgo/out.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index b2d0bcc32b21d1..47e16c2883d109 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -59,9 +59,9 @@ func (p *Package) writeDefs() { // Write C main file for using gcc to resolve imports. fmt.Fprintf(fm, "int main() { return 0; }\n") if *importRuntimeCgo { - fmt.Fprintf(fm, "void crosscall2(void(* __attribute__((unused)) fn)(void*), void * __attribute__((unused)) a, int __attribute__((unused)) c, __SIZE_TYPE__ __attribute__((unused)) ctxt) { }\n") + fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*) __attribute__((unused)), void *a __attribute__((unused)), int c __attribute__((unused)), __SIZE_TYPE__ ctxt __attribute__((unused))) { }\n") fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void) { return 0; }\n") - fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__ __attribute__((unused)) ctxt) { }\n") + fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__ ctxt __attribute__((unused))) { }\n") fmt.Fprintf(fm, "char* _cgo_topofstack(void) { return (char*)0; }\n") } else { // If we're not importing runtime/cgo, we *are* runtime/cgo, @@ -70,8 +70,8 @@ func (p *Package) writeDefs() { fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void);\n") fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__);\n") } - fmt.Fprintf(fm, "void _cgo_allocate(void * __attribute__((unused)) a, int __attribute__((unused)) c) { }\n") - fmt.Fprintf(fm, "void _cgo_panic(void * __attribute__((unused)) a, int __attribute__((unused)) c) { }\n") + fmt.Fprintf(fm, "void _cgo_allocate(void *a __attribute__((unused)), int c __attribute__((unused))) { }\n") + fmt.Fprintf(fm, "void _cgo_panic(void *a __attribute__((unused)), int c __attribute__((unused))) { }\n") fmt.Fprintf(fm, "void _cgo_reginit(void) { }\n") // Write second Go output: definitions of _C_xxx.