Search in sources :

Example 6 with QueryExpression

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;
}
Also used : Target(com.google.devtools.build.lib.packages.Target) TargetNotFoundException(com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetNotFoundException) ArrayList(java.util.ArrayList) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule) AggregatingAttributeMapper(com.google.devtools.build.lib.packages.AggregatingAttributeMapper)

Example 7 with QueryExpression

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));
}
Also used : QueryException(com.google.devtools.build.lib.query2.engine.QueryException) TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) ImmutableSet(com.google.common.collect.ImmutableSet) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 8 with QueryExpression

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;
}
Also used : Argument(com.google.devtools.build.lib.query2.engine.QueryEnvironment.Argument) ImmutableList(com.google.common.collect.ImmutableList)

Example 9 with QueryExpression

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);
}
Also used : TokenKind(com.google.devtools.build.lib.query2.engine.Lexer.TokenKind) ArrayList(java.util.ArrayList)

Example 10 with QueryExpression

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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QueryEvalResult(com.google.devtools.build.lib.query2.engine.QueryEvalResult) IOException(java.io.IOException) QueryException(com.google.devtools.build.lib.query2.engine.QueryException) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException)

Aggregations

QueryException (com.google.devtools.build.lib.query2.engine.QueryException)5 IOException (java.io.IOException)4 ImmutableList (com.google.common.collect.ImmutableList)3 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)3 Target (com.google.devtools.build.lib.packages.Target)3 QueryEvalResult (com.google.devtools.build.lib.query2.engine.QueryEvalResult)3 QueryExpression (com.google.devtools.build.lib.query2.engine.QueryExpression)3 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)2 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)2 ArrayList (java.util.ArrayList)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)1 NoBuildEvent (com.google.devtools.build.lib.analysis.NoBuildEvent)1 Label (com.google.devtools.build.lib.cmdline.Label)1 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 TargetPattern (com.google.devtools.build.lib.cmdline.TargetPattern)1 ThreadSafe (com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)1 AggregatingAttributeMapper (com.google.devtools.build.lib.packages.AggregatingAttributeMapper)1 BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)1