Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(282)

Issue 11377043: code review 11377043: cmd/cgo: Fix issue with cgo cdefs

Can't Edit
Can't Publish+Mail
Start Review
Created:
11 years, 8 months ago by klueska
Modified:
11 years, 8 months ago
Reviewers:
iant
CC:
golang-dev, bradfitz, dave_cheney.net, iant
Visibility:
Public.

Description

cmd/cgo: Fix issue with cgo cdefs The problem is that the cdecl() function in cmd/cgo/godefs.go isn't properly translating the Go array type to a C array type when an asterisk follows the [] in the array type declaration (it is perfectly legal to put the asterisk on either side of the [] in go syntax, depending on how you set up your pointers). That said, the cdefs tool is only designed to translate from Go types generated using the cgo *godefs* tool -- where the godefs tool is designed to translate gcc-style C types into Go types. In essence, the cdefs tool translates from gcc-style C types to Go types (via the godefs tool), then back to kenc-style C types. Because of this, cdefs does not need to know how to translate arbitraty Go types into C, just the ones produced by godefs. The problem is that during this translation process, the logic is slightly wrong when going from (e.g.): char *array[10]; to: array [10]*int8; back to: int8 *array[10]; In the current implementation of cdecl(), the translation from the Go type declaration back to the kenc-style declaration looks for Go types of the form: name *[]type; rather than the actual generated Go type declaration of: name []*type; Both are valid Go syntax, with slightly different semantics, but the latter is the only one that can ever be generated by the godefs tools. (The semantics of the former are not directly expressible in a single C statement -- you would have to have to first typedef the array type, then declare a pointer to that typedef'd type in a separate statement). This commit changes the logic of cdecl() to look properly for, and translate, Go type declarations of the form: name []*type; Additionally, the original implementation only allowed for a single asterisk and a single sized aray (i.e. only a single level of pointer indirection, and only one set of []) on the type, whereas the patched version allows for an arbitrary number of both. Tests are included in misc/cgo/testcdefs and the all.bash script has been updated to account for these.

Patch Set 1 #

Patch Set 2 : diff -r f3170d09181d https://code.google.com/p/go #

Patch Set 3 : diff -r f3170d09181d https://code.google.com/p/go #

Patch Set 4 : diff -r 0796c8d8af24 https://code.google.com/p/go #

Patch Set 5 : diff -r c242bbf5fa8c https://code.google.com/p/go #

Total comments: 2

Patch Set 6 : diff -r 5990f8211e89 https://code.google.com/p/go #

Unified diffs Side-by-side diffs Delta from patch set Stats (+84 lines, -6 lines) Patch
A misc/cgo/testcdefs/cdefstest.c View 1 2 3 4 5 1 chunk +8 lines, -0 lines 0 comments Download
A misc/cgo/testcdefs/cdefstest.go View 1 2 3 4 5 1 chunk +41 lines, -0 lines 0 comments Download
A misc/cgo/testcdefs/main.go View 1 2 3 4 5 1 chunk +8 lines, -0 lines 0 comments Download
A misc/cgo/testcdefs/test.bash View 1 2 3 4 5 1 chunk +16 lines, -0 lines 0 comments Download
M src/cmd/cgo/godefs.go View 1 2 3 1 chunk +6 lines, -6 lines 0 comments Download
M src/run.bash View 1 2 3 4 1 chunk +5 lines, -0 lines 0 comments Download

Messages

Total messages: 13
klueska
Hello golang-dev@googlegroups.com (cc: iant@golang.org), I'd like you to review this change to https://code.google.com/p/go
11 years, 8 months ago (2013-07-16 18:52:29 UTC) #1
klueska
On 2013/07/16 18:52:29, klueska wrote: > Hello mailto:golang-dev@googlegroups.com (cc: mailto:iant@golang.org), > > I'd like you ...
11 years, 8 months ago (2013-07-16 20:37:56 UTC) #2
bradfitz
I guess cgo has no tests? On Wed, Jul 17, 2013 at 4:52 AM, <klueska@gmail.com> ...
11 years, 8 months ago (2013-07-17 00:18:35 UTC) #3
dave_cheney.net
I thought we put the tests in misc/cgo/test On Wed, Jul 17, 2013 at 10:18 ...
11 years, 8 months ago (2013-07-17 00:48:33 UTC) #4
klueska
There appear to be cgo tests in there, but nothing that tests the output of ...
11 years, 8 months ago (2013-07-17 00:52:13 UTC) #5
klueska
I've added some tests in misc/cgo/testcdefs that test this bug and verify it is fixed ...
11 years, 8 months ago (2013-07-17 21:28:23 UTC) #6
bradfitz
R=iant
11 years, 8 months ago (2013-07-23 16:35:50 UTC) #7
iant
Can you verify that the generated files like pkg/runtime/defs_linux_amd64.h are not changed by this? https://codereview.appspot.com/11377043/diff/18001/misc/cgo/testcdefs/issue11377043.c ...
11 years, 8 months ago (2013-07-24 18:54:38 UTC) #8
klueska
I have verified this for linux-386 and linux-amd64. Running the following script from $GOROOT/src returns ...
11 years, 8 months ago (2013-07-24 21:04:11 UTC) #9
iant
Looks good. Have you signed at the CLA as described near the end of http://golang.org/doc/contribute.html ...
11 years, 8 months ago (2013-07-24 22:37:03 UTC) #10
klueska
I just signed and submitted the individual contributors form. On Wed, Jul 24, 2013 at ...
11 years, 8 months ago (2013-07-24 23:09:59 UTC) #11
iant
LGTM
11 years, 8 months ago (2013-07-25 00:27:12 UTC) #12
iant
11 years, 8 months ago (2013-07-25 00:27:48 UTC) #13
*** Submitted as https://code.google.com/p/go/source/detail?r=c8e02f321281 ***

cmd/cgo: Fix issue with cgo cdefs

The problem is that the cdecl() function in cmd/cgo/godefs.go isn't
properly translating the Go array type to a C array type when an
asterisk follows the [] in the array type declaration (it is perfectly
legal to put the asterisk on either side of the [] in go syntax,
depending on how you set up your pointers).

That said, the cdefs tool is only designed to translate from Go types
generated using the cgo *godefs* tool -- where the godefs tool is
designed to translate gcc-style C types into Go types. In essence, the
cdefs tool translates from gcc-style C types to Go types (via the godefs
tool), then back to kenc-style C types. Because of this, cdefs does not
need to know how to translate arbitraty Go types into C, just the ones
produced by godefs.

The problem is that during this translation process, the logic is
slightly wrong when going from (e.g.):

char *array[10];
to:
array [10]*int8;
back to:
int8 *array[10];

In the current implementation of cdecl(), the translation from the Go
type declaration back to the kenc-style declaration looks for Go
types of the form:

name *[]type;
rather than the actual generated Go type declaration of:
name []*type;

Both are valid Go syntax, with slightly different semantics, but the
latter is the only one that can ever be generated by the godefs tools.
(The semantics of the former are not directly expressible in a
single C statement -- you would have to have to first typedef the array
type, then declare a pointer to that typedef'd type in a separate
statement).

This commit changes the logic of cdecl() to look properly for, and
translate, Go type declarations of the form:
name []*type;

Additionally, the original implementation only allowed for a single
asterisk and a single sized aray (i.e. only a single level of pointer
indirection, and only one set of []) on the type, whereas the patched
version allows for an arbitrary number of both.

Tests are included in misc/cgo/testcdefs and the all.bash script has been
updated to account for these.

R=golang-dev, bradfitz, dave, iant
CC=golang-dev
https://codereview.appspot.com/11377043

Committer: Ian Lance Taylor <iant@golang.org>
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b