]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: recognize untyped constants defined in different files
authorIan Lance Taylor <iant@golang.org>
Thu, 29 Nov 2018 00:02:15 +0000 (16:02 -0800)
committerIan Lance Taylor <iant@golang.org>
Thu, 29 Nov 2018 01:29:56 +0000 (01:29 +0000)
An untyped constant can be defined in any input file, we shouldn't
segregate them by file.

Updates #28772

Change-Id: I0347f15236833bb511eb49f86c449ee9241b0a25
Reviewed-on: https://go-review.googlesource.com/c/151600
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
misc/cgo/test/issue28545.go
misc/cgo/test/issue28772.go [new file with mode: 0644]
src/cmd/cgo/ast.go
src/cmd/cgo/gcc.go
src/cmd/cgo/main.go

index 0410a1662267da4b96ce5c111febb9a1eb50fc48..8419b89c0afbed1c726628af5d24815d92adafb5 100644 (file)
@@ -22,5 +22,5 @@ const issue28772Constant = C.issue28772Constant
 func issue28545G(p **C.char) {
        C.issue28545F(p, -1, (0))
        C.issue28545F(p, 2+3, complex(1, 1))
-       C.issue28545F(p, issue28772Constant, (0))
+       C.issue28545F(p, issue28772Constant, issue28772Constant2)
 }
diff --git a/misc/cgo/test/issue28772.go b/misc/cgo/test/issue28772.go
new file mode 100644 (file)
index 0000000..bed786b
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2018 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
+
+// Constants didn't work if defined in different source file.
+
+// #define issue28772Constant2 2
+import "C"
+
+const issue28772Constant2 = C.issue28772Constant2
index c342a017833b567e4289a96b5e9e6d72233ea50b..06058cb570d4fe4d7287169a2634b45edd03a20e 100644 (file)
@@ -66,7 +66,6 @@ func (f *File) ParseGo(name string, src []byte) {
        f.Package = ast1.Name.Name
        f.Name = make(map[string]*Name)
        f.NamePos = make(map[*Name]token.Pos)
-       f.Consts = make(map[string]bool)
 
        // In ast1, find the import "C" line and get any extra C preamble.
        sawC := false
@@ -198,7 +197,7 @@ func (f *File) saveExprs(x interface{}, context astContext) {
                                vs := spec.(*ast.ValueSpec)
                                if vs.Type == nil {
                                        for _, name := range spec.(*ast.ValueSpec).Names {
-                                               f.Consts[name.Name] = true
+                                               consts[name.Name] = true
                                        }
                                }
                        }
index fdd34f560fd71199dc428827d6c3885b268735b3..56a4775746cd63d9808a3332a98e6530bb370fca 100644 (file)
@@ -1233,7 +1233,7 @@ func (p *Package) isConst(f *File, x ast.Expr) bool {
                        strings.HasPrefix(x.Name, "_Ciconst_") ||
                        strings.HasPrefix(x.Name, "_Cfconst_") ||
                        strings.HasPrefix(x.Name, "_Csconst_") ||
-                       f.Consts[x.Name]
+                       consts[x.Name]
        case *ast.UnaryExpr:
                return p.isConst(f, x.X)
        case *ast.BinaryExpr:
index a317a1494d7824dd86d44fa69c4f029a69cab2d4..e28a57b1481d05115989c64fc95c11dc2fc7d1d4 100644 (file)
@@ -62,9 +62,11 @@ type File struct {
        Name     map[string]*Name    // map from Go name to Name
        NamePos  map[*Name]token.Pos // map from Name to position of the first reference
        Edit     *edit.Buffer
-       Consts   map[string]bool // untyped constants
 }
 
+// Untyped constants in the current package.
+var consts = make(map[string]bool)
+
 func (f *File) offset(p token.Pos) int {
        return fset.Position(p).Offset
 }