aboutsummaryrefslogtreecommitdiffstats
path: root/bpfix/bpfix/bpfix.go
blob: 124949438bd6cbeb325b0e9ae6e3927c963bdc31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file implements the logic of bpfix and also provides a programmatic interface

package bpfix

import (
	"bytes"
	"fmt"
	"github.com/google/blueprint/parser"
)

// 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
	removeEmptyLists                bool
}

func NewFixRequest() FixRequest {
	return FixRequest{}
}

func (r FixRequest) AddAll() (result FixRequest) {
	result = r
	result.simplifyKnownRedundantVariables = true
	result.removeEmptyLists = true
	return result
}

// 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) {
	prevIdentifier, err := fingerprint(tree)
	if err != nil {
		return nil, err
	}

	fixed = tree
	maxNumIterations := 20
	i := 0
	for {
		fixed, err = fixTreeOnce(fixed, config)
		newIdentifier, err := fingerprint(tree)
		if err != nil {
			return nil, err
		}
		if bytes.Equal(newIdentifier, prevIdentifier) {
			break
		}
		prevIdentifier = newIdentifier
		// any errors from a previous iteration generally get thrown away and overwritten by errors on the next iteration

		// 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)
			break
		}
	}
	return fixed, err
}

// returns a unique identifier for the given tree that can be used to determine whether the tree changed
func fingerprint(tree *parser.File) (fingerprint []byte, err error) {
	bytes, err := parser.Print(tree)
	if err != nil {
		return nil, err
	}
	return bytes, nil
}

func fixTreeOnce(tree *parser.File, config FixRequest) (fixed *parser.File, err error) {
	if config.simplifyKnownRedundantVariables {
		tree, err = simplifyKnownPropertiesDuplicatingEachOther(tree)
		if err != nil {
			return nil, err
		}
	}
	if config.removeEmptyLists {
		tree, err = removePropertiesHavingTheirDefaultValues(tree)
		if err != nil {
			return nil, err
		}
	}
	return tree, err
}

func simplifyKnownPropertiesDuplicatingEachOther(tree *parser.File) (fixed *parser.File, err error) {
	// remove from local_include_dirs anything in export_include_dirs
	fixed, err = removeMatchingModuleListProperties(tree, "export_include_dirs", "local_include_dirs")
	return fixed, err
}

// removes from <items> every item present in <removals>
func filterExpressionList(items *parser.List, removals *parser.List) {
	writeIndex := 0
	for _, item := range items.Values {
		included := true
		for _, removal := range removals.Values {
			equal, err := parser.ExpressionsAreSame(item, removal)
			if err != nil {
				continue
			}
			if equal {
				included = false
				break
			}
		}
		if included {
			items.Values[writeIndex] = item
			writeIndex++
		}
	}
	items.Values = items.Values[:writeIndex]
}

// 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) {
	for _, def := range tree.Defs {
		mod, ok := def.(*parser.Module)
		if !ok {
			continue
		}
		legacy, ok := mod.GetProperty(legacyName)
		if !ok {
			continue
		}
		legacyList, ok := legacy.Value.(*parser.List)
		if !ok {
			continue
		}
		canonical, ok := mod.GetProperty(canonicalName)
		if !ok {
			continue
		}
		canonicalList, ok := canonical.Value.(*parser.List)
		if !ok {
			continue
		}
		filterExpressionList(legacyList, canonicalList)
	}
	return tree, nil
}

func removePropertiesHavingTheirDefaultValues(tree *parser.File) (fixed *parser.File, err error) {
	for _, def := range tree.Defs {
		mod, ok := def.(*parser.Module)
		if !ok {
			continue
		}
		writeIndex := 0
		for _, prop := range mod.Properties {
			val := prop.Value
			keep := true
			switch val := val.(type) {
			case *parser.List:
				if len(val.Values) == 0 {
					keep = false
				}
				break
			default:
				keep = true
			}
			if keep {
				mod.Properties[writeIndex] = prop
				writeIndex++
			}
		}
		mod.Properties = mod.Properties[:writeIndex]
	}
	return tree, nil
}