-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi.go
59 lines (44 loc) · 1.7 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package gen
import (
"fmt"
"os"
"strings"
)
// API represents a Clang bindings generation.
type API struct {
// PrepareFunctionName returns a prepared function name for further processing.
PrepareFunctionName func(g *Generation, f *Function) string
// PrepareFunction prepares a function for further processing.
PrepareFunction func(f *Function)
// FilterFunction determines if a function is generateable.
FilterFunction func(f *Function) bool
// FilterFunctionParameter determines if a function parameter is generateable.
FilterFunctionParameter func(p FunctionParameter) bool
// FixFunctionName returns an unempty string if a function needs to receive a specific name.
FixFunctionName func(f *Function) string
// PrepareStructFields is called before adding struct field getters.
PrepareStructFields func(s *Struct)
// FilterStructFieldGetter determines if a getter should be generated for a field.
FilterStructFieldGetter func(f *StructField) bool
// ClangArguments holds the command line arguments for Clang.
ClangArguments []string
}
// HandleDirectory handles header files on dir and returns the *HeaderFile slice.
func (a *API) HandleDirectory(dir string) ([]*HeaderFile, error) {
headers, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("cannot read clang-c directory: %w", err)
}
headerFiles := make([]*HeaderFile, 0, len(headers))
for _, hf := range headers {
if hf.IsDir() || !strings.HasSuffix(hf.Name(), ".h") {
continue
}
h := NewHeaderFile(a, hf.Name(), dir)
if err := h.Parse(a.ClangArguments); err != nil {
return nil, fmt.Errorf("cannot handle header file %q: %w", h.FullPath(), err)
}
headerFiles = append(headerFiles, h)
}
return headerFiles, nil
}