From 351475627b9e94e5afdf472cbf465f49c433a25e Mon Sep 17 00:00:00 2001 From: Nathan Harmata Date: Tue, 5 Apr 2016 19:49:43 +0000 Subject: Make non-empty attribute checks happen during analysis of the target in question, rather than during loading of the target's package. This way a target's package won't be in error if e.g. an unrelated target has empty 'srcs'. -- MOS_MIGRATED_REVID=119079777 --- .../devtools/build/lib/analysis/RuleContext.java | 11 ++++-- .../devtools/build/lib/packages/RuleClass.java | 41 +++++++++++----------- 2 files changed, 29 insertions(+), 23 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java index 706d986193..5eac0d686a 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java @@ -158,6 +158,7 @@ public final class RuleContext extends TargetContext private RuleContext( Builder builder, + AttributeMap attributes, ListMultimap targetMap, ListMultimap filesetEntryMap, Set configConditions, @@ -172,8 +173,7 @@ public final class RuleContext extends TargetContext this.targetMap = targetMap; this.filesetEntryMap = filesetEntryMap; this.configConditions = configConditions; - this.attributes = - ConfiguredAttributeMapper.of(builder.rule, configConditions); + this.attributes = attributes; this.features = getEnabledFeatures(); this.ruleClassNameForLogging = ruleClassNameForLogging; this.aspectAttributes = aspectAttributes; @@ -1306,11 +1306,14 @@ public final class RuleContext extends TargetContext Preconditions.checkNotNull(prerequisiteMap); Preconditions.checkNotNull(configConditions); Preconditions.checkNotNull(visibility); + AttributeMap attributes = ConfiguredAttributeMapper.of(rule, configConditions); + validateAttributes(attributes); ListMultimap targetMap = createTargetMap(); ListMultimap filesetEntryMap = createFilesetEntryMap(rule, configConditions); return new RuleContext( this, + attributes, targetMap, filesetEntryMap, configConditions, @@ -1319,6 +1322,10 @@ public final class RuleContext extends TargetContext aspectAttributes != null ? aspectAttributes : ImmutableMap.of()); } + private void validateAttributes(AttributeMap attributes) { + rule.getRuleClassObject().checkAttributesNonEmpty(rule, reporter, attributes); + } + Builder setVisibility(NestedSet visibility) { this.visibility = visibility; return this; diff --git a/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java b/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java index 9accac3899..ed2ad3064c 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java +++ b/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java @@ -1401,7 +1401,6 @@ public final class RuleClass { boolean explicit = attributeValues.isAttributeExplicitlySpecified(attributeName); setRuleAttributeValue(rule, eventHandler, attr, nativeAttributeValue, explicit); definedAttrIndices.set(attrIndex); - checkAttrValNonEmpty(rule, eventHandler, attributeValue, attr); } return definedAttrIndices; } @@ -1457,7 +1456,6 @@ public final class RuleClass { attrsWithComputedDefaults.add(attr); } else { Object defaultValue = getAttributeNoncomputedDefaultValue(attr, pkgBuilder); - checkAttrValNonEmpty(rule, eventHandler, defaultValue, attr); rule.setAttributeValue(attr, defaultValue, /*explicit=*/ false); checkAllowedValues(rule, attr, eventHandler); } @@ -1500,26 +1498,27 @@ public final class RuleClass { /*explicit=*/false); } - private void checkAttrValNonEmpty( - Rule rule, EventHandler eventHandler, Object attributeValue, Attribute attr) { - if (!attr.isNonEmpty()) { - return; - } - - boolean isEmpty = false; - - if (attributeValue instanceof SkylarkList) { - isEmpty = ((SkylarkList) attributeValue).isEmpty(); - } else if (attributeValue instanceof List) { - isEmpty = ((List) attributeValue).isEmpty(); - } else if (attributeValue instanceof Map) { - isEmpty = ((Map) attributeValue).isEmpty(); - } + public void checkAttributesNonEmpty( + Rule rule, RuleErrorConsumer ruleErrorConsumer, AttributeMap attributes) { + for (String attributeName : attributes.getAttributeNames()) { + Attribute attr = attributes.getAttributeDefinition(attributeName); + if (!attr.isNonEmpty()) { + continue; + } + Object attributeValue = attributes.get(attributeName, attr.getType()); + + boolean isEmpty = false; + if (attributeValue instanceof SkylarkList) { + isEmpty = ((SkylarkList) attributeValue).isEmpty(); + } else if (attributeValue instanceof List) { + isEmpty = ((List) attributeValue).isEmpty(); + } else if (attributeValue instanceof Map) { + isEmpty = ((Map) attributeValue).isEmpty(); + } - if (isEmpty) { - rule.reportError(rule.getLabel() + ": non empty attribute '" + attr.getName() - + "' in '" + name + "' rule '" + rule.getLabel() + "' has to have at least one value", - eventHandler); + if (isEmpty) { + ruleErrorConsumer.attributeError(attr.getName(), "attribute must be non empty"); + } } } -- cgit v1.2.3