summaryrefslogtreecommitdiff
path: root/bpfix/bpfix/bpfix.go
diff options
context:
space:
mode:
Diffstat (limited to 'bpfix/bpfix/bpfix.go')
-rw-r--r--bpfix/bpfix/bpfix.go80
1 files changed, 63 insertions, 17 deletions
diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go
index d4384b274..adb0a7bb9 100644
--- a/bpfix/bpfix/bpfix.go
+++ b/bpfix/bpfix/bpfix.go
@@ -130,7 +130,11 @@ var fixSteps = []FixStep{
},
{
Name: "removePdkProperty",
- Fix: runPatchListMod(removePdkProperty),
+ Fix: runPatchListMod(removeObsoleteProperty("product_variables.pdk")),
+ },
+ {
+ Name: "removeScudoProperty",
+ Fix: runPatchListMod(removeObsoleteProperty("sanitize.scudo")),
},
}
@@ -863,7 +867,9 @@ func convertToSingleSource(mod *parser.Module, srcPropertyName string) {
}
}
-func runPatchListMod(modFunc func(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error) func(*Fixer) error {
+type patchListModFunction func(*parser.Module, []byte, *parser.PatchList) error
+
+func runPatchListMod(modFunc patchListModFunction) func(*Fixer) error {
return func(f *Fixer) error {
// Make sure all the offsets are accurate
buf, err := f.reparse()
@@ -1033,23 +1039,63 @@ func removeTags(mod *parser.Module, buf []byte, patchlist *parser.PatchList) err
return patchlist.Add(prop.Pos().Offset, prop.End().Offset+2, replaceStr)
}
-func removePdkProperty(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error {
- prop, ok := mod.GetProperty("product_variables")
- if !ok {
- return nil
- }
- propMap, ok := prop.Value.(*parser.Map)
- if !ok {
- return nil
- }
- pdkProp, ok := propMap.GetProperty("pdk")
- if !ok {
- return nil
+type propertyProvider interface {
+ GetProperty(string) (*parser.Property, bool)
+ RemoveProperty(string) bool
+}
+
+func removeNestedProperty(mod *parser.Module, patchList *parser.PatchList, propName string) error {
+ propNames := strings.Split(propName, ".")
+
+ var propProvider, toRemoveFrom propertyProvider
+ propProvider = mod
+
+ var propToRemove *parser.Property
+ for i, name := range propNames {
+ p, ok := propProvider.GetProperty(name)
+ if !ok {
+ return nil
+ }
+ // if this is the inner most element, it's time to delete
+ if i == len(propNames)-1 {
+ if propToRemove == nil {
+ // if we cannot remove the properties that the current property is nested in,
+ // remove only the current property
+ propToRemove = p
+ toRemoveFrom = propProvider
+ }
+
+ // remove the property from the list, in case we remove other properties in this list
+ toRemoveFrom.RemoveProperty(propToRemove.Name)
+ // only removing the property would leave blank line(s), remove with a patch
+ if err := patchList.Add(propToRemove.Pos().Offset, propToRemove.End().Offset+2, ""); err != nil {
+ return err
+ }
+ } else {
+ propMap, ok := p.Value.(*parser.Map)
+ if !ok {
+ return nil
+ }
+ if len(propMap.Properties) > 1 {
+ // if there are other properties in this struct, we need to keep this struct
+ toRemoveFrom = nil
+ propToRemove = nil
+ } else if propToRemove == nil {
+ // otherwise, we can remove the empty struct entirely
+ toRemoveFrom = propProvider
+ propToRemove = p
+ }
+ propProvider = propMap
+ }
}
- if len(propMap.Properties) > 1 {
- return patchlist.Add(pdkProp.Pos().Offset, pdkProp.End().Offset+2, "")
+
+ return nil
+}
+
+func removeObsoleteProperty(propName string) patchListModFunction {
+ return func(mod *parser.Module, buf []byte, patchList *parser.PatchList) error {
+ return removeNestedProperty(mod, patchList, propName)
}
- return patchlist.Add(prop.Pos().Offset, prop.End().Offset+2, "")
}
func mergeMatchingModuleProperties(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error {