| |
| |
| |
|
|
| package out_test |
|
|
| import ( |
| "bufio" |
| "bytes" |
| "fmt" |
| "internal/goarch" |
| "internal/testenv" |
| "os" |
| "path/filepath" |
| "regexp" |
| "strconv" |
| "strings" |
| "testing" |
| ) |
|
|
| type methodAlign struct { |
| Method string |
| Align int |
| } |
|
|
| var wantAligns = map[string]int{ |
| "ReturnEmpty": 1, |
| "ReturnOnlyUint8": 1, |
| "ReturnOnlyUint16": 2, |
| "ReturnOnlyUint32": 4, |
| "ReturnOnlyUint64": goarch.PtrSize, |
| "ReturnOnlyInt": goarch.PtrSize, |
| "ReturnOnlyPtr": goarch.PtrSize, |
| "ReturnByteSlice": goarch.PtrSize, |
| "ReturnString": goarch.PtrSize, |
| "InputAndReturnUint8": 1, |
| "MixedTypes": goarch.PtrSize, |
| } |
|
|
| |
| |
| |
| func TestAligned(t *testing.T) { |
| testenv.MustHaveGoRun(t) |
| testenv.MustHaveCGO(t) |
|
|
| testdata, err := filepath.Abs("testdata") |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| objDir := t.TempDir() |
|
|
| cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "cgo", |
| "-objdir", objDir, |
| filepath.Join(testdata, "aligned.go")) |
| cmd.Stderr = new(bytes.Buffer) |
|
|
| err = cmd.Run() |
| if err != nil { |
| t.Fatalf("%#q: %v\n%s", cmd, err, cmd.Stderr) |
| } |
|
|
| haveAligns, err := parseAlign(filepath.Join(objDir, "_cgo_export.c")) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| |
| if len(haveAligns) != len(wantAligns) { |
| t.Fatalf("have %d methods with aligned, want %d", len(haveAligns), len(wantAligns)) |
| } |
|
|
| for i := range haveAligns { |
| method := haveAligns[i].Method |
| haveAlign := haveAligns[i].Align |
|
|
| wantAlign, ok := wantAligns[method] |
| if !ok { |
| t.Errorf("method %s: have aligned %d, want missing entry", method, haveAlign) |
| } else if haveAlign != wantAlign { |
| t.Errorf("method %s: have aligned %d, want %d", method, haveAlign, wantAlign) |
| } |
| } |
| } |
|
|
| func parseAlign(filename string) ([]methodAlign, error) { |
| file, err := os.Open(filename) |
| if err != nil { |
| return nil, fmt.Errorf("failed to open file: %w", err) |
| } |
| defer file.Close() |
|
|
| var results []methodAlign |
| scanner := bufio.NewScanner(file) |
|
|
| |
| funcRegex := regexp.MustCompile(`^struct\s+(\w+)_return\s+(\w+)\(`) |
| |
| simpleFuncRegex := regexp.MustCompile(`^Go\w+\s+(\w+)\(`) |
| |
| voidFuncRegex := regexp.MustCompile(`^void\s+(\w+)\(`) |
| |
| alignRegex := regexp.MustCompile(`__attribute__\(\(aligned\((\d+)\)\)\)`) |
|
|
| var currentMethod string |
|
|
| for scanner.Scan() { |
| line := strings.TrimSpace(scanner.Text()) |
|
|
| |
| if matches := funcRegex.FindStringSubmatch(line); matches != nil { |
| currentMethod = matches[2] |
| } else if matches := simpleFuncRegex.FindStringSubmatch(line); matches != nil { |
| |
| currentMethod = matches[1] |
| } else if matches := voidFuncRegex.FindStringSubmatch(line); matches != nil { |
| |
| currentMethod = matches[1] |
| } |
|
|
| |
| if alignMatches := alignRegex.FindStringSubmatch(line); alignMatches != nil && currentMethod != "" { |
| alignStr := alignMatches[1] |
| align, err := strconv.Atoi(alignStr) |
| if err != nil { |
| |
| currentMethod = "" |
| continue |
| } |
| results = append(results, methodAlign{ |
| Method: currentMethod, |
| Align: align, |
| }) |
| currentMethod = "" |
| } |
| } |
|
|
| if err := scanner.Err(); err != nil { |
| return nil, fmt.Errorf("error reading file: %w", err) |
| } |
|
|
| return results, nil |
| } |
|
|