@@ -11,6 +11,7 @@ import (
11
11
"go/types"
12
12
"io/ioutil"
13
13
"path/filepath"
14
+ "runtime"
14
15
"strings"
15
16
"testing"
16
17
)
@@ -21,7 +22,7 @@ var flagUpdate = flag.Bool("update", false, "Update images based on test output.
21
22
func TestCGo (t * testing.T ) {
22
23
var cflags = []string {"--target=armv6m-none-eabi" }
23
24
24
- for _ , name := range []string {"basic" , "types" } {
25
+ for _ , name := range []string {"basic" , "errors" , " types" } {
25
26
name := name // avoid a race condition
26
27
t .Run (name , func (t * testing.T ) {
27
28
t .Parallel ()
@@ -35,23 +36,19 @@ func TestCGo(t *testing.T) {
35
36
}
36
37
37
38
// Process the AST with CGo.
38
- cgoAST , errs := Process ([]* ast.File {f }, "testdata" , fset , cflags )
39
- for _ , err := range errs {
40
- t .Errorf ("error during CGo processing: %v" , err )
41
- }
39
+ cgoAST , cgoErrors := Process ([]* ast.File {f }, "testdata" , fset , cflags )
42
40
43
41
// Check the AST for type errors.
44
- hasTypeError := false
42
+ var typecheckErrors [] error
45
43
config := types.Config {
46
44
Error : func (err error ) {
47
- t .Error ("typecheck error:" , err )
48
- hasTypeError = true
45
+ typecheckErrors = append (typecheckErrors , err )
49
46
},
50
47
Importer : simpleImporter {},
51
48
Sizes : types .SizesFor ("gccgo" , "arm" ),
52
49
}
53
50
_ , err = config .Check ("" , fset , []* ast.File {f , cgoAST }, nil )
54
- if err != nil && ! hasTypeError {
51
+ if err != nil && len ( typecheckErrors ) == 0 {
55
52
// Only report errors when no type errors are found (an
56
53
// unexpected condition).
57
54
t .Error (err )
@@ -61,6 +58,20 @@ func TestCGo(t *testing.T) {
61
58
// becomes easier to read (and will hopefully change less with CGo
62
59
// changes).
63
60
buf := & bytes.Buffer {}
61
+ if len (cgoErrors ) != 0 {
62
+ buf .WriteString ("// CGo errors:\n " )
63
+ for _ , err := range cgoErrors {
64
+ buf .WriteString (formatDiagnostic (err ))
65
+ }
66
+ buf .WriteString ("\n " )
67
+ }
68
+ if len (typecheckErrors ) != 0 {
69
+ buf .WriteString ("// Type checking errors after CGo processing:\n " )
70
+ for _ , err := range typecheckErrors {
71
+ buf .WriteString (formatDiagnostic (err ))
72
+ }
73
+ buf .WriteString ("\n " )
74
+ }
64
75
err = format .Node (buf , fset , cgoAST )
65
76
if err != nil {
66
77
t .Errorf ("could not write out CGo AST: %v" , err )
@@ -107,3 +118,14 @@ func (i simpleImporter) Import(path string) (*types.Package, error) {
107
118
return nil , fmt .Errorf ("importer not implemented for package %s" , path )
108
119
}
109
120
}
121
+
122
+ // formatDiagnostics formats the error message to be an indented comment. It
123
+ // also fixes Windows path name issues (backward slashes).
124
+ func formatDiagnostic (err error ) string {
125
+ msg := err .Error ()
126
+ if runtime .GOOS == "windows" {
127
+ // Fix Windows path slashes.
128
+ msg = strings .Replace (msg , "testdata\\ " , "testdata/" , - 1 )
129
+ }
130
+ return "// " + msg + "\n "
131
+ }
0 commit comments