Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ae9d915 authored by Colin Cross's avatar Colin Cross Committed by android-build-merger
Browse files

Don't pretend *parser.List is immutable

am: 3fad895e

Change-Id: I342ed780933174e1126980c9f658891c0c73dd80
parents cb713799 3fad895e
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -239,12 +239,12 @@ func convertFile(filename string, buffer *bytes.Buffer) (string, []error) {
	}

	// check for common supported but undesirable structures and clean them up
	fixed, err := bpfix.FixTree(tree, bpfix.NewFixRequest().AddAll())
	err := bpfix.FixTree(tree, bpfix.NewFixRequest().AddAll())
	if err != nil {
		return "", []error{err}
	}

	out, err := bpparser.Print(fixed)
	out, err := bpparser.Print(tree)
	if err != nil {
		return "", []error{err}
	}
+25 −26
Original line number Diff line number Diff line
@@ -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)
		legacyList, ok := getLiteralListProperty(mod, legacyName)
		if !ok {
			continue
		}
		legacyList, ok := legacy.Value.(*parser.List)
		canonicalList, ok := getLiteralListProperty(mod, canonicalName)
		if !ok {
			continue
		}
		canonical, ok := mod.GetProperty(canonicalName)
		if !ok {
			continue
		filterExpressionList(legacyList, canonicalList)
	}
		canonicalList, ok := canonical.Value.(*parser.List)
		if !ok {
			continue
	return nil
}
		filterExpressionList(legacyList, canonicalList)

func getLiteralListProperty(mod *parser.Module, name string) (list *parser.List, found bool) {
	prop, ok := mod.GetProperty(name)
	if !ok {
		return nil, false
	}
	return tree, nil
	list, ok = prop.Value.(*parser.List)
	return list, ok
}
+3 −2
Original line number Diff line number Diff line
@@ -21,8 +21,9 @@ import (
	"strings"
	"testing"

	"github.com/google/blueprint/parser"
	"reflect"

	"github.com/google/blueprint/parser"
)

// TODO(jeffrygaston) remove this when position is removed from ParseNode (in b/38325146) and we can directly do reflect.DeepEqual
@@ -62,7 +63,7 @@ func implFilterListTest(t *testing.T, local_include_dirs []string, export_includ
	}

	// apply simplifications
	tree, err := simplifyKnownPropertiesDuplicatingEachOther(tree)
	err := simplifyKnownPropertiesDuplicatingEachOther(tree)
	if len(errs) > 0 {
		t.Fatal(err)
	}
+2 −2
Original line number Diff line number Diff line
@@ -75,13 +75,13 @@ func processFile(filename string, in io.Reader, out io.Writer, fixRequest bpfix.
	}

	// compute and apply any requested fixes
	fixed, err := bpfix.FixTree(file, fixRequest)
	err = bpfix.FixTree(file, fixRequest)
	if err != nil {
		return err
	}

	// output the results
	res, err := parser.Print(fixed)
	res, err := parser.Print(file)
	if err != nil {
		return err
	}