use of com.google.devtools.build.lib.query2.engine.QueryExpression in project bazel by bazelbuild.
the class BlazeTargetAccessor method getLabelListAttr.
@Override
public List<Target> getLabelListAttr(QueryExpression caller, Target target, String attrName, String errorMsgPrefix) throws QueryException, InterruptedException {
Preconditions.checkArgument(target instanceof Rule);
List<Target> result = new ArrayList<>();
Rule rule = (Rule) target;
AggregatingAttributeMapper attrMap = AggregatingAttributeMapper.of(rule);
Type<?> attrType = attrMap.getAttributeType(attrName);
if (attrType == null) {
// Return an empty list if the attribute isn't defined for this rule.
return ImmutableList.of();
}
for (Label label : attrMap.getReachableLabels(attrName, false)) {
try {
result.add(queryEnvironment.getTarget(label));
} catch (TargetNotFoundException e) {
queryEnvironment.reportBuildFileError(caller, errorMsgPrefix + e.getMessage());
}
}
return result;
}
use of com.google.devtools.build.lib.query2.engine.QueryExpression in project bazel by bazelbuild.
the class SkyQueryEnvironment method getTargetsMatchingPattern.
@ThreadSafe
@Override
public QueryTaskFuture<Void> getTargetsMatchingPattern(final QueryExpression owner, String pattern, Callback<Target> callback) {
// Directly evaluate the target pattern, making use of packages in the graph.
Pair<TargetPattern, ImmutableSet<PathFragment>> patternToEvalAndSubdirectoriesToExclude;
try {
patternToEvalAndSubdirectoriesToExclude = getPatternAndExcludes(pattern);
} catch (TargetParsingException tpe) {
try {
reportBuildFileError(owner, tpe.getMessage());
} catch (QueryException qe) {
return immediateFailedFuture(qe);
}
return immediateSuccessfulFuture(null);
} catch (InterruptedException ie) {
return immediateCancelledFuture();
}
TargetPattern patternToEval = patternToEvalAndSubdirectoriesToExclude.getFirst();
ImmutableSet<PathFragment> subdirectoriesToExclude = patternToEvalAndSubdirectoriesToExclude.getSecond();
AsyncFunction<TargetParsingException, Void> reportBuildFileErrorAsyncFunction = new AsyncFunction<TargetParsingException, Void>() {
@Override
public ListenableFuture<Void> apply(TargetParsingException exn) throws QueryException {
reportBuildFileError(owner, exn.getMessage());
return Futures.immediateFuture(null);
}
};
ListenableFuture<Void> evalFuture = patternToEval.evalAsync(resolver, subdirectoriesToExclude, callback, QueryException.class, executor);
return QueryTaskFutureImpl.ofDelegate(Futures.catchingAsync(evalFuture, TargetParsingException.class, reportBuildFileErrorAsyncFunction));
}
use of com.google.devtools.build.lib.query2.engine.QueryExpression in project bazel by bazelbuild.
the class QueryExpressionMapper method map.
public QueryExpression map(FunctionExpression functionExpression) {
boolean changed = false;
ImmutableList.Builder<Argument> mappedArgumentBuilder = ImmutableList.builder();
for (Argument argument : functionExpression.getArgs()) {
switch(argument.getType()) {
case EXPRESSION:
{
QueryExpression expr = argument.getExpression();
QueryExpression mappedExpression = expr.getMapped(this);
mappedArgumentBuilder.add(Argument.of(mappedExpression));
if (expr != mappedExpression) {
changed = true;
}
break;
}
default:
mappedArgumentBuilder.add(argument);
break;
}
}
return changed ? new FunctionExpression(functionExpression.getFunction(), mappedArgumentBuilder.build()) : functionExpression;
}
use of com.google.devtools.build.lib.query2.engine.QueryExpression in project bazel by bazelbuild.
the class QueryParser method parseBinaryOperatorTail.
/**
* tail ::= ( <op> <primary> )*
* All operators have equal precedence.
* This factoring is required for left-associative binary operators in LL(1).
*/
private QueryExpression parseBinaryOperatorTail(QueryExpression lhs) throws QueryException {
if (!BINARY_OPERATORS.contains(token.kind)) {
return lhs;
}
List<QueryExpression> operands = new ArrayList<>();
operands.add(lhs);
TokenKind lastOperator = token.kind;
while (BINARY_OPERATORS.contains(token.kind)) {
TokenKind operator = token.kind;
consume(operator);
if (operator != lastOperator) {
lhs = new BinaryOperatorExpression(lastOperator, operands);
operands.clear();
operands.add(lhs);
lastOperator = operator;
}
QueryExpression rhs = parsePrimary();
operands.add(rhs);
}
return new BinaryOperatorExpression(lastOperator, operands);
}
use of com.google.devtools.build.lib.query2.engine.QueryExpression in project bazel by bazelbuild.
the class AbstractBlazeQueryEnvironment method evaluateQuery.
/**
* Evaluate the specified query expression in this environment, streaming results to the given
* {@code callback}. {@code callback.start()} will be called before query evaluation and
* {@code callback.close()} will be unconditionally called at the end of query evaluation
* (i.e. regardless of whether it was successful).
*
* @return a {@link QueryEvalResult} object that contains the resulting set of targets and a bit
* to indicate whether errors occurred during evaluation; note that the
* success status can only be false if {@code --keep_going} was in effect
* @throws QueryException if the evaluation failed and {@code --nokeep_going} was in
* effect
*/
public QueryEvalResult evaluateQuery(QueryExpression expr, ThreadSafeOutputFormatterCallback<T> callback) throws QueryException, InterruptedException, IOException {
EmptinessSensingCallback<T> emptySensingCallback = new EmptinessSensingCallback<>(callback);
long startTime = System.currentTimeMillis();
// In the --nokeep_going case, errors are reported in the order in which the patterns are
// specified; using a linked hash set here makes sure that the left-most error is reported.
Set<String> targetPatternSet = new LinkedHashSet<>();
expr.collectTargetPatterns(targetPatternSet);
try {
preloadOrThrow(expr, targetPatternSet);
} catch (TargetParsingException e) {
// Unfortunately, by evaluating the patterns in parallel, we lose some location information.
throw new QueryException(expr, e.getMessage());
}
IOException ioExn = null;
boolean failFast = true;
try {
callback.start();
evalTopLevelInternal(expr, emptySensingCallback);
failFast = false;
} catch (QueryException e) {
throw new QueryException(e, expr);
} catch (InterruptedException e) {
throw e;
} finally {
try {
callback.close(failFast);
} catch (IOException e) {
// Only throw this IOException if we weren't about to throw a different exception.
ioExn = e;
}
}
if (ioExn != null) {
throw ioExn;
}
long elapsedTime = System.currentTimeMillis() - startTime;
if (elapsedTime > 1) {
logger.info("Spent " + elapsedTime + " milliseconds evaluating query");
}
if (eventHandler.hasErrors()) {
if (!keepGoing) {
// of target patterns that don't cause evaluation to fail per se.
throw new QueryException("Evaluation of query \"" + expr + "\" failed due to BUILD file errors");
} else {
eventHandler.handle(Event.warn("--keep_going specified, ignoring errors. " + "Results may be inaccurate"));
}
}
return new QueryEvalResult(!eventHandler.hasErrors(), emptySensingCallback.isEmpty());
}
Aggregations