From e50346d26a935cd43023856d0df65a158d867c00 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Sun, 1 May 2016 17:03:46 -0700 Subject: [PATCH] cmd/cgo, misc/cgo/test: make -Wdeclaration-after-statement clean I got a complaint that cgo output triggers warnings with -Wdeclaration-after-statement. I don't think it's worth testing for this--C has permitted declarations after statements since C99--but it is easy enough to fix. It may break again; so it goes. This CL also fixes errno handling to avoid getting confused if the tsan functions happen to change the global errno variable. Change-Id: I0ec7c63a6be5653ef44799d134c8d27cb5efa441 Reviewed-on: https://go-review.googlesource.com/22686 Run-TryBot: Ian Lance Taylor Reviewed-by: Minux Ma TryBot-Result: Gobot Gobot --- misc/cgo/test/issue3250.go | 2 +- misc/cgo/test/issue5986.go | 3 ++- src/cmd/cgo/out.go | 20 +++++++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/misc/cgo/test/issue3250.go b/misc/cgo/test/issue3250.go index b1ff03941d70f..4df3e348c889d 100644 --- a/misc/cgo/test/issue3250.go +++ b/misc/cgo/test/issue3250.go @@ -13,9 +13,9 @@ package cgotest #include static void *thread(void *p) { - (void)p; const int M = 100; int i; + (void)p; for (i = 0; i < M; i++) { pthread_kill(pthread_self(), SIGCHLD); usleep(rand() % 20 + 5); diff --git a/misc/cgo/test/issue5986.go b/misc/cgo/test/issue5986.go index 4f772cdb967c9..b6a5b685f9b1d 100644 --- a/misc/cgo/test/issue5986.go +++ b/misc/cgo/test/issue5986.go @@ -13,6 +13,7 @@ static void output5986() { int current_row = 0, row_count = 0; double sum_squares = 0; + double d; do { if (current_row == 10) { current_row = 0; @@ -20,7 +21,7 @@ static void output5986() ++row_count; } while (current_row++ != 1); - double d = sqrt(sum_squares / row_count); + d = sqrt(sum_squares / row_count); printf("sqrt is: %g\n", d); } */ diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 1fa3a93becb4e..e91abe6e9d276 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -568,7 +568,7 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) { fmt.Fprintf(fgcc, "_cgo%s%s(void *v)\n", cPrefix, n.Mangle) fmt.Fprintf(fgcc, "{\n") if n.AddError { - fmt.Fprintf(fgcc, "\terrno = 0;\n") + fmt.Fprintf(fgcc, "\tint _cgo_errno;\n") } // We're trying to write a gcc struct that matches gc's layout. // Use packed attribute to force no padding in this struct in case @@ -578,11 +578,18 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) { // Save the stack top for use below. fmt.Fprintf(fgcc, "\tchar *stktop = _cgo_topofstack();\n") } + tr := n.FuncType.Result + if tr != nil { + fmt.Fprintf(fgcc, "\t__typeof__(a->r) r;\n") + } fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n") + if n.AddError { + fmt.Fprintf(fgcc, "\terrno = 0;\n") + } fmt.Fprintf(fgcc, "\t") - if t := n.FuncType.Result; t != nil { - fmt.Fprintf(fgcc, "__typeof__(a->r) r = ") - if c := t.C.String(); c[len(c)-1] == '*' { + if tr != nil { + fmt.Fprintf(fgcc, "r = ") + if c := tr.C.String(); c[len(c)-1] == '*' { fmt.Fprint(fgcc, "(__typeof__(a->r)) ") } } @@ -604,6 +611,9 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) { fmt.Fprintf(fgcc, "a->p%d", i) } fmt.Fprintf(fgcc, ");\n") + if n.AddError { + fmt.Fprintf(fgcc, "\t_cgo_errno = errno;\n") + } fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n") if n.FuncType.Result != nil { // The cgo call may have caused a stack copy (via a callback). @@ -613,7 +623,7 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) { fmt.Fprintf(fgcc, "\ta->r = r;\n") } if n.AddError { - fmt.Fprintf(fgcc, "\treturn errno;\n") + fmt.Fprintf(fgcc, "\treturn _cgo_errno;\n") } fmt.Fprintf(fgcc, "}\n") fmt.Fprintf(fgcc, "\n")