Merge changes I61fdd945,I746a00a3

* changes:
  bpfix: Convert local_include_dirs removal to PatchList
  bpfix: Add reorderCommonProperties
This commit is contained in:
Treehugger Robot
2018-05-15 00:08:08 +00:00
committed by Gerrit Code Review
2 changed files with 400 additions and 67 deletions

View File

@@ -49,6 +49,7 @@ type FixRequest struct {
rewriteIncorrectAndroidmkPrebuilts bool
rewriteIncorrectAndroidmkAndroidLibraries bool
mergeMatchingModuleProperties bool
reorderCommonProperties bool
}
func NewFixRequest() FixRequest {
@@ -61,6 +62,7 @@ func (r FixRequest) AddAll() (result FixRequest) {
result.rewriteIncorrectAndroidmkPrebuilts = true
result.rewriteIncorrectAndroidmkAndroidLibraries = true
result.mergeMatchingModuleProperties = true
result.reorderCommonProperties = true
return result
}
@@ -145,11 +147,12 @@ func parse(name string, r io.Reader) (*parser.File, error) {
func (f *Fixer) fixTreeOnce(config FixRequest) error {
if config.simplifyKnownRedundantVariables {
err := f.simplifyKnownPropertiesDuplicatingEachOther()
err := f.runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther)
if err != nil {
return err
}
}
if config.rewriteIncorrectAndroidmkPrebuilts {
err := f.rewriteIncorrectAndroidmkPrebuilts()
if err != nil {
@@ -165,7 +168,14 @@ func (f *Fixer) fixTreeOnce(config FixRequest) error {
}
if config.mergeMatchingModuleProperties {
err := f.mergeMatchingModuleProperties()
err := f.runPatchListMod(mergeMatchingModuleProperties)
if err != nil {
return err
}
}
if config.reorderCommonProperties {
err := f.runPatchListMod(reorderCommonProperties)
if err != nil {
return err
}
@@ -173,9 +183,10 @@ func (f *Fixer) fixTreeOnce(config FixRequest) error {
return nil
}
func (f *Fixer) simplifyKnownPropertiesDuplicatingEachOther() error {
func simplifyKnownPropertiesDuplicatingEachOther(mod *parser.Module, buf []byte, patchList *parser.PatchList) error {
// remove from local_include_dirs anything in export_include_dirs
return f.removeMatchingModuleListProperties("export_include_dirs", "local_include_dirs")
return removeMatchingModuleListProperties(mod, patchList,
"export_include_dirs", "local_include_dirs")
}
func (f *Fixer) rewriteIncorrectAndroidmkPrebuilts() error {
@@ -249,7 +260,7 @@ func (f *Fixer) rewriteIncorrectAndroidmkAndroidLibraries() error {
return nil
}
func (f *Fixer) mergeMatchingModuleProperties() error {
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 {
@@ -263,7 +274,7 @@ func (f *Fixer) mergeMatchingModuleProperties() error {
continue
}
err := mergeMatchingProperties(&mod.Properties, buf, &patchlist)
err := modFunc(mod, buf, &patchlist)
if err != nil {
return err
}
@@ -275,9 +286,12 @@ func (f *Fixer) mergeMatchingModuleProperties() error {
return err
}
// 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 err
return fmt.Errorf("Failed to parse: %v\nBuffer:\n%s", err, string(bufCopy))
}
f.tree = newTree
@@ -285,6 +299,63 @@ func (f *Fixer) mergeMatchingModuleProperties() error {
return nil
}
var commonPropertyPriorities = []string{
"name",
"defaults",
"device_supported",
"host_supported",
}
func reorderCommonProperties(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error {
if len(mod.Properties) == 0 {
return nil
}
pos := mod.LBracePos.Offset + 1
stage := ""
for _, name := range commonPropertyPriorities {
idx := propertyIndex(mod.Properties, name)
if idx == -1 {
continue
}
if idx == 0 {
err := patchlist.Add(pos, pos, stage)
if err != nil {
return err
}
stage = ""
pos = mod.Properties[0].End().Offset + 1
mod.Properties = mod.Properties[1:]
continue
}
prop := mod.Properties[idx]
mod.Properties = append(mod.Properties[:idx], mod.Properties[idx+1:]...)
stage += string(buf[prop.Pos().Offset : prop.End().Offset+1])
err := patchlist.Add(prop.Pos().Offset, prop.End().Offset+2, "")
if err != nil {
return err
}
}
if stage != "" {
err := patchlist.Add(pos, pos, stage)
if err != nil {
return err
}
}
return nil
}
func mergeMatchingModuleProperties(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error {
return mergeMatchingProperties(&mod.Properties, buf, patchlist)
}
func mergeMatchingProperties(properties *[]*parser.Property, buf []byte, patchlist *parser.PatchList) error {
seen := make(map[string]*parser.Property)
for i := 0; i < len(*properties); i++ {
@@ -359,7 +430,7 @@ func mergeListProperties(a, b *parser.Property, buf []byte, patchlist *parser.Pa
}
// removes from <items> every item present in <removals>
func filterExpressionList(items *parser.List, removals *parser.List) {
func filterExpressionList(patchList *parser.PatchList, items *parser.List, removals *parser.List) {
writeIndex := 0
for _, item := range items.Values {
included := true
@@ -376,32 +447,39 @@ func filterExpressionList(items *parser.List, removals *parser.List) {
if included {
items.Values[writeIndex] = item
writeIndex++
} else {
patchList.Add(item.Pos().Offset, item.End().Offset+2, "")
}
}
items.Values = items.Values[:writeIndex]
}
// Remove each modules[i].Properties[<legacyName>][j] that matches a modules[i].Properties[<canonicalName>][k]
func (f *Fixer) removeMatchingModuleListProperties(canonicalName string, legacyName string) error {
for _, def := range f.tree.Defs {
mod, ok := def.(*parser.Module)
if !ok {
continue
}
legacyList, ok := getLiteralListProperty(mod, legacyName)
if !ok || len(legacyList.Values) == 0 {
continue
}
canonicalList, ok := getLiteralListProperty(mod, canonicalName)
if !ok {
continue
}
filterExpressionList(legacyList, canonicalList)
func removeMatchingModuleListProperties(mod *parser.Module, patchList *parser.PatchList, canonicalName string, legacyName string) error {
legacyProp, ok := mod.GetProperty(legacyName)
if !ok {
return nil
}
legacyList, ok := legacyProp.Value.(*parser.List)
if !ok || len(legacyList.Values) == 0 {
return nil
}
canonicalList, ok := getLiteralListProperty(mod, canonicalName)
if !ok {
return nil
}
if len(legacyList.Values) == 0 {
removeProperty(mod, legacyName)
localPatches := parser.PatchList{}
filterExpressionList(&localPatches, legacyList, canonicalList)
if len(legacyList.Values) == 0 {
patchList.Add(legacyProp.Pos().Offset, legacyProp.End().Offset+2, "")
} else {
for _, p := range localPatches {
patchList.Add(p.Start, p.End, p.Replacement)
}
}
return nil
}
@@ -419,6 +497,15 @@ func getLiteralListProperty(mod *parser.Module, name string) (list *parser.List,
return list, ok
}
func propertyIndex(props []*parser.Property, propertyName string) int {
for i, prop := range props {
if prop.Name == propertyName {
return i
}
}
return -1
}
func renameProperty(mod *parser.Module, from, to string) {
for _, prop := range mod.Properties {
if prop.Name == from {

View File

@@ -66,7 +66,7 @@ func implFilterListTest(t *testing.T, local_include_dirs []string, export_includ
fixer := NewFixer(tree)
// apply simplifications
err := fixer.simplifyKnownPropertiesDuplicatingEachOther()
err := fixer.runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther)
if len(errs) > 0 {
t.Fatal(err)
}
@@ -125,6 +125,49 @@ func TestSimplifyKnownVariablesDuplicatingEachOther(t *testing.T) {
implFilterListTest(t, []string{}, []string{}, []string{})
}
func runPass(t *testing.T, in, out string, innerTest func(*Fixer) error) {
expected, err := Reformat(out)
if err != nil {
t.Fatal(err)
}
in, err = Reformat(in)
if err != nil {
t.Fatal(err)
}
tree, errs := parser.Parse("<testcase>", bytes.NewBufferString(in), parser.NewScope(nil))
if errs != nil {
t.Fatal(errs)
}
fixer := NewFixer(tree)
got := ""
prev := "foo"
passes := 0
for got != prev && passes < 10 {
err := innerTest(fixer)
if err != nil {
t.Fatal(err)
}
out, err := parser.Print(fixer.tree)
if err != nil {
t.Fatal(err)
}
prev = got
got = string(out)
passes++
}
if got != expected {
t.Errorf("output didn't match:\ninput:\n%s\n\nexpected:\n%s\ngot:\n%s\n",
in, expected, got)
}
}
func TestMergeMatchingProperties(t *testing.T) {
tests := []struct {
name string
@@ -207,47 +250,250 @@ func TestMergeMatchingProperties(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
expected, err := Reformat(test.out)
if err != nil {
t.Error(err)
}
in, err := Reformat(test.in)
if err != nil {
t.Error(err)
}
tree, errs := parser.Parse("<testcase>", bytes.NewBufferString(in), parser.NewScope(nil))
if errs != nil {
t.Fatal(errs)
}
fixer := NewFixer(tree)
got := ""
prev := "foo"
passes := 0
for got != prev && passes < 10 {
err := fixer.mergeMatchingModuleProperties()
if err != nil {
t.Fatal(err)
}
out, err := parser.Print(fixer.tree)
if err != nil {
t.Fatal(err)
}
prev = got
got = string(out)
passes++
}
if got != expected {
t.Errorf("failed testcase '%s'\ninput:\n%s\n\nexpected:\n%s\ngot:\n%s\n",
test.name, in, expected, got)
}
runPass(t, test.in, test.out, func(fixer *Fixer) error {
return fixer.runPatchListMod(mergeMatchingModuleProperties)
})
})
}
}
func TestReorderCommonProperties(t *testing.T) {
var tests = []struct {
name string
in string
out string
}{
{
name: "empty",
in: `cc_library {}`,
out: `cc_library {}`,
},
{
name: "only priority",
in: `
cc_library {
name: "foo",
}
`,
out: `
cc_library {
name: "foo",
}
`,
},
{
name: "already in order",
in: `
cc_library {
name: "foo",
defaults: ["bar"],
}
`,
out: `
cc_library {
name: "foo",
defaults: ["bar"],
}
`,
},
{
name: "reorder only priority",
in: `
cc_library {
defaults: ["bar"],
name: "foo",
}
`,
out: `
cc_library {
name: "foo",
defaults: ["bar"],
}
`,
},
{
name: "reorder",
in: `
cc_library {
name: "foo",
srcs: ["a.c"],
host_supported: true,
defaults: ["bar"],
shared_libs: ["baz"],
}
`,
out: `
cc_library {
name: "foo",
defaults: ["bar"],
host_supported: true,
srcs: ["a.c"],
shared_libs: ["baz"],
}
`,
},
}
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)
})
})
}
}
func TestRemoveMatchingModuleListProperties(t *testing.T) {
var tests = []struct {
name string
in string
out string
}{
{
name: "simple",
in: `
cc_library {
name: "foo",
foo: ["a"],
bar: ["a"],
}
`,
out: `
cc_library {
name: "foo",
bar: ["a"],
}
`,
},
{
name: "long",
in: `
cc_library {
name: "foo",
foo: [
"a",
"b",
],
bar: ["a"],
}
`,
out: `
cc_library {
name: "foo",
foo: [
"b",
],
bar: ["a"],
}
`,
},
{
name: "long fully removed",
in: `
cc_library {
name: "foo",
foo: [
"a",
],
bar: ["a"],
}
`,
out: `
cc_library {
name: "foo",
bar: ["a"],
}
`,
},
{
name: "comment",
in: `
cc_library {
name: "foo",
// comment
foo: ["a"],
bar: ["a"],
}
`,
out: `
cc_library {
name: "foo",
// comment
bar: ["a"],
}
`,
},
{
name: "inner comment",
in: `
cc_library {
name: "foo",
foo: [
// comment
"a",
],
bar: ["a"],
}
`,
out: `
cc_library {
name: "foo",
bar: ["a"],
}
`,
},
{
name: "eol comment",
in: `
cc_library {
name: "foo",
foo: ["a"], // comment
bar: ["a"],
}
`,
out: `
cc_library {
name: "foo",
// comment
bar: ["a"],
}
`,
},
{
name: "eol comment with blank lines",
in: `
cc_library {
name: "foo",
foo: ["a"], // comment
// bar
bar: ["a"],
}
`,
out: `
cc_library {
name: "foo",
// comment
// bar
bar: ["a"],
}
`,
},
}
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 removeMatchingModuleListProperties(mod, patchList, "bar", "foo")
})
})
})
}
}