Reduce boilerplate around bpfix passes

Make it easier to add bpfix passes by putting them in a single
list.

Test: bpfix_test.go
Change-Id: I194aeeb88457800545d58aceb5d1616c6752274a
This commit is contained in:
Colin Cross
2018-05-24 14:22:55 -07:00
parent c039ab084d
commit e467f44f9b
2 changed files with 79 additions and 90 deletions

View File

@@ -45,12 +45,39 @@ func Reformat(input string) (string, error) {
// A FixRequest specifies the details of which fixes to apply to an individual file // A FixRequest specifies the details of which fixes to apply to an individual file
// A FixRequest doesn't specify whether to do a dry run or where to write the results; that's in cmd/bpfix.go // A FixRequest doesn't specify whether to do a dry run or where to write the results; that's in cmd/bpfix.go
type FixRequest struct { type FixRequest struct {
simplifyKnownRedundantVariables bool steps []fixStep
rewriteIncorrectAndroidmkPrebuilts bool }
rewriteIncorrectAndroidmkAndroidLibraries bool
mergeMatchingModuleProperties bool type fixStep struct {
reorderCommonProperties bool name string
removeTags bool fix func(f *Fixer) error
}
var fixSteps = []fixStep{
{
name: "simplifyKnownRedundantVariables",
fix: runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther),
},
{
name: "rewriteIncorrectAndroidmkPrebuilts",
fix: rewriteIncorrectAndroidmkPrebuilts,
},
{
name: "rewriteIncorrectAndroidmkAndroidLibraries",
fix: rewriteIncorrectAndroidmkAndroidLibraries,
},
{
name: "mergeMatchingModuleProperties",
fix: runPatchListMod(mergeMatchingModuleProperties),
},
{
name: "reorderCommonProperties",
fix: runPatchListMod(reorderCommonProperties),
},
{
name: "removeTags",
fix: runPatchListMod(removeTags),
},
} }
func NewFixRequest() FixRequest { func NewFixRequest() FixRequest {
@@ -58,13 +85,8 @@ func NewFixRequest() FixRequest {
} }
func (r FixRequest) AddAll() (result FixRequest) { func (r FixRequest) AddAll() (result FixRequest) {
result = r result.steps = append([]fixStep(nil), r.steps...)
result.simplifyKnownRedundantVariables = true result.steps = append(result.steps, fixSteps...)
result.rewriteIncorrectAndroidmkPrebuilts = true
result.rewriteIncorrectAndroidmkAndroidLibraries = true
result.mergeMatchingModuleProperties = true
result.reorderCommonProperties = true
result.removeTags = true
return result return result
} }
@@ -148,43 +170,8 @@ func parse(name string, r io.Reader) (*parser.File, error) {
} }
func (f *Fixer) fixTreeOnce(config FixRequest) error { func (f *Fixer) fixTreeOnce(config FixRequest) error {
if config.simplifyKnownRedundantVariables { for _, fix := range config.steps {
err := f.runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther) err := fix.fix(f)
if err != nil {
return err
}
}
if config.rewriteIncorrectAndroidmkPrebuilts {
err := f.rewriteIncorrectAndroidmkPrebuilts()
if err != nil {
return err
}
}
if config.rewriteIncorrectAndroidmkAndroidLibraries {
err := f.rewriteIncorrectAndroidmkAndroidLibraries()
if err != nil {
return err
}
}
if config.mergeMatchingModuleProperties {
err := f.runPatchListMod(mergeMatchingModuleProperties)
if err != nil {
return err
}
}
if config.reorderCommonProperties {
err := f.runPatchListMod(reorderCommonProperties)
if err != nil {
return err
}
}
if config.removeTags {
err := f.runPatchListMod(removeTags)
if err != nil { if err != nil {
return err return err
} }
@@ -198,7 +185,7 @@ func simplifyKnownPropertiesDuplicatingEachOther(mod *parser.Module, buf []byte,
"export_include_dirs", "local_include_dirs") "export_include_dirs", "local_include_dirs")
} }
func (f *Fixer) rewriteIncorrectAndroidmkPrebuilts() error { func rewriteIncorrectAndroidmkPrebuilts(f *Fixer) error {
for _, def := range f.tree.Defs { for _, def := range f.tree.Defs {
mod, ok := def.(*parser.Module) mod, ok := def.(*parser.Module)
if !ok { if !ok {
@@ -234,7 +221,7 @@ func (f *Fixer) rewriteIncorrectAndroidmkPrebuilts() error {
return nil return nil
} }
func (f *Fixer) rewriteIncorrectAndroidmkAndroidLibraries() error { func rewriteIncorrectAndroidmkAndroidLibraries(f *Fixer) error {
for _, def := range f.tree.Defs { for _, def := range f.tree.Defs {
mod, ok := def.(*parser.Module) mod, ok := def.(*parser.Module)
if !ok { if !ok {
@@ -269,7 +256,8 @@ func (f *Fixer) rewriteIncorrectAndroidmkAndroidLibraries() error {
return nil return nil
} }
func (f *Fixer) runPatchListMod(modFunc func(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error) error { func runPatchListMod(modFunc func(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error) func(*Fixer) error {
return func(f *Fixer) error {
// Make sure all the offsets are accurate // Make sure all the offsets are accurate
buf, err := f.reparse() buf, err := f.reparse()
if err != nil { if err != nil {
@@ -306,6 +294,7 @@ func (f *Fixer) runPatchListMod(modFunc func(mod *parser.Module, buf []byte, pat
f.tree = newTree f.tree = newTree
return nil return nil
}
} }
var commonPropertyPriorities = []string{ var commonPropertyPriorities = []string{

View File

@@ -66,7 +66,7 @@ func implFilterListTest(t *testing.T, local_include_dirs []string, export_includ
fixer := NewFixer(tree) fixer := NewFixer(tree)
// apply simplifications // apply simplifications
err := fixer.runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther) err := runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther)(fixer)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(err) t.Fatal(err)
} }
@@ -251,7 +251,7 @@ func TestMergeMatchingProperties(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
runPass(t, test.in, test.out, func(fixer *Fixer) error { runPass(t, test.in, test.out, func(fixer *Fixer) error {
return fixer.runPatchListMod(mergeMatchingModuleProperties) return runPatchListMod(mergeMatchingModuleProperties)(fixer)
}) })
}) })
} }
@@ -337,7 +337,7 @@ func TestReorderCommonProperties(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
runPass(t, test.in, test.out, func(fixer *Fixer) error { runPass(t, test.in, test.out, func(fixer *Fixer) error {
return fixer.runPatchListMod(reorderCommonProperties) return runPatchListMod(reorderCommonProperties)(fixer)
}) })
}) })
} }
@@ -490,9 +490,9 @@ func TestRemoveMatchingModuleListProperties(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
runPass(t, test.in, test.out, func(fixer *Fixer) error { runPass(t, test.in, test.out, func(fixer *Fixer) error {
return fixer.runPatchListMod(func(mod *parser.Module, buf []byte, patchList *parser.PatchList) error { return runPatchListMod(func(mod *parser.Module, buf []byte, patchList *parser.PatchList) error {
return removeMatchingModuleListProperties(mod, patchList, "bar", "foo") return removeMatchingModuleListProperties(mod, patchList, "bar", "foo")
}) })(fixer)
}) })
}) })
} }