Add multiple property and replace functionality to bpmodify.

Test: go run bpmodify.go -w -m=libcore-memory-metrics-tests -property=something,static_libs,deps,required,test_suites -replace-property=ahat:ahat_lib,general-tests:something -s ~/aosp-master-with-phones/libcore/metrictests/memory/host/Android.bp
go test -v

Change-Id: I005b6dd675beb205f544e89c729fe9191e6470c2
This commit is contained in:
MarkDacek 2022-08-05 22:28:37 +00:00
parent ab1bac0e12
commit 856507f4d2
3 changed files with 262 additions and 113 deletions

View file

@ -2,13 +2,13 @@
// Copyright 2009 The Go Authors. All rights reserved. // Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package main
import ( import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"github.com/google/blueprint/parser"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
@ -17,36 +17,36 @@ import (
"strings" "strings"
"syscall" "syscall"
"unicode" "unicode"
"github.com/google/blueprint/parser"
) )
var ( var (
// main operation modes // main operation modes
list = flag.Bool("l", false, "list files that would be modified by bpmodify") list = flag.Bool("l", false, "list files that would be modified by bpmodify")
write = flag.Bool("w", false, "write result to (source) file instead of stdout") write = flag.Bool("w", false, "write result to (source) file instead of stdout")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files") doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
sortLists = flag.Bool("s", false, "sort touched lists, even if they were unsorted") sortLists = flag.Bool("s", false, "sort touched lists, even if they were unsorted")
targetedModules = new(identSet) targetedModules = new(identSet)
targetedProperty = new(qualifiedProperty) targetedProperties = new(qualifiedProperties)
addIdents = new(identSet) addIdents = new(identSet)
removeIdents = new(identSet) removeIdents = new(identSet)
removeProperty = flag.Bool("remove-property", false, "remove the property") removeProperty = flag.Bool("remove-property", false, "remove the property")
moveProperty = flag.Bool("move-property", false, "moves contents of property into new-location") moveProperty = flag.Bool("move-property", false, "moves contents of property into newLocation")
newLocation string newLocation string
setString *string setString *string
addLiteral *string addLiteral *string
replaceProperty = new(replacements)
) )
func init() { func init() {
flag.Var(targetedModules, "m", "comma or whitespace separated list of modules on which to operate") flag.Var(targetedModules, "m", "comma or whitespace separated list of modules on which to operate")
flag.Var(targetedProperty, "parameter", "alias to -property=`name`") flag.Var(targetedProperties, "parameter", "alias to -property=`name1[,name2[,... […]")
flag.Var(targetedProperty, "property", "fully qualified `name` of property to modify (default \"deps\")")
flag.StringVar(&newLocation, "new-location", "", " use with moveProperty to move contents of -property into a property with name -new-location ") flag.StringVar(&newLocation, "new-location", "", " use with moveProperty to move contents of -property into a property with name -new-location ")
flag.Var(targetedProperties, "property", "comma-separated list of fully qualified `name`s of properties to modify (default \"deps\")")
flag.Var(addIdents, "a", "comma or whitespace separated list of identifiers to add") flag.Var(addIdents, "a", "comma or whitespace separated list of identifiers to add")
flag.Var(stringPtrFlag{&addLiteral}, "add-literal", "a literal to add") flag.Var(stringPtrFlag{&addLiteral}, "add-literal", "a literal to add")
flag.Var(removeIdents, "r", "comma or whitespace separated list of identifiers to remove") flag.Var(removeIdents, "r", "comma or whitespace separated list of identifiers to remove")
flag.Var(stringPtrFlag{&setString}, "str", "set a string property") flag.Var(stringPtrFlag{&setString}, "str", "set a string property")
flag.Var(replaceProperty, "replace-property", "property names to be replaced, in the form of oldName1:newName1,oldName2:newName2")
flag.Usage = usage flag.Usage = usage
} }
@ -72,21 +72,16 @@ func processFile(filename string, in io.Reader, out io.Writer) error {
return err return err
} }
defer f.Close() defer f.Close()
if *write { if *write {
syscall.Flock(int(f.Fd()), syscall.LOCK_EX) syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
} }
in = f in = f
} }
src, err := ioutil.ReadAll(in) src, err := ioutil.ReadAll(in)
if err != nil { if err != nil {
return err return err
} }
r := bytes.NewBuffer(src) r := bytes.NewBuffer(src)
file, errs := parser.Parse(filename, r, parser.NewScope(nil)) file, errs := parser.Parse(filename, r, parser.NewScope(nil))
if len(errs) > 0 { if len(errs) > 0 {
for _, err := range errs { for _, err := range errs {
@ -94,7 +89,6 @@ func processFile(filename string, in io.Reader, out io.Writer) error {
} }
return fmt.Errorf("%d parsing errors", len(errs)) return fmt.Errorf("%d parsing errors", len(errs))
} }
modified, errs := findModules(file) modified, errs := findModules(file)
if len(errs) > 0 { if len(errs) > 0 {
for _, err := range errs { for _, err := range errs {
@ -102,13 +96,11 @@ func processFile(filename string, in io.Reader, out io.Writer) error {
} }
fmt.Fprintln(os.Stderr, "continuing...") fmt.Fprintln(os.Stderr, "continuing...")
} }
if modified { if modified {
res, err := parser.Print(file) res, err := parser.Print(file)
if err != nil { if err != nil {
return err return err
} }
if *list { if *list {
fmt.Fprintln(out, filename) fmt.Fprintln(out, filename)
} }
@ -126,23 +118,19 @@ func processFile(filename string, in io.Reader, out io.Writer) error {
fmt.Printf("diff %s bpfmt/%s\n", filename, filename) fmt.Printf("diff %s bpfmt/%s\n", filename, filename)
out.Write(data) out.Write(data)
} }
if !*list && !*write && !*doDiff { if !*list && !*write && !*doDiff {
_, err = out.Write(res) _, err = out.Write(res)
} }
} }
return err return err
} }
func findModules(file *parser.File) (modified bool, errs []error) { func findModules(file *parser.File) (modified bool, errs []error) {
for _, def := range file.Defs { for _, def := range file.Defs {
if module, ok := def.(*parser.Module); ok { if module, ok := def.(*parser.Module); ok {
for _, prop := range module.Properties { for _, prop := range module.Properties {
if prop.Name == "name" && prop.Value.Type() == parser.StringType { if prop.Name == "name" && prop.Value.Type() == parser.StringType && targetedModule(prop.Value.Eval().(*parser.String).Value) {
if targetedModule(prop.Value.Eval().(*parser.String).Value) { for _, p := range targetedProperties.properties {
m, newErrs := processModule(module, prop.Name, file) m, newErrs := processModuleProperty(module, prop.Name, file, p)
errs = append(errs, newErrs...) errs = append(errs, newErrs...)
modified = modified || m modified = modified || m
} }
@ -150,23 +138,22 @@ func findModules(file *parser.File) (modified bool, errs []error) {
} }
} }
} }
return modified, errs return modified, errs
} }
func processModule(module *parser.Module, moduleName string, func processModuleProperty(module *parser.Module, moduleName string,
file *parser.File) (modified bool, errs []error) { file *parser.File, property qualifiedProperty) (modified bool, errs []error) {
prop, parent, err := getRecursiveProperty(module, targetedProperty.name(), targetedProperty.prefixes()) prop, parent, err := getRecursiveProperty(module, property.name(), property.prefixes())
if err != nil { if err != nil {
return false, []error{err} return false, []error{err}
} }
if prop == nil { if prop == nil {
if len(addIdents.idents) > 0 || addLiteral != nil { if len(addIdents.idents) > 0 || addLiteral != nil {
// We are adding something to a non-existing list prop, so we need to create it first. // We are adding something to a non-existing list prop, so we need to create it first.
prop, modified, err = createRecursiveProperty(module, targetedProperty.name(), targetedProperty.prefixes(), &parser.List{}) prop, modified, err = createRecursiveProperty(module, property.name(), property.prefixes(), &parser.List{})
} else if setString != nil { } else if setString != nil {
// We setting a non-existent string property, so we need to create it first. // We setting a non-existent string property, so we need to create it first.
prop, modified, err = createRecursiveProperty(module, targetedProperty.name(), targetedProperty.prefixes(), &parser.String{}) prop, modified, err = createRecursiveProperty(module, property.name(), property.prefixes(), &parser.String{})
} else { } else {
// We cannot find an existing prop, and we aren't adding anything to the prop, // We cannot find an existing prop, and we aren't adding anything to the prop,
// which means we must be removing something from a non-existing prop, // which means we must be removing something from a non-existing prop,
@ -183,22 +170,19 @@ func processModule(module *parser.Module, moduleName string,
} else if *moveProperty { } else if *moveProperty {
return parent.MovePropertyContents(prop.Name, newLocation), nil return parent.MovePropertyContents(prop.Name, newLocation), nil
} }
m, errs := processParameter(prop.Value, targetedProperty.String(), moduleName, file) m, errs := processParameter(prop.Value, property.String(), moduleName, file)
modified = modified || m modified = modified || m
return modified, errs return modified, errs
} }
func getRecursiveProperty(module *parser.Module, name string, prefixes []string) (prop *parser.Property, parent *parser.Map, err error) { func getRecursiveProperty(module *parser.Module, name string, prefixes []string) (prop *parser.Property, parent *parser.Map, err error) {
prop, parent, _, err = getOrCreateRecursiveProperty(module, name, prefixes, nil) prop, parent, _, err = getOrCreateRecursiveProperty(module, name, prefixes, nil)
return prop, parent, err return prop, parent, err
} }
func createRecursiveProperty(module *parser.Module, name string, prefixes []string, func createRecursiveProperty(module *parser.Module, name string, prefixes []string,
empty parser.Expression) (prop *parser.Property, modified bool, err error) { empty parser.Expression) (prop *parser.Property, modified bool, err error) {
prop, _, modified, err = getOrCreateRecursiveProperty(module, name, prefixes, empty) prop, _, modified, err = getOrCreateRecursiveProperty(module, name, prefixes, empty)
return prop, modified, err return prop, modified, err
} }
func getOrCreateRecursiveProperty(module *parser.Module, name string, prefixes []string, func getOrCreateRecursiveProperty(module *parser.Module, name string, prefixes []string,
empty parser.Expression) (prop *parser.Property, parent *parser.Map, modified bool, err error) { empty parser.Expression) (prop *parser.Property, parent *parser.Map, modified bool, err error) {
m := &module.Map m := &module.Map
@ -234,38 +218,40 @@ func getOrCreateRecursiveProperty(module *parser.Module, name string, prefixes [
return nil, nil, false, nil return nil, nil, false, nil
} }
} }
func processParameter(value parser.Expression, paramName, moduleName string, func processParameter(value parser.Expression, paramName, moduleName string,
file *parser.File) (modified bool, errs []error) { file *parser.File) (modified bool, errs []error) {
if _, ok := value.(*parser.Variable); ok { if _, ok := value.(*parser.Variable); ok {
return false, []error{fmt.Errorf("parameter %s in module %s is a variable, unsupported", return false, []error{fmt.Errorf("parameter %s in module %s is a variable, unsupported",
paramName, moduleName)} paramName, moduleName)}
} }
if _, ok := value.(*parser.Operator); ok { if _, ok := value.(*parser.Operator); ok {
return false, []error{fmt.Errorf("parameter %s in module %s is an expression, unsupported", return false, []error{fmt.Errorf("parameter %s in module %s is an expression, unsupported",
paramName, moduleName)} paramName, moduleName)}
} }
if (*replaceProperty).size() != 0 {
list, ok := value.Eval().(*parser.List)
if !ok {
return false, []error{fmt.Errorf("expected parameter %s in module %s to be a list, found %s",
paramName, moduleName, value.Type().String())}
}
return parser.ReplaceStringsInList(list, (*replaceProperty).oldNameToNewName), nil
}
if len(addIdents.idents) > 0 || len(removeIdents.idents) > 0 { if len(addIdents.idents) > 0 || len(removeIdents.idents) > 0 {
list, ok := value.(*parser.List) list, ok := value.(*parser.List)
if !ok { if !ok {
return false, []error{fmt.Errorf("expected parameter %s in module %s to be list, found %s", return false, []error{fmt.Errorf("expected parameter %s in module %s to be list, found %s",
paramName, moduleName, value.Type().String())} paramName, moduleName, value.Type())}
} }
wasSorted := parser.ListIsSorted(list) wasSorted := parser.ListIsSorted(list)
for _, a := range addIdents.idents { for _, a := range addIdents.idents {
m := parser.AddStringToList(list, a) m := parser.AddStringToList(list, a)
modified = modified || m modified = modified || m
} }
for _, r := range removeIdents.idents { for _, r := range removeIdents.idents {
m := parser.RemoveStringFromList(list, r) m := parser.RemoveStringFromList(list, r)
modified = modified || m modified = modified || m
} }
if (wasSorted || *sortLists) && modified { if (wasSorted || *sortLists) && modified {
parser.SortList(file, list) parser.SortList(file, list)
} }
@ -290,14 +276,11 @@ func processParameter(value parser.Expression, paramName, moduleName string,
return false, []error{fmt.Errorf("expected parameter %s in module %s to be string, found %s", return false, []error{fmt.Errorf("expected parameter %s in module %s to be string, found %s",
paramName, moduleName, value.Type().String())} paramName, moduleName, value.Type().String())}
} }
str.Value = *setString str.Value = *setString
modified = true modified = true
} }
return modified, nil return modified, nil
} }
func targetedModule(name string) bool { func targetedModule(name string) bool {
if targetedModules.all { if targetedModules.all {
return true return true
@ -307,10 +290,8 @@ func targetedModule(name string) bool {
return true return true
} }
} }
return false return false
} }
func visitFile(path string, f os.FileInfo, err error) error { func visitFile(path string, f os.FileInfo, err error) error {
if err == nil && f.Name() == "Blueprints" { if err == nil && f.Name() == "Blueprints" {
err = processFile(path, nil, os.Stdout) err = processFile(path, nil, os.Stdout)
@ -320,11 +301,9 @@ func visitFile(path string, f os.FileInfo, err error) error {
} }
return nil return nil
} }
func walkDir(path string) { func walkDir(path string) {
filepath.Walk(path, visitFile) filepath.Walk(path, visitFile)
} }
func main() { func main() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
@ -332,17 +311,16 @@ func main() {
} }
os.Exit(exitCode) os.Exit(exitCode)
}() }()
flag.Parse() flag.Parse()
if len(targetedProperty.parts) == 0 && *moveProperty {
if len(targetedProperties.properties) == 0 && *moveProperty {
report(fmt.Errorf("-move-property must specify property")) report(fmt.Errorf("-move-property must specify property"))
return return
} }
if len(targetedProperty.parts) == 0 { if len(targetedProperties.properties) == 0 {
targetedProperty.Set("deps") targetedProperties.Set("deps")
} }
if flag.NArg() == 0 { if flag.NArg() == 0 {
if *write { if *write {
report(fmt.Errorf("error: cannot use -w with standard input")) report(fmt.Errorf("error: cannot use -w with standard input"))
@ -353,32 +331,27 @@ func main() {
} }
return return
} }
if len(targetedModules.idents) == 0 { if len(targetedModules.idents) == 0 {
report(fmt.Errorf("-m parameter is required")) report(fmt.Errorf("-m parameter is required"))
return return
} }
if len(addIdents.idents) == 0 && len(removeIdents.idents) == 0 && setString == nil && addLiteral == nil && !*removeProperty && !*moveProperty { if len(addIdents.idents) == 0 && len(removeIdents.idents) == 0 && setString == nil && addLiteral == nil && !*removeProperty && !*moveProperty && (*replaceProperty).size() == 0 {
report(fmt.Errorf("-a, -add-literal, -r, -remove-property, -move-property, or -str parameter is required")) report(fmt.Errorf("-a, -add-literal, -r, -remove-property, -move-property, replace-property or -str parameter is required"))
return return
} }
if *removeProperty && (len(addIdents.idents) > 0 || len(removeIdents.idents) > 0 || setString != nil || addLiteral != nil || (*replaceProperty).size() > 0) {
if *removeProperty && (len(addIdents.idents) > 0 || len(removeIdents.idents) > 0 || setString != nil || addLiteral != nil) {
report(fmt.Errorf("-remove-property cannot be used with other parameter(s)")) report(fmt.Errorf("-remove-property cannot be used with other parameter(s)"))
return return
} }
if *moveProperty && (len(addIdents.idents) > 0 || len(removeIdents.idents) > 0 || setString != nil || addLiteral != nil || (*replaceProperty).size() > 0) {
if *moveProperty && (len(addIdents.idents) > 0 || len(removeIdents.idents) > 0 || setString != nil || addLiteral != nil) {
report(fmt.Errorf("-move-property cannot be used with other parameter(s)")) report(fmt.Errorf("-move-property cannot be used with other parameter(s)"))
return return
} }
if *moveProperty && newLocation == "" { if *moveProperty && newLocation == "" {
report(fmt.Errorf("-move-property must specify -new-location")) report(fmt.Errorf("-move-property must specify -new-location"))
return return
} }
for i := 0; i < flag.NArg(); i++ { for i := 0; i < flag.NArg(); i++ {
path := flag.Arg(i) path := flag.Arg(i)
switch dir, err := os.Stat(path); { switch dir, err := os.Stat(path); {
@ -393,7 +366,6 @@ func main() {
} }
} }
} }
func diff(b1, b2 []byte) (data []byte, err error) { func diff(b1, b2 []byte) (data []byte, err error) {
f1, err := ioutil.TempFile("", "bpfmt") f1, err := ioutil.TempFile("", "bpfmt")
if err != nil { if err != nil {
@ -401,17 +373,14 @@ func diff(b1, b2 []byte) (data []byte, err error) {
} }
defer os.Remove(f1.Name()) defer os.Remove(f1.Name())
defer f1.Close() defer f1.Close()
f2, err := ioutil.TempFile("", "bpfmt") f2, err := ioutil.TempFile("", "bpfmt")
if err != nil { if err != nil {
return return
} }
defer os.Remove(f2.Name()) defer os.Remove(f2.Name())
defer f2.Close() defer f2.Close()
f1.Write(b1) f1.Write(b1)
f2.Write(b2) f2.Write(b2)
data, err = exec.Command("diff", "-uw", f1.Name(), f2.Name()).CombinedOutput() data, err = exec.Command("diff", "-uw", f1.Name(), f2.Name()).CombinedOutput()
if len(data) > 0 { if len(data) > 0 {
// diff exits with a non-zero status when the files don't match. // diff exits with a non-zero status when the files don't match.
@ -419,7 +388,6 @@ func diff(b1, b2 []byte) (data []byte, err error) {
err = nil err = nil
} }
return return
} }
type stringPtrFlag struct { type stringPtrFlag struct {
@ -430,7 +398,6 @@ func (f stringPtrFlag) Set(s string) error {
*f.s = &s *f.s = &s
return nil return nil
} }
func (f stringPtrFlag) String() string { func (f stringPtrFlag) String() string {
if f.s == nil || *f.s == nil { if f.s == nil || *f.s == nil {
return "" return ""
@ -438,6 +405,59 @@ func (f stringPtrFlag) String() string {
return **f.s return **f.s
} }
type replacements struct {
oldNameToNewName map[string]string
}
func (m *replacements) String() string {
ret := ""
sep := ""
for k, v := range m.oldNameToNewName {
ret += sep
ret += k
ret += ":"
ret += v
sep = ","
}
return ret
}
func (m *replacements) Set(s string) error {
usedNames := make(map[string]struct{})
pairs := strings.Split(s, ",")
length := len(pairs)
m.oldNameToNewName = make(map[string]string)
for i := 0; i < length; i++ {
pair := strings.SplitN(pairs[i], ":", 2)
if len(pair) != 2 {
return fmt.Errorf("Invalid replacement pair %s", pairs[i])
}
oldName := pair[0]
newName := pair[1]
if _, seen := usedNames[oldName]; seen {
return fmt.Errorf("Duplicated replacement name %s", oldName)
}
if _, seen := usedNames[newName]; seen {
return fmt.Errorf("Duplicated replacement name %s", newName)
}
usedNames[oldName] = struct{}{}
usedNames[newName] = struct{}{}
m.oldNameToNewName[oldName] = newName
}
return nil
}
func (m *replacements) Get() interface{} {
//TODO(dacek): Remove Get() method from interface as it seems unused.
return m.oldNameToNewName
}
func (m *replacements) size() (length int) {
return len(m.oldNameToNewName)
}
type identSet struct { type identSet struct {
idents []string idents []string
all bool all bool
@ -446,7 +466,6 @@ type identSet struct {
func (m *identSet) String() string { func (m *identSet) String() string {
return strings.Join(m.idents, ",") return strings.Join(m.idents, ",")
} }
func (m *identSet) Set(s string) error { func (m *identSet) Set(s string) error {
m.idents = strings.FieldsFunc(s, func(c rune) bool { m.idents = strings.FieldsFunc(s, func(c rune) bool {
return unicode.IsSpace(c) || c == ',' return unicode.IsSpace(c) || c == ','
@ -456,42 +475,70 @@ func (m *identSet) Set(s string) error {
} }
return nil return nil
} }
func (m *identSet) Get() interface{} { func (m *identSet) Get() interface{} {
return m.idents return m.idents
} }
type qualifiedProperties struct {
properties []qualifiedProperty
}
type qualifiedProperty struct { type qualifiedProperty struct {
parts []string parts []string
} }
var _ flag.Getter = (*qualifiedProperty)(nil) var _ flag.Getter = (*qualifiedProperties)(nil)
func (p *qualifiedProperty) name() string { func (p *qualifiedProperty) name() string {
return p.parts[len(p.parts)-1] return p.parts[len(p.parts)-1]
} }
func (p *qualifiedProperty) prefixes() []string { func (p *qualifiedProperty) prefixes() []string {
return p.parts[:len(p.parts)-1] return p.parts[:len(p.parts)-1]
} }
func (p *qualifiedProperty) String() string { func (p *qualifiedProperty) String() string {
return strings.Join(p.parts, ".") return strings.Join(p.parts, ".")
} }
func (p *qualifiedProperty) Set(s string) error { func parseQualifiedProperty(s string) (*qualifiedProperty, error) {
p.parts = strings.Split(s, ".") parts := strings.Split(s, ".")
if len(p.parts) == 0 { if len(parts) == 0 {
return nil, fmt.Errorf("%q is not a valid property name", s)
}
for _, part := range parts {
if part == "" {
return nil, fmt.Errorf("%q is not a valid property name", s)
}
}
prop := qualifiedProperty{parts}
return &prop, nil
}
func (p *qualifiedProperties) Set(s string) error {
properties := strings.Split(s, ",")
if len(properties) == 0 {
return fmt.Errorf("%q is not a valid property name", s) return fmt.Errorf("%q is not a valid property name", s)
} }
for _, part := range p.parts {
if part == "" { p.properties = make([]qualifiedProperty, len(properties))
return fmt.Errorf("%q is not a valid property name", s) for i := 0; i < len(properties); i++ {
tmp, err := parseQualifiedProperty(properties[i])
if err != nil {
return err
} }
p.properties[i] = *tmp
} }
return nil return nil
} }
func (p *qualifiedProperty) Get() interface{} { func (p *qualifiedProperties) String() string {
return p.parts arrayLength := len(p.properties)
props := make([]string, arrayLength)
for i := 0; i < len(p.properties); i++ {
props[i] = p.properties[i].String()
}
return strings.Join(props, ",")
}
func (p *qualifiedProperties) Get() interface{} {
return p.properties
} }

View file

@ -4,34 +4,33 @@
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
// //
// http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
// //
// Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package main package main
import ( import (
"strings"
"testing"
"github.com/google/blueprint/parser" "github.com/google/blueprint/parser"
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
"strings"
"testing"
) )
var testCases = []struct { var testCases = []struct {
name string name string
input string input string
output string output string
property string property string
addSet string addSet string
removeSet string removeSet string
addLiteral *string addLiteral *string
setString *string setString *string
removeProperty bool removeProperty bool
replaceProperty string
}{ }{
{ {
name: "add", name: "add",
@ -354,6 +353,48 @@ var testCases = []struct {
`, `,
property: "bar", property: "bar",
removeProperty: true, removeProperty: true,
}, {
name: "replace property",
property: "deps",
input: `
cc_foo {
name: "foo",
deps: ["baz", "unchanged"],
}
`,
output: `
cc_foo {
name: "foo",
deps: [
"baz_lib",
"unchanged",
],
}
`,
replaceProperty: "baz:baz_lib,foobar:foobar_lib",
}, {
name: "replace property multiple modules",
property: "deps,required",
input: `
cc_foo {
name: "foo",
deps: ["baz", "unchanged"],
unchanged: ["baz"],
required: ["foobar"],
}
`,
output: `
cc_foo {
name: "foo",
deps: [
"baz_lib",
"unchanged",
],
unchanged: ["baz"],
required: ["foobar_lib"],
}
`,
replaceProperty: "baz:baz_lib,foobar:foobar_lib",
}, },
} }
@ -364,16 +405,16 @@ func simplifyModuleDefinition(def string) string {
} }
return result return result
} }
func TestProcessModule(t *testing.T) { func TestProcessModule(t *testing.T) {
for i, testCase := range testCases { for i, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
targetedProperty.Set(testCase.property) targetedProperties.Set(testCase.property)
addIdents.Set(testCase.addSet) addIdents.Set(testCase.addSet)
removeIdents.Set(testCase.removeSet) removeIdents.Set(testCase.removeSet)
removeProperty = &testCase.removeProperty removeProperty = &testCase.removeProperty
setString = testCase.setString setString = testCase.setString
addLiteral = testCase.addLiteral addLiteral = testCase.addLiteral
replaceProperty.Set(testCase.replaceProperty)
inAst, errs := parser.ParseAndEval("", strings.NewReader(testCase.input), parser.NewScope(nil)) inAst, errs := parser.ParseAndEval("", strings.NewReader(testCase.input), parser.NewScope(nil))
if len(errs) > 0 { if len(errs) > 0 {
@ -384,16 +425,18 @@ func TestProcessModule(t *testing.T) {
t.Errorf("%+v", testCase) t.Errorf("%+v", testCase)
t.FailNow() t.FailNow()
} }
if inModule, ok := inAst.Defs[0].(*parser.Module); !ok { if inModule, ok := inAst.Defs[0].(*parser.Module); !ok {
t.Fatalf(" input must only contain a single module definition: %s", testCase.input) t.Fatalf(" input must only contain a single module definition: %s", testCase.input)
} else { } else {
_, errs := processModule(inModule, "", inAst) for _, p := range targetedProperties.properties {
if len(errs) > 0 { _, errs := processModuleProperty(inModule, "", inAst, p)
t.Errorf("test case %d:", i) if len(errs) > 0 {
for _, err := range errs { t.Errorf("test case %d:", i)
t.Errorf(" %s", err) for _, err := range errs {
t.Errorf(" %s", err)
}
} }
} }
inModuleText, _ := parser.Print(inAst) inModuleText, _ := parser.Print(inAst)
inModuleString := string(inModuleText) inModuleString := string(inModuleText)
@ -407,5 +450,46 @@ func TestProcessModule(t *testing.T) {
} }
}) })
} }
}
func TestReplacementsCycleError(t *testing.T) {
cycleString := "old1:new1,new1:old1"
err := replaceProperty.Set(cycleString)
if err.Error() != "Duplicated replacement name new1" {
t.Errorf("Error message did not match")
t.Errorf("Expected ")
t.Errorf(" Duplicated replacement name new1")
t.Errorf("actual error:")
t.Errorf(" %s", err.Error())
t.FailNow()
}
}
func TestReplacementsDuplicatedError(t *testing.T) {
cycleString := "a:b,a:c"
err := replaceProperty.Set(cycleString)
if err.Error() != "Duplicated replacement name a" {
t.Errorf("Error message did not match")
t.Errorf("Expected ")
t.Errorf(" Duplicated replacement name a")
t.Errorf("actual error:")
t.Errorf(" %s", err.Error())
t.FailNow()
}
}
func TestReplacementsMultipleReplacedToSame(t *testing.T) {
cycleString := "a:c,d:c"
err := replaceProperty.Set(cycleString)
if err.Error() != "Duplicated replacement name c" {
t.Errorf("Error message did not match")
t.Errorf("Expected ")
t.Errorf(" Duplicated replacement name c")
t.Errorf("actual error:")
t.Errorf(" %s", err.Error())
t.FailNow()
}
} }

View file

@ -56,6 +56,24 @@ func RemoveStringFromList(list *List, s string) (modified bool) {
return false return false
} }
func ReplaceStringsInList(list *List, replacements map[string]string) (replaced bool) {
modified := false
for i, v := range list.Values {
if v.Type() != StringType {
panic(fmt.Errorf("expected string in list, got %s", v.Type()))
}
if sv, ok := v.(*String); ok && replacements[sv.Value] != "" {
pos := list.Values[i].Pos()
list.Values[i] = &String{
LiteralPos: pos,
Value: replacements[sv.Value],
}
modified = true
}
}
return modified
}
// A Patch represents a region of a text buffer to be replaced [Start, End) and its Replacement // A Patch represents a region of a text buffer to be replaced [Start, End) and its Replacement
type Patch struct { type Patch struct {
Start, End int Start, End int