Skip to content

Commit

Permalink
cmd/cgo, misc/cgo/test: make -Wdeclaration-after-statement clean
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Minux Ma <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
ianlancetaylor committed May 2, 2016
1 parent 45f39fb commit e50346d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion misc/cgo/test/issue3250.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ package cgotest
#include <stdlib.h>
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);
Expand Down
3 changes: 2 additions & 1 deletion misc/cgo/test/issue5986.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ static void output5986()
{
int current_row = 0, row_count = 0;
double sum_squares = 0;
double d;
do {
if (current_row == 10) {
current_row = 0;
}
++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);
}
*/
Expand Down
20 changes: 15 additions & 5 deletions src/cmd/cgo/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) ")
}
}
Expand All @@ -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).
Expand All @@ -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")
Expand Down

0 comments on commit e50346d

Please sign in to comment.