diff options
author | Colin Cross <ccross@android.com> | 2018-05-24 14:22:55 -0700 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2018-05-24 14:53:58 -0700 |
commit | e467f44f9bbfbb969fe867b8a1b2e68f017ac485 (patch) | |
tree | 45d57dd6b263b9807b4164c8034a91c77d853e97 /bpfix | |
parent | c039ab084d61cb1af1938f77000a147ed6b4e25b (diff) | |
download | build_soong-e467f44f9bbfbb969fe867b8a1b2e68f017ac485.tar.gz build_soong-e467f44f9bbfbb969fe867b8a1b2e68f017ac485.tar.bz2 build_soong-e467f44f9bbfbb969fe867b8a1b2e68f017ac485.zip |
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
Diffstat (limited to 'bpfix')
-rw-r--r-- | bpfix/bpfix/bpfix.go | 147 | ||||
-rw-r--r-- | bpfix/bpfix/bpfix_test.go | 10 |
2 files changed, 73 insertions, 84 deletions
diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go index ee00907f..1cd04090 100644 --- a/bpfix/bpfix/bpfix.go +++ b/bpfix/bpfix/bpfix.go @@ -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 doesn't specify whether to do a dry run or where to write the results; that's in cmd/bpfix.go type FixRequest struct { - simplifyKnownRedundantVariables bool - rewriteIncorrectAndroidmkPrebuilts bool - rewriteIncorrectAndroidmkAndroidLibraries bool - mergeMatchingModuleProperties bool - reorderCommonProperties bool - removeTags bool + steps []fixStep +} + +type fixStep struct { + name string + 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 { @@ -58,13 +85,8 @@ func NewFixRequest() FixRequest { } func (r FixRequest) AddAll() (result FixRequest) { - result = r - result.simplifyKnownRedundantVariables = true - result.rewriteIncorrectAndroidmkPrebuilts = true - result.rewriteIncorrectAndroidmkAndroidLibraries = true - result.mergeMatchingModuleProperties = true - result.reorderCommonProperties = true - result.removeTags = true + result.steps = append([]fixStep(nil), r.steps...) + result.steps = append(result.steps, fixSteps...) return result } @@ -148,43 +170,8 @@ func parse(name string, r io.Reader) (*parser.File, error) { } func (f *Fixer) fixTreeOnce(config FixRequest) error { - if config.simplifyKnownRedundantVariables { - err := f.runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther) - 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) + for _, fix := range config.steps { + err := fix.fix(f) if err != nil { return err } @@ -198,7 +185,7 @@ func simplifyKnownPropertiesDuplicatingEachOther(mod *parser.Module, buf []byte, "export_include_dirs", "local_include_dirs") } -func (f *Fixer) rewriteIncorrectAndroidmkPrebuilts() error { +func rewriteIncorrectAndroidmkPrebuilts(f *Fixer) error { for _, def := range f.tree.Defs { mod, ok := def.(*parser.Module) if !ok { @@ -234,7 +221,7 @@ func (f *Fixer) rewriteIncorrectAndroidmkPrebuilts() error { return nil } -func (f *Fixer) rewriteIncorrectAndroidmkAndroidLibraries() error { +func rewriteIncorrectAndroidmkAndroidLibraries(f *Fixer) error { for _, def := range f.tree.Defs { mod, ok := def.(*parser.Module) if !ok { @@ -269,43 +256,45 @@ func (f *Fixer) rewriteIncorrectAndroidmkAndroidLibraries() error { return nil } -func (f *Fixer) runPatchListMod(modFunc func(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error) error { - // Make sure all the offsets are accurate - buf, err := f.reparse() - if err != nil { - return err - } +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 + buf, err := f.reparse() + if err != nil { + return err + } - var patchlist parser.PatchList - for _, def := range f.tree.Defs { - mod, ok := def.(*parser.Module) - if !ok { - continue + var patchlist parser.PatchList + for _, def := range f.tree.Defs { + mod, ok := def.(*parser.Module) + if !ok { + continue + } + + err := modFunc(mod, buf, &patchlist) + if err != nil { + return err + } } - err := modFunc(mod, buf, &patchlist) + newBuf := new(bytes.Buffer) + err = patchlist.Apply(bytes.NewReader(buf), newBuf) if err != nil { return err } - } - newBuf := new(bytes.Buffer) - err = patchlist.Apply(bytes.NewReader(buf), newBuf) - if err != nil { - return err - } - - // Save a copy of the buffer to print for errors below - bufCopy := append([]byte(nil), newBuf.Bytes()...) + // Save a copy of the buffer to print for errors below + bufCopy := append([]byte(nil), newBuf.Bytes()...) - newTree, err := parse(f.tree.Name, newBuf) - if err != nil { - return fmt.Errorf("Failed to parse: %v\nBuffer:\n%s", err, string(bufCopy)) - } + newTree, err := parse(f.tree.Name, newBuf) + if err != nil { + return fmt.Errorf("Failed to parse: %v\nBuffer:\n%s", err, string(bufCopy)) + } - f.tree = newTree + f.tree = newTree - return nil + return nil + } } var commonPropertyPriorities = []string{ diff --git a/bpfix/bpfix/bpfix_test.go b/bpfix/bpfix/bpfix_test.go index 654ccf93..6ba93f69 100644 --- a/bpfix/bpfix/bpfix_test.go +++ b/bpfix/bpfix/bpfix_test.go @@ -66,7 +66,7 @@ func implFilterListTest(t *testing.T, local_include_dirs []string, export_includ fixer := NewFixer(tree) // apply simplifications - err := fixer.runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther) + err := runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther)(fixer) if len(errs) > 0 { t.Fatal(err) } @@ -251,7 +251,7 @@ func TestMergeMatchingProperties(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { 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 { t.Run(test.name, func(t *testing.T) { 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 { t.Run(test.name, func(t *testing.T) { 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") - }) + })(fixer) }) }) } |