2016-04-10 15:32:26 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
cmd/cgo: stop using compiler error message text to analyze C names
The old approach to determining whether "name" was a type, constant,
or expression was to compile the C program
name;
and scan the errors and warnings generated by the compiler.
This requires looking for specific substrings in the errors and warnings,
which ties the implementation to specific compiler versions.
As compilers change their errors or drop warnings, cgo breaks.
This happens slowly but it does happen.
Clang in particular (now required on OS X) has a significant churn rate.
The new approach compiles a slightly more complex program
that is either valid C or not valid C depending on what kind of
thing "name" is. It uses only the presence or absence of an error
message on a particular line, not the error text itself. The program is:
// error if and only if name is undeclared
void f1(void) { typeof(name) *x; }
// error if and only if name is not a type
void f2(void) { name *x; }
// error if and only if name is not an integer constant
void f3(void) { enum { x = (name)*1 }; }
I had not been planning to do this until Go 1.3, because it is a
non-trivial change, but it fixes a real Xcode 5 problem in Go 1.2,
and the new code is easier to understand than the old code.
It should be significantly more robust.
Fixes #6596.
Fixes #6612.
R=golang-dev, r, james, iant
CC=golang-dev
https://golang.org/cl/15070043
2013-10-18 13:56:25 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// golang.org/issue/6612
|
|
|
|
// Test new scheme for deciding whether C.name is an expression, type, constant.
|
|
|
|
// Clang silences some warnings when the name is a #defined macro, so test those too
|
|
|
|
// (even though we now use errors exclusively, not warnings).
|
|
|
|
|
|
|
|
package cgotest
|
|
|
|
|
|
|
|
/*
|
|
|
|
void myfunc(void) {}
|
|
|
|
int myvar = 5;
|
|
|
|
const char *mytext = "abcdef";
|
|
|
|
typedef int mytype;
|
|
|
|
enum {
|
|
|
|
myenum = 1234,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define myfunc_def myfunc
|
|
|
|
#define myvar_def myvar
|
|
|
|
#define mytext_def mytext
|
|
|
|
#define mytype_def mytype
|
|
|
|
#define myenum_def myenum
|
|
|
|
#define myint_def 12345
|
|
|
|
#define myfloat_def 1.5
|
|
|
|
#define mystring_def "hello"
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func testNaming(t *testing.T) {
|
|
|
|
C.myfunc()
|
|
|
|
C.myfunc_def()
|
|
|
|
if v := C.myvar; v != 5 {
|
|
|
|
t.Errorf("C.myvar = %d, want 5", v)
|
|
|
|
}
|
|
|
|
if v := C.myvar_def; v != 5 {
|
|
|
|
t.Errorf("C.myvar_def = %d, want 5", v)
|
|
|
|
}
|
|
|
|
if s := C.GoString(C.mytext); s != "abcdef" {
|
|
|
|
t.Errorf("C.mytext = %q, want %q", s, "abcdef")
|
|
|
|
}
|
|
|
|
if s := C.GoString(C.mytext_def); s != "abcdef" {
|
|
|
|
t.Errorf("C.mytext_def = %q, want %q", s, "abcdef")
|
|
|
|
}
|
|
|
|
if c := C.myenum; c != 1234 {
|
|
|
|
t.Errorf("C.myenum = %v, want 1234", c)
|
|
|
|
}
|
|
|
|
if c := C.myenum_def; c != 1234 {
|
|
|
|
t.Errorf("C.myenum_def = %v, want 1234", c)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const c = C.myenum
|
|
|
|
if c != 1234 {
|
|
|
|
t.Errorf("C.myenum as const = %v, want 1234", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const c = C.myenum_def
|
|
|
|
if c != 1234 {
|
|
|
|
t.Errorf("C.myenum as const = %v, want 1234", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c := C.myint_def; c != 12345 {
|
|
|
|
t.Errorf("C.myint_def = %v, want 12345", c)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const c = C.myint_def
|
|
|
|
if c != 12345 {
|
|
|
|
t.Errorf("C.myint as const = %v, want 12345", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 16:34:18 -07:00
|
|
|
if c := C.myfloat_def; c != 1.5 {
|
|
|
|
t.Errorf("C.myint_def = %v, want 1.5", c)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const c = C.myfloat_def
|
|
|
|
if c != 1.5 {
|
cmd/cgo: stop using compiler error message text to analyze C names
The old approach to determining whether "name" was a type, constant,
or expression was to compile the C program
name;
and scan the errors and warnings generated by the compiler.
This requires looking for specific substrings in the errors and warnings,
which ties the implementation to specific compiler versions.
As compilers change their errors or drop warnings, cgo breaks.
This happens slowly but it does happen.
Clang in particular (now required on OS X) has a significant churn rate.
The new approach compiles a slightly more complex program
that is either valid C or not valid C depending on what kind of
thing "name" is. It uses only the presence or absence of an error
message on a particular line, not the error text itself. The program is:
// error if and only if name is undeclared
void f1(void) { typeof(name) *x; }
// error if and only if name is not a type
void f2(void) { name *x; }
// error if and only if name is not an integer constant
void f3(void) { enum { x = (name)*1 }; }
I had not been planning to do this until Go 1.3, because it is a
non-trivial change, but it fixes a real Xcode 5 problem in Go 1.2,
and the new code is easier to understand than the old code.
It should be significantly more robust.
Fixes #6596.
Fixes #6612.
R=golang-dev, r, james, iant
CC=golang-dev
https://golang.org/cl/15070043
2013-10-18 13:56:25 -06:00
|
|
|
t.Errorf("C.myint as const = %v, want 1.5", c)
|
|
|
|
}
|
2017-01-19 16:34:18 -07:00
|
|
|
}
|
cmd/cgo: stop using compiler error message text to analyze C names
The old approach to determining whether "name" was a type, constant,
or expression was to compile the C program
name;
and scan the errors and warnings generated by the compiler.
This requires looking for specific substrings in the errors and warnings,
which ties the implementation to specific compiler versions.
As compilers change their errors or drop warnings, cgo breaks.
This happens slowly but it does happen.
Clang in particular (now required on OS X) has a significant churn rate.
The new approach compiles a slightly more complex program
that is either valid C or not valid C depending on what kind of
thing "name" is. It uses only the presence or absence of an error
message on a particular line, not the error text itself. The program is:
// error if and only if name is undeclared
void f1(void) { typeof(name) *x; }
// error if and only if name is not a type
void f2(void) { name *x; }
// error if and only if name is not an integer constant
void f3(void) { enum { x = (name)*1 }; }
I had not been planning to do this until Go 1.3, because it is a
non-trivial change, but it fixes a real Xcode 5 problem in Go 1.2,
and the new code is easier to understand than the old code.
It should be significantly more robust.
Fixes #6596.
Fixes #6612.
R=golang-dev, r, james, iant
CC=golang-dev
https://golang.org/cl/15070043
2013-10-18 13:56:25 -06:00
|
|
|
|
|
|
|
if s := C.mystring_def; s != "hello" {
|
|
|
|
t.Errorf("C.mystring_def = %q, want %q", s, "hello")
|
|
|
|
}
|
|
|
|
}
|