Skip to content

Commit

Permalink
fix: fix trimmer extends matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean committed Dec 3, 2024
1 parent 15a58b5 commit d25f95f
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions tool/trimmer/trim/mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package trim

import (
"fmt"
"strings"

"github.com/cloudwego/thriftgo/parser"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (t *Trimmer) markService(svc *parser.Service, ast *parser.Thrift, filename
}

if len(t.trimMethods) != 0 && (svc.Extends != "" || svc.Reference != nil) {
t.traceExtendMethod(svc, svc, ast, filename)
t.traceExtendMethod([]*parser.Service{svc}, svc, ast, filename)
}

if svc.Extends != "" && t.marks[filename][svc] {
Expand Down Expand Up @@ -252,15 +253,21 @@ func (t *Trimmer) markKeptPart(ast *parser.Thrift, filename string) bool {
}

// for -m, trace the extends and find specified method to base on
func (t *Trimmer) traceExtendMethod(father, svc *parser.Service, ast *parser.Thrift, filename string) (ret bool) {
func (t *Trimmer) traceExtendMethod(fathers []*parser.Service, svc *parser.Service, ast *parser.Thrift, filename string) (ret bool) {
fmt.Println(svc.Name)
for _, function := range svc.Functions {
funcName := father.Name + "." + function.Name
for i, method := range t.trimMethods {
if ok, _ := method.MatchString(funcName); ok {
t.marks[filename][svc] = true
t.markFunction(function, ast, filename)
t.trimMethodValid[i] = true
ret = true
for _, father := range fathers {
// 子 method 写了来自 extends 的某个名字的时候,都间接向上查找,遍历所有子节点的名字尝试匹配
funcName := father.Name + "." + function.Name
for i, method := range t.trimMethods {
fmt.Println("try", method.String(), "with", funcName)
if ok, _ := method.MatchString(funcName); ok {
t.marks[filename][svc] = true
t.markFunction(function, ast, filename)
t.trimMethodValid[i] = true
ret = true
fmt.Println("Yes")
}
}
}
}
Expand All @@ -284,7 +291,7 @@ func (t *Trimmer) traceExtendMethod(father, svc *parser.Service, ast *parser.Thr
}
}
}
back := t.traceExtendMethod(father, nextSvc, nextAst, filename)
back := t.traceExtendMethod(append(fathers, nextSvc), nextSvc, nextAst, filename)
if !back {
t.markServiceExtends(svc)
}
Expand Down

0 comments on commit d25f95f

Please sign in to comment.