mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[release-branch.go1.9] cmd/cgo: support large unsigned macro again
The approach of https://golang.org/cl/43476 turned out incorrect. The problem is that the sniff introduced by the CL only work for simple expression. And when it fails it fallback to uint64, not int64, which breaks backward compatibility. In this CL, we use DWARF for guessing kind instead. That should be more reliable than previous approach. And importanly, it fallbacks to int64 even if it fails to guess kind. Fixes #21708 Change-Id: I39a18cb2efbe4faa9becdcf53d5ac68dba180d47 Reviewed-on: https://go-review.googlesource.com/60510 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-on: https://go-review.googlesource.com/60810 Reviewed-by: Hiroshi Ioka <hirochachacha@gmail.com> Reviewed-by: Chris Broadfoot <cbro@golang.org> Reviewed-on: https://go-review.googlesource.com/70970 Run-TryBot: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
33ce1682c7
commit
dffc9319f1
4 changed files with 29 additions and 31 deletions
|
|
@ -80,5 +80,6 @@ func Test20369(t *testing.T) { test20369(t) }
|
||||||
func Test18720(t *testing.T) { test18720(t) }
|
func Test18720(t *testing.T) { test18720(t) }
|
||||||
func Test20266(t *testing.T) { test20266(t) }
|
func Test20266(t *testing.T) { test20266(t) }
|
||||||
func Test20129(t *testing.T) { test20129(t) }
|
func Test20129(t *testing.T) { test20129(t) }
|
||||||
|
func Test21708(t *testing.T) { test21708(t) }
|
||||||
|
|
||||||
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
|
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
|
||||||
|
|
|
||||||
16
misc/cgo/test/issue21708.go
Normal file
16
misc/cgo/test/issue21708.go
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2017 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package cgotest
|
||||||
|
|
||||||
|
// #include <stdint.h>
|
||||||
|
// #define CAST_TO_INT64 (int64_t)(-1)
|
||||||
|
import "C"
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func test21708(t *testing.T) {
|
||||||
|
if got, want := C.CAST_TO_INT64, -1; got != want {
|
||||||
|
t.Errorf("C.CAST_TO_INT64 == %v, expected %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -305,18 +305,12 @@ func (p *Package) guessKinds(f *File) []*Name {
|
||||||
// void __cgo_f_xxx_4(void) { static const double __cgo_undefined__4 = (name); }
|
// void __cgo_f_xxx_4(void) { static const double __cgo_undefined__4 = (name); }
|
||||||
// #line xxx "not-str-lit"
|
// #line xxx "not-str-lit"
|
||||||
// void __cgo_f_xxx_5(void) { static const char __cgo_undefined__5[] = (name); }
|
// void __cgo_f_xxx_5(void) { static const char __cgo_undefined__5[] = (name); }
|
||||||
// #line xxx "not-signed-int-const"
|
|
||||||
// #if 0 < -(name)
|
|
||||||
// #line xxx "not-signed-int-const"
|
|
||||||
// #error found unsigned int
|
|
||||||
// #endif
|
|
||||||
//
|
//
|
||||||
// If we see an error at not-declared:xxx, the corresponding name is not declared.
|
// If we see an error at not-declared:xxx, the corresponding name is not declared.
|
||||||
// If we see an error at not-type:xxx, the corresponding name is a type.
|
// If we see an error at not-type:xxx, the corresponding name is a type.
|
||||||
// If we see an error at not-int-const:xxx, the corresponding name is not an integer constant.
|
// If we see an error at not-int-const:xxx, the corresponding name is not an integer constant.
|
||||||
// If we see an error at not-num-const:xxx, the corresponding name is not a number constant.
|
// If we see an error at not-num-const:xxx, the corresponding name is not a number constant.
|
||||||
// If we see an error at not-str-lit:xxx, the corresponding name is not a string literal.
|
// If we see an error at not-str-lit:xxx, the corresponding name is not a string literal.
|
||||||
// If we see an error at not-signed-int-const:xxx, the corresponding name is not a signed integer literal.
|
|
||||||
//
|
//
|
||||||
// The specific input forms are chosen so that they are valid C syntax regardless of
|
// The specific input forms are chosen so that they are valid C syntax regardless of
|
||||||
// whether name denotes a type or an expression.
|
// whether name denotes a type or an expression.
|
||||||
|
|
@ -335,18 +329,12 @@ func (p *Package) guessKinds(f *File) []*Name {
|
||||||
"#line %d \"not-num-const\"\n"+
|
"#line %d \"not-num-const\"\n"+
|
||||||
"void __cgo_f_%d_4(void) { static const double __cgo_undefined__4 = (%s); }\n"+
|
"void __cgo_f_%d_4(void) { static const double __cgo_undefined__4 = (%s); }\n"+
|
||||||
"#line %d \"not-str-lit\"\n"+
|
"#line %d \"not-str-lit\"\n"+
|
||||||
"void __cgo_f_%d_5(void) { static const char __cgo_undefined__5[] = (%s); }\n"+
|
"void __cgo_f_%d_5(void) { static const char __cgo_undefined__5[] = (%s); }\n",
|
||||||
"#line %d \"not-signed-int-const\"\n"+
|
|
||||||
"#if 0 < (%s)\n"+
|
|
||||||
"#line %d \"not-signed-int-const\"\n"+
|
|
||||||
"#error found unsigned int\n"+
|
|
||||||
"#endif\n",
|
|
||||||
i+1, i+1, n.C,
|
i+1, i+1, n.C,
|
||||||
i+1, i+1, n.C,
|
i+1, i+1, n.C,
|
||||||
i+1, i+1, n.C,
|
i+1, i+1, n.C,
|
||||||
i+1, i+1, n.C,
|
i+1, i+1, n.C,
|
||||||
i+1, i+1, n.C,
|
i+1, i+1, n.C,
|
||||||
i+1, n.C, i+1,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(&b, "#line 1 \"completed\"\n"+
|
fmt.Fprintf(&b, "#line 1 \"completed\"\n"+
|
||||||
|
|
@ -365,7 +353,6 @@ func (p *Package) guessKinds(f *File) []*Name {
|
||||||
notNumConst
|
notNumConst
|
||||||
notStrLiteral
|
notStrLiteral
|
||||||
notDeclared
|
notDeclared
|
||||||
notSignedIntConst
|
|
||||||
)
|
)
|
||||||
sawUnmatchedErrors := false
|
sawUnmatchedErrors := false
|
||||||
for _, line := range strings.Split(stderr, "\n") {
|
for _, line := range strings.Split(stderr, "\n") {
|
||||||
|
|
@ -419,8 +406,6 @@ func (p *Package) guessKinds(f *File) []*Name {
|
||||||
sniff[i] |= notNumConst
|
sniff[i] |= notNumConst
|
||||||
case "not-str-lit":
|
case "not-str-lit":
|
||||||
sniff[i] |= notStrLiteral
|
sniff[i] |= notStrLiteral
|
||||||
case "not-signed-int-const":
|
|
||||||
sniff[i] |= notSignedIntConst
|
|
||||||
default:
|
default:
|
||||||
if isError {
|
if isError {
|
||||||
sawUnmatchedErrors = true
|
sawUnmatchedErrors = true
|
||||||
|
|
@ -436,7 +421,7 @@ func (p *Package) guessKinds(f *File) []*Name {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, n := range names {
|
for i, n := range names {
|
||||||
switch sniff[i] &^ notSignedIntConst {
|
switch sniff[i] {
|
||||||
default:
|
default:
|
||||||
var tpos token.Pos
|
var tpos token.Pos
|
||||||
for _, ref := range f.Ref {
|
for _, ref := range f.Ref {
|
||||||
|
|
@ -447,11 +432,7 @@ func (p *Package) guessKinds(f *File) []*Name {
|
||||||
}
|
}
|
||||||
error_(tpos, "could not determine kind of name for C.%s", fixGo(n.Go))
|
error_(tpos, "could not determine kind of name for C.%s", fixGo(n.Go))
|
||||||
case notStrLiteral | notType:
|
case notStrLiteral | notType:
|
||||||
if sniff[i]¬SignedIntConst != 0 {
|
n.Kind = "iconst"
|
||||||
n.Kind = "uconst"
|
|
||||||
} else {
|
|
||||||
n.Kind = "iconst"
|
|
||||||
}
|
|
||||||
case notIntConst | notStrLiteral | notType:
|
case notIntConst | notStrLiteral | notType:
|
||||||
n.Kind = "fconst"
|
n.Kind = "fconst"
|
||||||
case notIntConst | notNumConst | notType:
|
case notIntConst | notNumConst | notType:
|
||||||
|
|
@ -496,7 +477,7 @@ func (p *Package) loadDWARF(f *File, names []*Name) {
|
||||||
b.WriteString("#line 1 \"cgo-dwarf-inference\"\n")
|
b.WriteString("#line 1 \"cgo-dwarf-inference\"\n")
|
||||||
for i, n := range names {
|
for i, n := range names {
|
||||||
fmt.Fprintf(&b, "__typeof__(%s) *__cgo__%d;\n", n.C, i)
|
fmt.Fprintf(&b, "__typeof__(%s) *__cgo__%d;\n", n.C, i)
|
||||||
if n.Kind == "iconst" || n.Kind == "uconst" {
|
if n.Kind == "iconst" {
|
||||||
fmt.Fprintf(&b, "enum { __cgo_enum__%d = %s };\n", i, n.C)
|
fmt.Fprintf(&b, "enum { __cgo_enum__%d = %s };\n", i, n.C)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -505,7 +486,7 @@ func (p *Package) loadDWARF(f *File, names []*Name) {
|
||||||
// so we can read them out of the object file.
|
// so we can read them out of the object file.
|
||||||
fmt.Fprintf(&b, "long long __cgodebug_ints[] = {\n")
|
fmt.Fprintf(&b, "long long __cgodebug_ints[] = {\n")
|
||||||
for _, n := range names {
|
for _, n := range names {
|
||||||
if n.Kind == "iconst" || n.Kind == "uconst" {
|
if n.Kind == "iconst" {
|
||||||
fmt.Fprintf(&b, "\t%s,\n", n.C)
|
fmt.Fprintf(&b, "\t%s,\n", n.C)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(&b, "\t0,\n")
|
fmt.Fprintf(&b, "\t0,\n")
|
||||||
|
|
@ -614,11 +595,11 @@ func (p *Package) loadDWARF(f *File, names []*Name) {
|
||||||
switch n.Kind {
|
switch n.Kind {
|
||||||
case "iconst":
|
case "iconst":
|
||||||
if i < len(ints) {
|
if i < len(ints) {
|
||||||
n.Const = fmt.Sprintf("%#x", ints[i])
|
if _, ok := types[i].(*dwarf.UintType); ok {
|
||||||
}
|
n.Const = fmt.Sprintf("%#x", uint64(ints[i]))
|
||||||
case "uconst":
|
} else {
|
||||||
if i < len(ints) {
|
n.Const = fmt.Sprintf("%#x", ints[i])
|
||||||
n.Const = fmt.Sprintf("%#x", uint64(ints[i]))
|
}
|
||||||
}
|
}
|
||||||
case "fconst":
|
case "fconst":
|
||||||
if i < len(floats) {
|
if i < len(floats) {
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ type Name struct {
|
||||||
Mangle string // name used in generated Go
|
Mangle string // name used in generated Go
|
||||||
C string // name used in C
|
C string // name used in C
|
||||||
Define string // #define expansion
|
Define string // #define expansion
|
||||||
Kind string // "iconst", "uconst", "fconst", "sconst", "type", "var", "fpvar", "func", "not-type"
|
Kind string // "iconst", "fconst", "sconst", "type", "var", "fpvar", "func", "not-type"
|
||||||
Type *Type // the type of xxx
|
Type *Type // the type of xxx
|
||||||
FuncType *FuncType
|
FuncType *FuncType
|
||||||
AddError bool
|
AddError bool
|
||||||
|
|
@ -100,7 +100,7 @@ func (n *Name) IsVar() bool {
|
||||||
return n.Kind == "var" || n.Kind == "fpvar"
|
return n.Kind == "var" || n.Kind == "fpvar"
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsConst reports whether Kind is either "iconst", "uconst", "fconst" or "sconst"
|
// IsConst reports whether Kind is either "iconst", "fconst" or "sconst"
|
||||||
func (n *Name) IsConst() bool {
|
func (n *Name) IsConst() bool {
|
||||||
return strings.HasSuffix(n.Kind, "const")
|
return strings.HasSuffix(n.Kind, "const")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue