aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Marcel Hlopko <hlopko@google.com>2017-01-10 13:29:55 +0000
committerGravatar Marcel Hlopko <hlopko@google.com>2017-01-10 14:31:41 +0000
commit25a275ae595f312110f663ea98a32cf6562deb80 (patch)
treea8ed73ba3bd68b493656c740cd1c675dc0034ac3 /src
parent03cb310e8baa5d8ebfbdd4717b2e2de5e623694a (diff)
Refactor CcToolchainFeatures.Variables.lookupVariable to produce less garbage
-- PiperOrigin-RevId: 144072456 MOS_MIGRATED_REVID=144072456
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java96
1 files changed, 47 insertions, 49 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java
index c54afbbd98..d3e83505ef 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java
@@ -33,7 +33,6 @@ import com.google.devtools.build.lib.analysis.config.InvalidConfigurationExcepti
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.VariableValue;
-import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
import java.io.IOException;
@@ -1519,12 +1518,7 @@ public class CcToolchainFeatures implements Serializable {
* accessing a field of non-structured variable
*/
public VariableValue getVariable(String name) {
- Pair<VariableValue, String> pair = lookupVariable(name);
- if (pair.getFirst() == null) {
- throw new ExpansionException(pair.getSecond());
- } else {
- return pair.getFirst();
- }
+ return lookupVariable(name, true);
}
/**
@@ -1534,83 +1528,87 @@ public class CcToolchainFeatures implements Serializable {
* @return Pair<VariableValue, String> returns either (variable value, null) or (null, string
* reason why variable was not found)
*/
- private Pair<VariableValue, String> lookupVariable(String name) {
- Pair<VariableValue, String> nonStructuredPair = getNonStructuredVariable(name);
- if (nonStructuredPair.getFirst() != null) {
- return nonStructuredPair;
- }
- Pair<VariableValue, String> structuredPair = getStructureVariable(name);
- if (structuredPair.getFirst() != null || structuredPair.getSecond() != null) {
- return structuredPair;
+ private VariableValue lookupVariable(String name, boolean throwOnMissingVariable) {
+ VariableValue nonStructuredVariable = getNonStructuredVariable(name);
+ if (nonStructuredVariable != null) {
+ return nonStructuredVariable;
+ }
+ VariableValue structuredVariable = getStructureVariable(name, throwOnMissingVariable);
+ if (structuredVariable != null) {
+ return structuredVariable;
+ } else if (throwOnMissingVariable) {
+ throw new ExpansionException(
+ String.format(
+ "Invalid toolchain configuration: Cannot find variable named '%s'.", name));
} else {
- return nonStructuredPair;
+ return null;
}
}
- public String getStringVariable(String variableName) {
- return getVariable(variableName).getStringValue(variableName);
- }
-
- public Iterable<? extends VariableValue> getSequenceVariable(String variableName) {
- return getVariable(variableName).getSequenceValue(variableName);
- }
-
- private Pair<VariableValue, String> getNonStructuredVariable(String name) {
+ private VariableValue getNonStructuredVariable(String name) {
if (variablesMap.containsKey(name)) {
- return Pair.of(variablesMap.get(name), null);
+ return variablesMap.get(name);
}
if (stringVariablesMap.containsKey(name)) {
- return Pair.<VariableValue, String>of(new StringValue(stringVariablesMap.get(name)), null);
+ return new StringValue(stringVariablesMap.get(name));
}
if (parent != null) {
return parent.getNonStructuredVariable(name);
}
- return Pair.of(
- null,
- String.format("Invalid toolchain configuration: Cannot find variable named '%s'.", name));
+ return null;
}
- private Pair<VariableValue, String> getStructureVariable(String name) {
+ private VariableValue getStructureVariable(String name, boolean throwOnMissingVariable) {
if (!name.contains(".")) {
- return Pair.of(null, null);
+ return null;
}
Stack<String> fieldsToAccess = new Stack<>();
String structPath = name;
- Pair<VariableValue, String> pair;
+ VariableValue variable;
do {
fieldsToAccess.push(structPath.substring(structPath.lastIndexOf('.') + 1));
structPath = structPath.substring(0, structPath.lastIndexOf('.'));
- pair = getNonStructuredVariable(structPath);
- } while (pair.getFirst() == null && structPath.contains("."));
+ variable = getNonStructuredVariable(structPath);
+ } while (variable == null && structPath.contains("."));
- if (pair.getFirst() == null) {
- return pair;
+ if (variable == null) {
+ return null;
}
- VariableValue structure = pair.getFirst();
while (!fieldsToAccess.empty()) {
String field = fieldsToAccess.pop();
- structure = structure.getFieldValue(structPath, field);
- if (structure == null) {
- return Pair.of(
- null,
- String.format(
- "Invalid toolchain configuration: Cannot expand variable '%s.%s': structure %s "
- + "doesn't have a field named '%s'",
- structPath, field, structPath, field));
+ variable = variable.getFieldValue(structPath, field);
+ if (variable == null) {
+ if (throwOnMissingVariable) {
+ throw new ExpansionException(
+ String.format(
+ "Invalid toolchain configuration: Cannot expand variable '%s.%s': structure %s "
+ + "doesn't have a field named '%s'",
+ structPath, field, structPath, field));
+ } else {
+ return null;
+ }
}
}
- return Pair.of(structure, null);
+ return variable;
+ }
+
+ public String getStringVariable(String variableName) {
+ return getVariable(variableName).getStringValue(variableName);
+ }
+
+ public Iterable<? extends VariableValue> getSequenceVariable(String variableName) {
+ return getVariable(variableName).getSequenceValue(variableName);
}
private String guessIteratedOverVariable(ImmutableSet<String> usedVariables) {
String sequenceName = null;
for (String usedVariable : usedVariables) {
- VariableValue variableValue = lookupVariable(usedVariable).getFirst();
+ VariableValue variableValue = lookupVariable(usedVariable, false);
if (variableValue != null && variableValue.isSequence()) {
if (sequenceName != null) {
throw new ExpansionException(
@@ -1630,7 +1628,7 @@ public class CcToolchainFeatures implements Serializable {
/** Returns whether {@code variable} is set. */
boolean isAvailable(String variable) {
- return lookupVariable(variable).getFirst() != null;
+ return lookupVariable(variable, false) != null;
}
}