diff --git a/internal/detection/detection.go b/internal/detection/detection.go index 1c4ee0f..4b6b730 100644 --- a/internal/detection/detection.go +++ b/internal/detection/detection.go @@ -44,10 +44,12 @@ func DetectClones(pset *parsing.ParseSet, threshold float64) (*CloneSet, error) for j := i + 1; j < len(functions); j++ { f2 := functions[j] - if isClone(f1, f2, threshold) { - cclones <- Clone{ - FunctionId1: f1.Id, - FunctionId2: f2.Id, + if math.Ceil(math.Max(float64(f1.LineLength), float64(f2.LineLength))*threshold) > math.Min(float64(f1.LineLength), float64(f1.LineLength)) { + if isClone(f1, f2, threshold) { + cclones <- Clone{ + FunctionId1: f1.Id, + FunctionId2: f2.Id, + } } } } diff --git a/internal/parsing/parsing.go b/internal/parsing/parsing.go index e4590a1..f5cdc50 100644 --- a/internal/parsing/parsing.go +++ b/internal/parsing/parsing.go @@ -7,6 +7,7 @@ import ( "go/printer" "go/token" "io/fs" + "log" "path/filepath" "strings" "sync" @@ -36,6 +37,7 @@ type Function struct { StartColumn uint EndLine uint EndColumn uint + LineLength uint Name string Params []Param Results []Param @@ -87,10 +89,19 @@ func Parse(files []string) (*ParseSet, error) { go func(file File, cfunc chan Function) { defer wg.Done() - - functions, _ := parsefile(file, parseSet.FileSet, &parseSet.Strings) - for _, function := range functions { - cfunc <- function + defer func() { + if perr := recover(); perr != nil { + log.Println("parsing failed for file", file.Path) + } + }() + + functions, errs := parseFile(file, parseSet.FileSet, &parseSet.Strings) + if len(errs) > 0 { + log.Println("prasing failed for file", file) + } else { + for _, function := range functions { + cfunc <- function + } } }(file, cfunc) } @@ -105,7 +116,7 @@ func Parse(files []string) (*ParseSet, error) { return &parseSet, nil } -func parsefile(file File, fset *token.FileSet, intern *sync.Map) ([]Function, []error) { +func parseFile(file File, fset *token.FileSet, intern *sync.Map) ([]Function, []error) { retval := make([]Function, 0, 10) errors := make([]error, 0, 1) @@ -163,6 +174,7 @@ func parsefile(file File, fset *token.FileSet, intern *sync.Map) ([]Function, [] StartColumn: uint(startPosition.Column), EndLine: uint(endPosition.Line), EndColumn: uint(endPosition.Column), + LineLength: uint(endPosition.Line - startPosition.Line + 1), Name: d.Name.Name, Params: params, Results: results,