Merge changes I101fa6b0,Ic1a9bb1e into rvc-dev

* changes:
  Report visibility errors in both check and gather phases
  Reduce duplication in visibility property management
This commit is contained in:
Paul Duffin 2020-05-05 11:33:11 +00:00 committed by Android (Google) Code Review
commit 5d5804a81f
4 changed files with 56 additions and 32 deletions

View File

@ -176,18 +176,18 @@ func InitDefaultsModule(module DefaultsModule) {
defaultsVisibility := module.defaultsVisibility() defaultsVisibility := module.defaultsVisibility()
module.AddProperties(&base.nameProperties, defaultsVisibility) module.AddProperties(&base.nameProperties, defaultsVisibility)
// The defaults_visibility property controls the visibility of a defaults module.
base.primaryVisibilityProperty =
newVisibilityProperty("defaults_visibility", &defaultsVisibility.Defaults_visibility)
// Unlike non-defaults modules the visibility property is not stored in m.base().commonProperties. // Unlike non-defaults modules the visibility property is not stored in m.base().commonProperties.
// Instead it is stored in a separate instance of commonProperties created above so use that. // Instead it is stored in a separate instance of commonProperties created above so clear the
// existing list of properties.
clearVisibilityProperties(module)
// The defaults_visibility property controls the visibility of a defaults module so it must be
// set as the primary property, which also adds it to the list.
setPrimaryVisibilityProperty(module, "defaults_visibility", &defaultsVisibility.Defaults_visibility)
// The visibility property needs to be checked (but not parsed) by the visibility module during // The visibility property needs to be checked (but not parsed) by the visibility module during
// its checking phase and parsing phase. // its checking phase and parsing phase so add it to the list as a normal property.
base.visibilityPropertyInfo = []visibilityProperty{ AddVisibilityProperty(module, "visibility", &commonProperties.Visibility)
base.primaryVisibilityProperty,
newVisibilityProperty("visibility", &commonProperties.Visibility),
}
base.module = module base.module = module
} }

View File

@ -607,10 +607,8 @@ func InitAndroidModule(m Module) {
base.customizableProperties = m.GetProperties() base.customizableProperties = m.GetProperties()
// The default_visibility property needs to be checked and parsed by the visibility module during // The default_visibility property needs to be checked and parsed by the visibility module during
// its checking and parsing phases. // its checking and parsing phases so make it the primary visibility property.
base.primaryVisibilityProperty = setPrimaryVisibilityProperty(m, "visibility", &base.commonProperties.Visibility)
newVisibilityProperty("visibility", &base.commonProperties.Visibility)
base.visibilityPropertyInfo = []visibilityProperty{base.primaryVisibilityProperty}
} }
func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) { func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {

View File

@ -101,10 +101,8 @@ func PackageFactory() Module {
module.AddProperties(&module.properties) module.AddProperties(&module.properties)
// The default_visibility property needs to be checked and parsed by the visibility module during // The default_visibility property needs to be checked and parsed by the visibility module during
// its checking and parsing phases. // its checking and parsing phases so make it the primary visibility property.
module.primaryVisibilityProperty = setPrimaryVisibilityProperty(module, "default_visibility", &module.properties.Default_visibility)
newVisibilityProperty("default_visibility", &module.properties.Default_visibility)
module.visibilityPropertyInfo = []visibilityProperty{module.primaryVisibilityProperty}
return module return module
} }

View File

@ -246,14 +246,8 @@ func checkRules(ctx BaseModuleContext, currentPkg, property string, visibility [
} }
for _, v := range visibility { for _, v := range visibility {
ok, pkg, name := splitRule(v, currentPkg) ok, pkg, name := splitRule(ctx, v, currentPkg, property)
if !ok { if !ok {
// Visibility rule is invalid so ignore it. Keep going rather than aborting straight away to
// ensure all the rules on this module are checked.
ctx.PropertyErrorf(property,
"invalid visibility pattern %q must match"+
" //<package>:<module>, //<package> or :<module>",
v)
continue continue
} }
@ -301,21 +295,24 @@ func visibilityRuleGatherer(ctx BottomUpMutatorContext) {
// Parse the visibility rules that control access to the module and store them by id // Parse the visibility rules that control access to the module and store them by id
// for use when enforcing the rules. // for use when enforcing the rules.
if visibility := m.visibility(); visibility != nil { primaryProperty := m.base().primaryVisibilityProperty
rule := parseRules(ctx, currentPkg, m.visibility()) if primaryProperty != nil {
if rule != nil { if visibility := primaryProperty.getStrings(); visibility != nil {
moduleToVisibilityRuleMap(ctx.Config()).Store(qualifiedModuleId, rule) rule := parseRules(ctx, currentPkg, primaryProperty.getName(), visibility)
if rule != nil {
moduleToVisibilityRuleMap(ctx.Config()).Store(qualifiedModuleId, rule)
}
} }
} }
} }
func parseRules(ctx BaseModuleContext, currentPkg string, visibility []string) compositeRule { func parseRules(ctx BaseModuleContext, currentPkg, property string, visibility []string) compositeRule {
rules := make(compositeRule, 0, len(visibility)) rules := make(compositeRule, 0, len(visibility))
hasPrivateRule := false hasPrivateRule := false
hasPublicRule := false hasPublicRule := false
hasNonPrivateRule := false hasNonPrivateRule := false
for _, v := range visibility { for _, v := range visibility {
ok, pkg, name := splitRule(v, currentPkg) ok, pkg, name := splitRule(ctx, v, currentPkg, property)
if !ok { if !ok {
continue continue
} }
@ -376,10 +373,16 @@ func isAllowedFromOutsideVendor(pkg string, name string) bool {
return !isAncestor("vendor", pkg) return !isAncestor("vendor", pkg)
} }
func splitRule(ruleExpression string, currentPkg string) (bool, string, string) { func splitRule(ctx BaseModuleContext, ruleExpression string, currentPkg, property string) (bool, string, string) {
// Make sure that the rule is of the correct format. // Make sure that the rule is of the correct format.
matches := visibilityRuleRegexp.FindStringSubmatch(ruleExpression) matches := visibilityRuleRegexp.FindStringSubmatch(ruleExpression)
if ruleExpression == "" || matches == nil { if ruleExpression == "" || matches == nil {
// Visibility rule is invalid so ignore it. Keep going rather than aborting straight away to
// ensure all the rules on this module are checked.
ctx.PropertyErrorf(property,
"invalid visibility pattern %q must match"+
" //<package>:<module>, //<package> or :<module>",
ruleExpression)
return false, "", "" return false, "", ""
} }
@ -480,3 +483,28 @@ func EffectiveVisibilityRules(ctx BaseModuleContext, module Module) []string {
return rule.Strings() return rule.Strings()
} }
// Clear the default visibility properties so they can be replaced.
func clearVisibilityProperties(module Module) {
module.base().visibilityPropertyInfo = nil
}
// Add a property that contains visibility rules so that they are checked for
// correctness.
func AddVisibilityProperty(module Module, name string, stringsProperty *[]string) {
addVisibilityProperty(module, name, stringsProperty)
}
func addVisibilityProperty(module Module, name string, stringsProperty *[]string) visibilityProperty {
base := module.base()
property := newVisibilityProperty(name, stringsProperty)
base.visibilityPropertyInfo = append(base.visibilityPropertyInfo, property)
return property
}
// Set the primary visibility property.
//
// Also adds the property to the list of properties to be validated.
func setPrimaryVisibilityProperty(module Module, name string, stringsProperty *[]string) {
module.base().primaryVisibilityProperty = addVisibilityProperty(module, name, stringsProperty)
}