aboutsummaryrefslogtreecommitdiffstats
path: root/bpfix/bpfix/bpfix.go
diff options
context:
space:
mode:
Diffstat (limited to 'bpfix/bpfix/bpfix.go')
-rw-r--r--bpfix/bpfix/bpfix.go51
1 files changed, 25 insertions, 26 deletions
diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go
index fcd4fecf..67a59099 100644
--- a/bpfix/bpfix/bpfix.go
+++ b/bpfix/bpfix/bpfix.go
@@ -41,20 +41,19 @@ func (r FixRequest) AddAll() (result FixRequest) {
// FixTree repeatedly applies the fixes listed in the given FixRequest to the given File
// until there is no fix that affects the tree
-func FixTree(tree *parser.File, config FixRequest) (fixed *parser.File, err error) {
+func FixTree(tree *parser.File, config FixRequest) error {
prevIdentifier, err := fingerprint(tree)
if err != nil {
- return nil, err
+ return err
}
- fixed = tree
maxNumIterations := 20
i := 0
for {
- fixed, err = fixTreeOnce(fixed, config)
+ err = fixTreeOnce(tree, config)
newIdentifier, err := fingerprint(tree)
if err != nil {
- return nil, err
+ return err
}
if bytes.Equal(newIdentifier, prevIdentifier) {
break
@@ -65,11 +64,11 @@ func FixTree(tree *parser.File, config FixRequest) (fixed *parser.File, err erro
// detect infinite loop
i++
if i >= maxNumIterations {
- return nil, fmt.Errorf("Applied fixes %s times and yet the tree continued to change. Is there an infinite loop?", i)
+ return fmt.Errorf("Applied fixes %s times and yet the tree continued to change. Is there an infinite loop?", i)
break
}
}
- return fixed, err
+ return err
}
// returns a unique identifier for the given tree that can be used to determine whether the tree changed
@@ -81,20 +80,19 @@ func fingerprint(tree *parser.File) (fingerprint []byte, err error) {
return bytes, nil
}
-func fixTreeOnce(tree *parser.File, config FixRequest) (fixed *parser.File, err error) {
+func fixTreeOnce(tree *parser.File, config FixRequest) error {
if config.simplifyKnownRedundantVariables {
- tree, err = simplifyKnownPropertiesDuplicatingEachOther(tree)
+ err := simplifyKnownPropertiesDuplicatingEachOther(tree)
if err != nil {
- return nil, err
+ return err
}
}
- return tree, err
+ return nil
}
-func simplifyKnownPropertiesDuplicatingEachOther(tree *parser.File) (fixed *parser.File, err error) {
+func simplifyKnownPropertiesDuplicatingEachOther(tree *parser.File) error {
// remove from local_include_dirs anything in export_include_dirs
- fixed, err = removeMatchingModuleListProperties(tree, "export_include_dirs", "local_include_dirs")
- return fixed, err
+ return removeMatchingModuleListProperties(tree, "export_include_dirs", "local_include_dirs")
}
// removes from <items> every item present in <removals>
@@ -121,29 +119,30 @@ func filterExpressionList(items *parser.List, removals *parser.List) {
}
// Remove each modules[i].Properties[<legacyName>][j] that matches a modules[i].Properties[<canonicalName>][k]
-func removeMatchingModuleListProperties(tree *parser.File, canonicalName string, legacyName string) (fixed *parser.File, err error) {
+func removeMatchingModuleListProperties(tree *parser.File, canonicalName string, legacyName string) error {
for _, def := range tree.Defs {
mod, ok := def.(*parser.Module)
if !ok {
continue
}
- legacy, ok := mod.GetProperty(legacyName)
- if !ok {
- continue
- }
- legacyList, ok := legacy.Value.(*parser.List)
- if !ok {
- continue
- }
- canonical, ok := mod.GetProperty(canonicalName)
+ legacyList, ok := getLiteralListProperty(mod, legacyName)
if !ok {
continue
}
- canonicalList, ok := canonical.Value.(*parser.List)
+ canonicalList, ok := getLiteralListProperty(mod, canonicalName)
if !ok {
continue
}
filterExpressionList(legacyList, canonicalList)
}
- return tree, nil
+ return nil
+}
+
+func getLiteralListProperty(mod *parser.Module, name string) (list *parser.List, found bool) {
+ prop, ok := mod.GetProperty(name)
+ if !ok {
+ return nil, false
+ }
+ list, ok = prop.Value.(*parser.List)
+ return list, ok
}