]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo, misc/cgo/test: make -Wdeclaration-after-statement clean
authorIan Lance Taylor <iant@golang.org>
Mon, 2 May 2016 00:03:46 +0000 (17:03 -0700)
committerIan Lance Taylor <iant@golang.org>
Mon, 2 May 2016 04:42:12 +0000 (04:42 +0000)
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 <iant@golang.org>
Reviewed-by: Minux Ma <minux@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

misc/cgo/test/issue3250.go
misc/cgo/test/issue5986.go
src/cmd/cgo/out.go

index b1ff03941d70f85fa2902e7b2d725c611bc7d511..4df3e348c889d30a05c772f69158c01cde204f54 100644 (file)
@@ -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);
index 4f772cdb967c9ae91b952794242b4e5a91f942c0..b6a5b685f9b1d2ac1cb719c23ee214017d5925bb 100644 (file)
@@ -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);
 }
 */
index 1fa3a93becb4e2f72356c8243de770cb5bc72b8d..e91abe6e9d276fd3224e5b7b08bf6045797bd86f 100644 (file)
@@ -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")