aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndy Maloney <asmaloney@gmail.com>2015-08-03 09:25:33 -0400
committerIan Lance Taylor <iant@golang.org>2015-08-26 00:24:34 +0000
commitdc110f245d9469874b17709351ba637a5287f3eb (patch)
tree8542b2c304a11f1d02f367e234dab84788fe607e /src
parent38519e69d05e21909e12f6149beb3fbd3808b3f3 (diff)
downloadgo-dc110f245d9469874b17709351ba637a5287f3eb.tar.xz
cmd/cgo: annotate named return struct members in comments
If an exported function has named return variables, then show the names as comments in the return struct we create in the header file. Example here: https://groups.google.com/forum/#!topic/golang-nuts/r393ne4zIfY Change-Id: I21fb4ca2673f6977bec35ccab0cef7d42b311f96 Reviewed-on: https://go-review.googlesource.com/13061 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/cgo/out.go56
1 files changed, 32 insertions, 24 deletions
diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go
index 4a371fe702..aaa105d6fd 100644
--- a/src/cmd/cgo/out.go
+++ b/src/cmd/cgo/out.go
@@ -693,7 +693,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
}
fntype := fn.Type
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype)
if off%t.Align != 0 {
pad := t.Align - off%t.Align
@@ -711,7 +711,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
npad++
}
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype)
if off%t.Align != 0 {
pad := t.Align - off%t.Align
@@ -744,8 +744,12 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
- fmt.Fprintf(fgcch, "\t%s r%d;\n", p.cgoType(atype).C, i)
+ func(i int, aname string, atype ast.Expr) {
+ fmt.Fprintf(fgcch, "\t%s r%d;", p.cgoType(atype).C, i)
+ if len(aname) > 0 {
+ fmt.Fprintf(fgcch, " /* %s */", aname)
+ }
+ fmt.Fprint(fgcch, "\n")
})
fmt.Fprintf(fgcch, "};\n")
gccResult = "struct " + exp.ExpName + "_return"
@@ -758,7 +762,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
s += " recv"
}
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
s += ", "
}
@@ -783,7 +787,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgcc, "\ta.recv = recv;\n")
}
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
fmt.Fprintf(fgcc, "\ta.p%d = p%d;\n", i, i)
})
fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &a, %d);\n", cPrefix, exp.ExpName, off)
@@ -792,7 +796,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgcc, "\treturn a.r0;\n")
} else {
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
fmt.Fprintf(fgcc, "\tr.r%d = a.r%d;\n", i, i)
})
fmt.Fprintf(fgcc, "\treturn r;\n")
@@ -824,7 +828,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgo2, "func %s(recv ", goname)
conf.Fprint(fgo2, fset, fn.Recv.List[0].Type)
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
fmt.Fprintf(fgo2, ", p%d ", i)
conf.Fprint(fgo2, fset, atype)
})
@@ -832,7 +836,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
if gccResult != "void" {
fmt.Fprint(fgo2, " (")
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 {
fmt.Fprint(fgo2, ", ")
}
@@ -847,7 +851,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
}
fmt.Fprintf(fgo2, "recv.%s(", exp.Func.Name)
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 {
fmt.Fprint(fgo2, ", ")
}
@@ -879,13 +883,13 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
cdeclBuf := new(bytes.Buffer)
resultCount := 0
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) { resultCount++ })
+ func(i int, aname string, atype ast.Expr) { resultCount++ })
switch resultCount {
case 0:
fmt.Fprintf(cdeclBuf, "void")
case 1:
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype)
fmt.Fprintf(cdeclBuf, "%s", t.C)
})
@@ -894,9 +898,13 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
fmt.Fprintf(fgcch, "struct %s_result {\n", exp.ExpName)
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
t := p.cgoType(atype)
- fmt.Fprintf(fgcch, "\t%s r%d;\n", t.C, i)
+ fmt.Fprintf(fgcch, "\t%s r%d;", t.C, i)
+ if len(aname) > 0 {
+ fmt.Fprintf(fgcch, " /* %s */", aname)
+ }
+ fmt.Fprint(fgcch, "\n")
})
fmt.Fprintf(fgcch, "};\n")
fmt.Fprintf(cdeclBuf, "struct %s_result", exp.ExpName)
@@ -911,7 +919,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
}
// Function parameters.
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
fmt.Fprintf(cdeclBuf, ", ")
}
@@ -956,7 +964,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprint(fgcc, "recv")
}
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
fmt.Fprintf(fgcc, ", ")
}
@@ -982,7 +990,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
printer.Fprint(fgo2, fset, fn.Recv.List[0].Type)
}
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
fmt.Fprintf(fgo2, ", ")
}
@@ -993,7 +1001,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
if resultCount > 0 {
fmt.Fprintf(fgo2, " (")
forFieldList(fntype.Results,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 {
fmt.Fprint(fgo2, ", ")
}
@@ -1013,7 +1021,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
}
fmt.Fprintf(fgo2, "%s(", exp.Func.Name)
forFieldList(fntype.Params,
- func(i int, atype ast.Expr) {
+ func(i int, aname string, atype ast.Expr) {
if i > 0 {
fmt.Fprint(fgo2, ", ")
}
@@ -1071,19 +1079,19 @@ func (p *Package) gccgoSymbolPrefix() string {
}
// Call a function for each entry in an ast.FieldList, passing the
-// index into the list and the type.
-func forFieldList(fl *ast.FieldList, fn func(int, ast.Expr)) {
+// index into the list, the name if any, and the type.
+func forFieldList(fl *ast.FieldList, fn func(int, string, ast.Expr)) {
if fl == nil {
return
}
i := 0
for _, r := range fl.List {
if r.Names == nil {
- fn(i, r.Type)
+ fn(i, "", r.Type)
i++
} else {
- for range r.Names {
- fn(i, r.Type)
+ for _, n := range r.Names {
+ fn(i, n.Name, r.Type)
i++
}
}