]> Cypherpunks repositories - gostls13.git/commitdiff
cgo: process DWARF info even when debug data is used for value
authorJoel Sing <jsing@google.com>
Thu, 20 Sep 2012 03:20:33 +0000 (13:20 +1000)
committerJoel Sing <jsing@google.com>
Thu, 20 Sep 2012 03:20:33 +0000 (13:20 +1000)
Always process the DWARF info, even when the const value is determined
using the debug data block. This ensures that the injected enum is
removed and future loads of the same constant do not trigger
inconsistent definitions.

Add tests for issues 2470 and 4054.
Fixes #4054.

R=golang-dev, fullung, dave, rsc, minux.ma
CC=golang-dev
https://golang.org/cl/6501101

misc/cgo/test/basic.go
misc/cgo/test/cgo_test.go
misc/cgo/test/issue4054a.go [new file with mode: 0644]
misc/cgo/test/issue4054b.go [new file with mode: 0644]
src/cmd/cgo/gcc.go

index 3716a4062eb3750808ac0d713b031cccd2dfe1e8..0c91801696ed5578e7ea377565fe683fe9f9581e 100644 (file)
@@ -14,6 +14,7 @@ package cgotest
 
 #define SHIFT(x, y)  ((x)<<(y))
 #define KILO SHIFT(1, 10)
+#define UINT32VAL 0xc008427bU
 
 enum E {
        Enum1 = 1,
@@ -141,3 +142,12 @@ func benchCgoCall(b *testing.B) {
                C.add(x, y)
        }
 }
+
+// Issue 2470.
+func testUnsignedInt(t *testing.T) {
+       a := (int64)(C.UINT32VAL)
+       b := (int64)(0xc008427b)
+       if a != b {
+               t.Errorf("Incorrect unsigned int - got %x, want %x", a, b)
+       }
+}
index c3c35b53c2fd7dd9e2b5e4720277e698a3e99c51..3b866290fe29b3b9e3b829d21402b2609c6b3a61 100644 (file)
@@ -16,6 +16,7 @@ func TestEnum(t *testing.T)                { testEnum(t) }
 func TestAtol(t *testing.T)                { testAtol(t) }
 func TestErrno(t *testing.T)               { testErrno(t) }
 func TestMultipleAssign(t *testing.T)      { testMultipleAssign(t) }
+func TestUnsignedInt(t *testing.T)         { testUnsignedInt(t) }
 func TestCallback(t *testing.T)            { testCallback(t) }
 func TestCallbackGC(t *testing.T)          { testCallbackGC(t) }
 func TestCallbackPanic(t *testing.T)       { testCallbackPanic(t) }
diff --git a/misc/cgo/test/issue4054a.go b/misc/cgo/test/issue4054a.go
new file mode 100644 (file)
index 0000000..2abdac5
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2012 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
+
+/*
+typedef enum {
+       A = 0,
+       B,
+       C,
+       D,
+       E,
+       F,
+       G,
+       H,
+       I,
+       J,
+} issue4054a;
+*/
+import "C"
+
+var issue4054a = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.I, C.J}
diff --git a/misc/cgo/test/issue4054b.go b/misc/cgo/test/issue4054b.go
new file mode 100644 (file)
index 0000000..048964c
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2012 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
+
+/*
+typedef enum {
+       A = 0,
+       B,
+       C,
+       D,
+       E,
+       F,
+       G,
+       H,
+       I,
+       J,
+} issue4054b;
+*/
+import "C"
+
+var issue4054b = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.I, C.J}
index 2aaa570d8305932d6b16125ac5648053c02777e9..d16d0202f663e053ae14a3f952e625311295ce82 100644 (file)
@@ -616,10 +616,7 @@ func (p *Package) loadDWARF(f *File, names []*Name) {
                        n.FuncType = conv.FuncType(f, pos)
                } else {
                        n.Type = conv.Type(types[i], pos)
-                       // Prefer debug data over DWARF debug output, if we have it.
-                       if n.Kind == "const" && i < len(enumVal) {
-                               n.Const = fmt.Sprintf("%#x", enumVal[i])
-                       } else if enums[i] != 0 && n.Type.EnumValues != nil {
+                       if enums[i] != 0 && n.Type.EnumValues != nil {
                                k := fmt.Sprintf("__cgo_enum__%d", i)
                                n.Kind = "const"
                                n.Const = fmt.Sprintf("%#x", n.Type.EnumValues[k])
@@ -627,6 +624,10 @@ func (p *Package) loadDWARF(f *File, names []*Name) {
                                // equally in future loads of the same constant.
                                delete(n.Type.EnumValues, k)
                        }
+                       // Prefer debug data over DWARF debug output, if we have it.
+                       if n.Kind == "const" && i < len(enumVal) {
+                               n.Const = fmt.Sprintf("%#x", enumVal[i])
+                       }
                }
        }