cgo: do type checking in CGo testing
Such type checking should hopefully catch more bugs. This commit also fixes some existing type errors.
Этот коммит содержится в:
		
							родитель
							
								
									913131bf62
								
							
						
					
					
						коммит
						473576e756
					
				
					 2 изменённых файлов: 39 добавлений и 4 удалений
				
			
		| 
						 | 
					@ -2,10 +2,12 @@ package cgo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"go/ast"
 | 
						"go/ast"
 | 
				
			||||||
	"go/format"
 | 
						"go/format"
 | 
				
			||||||
	"go/parser"
 | 
						"go/parser"
 | 
				
			||||||
	"go/token"
 | 
						"go/token"
 | 
				
			||||||
 | 
						"go/types"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
| 
						 | 
					@ -34,6 +36,23 @@ func TestCGo(t *testing.T) {
 | 
				
			||||||
				t.Errorf("error during CGo processing: %v", err)
 | 
									t.Errorf("error during CGo processing: %v", err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// Check the AST for type errors.
 | 
				
			||||||
 | 
								hasTypeError := false
 | 
				
			||||||
 | 
								config := types.Config{
 | 
				
			||||||
 | 
									Error: func(err error) {
 | 
				
			||||||
 | 
										t.Error("typecheck error:", err)
 | 
				
			||||||
 | 
										hasTypeError = true
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									Importer: simpleImporter{},
 | 
				
			||||||
 | 
									Sizes:    types.SizesFor("gccgo", "arm"),
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								_, err = config.Check("", fset, []*ast.File{f, cgoAST}, nil)
 | 
				
			||||||
 | 
								if err != nil && !hasTypeError {
 | 
				
			||||||
 | 
									// Only report errors when no type errors are found (an
 | 
				
			||||||
 | 
									// unexpected condition).
 | 
				
			||||||
 | 
									t.Error(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// Store the (formatted) output in a buffer. Format it, so it
 | 
								// Store the (formatted) output in a buffer. Format it, so it
 | 
				
			||||||
			// becomes easier to read (and will hopefully change less with CGo
 | 
								// becomes easier to read (and will hopefully change less with CGo
 | 
				
			||||||
			// changes).
 | 
								// changes).
 | 
				
			||||||
| 
						 | 
					@ -60,3 +79,19 @@ func TestCGo(t *testing.T) {
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// simpleImporter implements the types.Importer interface, but only allows
 | 
				
			||||||
 | 
					// importing the unsafe package.
 | 
				
			||||||
 | 
					type simpleImporter struct {
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Import implements the Importer interface. For testing usage only: it only
 | 
				
			||||||
 | 
					// supports importing the unsafe package.
 | 
				
			||||||
 | 
					func (i simpleImporter) Import(path string) (*types.Package, error) {
 | 
				
			||||||
 | 
						switch path {
 | 
				
			||||||
 | 
						case "unsafe":
 | 
				
			||||||
 | 
							return types.Unsafe, nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("importer not implemented for package %s", path)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										8
									
								
								cgo/testdata/types.go
									
										
									
									
										предоставленный
									
									
								
							
							
						
						
									
										8
									
								
								cgo/testdata/types.go
									
										
									
									
										предоставленный
									
									
								
							| 
						 | 
					@ -92,16 +92,16 @@ var (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Arrays.
 | 
						// Arrays.
 | 
				
			||||||
	_ C.myIntArray
 | 
						_ C.myIntArray
 | 
				
			||||||
	_ C.myIntArrayPtr
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Test bitfield accesses.
 | 
					// Test bitfield accesses.
 | 
				
			||||||
func foo() {
 | 
					func foo() {
 | 
				
			||||||
	var x C.bitfield_t
 | 
						var x C.bitfield_t
 | 
				
			||||||
	x.start = 3
 | 
						x.start = 3
 | 
				
			||||||
	x.a = 4
 | 
						x.set_bitfield_a(4)
 | 
				
			||||||
	x.b = 1
 | 
						x.set_bitfield_b(1)
 | 
				
			||||||
	x.c = 2
 | 
						x.set_bitfield_c(2)
 | 
				
			||||||
	x.d = 10
 | 
						x.d = 10
 | 
				
			||||||
	x.e = 5
 | 
						x.e = 5
 | 
				
			||||||
 | 
						var _ C.uchar = x.bitfield_a()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Загрузка…
	
	Создание таблицы
		
		Сослаться в новой задаче