Commit ba8f6fa0 authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/cgo: recognized untyped Go constants as untyped constants

Fixes #28772

Change-Id: I9446d95fb73fbcbb1cd9a4d2156ebc91bc9e91cb
Reviewed-on: https://go-review.googlesource.com/c/149858
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 92556886
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Failed to add type conversion for negative constant. // 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. // No runtime test; just make sure it compiles.
package cgotest package cgotest
...@@ -10,11 +11,16 @@ package cgotest ...@@ -10,11 +11,16 @@ package cgotest
/* /*
#include <complex.h> #include <complex.h>
#define issue28772Constant 1
static void issue28545F(char **p, int n, complex double a) {} static void issue28545F(char **p, int n, complex double a) {}
*/ */
import "C" import "C"
const issue28772Constant = C.issue28772Constant
func issue28545G(p **C.char) { func issue28545G(p **C.char) {
C.issue28545F(p, -1, (0)) C.issue28545F(p, -1, (0))
C.issue28545F(p, 2+3, complex(1, 1)) C.issue28545F(p, 2+3, complex(1, 1))
C.issue28545F(p, issue28772Constant, (0))
} }
...@@ -66,6 +66,7 @@ func (f *File) ParseGo(name string, src []byte) { ...@@ -66,6 +66,7 @@ func (f *File) ParseGo(name string, src []byte) {
f.Package = ast1.Name.Name f.Package = ast1.Name.Name
f.Name = make(map[string]*Name) f.Name = make(map[string]*Name)
f.NamePos = make(map[*Name]token.Pos) 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. // In ast1, find the import "C" line and get any extra C preamble.
sawC := false sawC := false
...@@ -191,6 +192,18 @@ func (f *File) saveExprs(x interface{}, context astContext) { ...@@ -191,6 +192,18 @@ func (f *File) saveExprs(x interface{}, context astContext) {
} }
case *ast.CallExpr: case *ast.CallExpr:
f.saveCall(x, context) 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
}
}
}
}
} }
} }
......
...@@ -1232,7 +1232,8 @@ func (p *Package) isConst(f *File, x ast.Expr) bool { ...@@ -1232,7 +1232,8 @@ func (p *Package) isConst(f *File, x ast.Expr) bool {
return x.Name == "nil" || return x.Name == "nil" ||
strings.HasPrefix(x.Name, "_Ciconst_") || strings.HasPrefix(x.Name, "_Ciconst_") ||
strings.HasPrefix(x.Name, "_Cfconst_") || strings.HasPrefix(x.Name, "_Cfconst_") ||
strings.HasPrefix(x.Name, "_Csconst_") strings.HasPrefix(x.Name, "_Csconst_") ||
f.Consts[x.Name]
case *ast.UnaryExpr: case *ast.UnaryExpr:
return p.isConst(f, x.X) return p.isConst(f, x.X)
case *ast.BinaryExpr: case *ast.BinaryExpr:
......
...@@ -62,6 +62,7 @@ type File struct { ...@@ -62,6 +62,7 @@ type File struct {
Name map[string]*Name // map from Go name to Name Name map[string]*Name // map from Go name to Name
NamePos map[*Name]token.Pos // map from Name to position of the first reference NamePos map[*Name]token.Pos // map from Name to position of the first reference
Edit *edit.Buffer Edit *edit.Buffer
Consts map[string]bool // untyped constants
} }
func (f *File) offset(p token.Pos) int { func (f *File) offset(p token.Pos) int {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment