diff options
author | 2017-07-05 10:25:01 -0400 | |
---|---|---|
committer | 2017-07-05 10:59:40 -0400 | |
commit | 6ff634d3f3582c74190a5dd5051a4b0253aec604 (patch) | |
tree | 0f3756c6b63539c17c409b5d8893c447b015017a /src/main/java/com/google/devtools/build/lib/query2 | |
parent | fd04ce8e20c62acc357a9473dcde727a413e915e (diff) |
Clean up string representations for labels
If --incompatible_descriptive_string_representations is passed, labels are converted
to strings using `repr` differently: `Label("//package:name")` instead of
`"//package:name"`
This CL doesn't affect representations of other object types but provides the
necessary infrastructure for it.
PiperOrigin-RevId: 160955284
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/query2')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java index 29cfb47c57..acc5a54644 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java @@ -466,7 +466,8 @@ public abstract class OutputFormatter implements Serializable { if (attributeMap.isConfigurable(attr.getName())) { // We don't know the actual value for configurable attributes, so we reconstruct // the select without trying to resolve it. - printStream.printf(outputAttributePattern, + printStream.printf( + outputAttributePattern, attr.getPublicName(), outputConfigurableAttrValue(rule, attributeMap, attr)); continue; @@ -479,20 +480,17 @@ public abstract class OutputFormatter implements Serializable { // Computed defaults that depend on configurable attributes can have multiple values. continue; } - printStream.printf(outputAttributePattern, + printStream.printf( + outputAttributePattern, attr.getPublicName(), outputAttrValue(Iterables.getOnlyElement(values))); } printStream.printf(")\n%s", lineTerm); } - /** - * Returns the given attribute value with BUILD output syntax. Does not support selects. - */ + /** Returns the given attribute value with BUILD output syntax. Does not support selects. */ private String outputAttrValue(Object value) { - if (value instanceof Label) { - value = ((Label) value).getDefaultCanonicalForm(); - } else if (value instanceof License) { + if (value instanceof License) { List<String> licenseTypes = new ArrayList<>(); for (License.LicenseType licenseType : ((License) value).getLicenseTypes()) { licenseTypes.add(licenseType.toString().toLowerCase()); @@ -504,7 +502,7 @@ public abstract class OutputFormatter implements Serializable { } else if (value instanceof TriState) { value = ((TriState) value).toInt(); } - return Printer.repr(value); + return new LabelPrinter().repr(value).toString(); } /** @@ -513,14 +511,16 @@ public abstract class OutputFormatter implements Serializable { * <p>Since query doesn't know which select path should be chosen, this doesn't try to * resolve the final value. Instead it just reconstructs the select. */ - private String outputConfigurableAttrValue(Rule rule, RawAttributeMapper attributeMap, - Attribute attr) { + private String outputConfigurableAttrValue( + Rule rule, RawAttributeMapper attributeMap, Attribute attr) { List<String> selectors = new ArrayList<>(); - for (BuildType.Selector<?> selector : ((BuildType.SelectorList<?>) - attributeMap.getRawAttributeValue(rule, attr)).getSelectors()) { + for (BuildType.Selector<?> selector : + ((BuildType.SelectorList<?>) attributeMap.getRawAttributeValue(rule, attr)) + .getSelectors()) { if (selector.isUnconditional()) { - selectors.add(outputAttrValue( - Iterables.getOnlyElement(selector.getEntries().entrySet()).getValue())); + selectors.add( + outputAttrValue( + Iterables.getOnlyElement(selector.getEntries().entrySet()).getValue())); } else { selectors.add(String.format("select(%s)", outputAttrValue(selector.getEntries()))); } @@ -834,4 +834,16 @@ public abstract class OutputFormatter implements Serializable { target.getPackage().getNameFragment()) : location.print(); } + + private static class LabelPrinter extends Printer.BasePrinter { + @Override + public LabelPrinter repr(Object o) { + if (o instanceof Label) { + writeString(((Label) o).getCanonicalForm()); + } else { + super.repr(o); + } + return this; + } + } } |