Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat symbol validation #166

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion generator/gen_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,12 @@ func (gen *Generator) submitHelper(h *Helper) {

func (gen *Generator) writeFunctionBody(wr io.Writer, decl *tl.CDecl) {
writeStartFuncBody(wr)

validateFunc, validateRet, matched := gen.tr.GetLibrarySymbolValidation(decl.Name)
if matched {
writeValidation(wr, validateFunc, decl.Name, validateRet)
}

wr2 := new(reverseBuffer)
from, to := gen.createProxies(decl.Name, decl.Spec)
for _, proxy := range from {
Expand Down Expand Up @@ -1129,7 +1135,7 @@ var (
m map[unsafe.Pointer]struct{}
}

var cgoAllocsUnknown = new(cgoAllocMap)
var cgoAllocsUnknown = new(cgoAllocMap)

func (a *cgoAllocMap) Add(ptr unsafe.Pointer) {
a.mux.Lock()
Expand Down
6 changes: 6 additions & 0 deletions generator/gen_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
tl "github.com/xlab/c-for-go/translator"
)

const validationTemplate = "if %s(\"%s\") != nil { \n return %s \n}\n"

var (
skipName = []byte("_")
skipNameStr = "_"
Expand Down Expand Up @@ -191,3 +193,7 @@ func writeSpace(wr io.Writer, n int) {
func writeError(wr io.Writer, err error) {
fmt.Fprintf(wr, "// error: %v\n", err)
}

func writeValidation(wr io.Writer, validateFunc, funcName, retStr string) {
fmt.Fprintf(wr, validationTemplate, validateFunc, funcName, retStr)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/xlab/c-for-go
go 1.17

require (
github.com/dlclark/regexp2 v1.11.0
github.com/stretchr/testify v1.6.1
github.com/tj/go-spin v1.1.0
github.com/xlab/pkgconfig v0.0.0-20170226114623-cea12a0fd245
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
Expand Down
18 changes: 18 additions & 0 deletions translator/rules.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
package translator

import (
"github.com/dlclark/regexp2"
)

type Rules map[RuleTarget][]RuleSpec
type ConstRules map[ConstScope]ConstRule
type PtrTips map[TipScope][]TipSpec
type TypeTips map[TipScope][]TipSpec
type MemTips []TipSpec

type Validations []ValidationSpec

type ValidationSpec struct {
ValidateFunc string
Ret string
MatchedFunc string
}

func (v ValidationSpec) MatchFunc(name string) bool {
reg := regexp2.MustCompile(v.MatchedFunc, 0)
matched, _ := reg.MatchString(name)
return matched
}

type RuleSpec struct {
From, To string
Action RuleAction
Expand Down
20 changes: 20 additions & 0 deletions translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"sort"
"strings"

"github.com/dlclark/regexp2"
"modernc.org/cc/v4"
)

type Translator struct {
validations Validations
rules Rules
compiledRxs map[RuleAction]RxMap
compiledPtrTipRxs PtrTipRxMap
Expand Down Expand Up @@ -88,6 +90,7 @@ func (t TipSpecRx) Self() Tip {
}

type Config struct {
Validations Validations `yaml:"Validations"`
Rules Rules `yaml:"Rules"`
ConstRules ConstRules `yaml:"ConstRules"`
PtrTips PtrTips `yaml:"PtrTips"`
Expand Down Expand Up @@ -122,6 +125,7 @@ func New(cfg *Config) (*Translator, error) {
}

t := &Translator{
validations: cfg.Validations,
rules: cfg.Rules,
constRules: cfg.ConstRules,
typemap: cfg.Typemap,
Expand Down Expand Up @@ -167,6 +171,13 @@ func New(cfg *Config) (*Translator, error) {
} else {
t.compiledMemTipRxs = rxList
}

for _, v := range t.validations {
if _, err := regexp2.Compile(v.MatchedFunc, 0); err != nil {
return nil, fmt.Errorf("translator: %s, invalid regexp %s", err.Error(), v.MatchedFunc)
}
}

return t, nil
}

Expand Down Expand Up @@ -1051,3 +1062,12 @@ func (t *Translator) Declares() []*CDecl {
func (t *Translator) Typedefs() []*CDecl {
return t.typedefs
}

func (t *Translator) GetLibrarySymbolValidation(name string) (string, string, bool) {
for _, v := range t.validations {
if v.MatchFunc(name) {
return v.ValidateFunc, v.Ret, true
}
}
return "", "", false
}