Add //visibility:override to allow control over inheritance

Visibility rules can be 'inherited' in one of two ways. Either from
defaults or from a module that called ctx.CreateModule(...).
Previously, in both cases the inheriting module could only append
additional visibility rules to the end of the inherited rules. That
made it impossible to restrict the visibility by removing or ignore
inherited rules.

The //visibility:override rectifies that by allowing the inheriting
module to ignore all the rules that they would have inherited. It can
only go at the beginning of a list of rules specified in a module but
after defaults are applied it can end up in the middle of a list of
rules. In that case it behaves as if all the rules up to and including
the //visibility:override rule were discarded.

It can be used with //visibility:private to override
//visibility:public and vice versa.

Bug: 155787200
Test: m nothing
Change-Id: I8a9c9c5a1bdceaee387c08864ae2b34629e0d46f
This commit is contained in:
Paul Duffin
2020-05-05 19:19:22 +01:00
parent b67b9a416e
commit 51084ff6cf
4 changed files with 193 additions and 2 deletions

View File

@@ -245,7 +245,7 @@ func checkRules(ctx BaseModuleContext, currentPkg, property string, visibility [
return
}
for _, v := range visibility {
for i, v := range visibility {
ok, pkg, name := splitRule(ctx, v, currentPkg, property)
if !ok {
continue
@@ -257,11 +257,18 @@ func checkRules(ctx BaseModuleContext, currentPkg, property string, visibility [
case "legacy_public":
ctx.PropertyErrorf(property, "//visibility:legacy_public must not be used")
continue
case "override":
// This keyword does not create a rule so pretend it does not exist.
ruleCount -= 1
default:
ctx.PropertyErrorf(property, "unrecognized visibility rule %q", v)
continue
}
if ruleCount != 1 {
if name == "override" {
if i != 0 {
ctx.PropertyErrorf(property, `"%v" may only be used at the start of the visibility rules`, v)
}
} else if ruleCount != 1 {
ctx.PropertyErrorf(property, "cannot mix %q with any other visibility rules", v)
continue
}
@@ -327,6 +334,14 @@ func parseRules(ctx BaseModuleContext, currentPkg, property string, visibility [
case "public":
r = publicRule{}
hasPublicRule = true
case "override":
// Discard all preceding rules and any state based on them.
rules = nil
hasPrivateRule = false
hasPublicRule = false
hasNonPrivateRule = false
// This does not actually create a rule so continue onto the next rule.
continue
}
} else {
switch name {