]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: recognized untyped Go constants as untyped constants
authorIan Lance Taylor <iant@golang.org>
Fri, 16 Nov 2018 02:41:03 +0000 (18:41 -0800)
committerIan Lance Taylor <iant@golang.org>
Fri, 16 Nov 2018 23:30:19 +0000 (23:30 +0000)
Fixes #28772

Change-Id: I9446d95fb73fbcbb1cd9a4d2156ebc91bc9e91cb
Reviewed-on: https://go-review.googlesource.com/c/149858
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

misc/cgo/test/issue28545.go
src/cmd/cgo/ast.go
src/cmd/cgo/gcc.go
src/cmd/cgo/main.go

index 802a20b779e6ebc38dcff50a95d1c360caad5707..0410a1662267da4b96ce5c111febb9a1eb50fc48 100644 (file)
@@ -3,6 +3,7 @@
 // license that can be found in the LICENSE file.
 
 // Failed to add type conversion for negative constant.
+// Issue 28772: Failed to add type conversion for Go constant set to C constant.
 // No runtime test; just make sure it compiles.
 
 package cgotest
@@ -10,11 +11,16 @@ package cgotest
 /*
 #include <complex.h>
 
+#define issue28772Constant 1
+
 static void issue28545F(char **p, int n, complex double a) {}
 */
 import "C"
 
+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))
 }
index 4462136bf4bc5720ee21bc55ff52cbf76ad71408..c342a017833b567e4289a96b5e9e6d72233ea50b 100644 (file)
@@ -66,6 +66,7 @@ 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
@@ -191,6 +192,18 @@ func (f *File) saveExprs(x interface{}, context astContext) {
                }
        case *ast.CallExpr:
                f.saveCall(x, context)
+       case *ast.GenDecl:
+               if x.Tok == token.CONST {
+                       for _, spec := range x.Specs {
+                               vs := spec.(*ast.ValueSpec)
+                               if vs.Type == nil {
+                                       for _, name := range spec.(*ast.ValueSpec).Names {
+                                               f.Consts[name.Name] = true
+                                       }
+                               }
+                       }
+               }
+
        }
 }
 
index 23b60a646afc8224bfa9f2017600dc33e0c8268b..fdd34f560fd71199dc428827d6c3885b268735b3 100644 (file)
@@ -1232,7 +1232,8 @@ func (p *Package) isConst(f *File, x ast.Expr) bool {
                return x.Name == "nil" ||
                        strings.HasPrefix(x.Name, "_Ciconst_") ||
                        strings.HasPrefix(x.Name, "_Cfconst_") ||
-                       strings.HasPrefix(x.Name, "_Csconst_")
+                       strings.HasPrefix(x.Name, "_Csconst_") ||
+                       f.Consts[x.Name]
        case *ast.UnaryExpr:
                return p.isConst(f, x.X)
        case *ast.BinaryExpr:
index 3098a4a63d3dcc86ebb0b86d410a02948735fcb3..a317a1494d7824dd86d44fa69c4f029a69cab2d4 100644 (file)
@@ -62,6 +62,7 @@ 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
 }
 
 func (f *File) offset(p token.Pos) int {