Fix default values in docs for attributes that are defined in ancestor classes. For example, cc_binary and cc_test both inherit from the same rule class but have different default values for the attribute stamp. RELNOTES: None PiperOrigin-RevId: 221399178
diff --git a/src/main/java/com/google/devtools/build/docgen/BuildDocCollector.java b/src/main/java/com/google/devtools/build/docgen/BuildDocCollector.java index df250d2..bd1fdf5 100644 --- a/src/main/java/com/google/devtools/build/docgen/BuildDocCollector.java +++ b/src/main/java/com/google/devtools/build/docgen/BuildDocCollector.java
@@ -214,6 +214,17 @@ } } if (bestAttributeDoc != null) { + try { + // We have to clone the matching RuleDocumentationAttribute here so that we don't + // overwrite the reference to the actual attribute later by another attribute with + // the same ancestor but different default values. + bestAttributeDoc = (RuleDocumentationAttribute) bestAttributeDoc.clone(); + } catch (CloneNotSupportedException e) { + throw new BuildEncyclopediaDocException( + bestAttributeDoc.getFileName(), + bestAttributeDoc.getStartLineCnt(), + "attribute doesn't support clone: " + e.toString()); + } // Add reference to the Attribute that the attribute doc is associated with // in order to generate documentation for the Attribute. bestAttributeDoc.setAttribute(attribute);
diff --git a/src/main/java/com/google/devtools/build/docgen/RuleDocumentationAttribute.java b/src/main/java/com/google/devtools/build/docgen/RuleDocumentationAttribute.java index cac1303..2fde0f1 100644 --- a/src/main/java/com/google/devtools/build/docgen/RuleDocumentationAttribute.java +++ b/src/main/java/com/google/devtools/build/docgen/RuleDocumentationAttribute.java
@@ -29,13 +29,14 @@ import java.util.Set; /** - * A class storing a rule attribute documentation along with some meta information. - * The class provides functionality to compute the ancestry level of this attribute's - * generator rule definition class compared to other rule definition classes. + * A class storing a rule attribute documentation along with some meta information. The class + * provides functionality to compute the ancestry level of this attribute's generator rule + * definition class compared to other rule definition classes. * * <p>Warning, two RuleDocumentationAttribute objects are equal based on only the attributeName. */ -public class RuleDocumentationAttribute implements Comparable<RuleDocumentationAttribute> { +public class RuleDocumentationAttribute + implements Comparable<RuleDocumentationAttribute>, Cloneable { private static final ImmutableMap<Type<?>, String> TYPE_DESC = ImmutableMap.<Type<?>, String>builder() @@ -70,6 +71,7 @@ private Set<String> flags; private Attribute attribute; + /** * Creates common RuleDocumentationAttribute such as deps or data. * These attribute docs have no definitionClass or htmlDocumentation (it's in the BE header). @@ -102,6 +104,12 @@ this.startLineCnt = startLineCnt; this.flags = flags; this.commonType = commonType; + this.fileName = fileName; + } + + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); } /** @@ -118,6 +126,11 @@ return attributeName; } + /** Returns the file name where the rule attribute is defined. */ + public String getFileName() { + return fileName; + } + /** * Returns whether this attribute is marked as deprecated. */