Add sdk:"keep" tag support

Fields tagged with `sdk:"keep"` will keep the value even if it would
normally be cleared because it was common across a number of structs.
This will allow a module type to specify the same value across all
structs populated directly from variants and have it be copied into all
common property structs without clearing it from the variant specific
structs.

Bug: 248258460
Test: m nothing
Change-Id: I147d946b11fd8766a7d875d9206e8f5034f585d6
This commit is contained in:
Paul Duffin 2022-09-22 16:21:54 +01:00
parent 02e25c85eb
commit bfdca96828
3 changed files with 26 additions and 5 deletions

View file

@ -738,6 +738,11 @@ type SdkMemberType interface {
// A field annotated with a tag of `sdk:"ignore"` will be treated as if it
// was not capitalized, i.e. ignored and not optimized for common values.
//
// A field annotated with a tag of `sdk:"keep"` will not be cleared even if the value is common
// across multiple structs. Common values will still be copied into the common property struct.
// So, if the same value is placed in all structs populated from variants that value would be
// copied into all common property structs and so be available in every instance.
//
// A field annotated with a tag of `android:"arch_variant"` will be allowed to have
// values that differ by arch, fields not tagged as such must have common values across
// all variants.

View file

@ -221,6 +221,7 @@ type testPropertiesStruct struct {
name string
private string
Public_Ignore string `sdk:"ignore"`
Public_Keep string `sdk:"keep"`
S_Common string
S_Different string `android:"arch_variant"`
A_Common []string
@ -247,6 +248,7 @@ func TestCommonValueOptimization(t *testing.T) {
name: "struct-0",
private: "common",
Public_Ignore: "common",
Public_Keep: "keep",
S_Common: "common",
S_Different: "upper",
A_Common: []string{"first", "second"},
@ -262,6 +264,7 @@ func TestCommonValueOptimization(t *testing.T) {
name: "struct-1",
private: "common",
Public_Ignore: "common",
Public_Keep: "keep",
S_Common: "common",
S_Different: "lower",
A_Common: []string{"first", "second"},
@ -285,6 +288,7 @@ func TestCommonValueOptimization(t *testing.T) {
name: "common",
private: "",
Public_Ignore: "",
Public_Keep: "keep",
S_Common: "common",
S_Different: "",
A_Common: []string{"first", "second"},
@ -303,6 +307,7 @@ func TestCommonValueOptimization(t *testing.T) {
name: "struct-0",
private: "common",
Public_Ignore: "common",
Public_Keep: "keep",
S_Common: "",
S_Different: "upper",
A_Common: nil,
@ -321,6 +326,7 @@ func TestCommonValueOptimization(t *testing.T) {
name: "struct-1",
private: "common",
Public_Ignore: "common",
Public_Keep: "keep",
S_Common: "",
S_Different: "lower",
A_Common: nil,

View file

@ -2172,6 +2172,11 @@ type extractorProperty struct {
// Retrieves the value on which common value optimization will be performed.
getter fieldAccessorFunc
// True if the field should never be cleared.
//
// This is set to true if and only if the field is annotated with `sdk:"keep"`.
keep bool
// The empty value for the field.
emptyValue reflect.Value
@ -2236,6 +2241,8 @@ func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingS
}
}
keep := proptools.HasTag(field, "sdk", "keep")
// Save a copy of the field index for use in the function.
fieldIndex := f
@ -2275,6 +2282,7 @@ func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingS
name,
filter,
fieldGetter,
keep,
reflect.Zero(field.Type),
proptools.HasTag(field, "android", "arch_variant"),
}
@ -2394,11 +2402,13 @@ func (e *commonValueExtractor) extractCommonProperties(commonProperties interfac
if commonValue != nil {
emptyValue := property.emptyValue
fieldGetter(commonStructValue).Set(*commonValue)
for i := 0; i < sliceValue.Len(); i++ {
container := sliceValue.Index(i).Interface().(propertiesContainer)
itemValue := reflect.ValueOf(container.optimizableProperties())
fieldValue := fieldGetter(itemValue)
fieldValue.Set(emptyValue)
if !property.keep {
for i := 0; i < sliceValue.Len(); i++ {
container := sliceValue.Index(i).Interface().(propertiesContainer)
itemValue := reflect.ValueOf(container.optimizableProperties())
fieldValue := fieldGetter(itemValue)
fieldValue.Set(emptyValue)
}
}
}